From 088eea1e3ba42fd13510861025e1c9fc989799a0 Mon Sep 17 00:00:00 2001 From: Carter Kolwey Date: Fri, 10 Mar 2017 01:19:36 -0600 Subject: [PATCH] Fixes for network calculation issues This fixes several issues: * More than one switching station could become active on large networks - Switching stations now semi-permanently become disabled if another is present - Power monitors have been added to replace the function of "slave" switching stations, to discourage overuse of switching stations * Networks did not reliably "split" when cutting a cable - I "may" have caused this issue, but I believe it is solved by this * Machines did not run without a player near the switching station - Active switching stations now forceload themselves, and free the forceloaded block if disabled, dug, or disconnected - Machines are only loaded to run them (as before), so only one mapblock (or two if the bottom edge of the switching station is a mapblock boundary) is loaded - Cables are still only loaded during a full network recalculation --- technic/machines/init.lua | 1 + technic/machines/power_monitor.lua | 61 ++++++++++++++++++++++++++ technic/machines/register/cables.lua | 9 +++- technic/machines/switching_station.lua | 55 ++++++++++++++++++----- 4 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 technic/machines/power_monitor.lua diff --git a/technic/machines/init.lua b/technic/machines/init.lua index ad79eab..f2e31c9 100644 --- a/technic/machines/init.lua +++ b/technic/machines/init.lua @@ -8,6 +8,7 @@ dofile(path.."/MV/init.lua") dofile(path.."/HV/init.lua") dofile(path.."/switching_station.lua") +dofile(path.."/power_monitor.lua") dofile(path.."/supply_converter.lua") dofile(path.."/other/init.lua") diff --git a/technic/machines/power_monitor.lua b/technic/machines/power_monitor.lua new file mode 100644 index 0000000..4e90460 --- /dev/null +++ b/technic/machines/power_monitor.lua @@ -0,0 +1,61 @@ +-- POWER MONITOR +-- The power monitor can be used to monitor how much power is available on a network, +-- similarly to the old "slave" switching stations. + +local S = technic.getter + +minetest.register_craft({ + output = "technic:power_monitor", + recipe = { + {"", "", ""}, + {"", "technic:machine_casing", "default:copper_ingot"}, + {"technic:lv_cable", "technic:lv_cable", "technic:lv_cable"} + } +}) + +minetest.register_node("technic:power_monitor",{ + description = S("Power Monitor"), + tiles = {"technic_water_mill_top_active.png", "technic_water_mill_top_active.png", + "technic_water_mill_top_active.png", "technic_water_mill_top_active.png", + "technic_water_mill_top_active.png", "technic_water_mill_top_active.png"}, + groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_all_tiers=1, technic_machine=1}, + connect_sides = {"bottom"}, + sounds = default.node_sound_wood_defaults(), + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("infotext", S("Power Monitor")) + end, +}) + +minetest.register_abm({ + nodenames = {"technic:power_monitor"}, + label = "Power Monitor", + interval = 1, + chance = 1, + action = function(pos, node, active_object_count, active_object_count_wider) + local meta = minetest.get_meta(pos) + local network_hash = technic.cables[minetest.hash_node_position(pos)] + local network = network_hash and minetest.get_position_from_hash(network_hash) + local sw_pos = network and {x=network.x,y=network.y+1,z=network.z} + local timeout = 0 + for tier in pairs(technic.machines) do + timeout = math.max(meta:get_int(tier.."_EU_timeout"),timeout) + end + if timeout > 0 and sw_pos and minetest.get_node(sw_pos).name == "technic:switching_station" then + local sw_meta = minetest.get_meta(sw_pos) + local supply = sw_meta:get_int("supply") + local demand = sw_meta:get_int("demand") + meta:set_string("infotext", + S("Power Monitor. Supply: @1 Demand: @2", + technic.pretty_num(supply), technic.pretty_num(demand))) + else + meta:set_string("infotext",S("Power Monitor Has No Network")) + end + end, +}) + +for tier in pairs(technic.machines) do + -- RE in order to use the "timeout" functions, although it consumes 0 power + technic.register_machine(tier, "technic:power_monitor", "RE") +end + diff --git a/technic/machines/register/cables.lua b/technic/machines/register/cables.lua index a7e72a3..04c6096 100644 --- a/technic/machines/register/cables.lua +++ b/technic/machines/register/cables.lua @@ -20,8 +20,13 @@ local function clear_networks(pos) {x=pos.x, y=pos.y, z=pos.z+1}, {x=pos.x, y=pos.y, z=pos.z-1}} for _,connected_pos in pairs(positions) do - if technic.cables[minetest.hash_node_position(connected_pos)] then - technic.networks[technic.cables[minetest.hash_node_position(connected_pos)]] = nil + local net = technic.cables[minetest.hash_node_position(connected_pos)] + if net and technic.networks[net] then + for _,v in pairs(technic.networks[net].all_nodes) do + local pos1 = minetest.hash_node_position(v) + technic.cables[pos1] = nil + end + technic.networks[net] = nil end end end diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua index 2051acf..f925817 100644 --- a/technic/machines/switching_station.lua +++ b/technic/machines/switching_station.lua @@ -58,6 +58,11 @@ minetest.register_node("technic:switching_station",{ meta:set_string("infotext", S("Switching Station")) meta:set_string("active", 1) end, + after_dig_node = function(pos) + minetest.forceload_free_block(pos) + pos.y = pos.y - 1 + minetest.forceload_free_block(pos) + end, }) -------------------------------------------------- @@ -89,6 +94,7 @@ local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nod add_new_cable_node(all_nodes, pos,network_id) elseif machines[name] then --dprint(name.." is a "..machines[name]) + meta:set_string(tier.."_network",minetest.pos_to_string(sw_pos)) if machines[name] == technic.producer then add_new_cable_node(PR_nodes, pos, network_id) elseif machines[name] == technic.receiver then @@ -102,7 +108,6 @@ local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nod -- Another switching station -> disable it add_new_cable_node(SP_nodes, pos, network_id) meta:set_int("active", 0) - meta:set_string("active_pos", minetest.serialize(sw_pos)) elseif machines[name] == technic.battery then add_new_cable_node(BA_nodes, pos, network_id) end @@ -159,7 +164,7 @@ local get_network = function(sw_pos, pos1, tier) i = i + 1 until all_nodes[i] == nil technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes, - RE_nodes = RE_nodes, BA_nodes = BA_nodes, SP_nodes = SP_nodes} + RE_nodes = RE_nodes, BA_nodes = BA_nodes, SP_nodes = SP_nodes, all_nodes = all_nodes} return PR_nodes, BA_nodes, RE_nodes end @@ -185,27 +190,30 @@ minetest.register_abm({ local BA_nodes local RE_nodes local machine_name = S("Switching Station") - - if meta:get_int("active") ~= 1 then - meta:set_int("active", 1) - local active_pos = minetest.deserialize(meta:get_string("active_pos")) - if active_pos then - local meta1 = minetest.get_meta(active_pos) - meta:set_string("infotext", S("%s (Slave)"):format(meta1:get_string("infotext"))) - end - return - end -- Which kind of network are we on: pos1 = {x=pos.x, y=pos.y-1, z=pos.z} + --Disable if necessary + if meta:get_int("active") ~= 1 then + minetest.forceload_free_block(pos) + minetest.forceload_free_block(pos1) + meta:set_string("infotext",S("%s Already Present"):format(machine_name)) + return + end + local name = minetest.get_node(pos1).name local tier = technic.get_cable_tier(name) if tier then + -- Forceload switching station + minetest.forceload_block(pos) + minetest.forceload_block(pos1) PR_nodes, BA_nodes, RE_nodes = get_network(pos, pos1, tier) else --dprint("Not connected to a network") meta:set_string("infotext", S("%s Has No Network"):format(machine_name)) + minetest.forceload_free_block(pos) + minetest.forceload_free_block(pos1) return end @@ -293,6 +301,10 @@ minetest.register_abm({ S("@1. Supply: @2 Demand: @3", machine_name, technic.pretty_num(PR_eu_supply), technic.pretty_num(RE_eu_demand))) + -- Data that will be used by the power monitor + meta:set_int("supply",PR_eu_supply) + meta:set_int("demand",RE_eu_demand) + -- If the PR supply is enough for the RE demand supply them all if PR_eu_supply >= RE_eu_demand then --dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand) @@ -354,6 +366,7 @@ minetest.register_abm({ meta1 = minetest.get_meta(pos1) meta1:set_int(eu_input_str, 0) end + end, }) @@ -376,6 +389,7 @@ minetest.register_abm({ interval = 1, chance = 1, action = function(pos, node, active_object_count, active_object_count_wider) + local meta = minetest.get_meta(pos) for tier, machines in pairs(technic.machines) do if machines[node.name] and switching_station_timeout_count(pos, tier) then local nodedef = minetest.registered_nodes[node.name] @@ -394,6 +408,23 @@ minetest.register_abm({ end, }) +--Re-enable disabled switching station if necessary, similar to the timeout above +minetest.register_abm({ + nodenames = {"technic:switching_station"}, + interval = 1, + chance = 1, + action = function(pos, node, active_object_count, active_object_count_wider) + local meta = minetest.get_meta(pos) + local pos1 = {x=pos.x,y=pos.y-1,z=pos.z} + local tier = technic.get_cable_tier(minetest.get_node(pos1).name) + if not tier then return end + if switching_station_timeout_count(pos, tier) then + local meta = minetest.get_meta(pos) + meta:set_int("active",1) + end + end, +}) + for tier, machines in pairs(technic.machines) do -- SPECIAL will not be traversed technic.register_machine(tier, "technic:switching_station", "SPECIAL")