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