summaryrefslogtreecommitdiff
path: root/technic/machines/switching_station.lua
diff options
context:
space:
mode:
Diffstat (limited to 'technic/machines/switching_station.lua')
-rw-r--r--technic/machines/switching_station.lua79
1 files changed, 56 insertions, 23 deletions
diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua
index f7ec0b6..f925817 100644
--- a/technic/machines/switching_station.lua
+++ b/technic/machines/switching_station.lua
@@ -32,6 +32,7 @@
-- This way the supplies are separated per network.
technic.networks = {}
+technic.cables = {}
local S = technic.getter
@@ -57,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,
})
--------------------------------------------------
@@ -64,7 +70,8 @@ minetest.register_node("technic:switching_station",{
--------------------------------------------------
-- Add a wire node to the LV/MV/HV network
-local add_new_cable_node = function(nodes, pos)
+local add_new_cable_node = function(nodes, pos, network_id)
+ technic.cables[minetest.hash_node_position(pos)] = network_id
-- Ignore if the node has already been added
for i = 1, #nodes do
if pos.x == nodes[i].x and
@@ -78,31 +85,31 @@ local add_new_cable_node = function(nodes, pos)
end
-- Generic function to add found connected nodes to the right classification array
-local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below)
+local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below, network_id)
technic.get_or_load_node(pos)
local meta = minetest.get_meta(pos)
local name = minetest.get_node(pos).name
if technic.is_tier_cable(name, tier) then
- add_new_cable_node(all_nodes, pos)
+ 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)
+ add_new_cable_node(PR_nodes, pos, network_id)
elseif machines[name] == technic.receiver then
- add_new_cable_node(RE_nodes, pos)
+ add_new_cable_node(RE_nodes, pos, network_id)
elseif machines[name] == technic.producer_receiver then
- add_new_cable_node(PR_nodes, pos)
- add_new_cable_node(RE_nodes, pos)
+ add_new_cable_node(PR_nodes, pos, network_id)
+ add_new_cable_node(RE_nodes, pos, network_id)
elseif machines[name] == "SPECIAL" and
(pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) and
from_below then
-- Another switching station -> disable it
- add_new_cable_node(SP_nodes, pos)
+ 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)
+ add_new_cable_node(BA_nodes, pos, network_id)
end
meta:set_int(tier.."_EU_timeout", 2) -- Touch node
@@ -110,7 +117,7 @@ local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nod
end
-- Traverse a network given a list of machines and a cable type name
-local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos)
+local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos, network_id)
local pos = all_nodes[i]
local positions = {
{x=pos.x+1, y=pos.y, z=pos.z},
@@ -121,7 +128,7 @@ local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_no
{x=pos.x, y=pos.y, z=pos.z-1}}
--print("ON")
for i, cur_pos in pairs(positions) do
- check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3)
+ check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3, network_id)
end
end
@@ -153,11 +160,11 @@ local get_network = function(sw_pos, pos1, tier)
local all_nodes = {pos1}
repeat
traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes,
- i, technic.machines[tier], tier, sw_pos)
+ i, technic.machines[tier], tier, sw_pos, minetest.hash_node_position(pos1))
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
@@ -183,27 +190,30 @@ minetest.register_abm({
local BA_nodes
local RE_nodes
local machine_name = S("Switching Station")
+
+ -- 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
- 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
+ minetest.forceload_free_block(pos)
+ minetest.forceload_free_block(pos1)
+ meta:set_string("infotext",S("%s Already Present"):format(machine_name))
return
end
-
- -- Which kind of network are we on:
- pos1 = {x=pos.x, y=pos.y-1, z=pos.z}
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
@@ -291,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)
@@ -352,6 +366,7 @@ minetest.register_abm({
meta1 = minetest.get_meta(pos1)
meta1:set_int(eu_input_str, 0)
end
+
end,
})
@@ -374,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]
@@ -392,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")