Skip to content

Commit

Permalink
Cleanup caches immediately when new switching station placed
Browse files Browse the repository at this point in the history
  • Loading branch information
SX committed Sep 20, 2020
1 parent b131e3f commit adc0b33
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions technic/machines/switching_station.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ local function reset_overloaded(network_id)
local remaining = math.max(0, overloaded_networks[network_id] - minetest.get_us_time())
if remaining == 0 then
-- Clear cache, remove overload and restart network
local cables = technic.cables
for pos_hash,cable_net_id in pairs(cables) do
if cable_net_id == network_id then
cables[pos_hash] = nil
end
end
technic.remove_network(network_id)
overloaded_networks[network_id] = nil
technic.networks[network_id] = nil
end
-- Returns 0 when network reset or remaining time if reset timer has not expired yet
return remaining
Expand Down Expand Up @@ -68,13 +62,20 @@ minetest.register_node("technic:switching_station",{
meta:set_string("infotext", S("Switching Station"))
local network_id = technic.sw_pos2network(pos)
local net_sw_pos = network_id and technic.network2sw_pos(network_id)
local net_sw_node = net_sw_pos and minetest.get_node(net_sw_pos)
if net_sw_node and net_sw_node.name == "technic:switching_station" then
local net_sw_node = net_sw_pos and minetest.get_node_or_nil(net_sw_pos)
if net_sw_node then
-- There's already network with same id, check if it already has active switching station
-- set active to 0 for this switch if there's already another active
local net_sw_meta = minetest.get_meta(net_sw_pos)
meta:set_string("active", net_sw_meta:get_int("active") == 1 and 0 or 1)
if net_sw_node.name == "technic:switching_station" then
-- Another switch found set active to 0 for this switch if another is already active
local net_sw_meta = minetest.get_meta(net_sw_pos)
meta:set_string("active", net_sw_meta:get_int("active") == 1 and 0 or 1)
else
-- Network switching station disappeared, cleanup caches and start new network
technic.remove_network(network_id)
meta:set_string("active", 1)
end
else
-- Clean start, not previous networks, no other switching stations
meta:set_string("active", 1)
end
meta:set_string("channel", "switching_station"..minetest.pos_to_string(pos))
Expand Down Expand Up @@ -226,6 +227,16 @@ local function traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_node
end
end

function technic.remove_network(network_id)
local cables = technic.cables
for pos_hash,cable_net_id in pairs(cables) do
if cable_net_id == network_id then
cables[pos_hash] = nil
end
end
technic.networks[network_id] = nil
end

function technic.sw_pos2network(pos)
return pos and technic.cables[minetest.hash_node_position({x=pos.x,y=pos.y-1,z=pos.z})]
end
Expand Down

0 comments on commit adc0b33

Please sign in to comment.