Zefram
2014-05-18 623fcae4a4ad3ec12cc242b29b0d781357cff3f7
commit | author | age
ee5c6c 1 -- SWITCHING STATION
K 2 -- The switching station is the center of all power distribution on an electric network.
3 -- The station will collect all produced power from producers (PR) and batteries (BA)
4 -- and distribute it to receivers (RE) and depleted batteries (BA).
5 --
6 -- It works like this:
7 --  All PR,BA,RE nodes are indexed and tagged with the switching station.
8 -- The tagging is to allow more stations to be built without allowing a cheat
9 -- with duplicating power.
10 --  All the RE nodes are queried for their current EU demand. Those which are off
11 -- would require no or a small standby EU demand, while those which are on would
12 -- require more.
13 -- If the total demand is less than the available power they are all updated with the
14 -- demand number.
15 -- If any surplus exists from the PR nodes the batteries will be charged evenly with this.
16 -- If the total demand requires draw on the batteries they will be discharged evenly.
17 --
18 -- If the total demand is more than the available power all RE nodes will be shut down.
19 -- We have a brown-out situation.
20 --
21 -- Hence all the power distribution logic resides in this single node.
22 --
23 --  Nodes connected to the network will have one or more of these parameters as meta data:
24 --   <LV|MV|HV>_EU_supply : Exists for PR and BA node types. This is the EU value supplied by the node. Output
25 --   <LV|MV|HV>_EU_demand : Exists for RE and BA node types. This is the EU value the node requires to run. Output
26 --   <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
27 --
28 --  The reason the LV|MV|HV type is prepended toe meta data is because some machine could require several supplies to work.
29 --  This way the supplies are separated per network.
30
468d79 31 technic.networks = {}
be2f30 32
S 33 local S = technic.getter
468d79 34
ee0765 35 minetest.register_craft({
S 36     output = "technic:switching_station",
37     recipe = {
68b7bc 38         {"technic:cast_iron_ingot", "technic:lv_transformer", "technic:cast_iron_ingot"},
Z 39         {"default:copper_ingot",    "technic:lv_cable0",      "default:copper_ingot"},
40         {"technic:cast_iron_ingot", "technic:lv_cable0",      "technic:cast_iron_ingot"}
ee0765 41     }
S 42 })
ee5c6c 43
ee0765 44 minetest.register_node("technic:switching_station",{
be2f30 45     description = S("Switching Station"),
ee0765 46     tiles  = {"technic_water_mill_top_active.png", "technic_water_mill_top_active.png",
S 47                   "technic_water_mill_top_active.png", "technic_water_mill_top_active.png",
48               "technic_water_mill_top_active.png", "technic_water_mill_top_active.png"},
49     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2},
50     sounds = default.node_sound_wood_defaults(),
51     drawtype = "nodebox",
52     paramtype = "light",
53     node_box = {
54         type = "fixed",
55         fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
56     },
57     on_construct = function(pos)
58         local meta = minetest.get_meta(pos)
be2f30 59         meta:set_string("infotext", S("Switching Station"))
ee0765 60     end,
S 61 })
ee5c6c 62
K 63 --------------------------------------------------
64 -- Functions to help the machines on the electrical network
65 --------------------------------------------------
66 -- This one provides a timeout for a node in case it was disconnected from the network
67 -- A node must be touched by the station continuously in order to function
ee0765 68 function technic.switching_station_timeout_count(pos, tier)
S 69     local meta = minetest.get_meta(pos)
d8437f 70     local timeout = meta:get_int(tier.."_EU_timeout")
ee0765 71     if timeout == 0 then
S 72         meta:set_int(tier.."_EU_input", 0)
73     else
74         meta:set_int(tier.."_EU_timeout", timeout - 1)
75     end
76 end
ee5c6c 77
K 78 --------------------------------------------------
79 -- Functions to traverse the electrical network
80 --------------------------------------------------
81
82 -- Add a wire node to the LV/MV/HV network
ee0765 83 local add_new_cable_node = function(nodes, pos)
S 84     -- Ignore if the node has already been added
85     for i = 1, #nodes do
86         if pos.x == nodes[i].x and
87            pos.y == nodes[i].y and
88            pos.z == nodes[i].z then
89             return false
90         end
91     end
92     table.insert(nodes, {x=pos.x, y=pos.y, z=pos.z, visited=1})
93     return true
94 end
ee5c6c 95
K 96 -- Generic function to add found connected nodes to the right classification array
ee0765 97 local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, all_nodes, pos, machines, tier)
S 98     local meta = minetest.get_meta(pos)
99     local name = minetest.get_node(pos).name
100
101     if technic.is_tier_cable(name, tier) then
102         add_new_cable_node(all_nodes, pos)
103     elseif machines[name] then
104         --dprint(name.." is a "..machines[name])
105         if     machines[name] == technic.producer then
106             add_new_cable_node(PR_nodes, pos)
107         elseif machines[name] == technic.receiver then
108             add_new_cable_node(RE_nodes, pos)
623fca 109         elseif machines[name] == technic.producer_receiver then
Z 110             add_new_cable_node(PR_nodes, pos)
111             add_new_cable_node(RE_nodes, pos)
ee0765 112         elseif machines[name] == technic.battery then
S 113             add_new_cable_node(BA_nodes, pos)
114         end
115
116         meta:set_int(tier.."_EU_timeout", 2) -- Touch node
117     end
118 end
ee5c6c 119
K 120 -- Traverse a network given a list of machines and a cable type name
ee0765 121 local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, all_nodes, i, machines, tier)
S 122     local pos = all_nodes[i]
123     local positions = {
124         {x=pos.x+1, y=pos.y,   z=pos.z},
125         {x=pos.x-1, y=pos.y,   z=pos.z},
126         {x=pos.x,   y=pos.y+1, z=pos.z},
127         {x=pos.x,   y=pos.y-1, z=pos.z},
128         {x=pos.x,   y=pos.y,   z=pos.z+1},
129         {x=pos.x,   y=pos.y,   z=pos.z-1}}
130     --print("ON")
131     for i, cur_pos in pairs(positions) do
132         check_node_subp(PR_nodes, RE_nodes, BA_nodes, all_nodes, cur_pos, machines, tier)
133     end
134 end
ee5c6c 135
f4ac2b 136 local touch_nodes = function(list, tier)
N 137     for _, pos in ipairs(list) do
138         local meta = minetest.get_meta(pos)
139         meta:set_int(tier.."_EU_timeout", 2) -- Touch node
140     end
141 end
142
143 local get_network = function(pos1, tier)
12d29c 144     local cached = technic.networks[minetest.hash_node_position(pos1)]
f4ac2b 145     if cached and cached.tier == tier then
N 146         touch_nodes(cached.PR_nodes, tier)
147         touch_nodes(cached.BA_nodes, tier)
148         touch_nodes(cached.RE_nodes, tier)
149         return cached.PR_nodes, cached.BA_nodes, cached.RE_nodes
150     end
151     local i = 1
152     local PR_nodes = {}
153     local BA_nodes = {}
154     local RE_nodes = {}
155     local all_nodes = {pos1}
156     repeat
157         traverse_network(PR_nodes, RE_nodes, BA_nodes, all_nodes,
158                 i, technic.machines[tier], tier)
159         i = i + 1
160     until all_nodes[i] == nil
12d29c 161     technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes, RE_nodes = RE_nodes, BA_nodes = BA_nodes}
f4ac2b 162     return PR_nodes, BA_nodes, RE_nodes
N 163 end
164
ee0765 165 -----------------------------------------------
S 166 -- The action code for the switching station --
167 -----------------------------------------------
168 minetest.register_abm({
169     nodenames = {"technic:switching_station"},
ee5c6c 170     interval   = 1,
K 171     chance     = 1,
172     action = function(pos, node, active_object_count, active_object_count_wider)
ee0765 173         local meta             = minetest.get_meta(pos)
S 174         local meta1            = nil
175         local pos1             = {}
176         local PR_EU            = 0 -- EUs from PR nodes
177         local BA_PR_EU         = 0 -- EUs from BA nodes (discharching)
178         local BA_RE_EU         = 0 -- EUs to BA nodes (charging)
179         local RE_EU            = 0 -- EUs to RE nodes
ee5c6c 180
ee0765 181         local tier      = ""
f4ac2b 182         local PR_nodes
N 183         local BA_nodes
184         local RE_nodes
be2f30 185         local machine_name = S("Switching Station")
ee5c6c 186
ee0765 187         -- Which kind of network are we on:
S 188         pos1 = {x=pos.x, y=pos.y-1, z=pos.z}
ee5c6c 189
ee0765 190         local name = minetest.get_node(pos1).name
S 191         local tier = technic.get_cable_tier(name)
192         if tier then
468d79 193             PR_nodes, BA_nodes, RE_nodes = get_network(pos1, tier)
ee0765 194         else
S 195             --dprint("Not connected to a network")
be2f30 196             meta:set_string("infotext", S("%s Has No Network"):format(machine_name))
ee0765 197             return
S 198         end
199         --dprint("nodes="..table.getn(all_nodes)
200         --        .." PR="..table.getn(PR_nodes)
201         --        .." BA="..table.getn(BA_nodes)
202         --        .." RE="..table.getn(RE_nodes))
ee5c6c 203
ee0765 204         -- Strings for the meta data
S 205         local eu_demand_str    = tier.."_EU_demand"
206         local eu_input_str     = tier.."_EU_input"
207         local eu_supply_str    = tier.."_EU_supply"
ee5c6c 208
ee0765 209         -- Get all the power from the PR nodes
S 210         local PR_eu_supply = 0 -- Total power
211         for _, pos1 in pairs(PR_nodes) do
212             meta1 = minetest.get_meta(pos1)
213             PR_eu_supply = PR_eu_supply + meta1:get_int(eu_supply_str)
214         end
215         --dprint("Total PR supply:"..PR_eu_supply)
ee5c6c 216
ee0765 217         -- Get all the demand from the RE nodes
S 218         local RE_eu_demand = 0
219         for _, pos1 in pairs(RE_nodes) do
220             meta1 = minetest.get_meta(pos1)
221             RE_eu_demand = RE_eu_demand + meta1:get_int(eu_demand_str)
222         end
223         --dprint("Total RE demand:"..RE_eu_demand)
ee5c6c 224
ee0765 225         -- Get all the power from the BA nodes
S 226         local BA_eu_supply = 0
227         for _, pos1 in pairs(BA_nodes) do
228             meta1 = minetest.get_meta(pos1)
229             BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str)
230         end
231         --dprint("Total BA supply:"..BA_eu_supply)
ee5c6c 232
ee0765 233         -- Get all the demand from the BA nodes
S 234         local BA_eu_demand = 0
235         for _, pos1 in pairs(BA_nodes) do
236             meta1 = minetest.get_meta(pos1)
237             BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str)
238         end
239         --dprint("Total BA demand:"..BA_eu_demand)
ee5c6c 240
ee0765 241         meta:set_string("infotext",
be2f30 242                 S("%s. Supply: %d Demand: %d"):format(
S 243                 machine_name, PR_eu_supply, RE_eu_demand))
ee5c6c 244
ee0765 245         -- If the PR supply is enough for the RE demand supply them all
S 246         if PR_eu_supply >= RE_eu_demand then
247         --dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
248             for _, pos1 in pairs(RE_nodes) do
249                 meta1 = minetest.get_meta(pos1)
250                 local eu_demand = meta1:get_int(eu_demand_str)
251                 meta1:set_int(eu_input_str, eu_demand)
252             end
253             -- We have a surplus, so distribute the rest equally to the BA nodes
254             -- Let's calculate the factor of the demand
255             PR_eu_supply = PR_eu_supply - RE_eu_demand
256             local charge_factor = 0 -- Assume all batteries fully charged
257             if BA_eu_demand > 0 then
258                 charge_factor = PR_eu_supply / BA_eu_demand
259             end
260             for n, pos1 in pairs(BA_nodes) do
261                 meta1 = minetest.get_meta(pos1)
262                 local eu_demand = meta1:get_int(eu_demand_str)
263                 meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
264                 --dprint("Charging battery:"..math.floor(eu_demand*charge_factor))
265             end
266             return
267         end
ee5c6c 268
ee0765 269         -- If the PR supply is not enough for the RE demand we will discharge the batteries too
S 270         if PR_eu_supply + BA_eu_supply >= RE_eu_demand then
271             --dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
272             for _, pos1 in pairs(RE_nodes) do
273                 meta1  = minetest.get_meta(pos1)
274                 local eu_demand = meta1:get_int(eu_demand_str)
275                 meta1:set_int(eu_input_str, eu_demand)
276             end
277             -- We have a deficit, so distribute to the BA nodes
278             -- Let's calculate the factor of the supply
279             local charge_factor = 0 -- Assume all batteries depleted
280             if BA_eu_supply > 0 then
281                 charge_factor = (PR_eu_supply - RE_eu_demand) / BA_eu_supply
282             end
283             for n,pos1 in pairs(BA_nodes) do
284                 meta1 = minetest.get_meta(pos1)
ee5c6c 285                 local eu_supply = meta1:get_int(eu_supply_str)
ee0765 286                 meta1:set_int(eu_input_str, math.floor(eu_supply * charge_factor))
S 287                 --dprint("Discharging battery:"..math.floor(eu_supply*charge_factor))
288             end
289             return
290         end
ee5c6c 291
ee0765 292         -- If the PR+BA supply is not enough for the RE demand: Power only the batteries
S 293         local charge_factor = 0 -- Assume all batteries fully charged
294         if BA_eu_demand > 0 then
295             charge_factor = PR_eu_supply / BA_eu_demand
296         end
297         for n, pos1 in pairs(BA_nodes) do
298             meta1 = minetest.get_meta(pos1)
299             local eu_demand = meta1:get_int(eu_demand_str)
300             meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
301         end
302         for n, pos1 in pairs(RE_nodes) do
303             meta1 = minetest.get_meta(pos1)
304             meta1:set_int(eu_input_str, 0)
305         end
ee5c6c 306     end,
K 307 })
ee0765 308
S 309 for tier, machines in pairs(technic.machines) do
310     -- SPECIAL will not be traversed
311     technic.register_machine(tier, "technic:switching_station", "SPECIAL")
312 end
313