you
2017-06-05 987cc5a6a425b1f9bcd9000608dc389a45c675a1
commit | author | age
987cc5 1 -- See also technic/doc/api.md
ee5c6c 2
468d79 3 technic.networks = {}
d3f40e 4 technic.cables = {}
be2f30 5
6abd85 6 local mesecons_path = minetest.get_modpath("mesecons")
D 7 local digilines_path = minetest.get_modpath("digilines")
8
be2f30 9 local S = technic.getter
468d79 10
54004f 11 local cable_entry = "^technic_cable_connection_overlay.png"
VE 12
ee0765 13 minetest.register_craft({
S 14     output = "technic:switching_station",
15     recipe = {
83c649 16         {"",                     "technic:lv_transformer", ""},
S 17         {"default:copper_ingot", "technic:machine_casing", "default:copper_ingot"},
18         {"technic:lv_cable",     "technic:lv_cable",       "technic:lv_cable"}
ee0765 19     }
S 20 })
ee5c6c 21
338f3b 22 local mesecon_def
D 23 if mesecons_path then
24     mesecon_def = {effector = {
25         rules = mesecon.rules.default,
26     }}
27 end
28
ee0765 29 minetest.register_node("technic:switching_station",{
be2f30 30     description = S("Switching Station"),
54004f 31     tiles  = {
VE 32         "technic_water_mill_top_active.png",
33         "technic_water_mill_top_active.png"..cable_entry,
34         "technic_water_mill_top_active.png",
35         "technic_water_mill_top_active.png",
36         "technic_water_mill_top_active.png",
37         "technic_water_mill_top_active.png"},
83c649 38     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_all_tiers=1},
S 39     connect_sides = {"bottom"},
ee0765 40     sounds = default.node_sound_wood_defaults(),
S 41     on_construct = function(pos)
42         local meta = minetest.get_meta(pos)
be2f30 43         meta:set_string("infotext", S("Switching Station"))
563a4c 44         meta:set_string("active", 1)
6abd85 45         meta:set_string("channel", "switching_station"..minetest.pos_to_string(pos))
D 46         meta:set_string("formspec", "field[channel;Channel;${channel}]")
ee0765 47     end,
088eea 48     after_dig_node = function(pos)
CK 49         minetest.forceload_free_block(pos)
50         pos.y = pos.y - 1
51         minetest.forceload_free_block(pos)
52     end,
6abd85 53     on_receive_fields = function(pos, formname, fields, sender)
D 54         if not fields.channel then
55             return
56         end
57         local plname = sender:get_player_name()
58         if minetest.is_protected(pos, plname) then
59             minetest.record_protection_violation(pos, plname)
60             return
61         end
62         local meta = minetest.get_meta(pos)
63         meta:set_string("channel", fields.channel)
64     end,
338f3b 65     mesecons = mesecon_def,
6abd85 66     digiline = {
D 67         receptor = {action = function() end},
68         effector = {
69             action = function(pos, node, channel, msg)
70                 if msg ~= "GET" and msg ~= "get" then
71                     return
72                 end
73                 local meta = minetest.get_meta(pos)
74                 if channel ~= meta:get_string("channel") then
75                     return
76                 end
77                 digilines.receptor_send(pos, digilines.rules.default, channel, {
78                     supply = meta:get_int("supply"),
79                     demand = meta:get_int("demand")
80                 })
81             end
82         },
83     },
ee0765 84 })
ee5c6c 85
K 86 --------------------------------------------------
87 -- Functions to traverse the electrical network
88 --------------------------------------------------
89
90 -- Add a wire node to the LV/MV/HV network
d3f40e 91 local add_new_cable_node = function(nodes, pos, network_id)
CK 92     technic.cables[minetest.hash_node_position(pos)] = network_id
ee0765 93     -- Ignore if the node has already been added
S 94     for i = 1, #nodes do
95         if pos.x == nodes[i].x and
96            pos.y == nodes[i].y and
97            pos.z == nodes[i].z then
98             return false
99         end
100     end
101     table.insert(nodes, {x=pos.x, y=pos.y, z=pos.z, visited=1})
102     return true
103 end
ee5c6c 104
K 105 -- Generic function to add found connected nodes to the right classification array
d3f40e 106 local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below, network_id)
c38da0 107     technic.get_or_load_node(pos)
ee0765 108     local meta = minetest.get_meta(pos)
S 109     local name = minetest.get_node(pos).name
110
111     if technic.is_tier_cable(name, tier) then
d3f40e 112         add_new_cable_node(all_nodes, pos,network_id)
ee0765 113     elseif machines[name] then
S 114         --dprint(name.." is a "..machines[name])
088eea 115         meta:set_string(tier.."_network",minetest.pos_to_string(sw_pos))
ee0765 116         if     machines[name] == technic.producer then
d3f40e 117             add_new_cable_node(PR_nodes, pos, network_id)
ee0765 118         elseif machines[name] == technic.receiver then
d3f40e 119             add_new_cable_node(RE_nodes, pos, network_id)
623fca 120         elseif machines[name] == technic.producer_receiver then
d3f40e 121             add_new_cable_node(PR_nodes, pos, network_id)
CK 122             add_new_cable_node(RE_nodes, pos, network_id)
563a4c 123         elseif machines[name] == "SPECIAL" and
9444ef 124                 (pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) and
E 125                 from_below then
563a4c 126             -- Another switching station -> disable it
d3f40e 127             add_new_cable_node(SP_nodes, pos, network_id)
563a4c 128             meta:set_int("active", 0)
ee0765 129         elseif machines[name] == technic.battery then
d3f40e 130             add_new_cable_node(BA_nodes, pos, network_id)
ee0765 131         end
S 132
133         meta:set_int(tier.."_EU_timeout", 2) -- Touch node
134     end
135 end
ee5c6c 136
K 137 -- Traverse a network given a list of machines and a cable type name
d3f40e 138 local traverse_network = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, i, machines, tier, sw_pos, network_id)
ee0765 139     local pos = all_nodes[i]
S 140     local positions = {
141         {x=pos.x+1, y=pos.y,   z=pos.z},
142         {x=pos.x-1, y=pos.y,   z=pos.z},
143         {x=pos.x,   y=pos.y+1, z=pos.z},
144         {x=pos.x,   y=pos.y-1, z=pos.z},
145         {x=pos.x,   y=pos.y,   z=pos.z+1},
146         {x=pos.x,   y=pos.y,   z=pos.z-1}}
147     --print("ON")
148     for i, cur_pos in pairs(positions) do
d3f40e 149         check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3, network_id)
ee0765 150     end
S 151 end
ee5c6c 152
f4ac2b 153 local touch_nodes = function(list, tier)
N 154     for _, pos in ipairs(list) do
155         local meta = minetest.get_meta(pos)
156         meta:set_int(tier.."_EU_timeout", 2) -- Touch node
157     end
158 end
159
563a4c 160 local get_network = function(sw_pos, pos1, tier)
12d29c 161     local cached = technic.networks[minetest.hash_node_position(pos1)]
f4ac2b 162     if cached and cached.tier == tier then
N 163         touch_nodes(cached.PR_nodes, tier)
164         touch_nodes(cached.BA_nodes, tier)
165         touch_nodes(cached.RE_nodes, tier)
563a4c 166         for _, pos in ipairs(cached.SP_nodes) do
N 167             local meta = minetest.get_meta(pos)
168             meta:set_int("active", 0)
169             meta:set_string("active_pos", minetest.serialize(sw_pos))
170         end
f4ac2b 171         return cached.PR_nodes, cached.BA_nodes, cached.RE_nodes
N 172     end
173     local i = 1
174     local PR_nodes = {}
175     local BA_nodes = {}
176     local RE_nodes = {}
563a4c 177     local SP_nodes = {}
f4ac2b 178     local all_nodes = {pos1}
N 179     repeat
563a4c 180         traverse_network(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes,
d3f40e 181                 i, technic.machines[tier], tier, sw_pos, minetest.hash_node_position(pos1))
f4ac2b 182         i = i + 1
N 183     until all_nodes[i] == nil
563a4c 184     technic.networks[minetest.hash_node_position(pos1)] = {tier = tier, PR_nodes = PR_nodes,
088eea 185             RE_nodes = RE_nodes, BA_nodes = BA_nodes, SP_nodes = SP_nodes, all_nodes = all_nodes}
f4ac2b 186     return PR_nodes, BA_nodes, RE_nodes
N 187 end
188
ee0765 189 -----------------------------------------------
S 190 -- The action code for the switching station --
191 -----------------------------------------------
192 minetest.register_abm({
193     nodenames = {"technic:switching_station"},
c6464d 194     label = "Switching Station", -- allows the mtt profiler to profile this abm individually
ee5c6c 195     interval   = 1,
K 196     chance     = 1,
197     action = function(pos, node, active_object_count, active_object_count_wider)
ee0765 198         local meta             = minetest.get_meta(pos)
S 199         local meta1            = nil
200         local pos1             = {}
201         local PR_EU            = 0 -- EUs from PR nodes
202         local BA_PR_EU         = 0 -- EUs from BA nodes (discharching)
203         local BA_RE_EU         = 0 -- EUs to BA nodes (charging)
204         local RE_EU            = 0 -- EUs to RE nodes
ee5c6c 205
ee0765 206         local tier      = ""
f4ac2b 207         local PR_nodes
N 208         local BA_nodes
209         local RE_nodes
be2f30 210         local machine_name = S("Switching Station")
6abd85 211
ee0765 212         -- Which kind of network are we on:
S 213         pos1 = {x=pos.x, y=pos.y-1, z=pos.z}
ee5c6c 214
088eea 215         --Disable if necessary
CK 216         if meta:get_int("active") ~= 1 then
217             minetest.forceload_free_block(pos)
218             minetest.forceload_free_block(pos1)
219             meta:set_string("infotext",S("%s Already Present"):format(machine_name))
220             return
221         end
222
ee0765 223         local name = minetest.get_node(pos1).name
S 224         local tier = technic.get_cable_tier(name)
225         if tier then
088eea 226             -- Forceload switching station
CK 227             minetest.forceload_block(pos)
228             minetest.forceload_block(pos1)
563a4c 229             PR_nodes, BA_nodes, RE_nodes = get_network(pos, pos1, tier)
ee0765 230         else
S 231             --dprint("Not connected to a network")
be2f30 232             meta:set_string("infotext", S("%s Has No Network"):format(machine_name))
088eea 233             minetest.forceload_free_block(pos)
CK 234             minetest.forceload_free_block(pos1)
ee0765 235             return
S 236         end
6abd85 237
563a4c 238         -- Run all the nodes
10307f 239         local function run_nodes(list, run_stage)
563a4c 240             for _, pos2 in ipairs(list) do
c38da0 241                 technic.get_or_load_node(pos2)
563a4c 242                 local node2 = minetest.get_node(pos2)
N 243                 local nodedef
244                 if node2 and node2.name then
245                     nodedef = minetest.registered_nodes[node2.name]
246                 end
247                 if nodedef and nodedef.technic_run then
10307f 248                     nodedef.technic_run(pos2, node2, run_stage)
563a4c 249                 end
N 250             end
251         end
6abd85 252
10307f 253         run_nodes(PR_nodes, technic.producer)
M'P 254         run_nodes(RE_nodes, technic.receiver)
255         run_nodes(BA_nodes, technic.battery)
ee5c6c 256
ee0765 257         -- Strings for the meta data
S 258         local eu_demand_str    = tier.."_EU_demand"
259         local eu_input_str     = tier.."_EU_input"
260         local eu_supply_str    = tier.."_EU_supply"
ee5c6c 261
7cfb38 262         -- Distribute charge equally across multiple batteries.
KZ 263         local charge_total = 0
264         local battery_count = 0
265
266         for n, pos1 in pairs(BA_nodes) do
267             meta1 = minetest.get_meta(pos1)
268             local charge = meta1:get_int("internal_EU_charge")
269
270             if (meta1:get_int(eu_demand_str) ~= 0) then
271                 charge_total = charge_total + charge
272                 battery_count = battery_count + 1
273             end
274         end
275
276         local charge_distributed = math.floor(charge_total / battery_count)
277
278         for n, pos1 in pairs(BA_nodes) do
279             meta1 = minetest.get_meta(pos1)
280
281             if (meta1:get_int(eu_demand_str) ~= 0) then
282                 meta1:set_int("internal_EU_charge", charge_distributed)
283             end
284         end
285
ee0765 286         -- Get all the power from the PR nodes
S 287         local PR_eu_supply = 0 -- Total power
288         for _, pos1 in pairs(PR_nodes) do
289             meta1 = minetest.get_meta(pos1)
290             PR_eu_supply = PR_eu_supply + meta1:get_int(eu_supply_str)
291         end
292         --dprint("Total PR supply:"..PR_eu_supply)
ee5c6c 293
ee0765 294         -- Get all the demand from the RE nodes
S 295         local RE_eu_demand = 0
296         for _, pos1 in pairs(RE_nodes) do
297             meta1 = minetest.get_meta(pos1)
298             RE_eu_demand = RE_eu_demand + meta1:get_int(eu_demand_str)
299         end
300         --dprint("Total RE demand:"..RE_eu_demand)
ee5c6c 301
ee0765 302         -- Get all the power from the BA nodes
S 303         local BA_eu_supply = 0
304         for _, pos1 in pairs(BA_nodes) do
305             meta1 = minetest.get_meta(pos1)
306             BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str)
307         end
308         --dprint("Total BA supply:"..BA_eu_supply)
ee5c6c 309
ee0765 310         -- Get all the demand from the BA nodes
S 311         local BA_eu_demand = 0
312         for _, pos1 in pairs(BA_nodes) do
313             meta1 = minetest.get_meta(pos1)
314             BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str)
315         end
316         --dprint("Total BA demand:"..BA_eu_demand)
ee5c6c 317
ee0765 318         meta:set_string("infotext",
4b1798 319                 S("@1. Supply: @2 Demand: @3",
85a984 320                 machine_name, technic.pretty_num(PR_eu_supply), technic.pretty_num(RE_eu_demand)))
ee5c6c 321
6abd85 322         -- If mesecon signal and power supply or demand changed then
D 323         -- send them via digilines.
324         if mesecons_path and digilines_path and mesecon.is_powered(pos) then
325             if PR_eu_supply ~= meta:get_int("supply") or
326                     RE_eu_demand ~= meta:get_int("demand") then
327                 local channel = meta:get_string("channel")
328                 digilines.receptor_send(pos, digilines.rules.default, channel, {
329                     supply = PR_eu_supply,
330                     demand = RE_eu_demand
331                 })
332             end
333         end
334
088eea 335         -- Data that will be used by the power monitor
CK 336         meta:set_int("supply",PR_eu_supply)
337         meta:set_int("demand",RE_eu_demand)
338
ee0765 339         -- If the PR supply is enough for the RE demand supply them all
S 340         if PR_eu_supply >= RE_eu_demand then
341         --dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
342             for _, pos1 in pairs(RE_nodes) do
343                 meta1 = minetest.get_meta(pos1)
344                 local eu_demand = meta1:get_int(eu_demand_str)
345                 meta1:set_int(eu_input_str, eu_demand)
346             end
347             -- We have a surplus, so distribute the rest equally to the BA nodes
348             -- Let's calculate the factor of the demand
349             PR_eu_supply = PR_eu_supply - RE_eu_demand
350             local charge_factor = 0 -- Assume all batteries fully charged
351             if BA_eu_demand > 0 then
352                 charge_factor = PR_eu_supply / BA_eu_demand
353             end
354             for n, pos1 in pairs(BA_nodes) do
355                 meta1 = minetest.get_meta(pos1)
356                 local eu_demand = meta1:get_int(eu_demand_str)
357                 meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
358                 --dprint("Charging battery:"..math.floor(eu_demand*charge_factor))
359             end
360             return
361         end
ee5c6c 362
ee0765 363         -- If the PR supply is not enough for the RE demand we will discharge the batteries too
S 364         if PR_eu_supply + BA_eu_supply >= RE_eu_demand then
365             --dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
366             for _, pos1 in pairs(RE_nodes) do
367                 meta1  = minetest.get_meta(pos1)
368                 local eu_demand = meta1:get_int(eu_demand_str)
369                 meta1:set_int(eu_input_str, eu_demand)
370             end
371             -- We have a deficit, so distribute to the BA nodes
372             -- Let's calculate the factor of the supply
373             local charge_factor = 0 -- Assume all batteries depleted
374             if BA_eu_supply > 0 then
375                 charge_factor = (PR_eu_supply - RE_eu_demand) / BA_eu_supply
376             end
377             for n,pos1 in pairs(BA_nodes) do
378                 meta1 = minetest.get_meta(pos1)
ee5c6c 379                 local eu_supply = meta1:get_int(eu_supply_str)
ee0765 380                 meta1:set_int(eu_input_str, math.floor(eu_supply * charge_factor))
S 381                 --dprint("Discharging battery:"..math.floor(eu_supply*charge_factor))
382             end
383             return
384         end
ee5c6c 385
ee0765 386         -- If the PR+BA supply is not enough for the RE demand: Power only the batteries
S 387         local charge_factor = 0 -- Assume all batteries fully charged
388         if BA_eu_demand > 0 then
389             charge_factor = PR_eu_supply / BA_eu_demand
390         end
391         for n, pos1 in pairs(BA_nodes) do
392             meta1 = minetest.get_meta(pos1)
393             local eu_demand = meta1:get_int(eu_demand_str)
394             meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor))
395         end
396         for n, pos1 in pairs(RE_nodes) do
397             meta1 = minetest.get_meta(pos1)
398             meta1:set_int(eu_input_str, 0)
399         end
6abd85 400
ee5c6c 401     end,
K 402 })
ee0765 403
563a4c 404 -- Timeout ABM
N 405 -- Timeout for a node in case it was disconnected from the network
406 -- A node must be touched by the station continuously in order to function
407 local function switching_station_timeout_count(pos, tier)
408     local meta = minetest.get_meta(pos)
409     local timeout = meta:get_int(tier.."_EU_timeout")
410     if timeout <= 0 then
4ac36e 411         meta:set_int(tier.."_EU_input", 0) -- Not needed anymore <-- actually, it is for supply converter
563a4c 412         return true
N 413     else
414         meta:set_int(tier.."_EU_timeout", timeout - 1)
415         return false
416     end
417 end
418 minetest.register_abm({
78f16c 419     label = "Machines: timeout check",
563a4c 420     nodenames = {"group:technic_machine"},
N 421     interval   = 1,
422     chance     = 1,
423     action = function(pos, node, active_object_count, active_object_count_wider)
088eea 424         local meta = minetest.get_meta(pos)
563a4c 425         for tier, machines in pairs(technic.machines) do
N 426             if machines[node.name] and switching_station_timeout_count(pos, tier) then
427                 local nodedef = minetest.registered_nodes[node.name]
428                 if nodedef and nodedef.technic_disabled_machine_name then
429                     node.name = nodedef.technic_disabled_machine_name
430                     minetest.swap_node(pos, node)
1c617f 431                 elseif nodedef and nodedef.technic_on_disable then
N 432                     nodedef.technic_on_disable(pos, node)
563a4c 433                 end
N 434                 if nodedef then
435                     local meta = minetest.get_meta(pos)
436                     meta:set_string("infotext", S("%s Has No Network"):format(nodedef.description))
437                 end
438             end
439         end
440     end,
441 })
442
088eea 443 --Re-enable disabled switching station if necessary, similar to the timeout above
CK 444 minetest.register_abm({
78f16c 445     label = "Machines: re-enable check",
088eea 446     nodenames = {"technic:switching_station"},
CK 447     interval   = 1,
448     chance     = 1,
449     action = function(pos, node, active_object_count, active_object_count_wider)
450         local meta = minetest.get_meta(pos)
451         local pos1 = {x=pos.x,y=pos.y-1,z=pos.z}
452         local tier = technic.get_cable_tier(minetest.get_node(pos1).name)
453         if not tier then return end
454         if switching_station_timeout_count(pos, tier) then
455             local meta = minetest.get_meta(pos)
456             meta:set_int("active",1)
457         end
458     end,
459 })
460
ee0765 461 for tier, machines in pairs(technic.machines) do
S 462     -- SPECIAL will not be traversed
463     technic.register_machine(tier, "technic:switching_station", "SPECIAL")
464 end
465