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