Skip to content

Commit 99b9001

Browse files
authored
Use switching station ref counters (#265)
* Use switching station ref counters * Fix luacheck and switch supply/demand reporting * Make switch_index local, fix switching station infotext * Remove unnecessary variable
1 parent 4434025 commit 99b9001

File tree

3 files changed

+62
-48
lines changed

3 files changed

+62
-48
lines changed

technic/machines/network.lua

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ function technic.remove_network(network_id)
9999
technic.active_networks[network_id] = nil
100100
end
101101

102+
local function switch_index(pos, net)
103+
for index, spos in ipairs(net.swpos) do
104+
if pos.x == spos.x and pos.y == spos.y and pos.z == spos.z then
105+
return index
106+
end
107+
end
108+
end
109+
110+
function technic.switch_insert(pos, net)
111+
if not switch_index(pos, net) then
112+
table.insert(net.swpos, table.copy(pos))
113+
end
114+
return #net.swpos
115+
end
116+
117+
function technic.switch_remove(pos, net)
118+
local swindex = switch_index(pos, net)
119+
if swindex then
120+
table.remove(net.swpos, swindex)
121+
end
122+
return #net.swpos
123+
end
124+
102125
function technic.sw_pos2network(pos)
103126
return technic_cables[poshash({x=pos.x,y=pos.y-1,z=pos.z})]
104127
end
@@ -514,8 +537,8 @@ function technic.build_network(network_id)
514537
network = {
515538
-- Build queue
516539
queue = {},
517-
-- Basic network data and lookup table for attached nodes (no switching stations)
518-
id = network_id, tier = tier, all_nodes = {},
540+
-- Basic network data and lookup table for attached nodes
541+
id = network_id, tier = tier, all_nodes = {}, swpos = {},
519542
-- Indexed arrays for iteration by machine type
520543
PR_nodes = {}, RE_nodes = {}, BA_nodes = {},
521544
-- Power generation, usage and capacity related variables
@@ -580,9 +603,6 @@ local function run_nodes(list, vm, run_stage, network)
580603
end
581604
end
582605

583-
local mesecons_path = minetest.get_modpath("mesecons")
584-
local digilines_path = minetest.get_modpath("digilines")
585-
586606
function technic.network_run(network_id)
587607
--
588608
-- !!! !!! !!! !!! !!! !!! !!! !!! !!! !!! !!!
@@ -592,19 +612,16 @@ function technic.network_run(network_id)
592612
-- should be removed and/or refactored.
593613
--
594614

595-
local pos = technic.network2sw_pos(network_id)
596615
local t0 = minetest.get_us_time()
597616

598617
local PR_nodes
599618
local BA_nodes
600619
local RE_nodes
601620

602-
local tier = technic.sw_pos2tier(pos)
603-
local network
604-
if tier then
605-
PR_nodes, BA_nodes, RE_nodes = get_network(network_id, tier)
621+
local network = networks[network_id]
622+
if network then
623+
PR_nodes, BA_nodes, RE_nodes = get_network(network_id, network.tier)
606624
if not PR_nodes or technic.is_overloaded(network_id) then return end
607-
network = networks[network_id]
608625
else
609626
--dprint("Not connected to a network")
610627
technic.network_infotext(network_id, S("@1 Has No Network", S("Switching Station")))
@@ -625,9 +642,9 @@ function technic.network_run(network_id)
625642
run_nodes(BA_nodes, vm, technic.battery, network)
626643

627644
-- Strings for the meta data
628-
local eu_demand_str = tier.."_EU_demand"
629-
local eu_input_str = tier.."_EU_input"
630-
local eu_supply_str = tier.."_EU_supply"
645+
local eu_demand_str = network.tier.."_EU_demand"
646+
local eu_input_str = network.tier.."_EU_input"
647+
local eu_supply_str = network.tier.."_EU_supply"
631648

632649
-- Distribute charge equally across multiple batteries.
633650
local charge_distributed = math.floor(network.BA_charge_active / network.BA_count_active)
@@ -658,20 +675,6 @@ function technic.network_run(network_id)
658675
S("Switching Station"), technic.EU_string(PR_eu_supply),
659676
technic.EU_string(RE_eu_demand)))
660677

661-
-- If mesecon signal and power supply or demand changed then
662-
-- send them via digilines.
663-
if mesecons_path and digilines_path and mesecon.is_powered(pos) then
664-
if PR_eu_supply ~= network.supply or
665-
RE_eu_demand ~= network.demand then
666-
local meta = minetest.get_meta(pos)
667-
local channel = meta:get_string("channel")
668-
digilines.receptor_send(pos, technic.digilines.rules, channel, {
669-
supply = PR_eu_supply,
670-
demand = RE_eu_demand
671-
})
672-
end
673-
end
674-
675678
-- Data that will be used by the power monitor
676679
network.supply = PR_eu_supply
677680
network.demand = RE_eu_demand
@@ -703,7 +706,8 @@ function technic.network_run(network_id)
703706
local t1 = minetest.get_us_time()
704707
local diff = t1 - t0
705708
if diff > 50000 then
706-
minetest.log("warning", "[technic] [+supply] technic_run took " .. diff .. " us at " .. minetest.pos_to_string(pos))
709+
minetest.log("warning", "[technic] [+supply] technic_run took " .. diff .. " us at "
710+
.. minetest.pos_to_string(hashpos(network_id)))
707711
end
708712

