Vanessa Dannenberg
2018-10-31 44cb8df048e09b64214f59db73a3fd23cfe12e77
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
563a4c 123     local remain = 0.9
N 124     -- Machine information
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)
7d5329 136     local demand = enabled and meta:get_int("power") or 0
563a4c 137
N 138     local pos_up        = {x=pos.x, y=pos.y+1, z=pos.z}
139     local pos_down      = {x=pos.x, y=pos.y-1, z=pos.z}
140     local name_up       = minetest.get_node(pos_up).name
141     local name_down     = minetest.get_node(pos_down).name
142
143     local from = technic.get_cable_tier(name_up)
144     local to   = technic.get_cable_tier(name_down)
145
146     if from and to then
147         local input = meta:get_int(from.."_EU_input")
148         meta:set_int(from.."_EU_demand", demand)
149         meta:set_int(from.."_EU_supply", 0)
150         meta:set_int(to.."_EU_demand", 0)
151         meta:set_int(to.."_EU_supply", input * remain)
41f175 152         meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name,
H 153             technic.EU_string(input), from,
154             technic.EU_string(input * remain), to))
563a4c 155     else
N 156         meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
efd5ff 157         if to then
N 158             meta:set_int(to.."_EU_supply", 0)
159         end
160         if from then
161             meta:set_int(from.."_EU_demand", 0)
162         end
563a4c 163         return
N 164     end
165
166 end
167
ee0765 168 minetest.register_node("technic:supply_converter", {
be2f30 169     description = S("Supply Converter"),
54004f 170     tiles  = {
VE 171         "technic_supply_converter_tb.png"..cable_entry,
172         "technic_supply_converter_tb.png"..cable_entry,
173         "technic_supply_converter_side.png",
174         "technic_supply_converter_side.png",
175         "technic_supply_converter_side.png",
176         "technic_supply_converter_side.png"
177         },
83c649 178     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
S 179         technic_machine=1, technic_all_tiers=1},
180     connect_sides = {"top", "bottom"},
ee0765 181     sounds = default.node_sound_wood_defaults(),
e8ac23 182     on_receive_fields = supply_converter_receive_fields,
ee0765 183     on_construct = function(pos)
a41390 184         local meta = minetest.get_meta(pos)
be2f30 185         meta:set_string("infotext", S("Supply Converter"))
6c4e90 186         if digilines_path then
D 187             meta:set_string("channel", "supply_converter"..minetest.pos_to_string(pos))
188         end
03df68 189         meta:set_int("power", 10000)
e8ac23 190         meta:set_int("enabled", 1)
NZ 191         meta:set_int("mesecon_mode", 0)
192         meta:set_int("mesecon_effect", 0)
193         set_supply_converter_formspec(meta)
ee0765 194     end,
e8ac23 195     mesecons = mesecons,
6c4e90 196     digiline = digiline_def,
563a4c 197     technic_run = run,
d3f40e 198     technic_on_disable = run,
ee0765 199 })
ee5c6c 200
K 201 minetest.register_craft({
202     output = 'technic:supply_converter 1',
203     recipe = {
44cb8d 204         {'basic_materials:gold_wire', 'technic:rubber',         'technic:doped_silicon_wafer'},
83c649 205         {'technic:mv_transformer', 'technic:machine_casing', 'technic:lv_transformer'},
S 206         {'technic:mv_cable',       'technic:rubber',         'technic:lv_cable'},
44cb8d 207     },
VD 208     replacements = { {"basic_materials:gold_wire", "basic_materials:empty_spool"}, },
ee5c6c 209 })
ee0765 210
S 211 for tier, machines in pairs(technic.machines) do
623fca 212     technic.register_machine(tier, "technic:supply_converter", technic.producer_receiver)
ee0765 213 end
S 214