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 = {} |
d3f40e
|
35 |
technic.cables = {} |
be2f30
|
36 |
|
6abd85
|
37 |
local mesecons_path = minetest.get_modpath("mesecons") |
D |
38 |
local digilines_path = minetest.get_modpath("digilines") |
|
39 |
|
be2f30
|
40 |
local S = technic.getter |
468d79
|
41 |
|
ee0765
|
42 |
minetest.register_craft({ |
S |
43 |
output = "technic:switching_station", |
|
44 |
recipe = { |
83c649
|
45 |
{"", "technic:lv_transformer", ""}, |
S |
46 |
{"default:copper_ingot", "technic:machine_casing", "default:copper_ingot"}, |
|
47 |
{"technic:lv_cable", "technic:lv_cable", "technic:lv_cable"} |
ee0765
|
48 |
} |
S |
49 |
}) |
ee5c6c
|
50 |
|
338f3b
|
51 |
local mesecon_def |
D |
52 |
if mesecons_path then |
|
53 |
mesecon_def = {effector = { |
|
54 |
rules = mesecon.rules.default, |
|
55 |
}} |
|
56 |
end |
|
57 |
|
ee0765
|
58 |
minetest.register_node("technic:switching_station",{ |
be2f30
|
59 |
description = S("Switching Station"), |
ee0765
|
60 |
tiles = {"technic_water_mill_top_active.png", "technic_water_mill_top_active.png", |
S |
61 |
"technic_water_mill_top_active.png", "technic_water_mill_top_active.png", |
|
62 |
"technic_water_mill_top_active.png", "technic_water_mill_top_active.png"}, |
83c649
|
63 |
groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_all_tiers=1}, |
S |
64 |
connect_sides = {"bottom"}, |
ee0765
|
65 |
sounds = default.node_sound_wood_defaults(), |
S |
66 |
on_construct = function(pos) |
|
67 |
local meta = minetest.get_meta(pos) |
be2f30
|
68 |
meta:set_string("infotext", S("Switching Station")) |
563a4c
|
69 |
meta:set_string("active", 1) |
6abd85
|
70 |
meta:set_string("channel", "switching_station"..minetest.pos_to_string(pos)) |
D |
71 |
meta:set_string("formspec", "field[channel;Channel;${channel}]") |
ee0765
|
72 |
end, |
088eea
|
73 |
after_dig_node = function(pos) |
CK |
74 |
minetest.forceload_free_block(pos) |
|
75 |
pos.y = pos.y - 1 |
|
76 |
minetest.forceload_free_block(pos) |
|
77 |
end, |
6abd85
|
78 |
on_receive_fields = function(pos, formname, fields, sender) |
D |
79 |
if not fields.channel then |
|
80 |
return |
|
81 |
end |
|
82 |
local plname = sender:get_player_name() |
|
83 |
if minetest.is_protected(pos, plname) then |
|
84 |
minetest.record_protection_violation(pos, plname) |
|
85 |
return |
|
86 |
end |
|
87 |
local meta = minetest.get_meta(pos) |
|
88 |
meta:set_string("channel", fields.channel) |
|
89 |
end, |
338f3b
|
90 |
mesecons = mesecon_def, |
6abd85
|
91 |
digiline = { |
D |
92 |
receptor = {action = function() end}, |
|
93 |
effector = { |
|
94 |
action = function(pos, node, channel, msg) |
|
95 |
if msg ~= "GET" and msg ~= "get" then |
|
96 |
return |
|
97 |
end |
|
98 |
local meta = minetest.get_meta(pos) |
|
99 |
if channel ~= meta:get_string("channel") then |
|
100 |
return |
|
101 |
end |
|
102 |
digilines.receptor_send(pos, digilines.rules.default, channel, { |
|
103 |
supply = meta:get_int("supply"), |
|
104 |
demand = meta:get_int("demand") |
|
105 |
}) |
|
106 |
end |
|
107 |
}, |
|
108 |
}, |
ee0765
|
109 |
}) |
ee5c6c
|
110 |
|
K |
111 |
-------------------------------------------------- |
|
112 |
-- Functions to traverse the electrical network |
|
113 |
-------------------------------------------------- |
|
114 |
|
|
115 |
-- Add a wire node to the LV/MV/HV network |
d3f40e
|
116 |
local add_new_cable_node = function(nodes, pos, network_id) |
CK |
117 |
technic.cables[minetest.hash_node_position(pos)] = network_id |
ee0765
|
118 |
-- Ignore if the node has already been added |
S |
119 |
for i = 1, #nodes do |
|
120 |
if pos.x == nodes[i].x and |
|
121 |
pos.y == nodes[i].y and |
|
122 |
pos.z == nodes[i].z then |
|
123 |
return false |
|
124 |
end |
|
125 |
end |
|
126 |
table.insert(nodes, {x=pos.x, y=pos.y, z=pos.z, visited=1}) |
|
127 |
return true |
|
128 |
end |
ee5c6c
|
129 |
|
K |
130 |
-- Generic function to add found connected nodes to the right classification array |
d3f40e
|
131 |
local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below, network_id) |
c38da0
|
132 |
technic.get_or_load_node(pos) |
ee0765
|
133 |
local meta = minetest.get_meta(pos) |
S |
134 |
local name = minetest.get_node(pos).name |
|
135 |
|
|
136 |
if technic.is_tier_cable(name, tier) then |
d3f40e
|
137 |
add_new_cable_node(all_nodes, pos,network_id) |
ee0765
|
138 |
elseif machines[name] then |
S |
139 |
--dprint(name.." is a "..machines[name]) |
088eea
|
140 |
meta:set_string(tier.."_network",minetest.pos_to_string(sw_pos)) |
ee0765
|
141 |
if machines[name] == technic.producer then |
d3f40e
|
142 |
add_new_cable_node(PR_nodes, pos, network_id) |
ee0765
|
143 |
elseif machines[name] == technic.receiver then |
d3f40e
|
144 |
add_new_cable_node(RE_nodes, pos, network_id) |
623fca
|
145 |
elseif machines[name] == technic.producer_receiver then |
d3f40e
|
146 |
add_new_cable_node(PR_nodes, pos, network_id) |
CK |
147 |
add_new_cable_node(RE_nodes, pos, network_id) |
563a4c
|
148 |
elseif machines[name] == "SPECIAL" and |
9444ef
|
149 |
(pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) and |
E |
150 |
from_below then |
563a4c
|
151 |
-- Another switching station -> disable it |
d3f40e
|
152 |
add_new_cable_node(SP_nodes, pos, network_id) |
563a4c
|
153 |
meta:set_int("active", 0) |
ee0765
|
154 |
elseif machines[name] == technic.battery then |
d3f40e
|
155 |
add_new_cable_node(BA_nodes, pos, network_id) |
ee0765
|
156 |
end |
S |
157 |
|
|
158 |
meta:set_int(tier.."_EU_timeout", 2) -- Touch node |
|
159 |
end |
|
160 |
end |
ee5c6c
|
161 |
|
K |
162 |
-- Traverse a network given a list of machines and a cable type name |
d3f40e
|
163 |
local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos, network_id) |
ee0765
|
164 |
local pos = all_nodes[i] |
S |
165 |
local positions = { |
|
166 |
{x=pos.x+1, y=pos.y, z=pos.z}, |
|
167 |
{x=pos.x-1, y=pos.y, z=pos.z}, |
|
168 |
{x=pos.x, y=pos.y+1, z=pos.z}, |
|
169 |
{x=pos.x, y=pos.y-1, z=pos.z}, |
|
170 |
{x=pos.x, y=pos.y, z=pos.z+1}, |
|
171 |
{x=pos.x, y=pos.y, z=pos.z-1}} |
|
172 |
--print("ON") |
|
173 |
for i, cur_pos in pairs(positions) do |
d3f40e
|
174 |
check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3, network_id) |
ee0765
|
175 |
end |
S |
176 |
end |
ee5c6c
|
177 |
|
f4ac2b
|
178 |
local touch_nodes = function(list, tier) |
N |
179 |
for _, pos in ipairs(list) do |
|
180 |
local meta = minetest.get_meta(pos) |
|
181 |
meta:set_int(tier.."_EU_timeout", 2) -- Touch node |
|
182 |
end |
|
183 |
end |
|
184 |
|
563a4c
|
185 |
local get_network = function(sw_pos, pos1, tier) |
12d29c
|
186 |
local cached = technic.networks[minetest.hash_node_position(pos1)] |
f4ac2b
|
187 |
if cached and cached.tier == tier then |
N |
188 |
touch_nodes(cached.PR_nodes, tier) |
|
189 |
touch_nodes(cached.BA_nodes, tier) |
|
190 |
touch_nodes(cached.RE_nodes, tier) |
563a4c
|
191 |
for _, pos in ipairs(cached.SP_nodes) do |
N |
192 |
local meta = minetest.get_meta(pos) |
|
193 |
meta:set_int("active", 0) |
|
194 |
meta:set_string("active_pos", minetest.serialize(sw_pos)) |
|
195 |
end |
f4ac2b
|
196 |
return cached.PR_nodes, cached.BA_nodes, cached.RE_nodes |
N |
197 |
end |
|
198 |
local i = 1 |
|
199 |
local PR_nodes = {} |
|
200 |
local BA_nodes = {} |
|
201 |
local RE_nodes = {} |
563a4c
|
202 |
local SP_nodes = {} |
f4ac2b
|
203 |
local all_nodes = {pos1} |
N |
204 |
repeat |
563a4c
|
205 |
traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, |
d3f40e
|
206 |
i, technic.machines[tier], tier, sw_pos, minetest.hash_node_position(pos1)) |
f4ac2b
|
207 |
i = i + 1 |
N |
208 |
until all_nodes[i] == nil |
563a4c
|
209 |
technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes, |
088eea
|
210 |
RE_nodes = RE_nodes, BA_nodes = BA_nodes, SP_nodes = SP_nodes, all_nodes = all_nodes} |
f4ac2b
|
211 |
return PR_nodes, BA_nodes, RE_nodes |
N |
212 |
end |
|
213 |
|
ee0765
|
214 |
----------------------------------------------- |
S |
215 |
-- The action code for the switching station -- |
|
216 |
----------------------------------------------- |
|
217 |
minetest.register_abm({ |
|
218 |
nodenames = {"technic:switching_station"}, |
c6464d
|
219 |
label = "Switching Station", -- allows the mtt profiler to profile this abm individually |
ee5c6c
|
220 |
interval = 1, |
K |
221 |
chance = 1, |
|
222 |
action = function(pos, node, active_object_count, active_object_count_wider) |
ee0765
|
223 |
local meta = minetest.get_meta(pos) |
S |
224 |
local meta1 = nil |
|
225 |
local pos1 = {} |
|
226 |
local PR_EU = 0 -- EUs from PR nodes |
|
227 |
local BA_PR_EU = 0 -- EUs from BA nodes (discharching) |
|
228 |
local BA_RE_EU = 0 -- EUs to BA nodes (charging) |
|
229 |
local RE_EU = 0 -- EUs to RE nodes |
ee5c6c
|
230 |
|
ee0765
|
231 |
local tier = "" |
f4ac2b
|
232 |
local PR_nodes |
N |
233 |
local BA_nodes |
|
234 |
local RE_nodes |
be2f30
|
235 |
local machine_name = S("Switching Station") |
6abd85
|
236 |
|
ee0765
|
237 |
-- Which kind of network are we on: |
S |
238 |
pos1 = {x=pos.x, y=pos.y-1, z=pos.z} |
ee5c6c
|
239 |
|
088eea
|
240 |
--Disable if necessary |
CK |
241 |
if meta:get_int("active") ~= 1 then |
|
242 |
minetest.forceload_free_block(pos) |
|
243 |
minetest.forceload_free_block(pos1) |
|
244 |
meta:set_string("infotext",S("%s Already Present"):format(machine_name)) |
|
245 |
return |
|
246 |
end |
|
247 |
|
ee0765
|
248 |
local name = minetest.get_node(pos1).name |
S |
249 |
local tier = technic.get_cable_tier(name) |
|
250 |
if tier then |
088eea
|
251 |
-- Forceload switching station |
CK |
252 |
minetest.forceload_block(pos) |
|
253 |
minetest.forceload_block(pos1) |
563a4c
|
254 |
PR_nodes, BA_nodes, RE_nodes = get_network(pos, pos1, tier) |
ee0765
|
255 |
else |
S |
256 |
--dprint("Not connected to a network") |
be2f30
|
257 |
meta:set_string("infotext", S("%s Has No Network"):format(machine_name)) |
088eea
|
258 |
minetest.forceload_free_block(pos) |
CK |
259 |
minetest.forceload_free_block(pos1) |
ee0765
|
260 |
return |
S |
261 |
end |
6abd85
|
262 |
|
563a4c
|
263 |
-- Run all the nodes |
10307f
|
264 |
local function run_nodes(list, run_stage) |
563a4c
|
265 |
for _, pos2 in ipairs(list) do |
c38da0
|
266 |
technic.get_or_load_node(pos2) |
563a4c
|
267 |
local node2 = minetest.get_node(pos2) |
N |
268 |
local nodedef |
|
269 |
if node2 and node2.name then |
|
270 |
nodedef = minetest.registered_nodes[node2.name] |
|
271 |
end |
|
272 |
if nodedef and nodedef.technic_run then |
10307f
|
273 |
nodedef.technic_run(pos2, node2, run_stage) |
563a4c
|
274 |
end |
N |
275 |
end |
|
276 |
end |
6abd85
|
277 |
|
10307f
|
278 |
run_nodes(PR_nodes, technic.producer) |
M'P |
279 |
run_nodes(RE_nodes, technic.receiver) |
|
280 |
run_nodes(BA_nodes, technic.battery) |
ee5c6c
|
281 |
|
ee0765
|
282 |
-- Strings for the meta data |
S |
283 |
local eu_demand_str = tier.."_EU_demand" |
|
284 |
local eu_input_str = tier.."_EU_input" |
|
285 |
local eu_supply_str = tier.."_EU_supply" |
ee5c6c
|
286 |
|
7cfb38
|
287 |
-- Distribute charge equally across multiple batteries. |
KZ |
288 |
local charge_total = 0 |
|
289 |
local battery_count = 0 |
|
290 |
|
|
291 |
for n, pos1 in pairs(BA_nodes) do |
|
292 |
meta1 = minetest.get_meta(pos1) |
|
293 |
local charge = meta1:get_int("internal_EU_charge") |
|
294 |
|
|
295 |
if (meta1:get_int(eu_demand_str) ~= 0) then |
|
296 |
charge_total = charge_total + charge |
|
297 |
battery_count = battery_count + 1 |
|
298 |
end |
|
299 |
end |
|
300 |
|
|
301 |
local charge_distributed = math.floor(charge_total / battery_count) |
|
302 |
|
|
303 |
for n, pos1 in pairs(BA_nodes) do |
|
304 |
meta1 = minetest.get_meta(pos1) |
|
305 |
|
|
306 |
if (meta1:get_int(eu_demand_str) ~= 0) then |
|
307 |
meta1:set_int("internal_EU_charge", charge_distributed) |
|
308 |
end |
|
309 |
end |
|
310 |
|
ee0765
|
311 |
-- Get all the power from the PR nodes |
S |
312 |
local PR_eu_supply = 0 -- Total power |
|
313 |
for _, pos1 in pairs(PR_nodes) do |
|
314 |
meta1 = minetest.get_meta(pos1) |
|
315 |
PR_eu_supply = PR_eu_supply + meta1:get_int(eu_supply_str) |
|
316 |
end |
|
317 |
--dprint("Total PR supply:"..PR_eu_supply) |
ee5c6c
|
318 |
|
ee0765
|
319 |
-- Get all the demand from the RE nodes |
S |
320 |
local RE_eu_demand = 0 |
|
321 |
for _, pos1 in pairs(RE_nodes) do |
|
322 |
meta1 = minetest.get_meta(pos1) |
|
323 |
RE_eu_demand = RE_eu_demand + meta1:get_int(eu_demand_str) |
|
324 |
end |
|
325 |
--dprint("Total RE demand:"..RE_eu_demand) |
ee5c6c
|
326 |
|
ee0765
|
327 |
-- Get all the power from the BA nodes |
S |
328 |
local BA_eu_supply = 0 |
|
329 |
for _, pos1 in pairs(BA_nodes) do |
|
330 |
meta1 = minetest.get_meta(pos1) |
|
331 |
BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str) |
|
332 |
end |
|
333 |
--dprint("Total BA supply:"..BA_eu_supply) |
ee5c6c
|
334 |
|
ee0765
|
335 |
-- Get all the demand from the BA nodes |
S |
336 |
local BA_eu_demand = 0 |
|
337 |
for _, pos1 in pairs(BA_nodes) do |
|
338 |
meta1 = minetest.get_meta(pos1) |
|
339 |
BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str) |
|
340 |
end |
|
341 |
--dprint("Total BA demand:"..BA_eu_demand) |
ee5c6c
|
342 |
|
ee0765
|
343 |
meta:set_string("infotext", |
4b1798
|
344 |
S("@1. Supply: @2 Demand: @3", |
85a984
|
345 |
machine_name, technic.pretty_num(PR_eu_supply), technic.pretty_num(RE_eu_demand))) |
ee5c6c
|
346 |
|
6abd85
|
347 |
-- If mesecon signal and power supply or demand changed then |
D |
348 |
-- send them via digilines. |
|
349 |
if mesecons_path and digilines_path and mesecon.is_powered(pos) then |
|
350 |
if PR_eu_supply ~= meta:get_int("supply") or |
|
351 |
RE_eu_demand ~= meta:get_int("demand") then |
|
352 |
local channel = meta:get_string("channel") |
|
353 |
digilines.receptor_send(pos, digilines.rules.default, channel, { |
|
354 |
supply = PR_eu_supply, |
|
355 |
demand = RE_eu_demand |
|
356 |
}) |
|
357 |
end |
|
358 |
end |
|
359 |
|
088eea
|
360 |
-- Data that will be used by the power monitor |
CK |
361 |
meta:set_int("supply",PR_eu_supply) |
|
362 |
meta:set_int("demand",RE_eu_demand) |
|
363 |
|
ee0765
|
364 |
-- If the PR supply is enough for the RE demand supply them all |
S |
365 |
if PR_eu_supply >= RE_eu_demand then |
|
366 |
--dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand) |
|
367 |
for _, pos1 in pairs(RE_nodes) do |
|
368 |
meta1 = minetest.get_meta(pos1) |
|
369 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
370 |
meta1:set_int(eu_input_str, eu_demand) |
|
371 |
end |
|
372 |
-- We have a surplus, so distribute the rest equally to the BA nodes |
|
373 |
-- Let's calculate the factor of the demand |
|
374 |
PR_eu_supply = PR_eu_supply - RE_eu_demand |
|
375 |
local charge_factor = 0 -- Assume all batteries fully charged |
|
376 |
if BA_eu_demand > 0 then |
|
377 |
charge_factor = PR_eu_supply / BA_eu_demand |
|
378 |
end |
|
379 |
for n, pos1 in pairs(BA_nodes) do |
|
380 |
meta1 = minetest.get_meta(pos1) |
|
381 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
382 |
meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor)) |
|
383 |
--dprint("Charging battery:"..math.floor(eu_demand*charge_factor)) |
|
384 |
end |
|
385 |
return |
|
386 |
end |
ee5c6c
|
387 |
|
ee0765
|
388 |
-- If the PR supply is not enough for the RE demand we will discharge the batteries too |
S |
389 |
if PR_eu_supply + BA_eu_supply >= RE_eu_demand then |
|
390 |
--dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand) |
|
391 |
for _, pos1 in pairs(RE_nodes) do |
|
392 |
meta1 = minetest.get_meta(pos1) |
|
393 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
394 |
meta1:set_int(eu_input_str, eu_demand) |
|
395 |
end |
|
396 |
-- We have a deficit, so distribute to the BA nodes |
|
397 |
-- Let's calculate the factor of the supply |
|
398 |
local charge_factor = 0 -- Assume all batteries depleted |
|
399 |
if BA_eu_supply > 0 then |
|
400 |
charge_factor = (PR_eu_supply - RE_eu_demand) / BA_eu_supply |
|
401 |
end |
|
402 |
for n,pos1 in pairs(BA_nodes) do |
|
403 |
meta1 = minetest.get_meta(pos1) |
ee5c6c
|
404 |
local eu_supply = meta1:get_int(eu_supply_str) |
ee0765
|
405 |
meta1:set_int(eu_input_str, math.floor(eu_supply * charge_factor)) |
S |
406 |
--dprint("Discharging battery:"..math.floor(eu_supply*charge_factor)) |
|
407 |
end |
|
408 |
return |
|
409 |
end |
ee5c6c
|
410 |
|
ee0765
|
411 |
-- If the PR+BA supply is not enough for the RE demand: Power only the batteries |
S |
412 |
local charge_factor = 0 -- Assume all batteries fully charged |
|
413 |
if BA_eu_demand > 0 then |
|
414 |
charge_factor = PR_eu_supply / BA_eu_demand |
|
415 |
end |
|
416 |
for n, pos1 in pairs(BA_nodes) do |
|
417 |
meta1 = minetest.get_meta(pos1) |
|
418 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
419 |
meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor)) |
|
420 |
end |
|
421 |
for n, pos1 in pairs(RE_nodes) do |
|
422 |
meta1 = minetest.get_meta(pos1) |
|
423 |
meta1:set_int(eu_input_str, 0) |
|
424 |
end |
6abd85
|
425 |
|
ee5c6c
|
426 |
end, |
K |
427 |
}) |
ee0765
|
428 |
|
563a4c
|
429 |
-- Timeout ABM |
N |
430 |
-- Timeout for a node in case it was disconnected from the network |
|
431 |
-- A node must be touched by the station continuously in order to function |
|
432 |
local function switching_station_timeout_count(pos, tier) |
|
433 |
local meta = minetest.get_meta(pos) |
|
434 |
local timeout = meta:get_int(tier.."_EU_timeout") |
|
435 |
if timeout <= 0 then |
4ac36e
|
436 |
meta:set_int(tier.."_EU_input", 0) -- Not needed anymore <-- actually, it is for supply converter |
563a4c
|
437 |
return true |
N |
438 |
else |
|
439 |
meta:set_int(tier.."_EU_timeout", timeout - 1) |
|
440 |
return false |
|
441 |
end |
|
442 |
end |
|
443 |
minetest.register_abm({ |
78f16c
|
444 |
label = "Machines: timeout check", |
563a4c
|
445 |
nodenames = {"group:technic_machine"}, |
N |
446 |
interval = 1, |
|
447 |
chance = 1, |
|
448 |
action = function(pos, node, active_object_count, active_object_count_wider) |
088eea
|
449 |
local meta = minetest.get_meta(pos) |
563a4c
|
450 |
for tier, machines in pairs(technic.machines) do |
N |
451 |
if machines[node.name] and switching_station_timeout_count(pos, tier) then |
|
452 |
local nodedef = minetest.registered_nodes[node.name] |
|
453 |
if nodedef and nodedef.technic_disabled_machine_name then |
|
454 |
node.name = nodedef.technic_disabled_machine_name |
|
455 |
minetest.swap_node(pos, node) |
1c617f
|
456 |
elseif nodedef and nodedef.technic_on_disable then |
N |
457 |
nodedef.technic_on_disable(pos, node) |
563a4c
|
458 |
end |
N |
459 |
if nodedef then |
|
460 |
local meta = minetest.get_meta(pos) |
|
461 |
meta:set_string("infotext", S("%s Has No Network"):format(nodedef.description)) |
|
462 |
end |
|
463 |
end |
|
464 |
end |
|
465 |
end, |
|
466 |
}) |
|
467 |
|
088eea
|
468 |
--Re-enable disabled switching station if necessary, similar to the timeout above |
CK |
469 |
minetest.register_abm({ |
78f16c
|
470 |
label = "Machines: re-enable check", |
088eea
|
471 |
nodenames = {"technic:switching_station"}, |
CK |
472 |
interval = 1, |
|
473 |
chance = 1, |
|
474 |
action = function(pos, node, active_object_count, active_object_count_wider) |
|
475 |
local meta = minetest.get_meta(pos) |
|
476 |
local pos1 = {x=pos.x,y=pos.y-1,z=pos.z} |
|
477 |
local tier = technic.get_cable_tier(minetest.get_node(pos1).name) |
|
478 |
if not tier then return end |
|
479 |
if switching_station_timeout_count(pos, tier) then |
|
480 |
local meta = minetest.get_meta(pos) |
|
481 |
meta:set_int("active",1) |
|
482 |
end |
|
483 |
end, |
|
484 |
}) |
|
485 |
|
ee0765
|
486 |
for tier, machines in pairs(technic.machines) do |
S |
487 |
-- SPECIAL will not be traversed |
|
488 |
technic.register_machine(tier, "technic:switching_station", "SPECIAL") |
|
489 |
end |
|
490 |
|