| | |
| | | -- SWITCHING STATION |
| | | -- The switching station is the center of all power distribution on an electric network. |
| | | -- The station will collect all produced power from producers (PR) and batteries (BA) |
| | | -- and distribute it to receivers (RE) and depleted batteries (BA). |
| | | -- |
| | | -- The station collects power from sources (PR), distributes it to sinks (RE), |
| | | -- and uses the excess/shortfall to charge and discharge batteries (BA). |
| | | -- |
| | | -- For now, all supply and demand values are expressed in kW. |
| | | -- |
| | | -- It works like this: |
| | | -- All PR,BA,RE nodes are indexed and tagged with the switching station. |
| | |
| | | return true |
| | | end |
| | | |
| | | local load_position = function(pos) |
| | | local vm = VoxelManip() |
| | | local MinEdge, MaxEdge = vm:read_from_map(pos, pos) |
| | | end |
| | | |
| | | -- Generic function to add found connected nodes to the right classification array |
| | | local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos) |
| | | load_position(pos) |
| | | local check_node_subp = function(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, pos, machines, tier, sw_pos, from_below) |
| | | technic.get_or_load_node(pos) |
| | | local meta = minetest.get_meta(pos) |
| | | local name = minetest.get_node(pos).name |
| | | |
| | |
| | | add_new_cable_node(PR_nodes, pos) |
| | | add_new_cable_node(RE_nodes, pos) |
| | | elseif machines[name] == "SPECIAL" and |
| | | (pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) then |
| | | (pos.x ~= sw_pos.x or pos.y ~= sw_pos.y or pos.z ~= sw_pos.z) and |
| | | from_below then |
| | | -- Another switching station -> disable it |
| | | add_new_cable_node(SP_nodes, pos) |
| | | meta:set_int("active", 0) |
| | |
| | | {x=pos.x, y=pos.y, z=pos.z-1}} |
| | | --print("ON") |
| | | for i, cur_pos in pairs(positions) do |
| | | check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos) |
| | | check_node_subp(PR_nodes, RE_nodes, BA_nodes, SP_nodes, all_nodes, cur_pos, machines, tier, sw_pos, i == 3) |
| | | end |
| | | end |
| | | |
| | |
| | | -- Run all the nodes |
| | | local function run_nodes(list) |
| | | for _, pos2 in ipairs(list) do |
| | | load_position(pos2) |
| | | technic.get_or_load_node(pos2) |
| | | local node2 = minetest.get_node(pos2) |
| | | local nodedef |
| | | if node2 and node2.name then |
| | |
| | | local eu_demand_str = tier.."_EU_demand" |
| | | local eu_input_str = tier.."_EU_input" |
| | | local eu_supply_str = tier.."_EU_supply" |
| | | |
| | | -- Distribute charge equally across multiple batteries. |
| | | local charge_total = 0 |
| | | local battery_count = 0 |
| | | |
| | | for n, pos1 in pairs(BA_nodes) do |
| | | meta1 = minetest.get_meta(pos1) |
| | | local charge = meta1:get_int("internal_EU_charge") |
| | | |
| | | if (meta1:get_int(eu_demand_str) ~= 0) then |
| | | charge_total = charge_total + charge |
| | | battery_count = battery_count + 1 |
| | | end |
| | | end |
| | | |
| | | local charge_distributed = math.floor(charge_total / battery_count) |
| | | |
| | | for n, pos1 in pairs(BA_nodes) do |
| | | meta1 = minetest.get_meta(pos1) |
| | | |
| | | if (meta1:get_int(eu_demand_str) ~= 0) then |
| | | meta1:set_int("internal_EU_charge", charge_distributed) |
| | | end |
| | | end |
| | | |
| | | -- Get all the power from the PR nodes |
| | | local PR_eu_supply = 0 -- Total power |
| | |
| | | --dprint("Total BA demand:"..BA_eu_demand) |
| | | |
| | | meta:set_string("infotext", |
| | | S("%s. Supply: %d Demand: %d"):format( |
| | | technic.format(S("%s. Supply: %e Demand: %e"), |
| | | machine_name, PR_eu_supply, RE_eu_demand)) |
| | | |
| | | -- If the PR supply is enough for the RE demand supply them all |
| | |
| | | local meta = minetest.get_meta(pos) |
| | | local timeout = meta:get_int(tier.."_EU_timeout") |
| | | if timeout <= 0 then |
| | | --meta:set_int(tier.."_EU_input", 0) -- Not needed anymore |
| | | meta:set_int(tier.."_EU_input", 0) -- Not needed anymore <-- actually, it is for supply converter |
| | | return true |
| | | else |
| | | meta:set_int(tier.."_EU_timeout", timeout - 1) |
| | |
| | | if machines[node.name] and switching_station_timeout_count(pos, tier) then |
| | | local nodedef = minetest.registered_nodes[node.name] |
| | | if nodedef and nodedef.technic_disabled_machine_name then |
| | | print(nodedef.technic_disabled_machine_name) |
| | | node.name = nodedef.technic_disabled_machine_name |
| | | minetest.swap_node(pos, node) |
| | | elseif nodedef and nodedef.technic_on_disable then |
| | | nodedef.technic_on_disable(pos, node) |
| | | end |
| | | if nodedef then |
| | | local meta = minetest.get_meta(pos) |