techage/test/generator.lua

125 lines
3.4 KiB
Lua
Raw Normal View History

-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local P = minetest.string_to_pos
local M = minetest.get_meta
-- Load support for intllib.
2019-03-17 17:33:13 +03:00
local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua")
local STANDBY_TICKS = 4
local COUNTDOWN_TICKS = 4
2019-03-05 23:20:44 +03:00
local CYCLE_TIME = 16
2019-03-08 20:16:44 +03:00
local POWER_CAPACITY = 8
2019-05-05 23:29:00 +03:00
--local Power = techage.ElectricCable
local Power = techage.SteamPipe
2019-03-08 20:16:44 +03:00
local generator = techage.generator
local function formspec(self, pos, mem)
return "size[8,7]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
2019-03-08 20:16:44 +03:00
"image[6,0.5;1,2;"..generator.formspec_level(mem, mem.power_result)..
"image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]"..
"button[2.5,1;1.8,1;update;"..I("Update").."]"..
"list[current_player;main;0,3;8,4;]"..
default.get_hotbar_bg(0, 3)
end
local function start_node(pos, mem, state)
2019-03-08 20:16:44 +03:00
generator.turn_power_on(pos, POWER_CAPACITY)
end
local function stop_node(pos, mem, state)
2019-03-08 20:16:44 +03:00
generator.turn_power_on(pos, 0)
end
local State = techage.NodeStates:new({
2019-03-08 01:12:07 +03:00
node_name_passive = "techage:generator",
cycle_time = CYCLE_TIME,
standby_ticks = STANDBY_TICKS,
formspec_func = formspec,
start_node = start_node,
stop_node = stop_node,
})
local function node_timer(pos, elapsed)
2019-03-08 20:16:44 +03:00
print("node_timer")
local mem = tubelib2.get_mem(pos)
return State:is_active(mem)
end
2019-03-08 20:16:44 +03:00
local function turn_power_on(pos, in_dir, sum)
2019-03-05 23:20:44 +03:00
local mem = tubelib2.get_mem(pos)
2019-03-08 20:16:44 +03:00
-- store result for formspec
mem.power_result = sum
if State:is_active(mem) and sum <= 0 then
2019-03-05 23:20:44 +03:00
State:fault(pos, mem)
2019-03-08 23:18:55 +03:00
-- No automatic turn on
mem.power_capacity = 0
2019-03-05 23:20:44 +03:00
end
2019-03-08 20:44:23 +03:00
M(pos):set_string("formspec", formspec(State, pos, mem))
2019-03-05 23:20:44 +03:00
end
local function on_receive_fields(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local mem = tubelib2.get_mem(pos)
State:state_button_event(pos, mem, fields)
if fields.update then
M(pos):set_string("formspec", formspec(State, pos, mem))
end
end
local function on_rightclick(pos)
local mem = tubelib2.get_mem(pos)
M(pos):set_string("formspec", formspec(State, pos, mem))
end
2019-03-08 01:12:07 +03:00
minetest.register_node("techage:generator", {
description = "TechAge Generator",
tiles = {
-- up, down, right, left, back, front
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png^techage_electric_plug.png',
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png',
'techage_electric_button.png^techage_electric_power.png',
},
paramtype2 = "facedir",
groups = {cracky=2, crumbly=2, choppy=2},
on_rotate = screwdriver.disallow,
is_ground_content = false,
techage = {
2019-03-05 23:20:44 +03:00
turn_on = turn_power_on,
2019-03-08 20:16:44 +03:00
read_power_consumption = generator.read_power_consumption,
2019-05-05 23:29:00 +03:00
power_network = Power,
2019-03-09 16:23:35 +03:00
power_side = "R",
},
after_place_node = function(pos, placer)
2019-03-08 20:16:44 +03:00
local mem = generator.after_place_node(pos)
State:node_init(pos, mem, "")
2019-03-08 23:18:55 +03:00
on_rightclick(pos)
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
State:after_dig_node(pos, oldnode, oldmetadata, digger)
2019-03-08 20:16:44 +03:00
generator.after_dig_node(pos, oldnode)
end,
2019-03-08 20:16:44 +03:00
after_tube_update = generator.after_tube_update,
on_receive_fields = on_receive_fields,
on_rightclick = on_rightclick,
2019-03-08 23:18:55 +03:00
on_timer = node_timer,
})
2019-05-05 23:29:00 +03:00
Power:add_secondary_node_names({"techage:generator"})