ShadowNinja
2013-10-06 363f0332788e04e2e4bb63af5cd21fac5ae56ae5
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 technic.DBG = 1
31 local dprint = technic.dprint
32
468d79 33 technic.networks = {}
N 34
ee0765 35 minetest.register_craft({
S 36     output = "technic:switching_station",
37     recipe = {
38         {"default:steel_ingot",  "technic:lv_transformer", "default:steel_ingot"},
39         {"default:copper_ingot", "technic:lv_cable0",      "default:copper_ingot"},
40         {"default:steel_ingot",  "technic:lv_cable0",      "default:steel_ingot"}
41     }
42 })
ee5c6c 43
ee0765 44 minetest.register_node("technic:switching_station",{
S 45     description = "Switching Station",
46     tiles  = {"technic_water_mill_top_active.png", "technic_water_mill_top_active.png",
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)
59         meta:set_string("infotext", "Switching Station")
60     end,
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)
70     timeout = meta:get_int(tier.."_EU_timeout")
71     if timeout == 0 then
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)
109         elseif machines[name] == technic.battery then
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
ee0765 118 local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, all_nodes, i, machines, tier)
S 119     local pos = all_nodes[i]
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
129         check_node_subp(PR_nodes, RE_nodes, BA_nodes, all_nodes, cur_pos, machines, tier)
130     end
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
140 local get_network = function(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)
146         return cached.PR_nodes, cached.BA_nodes, cached.RE_nodes
147     end
148     local i = 1
149     local PR_nodes = {}
150     local BA_nodes = {}
151     local RE_nodes = {}
152     local all_nodes = {pos1}
153     repeat
154         traverse_network(PR_nodes, RE_nodes, BA_nodes, all_nodes,
155                 i, technic.machines[tier], tier)
156         i = i + 1
157     until all_nodes[i] == nil
12d29c 158     technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes, RE_nodes = RE_nodes, BA_nodes = BA_nodes}
f4ac2b 159     return PR_nodes, BA_nodes, RE_nodes
N 160 end
161
ee0765 162 -----------------------------------------------
S 163 -- The action code for the switching station --
164 -----------------------------------------------
165 minetest.register_abm({
166     nodenames = {"technic:switching_station"},
ee5c6c 167     interval   = 1,
K 168     chance     = 1,
169     action = function(pos, node, active_object_count, active_object_count_wider)
ee0765 170         local meta             = minetest.get_meta(pos)
S 171         local meta1            = nil
172         local pos1             = {}
173         local PR_EU            = 0 -- EUs from PR nodes
174         local BA_PR_EU         = 0 -- EUs from BA nodes (discharching)
175         local BA_RE_EU         = 0 -- EUs to BA nodes (charging)
176         local RE_EU            = 0 -- EUs to RE nodes
ee5c6c 177
ee0765 178         local tier      = ""
f4ac2b 179         local PR_nodes
N 180         local BA_nodes
181         local RE_nodes
ee5c6c 182
ee0765 183         -- Which kind of network are we on:
S 184         pos1 = {x=pos.x, y=pos.y-1, z=pos.z}
ee5c6c 185
ee0765 186         local name = minetest.get_node(pos1).name
S 187         local tier = technic.get_cable_tier(name)
188         if tier then
468d79 189             PR_nodes, BA_nodes, RE_nodes = get_network(pos1, tier)
ee0765 190         else
S 191             --dprint("Not connected to a network")
192             meta:set_string("infotext", "Switching Station - no network")
193             return
194         end
195         --dprint("nodes="..table.getn(all_nodes)
196         --        .." PR="..table.getn(PR_nodes)
197         --        .." BA="..table.getn(BA_nodes)
198         --        .." RE="..table.getn(RE_nodes))
ee5c6c 199
ee0765 200         -- Strings for the meta data
S 201         local eu_demand_str    = tier.."_EU_demand"
202         local eu_input_str     = tier.."_EU_input"
203         local eu_supply_str    = tier.."_EU_supply"
ee5c6c 204
ee0765 205         -- Get all the power from the PR nodes
S 206         local PR_eu_supply = 0 -- Total power
207         for _, pos1 in pairs(PR_nodes) do
208             meta1 = minetest.get_meta(pos1)
209             PR_eu_supply = PR_eu_supply + meta1:get_int(eu_supply_str)
210         end
211         --dprint("Total PR supply:"..PR_eu_supply)
ee5c6c 212
ee0765 213         -- Get all the demand from the RE nodes
S 214         local RE_eu_demand = 0
215         for _, pos1 in pairs(RE_nodes) do
216             meta1 = minetest.get_meta(pos1)
217             RE_eu_demand = RE_eu_demand + meta1:get_int(eu_demand_str)
218         end
219         --dprint("Total RE demand:"..RE_eu_demand)
ee5c6c 220
ee0765 221         -- Get all the power from the BA nodes
S 222         local BA_eu_supply = 0
223         for _, pos1 in pairs(BA_nodes) do
224             meta1 = minetest.get_meta(pos1)
225             BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str)
226         end
227         --dprint("Total BA supply:"..BA_eu_supply)
ee5c6c 228
ee0765 229         -- Get all the demand from the BA nodes
S 230         local BA_eu_demand = 0
231         for _, pos1 in pairs(BA_nodes) do
232             meta1 = minetest.get_meta(pos1)
233             BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str)
234         end
235         --dprint("Total BA demand:"..BA_eu_demand)
ee5c6c 236
ee0765 237         meta:set_string("infotext",
S 238                 "Switching Station. Supply: "..PR_eu_supply
239                 .." Demand: "..RE_eu_demand)
ee5c6c 240
ee0765 241         -- If the PR supply is enough for the RE demand supply them all
S 242         if PR_eu_supply >= RE_eu_demand then
243         --dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
244             for _, pos1 in pairs(RE_nodes) do
245                 meta1 = minetest.get_meta(pos1)
246                 local eu_demand = meta1:get_int(eu_demand_str)
247                 meta1:set_int(eu_input_str, eu_demand)
248             end
249             -- We have a surplus, so distribute the rest equally to the BA nodes
250             -- Let's calculate the factor of the demand
251             PR_eu_supply = PR_eu_supply - RE_eu_demand
252             local charge_factor = 0 -- Assume all batteries fully charged
253             if BA_eu_demand > 0 then
254                 charge_factor = PR_eu_supply / BA_eu_demand
255             end
256             for n, pos1 in pairs(BA_nodes) do
257                 meta1 = minetest.get_meta(pos1)
258                 local eu_demand = meta1:get_int(eu_demand_str)
259                 meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
260                 --dprint("Charging battery:"..math.floor(eu_demand*charge_factor))
261             end
262             return
263         end
ee5c6c 264
ee0765 265         -- If the PR supply is not enough for the RE demand we will discharge the batteries too
S 266         if PR_eu_supply + BA_eu_supply >= RE_eu_demand then
267             --dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
268             for _, pos1 in pairs(RE_nodes) do
269                 meta1  = minetest.get_meta(pos1)
270                 local eu_demand = meta1:get_int(eu_demand_str)
271                 meta1:set_int(eu_input_str, eu_demand)
272             end
273             -- We have a deficit, so distribute to the BA nodes
274             -- Let's calculate the factor of the supply
275             local charge_factor = 0 -- Assume all batteries depleted
276             if BA_eu_supply > 0 then
277                 charge_factor = (PR_eu_supply - RE_eu_demand) / BA_eu_supply
278             end
279             for n,pos1 in pairs(BA_nodes) do
280                 meta1 = minetest.get_meta(pos1)
ee5c6c 281                 local eu_supply = meta1:get_int(eu_supply_str)
ee0765 282                 meta1:set_int(eu_input_str, math.floor(eu_supply * charge_factor))
S 283                 --dprint("Discharging battery:"..math.floor(eu_supply*charge_factor))
284             end
285             return
286         end
ee5c6c 287
ee0765 288         -- If the PR+BA supply is not enough for the RE demand: Power only the batteries
S 289         local charge_factor = 0 -- Assume all batteries fully charged
290         if BA_eu_demand > 0 then
291             charge_factor = PR_eu_supply / BA_eu_demand
292         end
293         for n, pos1 in pairs(BA_nodes) do
294             meta1 = minetest.get_meta(pos1)
295             local eu_demand = meta1:get_int(eu_demand_str)
296             meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
297         end
298         for n, pos1 in pairs(RE_nodes) do
299             meta1 = minetest.get_meta(pos1)
300             meta1:set_int(eu_input_str, 0)
301         end
ee5c6c 302     end,
K 303 })
ee0765 304
S 305 for tier, machines in pairs(technic.machines) do
306     -- SPECIAL will not be traversed
307     technic.register_machine(tier, "technic:switching_station", "SPECIAL")
308 end
309