diff --git a/basis/power.lua b/basis/power.lua index 89ffc7e..9cd550e 100644 --- a/basis/power.lua +++ b/basis/power.lua @@ -179,7 +179,8 @@ function techage.generator.turn_power_on(pos, power_capacity) local mem = tubelib2.get_mem(pos) mem.power_capacity = power_capacity -- Starts the overall power consumption and depending on that turns all nodes on/off - start_network_power_consumption(pos, mem.power_dir) + -- To be called delayed, so that the generator state machine can be handled before + minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir) end -- Power network callback function @@ -197,8 +198,8 @@ function techage.generator.after_dig_node(pos, oldnode) tubelib2.del_mem(pos) end -function techage.generator.formspec_level(mem) - local percent = ((mem.power_result or 0) * 100) / (mem.power_produce or 1) +function techage.generator.formspec_level(mem, sum) + local percent = ((sum or 0) * 100) / (mem.power_capacity or 1) return "techage_form_level_bg.png^[lowpart:"..percent..":techage_form_level_fg.png]" end diff --git a/electric/generator.lua b/electric/generator.lua index 24f0873..f01f9f1 100644 --- a/electric/generator.lua +++ b/electric/generator.lua @@ -10,82 +10,57 @@ local I,_ = dofile(MP.."/intllib.lua") local STANDBY_TICKS = 4 local COUNTDOWN_TICKS = 4 local CYCLE_TIME = 16 +local POWER_CAPACITY = 8 + +local Cable = techage.ElectricCable +local generator = techage.generator local function formspec(self, pos, mem) return "size[8,7]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. - "image[2,0.5;1,2;"..techage.generator_formspec_level(mem).. - "image_button[3,1.2;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. - "button[5.5,1.2;1.8,1;update;"..I("Update").."]".. + "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 can_start(pos, mem, state) - print("can_start") - local sum = techage.calc_power_consumption(pos, mem, 8) - print("sum", sum) - if sum > 0 then - M(pos):set_string("infotext", "On:"..sum.." / "..8) - return true - end - return false -end - local function start_node(pos, mem, state) - print("start_node") - techage.generator_on(pos, mem) + generator.turn_power_on(pos, POWER_CAPACITY) end local function stop_node(pos, mem, state) - techage.generator_off(pos, mem) - M(pos):set_string("infotext", "Off") + generator.turn_power_on(pos, 0) end local State = techage.NodeStates:new({ node_name_passive = "techage:generator", cycle_time = CYCLE_TIME, standby_ticks = STANDBY_TICKS, - has_item_meter = true, - aging_factor = 10, formspec_func = formspec, - can_start = can_start, + --can_start = can_start, start_node = start_node, stop_node = stop_node, }) -local function distibuting(pos, mem) - local sum = techage.calc_power_consumption(pos, mem, 8) - if sum > 0 then - State:keep_running(pos, mem, COUNTDOWN_TICKS) - else - State:fault(pos, mem) - end - M(pos):set_string("infotext", "On:"..sum.." / "..8) -end - local function node_timer(pos, elapsed) - --print("node_timer") + print("node_timer") local mem = tubelib2.get_mem(pos) - distibuting(pos, mem) return State:is_active(mem) end -local function valid_power_dir(pos, mem, in_dir) - return mem.power_dir == in_dir -end - -local function turn_power_on(pos, in_dir, on) +local function turn_power_on(pos, in_dir, sum) local mem = tubelib2.get_mem(pos) - if State:is_active(mem) and not on then + -- store result for formspec + mem.power_result = sum + if State:is_active(mem) and sum <= 0 then State:fault(pos, mem) end end - local function on_receive_fields(pos, formname, fields, player) if minetest.is_protected(pos, player:get_player_name()) then return @@ -120,26 +95,22 @@ minetest.register_node("techage:generator", { is_ground_content = false, techage = { - power_consumption = techage.generator_power_consumption, - power_network = techage.ElectricCable, - power_consume = 0, - valid_power_dir = valid_power_dir, turn_on = turn_power_on, + read_power_consumption = generator.read_power_consumption, + power_network = Cable, }, after_place_node = function(pos, placer) - local mem = techage.generator_after_place_node(pos) + local mem = generator.after_place_node(pos) State:node_init(pos, mem, "") end, after_dig_node = function(pos, oldnode, oldmetadata, digger) State:after_dig_node(pos, oldnode, oldmetadata, digger) - techage.generator_after_dig_node(pos, oldnode) + generator.after_dig_node(pos, oldnode) end, - after_tube_update = techage.generator_after_tube_update, - on_destruct = techage.generator_on_destruct, - on_timer = node_timer, + after_tube_update = generator.after_tube_update, on_receive_fields = on_receive_fields, on_rightclick = on_rightclick, }) diff --git a/electric/test.lua b/electric/test.lua index 3614675..351e225 100644 --- a/electric/test.lua +++ b/electric/test.lua @@ -37,18 +37,14 @@ end local function lamp_on_rightclick(pos, node, clicker) local mem = tubelib2.get_mem(pos) if not mem.running then - -- Note the order!!! swap_node(pos, "techage:lamp_on") mem.running = true M(pos):set_string("infotext", "On") - -- last command!!! consumer.turn_power_on(pos, POWER_CONSUMPTION) else - -- Note the order!!! swap_node(pos, "techage:lamp") mem.running = false M(pos):set_string("infotext", "Off") - -- last command!!! consumer.turn_power_on(pos, 0) end end @@ -129,13 +125,10 @@ end local function generator_on_rightclick(pos, node, clicker) local mem = tubelib2.get_mem(pos) if not mem.running then - -- Note the order!!! mem.running = true M(pos):set_string("infotext", "On") - -- last command!!! generator.turn_power_on(pos, POWER_CAPACITY) else - -- Note the order!!! generator.turn_power_on(pos, 0) mem.running = false M(pos):set_string("infotext", "Off") diff --git a/init.lua b/init.lua index 655a8e5..b3a1ad7 100644 --- a/init.lua +++ b/init.lua @@ -30,8 +30,8 @@ dofile(MP.."/basis/junction.lua") -- network junction box dofile(MP.."/electric/electric_cable.lua") dofile(MP.."/electric/test.lua") ---dofile(MP.."/electric/generator.lua") ---dofile(MP.."/electric/consumer.lua") +dofile(MP.."/electric/generator.lua") +dofile(MP.."/electric/consumer.lua") --dofile(MP.."/fermenter/biogas_pipe.lua")