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