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) |
|
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 |
be2f30
|
182 |
local machine_name = S("Switching Station") |
ee5c6c
|
183 |
|
ee0765
|
184 |
-- Which kind of network are we on: |
S |
185 |
pos1 = {x=pos.x, y=pos.y-1, z=pos.z} |
ee5c6c
|
186 |
|
ee0765
|
187 |
local name = minetest.get_node(pos1).name |
S |
188 |
local tier = technic.get_cable_tier(name) |
|
189 |
if tier then |
468d79
|
190 |
PR_nodes, BA_nodes, RE_nodes = get_network(pos1, tier) |
ee0765
|
191 |
else |
S |
192 |
--dprint("Not connected to a network") |
be2f30
|
193 |
meta:set_string("infotext", S("%s Has No Network"):format(machine_name)) |
ee0765
|
194 |
return |
S |
195 |
end |
|
196 |
--dprint("nodes="..table.getn(all_nodes) |
|
197 |
-- .." PR="..table.getn(PR_nodes) |
|
198 |
-- .." BA="..table.getn(BA_nodes) |
|
199 |
-- .." RE="..table.getn(RE_nodes)) |
ee5c6c
|
200 |
|
ee0765
|
201 |
-- Strings for the meta data |
S |
202 |
local eu_demand_str = tier.."_EU_demand" |
|
203 |
local eu_input_str = tier.."_EU_input" |
|
204 |
local eu_supply_str = tier.."_EU_supply" |
ee5c6c
|
205 |
|
ee0765
|
206 |
-- Get all the power from the PR nodes |
S |
207 |
local PR_eu_supply = 0 -- Total power |
|
208 |
for _, pos1 in pairs(PR_nodes) do |
|
209 |
meta1 = minetest.get_meta(pos1) |
|
210 |
PR_eu_supply = PR_eu_supply + meta1:get_int(eu_supply_str) |
|
211 |
end |
|
212 |
--dprint("Total PR supply:"..PR_eu_supply) |
ee5c6c
|
213 |
|
ee0765
|
214 |
-- Get all the demand from the RE nodes |
S |
215 |
local RE_eu_demand = 0 |
|
216 |
for _, pos1 in pairs(RE_nodes) do |
|
217 |
meta1 = minetest.get_meta(pos1) |
|
218 |
RE_eu_demand = RE_eu_demand + meta1:get_int(eu_demand_str) |
|
219 |
end |
|
220 |
--dprint("Total RE demand:"..RE_eu_demand) |
ee5c6c
|
221 |
|
ee0765
|
222 |
-- Get all the power from the BA nodes |
S |
223 |
local BA_eu_supply = 0 |
|
224 |
for _, pos1 in pairs(BA_nodes) do |
|
225 |
meta1 = minetest.get_meta(pos1) |
|
226 |
BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str) |
|
227 |
end |
|
228 |
--dprint("Total BA supply:"..BA_eu_supply) |
ee5c6c
|
229 |
|
ee0765
|
230 |
-- Get all the demand from the BA nodes |
S |
231 |
local BA_eu_demand = 0 |
|
232 |
for _, pos1 in pairs(BA_nodes) do |
|
233 |
meta1 = minetest.get_meta(pos1) |
|
234 |
BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str) |
|
235 |
end |
|
236 |
--dprint("Total BA demand:"..BA_eu_demand) |
ee5c6c
|
237 |
|
ee0765
|
238 |
meta:set_string("infotext", |
be2f30
|
239 |
S("%s. Supply: %d Demand: %d"):format( |
S |
240 |
machine_name, PR_eu_supply, RE_eu_demand)) |
ee5c6c
|
241 |
|
ee0765
|
242 |
-- If the PR supply is enough for the RE demand supply them all |
S |
243 |
if PR_eu_supply >= RE_eu_demand then |
|
244 |
--dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand) |
|
245 |
for _, pos1 in pairs(RE_nodes) do |
|
246 |
meta1 = minetest.get_meta(pos1) |
|
247 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
248 |
meta1:set_int(eu_input_str, eu_demand) |
|
249 |
end |
|
250 |
-- We have a surplus, so distribute the rest equally to the BA nodes |
|
251 |
-- Let's calculate the factor of the demand |
|
252 |
PR_eu_supply = PR_eu_supply - RE_eu_demand |
|
253 |
local charge_factor = 0 -- Assume all batteries fully charged |
|
254 |
if BA_eu_demand > 0 then |
|
255 |
charge_factor = PR_eu_supply / BA_eu_demand |
|
256 |
end |
|
257 |
for n, pos1 in pairs(BA_nodes) do |
|
258 |
meta1 = minetest.get_meta(pos1) |
|
259 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
260 |
meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor)) |
|
261 |
--dprint("Charging battery:"..math.floor(eu_demand*charge_factor)) |
|
262 |
end |
|
263 |
return |
|
264 |
end |
ee5c6c
|
265 |
|
ee0765
|
266 |
-- If the PR supply is not enough for the RE demand we will discharge the batteries too |
S |
267 |
if PR_eu_supply + BA_eu_supply >= RE_eu_demand then |
|
268 |
--dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand) |
|
269 |
for _, pos1 in pairs(RE_nodes) do |
|
270 |
meta1 = minetest.get_meta(pos1) |
|
271 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
272 |
meta1:set_int(eu_input_str, eu_demand) |
|
273 |
end |
|
274 |
-- We have a deficit, so distribute to the BA nodes |
|
275 |
-- Let's calculate the factor of the supply |
|
276 |
local charge_factor = 0 -- Assume all batteries depleted |
|
277 |
if BA_eu_supply > 0 then |
|
278 |
charge_factor = (PR_eu_supply - RE_eu_demand) / BA_eu_supply |
|
279 |
end |
|
280 |
for n,pos1 in pairs(BA_nodes) do |
|
281 |
meta1 = minetest.get_meta(pos1) |
ee5c6c
|
282 |
local eu_supply = meta1:get_int(eu_supply_str) |
ee0765
|
283 |
meta1:set_int(eu_input_str, math.floor(eu_supply * charge_factor)) |
S |
284 |
--dprint("Discharging battery:"..math.floor(eu_supply*charge_factor)) |
|
285 |
end |
|
286 |
return |
|
287 |
end |
ee5c6c
|
288 |
|
ee0765
|
289 |
-- If the PR+BA supply is not enough for the RE demand: Power only the batteries |
S |
290 |
local charge_factor = 0 -- Assume all batteries fully charged |
|
291 |
if BA_eu_demand > 0 then |
|
292 |
charge_factor = PR_eu_supply / BA_eu_demand |
|
293 |
end |
|
294 |
for n, pos1 in pairs(BA_nodes) do |
|
295 |
meta1 = minetest.get_meta(pos1) |
|
296 |
local eu_demand = meta1:get_int(eu_demand_str) |
|
297 |
meta1:set_int(eu_input_str, math.floor(eu_demand * charge_factor)) |
|
298 |
end |
|
299 |
for n, pos1 in pairs(RE_nodes) do |
|
300 |
meta1 = minetest.get_meta(pos1) |
|
301 |
meta1:set_int(eu_input_str, 0) |
|
302 |
end |
ee5c6c
|
303 |
end, |
K |
304 |
}) |
ee0765
|
305 |
|
S |
306 |
for tier, machines in pairs(technic.machines) do |
|
307 |
-- SPECIAL will not be traversed |
|
308 |
technic.register_machine(tier, "technic:switching_station", "SPECIAL") |
|
309 |
end |
|
310 |
|