est31
2015-02-02 9444eff7f7853b0e4385adbd117cd6bace8dcb8f
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"},
ee5c6c 174     interval   = 1,
K 175     chance     = 1,
176     action = function(pos, node, active_object_count, active_object_count_wider)
ee0765 177         local meta             = minetest.get_meta(pos)
S 178         local meta1            = nil
179         local pos1             = {}
180         local PR_EU            = 0 -- EUs from PR nodes
181         local BA_PR_EU         = 0 -- EUs from BA nodes (discharching)
182         local BA_RE_EU         = 0 -- EUs to BA nodes (charging)
183         local RE_EU            = 0 -- EUs to RE nodes
ee5c6c 184
ee0765 185         local tier      = ""
f4ac2b 186         local PR_nodes
N 187         local BA_nodes
188         local RE_nodes
be2f30 189         local machine_name = S("Switching Station")
ee5c6c 190
563a4c 191         if meta:get_int("active") ~= 1 then
N 192             meta:set_int("active", 1)
193             local active_pos = minetest.deserialize(meta:get_string("active_pos"))
194             if active_pos then
195                 local meta1 = minetest.get_meta(active_pos)
196                 meta:set_string("infotext", S("%s (Slave)"):format(meta1:get_string("infotext")))
197             end
198             return
199         end
200         
ee0765 201         -- Which kind of network are we on:
S 202         pos1 = {x=pos.x, y=pos.y-1, z=pos.z}
ee5c6c 203
ee0765 204         local name = minetest.get_node(pos1).name
S 205         local tier = technic.get_cable_tier(name)
206         if tier then
563a4c 207             PR_nodes, BA_nodes, RE_nodes = get_network(pos, pos1, tier)
ee0765 208         else
S 209             --dprint("Not connected to a network")
be2f30 210             meta:set_string("infotext", S("%s Has No Network"):format(machine_name))
ee0765 211             return
S 212         end
563a4c 213         
N 214         -- Run all the nodes
215         local function run_nodes(list)
216             for _, pos2 in ipairs(list) do
c38da0 217                 technic.get_or_load_node(pos2)
563a4c 218                 local node2 = minetest.get_node(pos2)
N 219                 local nodedef
220                 if node2 and node2.name then
221                     nodedef = minetest.registered_nodes[node2.name]
222                 end
223                 if nodedef and nodedef.technic_run then
224                     nodedef.technic_run(pos2, node2)
225                 end
226             end
227         end
228         
229         run_nodes(PR_nodes)
230         run_nodes(RE_nodes)
231         run_nodes(BA_nodes)
ee5c6c 232
ee0765 233         -- Strings for the meta data
S 234         local eu_demand_str    = tier.."_EU_demand"
235         local eu_input_str     = tier.."_EU_input"
236         local eu_supply_str    = tier.."_EU_supply"
ee5c6c 237
7cfb38 238         -- Distribute charge equally across multiple batteries.
KZ 239         local charge_total = 0
240         local battery_count = 0
241
242         for n, pos1 in pairs(BA_nodes) do
243             meta1 = minetest.get_meta(pos1)
244             local charge = meta1:get_int("internal_EU_charge")
245
246             if (meta1:get_int(eu_demand_str) ~= 0) then
247                 charge_total = charge_total + charge
248                 battery_count = battery_count + 1
249             end
250         end
251
252         local charge_distributed = math.floor(charge_total / battery_count)
253
254         for n, pos1 in pairs(BA_nodes) do
255             meta1 = minetest.get_meta(pos1)
256
257             if (meta1:get_int(eu_demand_str) ~= 0) then
258                 meta1:set_int("internal_EU_charge", charge_distributed)
259             end
260         end
261
ee0765 262         -- Get all the power from the PR nodes
S 263         local PR_eu_supply = 0 -- Total power
264         for _, pos1 in pairs(PR_nodes) do
265             meta1 = minetest.get_meta(pos1)
266             PR_eu_supply = PR_eu_supply + meta1:get_int(eu_supply_str)
267         end
268         --dprint("Total PR supply:"..PR_eu_supply)
ee5c6c 269
ee0765 270         -- Get all the demand from the RE nodes
S 271         local RE_eu_demand = 0
272         for _, pos1 in pairs(RE_nodes) do
273             meta1 = minetest.get_meta(pos1)
274             RE_eu_demand = RE_eu_demand + meta1:get_int(eu_demand_str)
275         end
276         --dprint("Total RE demand:"..RE_eu_demand)
ee5c6c 277
ee0765 278         -- Get all the power from the BA nodes
S 279         local BA_eu_supply = 0
280         for _, pos1 in pairs(BA_nodes) do
281             meta1 = minetest.get_meta(pos1)
282             BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str)
283         end
284         --dprint("Total BA supply:"..BA_eu_supply)
ee5c6c 285
ee0765 286         -- Get all the demand from the BA nodes
S 287         local BA_eu_demand = 0
288         for _, pos1 in pairs(BA_nodes) do
289             meta1 = minetest.get_meta(pos1)
290             BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str)
291         end
292         --dprint("Total BA demand:"..BA_eu_demand)
ee5c6c 293
ee0765 294         meta:set_string("infotext",
d9bf98 295                 technic.format(S("%s. Supply: %e Demand: %e"),
be2f30 296                 machine_name, PR_eu_supply, RE_eu_demand))
ee5c6c 297
ee0765 298         -- If the PR supply is enough for the RE demand supply them all
S 299         if PR_eu_supply >= RE_eu_demand then
300         --dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
301             for _, pos1 in pairs(RE_nodes) do
302                 meta1 = minetest.get_meta(pos1)
303                 local eu_demand = meta1:get_int(eu_demand_str)
304                 meta1:set_int(eu_input_str, eu_demand)
305             end
306             -- We have a surplus, so distribute the rest equally to the BA nodes
307             -- Let's calculate the factor of the demand
308             PR_eu_supply = PR_eu_supply - RE_eu_demand
309             local charge_factor = 0 -- Assume all batteries fully charged
310             if BA_eu_demand > 0 then
311                 charge_factor = PR_eu_supply / BA_eu_demand
312             end
313             for n, pos1 in pairs(BA_nodes) do
314                 meta1 = minetest.get_meta(pos1)
315                 local eu_demand = meta1:get_int(eu_demand_str)
316                 meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
317                 --dprint("Charging battery:"..math.floor(eu_demand*charge_factor))
318             end
319             return
320         end
ee5c6c 321
ee0765 322         -- If the PR supply is not enough for the RE demand we will discharge the batteries too
S 323         if PR_eu_supply + BA_eu_supply >= RE_eu_demand then
324             --dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
325             for _, pos1 in pairs(RE_nodes) do
326                 meta1  = minetest.get_meta(pos1)
327                 local eu_demand = meta1:get_int(eu_demand_str)
328                 meta1:set_int(eu_input_str, eu_demand)
329             end
330             -- We have a deficit, so distribute to the BA nodes
331             -- Let's calculate the factor of the supply
332             local charge_factor = 0 -- Assume all batteries depleted
333             if BA_eu_supply > 0 then
334                 charge_factor = (PR_eu_supply - RE_eu_demand) / BA_eu_supply
335             end
336             for n,pos1 in pairs(BA_nodes) do
337                 meta1 = minetest.get_meta(pos1)
ee5c6c 338                 local eu_supply = meta1:get_int(eu_supply_str)
ee0765 339                 meta1:set_int(eu_input_str, math.floor(eu_supply * charge_factor))
S 340                 --dprint("Discharging battery:"..math.floor(eu_supply*charge_factor))
341             end
342             return
343         end
ee5c6c 344
ee0765 345         -- If the PR+BA supply is not enough for the RE demand: Power only the batteries
S 346         local charge_factor = 0 -- Assume all batteries fully charged
347         if BA_eu_demand > 0 then
348             charge_factor = PR_eu_supply / BA_eu_demand
349         end
350         for n, pos1 in pairs(BA_nodes) do
351             meta1 = minetest.get_meta(pos1)
352             local eu_demand = meta1:get_int(eu_demand_str)
353             meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
354         end
355         for n, pos1 in pairs(RE_nodes) do
356             meta1 = minetest.get_meta(pos1)
357             meta1:set_int(eu_input_str, 0)
358         end
ee5c6c 359     end,
K 360 })
ee0765 361
563a4c 362 -- Timeout ABM
N 363 -- Timeout for a node in case it was disconnected from the network
364 -- A node must be touched by the station continuously in order to function
365 local function switching_station_timeout_count(pos, tier)
366     local meta = minetest.get_meta(pos)
367     local timeout = meta:get_int(tier.."_EU_timeout")
368     if timeout <= 0 then
4ac36e 369         meta:set_int(tier.."_EU_input", 0) -- Not needed anymore <-- actually, it is for supply converter
563a4c 370         return true
N 371     else
372         meta:set_int(tier.."_EU_timeout", timeout - 1)
373         return false
374     end
375 end
376 minetest.register_abm({
377     nodenames = {"group:technic_machine"},
378     interval   = 1,
379     chance     = 1,
380     action = function(pos, node, active_object_count, active_object_count_wider)
381         for tier, machines in pairs(technic.machines) do
382             if machines[node.name] and switching_station_timeout_count(pos, tier) then
383                 local nodedef = minetest.registered_nodes[node.name]
384                 if nodedef and nodedef.technic_disabled_machine_name then
385                     node.name = nodedef.technic_disabled_machine_name
386                     minetest.swap_node(pos, node)
1c617f 387                 elseif nodedef and nodedef.technic_on_disable then
N 388                     nodedef.technic_on_disable(pos, node)
563a4c 389                 end
N 390                 if nodedef then
391                     local meta = minetest.get_meta(pos)
392                     meta:set_string("infotext", S("%s Has No Network"):format(nodedef.description))
393                 end
394             end
395         end
396     end,
397 })
398
ee0765 399 for tier, machines in pairs(technic.machines) do
S 400     -- SPECIAL will not be traversed
401     technic.register_machine(tier, "technic:switching_station", "SPECIAL")
402 end
403