kpoppel
2013-07-02 053fa59739f4b772174bf0a090969b3395ab3f98
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 will not convert from HV directly to LV.
4 -- The machine is configured by the wiring below and above it.
5 -- It is prepared for an upgrade slot if this is to be implemented later.
6 --
7 -- The conversion factor is a constant and the conversion is a lossy operation.
8 --
9 -- It works like this:
10 --   The top side is setup as the "RE" side, the bottom as the "PR" side.
11 --   Once the RE side is powered it will deliver power to the other side.
12 --   Unused power is wasted just like any other producer!
13 --   
14 minetest.register_node(
15    "technic:supply_converter", {
16       description = "Supply Converter",
17       tiles  = {"technic_mv_down_converter_top.png", "technic_mv_down_converter_bottom.png", "technic_mv_down_converter_side.png",
18         "technic_mv_down_converter_side.png", "technic_mv_down_converter_side.png", "technic_mv_down_converter_side.png"},
19       groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
20       sounds = default.node_sound_wood_defaults(),
21       drawtype = "nodebox",
22       paramtype = "light",
23       is_ground_content = true,
24       node_box = {
25      type = "fixed",
26      fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
27       },
28       selection_box = {
29      type = "fixed",
30      fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
31       },
32       on_construct = function(pos)
33             local meta = minetest.env:get_meta(pos)
34             meta:set_float("technic_hv_power_machine", 1)
35             meta:set_float("technic_mv_power_machine", 1)
36             meta:set_float("technic_power_machine", 1)
37             meta:set_string("infotext", "Supply Converter")
38               meta:set_float("active", false)
39                end,
40    })
41
42 minetest.register_craft({
43     output = 'technic:supply_converter 1',
44     recipe = {
45         {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot','technic:stainless_steel_ingot'},
46         {'technic:mv_transformer',        'technic:mv_cable',             'technic:lv_transformer'},
47         {'technic:mv_cable',              'technic:rubber',               'technic:lv_cable'},
48     }
49 })
50
51 minetest.register_abm(
52    { nodenames = {"technic:supply_converter"},
53      interval   = 1,
54      chance     = 1,
55      action = function(pos, node, active_object_count, active_object_count_wider)
56          -- Conversion factors (a picture of V*A - loss) Asymmetric.
57          local lv_mv_factor  = 5 -- division (higher is less efficient)
58          local mv_lv_factor  = 4 -- multiplication (higher is more efficient)
59          local mv_hv_factor  = 5 -- division
60          local hv_mv_factor  = 4 -- multiplication
61          local max_lv_demand = 2000 -- The increment size pwer supply tier. Determines how many are needed
62          local max_mv_demand = 2000 -- -""-
63          local max_hv_demand = 2000 -- -""-
64
65          -- Machine information
66          local machine_name  = "Supply Converter"
67          local meta          = minetest.env:get_meta(pos)
68          local upgrade       = "" -- Replace with expansion slot later??
69          
70          -- High voltage on top, low at bottom regardless of converter direction
71          local pos_up        = {x=pos.x, y=pos.y+1, z=pos.z}
72          local pos_down      = {x=pos.x, y=pos.y-1, z=pos.z}
73          local meta_up       = minetest.env:get_meta(pos_up)
74          local meta_down     = minetest.env:get_meta(pos_down)
75          local convert_MV_LV = 0
76          local convert_LV_MV = 0
77          local convert_MV_HV = 0
78          local convert_HV_MV = 0
79          -- check cabling
80          if meta_up:get_float("mv_cablelike") == 1 and meta_down:get_float("cablelike") == 1 then
81             convert_MV_LV = 1
82             upgrade = "MV-LV step down"
83          end
84          if meta_up:get_float("cablelike") == 1 and meta_down:get_float("mv_cablelike") == 1 then
85             convert_LV_MV = 1
86             upgrade = "LV-MV step up"
87          end
88          if meta_up:get_float("mv_cablelike") == 1 and meta_down:get_float("hv_cablelike") == 1 then
89             convert_MV_HV = 1
90             upgrade = "MV-HV step up"
91          end
92          if meta_up:get_float("hv_cablelike") == 1 and meta_down:get_float("mv_cablelike") == 1 then
93             convert_HV_MV = 1
94             upgrade = "HV-MV step down"
95          end
96          --print("Cabling:"..convert_MV_LV.."|"..convert_LV_MV.."|"..convert_HV_MV.."|"..convert_MV_HV)
97
98          if convert_MV_LV == 0 and convert_LV_MV == 0 and convert_HV_MV == 0 and convert_MV_HV == 0 then
99             meta:set_string("infotext", machine_name.." has bad cabling")
100             return
101          end
102
103          -- The node is programmed with an upgrade slot
104          -- containing a MV-LV step down, LV-MV step up, HV-MV step down or MV-HV step up unit
105
106          if upgrade == "" then
107             meta:set_string("infotext", machine_name.." has an empty converter slot");
108             technic.unregister_LV_machine("technic:supply_converter")
109             technic.unregister_MV_machine("technic:supply_converter")
110             technic.unregister_HV_machine("technic:supply_converter")
111             meta:set_int("LV_EU_demand", 0)
112             meta:set_int("LV_EU_supply", 0)
113             meta:set_int("LV_EU_input",  0)
114             meta:set_int("MV_EU_demand", 0)
115             meta:set_int("MV_EU_supply", 0)
116             meta:set_int("MV_EU_input",  0)
117             meta:set_int("HV_EU_demand", 0)
118             meta:set_int("HV_EU_supply", 0)
119             meta:set_int("HV_EU_input",  0)
120             return
121          end
122
123          -- State machine
124          if upgrade == "MV-LV step down" and convert_MV_LV then
125             -- Register machine type
126             technic.register_LV_machine("technic:supply_converter","PR")
127             technic.register_MV_machine("technic:supply_converter","RE")
128
129             -- Power off automatically if no longer connected to a switching station
130             technic.switching_station_timeout_count(pos, "MV")
131
132             local eu_input  = meta:get_int("MV_EU_input")
133             if eu_input == 0 then
134                -- Unpowered - go idle
135                --hacky_swap_node(pos, machine_node)
136                meta:set_string("infotext", machine_name.." Unpowered")
137                meta:set_int("LV_EU_supply", 0)
138                meta:set_int("MV_EU_supply", 0)
139
140                meta:set_int("LV_EU_demand", 0)
141                meta:set_int("MV_EU_demand", max_mv_demand)
142             else
143                -- MV side has got power to spare
144                meta:set_string("infotext", machine_name.." is active (MV:"..max_mv_demand.."->LV:"..eu_input*mv_lv_factor..")");
145                meta:set_int("LV_EU_supply", eu_input*mv_lv_factor)
146             end
147             ---------------------------------------------------
148          elseif upgrade == "LV-MV step up"   and convert_LV_MV then
149             -- Register machine type
150             technic.register_LV_machine("technic:supply_converter","RE")
151             technic.register_MV_machine("technic:supply_converter","PR")
152
153             -- Power off automatically if no longer connected to a switching station
154             technic.switching_station_timeout_count(pos, "LV")
155
156             local eu_input  = meta:get_int("LV_EU_input")
157             if eu_input == 0 then
158                -- Unpowered - go idle
159                --hacky_swap_node(pos, machine_node)
160                meta:set_string("infotext", machine_name.." Unpowered")
161                meta:set_int("LV_EU_supply", 0)
162                meta:set_int("MV_EU_supply", 0)
163
164                meta:set_int("LV_EU_demand", max_lv_demand)
165                meta:set_int("MV_EU_demand", 0)
166             else
167                -- LV side has got power to spare
168                meta:set_string("infotext", machine_name.." is active (LV:"..max_lv_demand.."->MV:"..eu_input/lv_mv_factor..")");
169                meta:set_int("MV_EU_supply", eu_input/lv_mv_factor)
170             end
171             ---------------------------------------------------
172
173          elseif upgrade == "HV-MV step down" and convert_HV_MV then
174             -- Register machine type
175             technic.register_MV_machine("technic:supply_converter","PR")
176             technic.register_HV_machine("technic:supply_converter","RE")
177
178             -- Power off automatically if no longer connected to a switching station
179             technic.switching_station_timeout_count(pos, "HV")
180
181             local eu_input  = meta:get_int("HV_EU_input")
182             if eu_input == 0 then
183                -- Unpowered - go idle
184                --hacky_swap_node(pos, machine_node)
185                meta:set_string("infotext", machine_name.." Unpowered")
186                meta:set_int("MV_EU_supply", 0)
187                meta:set_int("HV_EU_supply", 0)
188
189                meta:set_int("MV_EU_demand", 0)
190                meta:set_int("HV_EU_demand", max_hv_demand)
191             else
192                -- HV side has got power to spare
193                meta:set_string("infotext", machine_name.." is active (HV:"..max_hv_demand.."->MV:"..eu_input*hv_mv_factor..")");
194                meta:set_int("MV_EU_supply", eu_input*hv_mv_factor)
195             end
196             ---------------------------------------------------
197          elseif upgrade == "MV-HV step up"   and convert_MV_HV then
198             -- Register machine type
199             technic.register_MV_machine("technic:supply_converter","RE")
200             technic.register_HV_machine("technic:supply_converter","PR")
201
202             -- Power off automatically if no longer connected to a switching station
203             technic.switching_station_timeout_count(pos, "MV")
204
205             local eu_input  = meta:get_int("MV_EU_input")
206             if eu_input == 0 then
207                -- Unpowered - go idle
208                --hacky_swap_node(pos, machine_node)
209                meta:set_string("infotext", machine_name.." Unpowered")
210                meta:set_int("MV_EU_supply", 0)
211                meta:set_int("HV_EU_supply", 0)
212
213                meta:set_int("MV_EU_demand", max_mv_demand)
214                meta:set_int("HV_EU_demand", 0)
215             else
216                -- MV side has got power to spare
217                meta:set_string("infotext", machine_name.." is active (MV:"..max_mv_demand.."->HV:"..eu_input/mv_hv_factor..")");
218                meta:set_int("HV_EU_supply", eu_input/mv_hv_factor)
219             end
220             ---------------------------------------------------
221          end
222     end,
223 })