diff --git a/basis/power.lua b/basis/power.lua index 9cd550e..2af5ce4 100644 --- a/basis/power.lua +++ b/basis/power.lua @@ -267,7 +267,8 @@ function techage.consumer.turn_power_on(pos, power_consumption) local mem = tubelib2.get_mem(pos) mem.power_consumption = power_consumption -- 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 consumer state machine can be handled before + minetest.after(0.2, start_network_power_consumption, pos, mem.power_dir) end -- Power network callback function diff --git a/electric/consumer.lua b/electric/consumer.lua index d83b555..946fa79 100644 --- a/electric/consumer.lua +++ b/electric/consumer.lua @@ -7,38 +7,33 @@ local M = minetest.get_meta local MP = minetest.get_modpath("tubelib2") local I,_ = dofile(MP.."/intllib.lua") - -local POWER_CONSUME = 4 +local POWER_CONSUMPTION = 2 local STANDBY_TICKS = 4 -local COUNTDOWN_TICKS = 4 local CYCLE_TIME = 4 +local Cable = techage.ElectricCable +local consumer = techage.consumer + 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").."]".. "list[current_player;main;0,3;8,4;]".. default.get_hotbar_bg(0, 3) end -local function can_start(pos, mem, state) - -- signal power consumption - mem.power_consume = POWER_CONSUME - -- goto RUNNING or FAULT, depending on power supply - return mem.power_supply +local function valid_power_dir(pos, mem, in_dir) + return mem.power_dir == in_dir end local function start_node(pos, mem, state) - techage.consumer_check_power_consumption(pos) + consumer.turn_power_on(pos, POWER_CONSUMPTION) end local function stop_node(pos, mem, state) - mem.power_consume = 0 - techage.consumer_check_power_consumption(pos) + consumer.turn_power_on(pos, 0) end local State = techage.NodeStates:new({ @@ -47,37 +42,28 @@ local State = techage.NodeStates:new({ cycle_time = CYCLE_TIME, standby_ticks = STANDBY_TICKS, formspec_func = formspec, - can_start = can_start, start_node = start_node, stop_node = stop_node, }) -local function node_timer(pos, elapsed) - --print("node_timer") +local function lamp_turn_on_clbk(pos, in_dir, sum) local mem = tubelib2.get_mem(pos) - return State:is_active(mem) -end - --- To be able to check if power connection is on the --- correct node side (mem.power_dir == in_dir) -local function valid_power_dir(pos, mem, in_dir) - --print("valid_power_dir", mem.power_dir, in_dir) - return true -end - -local function turn_power_on(pos, in_dir, on) - local mem = tubelib2.get_mem(pos) - mem.power_supply = on local state = State:get_state(mem) - if on and state == techage.FAULT then + if sum > 0 and state == techage.FAULT then State:stop(pos, mem) State:start(pos, mem) - elseif not on and state == techage.RUNNING then + elseif sum <= 0 and state == techage.RUNNING then State:fault(pos, mem) end end +local function node_timer(pos, elapsed) + print("node_timer") + local mem = tubelib2.get_mem(pos) + return State:is_active(mem) +end + local function on_receive_fields(pos, formname, fields, player) if minetest.is_protected(pos, player:get_player_name()) then return @@ -102,32 +88,30 @@ minetest.register_node("techage:ele_consumer", { 'techage_electric_button.png', 'techage_electric_button.png', 'techage_electric_button.png', - 'techage_electric_button.png', + 'techage_electric_button.png^techage_electric_plug.png', 'techage_electric_button.png', 'techage_electric_button.png', }, techage = { - power_consumption = techage.consumer_power_consumption, - power_network = techage.ElectricCable, + turn_on = lamp_turn_on_clbk, + read_power_consumption = consumer.read_power_consumption, + power_network = Cable, power_side = "L", valid_power_dir = valid_power_dir, - turn_on = turn_power_on, }, after_place_node = function(pos, placer) - local mem = techage.consumer_after_place_node(pos, placer) - mem.power_consume = 0 - mem.power_supply = false - on_rightclick(pos) + local mem = consumer.after_place_node(pos, placer) State:node_init(pos, mem, "") + on_rightclick(pos) end, after_dig_node = function(pos, oldnode, oldmetadata, digger) State:after_dig_node(pos, oldnode, oldmetadata, digger) - techage.consumer_after_dig_node(pos, oldnode) + consumer.after_dig_node(pos, oldnode) end, - after_tube_update = techage.consumer_after_tube_update, + after_tube_update = consumer.after_tube_update, on_timer = node_timer, on_receive_fields = on_receive_fields, on_rightclick = on_rightclick, @@ -147,19 +131,19 @@ minetest.register_node("techage:ele_consumer_on", { 'techage_electric_button.png', }, techage = { - power_consumption = techage.consumer_power_consumption, - power_network = techage.ElectricCable, + turn_on = lamp_turn_on_clbk, + read_power_consumption = consumer.read_power_consumption, + power_network = Cable, power_side = "L", valid_power_dir = valid_power_dir, - turn_on = turn_power_on, }, after_dig_node = function(pos, oldnode, oldmetadata, digger) State:after_dig_node(pos, oldnode, oldmetadata, digger) - techage.consumer_after_dig_node(pos, oldnode) + consumer.after_dig_node(pos, oldnode) end, - after_tube_update = techage.consumer_after_tube_update, + after_tube_update = consumer.after_tube_update, on_timer = node_timer, on_receive_fields = on_receive_fields, on_rightclick = on_rightclick, diff --git a/electric/generator.lua b/electric/generator.lua index f01f9f1..6add4d0 100644 --- a/electric/generator.lua +++ b/electric/generator.lua @@ -40,7 +40,6 @@ local State = techage.NodeStates:new({ cycle_time = CYCLE_TIME, standby_ticks = STANDBY_TICKS, formspec_func = formspec, - --can_start = can_start, start_node = start_node, stop_node = stop_node, }) @@ -59,6 +58,7 @@ local function turn_power_on(pos, in_dir, sum) if State:is_active(mem) and sum <= 0 then State:fault(pos, mem) end + M(pos):set_string("formspec", formspec(State, pos, mem)) end local function on_receive_fields(pos, formname, fields, player)