you
2017-06-05 987cc5a6a425b1f9bcd9000608dc389a45c675a1
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)
75             local meta = minetest.get_meta(pos)
76             if channel ~= meta:get_string("channel") then
77                 return
78             end
79             msg = msg:lower()
80             if msg == "get" then
81                 digilines.receptor_send(pos, digilines.rules.default, channel, {
82                     enabled      = meta:get_int("enabled"),
83                     power        = meta:get_int("power"),
84                     mesecon_mode = meta:get_int("mesecon_mode")
85                 })
86                 return
87             elseif msg == "off" then
88                 meta:set_int("enabled", 0)
89             elseif msg == "on" then
90                 meta:set_int("enabled", 1)
91             elseif msg == "toggle" then
92                 local onn = meta:get_int("enabled")
93                 onn = -(onn-1) -- Mirror onn with pivot 0.5, so switch between 1 and 0.
94                 meta:set_int("enabled", onn)
95             elseif msg:sub(1, 5) == "power" then
96                 local power = tonumber(msg:sub(7))
97                 if not power then
98                     return
99                 end
100                 power = math.max(power, 0)
101                 power = math.min(power, 10000)
102                 power = 100 * math.floor(power / 100)
103                 meta:set_int("power", power)
104             elseif msg:sub(1, 12) == "mesecon_mode" then
105                 meta:set_int("mesecon_mode", tonumber(msg:sub(14)))
106             end
107             set_supply_converter_formspec(meta)
108         end
109     },
e8ac23 110 }
NZ 111
10307f 112 local run = function(pos, node, run_stage)
M'P 113     -- run only in producer stage.
114     if run_stage == technic.receiver then
115         return
116     end
117
563a4c 118     local remain = 0.9
N 119     -- Machine information
120     local machine_name  = S("Supply Converter")
121     local meta          = minetest.get_meta(pos)
408128 122     local enabled       = meta:get_string("enabled")
NZ 123     if enabled == "" then
124         -- Backwards compatibility
125         minetest.registered_nodes["technic:supply_converter"].on_construct(pos)
126         enabled = true
127     else
128         enabled = enabled == "1"
129     end
130     enabled = enabled and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0)
7d5329 131     local demand = enabled and meta:get_int("power") or 0
563a4c 132
N 133     local pos_up        = {x=pos.x, y=pos.y+1, z=pos.z}
134     local pos_down      = {x=pos.x, y=pos.y-1, z=pos.z}
135     local name_up       = minetest.get_node(pos_up).name
136     local name_down     = minetest.get_node(pos_down).name
137
138     local from = technic.get_cable_tier(name_up)
139     local to   = technic.get_cable_tier(name_down)
140
141     if from and to then
142         local input = meta:get_int(from.."_EU_input")
143         meta:set_int(from.."_EU_demand", demand)
144         meta:set_int(from.."_EU_supply", 0)
145         meta:set_int(to.."_EU_demand", 0)
146         meta:set_int(to.."_EU_supply", input * remain)
85a984 147         meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, technic.pretty_num(input), from, technic.pretty_num(input * remain), to))
563a4c 148     else
N 149         meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
efd5ff 150         if to then
N 151             meta:set_int(to.."_EU_supply", 0)
152         end
153         if from then
154             meta:set_int(from.."_EU_demand", 0)
155         end
563a4c 156         return
N 157     end
158
159 end
160
ee0765 161 minetest.register_node("technic:supply_converter", {
be2f30 162     description = S("Supply Converter"),
54004f 163     tiles  = {
VE 164         "technic_supply_converter_tb.png"..cable_entry,
165         "technic_supply_converter_tb.png"..cable_entry,
166         "technic_supply_converter_side.png",
167         "technic_supply_converter_side.png",
168         "technic_supply_converter_side.png",
169         "technic_supply_converter_side.png"
170         },
83c649 171     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
S 172         technic_machine=1, technic_all_tiers=1},
173     connect_sides = {"top", "bottom"},
ee0765 174     sounds = default.node_sound_wood_defaults(),
e8ac23 175     on_receive_fields = supply_converter_receive_fields,
ee0765 176     on_construct = function(pos)
a41390 177         local meta = minetest.get_meta(pos)
be2f30 178         meta:set_string("infotext", S("Supply Converter"))
6c4e90 179         if digilines_path then
D 180             meta:set_string("channel", "supply_converter"..minetest.pos_to_string(pos))
181         end
03df68 182         meta:set_int("power", 10000)
e8ac23 183         meta:set_int("enabled", 1)
NZ 184         meta:set_int("mesecon_mode", 0)
185         meta:set_int("mesecon_effect", 0)
186         set_supply_converter_formspec(meta)
ee0765 187     end,
e8ac23 188     mesecons = mesecons,
6c4e90 189     digiline = digiline_def,
563a4c 190     technic_run = run,
d3f40e 191     technic_on_disable = run,
ee0765 192 })
ee5c6c 193
K 194 minetest.register_craft({
195     output = 'technic:supply_converter 1',
196     recipe = {
83c649 197         {'technic:fine_gold_wire', 'technic:rubber',         'technic:doped_silicon_wafer'},
S 198         {'technic:mv_transformer', 'technic:machine_casing', 'technic:lv_transformer'},
199         {'technic:mv_cable',       'technic:rubber',         'technic:lv_cable'},
ee5c6c 200     }
K 201 })
ee0765 202
S 203 for tier, machines in pairs(technic.machines) do
623fca 204     technic.register_machine(tier, "technic:supply_converter", technic.producer_receiver)
ee0765 205 end
S 206