From 66937f57431ebd43d16388d9edd01a94e5c05674 Mon Sep 17 00:00:00 2001 From: Joachim Stolberg Date: Sun, 19 Feb 2023 15:11:43 +0100 Subject: [PATCH] Fix power consumption bug for a stopped collider --- collider/detector.lua | 80 ++++++++++++++++++++++++++++++------------- collider/inlets.lua | 11 +++++- collider/magnet.lua | 10 +++++- 3 files changed, 76 insertions(+), 25 deletions(-) diff --git a/collider/detector.lua b/collider/detector.lua index 494d6b9..c5dee94 100644 --- a/collider/detector.lua +++ b/collider/detector.lua @@ -3,7 +3,7 @@ TechAge ======= - Copyright (C) 2019-2021 Joachim Stolberg + Copyright (C) 2019-2023 Joachim Stolberg AGPL v3 See LICENSE.txt for more information @@ -157,7 +157,7 @@ local function create_task(pos, task) end -- Call on_cyclic_check of all magents so that the magnets don't need a FLB. -local function magnet_on_cyclic_check(pos, nvm) +local function magnets_on_cyclic_check(pos, nvm) local ndef = minetest.registered_nodes["techage:ta4_magnet"] for idx,pos2 in ipairs(nvm.magnet_positions or {}) do local res = ndef.on_cyclic_check(pos2) @@ -173,6 +173,29 @@ local function magnet_on_cyclic_check(pos, nvm) return true end +-- Turn off all magnets so that they don't consume power +local function magnets_turn_off(pos, nvm) + local ndef = minetest.registered_nodes["techage:ta4_magnet"] + for idx,pos2 in ipairs(nvm.magnet_positions or {}) do + ndef.on_turn_off(pos2) + end +end + +local function cable_inlets_turn_on_off(pos, on) + local turn_on_off = function(pos, param2, item) + local pos2 = getpos(pos, param2, item.route, item.yoffs) + local node2 = minetest.get_node(pos2) + if item.name == node2.name then + local nvm = techage.get_nvm(pos2) + techage.power_inlet_turn_on_off(pos2, nvm, on) + end + end + + local param2 = minetest.get_node(pos).param2 + turn_on_off(pos, param2, Schedule[2]) + turn_on_off(pos, param2, Schedule[3]) +end + minetest.register_node("techage:ta4_detector_core", { description = S("TA4 Collider Detector Core"), tiles = { @@ -202,29 +225,33 @@ minetest.register_node("techage:ta4_detector_core", { on_timer = function(pos, elapsed) local nvm = techage.get_nvm(pos) - if not magnet_on_cyclic_check(pos, nvm) then - techage.del_laser(pos) - if nvm.running then - terminal_message(pos, "Detector stopped.") - nvm.running = false - end - nvm.magnet_positions = nil - elseif nvm.running then - local res = check_state(pos) - if res == true then - experience_points(pos) - add_laser(pos) - if nvm.ticks <= TIME_SLOTS then -- only once - terminal_message(pos, "Detector running.") - end - elseif res == false then + if nvm.running then + if not magnets_on_cyclic_check(pos, nvm) then techage.del_laser(pos) + terminal_message(pos, "Detector stopped.") + magnets_turn_off(pos, nvm) + cable_inlets_turn_on_off(pos, false) nvm.running = false nvm.magnet_positions = nil - terminal_message(pos, "Detector stopped.") - end - if nvm.running then - play_sound(pos) + else + local res = check_state(pos) + if res == true then + experience_points(pos) + add_laser(pos) + if nvm.ticks <= TIME_SLOTS then -- only once + terminal_message(pos, "Detector running.") + end + elseif res == false then + techage.del_laser(pos) + magnets_turn_off(pos, nvm) + cable_inlets_turn_on_off(pos, false) + nvm.running = false + nvm.magnet_positions = nil + terminal_message(pos, "Detector stopped.") + end + if nvm.running then + play_sound(pos) + end end end return true @@ -287,6 +314,7 @@ local function start_task(pos) end nvm.magnet_positions = t techage.send_single(own_num, term_num, "append", "ok") + cable_inlets_turn_on_off(pos, true) coroutine.yield() techage.send_single(own_num, term_num, "text", "- Check magnets...") @@ -310,6 +338,7 @@ local function start_task(pos) techage.send_single(own_num, term_num, "append", err .. "!!!") nvm.magnet_positions = nil nvm.locked = false + cable_inlets_turn_on_off(pos, false) return end else @@ -323,12 +352,14 @@ local function start_task(pos) techage.send_single(own_num, term_num, "append", err .. "!!!") nvm.magnet_positions = nil nvm.locked = false + cable_inlets_turn_on_off(pos, false) return end else techage.send_single(own_num, term_num, "append", "defect!!!") nvm.magnet_positions = nil nvm.locked = false + cable_inlets_turn_on_off(pos, false) return end coroutine.yield() @@ -337,7 +368,7 @@ local function start_task(pos) techage.send_single(own_num, term_num, "append", "ok") coroutine.yield() - techage.send_single(own_num, term_num, "text", "Collider started.") + techage.send_single(own_num, term_num, "text", "Collider starting...") nvm.ticks = 0 nvm.running = true end @@ -372,6 +403,9 @@ techage.register_node({"techage:ta4_detector_core"}, { nvm.running = false techage.del_laser(pos) nvm.locked = false + magnets_turn_off(pos, nvm) + cable_inlets_turn_on_off(pos, false) + nvm.magnet_positions = nil return "Detector stopped." elseif topic == "status" then if nvm.running == true then diff --git a/collider/inlets.lua b/collider/inlets.lua index 7826026..2f60662 100644 --- a/collider/inlets.lua +++ b/collider/inlets.lua @@ -328,7 +328,11 @@ minetest.register_node("techage:ta4_collider_cable_inlet", { on_timer = function(pos, elapsed) local nvm = techage.get_nvm(pos) - nvm.consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + if nvm.running then + nvm.consumed = power.consume_power(pos, Cable, nil, PWR_NEEDED) + else + nvm.consumed = 0 + end return true end, @@ -346,6 +350,11 @@ function techage.power_inlet_check(pos, node, meta, nvm) return false, "no power" end +-- Used by the detector to turn on/off the node +function techage.power_inlet_turn_on_off(pos, nvm, on) + nvm.running = on +end + power.register_nodes({"techage:ta4_collider_cable_inlet"}, Cable, "con", {"F"}) techage.register_node({"techage:ta4_collider_cable_inlet"}, { diff --git a/collider/magnet.lua b/collider/magnet.lua index 4062413..315541e 100644 --- a/collider/magnet.lua +++ b/collider/magnet.lua @@ -3,7 +3,7 @@ TechAge ======= - Copyright (C) 2019-2021 Joachim Stolberg + Copyright (C) 2019-2023 Joachim Stolberg AGPL v3 See LICENSE.txt for more information @@ -119,14 +119,22 @@ minetest.register_node("techage:ta4_magnet", { nvm.consumed = power.consume_power(pos, Cable, 6, PWR_NEEDED) if nvm.tube_damage then nvm.tube_damage = nil + nvm.running = nil return -1 elseif nvm.liquid.amount == CAPACITY and nvm.liquid.name == "techage:isobutane" and nvm.consumed == PWR_NEEDED then + nvm.running = true return 0 end + nvm.running = nil return -2 end, + + on_turn_off = function(pos) + local nvm = techage.get_nvm(pos) + nvm.running = nil + end, tubelib2_on_update2 = function(pos, outdir, tlib2, node) if tlib2.tube_type == "vtube" then