Maciej 'agaran' Pijanka
2017-03-15 10307f23a78b33af50dc4a5f3d1baafb4ee4b0d9
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
10307f 61 local run = function(pos, node, run_stage)
M'P 62     -- run only in producer stage.
63     if run_stage == technic.receiver then
64         return
65     end
66
563a4c 67     local remain = 0.9
N 68     -- Machine information
69     local machine_name  = S("Supply Converter")
70     local meta          = minetest.get_meta(pos)
03df68 71     local enabled = meta:get_int("enabled") ~= 0 and (meta:get_int("mesecon_mode") == 0 or meta:get_int("mesecon_effect") ~= 0)
7d5329 72     local demand = enabled and meta:get_int("power") or 0
563a4c 73
N 74     local pos_up        = {x=pos.x, y=pos.y+1, z=pos.z}
75     local pos_down      = {x=pos.x, y=pos.y-1, z=pos.z}
76     local name_up       = minetest.get_node(pos_up).name
77     local name_down     = minetest.get_node(pos_down).name
78
79     local from = technic.get_cable_tier(name_up)
80     local to   = technic.get_cable_tier(name_down)
81
82     if from and to then
83         local input = meta:get_int(from.."_EU_input")
84         meta:set_int(from.."_EU_demand", demand)
85         meta:set_int(from.."_EU_supply", 0)
86         meta:set_int(to.."_EU_demand", 0)
87         meta:set_int(to.."_EU_supply", input * remain)
85a984 88         meta:set_string("infotext", S("@1 (@2 @3 -> @4 @5)", machine_name, technic.pretty_num(input), from, technic.pretty_num(input * remain), to))
563a4c 89     else
N 90         meta:set_string("infotext", S("%s Has Bad Cabling"):format(machine_name))
efd5ff 91         if to then
N 92             meta:set_int(to.."_EU_supply", 0)
93         end
94         if from then
95             meta:set_int(from.."_EU_demand", 0)
96         end
563a4c 97         return
N 98     end
99
100 end
101
ee0765 102 minetest.register_node("technic:supply_converter", {
be2f30 103     description = S("Supply Converter"),
ee0765 104     tiles  = {"technic_supply_converter_top.png", "technic_supply_converter_bottom.png",
S 105               "technic_supply_converter_side.png", "technic_supply_converter_side.png",
106               "technic_supply_converter_side.png", "technic_supply_converter_side.png"},
83c649 107     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2,
S 108         technic_machine=1, technic_all_tiers=1},
109     connect_sides = {"top", "bottom"},
ee0765 110     sounds = default.node_sound_wood_defaults(),
e8ac23 111     on_receive_fields = supply_converter_receive_fields,
ee0765 112     on_construct = function(pos)
a41390 113         local meta = minetest.get_meta(pos)
be2f30 114         meta:set_string("infotext", S("Supply Converter"))
03df68 115         meta:set_int("power", 10000)
e8ac23 116         meta:set_int("enabled", 1)
NZ 117         meta:set_int("mesecon_mode", 0)
118         meta:set_int("mesecon_effect", 0)
119         set_supply_converter_formspec(meta)
ee0765 120     end,
e8ac23 121     mesecons = mesecons,
563a4c 122     technic_run = run,
d3f40e 123     technic_on_disable = run,
ee0765 124 })
ee5c6c 125
K 126 minetest.register_craft({
127     output = 'technic:supply_converter 1',
128     recipe = {
83c649 129         {'technic:fine_gold_wire', 'technic:rubber',         'technic:doped_silicon_wafer'},
S 130         {'technic:mv_transformer', 'technic:machine_casing', 'technic:lv_transformer'},
131         {'technic:mv_cable',       'technic:rubber',         'technic:lv_cable'},
ee5c6c 132     }
K 133 })
ee0765 134
S 135 for tier, machines in pairs(technic.machines) do
623fca 136     technic.register_machine(tier, "technic:supply_converter", technic.producer_receiver)
ee0765 137 end
S 138