709713
return
@@ -733,7 +737,8 @@ function technic.network_run(network_id)
733737
local t1 = minetest.get_us_time()
734738
local diff = t1 - t0
735739
if diff > 50000 then
736-
minetest.log("warning", "[technic] [-supply] technic_run took " .. diff .. " us at " .. minetest.pos_to_string(pos))
740+
minetest.log("warning", "[technic] [-supply] technic_run took " .. diff .. " us at "
741+
.. minetest.pos_to_string(hashpos(network_id)))
737742
end
738743

739744
return
@@ -757,7 +762,8 @@ function technic.network_run(network_id)
757762
local t1 = minetest.get_us_time()
758763
local diff = t1 - t0
759764
if diff > 50000 then
760-
minetest.log("warning", "[technic] technic_run took " .. diff .. " us at " .. minetest.pos_to_string(pos))
765+
minetest.log("warning", "[technic] technic_run took " .. diff .. " us at "
766+
.. minetest.pos_to_string(hashpos(network_id)))
761767
end
762768

763769
end

technic/machines/switching_station.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- See also technic/doc/api.md
22

33
local mesecons_path = minetest.get_modpath("mesecons")
4+
local digilines_path = minetest.get_modpath("digilines")
45

56
local S = technic.getter
67

@@ -20,10 +21,13 @@ local function start_network(pos)
2021
if not tier then
2122
local meta = minetest.get_meta(pos)
2223
meta:set_string("infotext", S("@1 Has No Network", S("Switching Station")))
23-
return
24+
else
25+
local network_id = technic.sw_pos2network(pos) or technic.create_network(pos)
26+
local network = network_id and technic.networks[network_id]
27+
if network and technic.switch_insert(pos, network) > 0 then
28+
technic.activate_network(network_id)
29+
end
2430
end
25-
local network_id = technic.sw_pos2network(pos) or technic.create_network(pos)
26-
technic.activate_network(network_id)
2731
end
2832

2933
local mesecon_def
@@ -55,10 +59,10 @@ minetest.register_node("technic:switching_station",{
5559
minetest.get_node_timer(pos):start(1.0)
5660
end,
5761
on_destruct = function(pos)
58-
-- Remove network when switching station is removed, if
59-
-- there's another switching station network will be rebuilt.
62+
-- Remove network when last switching stations is removed
6063
local network_id = technic.sw_pos2network(pos)
61-
if technic.networks[network_id] then
64+
local network = network_id and technic.networks[network_id]
65+
if network and technic.switch_remove(pos, network) < 1 then
6266
technic.remove_network(network_id)
6367
end
6468
end,
@@ -94,6 +98,19 @@ minetest.register_node("technic:switching_station",{
9498
-- Network exists and is not overloaded, reactivate network
9599
technic.activate_network(network_id)
96100
infotext = technic.network_infotext(network_id)
101+
-- If mesecon signal enabled and power supply or demand changed then send them via digilines.
102+
if mesecons_path and digilines_path and mesecon.is_powered(pos) then
103+
local network = technic.networks[network_id]
104+
if meta:get_int("supply") ~= network.supply or meta:get_int("demand") ~= network.demand then
105+
meta:set_int("supply", network.supply)
106+
meta:set_int("demand", network.demand)
107+
local channel = meta:get_string("channel")
108+
digilines.receptor_send(pos, technic.digilines.rules, channel, {
109+
supply = network.supply,
110+
demand = network.demand
111+
})
112+
end
113+
end
97114
end
98115
meta:set_string("infotext", infotext)
99116
else

technic/machines/switching_station_globalstep.lua

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,7 @@ minetest.register_globalstep(function(dtime)
4949
local active_switches = 0
5050

5151
for network_id, network in pairs(technic.active_networks) do
52-
local pos = technic.network2sw_pos(network_id)
53-
54-
local node = technic.get_or_load_node(pos) or minetest.get_node(pos)
55-
56-
if node.name ~= "technic:switching_station" then
57-
-- station vanished
58-
technic.remove_network(network_id)
59-
60-
elseif network.timeout > now and not technic.is_overloaded(network_id) then
52+
if network.timeout > now and not technic.is_overloaded(network_id) then
6153
-- station active
6254
active_switches = active_switches + 1
6355

@@ -95,9 +87,8 @@ minetest.register_globalstep(function(dtime)
9587
end
9688

9789
else
98-
-- station timed out
90+
-- network timed out
9991
technic.active_networks[network_id] = nil
100-
10192
end
10293
end
10394

0 commit comments

Comments
 (0)