commit | author | age
|
ee5c6c
|
1 |
-- The supply converter is a generic device which can convert from |
K |
2 |
-- LV to MV and back, and HV to MV and back. |
|
3 |
-- The machine is configured by the wiring below and above it. |
|
4 |
-- |
|
5 |
-- It works like this: |
ee0765
|
6 |
-- The top side is setup as the receiver side, the bottom as the producer side. |
S |
7 |
-- Once the receiver side is powered it will deliver power to the other side. |
ee5c6c
|
8 |
-- Unused power is wasted just like any other producer! |
ee0765
|
9 |
|
6c4e90
|
10 |
local digilines_path = minetest.get_modpath("digilines") |
D |
11 |
|
be2f30
|
12 |
local S = technic.getter |
S |
13 |
|
54004f
|
14 |
local cable_entry = "^technic_cable_connection_overlay.png" |
VE |
15 |
|
e8ac23
|
16 |
local function set_supply_converter_formspec(meta) |
03df68
|
17 |
local formspec = "size[5,2.25]".. |
NZ |
18 |
"field[0.3,0.5;2,1;power;"..S("Input Power")..";"..meta:get_int("power").."]" |
6c4e90
|
19 |
if digilines_path then |
D |
20 |
formspec = formspec.. |
|
21 |
"field[2.3,0.5;3,1;channel;Digiline Channel;"..meta:get_string("channel").."]" |
|
22 |
end |
e8ac23
|
23 |
-- The names for these toggle buttons are explicit about which |
NZ |
24 |
-- state they'll switch to, so that multiple presses (arising |
|
25 |
-- from the ambiguity between lag and a missed press) only make |
|
26 |
-- the single change that the user expects. |
|
27 |
if meta:get_int("mesecon_mode") == 0 then |
03df68
|
28 |
formspec = formspec.."button[0,1;5,1;mesecon_mode_1;"..S("Ignoring Mesecon Signal").."]" |
e8ac23
|
29 |
else |
03df68
|
30 |
formspec = formspec.."button[0,1;5,1;mesecon_mode_0;"..S("Controlled by Mesecon Signal").."]" |
e8ac23
|
31 |
end |
NZ |
32 |
if meta:get_int("enabled") == 0 then |
03df68
|
33 |
formspec = formspec.."button[0,1.75;5,1;enable;"..S("%s Disabled"):format(S("Supply Converter")).."]" |
e8ac23
|
34 |
else |
03df68
|
35 |
formspec = formspec.."button[0,1.75;5,1;disable;"..S("%s Enabled"):format(S("Supply Converter")).."]" |
e8ac23
|
36 |
end |
NZ |
37 |
meta:set_string("formspec", formspec) |
|
38 |
end |
|
39 |
|
|
40 |
local supply_converter_receive_fields = function(pos, formname, fields, sender) |
|
41 |
local meta = minetest.get_meta(pos) |
03df68
|
42 |
local power = nil |
NZ |
43 |
if fields.power then |
|
44 |
power = tonumber(fields.power) or 0 |
|
45 |
power = math.max(power, 0) |
|
46 |
power = math.min(power, 10000) |
6c4e90
|
47 |
power = 100 * math.floor(power / 100) |
03df68
|
48 |
if power == meta:get_int("power") then power = nil end |
NZ |
49 |
end |
|
50 |
if power then meta:set_int("power", power) end |
6c4e90
|
51 |
if fields.channel then meta:set_string("channel", fields.channel) end |
D |
52 |
if fields.enable then meta:set_int("enabled", 1) end |
e8ac23
|
53 |
if fields.disable then meta:set_int("enabled", 0) end |
NZ |
54 |
if fields.mesecon_mode_0 then meta:set_int("mesecon_mode", 0) end |
|
55 |
if fields.mesecon_mode_1 then meta:set_int("mesecon_mode", 1) end |
|
56 |
set_supply_converter_formspec(meta) |
|
57 |
end |
|
58 |
|
|
59 |
local mesecons = { |
|
60 |
effector = { |
|
61 |
action_on = function(pos, node) |
|
62 |
minetest.get_meta(pos):set_int("mesecon_effect", 1) |
|
63 |
end, |
|
64 |
action_off = function(pos, node) |
|
65 |
minetest.get_meta(pos):set_int("mesecon_effect", 0) |
|
66 |
end |
|
67 |
} |
6c4e90
|
68 |
} |
D |
69 |
|
|
70 |
|
|
71 |
local digiline_def = { |
|
72 |
receptor = {action = function() end}, |
|
73 |
effector = { |
|
74 |
action = function(pos, node, channel, msg) |
37d491
|
75 |
if type(msg) ~= "string" then |
D |
76 |
return |
|
77 |
end |
6c4e90
|
78 |
local meta = minetest.get_meta(pos) |
D |
79 |
if channel ~= meta:get_string("channel") then |
|
80 |
return |
|
81 |
end |
|
82 |
msg = msg:lower() |
|
83 |
if msg == "get" then |
|
84 |
digilines.receptor_send(pos, digilines.rules.default, channel, { |
|
85 |
enabled = meta:get_int("enabled"), |
|
86 |
power = meta:get_int("power"), |
|
87 |
mesecon_mode = meta:get_int("mesecon_mode") |
|
88 |
}) |
|
89 |
return |
|
90 |
elseif msg == "off" then |
|
91 |
meta:set_int("enabled", 0) |
|
92 |
elseif msg == "on" then |
|
93 |
meta:set_int("enabled", 1) |
|
94 |
elseif msg == "toggle" then |
|
95 |
local onn = meta:get_int("enabled") |
37d491
|
96 |
onn = 1-onn -- Mirror onn with pivot 0.5, so switch between 1 and 0. |
6c4e90
|
97 |
meta:set_int("enabled", onn) |
D |
98 |
elseif msg:sub(1, 5) == "power" then |
|
99 |
local power = tonumber(msg:sub(7)) |
|
100 |
if not power then |
|
101 |
return |
|
102 |
end |
|
103 |
power = math.max(power, 0) |
|
104 |
power = math.min(power, 10000) |
|
105 |
power = 100 * math.floor(power / 100) |
|
106 |
meta:set_int("power", power) |
|
107 |
elseif msg:sub(1, 12) == "mesecon_mode" then |
|
108 |
meta:set_int("mesecon_mode", tonumber(msg:sub(14))) |
37d491
|
109 |
else |
D |
110 |
return |
6c4e90
|
111 |
end |
D |
112 |
set_supply_converter_formspec(meta) |
|
113 |
end |
|
114 |
}, |
e8ac23
|
115 |
} |
NZ |
116 |
|
10307f
|
117 |
local run = function(pos, node, run_stage) |
M'P |
118 |
-- run only in producer stage. |
|
119 |
if run_stage == technic.receiver then |
|
120 |
return |
|
121 |
end |
|
122 |
|
718a5b
|
123 |
local efficiency = 0.9 |
563a4c
|
124 |
-- Machine information |
N |
125 |
local machine_name = S("Supply Converter") |
|
126 |
local meta = minetest.get_meta(pos) |
408128
|
127 |
local enabled = meta:get_string("enabled") |
NZ |
128 |
if enabled == "" then |
|
129 |
-- Backwards compatibility |
|
130 |
minetest.registered_nodes["technic:supply_converter"].on_construct(pos) |
|
131 |
enabled = true |
|
132 |
else |
|
133 |
enabled = enabled == "1" |
|
134 |
end |
|
135 |
enabled = enabled and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0) |
563a4c
|
136 |
|
N |
137 |
local pos_up = {x=pos.x, y=pos.y+1, z=pos.z} |
|
138 |
local pos_down = {x=pos.x, y=pos.y-1, z=pos.z} |
|
139 |
local name_up = minetest.get_node(pos_up).name |
|
140 |
local name_down = minetest.get_node(pos_down).name |
|
141 |
|
|
142 |
local from = technic.get_cable_tier(name_up) |
|
143 |
local to = technic.get_cable_tier(name_down) |
|
144 |
|
|
145 |
if from and to then |
718a5b
|
146 |
-- Get the "to" network switching station for EU demand calculation |
Z |
147 |
local network_hash = technic.cables[minetest.hash_node_position(pos_down)] |
|
148 |
local network = network_hash and minetest.get_position_from_hash(network_hash) |
|
149 |
local sw_pos = network and {x=network.x,y=network.y+1,z=network.z} |
|
150 |
local timeout = 0 |
|
151 |
for tier in pairs(technic.machines) do |
|
152 |
-- Supply converter must be connected to a network |
|
153 |
timeout = math.max(meta:get_int(tier.."_EU_timeout"), timeout) |
|
154 |
end |
|
155 |
if timeout > 0 and sw_pos and minetest.get_node(sw_pos).name == "technic:switching_station" then |
|
156 |
local sw_meta = minetest.get_meta(sw_pos) |
|
157 |
local demand = 0 |
|
158 |
if enabled then |
|
159 |
-- Reverse evaluate the required machine and round to a nice number |
705961
|
160 |
demand = sw_meta:get_int("ba_demand") + sw_meta:get_int("demand") |
S |
161 |
demand = 100 * math.ceil(demand / efficiency / 100) |
718a5b
|
162 |
-- Do not draw more than the limit |
Z |
163 |
demand = math.min(demand, meta:get_int("power")) |
|
164 |
end |
|
165 |
|
|
166 |
local input = meta:get_int(from.."_EU_input") -- actual input |
|
167 |
meta:set_int(from.."_EU_demand", demand) -- desired input |
|
168 |
meta:set_int(from.."_EU_supply", 0) |
|
169 |
meta:set_int(to.."_EU_demand", 0) |
|
170 |
meta:set_int(to.."_EU_supply", input * efficiency) |
|
171 |
meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, |
|
172 |
technic.EU_string(input), from, |
|
173 |
technic.EU_string(input * efficiency), to)) |
|
174 |
else |
|
175 |
meta:set_string("infotext",S("%s Has No Network"):format(machine_name)) |
|
176 |
end |
563a4c
|
177 |
else |
N |
178 |
meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name)) |
efd5ff
|
179 |
if to then |
N |
180 |
meta:set_int(to.."_EU_supply", 0) |
|
181 |
end |
|
182 |
if from then |
|
183 |
meta:set_int(from.."_EU_demand", 0) |
|
184 |
end |
563a4c
|
185 |
return |
N |
186 |
end |
|
187 |
|
|
188 |
end |
|
189 |
|
ee0765
|
190 |
minetest.register_node("technic:supply_converter", { |
be2f30
|
191 |
description = S("Supply Converter"), |
54004f
|
192 |
tiles = { |
VE |
193 |
"technic_supply_converter_tb.png"..cable_entry, |
|
194 |
"technic_supply_converter_tb.png"..cable_entry, |
|
195 |
"technic_supply_converter_side.png", |
|
196 |
"technic_supply_converter_side.png", |
|
197 |
"technic_supply_converter_side.png", |
|
198 |
"technic_supply_converter_side.png" |
|
199 |
}, |
83c649
|
200 |
groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, |
S |
201 |
technic_machine=1, technic_all_tiers=1}, |
|
202 |
connect_sides = {"top", "bottom"}, |
ee0765
|
203 |
sounds = default.node_sound_wood_defaults(), |
e8ac23
|
204 |
on_receive_fields = supply_converter_receive_fields, |
ee0765
|
205 |
on_construct = function(pos) |
a41390
|
206 |
local meta = minetest.get_meta(pos) |
be2f30
|
207 |
meta:set_string("infotext", S("Supply Converter")) |
6c4e90
|
208 |
if digilines_path then |
D |
209 |
meta:set_string("channel", "supply_converter"..minetest.pos_to_string(pos)) |
|
210 |
end |
03df68
|
211 |
meta:set_int("power", 10000) |
e8ac23
|
212 |
meta:set_int("enabled", 1) |
NZ |
213 |
meta:set_int("mesecon_mode", 0) |
|
214 |
meta:set_int("mesecon_effect", 0) |
|
215 |
set_supply_converter_formspec(meta) |
ee0765
|
216 |
end, |
e8ac23
|
217 |
mesecons = mesecons, |
6c4e90
|
218 |
digiline = digiline_def, |
563a4c
|
219 |
technic_run = run, |
d3f40e
|
220 |
technic_on_disable = run, |
ee0765
|
221 |
}) |
ee5c6c
|
222 |
|
K |
223 |
minetest.register_craft({ |
|
224 |
output = 'technic:supply_converter 1', |
|
225 |
recipe = { |
44cb8d
|
226 |
{'basic_materials:gold_wire', 'technic:rubber', 'technic:doped_silicon_wafer'}, |
83c649
|
227 |
{'technic:mv_transformer', 'technic:machine_casing', 'technic:lv_transformer'}, |
S |
228 |
{'technic:mv_cable', 'technic:rubber', 'technic:lv_cable'}, |
44cb8d
|
229 |
}, |
VD |
230 |
replacements = { {"basic_materials:gold_wire", "basic_materials:empty_spool"}, }, |
ee5c6c
|
231 |
}) |
ee0765
|
232 |
|
S |
233 |
for tier, machines in pairs(technic.machines) do |
623fca
|
234 |
technic.register_machine(tier, "technic:supply_converter", technic.producer_receiver) |
ee0765
|
235 |
end |
S |
236 |
|