diff --git a/basic_machines/source.lua b/basic_machines/source.lua index d8e9156..ba0e9e1 100644 --- a/basic_machines/source.lua +++ b/basic_machines/source.lua @@ -20,9 +20,7 @@ local S = techage.S local TA2_Power = techage.Axle local TA3_Power = techage.SteamPipe local TA4_Power = techage.ElectricCable -local provide_power = techage.power.provide_power -local power_switched = techage.power.power_switched -local power_distribution = techage.power.power_distribution +local power = techage.power local STANDBY_TICKS = 4 local COUNTDOWN_TICKS = 4 @@ -43,14 +41,14 @@ end local function start_node(pos, mem, state) mem.generating = true - power_switched(pos) + power.generator_start(pos, mem, PWR_CAPA) techage.switch_axles(pos, true) end local function stop_node(pos, mem, state) mem.generating = false mem.provided = 0 - power_switched(pos) + power.generator_stop(pos, mem) techage.switch_axles(pos, false) end @@ -81,26 +79,10 @@ local State4 = techage.NodeStates:new({ stop_node = stop_node, }) -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - if mem.generating then - mem.provided = provide_power(pos, PWR_CAPA) - else - mem.provided = 0 - end - mem.trigger = 2 -end - local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) if mem.generating then - power_distribution(pos) - mem.trigger = (mem.trigger or 1) - 1 - if mem.trigger <= 0 then - -- power distribution timeout - --print("source not triggered") - power_switched(pos) - end + local provided = power.generator_alive(pos, mem) end return mem.generating end @@ -216,17 +198,14 @@ minetest.register_node("techage:t4_source", { techage.power.register_node({"techage:t2_source"}, { conn_sides = {"R"}, power_network = TA2_Power, - on_power = on_power, }) techage.power.register_node({"techage:t3_source"}, { conn_sides = {"R"}, power_network = TA3_Power, - on_power = on_power, }) techage.power.register_node({"techage:t4_source"}, { conn_sides = {"R"}, power_network = TA4_Power, - on_power = on_power, }) diff --git a/basis/consumer.lua b/basis/consumer.lua index ea36464..a4ef0d0 100644 --- a/basis/consumer.lua +++ b/basis/consumer.lua @@ -26,53 +26,39 @@ local M = minetest.get_meta local CRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).consumer end local CRDN = function(node) return (minetest.registered_nodes[node.name] or {}).consumer end ---local CYCLE_TIME = 2 -- required from power - -local consume_power = techage.power.consume_power -local power_available = techage.power.power_available +local power = techage.power local function can_start(pos, mem, state) - return power_available(pos, CRD(pos).power_consumption) + return power.power_available(pos, mem, CRD(pos).power_consumption) end local function start_node(pos, mem, state) - mem.conn_next_call = 0 - mem.conn_cycle_timer = 0 + local crd = CRD(pos) + power.consumer_start(pos, mem, crd.cycle_time, crd.power_consumption) end local function stop_node(pos, mem, state) + power.consumer_stop(pos, mem) end -local function on_power(pos) +local function on_power(pos, mem) local crd = CRD(pos) - local mem = tubelib2.get_mem(pos) - local state = mem.techage_state - mem.node_loaded = (mem.node_loaded or 1) - 1 - if mem.node_loaded >= 0 then - if techage.needs_power(mem)then - local got = consume_power(pos, crd.power_consumption) - if got < crd.power_consumption then - crd.State:nopower(pos, mem) - end - elseif state == techage.STANDBY and not power_available(pos) then - crd.State:nopower(pos, mem) - elseif state == techage.NOPOWER and power_available(pos) then - crd.State:start(pos, mem) - end - mem.power_available = true - end + crd.State:start(pos, mem) +end + +local function on_nopower(pos, mem) + local crd = CRD(pos) + crd.State:nopower(pos, mem) end local function node_timer(pos, elapsed) local crd = CRD(pos) local mem = tubelib2.get_mem(pos) local state = mem.techage_state - if crd.power_consumption > 0 and not mem.power_available then - crd.State:nopower(pos, mem) + print("consumer node_timer", techage.needs_power(mem)) + if techage.power_alive(mem) then + power.consumer_alive(pos, mem) end - mem.power_available = false - -- node cycle time / power cycle time + security surcharge - mem.node_loaded = crd.cycle_time/2 + 1 -- call the node timer routine if techage.is_operational(mem) then crd.node_timer(pos, crd.cycle_time) @@ -239,6 +225,7 @@ function techage.register_consumer(base_name, inv_name, tiles, tNode, validState conn_sides = {"F", "B"}, power_network = power_network, on_power = on_power, + on_nopower = on_nopower, }) end techage.register_node({name_pas, name_act}, tNode.tubing) diff --git a/basis/node_states.lua b/basis/node_states.lua index e79d4dd..701298d 100644 --- a/basis/node_states.lua +++ b/basis/node_states.lua @@ -138,11 +138,18 @@ function techage.is_operational(mem) return state < NOPOWER end +-- consumes power function techage.needs_power(mem) local state = mem.techage_state or STOPPED return state < STANDBY end +-- is node alive (power related) +function techage.power_alive(mem) + local state = mem.techage_state or STOPPED + return state < FAULT +end + function NodeStates:new(attr) local o = { -- mandatory diff --git a/coal_power_station/akkubox.lua b/coal_power_station/akkubox.lua index 0465419..61aefd0 100644 --- a/coal_power_station/akkubox.lua +++ b/coal_power_station/akkubox.lua @@ -21,9 +21,7 @@ local PWR_PERF = 10 local PWR_CAPA = 3000 local Power = techage.ElectricCable -local secondary_power = techage.power.secondary_power -local power_switched = techage.power.power_switched -local power_distribution = techage.power.power_distribution +local power = techage.power local function in_range(val, min, max) if val < min then return min end @@ -46,15 +44,15 @@ end local function start_node(pos, mem, state) - mem.generating = true + mem.running = true mem.delivered = 0 - power_switched(pos) + power.secondary_start(pos, mem, PWR_PERF, PWR_PERF) end local function stop_node(pos, mem, state) - mem.generating = false + mem.running = false mem.delivered = 0 - power_switched(pos) + power.secondary_stop(pos, mem) end local State = techage.NodeStates:new({ @@ -66,40 +64,14 @@ local State = techage.NodeStates:new({ stop_node = stop_node, }) -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - mem.capa = mem.capa or 0 - if mem.generating then - local delivered - if mem.capa >= PWR_CAPA then - mem.delivered = secondary_power(pos, PWR_PERF, 0) - elseif mem.capa <= 0 then - mem.delivered = secondary_power(pos, 0, PWR_PERF) - else - mem.delivered = secondary_power(pos, PWR_PERF, PWR_PERF) - end - mem.capa = mem.capa - mem.delivered - mem.capa = in_range(mem.capa, 0, PWR_CAPA) - mem.trigger = 2 - return true - end - mem.delivered = 0 - return false -end - local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) - if mem.generating then - power_distribution(pos) - mem.trigger = (mem.trigger or 1) - 1 - if mem.trigger <= 0 then - power_switched(pos) - end - return true - else - mem.provided = 0 + if mem.running then + mem.delivered = power.secondary_alive(pos, mem, mem.capa, PWR_CAPA) + mem.capa = mem.capa - mem.delivered + mem.capa = in_range(mem.capa, 0, PWR_CAPA) end - return false + return mem.running end local function on_receive_fields(pos, formname, fields, player) diff --git a/coal_power_station/generator.lua b/coal_power_station/generator.lua index d51c455..20ee2e1 100644 --- a/coal_power_station/generator.lua +++ b/coal_power_station/generator.lua @@ -22,16 +22,14 @@ local CYCLE_TIME = 2 local PWR_CAPA = 80 local Cable = techage.ElectricCable -local provide_power = techage.power.provide_power -local power_switched = techage.power.power_switched -local power_distribution = techage.power.power_distribution +local power = techage.power local function formspec(self, pos, mem) return "size[8,7]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. - "image[6,0.5;1,2;"..techage.power.formspec_power_bar(PWR_CAPA, mem.provided).."]".. + "image[6,0.5;1,2;"..power.formspec_power_bar(PWR_CAPA, mem.provided).."]".. "image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. "button[2,1.5;2,1;update;"..S("Update").."]".. "list[current_player;main;0,3;8,4;]".. @@ -39,19 +37,17 @@ local function formspec(self, pos, mem) end local function can_start(pos, mem, state) - return (mem.triggered or 0) > 0 -- by means of firebox + return (mem.firebox_trigger or 0) > 0 -- by means of firebox end local function start_node(pos, mem, state) mem.generating = true -- needed for power distribution - minetest.get_node_timer(pos):start(CYCLE_TIME) - power_switched(pos) + power.generator_start(pos, mem, PWR_CAPA) end local function stop_node(pos, mem, state) mem.generating = false - minetest.get_node_timer(pos):stop() - power_switched(pos) + power.generator_stop(pos, mem) mem.provided = 0 end @@ -67,33 +63,21 @@ local State = techage.NodeStates:new({ stop_node = stop_node, }) -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - if mem.generating then - mem.provided = provide_power(pos, PWR_CAPA) - else - mem.provided = 0 - end - mem.power_available = 2 -end - local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) if mem.generating then - power_distribution(pos) - State:keep_running(pos, mem, COUNTDOWN_TICKS) - mem.power_available = (mem.power_available or 1) - 1 - mem.triggered = (mem.triggered or 1) - 1 - if mem.power_available < 0 or mem.triggered < 0 then - power_switched(pos) - State:stop(pos, mem) + mem.firebox_trigger = (mem.firebox_trigger or 0) - 1 + if mem.firebox_trigger <= 0 then + State:nopower(pos, mem) mem.generating = false + power.generator_stop(pos, mem) mem.provided = 0 + else + mem.provided = power.generator_alive(pos, mem) + State:keep_running(pos, mem, COUNTDOWN_TICKS) end - else - mem.provided = 0 end - return mem.generating + return State:is_active(mem) end local function on_receive_fields(pos, formname, fields, player) @@ -203,7 +187,6 @@ minetest.register_craft({ techage.power.register_node({"techage:generator", "techage:generator_on"}, { conn_sides = {"R"}, power_network = Cable, - on_power = on_power, }) -- for logical communication @@ -215,7 +198,7 @@ techage.register_node({"techage:generator", "techage:generator_on"}, { local mem = tubelib2.get_mem(pos) mem.power_level = payload elseif topic == "trigger" then - mem.triggered = 2 + mem.firebox_trigger = 3 mem.power_level = payload if mem.generating then return math.max((mem.provided or PWR_CAPA) / PWR_CAPA, 0.02) @@ -233,9 +216,6 @@ techage.register_node({"techage:generator", "techage:generator_on"}, { end end, on_node_load = function(pos) - local mem = tubelib2.get_mem(pos) - -- bug workaround, TODO: remove - mem.generating = mem.techage_state == techage.RUNNING State:on_node_load(pos) end, }) diff --git a/furnace/booster.lua b/furnace/booster.lua index 3bf2eea..f13e319 100644 --- a/furnace/booster.lua +++ b/furnace/booster.lua @@ -20,8 +20,7 @@ local PWR_NEEDED = 3 local CYCLE_TIME = 2 local Power = techage.ElectricCable -local consume_power = techage.power.consume_power -local power_available = techage.power.power_available +local power = techage.power local function infotext(pos, state) M(pos):set_string("infotext", S("TA3 Booster")..": "..state) @@ -36,47 +35,29 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - mem.timer_running = (mem.timer_running or 1) - 1 - if mem.timer_running >= 0 then - local got = consume_power(pos, PWR_NEEDED) - if got < PWR_NEEDED then - swap_node(pos, "techage:ta3_booster") - infotext(pos, "no power") - mem.running = false - else - swap_node(pos, "techage:ta3_booster_on") - infotext(pos, "running") - mem.power_available = 2 - minetest.sound_play("techage_booster", { - pos = pos, - gain = 1, - max_hear_distance = 7}) - end - else - swap_node(pos, "techage:ta3_booster") - infotext(pos, "stopped") - mem.running = false +local function on_power(pos, mem) + if mem.running then + swap_node(pos, "techage:ta3_booster_on") + infotext(pos, "running") + minetest.get_node_timer(pos):start(CYCLE_TIME) end end +local function on_nopower(pos, mem) + swap_node(pos, "techage:ta3_booster") + infotext(pos, "no power") +end + local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) - mem.power_available = (mem.power_available or 1) - 1 - if mem.running and mem.power_available < 0 then - swap_node(pos, "techage:ta3_booster") - mem.running = false - return false - end - mem.timer_running = CYCLE_TIME/2 + 1 - return true -end - -local function on_rightclick(pos, node, clicker) if mem.running then - minetest.get_node_timer(pos):start(CYCLE_TIME) + minetest.sound_play("techage_booster", { + pos = pos, + gain = 1, + max_hear_distance = 7}) end + mem.still_powered = power.consumer_alive(pos, mem) + return mem.running end minetest.register_node("techage:ta3_booster", { @@ -150,6 +131,7 @@ techage.power.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, { power_network = Power, conn_sides = {"F", "B", "U", "D", "L"}, on_power = on_power, + on_nopower = on_nopower, }) -- for intra machine communication @@ -158,12 +140,12 @@ techage.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, { if M(pos):get_int("indir") == in_dir then local mem = tubelib2.get_mem(pos) if topic == "power" then - return mem.running - elseif topic == "start" then - if power_available(pos, 0) then + return mem.still_powered + elseif topic == "start" and not mem.running then + if power.power_available(pos, mem, 0) then mem.running = true - node_timer(pos, 2) - infotext(pos, "running") + mem.still_powered = treu + power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED) minetest.get_node_timer(pos):start(CYCLE_TIME) else infotext(pos, "no power") @@ -171,12 +153,9 @@ techage.register_node({"techage:ta3_booster", "techage:ta3_booster_on"}, { elseif topic == "stop" then mem.running = false swap_node(pos, "techage:ta3_booster") + power.consumer_stop(pos, mem) minetest.get_node_timer(pos):stop() - if mem.power_available then - infotext(pos, "stopped") - else - infotext(pos, "no power") - end + infotext(pos, "stopped") end end end diff --git a/lamps/lib.lua b/lamps/lib.lua index f9315db..f88b811 100644 --- a/lamps/lib.lua +++ b/lamps/lib.lua @@ -7,8 +7,7 @@ local PWR_NEEDED = 0.5 local CYCLE_TIME = 4 local Cable = techage.ElectricCable -local consume_power = techage.power.consume_power -local power_available = techage.power.power_available +local power = techage.power local function swap_node(pos, postfix) local node = techage.get_node_lvm(pos) @@ -20,30 +19,22 @@ local function swap_node(pos, postfix) minetest.swap_node(pos, node) end -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - mem.node_loaded = (mem.node_loaded or 1) - 1 - if mem.turned_on and mem.node_loaded >= 0 then - local got = consume_power(pos, PWR_NEEDED) - if got < PWR_NEEDED and mem.node_on then - swap_node(pos, "off") - mem.node_on = false - elseif not mem.node_on then - swap_node(pos, "on") - mem.node_on = true - end - mem.power_available = true +local function on_power(pos, mem) + if mem.turned_on then + swap_node(pos, "on") end end +local function on_nopower(pos, mem) + print(dump(mem)) + swap_node(pos, "off") +end + local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) - if mem.node_on and not mem.power_available then - mem.node_on = false - swap_node(pos, "off") + if mem.turned_on then + power.consumer_alive(pos, mem) end - mem.power_available = false - mem.node_loaded = CYCLE_TIME/2 + 1 return mem.turned_on end @@ -51,20 +42,17 @@ local function lamp_on_rightclick(pos, node, clicker) if minetest.is_protected(pos, clicker:get_player_name()) then return end + local mem = tubelib2.get_mem(pos) - mem.turned_on = not mem.turned_on - if mem.turned_on and power_available(pos, PWR_NEEDED) then - mem.node_on = true + if not mem.turned_on and power.power_available(pos, mem, PWR_NEEDED) then + mem.turned_on = true swap_node(pos, "on") - mem.trigger = true - minetest.get_node_timer(pos):start(CYCLE_TIME) - elseif mem.turned_on then - mem.node_on = false - swap_node(pos, "off") + power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED) minetest.get_node_timer(pos):start(CYCLE_TIME) else - mem.node_on = false + mem.turned_on = false swap_node(pos, "off") + power.consumer_stop(pos, mem) minetest.get_node_timer(pos):stop() end end @@ -125,5 +113,6 @@ function techage.register_lamp(basename, ndef_off, ndef_on) power_network = Cable, conn_sides = ndef_off.conn_sides or determine_power_side, -- will be handled by clbk function on_power = on_power, + on_nopower = on_nopower, }) end diff --git a/oil/generator.lua b/oil/generator.lua index f05b9a9..0616ed8 100644 --- a/oil/generator.lua +++ b/oil/generator.lua @@ -18,9 +18,7 @@ local S = techage.S local Power = techage.ElectricCable local firebox = techage.firebox -local provide_power = techage.power.provide_power -local power_switched = techage.power.power_switched -local power_distribution = techage.power.power_distribution +local power = techage.power local CYCLE_TIME = 2 local PWR_CAPA = 12 @@ -40,7 +38,7 @@ local function formspec(self, pos, mem) fuel_percent..":default_furnace_fire_fg.png]".. "button[3,1;1.8,1;update;"..S("Update").."]".. "image_button[5.5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. - "image[6.5,0.5;1,2;"..techage.power.formspec_power_bar(PWR_CAPA, mem.provided).."]".. + "image[6.5,0.5;1,2;"..power.formspec_power_bar(PWR_CAPA, mem.provided).."]".. "list[current_player;main;0,3;8,4;]".. default.get_hotbar_bg(0, 3) end @@ -53,7 +51,7 @@ end local function start_node(pos, mem, state) mem.generating = true - power_switched(pos) + power.generator_start(pos, mem, PWR_CAPA) minetest.sound_play("techage_generator", { pos = pos, gain = 1, @@ -63,7 +61,7 @@ end local function stop_node(pos, mem, state) mem.generating = false mem.provided = 0 - power_switched(pos) + power.generator_stop(pos, mem) end local State = techage.NodeStates:new({ @@ -98,31 +96,14 @@ local function burning(pos, mem) end end -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - if mem.generating then - mem.provided = provide_power(pos, PWR_CAPA) - else - mem.provided = 0 - end - mem.trigger = 2 -end - local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) if mem.generating and burning(pos, mem) then - power_distribution(pos) + mem.provided = power.generator_alive(pos, mem) minetest.sound_play("techage_generator", { pos = pos, gain = 1, max_hear_distance = 10}) - --State:keep_running(pos, mem, CYCLE_TIME) - mem.trigger = (mem.trigger or 1) - 1 - if mem.trigger <= 0 then - power_switched(pos) - mem.generating = false - mem.provided = 0 - end return true else mem.provided = 0 @@ -241,14 +222,13 @@ minetest.register_node("techage:tiny_generator_on", { techage.power.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, { conn_sides = {"R"}, power_network = Power, - on_power = on_power, }) techage.register_node({"techage:tiny_generator", "techage:tiny_generator_on"}, { on_recv_message = function(pos, topic, payload) local mem = tubelib2.get_mem(pos) if topic == "load" then - return techage.power.percent(PWR_CAPA, mem.provided) + return power.percent(PWR_CAPA, mem.provided) else return State:on_receive_message(pos, topic, payload) end diff --git a/power/power.lua b/power/power.lua index 3e13152..cb961f4 100644 --- a/power/power.lua +++ b/power/power.lua @@ -30,6 +30,117 @@ local STOPPED = 1 local NOPOWER = 2 local RUNNING = 3 +-------------------------------------------------- Migrate +local CRD = function(pos) return (minetest.registered_nodes[minetest.get_node(pos).name] or {}).consumer end +local Consumer = { + ["techage:streetlamp_off"] = 0, + ["techage:streetlamp_on"] = 0.5, + ["techage:industriallamp1_off"] = 0, + ["techage:industriallamp1_on"] = 0.5, + ["techage:industriallamp2_off"] = 0, + ["techage:industriallamp2_on"] = 0.5, + ["techage:industriallamp3_off"] = 0, + ["techage:industriallamp3_on"] = 0.5, + ["techage:simplelamp_off"] = 0, + ["techage:simplelamp_on"] = 0.5, + ["techage:ceilinglamp_off"] = 0, + ["techage:ceilinglamp_on"] = 0.5, + ["techage:ta2_autocrafter_pas"] = 0, + ["techage:ta2_autocrafter_act"] = 4, + ["techage:ta3_autocrafter_pas"] = 0, + ["techage:ta3_autocrafter_act"] = 6, + ["techage:ta2_electronic_fab_pas"] = 0, + ["techage:ta2_electronic_fab_act"] = 8, + ["techage:ta3_electronic_fab_pas"] = 0, + ["techage:ta3_electronic_fab_act"] = 12, + ["techage:ta2_gravelsieve_pas"] = 0, + ["techage:ta2_gravelsieve_act"] = 3, + ["techage:ta3_gravelsieve_pas"] = 0, + ["techage:ta3_gravelsieve_act"] = 4, + ["techage:ta2_grinder_pas"] = 0, + ["techage:ta2_grinder_act"] = 4, + ["techage:ta3_grinder_pas"] = 0, + ["techage:ta3_grinder_act"] = 6, + ["techage:ta2_rinser_pas"] = 0, + ["techage:ta2_rinser_act"] = 3, + ["techage:ta3_booster"] = 0, + ["techage:ta3_booster_on"] = 3, + ["techage:ta3_drillbox_pas"] = 0, + ["techage:ta3_drillbox_act"] = 16, + ["techage:ta3_pumpjack_pas"] = 0, + ["techage:ta3_pumpjack_act"] = 16, + ["techage:gearbox"] = 0, + ["techage:gearbox_on"] = 1, +} +local Generator = { + ["techage:t2_source"] = 20, + ["techage:t3_source"] = 20, + ["techage:t4_source"] = 20, + ["techage:flywheel"] = 0, + ["techage:flywheel_on"] = 25, + ["techage:generator"] = 0, + ["techage:generator_on"] = 80, + ["techage:tiny_generator"] = 0, + ["techage:tiny_generator_on"] = 12, +} +local Akku = { + ["techage:ta3_akku"] = 10 +} + +local function migrate(pos, mem) + if mem.master_pos then + mem.pwr_master_pos = table.copy(mem.master_pos); mem.master_pos = nil + mem.pwr_is_master = mem.is_master; mem.is_master = nil + mem.available1 = nil + mem.available2 = nil + mem.supply1 = nil + mem.supply2 = nil + mem.needed1 = nil + mem.needed2 = nil + mem.demand1 = nil + mem.demand2 = nil + mem.reserve = nil + mem.could_be_master = nil + mem.node_loaded = nil + + mem.pwr_power_provided_cnt = 2 + mem.pwr_node_alive_cnt = 4 + + local name = minetest.get_node(pos).name + mem.pwr_needed = Consumer[name] + mem.pwr_available = Generator[name] + mem.pwr_could_provide = Akku[name] + mem.pwr_could_need = Akku[name] + + if Consumer[name] then + if mem.techage_state then + if mem.techage_state == techage.STOPPED then + mem.pwr_state = STOPPED + else + local crd = CRD(pos) + techage.power.consumer_start(pos, mem, crd.cycle_time, crd.power_consumption) + end + elseif mem.turned_on then + mem.pwr_state = RUNNING + elseif mem.pwr_needed then + mem.pwr_state = RUNNING + else + mem.pwr_state = STOPPED + end + elseif Generator[name] then + if mem.generating then + techage.power.generator_start(pos, mem, Generator[name]) + else + techage.power.generator_stop(pos, mem) + end + end + + if not mem.pwr_needed and not mem.pwr_available and not mem.pwr_available2 then + print(name) + end + end +end +-------------------------------------------------- Migrate local function pos_already_reached(pos) local key = minetest.hash_node_position(pos) @@ -47,14 +158,16 @@ local function min(val, max) end local function accounting(pos, mem) - -- calculate the primary and secondary supply and demand - mem.mst_supply1 = min(mem.mst_needed1 + mem.mst_needed2, mem.mst_available1) - mem.mst_demand1 = min(mem.mst_needed1, mem.mst_available1 + mem.mst_available2) - mem.mst_supply2 = min(mem.mst_demand1 - mem.mst_supply1, mem.mst_available2) - mem.mst_demand2 = min(mem.mst_supply1 - mem.mst_demand1, mem.mst_available1) - mem.mst_reserve = (mem.mst_available1 + mem.mst_available2) - mem.mst_needed1 - print("needed = "..mem.mst_needed1.."/"..mem.mst_needed2..", available = "..mem.mst_available1.."/"..mem.mst_available2) - print("supply = "..mem.mst_supply1.."/"..mem.mst_supply2..", demand = "..mem.mst_demand1.."/"..mem.mst_demand2..", reserve = "..mem.mst_reserve) + if mem.pwr_is_master then + -- calculate the primary and secondary supply and demand + mem.mst_supply1 = min(mem.mst_needed1 + mem.mst_needed2, mem.mst_available1) + mem.mst_demand1 = min(mem.mst_needed1, mem.mst_available1 + mem.mst_available2) + mem.mst_supply2 = min(mem.mst_demand1 - mem.mst_supply1, mem.mst_available2) + mem.mst_demand2 = min(mem.mst_supply1 - mem.mst_demand1, mem.mst_available1) + mem.mst_reserve = (mem.mst_available1 + mem.mst_available2) - mem.mst_needed1 + print("needed = "..mem.mst_needed1.."/"..mem.mst_needed2..", available = "..mem.mst_available1.."/"..mem.mst_available2) + print("supply = "..mem.mst_supply1.."/"..mem.mst_supply2..", demand = "..mem.mst_demand1.."/"..mem.mst_demand2..", reserve = "..mem.mst_reserve) + end end local function connection_walk(pos, clbk) @@ -99,8 +212,8 @@ local function determine_master(pos) local master = nil connection_walk(pos, function(pos, mem) if (mem.pwr_node_alive_cnt or 0) >= 0 and - (mem.pwr_available or 0) > 0 or - (mem.pwr_available2 or 0) > 0 then -- active generator? + ((mem.pwr_available or 0) > 0 or + (mem.pwr_available2 or 0) > 0) then -- active generator? local new = minetest.hash_node_position(pos) if hash <= new then @@ -143,7 +256,7 @@ local function handle_consumer(mst_mem, mem, pos, power_needed) -- for next cycle mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed -- current cycle - if (mst_mem.mst_demand1 or 0) - power_needed >= 0 then + if (mst_mem.mst_demand1 or 0) >= power_needed then mst_mem.mst_demand1 = (mst_mem.mst_demand1 or 0) - power_needed consumer_turn_on(pos, mem) end @@ -152,8 +265,12 @@ local function handle_consumer(mst_mem, mem, pos, power_needed) -- for next cycle mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed -- current cycle - mst_mem.mst_demand1 = (mst_mem.mst_demand1 or 0) - power_needed - if mst_mem.mst_demand1 < 0 then + if (mst_mem.mst_demand1 or 0) >= power_needed then + mst_mem.mst_demand1 = (mst_mem.mst_demand1 or 0) - power_needed + -- small consumer like lamps are allowed to "use" the reserve + elseif power_needed <= 2 and (mst_mem.mst_reserve or 0) >= power_needed then + mst_mem.mst_reserve = (mst_mem.mst_reserve or 0) - power_needed + else -- no power available mst_mem.mst_demand1 = 0 consumer_turn_off(pos, mem) end @@ -201,6 +318,19 @@ local function trigger_nodes(mst_pos, mst_mem, dec) end) end +local function turn_off_nodes(mst_pos) + Route = {} + pos_already_reached(mst_pos) + connection_walk(mst_pos, function(pos, mem) + print("turn_off_nodes", minetest.get_node(pos).name) + if (mem.pwr_node_alive_cnt or -1) >= 0 then + if mem.pwr_needed then + consumer_turn_off(pos, mem) + end + end + end) +end + local function determine_new_master(pos, mem) local was_master = mem.pwr_is_master mem.pwr_is_master = false @@ -215,16 +345,18 @@ local function determine_new_master(pos, mem) mem.mst_supply2 = 0 mem.mst_reserve = 0 end - return was_master or mem.pwr_is_master or not mpos + return was_master or mem.pwr_is_master end -- called from master position local function power_distribution(pos, mem, dec) print("power_distribution") - mem.mst_needed1 = 0 - mem.mst_needed2 = 0 - mem.mst_available1 = 0 - mem.mst_available2 = 0 + if mem.pwr_is_master then + mem.mst_needed1 = 0 + mem.mst_needed2 = 0 + mem.mst_available1 = 0 + mem.mst_available2 = 0 + end trigger_nodes(pos, mem, dec or 0) accounting(pos, mem) end @@ -239,6 +371,8 @@ function techage.power.network_changed(pos, mem) mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1 if determine_new_master(pos, mem) then -- new master? power_distribution(pos, mem) + elseif not mem.pwr_master_pos then -- no master? + turn_off_nodes(pos) elseif not next(mem.connections) then -- isolated? if mem.pwr_needed then -- consumer? consumer_turn_off(pos, mem) @@ -267,6 +401,7 @@ function techage.power.generator_stop(pos, mem) end function techage.power.generator_alive(pos, mem) + migrate(pos, mem) -------------------------------- REMOVE mem.pwr_node_alive_cnt = 2 if mem.pwr_is_master then power_distribution(pos, mem, 1) @@ -277,15 +412,6 @@ end -- -- Consumer related functions -- -function techage.power.consumer_alive(pos, mem) - print("consumer_alive", mem.pwr_power_provided_cnt) - mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1 - mem.pwr_power_provided_cnt = (mem.pwr_power_provided_cnt or 0) - (mem.pwr_cycle_time or 2)/2 - if mem.pwr_power_provided_cnt < 0 and mem.pwr_state == RUNNING then - consumer_turn_off(pos, mem) - end -end - function techage.power.consumer_start(pos, mem, cycle_time, needed) mem.pwr_cycle_time = cycle_time mem.pwr_power_provided_cnt = 0 @@ -301,12 +427,25 @@ function techage.power.consumer_stop(pos, mem) mem.pwr_state = STOPPED end +function techage.power.consumer_alive(pos, mem) + migrate(pos, mem) -------------------------------- REMOVE + print("consumer_alive", mem.pwr_power_provided_cnt, mem.pwr_cycle_time) + mem.pwr_node_alive_cnt = (mem.pwr_cycle_time or 2)/2 + 1 + mem.pwr_power_provided_cnt = (mem.pwr_power_provided_cnt or 0) - (mem.pwr_cycle_time or 2)/2 + if mem.pwr_power_provided_cnt < 0 and mem.pwr_state == RUNNING then + consumer_turn_off(pos, mem) + return false + end + return mem.pwr_power_provided_cnt >= 0 +end + -- Lamp related function to speed up the turn on function techage.power.power_available(pos, mem, needed) + migrate(pos, mem) -------------------------------- REMOVE if mem.pwr_master_pos and (mem.pwr_power_provided_cnt or 0) > 0 then mem = tubelib2.get_mem(mem.pwr_master_pos) - if (mem.mst_reserve or 0) - needed >= 0 then - mem.mst_reserve = mem.mst_reserve - needed + if (mem.mst_reserve or 0) >= needed then + mem.mst_reserve = (mem.mst_reserve or 0) - needed return true end end @@ -354,6 +493,7 @@ function techage.power.secondary_stop(pos, mem) end function techage.power.secondary_alive(pos, mem, capa_curr, capa_max) + migrate(pos, mem) -------------------------------- REMOVE --print("secondary_alive") if capa_curr >= capa_max then mem.pwr_available2, mem.pwr_needed2 = mem.pwr_could_provide, 0 -- can provide only diff --git a/power/power2.lua b/power/power2.lua index d10cd27..fda6010 100644 --- a/power/power2.lua +++ b/power/power2.lua @@ -136,6 +136,7 @@ function techage.power.register_node(names, pwr_def) -- To be called delayed, so that all network connections have been established minetest.after(0.2, network_changed, pos, mem) if pwr.after_tube_update then + print("after_tube_update", node.name) return pwr.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) end end, diff --git a/steam_engine/flywheel.lua b/steam_engine/flywheel.lua index a626da9..cc8cd47 100644 --- a/steam_engine/flywheel.lua +++ b/steam_engine/flywheel.lua @@ -23,16 +23,14 @@ local CYCLE_TIME = 2 local PWR_CAPA = 25 local Axle = techage.Axle -local provide_power = techage.power.provide_power -local power_switched = techage.power.power_switched -local power_distribution = techage.power.power_distribution +local power = techage.power local function formspec(self, pos, mem) return "size[8,7]".. default.gui_bg.. default.gui_bg_img.. default.gui_slots.. - "image[6,0.5;1,2;"..techage.power.formspec_power_bar(PWR_CAPA, mem.provided).."]".. + "image[6,0.5;1,2;"..power.formspec_power_bar(PWR_CAPA, mem.provided).."]".. "image_button[5,1;1,1;".. self:get_state_button_image(mem) ..";state_button;]".. "button[2,1.5;2,1;update;"..S("Update").."]".. "list[current_player;main;0,3;8,4;]".. @@ -46,23 +44,21 @@ end local function start_node(pos, mem, state) mem.generating = true -- needed for power distribution techage.switch_axles(pos, true) - minetest.get_node_timer(pos):start(CYCLE_TIME) mem.handle = minetest.sound_play("techage_steamengine", { pos = pos, gain = 0.5, max_hear_distance = 10}) - power_switched(pos) + power.generator_start(pos, mem, PWR_CAPA) end local function stop_node(pos, mem, state) mem.generating = false techage.switch_axles(pos, false) - minetest.get_node_timer(pos):stop() if mem.handle then minetest.sound_stop(mem.handle) mem.handle = nil end - power_switched(pos) + power.generator_stop(pos, mem) mem.provided = 0 end @@ -77,39 +73,24 @@ local State = techage.NodeStates:new({ stop_node = stop_node, }) -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - if mem.generating then - mem.provided = provide_power(pos, PWR_CAPA) - else - mem.provided = 0 - end - mem.master_trigger = 2 -end - local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) if mem.generating then mem.firebox_trigger = (mem.firebox_trigger or 0) - 1 - mem.master_trigger = (mem.master_trigger or 0) - 1 - if mem.firebox_trigger <= 0 then - power_switched(pos) State:nopower(pos, mem) mem.generating = false + techage.switch_axles(pos, false) + power.generator_stop(pos, mem) mem.provided = 0 techage.transfer(pos, "L", "stop", nil, nil, {"techage:cylinder_on"}) else - power_distribution(pos) + mem.provided = power.generator_alive(pos, mem) State:keep_running(pos, mem, COUNTDOWN_TICKS) mem.handle = minetest.sound_play("techage_steamengine", { pos = pos, gain = 0.5, max_hear_distance = 10}) - - if mem.master_trigger <= 0 then - power_switched(pos) - end end end return State:is_active(mem) @@ -221,7 +202,6 @@ minetest.register_node("techage:flywheel_on", { techage.power.register_node({"techage:flywheel", "techage:flywheel_on"}, { conn_sides = {"R"}, power_network = Axle, - on_power = on_power, }) techage.register_node({"techage:flywheel", "techage:flywheel_on"}, { diff --git a/steam_engine/gearbox.lua b/steam_engine/gearbox.lua index 96abd15..c7172d6 100644 --- a/steam_engine/gearbox.lua +++ b/steam_engine/gearbox.lua @@ -21,7 +21,7 @@ local PWR_NEEDED = 1 local CYCLE_TIME = 4 local Axle = techage.Axle -local consume_power = techage.power.consume_power +local power = techage.power local function swap_node(pos, name) local node = minetest.get_node(pos) @@ -32,43 +32,37 @@ local function swap_node(pos, name) minetest.swap_node(pos, node) end -local function on_power(pos) - local mem = tubelib2.get_mem(pos) - mem.node_loaded = (mem.node_loaded or 1) - 1 - if mem.node_loaded >= 0 then - local got = consume_power(pos, PWR_NEEDED) - if got < PWR_NEEDED and mem.node_on then - swap_node(pos, "techage:gearbox") - techage.switch_axles(pos, false) - mem.node_on = false - elseif not mem.node_on then - swap_node(pos, "techage:gearbox_on") - techage.switch_axles(pos, true) - mem.node_on = true - end - mem.power_available = true - end +local function on_power(pos, mem) + swap_node(pos, "techage:gearbox_on") + techage.switch_axles(pos, true) end +local function on_nopower(pos, mem) + swap_node(pos, "techage:gearbox") + techage.switch_axles(pos, false) +end local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) - if mem.node_on and not mem.power_available then - swap_node(pos, "techage:gearbox") - techage.switch_axles(pos, false) - mem.node_on = false - end - mem.power_available = false - mem.node_loaded = CYCLE_TIME/2 + 1 + power.consumer_alive(pos, mem) return true end +-- to be able to restart the node after server crashes local function on_rightclick(pos, node, clicker) + local mem = tubelib2.get_mem(pos) minetest.get_node_timer(pos):start(CYCLE_TIME) + power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED) end local function after_place_node(pos, placer, itemstack, pointed_thing) + local mem = tubelib2.get_mem(pos) minetest.get_node_timer(pos):start(CYCLE_TIME) + power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED) +end + +local function after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) + techage.switch_axles(pos, node.name == "techage:gearbox_on") end minetest.register_node("techage:gearbox", { @@ -78,6 +72,7 @@ minetest.register_node("techage:gearbox", { on_construct = tubelib2.init_mem, after_place_node = after_place_node, on_rightclick = on_rightclick, + after_tube_update = after_tube_update, on_timer = node_timer, paramtype2 = "facedir", groups = {cracky=2, crumbly=2, choppy=2}, @@ -105,6 +100,7 @@ minetest.register_node("techage:gearbox_on", { after_place_node = after_place_node, on_rightclick = on_rightclick, on_timer = node_timer, + after_tube_update = after_tube_update, paramtype2 = "facedir", groups = {not_in_creative_inventory=1}, diggable = false, @@ -117,6 +113,7 @@ minetest.register_node("techage:gearbox_on", { techage.power.register_node({"techage:gearbox", "techage:gearbox_on"}, { power_network = Axle, on_power = on_power, + on_nopower = on_nopower, }) minetest.register_craft({ diff --git a/tools/repairkit.lua b/tools/repairkit.lua index 0ea8264..fb362b1 100644 --- a/tools/repairkit.lua +++ b/tools/repairkit.lua @@ -74,8 +74,10 @@ local function read_state(itemstack, user, pointed_thing) elseif ndef and ndef.description then if ndef.is_power_available then local power = ndef.is_power_available(pos) - local text = "\nGenerators = "..power.prim_available.."\nAkkus = "..power.sec_available.."\nMachines = "..power.prim_needed.."\n" - minetest.chat_send_player(user:get_player_name(), ndef.description..": power = "..text) + if power and power.prim_available then + local text = "\nGenerators = "..power.prim_available.."\nAkkus = "..power.sec_available.."\nMachines = "..power.prim_needed.."\n" + minetest.chat_send_player(user:get_player_name(), ndef.description..": power = "..text) + end end itemstack:add_wear(65636/200) return itemstack