commit | author | age
|
ee5c6c
|
1 |
-- SWITCHING STATION |
K |
2 |
-- The switching station is the center of all power distribution on an electric network. |
7cfb38
|
3 |
-- |
KZ |
4 |
-- The station collects power from sources (PR), distributes it to sinks (RE), |
|
5 |
-- and uses the excess/shortfall to charge and discharge batteries (BA). |
|
6 |
-- |
|
7 |
-- For now, all supply and demand values are expressed in kW. |
ee5c6c
|
8 |
-- |
K |
9 |
-- It works like this: |
|
10 |
-- All PR,BA,RE nodes are indexed and tagged with the switching station. |
|
11 |
-- The tagging is to allow more stations to be built without allowing a cheat |
|
12 |
-- with duplicating power. |
|
13 |
-- All the RE nodes are queried for their current EU demand. Those which are off |
|
14 |
-- would require no or a small standby EU demand, while those which are on would |
|
15 |
-- require more. |
|
16 |
-- If the total demand is less than the available power they are all updated with the |
|
17 |
-- demand number. |
|
18 |
-- If any surplus exists from the PR nodes the batteries will be charged evenly with this. |
|
19 |
-- If the total demand requires draw on the batteries they will be discharged evenly. |
|
20 |
-- |
|
21 |
-- If the total demand is more than the available power all RE nodes will be shut down. |
|
22 |
-- We have a brown-out situation. |
|
23 |
-- |
|
24 |
-- Hence all the power distribution logic resides in this single node. |
|
25 |
-- |
|
26 |
-- Nodes connected to the network will have one or more of these parameters as meta data: |
|
27 |
-- <LV|MV|HV>_EU_supply : Exists for PR and BA node types. This is the EU value supplied by the node. Output |
|
28 |
-- <LV|MV|HV>_EU_demand : Exists for RE and BA node types. This is the EU value the node requires to run. Output |
|
29 |
-- <LV|MV|HV>_EU_input : Exists for RE and BA node types. This is the actual EU value the network can give the node. Input |
|
30 |
-- |
|
31 |
-- The reason the LV|MV|HV type is prepended toe meta data is because some machine could require several supplies to work. |
|
32 |
-- This way the supplies are separated per network. |
|
33 |
|
468d79
|
34 |
technic.networks = {} |
be2f30
|
35 |
|
S |
36 |
local S = technic.getter |
468d79
|
37 |
|
ee0765
|
38 |
minetest.register_craft({ |
S |
39 |
output = "technic:switching_station", |
|
40 |
recipe = { |
83c649
|
41 |
{"", "technic:lv_transformer", ""}, |
S |
42 |
{"default:copper_ingot", "technic:machine_casing", "default:copper_ingot"}, |
|
43 |
{"technic:lv_cable", "technic:lv_cable", "technic:lv_cable"} |
ee0765
|
44 |
} |
S |
45 |
}) |
ee5c6c
|
46 |
|
ee0765
|
47 |
minetest.register_node("technic:switching_station",{ |
be2f30
|
48 |
description = S("Switching Station"), |
ee0765
|
49 |
tiles = {"technic_water_mill_top_active.png", "technic_water_mill_top_active.png", |
S |
50 |
"technic_water_mill_top_active.png", "technic_water_mill_top_active.png", |
|
51 |
"technic_water_mill_top_active.png", "technic_water_mill_top_active.png"}, |
83c649
|
52 |
groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_all_tiers=1}, |
S |
53 |
connect_sides = {"bottom"}, |
ee0765
|
54 |
sounds = default.node_sound_wood_defaults(), |
S |
55 |
on_construct = function(pos) |
|
56 |
local meta = minetest.get_meta(pos) |
be2f30
|
57 |
meta:set_string("infotext", S("Switching Station")) |
563a4c
|
58 |
meta:set_string("active", 1) |
ee0765
|
59 |
end, |
S |
60 |
}) |
ee5c6c
|
61 |
|
K |
62 |
-------------------------------------------------- |
|
63 |
-- Functions to traverse the electrical network |
|
64 |
-------------------------------------------------- |
|
65 |
|
|
66 |
-- Add a wire node to the LV/MV/HV network |
ee0765
|
67 |
local add_new_cable_node = function(nodes, pos) |
S |
68 |
-- Ignore if the node has already been added |
|
69 |
for i = 1, #nodes do |
|
70 |
if pos.x == nodes[i].x and |
|
71 |
pos.y == nodes[i].y and |
|
72 |
pos.z == nodes[i].z then |
|
73 |
return false |
|
74 |
end |
|
75 |
end |
|
76 |
table.insert(nodes, {x=pos.x, y=pos.y, z=pos.z, visited=1}) |
|
77 |
return true |
|
78 |
end |
ee5c6c
|
79 |
|
K |
80 |
-- Generic function to add found connected nodes to the right classification array |
9444ef
|
81 |
local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below) |
c38da0
|
82 |
technic.get_or_load_node(pos) |
ee0765
|
83 |
local meta = minetest.get_meta(pos) |
S |
84 |
local name = minetest.get_node(pos).name |
|
85 |
|
|
86 |
if technic.is_tier_cable(name, tier) then |
|
87 |
add_new_cable_node(all_nodes, pos) |
|
88 |
elseif machines[name] then |
|
89 |
--dprint(name.." is a "..machines[name]) |
|
90 |
if machines[name] == technic.producer then |
|
91 |
add_new_cable_node(PR_nodes, pos) |
|
92 |
elseif machines[name] == technic.receiver then |
|
93 |
add_new_cable_node(RE_nodes, pos) |
623fca
|
94 |
elseif machines[name] == technic.producer_receiver then |
Z |
95 |
add_new_cable_node(PR_nodes, pos) |
|
96 |
add_new_cable_node(RE_nodes, pos) |
563a4c
|
97 |
elseif machines[name] == "SPECIAL" and |
9444ef
|
98 |
(pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) and |
E |
99 |
from_below then |
563a4c
|
100 |
-- Another switching station -> disable it |
N |
101 |
add_new_cable_node(SP_nodes, pos) |
|
102 |
meta:set_int("active", 0) |
|
103 |
meta:set_string("active_pos", minetest.serialize(sw_pos)) |
ee0765
|
104 |
elseif machines[name] == technic.battery then |
S |
105 |
add_new_cable_node(BA_nodes, pos) |
|
106 |
end |
|
107 |
|
|
108 |
meta:set_int(tier.."_EU_timeout", 2) -- Touch node |
|
109 |
end |
|
110 |
end |
ee5c6c
|
111 |
|
K |
112 |
-- Traverse a network given a list of machines and a cable type name |
563a4c
|
113 |
local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos) |
ee0765
|
114 |
local pos = all_nodes[i] |
S |
115 |
local positions = { |
|
116 |
{x=pos.x+1, y=pos.y, z=pos.z}, |
|
117 |
{x=pos.x-1, y=pos.y, z=pos.z}, |
|
118 |
{x=pos.x, y=pos.y+1, z=pos.z}, |
|
119 |
{x=pos.x, y=pos.y-1, z=pos.z}, |
|
120 |
{x=pos.x, y=pos.y, z=pos.z+1}, |
|
121 |
{x=pos.x, y=pos.y, z=pos.z-1}} |
|
122 |
--print("ON") |
|
123 |
for i, cur_pos in pairs(positions) do |
9444ef
|
124 |
check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3) |
ee0765
|
125 |
end |
S |
126 |
end |
ee5c6c
|
127 |
|
f4ac2b
|
128 |
local touch_nodes = function(list, tier) |
N |
129 |
for _, pos in ipairs(list) do |
|
130 |
local meta = minetest.get_meta(pos) |
|
131 |
meta:set_int(tier.."_EU_timeout", 2) -- Touch node |
|
132 |
end |
|
133 |
end |
|
134 |
|
563a4c
|
135 |
local get_network = function(sw_pos, pos1, tier) |
12d29c
|
136 |
local cached = technic.networks[minetest.hash_node_position(pos1)] |
f4ac2b
|
137 |
if cached and cached.tier == tier then |
N |
138 |
touch_nodes(cached.PR_nodes, tier) |
|
139 |
touch_nodes(cached.BA_nodes, tier) |
|
140 |
touch_nodes(cached.RE_nodes, tier) |
563a4c
|
141 |
for _, pos in ipairs(cached.SP_nodes) do |
N |
142 |
local meta = minetest.get_meta(pos) |
|
143 |
meta:set_int("active", 0) |
|
144 |
meta:set_string("active_pos", minetest.serialize(sw_pos)) |
|
145 |
end |
f4ac2b
|
146 |
return cached.PR_nodes, cached.BA_nodes, cached.RE_nodes |
N |
147 |
end |
|
148 |
local i = 1 |
|
149 |
local PR_nodes = {} |
|
150 |
local BA_nodes = {} |
|
151 |
local RE_nodes = {} |
563a4c
|
152 |
local SP_nodes = {} |
f4ac2b
|
153 |
local all_nodes = {pos1} |
N |
154 |
repeat |
563a4c
|
155 |
traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, |
N |
156 |
i, technic.machines[tier], tier, sw_pos) |
f4ac2b
|
157 |
i = i + 1 |
N |
158 |
until all_nodes[i] == nil |
563a4c
|
159 |
technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes, |
N |
160 |
RE_nodes = RE_nodes, BA_nodes = BA_nodes, SP_nodes = SP_nodes} |
f4ac2b
|
161 |
return PR_nodes, BA_nodes, RE_nodes |
N |
162 |
end |
|
163 |
|
ee0765
|
164 |
----------------------------------------------- |
S |
165 |
-- The action code for the switching station -- |
|
166 |
----------------------------------------------- |
|
167 |
minetest.register_abm({ |
|
168 |
nodenames = {"technic:switching_station"}, |
c6464d
|
169 |
label = "Switching Station", -- allows the mtt profiler to profile this abm individually |
ee5c6c
|
170 |
interval = 1, |
K |
171 |
chance = 1, |
|
172 |
action = function(pos, node, active_object_count, active_object_count_wider) |
ee0765
|
173 |
local meta = minetest.get_meta(pos) |
S |
174 |
local meta1 = nil |
|
175 |
local pos1 = {} |
|
176 |
local PR_EU = 0 -- EUs from PR nodes |
|
177 |
local BA_PR_EU = 0 -- EUs from BA nodes (discharching) |
|
178 |
local BA_RE_EU = 0 -- EUs to BA nodes (charging) |
|
179 |
local RE_EU = 0 -- EUs to RE nodes |
ee5c6c
|
180 |
|
ee0765
|
181 |
local tier = "" |
f4ac2b
|
182 |
local PR_nodes |
N |
183 |
local BA_nodes |
|
184 |
local RE_nodes |
be2f30
|
185 |
local machine_name = S("Switching Station") |
ee5c6c
|
186 |
|
563a4c
|
187 |
if meta:get_int("active") ~= 1 then |
N |
188 |
meta:set_int("active", 1) |
|
189 |
local active_pos = minetest.deserialize(meta:get_string("active_pos")) |
|
190 |
if active_pos then |
|
191 |
local meta1 = minetest.get_meta(active_pos) |
|
192 |
meta:set_string("infotext", S("%s (Slave)"):format(meta1:get_string("infotext"))) |
|
193 |
end |
|
194 |
return |
|
195 |
end |
|
196 |
|
ee0765
|
197 |
-- Which kind of network are we on: |
S |
198 |
pos1 = {x=pos.x, y=pos.y-1, z=pos.z} |
ee5c6c
|
199 |
|
ee0765
|
200 |
local name = minetest.get_node(pos1).name |
S |
201 |
local tier = technic.get_cable_tier(name) |
|
202 |
if tier then |
563a4c
|
203 |
PR_nodes, BA_nodes, RE_nodes = get_network(pos, pos1, tier) |
ee0765
|
204 |
else |
S |
205 |
--dprint("Not connected to a network") |
be2f30
|
206 |
meta:set_string("infotext", S("%s Has No Network"):format(machine_name)) |
ee0765
|
207 |
return |
S |
208 |
end |
563a4c
|
209 |
|
N |
210 |
-- Run all the nodes |
|
211 |
local function run_nodes(list) |
|
212 |
for _, pos2 in ipairs(list) do |
c38da0
|
213 |
technic.get_or_load_node(pos2) |
563a4c
|
214 |
local node2 = minetest.get_node(pos2) |
N |
215 |
local nodedef |
|
216 |
if node2 and node2.name then |
|
217 |
nodedef = minetest.registered_nodes[node2.name] |
|
218 |
end |
|
219 |
if nodedef and nodedef.technic_run then |
|
220 |
nodedef.technic_run(pos2, node2) |
|
221 |
end |
|
222 |
end |
|
223 |
end |
|
224 |
|
|
225 |
run_nodes(PR_nodes) |
|
226 |
run_nodes(RE_nodes) |
|
227 |
run_nodes(BA_nodes) |
ee5c6c
|
228 |
|
ee0765
|
229 |
-- Strings for the meta data |
S |
230 |
local eu_demand_str = tier.."_EU_demand" |
|
231 |
local eu_input_str = tier.."_EU_input" |
|
232 |
local eu_supply_str = tier.."_EU_supply" |
ee5c6c
|
233 |
|
7cfb38
|
234 |
-- Distribute charge equally across multiple batteries. |
KZ |
235 |
local charge_total = 0 |
|
236 |
local battery_count = 0 |
|
237 |
|
|
238 |
for n, pos1 in pairs(BA_nodes) do |
|
239 |
meta1 = minetest.get_meta(pos1) |
|
240 |
local charge = meta1:get_int("internal_EU_charge") |
|
241 |
|
|
242 |
if (meta1:get_int(eu_demand_str) ~= 0) then |
|
243 |
charge_total = charge_total + charge |
|
244 |
battery_count = battery_count + 1 |
|
245 |
end |
|
246 |
end |
|
247 |
|
|
248 |
local charge_distributed = math.floor(charge_total / battery_count) |
|
249 |
|
|
250 |
for n, pos1 in pairs(BA_nodes) do |
|
251 |
meta1 = minetest.get_meta(pos1) |
|
252 |
|
|
253 |
if (meta1:get_int(eu_demand_str) ~= 0) then |
|
254 |
meta1:set_int("internal_EU_charge", charge_distributed) |
|
255 |
end |
|
256 |
end |
|
257 |
|
ee0765
|
258 |
-- Get all the power from the PR nodes |
S |
259 |
local PR_eu_supply = 0 -- Total power |
|
260 |
for _, pos1 in pairs(PR_nodes) do |
|
261 |
meta1 = minetest.get_meta(pos1) |
|
262 |
PR_eu_supply = PR_eu_supply + meta1:get_int(eu_supply_str) |
|
263 |
end |
|
264 |
--dprint("Total PR supply:"..PR_eu_supply) |
ee5c6c
|
265 |
|
ee0765
|
266 |
-- Get all the demand from the RE nodes |
S |
267 |
local RE_eu_demand = 0 |
|
268 |
for _, pos1 in pairs(RE_nodes) do |
|
269 |
meta1 = minetest.get_meta(pos1) |
|
270 |
RE_eu_demand = RE_eu_demand + meta1:get_int(eu_demand_str) |
|
271 |
end |
|
272 |
--dprint("Total RE demand:"..RE_eu_demand) |
ee5c6c
|
273 |
|
ee0765
|
274 |
-- Get all the power from the BA nodes |
S |
275 |
local BA_eu_supply = 0 |
|
276 |
for _, pos1 in pairs(BA_nodes) do |
|
277 |
meta1 = minetest.get_meta(pos1) |
|
278 |
BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str) |
|
279 |
end |
|
280 |
--dprint("Total BA supply:"..BA_eu_supply) |
ee5c6c
|
281 |
|
ee0765
|
282 |
-- Get all the demand from the BA nodes |
S |
283 |
local BA_eu_demand = 0 |
|
284 |
for _, pos1 in pairs(BA_nodes) do |
|
285 |
meta1 = minetest.get_meta(pos1) |
|
286 |
BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str) |
|
287 |
end |
|
288 |
--dprint("Total BA demand:"..BA_eu_demand) |
ee5c6c
|
289 |
|
ee0765
|
290 |
meta:set_string("infotext", |
4b1798
|
291 |
S("@1. Supply: @2 Demand: @3", |
85a984
|
292 |
machine_name, technic.pretty_num(PR_eu_supply), technic.pretty_num(RE_eu_demand))) |
ee5c6c
|
293 |
|
ee0765
|
294 |
-- If the PR supply is enough for the RE demand supply them all |
S |
295 |
if PR_eu_supply >= RE_eu_demand then |
|
296 |
--dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand) |
|
297 |
for _, pos1 in pairs(RE_nodes) do |
|
298 |
meta1 = minetest.get_meta(pos1) |
|
299 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
300 |
meta1:set_int(eu_input_str, eu_demand) |
|
301 |
end |
|
302 |
-- We have a surplus, so distribute the rest equally to the BA nodes |
|
303 |
-- Let's calculate the factor of the demand |
|
304 |
PR_eu_supply = PR_eu_supply - RE_eu_demand |
|
305 |
local charge_factor = 0 -- Assume all batteries fully charged |
|
306 |
if BA_eu_demand > 0 then |
|
307 |
charge_factor = PR_eu_supply / BA_eu_demand |
|
308 |
end |
|
309 |
for n, pos1 in pairs(BA_nodes) do |
|
310 |
meta1 = minetest.get_meta(pos1) |
|
311 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
312 |
meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor)) |
|
313 |
--dprint("Charging battery:"..math.floor(eu_demand*charge_factor)) |
|
314 |
end |
|
315 |
return |
|
316 |
end |
ee5c6c
|
317 |
|
ee0765
|
318 |
-- If the PR supply is not enough for the RE demand we will discharge the batteries too |
S |
319 |
if PR_eu_supply + BA_eu_supply >= RE_eu_demand then |
|
320 |
--dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand) |
|
321 |
for _, pos1 in pairs(RE_nodes) do |
|
322 |
meta1 = minetest.get_meta(pos1) |
|
323 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
324 |
meta1:set_int(eu_input_str, eu_demand) |
|
325 |
end |
|
326 |
-- We have a deficit, so distribute to the BA nodes |
|
327 |
-- Let's calculate the factor of the supply |
|
328 |
local charge_factor = 0 -- Assume all batteries depleted |
|
329 |
if BA_eu_supply > 0 then |
|
330 |
charge_factor = (PR_eu_supply - RE_eu_demand) / BA_eu_supply |
|
331 |
end |
|
332 |
for n,pos1 in pairs(BA_nodes) do |
|
333 |
meta1 = minetest.get_meta(pos1) |
ee5c6c
|
334 |
local eu_supply = meta1:get_int(eu_supply_str) |
ee0765
|
335 |
meta1:set_int(eu_input_str, math.floor(eu_supply * charge_factor)) |
S |
336 |
--dprint("Discharging battery:"..math.floor(eu_supply*charge_factor)) |
|
337 |
end |
|
338 |
return |
|
339 |
end |
ee5c6c
|
340 |
|
ee0765
|
341 |
-- If the PR+BA supply is not enough for the RE demand: Power only the batteries |
S |
342 |
local charge_factor = 0 -- Assume all batteries fully charged |
|
343 |
if BA_eu_demand > 0 then |
|
344 |
charge_factor = PR_eu_supply / BA_eu_demand |
|
345 |
end |
|
346 |
for n, pos1 in pairs(BA_nodes) do |
|
347 |
meta1 = minetest.get_meta(pos1) |
|
348 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
349 |
meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor)) |
|
350 |
end |
|
351 |
for n, pos1 in pairs(RE_nodes) do |
|
352 |
meta1 = minetest.get_meta(pos1) |
|
353 |
meta1:set_int(eu_input_str, 0) |
|
354 |
end |
ee5c6c
|
355 |
end, |
K |
356 |
}) |
ee0765
|
357 |
|
563a4c
|
358 |
-- Timeout ABM |
N |
359 |
-- Timeout for a node in case it was disconnected from the network |
|
360 |
-- A node must be touched by the station continuously in order to function |
|
361 |
local function switching_station_timeout_count(pos, tier) |
|
362 |
local meta = minetest.get_meta(pos) |
|
363 |
local timeout = meta:get_int(tier.."_EU_timeout") |
|
364 |
if timeout <= 0 then |
4ac36e
|
365 |
meta:set_int(tier.."_EU_input", 0) -- Not needed anymore <-- actually, it is for supply converter |
563a4c
|
366 |
return true |
N |
367 |
else |
|
368 |
meta:set_int(tier.."_EU_timeout", timeout - 1) |
|
369 |
return false |
|
370 |
end |
|
371 |
end |
|
372 |
minetest.register_abm({ |
|
373 |
nodenames = {"group:technic_machine"}, |
|
374 |
interval = 1, |
|
375 |
chance = 1, |
|
376 |
action = function(pos, node, active_object_count, active_object_count_wider) |
|
377 |
for tier, machines in pairs(technic.machines) do |
|
378 |
if machines[node.name] and switching_station_timeout_count(pos, tier) then |
|
379 |
local nodedef = minetest.registered_nodes[node.name] |
|
380 |
if nodedef and nodedef.technic_disabled_machine_name then |
|
381 |
node.name = nodedef.technic_disabled_machine_name |
|
382 |
minetest.swap_node(pos, node) |
1c617f
|
383 |
elseif nodedef and nodedef.technic_on_disable then |
N |
384 |
nodedef.technic_on_disable(pos, node) |
563a4c
|
385 |
end |
N |
386 |
if nodedef then |
|
387 |
local meta = minetest.get_meta(pos) |
|
388 |
meta:set_string("infotext", S("%s Has No Network"):format(nodedef.description)) |
|
389 |
end |
|
390 |
end |
|
391 |
end |
|
392 |
end, |
|
393 |
}) |
|
394 |
|
ee0765
|
395 |
for tier, machines in pairs(technic.machines) do |
S |
396 |
-- SPECIAL will not be traversed |
|
397 |
technic.register_machine(tier, "technic:switching_station", "SPECIAL") |
|
398 |
end |
|
399 |
|