Vanessa Ezekowitz
2017-03-10 343c7946d9014bf111e25a7a225a1b6f5746992b
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
be2f30 10 local S = technic.getter
S 11
e8ac23 12 local function set_supply_converter_formspec(meta)
03df68 13     local formspec = "size[5,2.25]"..
NZ 14         "field[0.3,0.5;2,1;power;"..S("Input Power")..";"..meta:get_int("power").."]"
e8ac23 15     -- The names for these toggle buttons are explicit about which
NZ 16     -- state they'll switch to, so that multiple presses (arising
17     -- from the ambiguity between lag and a missed press) only make
18     -- the single change that the user expects.
19     if meta:get_int("mesecon_mode") == 0 then
03df68 20         formspec = formspec.."button[0,1;5,1;mesecon_mode_1;"..S("Ignoring Mesecon Signal").."]"
e8ac23 21     else
03df68 22         formspec = formspec.."button[0,1;5,1;mesecon_mode_0;"..S("Controlled by Mesecon Signal").."]"
e8ac23 23     end
NZ 24     if meta:get_int("enabled") == 0 then
03df68 25         formspec = formspec.."button[0,1.75;5,1;enable;"..S("%s Disabled"):format(S("Supply Converter")).."]"
e8ac23 26     else
03df68 27         formspec = formspec.."button[0,1.75;5,1;disable;"..S("%s Enabled"):format(S("Supply Converter")).."]"
e8ac23 28     end
NZ 29     meta:set_string("formspec", formspec)
30 end
31
32 local supply_converter_receive_fields = function(pos, formname, fields, sender)
33     local meta = minetest.get_meta(pos)
03df68 34     local power = nil
NZ 35     if fields.power then
36         power = tonumber(fields.power) or 0
37         power = 100 * math.floor(power / 100)
38         power = math.max(power, 0)
39         power = math.min(power, 10000)
40         if power == meta:get_int("power") then power = nil end
41     end
42     if power then meta:set_int("power", power) end
e8ac23 43     if fields.enable then meta:set_int("enabled", 1) end
NZ 44     if fields.disable then meta:set_int("enabled", 0) end
45     if fields.mesecon_mode_0 then meta:set_int("mesecon_mode", 0) end
46     if fields.mesecon_mode_1 then meta:set_int("mesecon_mode", 1) end
47     set_supply_converter_formspec(meta)
48 end
49
50 local mesecons = {
51     effector = {
52         action_on = function(pos, node)
53             minetest.get_meta(pos):set_int("mesecon_effect", 1)
54         end,
55         action_off = function(pos, node)
56             minetest.get_meta(pos):set_int("mesecon_effect", 0)
57         end
58     }
59 }
60
563a4c 61 local run = function(pos, node)
N 62     local remain = 0.9
63     -- Machine information
64     local machine_name  = S("Supply Converter")
65     local meta          = minetest.get_meta(pos)
03df68 66     local enabled = meta:get_int("enabled") ~= 0 and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0)
NZ 67     local demand = enabled and meta:get_int("power") or 10000
563a4c 68
N 69     local pos_up        = {x=pos.x, y=pos.y+1, z=pos.z}
70     local pos_down      = {x=pos.x, y=pos.y-1, z=pos.z}
71     local name_up       = minetest.get_node(pos_up).name
72     local name_down     = minetest.get_node(pos_down).name
73
74     local from = technic.get_cable_tier(name_up)
75     local to   = technic.get_cable_tier(name_down)
76
77     if from and to then
78         local input = meta:get_int(from.."_EU_input")
79         meta:set_int(from.."_EU_demand", demand)
80         meta:set_int(from.."_EU_supply", 0)
81         meta:set_int(to.."_EU_demand", 0)
82         meta:set_int(to.."_EU_supply", input * remain)
85a984 83         meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, technic.pretty_num(input), from, technic.pretty_num(input * remain), to))
563a4c 84     else
N 85         meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
efd5ff 86         if to then
N 87             meta:set_int(to.."_EU_supply", 0)
88         end
89         if from then
90             meta:set_int(from.."_EU_demand", 0)
91         end
563a4c 92         return
N 93     end
94
95 end
96
ee0765 97 minetest.register_node("technic:supply_converter", {
be2f30 98     description = S("Supply Converter"),
ee0765 99     tiles  = {"technic_supply_converter_top.png", "technic_supply_converter_bottom.png",
S 100               "technic_supply_converter_side.png", "technic_supply_converter_side.png",
101               "technic_supply_converter_side.png", "technic_supply_converter_side.png"},
83c649 102     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
S 103         technic_machine=1, technic_all_tiers=1},
104     connect_sides = {"top", "bottom"},
ee0765 105     sounds = default.node_sound_wood_defaults(),
e8ac23 106     on_receive_fields = supply_converter_receive_fields,
ee0765 107     on_construct = function(pos)
a41390 108         local meta = minetest.get_meta(pos)
be2f30 109         meta:set_string("infotext", S("Supply Converter"))
03df68 110         meta:set_int("power", 10000)
e8ac23 111         meta:set_int("enabled", 1)
NZ 112         meta:set_int("mesecon_mode", 0)
113         meta:set_int("mesecon_effect", 0)
114         set_supply_converter_formspec(meta)
ee0765 115     end,
e8ac23 116     mesecons = mesecons,
563a4c 117     technic_run = run,
d3f40e 118     technic_on_disable = run,
ee0765 119 })
ee5c6c 120
K 121 minetest.register_craft({
122     output = 'technic:supply_converter 1',
123     recipe = {
83c649 124         {'technic:fine_gold_wire', 'technic:rubber',         'technic:doped_silicon_wafer'},
S 125         {'technic:mv_transformer', 'technic:machine_casing', 'technic:lv_transformer'},
126         {'technic:mv_cable',       'technic:rubber',         'technic:lv_cable'},
ee5c6c 127     }
K 128 })
ee0765 129
S 130 for tier, machines in pairs(technic.machines) do
623fca 131     technic.register_machine(tier, "technic:supply_converter", technic.producer_receiver)
ee0765 132 end
S 133