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