From 4ca1ff8f5a3588b3a13cb9622140c39f98c8398f Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Sat, 17 Aug 2019 13:33:46 +0200 Subject: [PATCH] power distribution changed --- .test/sink.lua | 8 +++---- basis/consumer.lua | 7 +++++- basis/firebox_lib.lua | 1 - basis/lib.lua | 10 ++++++++ coal_power_station/firebox.lua | 2 +- coal_power_station/generator.lua | 1 - coal_power_station/turbine.lua | 1 - furnace/booster.lua | 17 ++++++++++---- furnace/firebox.lua | 12 ++++++++-- furnace/furnace_top.lua | 2 +- furnace/recipes.lua | 2 -- init.lua | 9 +++++++ iron_age/coalburner.lua | 1 - iron_age/meltingpot.lua | 2 -- lamps/lib.lua | 1 - power/power.lua | 40 ++++++++++++++++---------------- power/power2.lua | 4 ++-- power/powerswitch.lua | 2 -- steam_engine/firebox.lua | 2 +- 19 files changed, 77 insertions(+), 47 deletions(-) diff --git a/.test/sink.lua b/.test/sink.lua index ce61e24..bf30ffe 100644 --- a/.test/sink.lua +++ b/.test/sink.lua @@ -19,7 +19,7 @@ local function swap_node(pos, name) end local function on_power(pos, mem) - print("on_power") + mydbg("dbg", "on_power") if mem.running then swap_node(pos, "techage:sink_on") minetest.get_node_timer(pos):start(CYCLE_TIME) @@ -28,7 +28,7 @@ local function on_power(pos, mem) end local function on_nopower(pos, mem) - print("on_nopower") + mydbg("dbg", "on_nopower") swap_node(pos, "techage:sink") M(pos):set_string("infotext", "nopower") end @@ -44,14 +44,14 @@ local function on_rightclick(pos, node, clicker) local mem = tubelib2.get_mem(pos) if not mem.running and power.power_available(pos, mem, PWR_NEEDED) then mem.running = true - print("turn on") + mydbg("dbg", "turn on") --swap_node(pos, "techage:sink_on") power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED) minetest.get_node_timer(pos):start(CYCLE_TIME) M(pos):set_string("infotext", "on") else mem.running = false - print("turn off") + mydbg("dbg", "turn off") swap_node(pos, "techage:sink") power.consumer_stop(pos, mem) minetest.get_node_timer(pos):stop() diff --git a/basis/consumer.lua b/basis/consumer.lua index a4ef0d0..c148216 100644 --- a/basis/consumer.lua +++ b/basis/consumer.lua @@ -29,24 +29,29 @@ local CRDN = function(node) return (minetest.registered_nodes[node.name] or {}). local power = techage.power local function can_start(pos, mem, state) + mydbg("con", "consumer can_start", state) return power.power_available(pos, mem, CRD(pos).power_consumption) end local function start_node(pos, mem, state) local crd = CRD(pos) + mydbg("con", "consumer start_node", state) power.consumer_start(pos, mem, crd.cycle_time, crd.power_consumption) end local function stop_node(pos, mem, state) + mydbg("con", "consumer stop_node", state) power.consumer_stop(pos, mem) end local function on_power(pos, mem) + mydbg("con", "consumer on_power") local crd = CRD(pos) crd.State:start(pos, mem) end local function on_nopower(pos, mem) + mydbg("con", "consumer on_nopower") local crd = CRD(pos) crd.State:nopower(pos, mem) end @@ -55,7 +60,7 @@ local function node_timer(pos, elapsed) local crd = CRD(pos) local mem = tubelib2.get_mem(pos) local state = mem.techage_state - print("consumer node_timer", techage.needs_power(mem)) + mydbg("con", "consumer node_timer", techage.needs_power(mem)) if techage.power_alive(mem) then power.consumer_alive(pos, mem) end diff --git a/basis/firebox_lib.lua b/basis/firebox_lib.lua index 8577772..3b5179f 100644 --- a/basis/firebox_lib.lua +++ b/basis/firebox_lib.lua @@ -96,7 +96,6 @@ function techage.firebox.on_receive_fields(pos, formname, fields, player) if minetest.is_protected(pos, player:get_player_name()) then return end - --print(dump(fields)) if fields.update then local mem = tubelib2.get_mem(pos) M(pos):set_string("formspec", techage.firebox.formspec(mem)) diff --git a/basis/lib.lua b/basis/lib.lua index 1cbf2b8..1c3da47 100644 --- a/basis/lib.lua +++ b/basis/lib.lua @@ -160,3 +160,13 @@ function techage.dropped_node(node, ndef) end return ndef.drop or node.name end + +function mydbg(topic, text, ...) + if techage.DebugTopics[topic] then + local t = string.format("%.4f %4s: ", minetest.get_us_time() / 1000000.0, topic) + if type(text) ~= "string" then + text = dump(text) + end + print(t..text, unpack({...})) + end +end \ No newline at end of file diff --git a/coal_power_station/firebox.lua b/coal_power_station/firebox.lua index 11da9d5..1214dc8 100644 --- a/coal_power_station/firebox.lua +++ b/coal_power_station/firebox.lua @@ -36,7 +36,7 @@ end local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) - --print("firebox burn_cycles = "..(mem.burn_cycles or 0)) + mydbg("dbg", "firebox burn_cycles = "..(mem.burn_cycles or 0)) if mem.running then -- trigger generator and provide power ratio 0..1 local ratio = techage.transfer( diff --git a/coal_power_station/generator.lua b/coal_power_station/generator.lua index 20ee2e1..90f5591 100644 --- a/coal_power_station/generator.lua +++ b/coal_power_station/generator.lua @@ -192,7 +192,6 @@ techage.power.register_node({"techage:generator", "techage:generator_on"}, { -- for logical communication techage.register_node({"techage:generator", "techage:generator_on"}, { on_transfer = function(pos, in_dir, topic, payload) - --print("generator", topic, payload) local mem = tubelib2.get_mem(pos) if topic == "power_level" then local mem = tubelib2.get_mem(pos) diff --git a/coal_power_station/turbine.lua b/coal_power_station/turbine.lua index 4676402..6cc210c 100644 --- a/coal_power_station/turbine.lua +++ b/coal_power_station/turbine.lua @@ -132,7 +132,6 @@ techage.power.register_node({"techage:turbine", "techage:turbine_on"}, { -- for logical communication techage.register_node({"techage:turbine", "techage:turbine_on"}, { on_transfer = function(pos, in_dir, topic, payload) - --print("turbine", topic, payload) local mem = tubelib2.get_mem(pos) if topic == "trigger" then return transfer_generator(pos, topic, payload) diff --git a/furnace/booster.lua b/furnace/booster.lua index f13e319..b12d908 100644 --- a/furnace/booster.lua +++ b/furnace/booster.lua @@ -41,22 +41,26 @@ local function on_power(pos, mem) infotext(pos, "running") minetest.get_node_timer(pos):start(CYCLE_TIME) end + mydbg("dbg2", "booster on_power") + mem.is_powered = true end local function on_nopower(pos, mem) swap_node(pos, "techage:ta3_booster") infotext(pos, "no power") + mydbg("dbg2", "booster on_nopower") + mem.is_powered = false end local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) - if mem.running then + if mem.is_powered then minetest.sound_play("techage_booster", { pos = pos, gain = 1, max_hear_distance = 7}) end - mem.still_powered = power.consumer_alive(pos, mem) + power.consumer_alive(pos, mem) return mem.running end @@ -140,18 +144,23 @@ 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.still_powered + mydbg("dbg2", "booster", mem.is_powered) + return mem.is_powered elseif topic == "start" and not mem.running then + mydbg("dbg2", "booster try start", mem.pwr_master_pos, mem.pwr_power_provided_cnt) if power.power_available(pos, mem, 0) then mem.running = true - mem.still_powered = treu + mydbg("dbg2", "booster start") power.consumer_start(pos, mem, CYCLE_TIME, PWR_NEEDED) minetest.get_node_timer(pos):start(CYCLE_TIME) else + mydbg("dbg2", "booster no power") infotext(pos, "no power") end elseif topic == "stop" then mem.running = false + mem.is_powered = false + mydbg("dbg2", "booster stop") swap_node(pos, "techage:ta3_booster") power.consumer_stop(pos, mem) minetest.get_node_timer(pos):stop() diff --git a/furnace/firebox.lua b/furnace/firebox.lua index 29c2fb1..b2cb9c6 100644 --- a/furnace/firebox.lua +++ b/furnace/firebox.lua @@ -188,18 +188,26 @@ techage.register_node({"techage:furnace_firebox", "techage:furnace_firebox_on"}, end, -- called from furnace_top on_transfer = function(pos, in_dir, topic, payload) - --print("on_transfer", topic, payload) if topic == "fuel" then local mem = tubelib2.get_mem(pos) - return has_fuel(pos, mem) and booster_cmnd(pos, "power") + if booster_cmnd(pos, "power") then + -- fire keeps on even if the booster is off for some seconds + mem.booster_cnt = 3 + end + mydbg("dbg2", "firebox fuel", mem.booster_cnt) + mem.booster_cnt = (mem.booster_cnt or 3) - 1 + return has_fuel(pos, mem) and mem.booster_cnt > 0 elseif topic == "start" then local mem = tubelib2.get_mem(pos) start_firebox(pos, mem) booster_cmnd(pos, "start") + mem.booster_cnt = 4 + mydbg("dbg2", "firebox start", mem.booster_cnt) elseif topic == "stop" then local mem = tubelib2.get_mem(pos) stop_firebox(pos, mem) booster_cmnd(pos, "stop") + mydbg("dbg2", "firebox stop", mem.booster_cnt) end end }) diff --git a/furnace/furnace_top.lua b/furnace/furnace_top.lua index cbec283..23c9672 100644 --- a/furnace/furnace_top.lua +++ b/furnace/furnace_top.lua @@ -77,7 +77,6 @@ end local function cooking(pos, crd, mem, elapsed) if firebox_cmnd(pos, "fuel") then local state = smelting(pos, mem, elapsed) - --print("cooking", techage.StateStrings[state]) if state == techage.RUNNING then crd.State:keep_running(pos, mem, COUNTDOWN_TICKS) elseif state == techage.BLOCKED then @@ -161,6 +160,7 @@ local function can_dig(pos, player) end local function on_node_state_change(pos, old_state, new_state) + mydbg("dbg2", "on_node_state_change", new_state) if new_state == techage.RUNNING then firebox_cmnd(pos, "start") else diff --git a/furnace/recipes.lua b/furnace/recipes.lua index c16fd3b..2dbbb00 100644 --- a/furnace/recipes.lua +++ b/furnace/recipes.lua @@ -49,8 +49,6 @@ minetest.after(1, function() if tbl then for _,recipe in ipairs(tbl) do if recipe and recipe.method == "cooking" then - --print(dump(idef), dump(recipe)) - --print(key, recipe.width) techage.furnace.register_recipe({ output = recipe.output, recipe = recipe.items, diff --git a/init.lua b/init.lua index 376e14e..eeba386 100644 --- a/init.lua +++ b/init.lua @@ -18,6 +18,15 @@ else -- Load support for I18n. techage.S = minetest.get_translator("techage") + + -- Debugging via "mydbg(topic, text)" + techage.DebugTopics = { + --con = true, -- consumer modell + --pwr = true, -- power distribution + sts = true, -- status plots + --dbg = true, + dbg2 = true, + } -- Basis features local MP = minetest.get_modpath("techage") diff --git a/iron_age/coalburner.lua b/iron_age/coalburner.lua index 93eb48f..83bb6ec 100644 --- a/iron_age/coalburner.lua +++ b/iron_age/coalburner.lua @@ -185,7 +185,6 @@ function techage.start_burner(pos, playername) end function techage.keep_running_burner(pos) - --print("keep_running_burner") local meta = minetest.get_meta(pos) local height = meta:get_int("height") remove_flame(pos, height) diff --git a/iron_age/meltingpot.lua b/iron_age/meltingpot.lua index 1604cfb..7ed36fc 100644 --- a/iron_age/meltingpot.lua +++ b/iron_age/meltingpot.lua @@ -206,7 +206,6 @@ local function get_heat(pos) pos.y = pos.y - 1 local node = minetest.get_node(pos) local meta = minetest.get_meta(pos) - --print("get_heat1", minetest.get_item_group(node.name, "techage_flame")) if minetest.get_item_group(node.name, "techage_flame") == 0 then pos.y = pos.y + 1 return 0 @@ -215,7 +214,6 @@ local function get_heat(pos) pos.y = pos.y - 1 node = minetest.get_node(pos) pos.y = pos.y + 2 - --print("get_heat2", node.name, minetest.get_item_group(node.name, "techage_flame")) if minetest.get_item_group(node.name, "techage_flame") == 0 and node.name ~= "techage:charcoal_burn" then return 0 diff --git a/lamps/lib.lua b/lamps/lib.lua index f88b811..46b979c 100644 --- a/lamps/lib.lua +++ b/lamps/lib.lua @@ -26,7 +26,6 @@ local function on_power(pos, mem) end local function on_nopower(pos, mem) - print(dump(mem)) swap_node(pos, "off") end diff --git a/power/power.lua b/power/power.lua index cb961f4..a70ccd6 100644 --- a/power/power.lua +++ b/power/power.lua @@ -136,7 +136,7 @@ local function migrate(pos, mem) end if not mem.pwr_needed and not mem.pwr_available and not mem.pwr_available2 then - print(name) + mydbg("dbg", name) end end end @@ -165,8 +165,8 @@ local function accounting(pos, mem) 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) + mydbg("sts", "needed = "..mem.mst_needed1.."/"..mem.mst_needed2..", available = "..mem.mst_available1.."/"..mem.mst_available2) + mydbg("sts", "supply = "..mem.mst_supply1.."/"..mem.mst_supply2..", demand = "..mem.mst_demand1.."/"..mem.mst_demand2..", reserve = "..mem.mst_reserve) end end @@ -184,18 +184,20 @@ local function connection_walk(pos, clbk) end end +-- if no power available local function consumer_turn_off(pos, mem) local pwr = PWR(pos) - print("consumer_turn_off") + mydbg("pwr", "consumer_turn_off") if pwr and pwr.on_nopower then pwr.on_nopower(pos, mem) end mem.pwr_state = NOPOWER + mem.pwr_power_provided_cnt = -1 end local function consumer_turn_on(pos, mem) local pwr = PWR(pos) - print("consumer_turn_on") + mydbg("pwr", "consumer_turn_on") if pwr and pwr.on_power then pwr.on_power(pos, mem) end @@ -250,9 +252,9 @@ local function handle_generator(mst_mem, mem, pos, power_available) end local function handle_consumer(mst_mem, mem, pos, power_needed) - print("handle_consumer", mem.pwr_state) + mydbg("pwr", "handle_consumer", mem.pwr_state) if mem.pwr_state == NOPOWER then - print("power_needed", power_needed,"mst_mem.demand1", mst_mem.mst_demand1) + mydbg("pwr", "power_needed", power_needed,"mst_mem.demand1", mst_mem.mst_demand1) -- for next cycle mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed -- current cycle @@ -261,7 +263,7 @@ local function handle_consumer(mst_mem, mem, pos, power_needed) consumer_turn_on(pos, mem) end elseif mem.pwr_state == RUNNING then - print("power_needed", power_needed,"mst_mem.demand1", mst_mem.mst_demand1) + mydbg("pwr", "power_needed", power_needed,"mst_mem.demand1", mst_mem.mst_demand1) -- for next cycle mst_mem.mst_needed1 = mst_mem.mst_needed1 + power_needed -- current cycle @@ -305,7 +307,7 @@ local function trigger_nodes(mst_pos, mst_mem, dec) connection_walk(mst_pos, function(pos, mem) mem.pwr_node_alive_cnt = (mem.pwr_node_alive_cnt or 1) - dec mem.pwr_power_provided_cnt = 2 - print("trigger_nodes", minetest.get_node(pos).name, mem.pwr_node_alive_cnt, mem.pwr_available2 or mem.pwr_available or mem.pwr_needed) + mydbg("pwr", "trigger_nodes", minetest.get_node(pos).name, mem.pwr_node_alive_cnt, mem.pwr_available2 or mem.pwr_available or mem.pwr_needed) if mem.pwr_node_alive_cnt >= 0 then if mem.pwr_available then handle_generator(mst_mem, mem, pos, mem.pwr_available) @@ -322,7 +324,7 @@ 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) + mydbg("pwr", "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) @@ -335,7 +337,7 @@ local function determine_new_master(pos, mem) local was_master = mem.pwr_is_master mem.pwr_is_master = false local mpos = determine_master(pos) - --print("determine_new_master", S(mpos)) + mydbg("pwr", "determine_new_master", S(mpos)) store_master(pos, mpos) if mpos then tubelib2.get_mem(mpos).pwr_is_master = true @@ -350,7 +352,7 @@ end -- called from master position local function power_distribution(pos, mem, dec) - print("power_distribution") + mydbg("pwr", "power_distribution") if mem.pwr_is_master then mem.mst_needed1 = 0 mem.mst_needed2 = 0 @@ -367,7 +369,7 @@ end -- To be called for each network change from any node function techage.power.network_changed(pos, mem) - print("network_changed") + mydbg("pwr", "network_changed") 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) @@ -412,16 +414,16 @@ end -- -- Consumer related functions -- +-- this is more a try to start, the start will be performed by consumer_turn_on() function techage.power.consumer_start(pos, mem, cycle_time, needed) mem.pwr_cycle_time = cycle_time - mem.pwr_power_provided_cnt = 0 + mem.pwr_power_provided_cnt = 0 -- must be zero! mem.pwr_node_alive_cnt = 2 mem.pwr_needed = needed mem.pwr_state = NOPOWER end function techage.power.consumer_stop(pos, mem) - mem.pwr_power_provided_cnt = 0 mem.pwr_node_alive_cnt = 0 mem.pwr_needed = 0 mem.pwr_state = STOPPED @@ -429,14 +431,12 @@ end function techage.power.consumer_alive(pos, mem) migrate(pos, mem) -------------------------------- REMOVE - print("consumer_alive", mem.pwr_power_provided_cnt, mem.pwr_cycle_time) + mydbg("pwr", "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 @@ -494,7 +494,7 @@ end function techage.power.secondary_alive(pos, mem, capa_curr, capa_max) migrate(pos, mem) -------------------------------- REMOVE - --print("secondary_alive") + mydbg("pwr", "secondary_alive") if capa_curr >= capa_max then mem.pwr_available2, mem.pwr_needed2 = mem.pwr_could_provide, 0 -- can provide only elseif capa_curr <= 0 then @@ -505,7 +505,7 @@ function techage.power.secondary_alive(pos, mem, capa_curr, capa_max) mem.pwr_node_alive_cnt = 2 if mem.pwr_is_master then - --print("secondary_alive is master") + mydbg("pwr", "secondary_alive is master") power_distribution(pos, mem, 1) end return mem.pwr_provided or 0 diff --git a/power/power2.lua b/power/power2.lua index fda6010..7e9a1e7 100644 --- a/power/power2.lua +++ b/power/power2.lua @@ -136,7 +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) + mydbg("tlib", "after_tube_update", node.name) return pwr.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir) end end, @@ -199,7 +199,7 @@ end -- Used to turn on/off the power by means of a power switch function techage.power.power_cut(pos, dir, cable, cut) - print("power_cut") + mydbg("pwr", "power_cut") local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0]) local node = minetest.get_node(npos) diff --git a/power/powerswitch.lua b/power/powerswitch.lua index 8a7e13a..7ab39f4 100644 --- a/power/powerswitch.lua +++ b/power/powerswitch.lua @@ -35,7 +35,6 @@ local function switch_on(pos, node, clicker) if clicker and minetest.is_protected(pos, clicker:get_player_name()) then return end - print("switch_on") node.name = "techage:powerswitch_on" minetest.swap_node(pos, node) minetest.sound_play("techage_button", { @@ -51,7 +50,6 @@ local function switch_off(pos, node, clicker) if clicker and minetest.is_protected(pos, clicker:get_player_name()) then return end - print("switch_off") node.name = "techage:powerswitch" minetest.swap_node(pos, node) minetest.get_node_timer(pos):stop() diff --git a/steam_engine/firebox.lua b/steam_engine/firebox.lua index 5779c98..17cc49c 100644 --- a/steam_engine/firebox.lua +++ b/steam_engine/firebox.lua @@ -24,7 +24,7 @@ local BURN_CYCLE_FACTOR = 0.8 local function node_timer(pos, elapsed) local mem = tubelib2.get_mem(pos) - --print("firebox burn_cycles = "..(mem.burn_cycles or 0)) + mydbg("dbg", "firebox burn_cycles = "..(mem.burn_cycles or 0)) if mem.running then local power = techage.transfer( {x=pos.x, y=pos.y+2, z=pos.z},