kpoppel
2013-08-27 ec9f92d98b88b13adada6250622b9a51f3705026
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",
9770be 17       tiles  = {"technic_supply_converter_top.png", "technic_supply_converter_bottom.png", "technic_supply_converter_side.png",
K 18         "technic_supply_converter_side.png", "technic_supply_converter_side.png", "technic_supply_converter_side.png"},
ee5c6c 19       groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
K 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
9770be 61          local max_lv_demand = 2000 -- The increment size power supply tier. Determines how many are needed
ee5c6c 62          local max_mv_demand = 2000 -- -""-
K 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")
85d937 100             meta:set_int("LV_EU_demand", 0)
K 101             meta:set_int("LV_EU_supply", 0)
102             meta:set_int("LV_EU_input",  0)
103             meta:set_int("MV_EU_demand", 0)
104             meta:set_int("MV_EU_supply", 0)
105             meta:set_int("MV_EU_input",  0)
106             meta:set_int("HV_EU_demand", 0)
107             meta:set_int("HV_EU_supply", 0)
108             meta:set_int("HV_EU_input",  0)
ee5c6c 109             return
K 110          end
111
112          -- The node is programmed with an upgrade slot
113          -- containing a MV-LV step down, LV-MV step up, HV-MV step down or MV-HV step up unit
114
115          if upgrade == "" then
116             meta:set_string("infotext", machine_name.." has an empty converter slot");
117             technic.unregister_LV_machine("technic:supply_converter")
118             technic.unregister_MV_machine("technic:supply_converter")
119             technic.unregister_HV_machine("technic:supply_converter")
120             meta:set_int("LV_EU_demand", 0)
121             meta:set_int("LV_EU_supply", 0)
122             meta:set_int("LV_EU_input",  0)
123             meta:set_int("MV_EU_demand", 0)
124             meta:set_int("MV_EU_supply", 0)
125             meta:set_int("MV_EU_input",  0)
126             meta:set_int("HV_EU_demand", 0)
127             meta:set_int("HV_EU_supply", 0)
128             meta:set_int("HV_EU_input",  0)
129             return
130          end
131
132          -- State machine
133          if upgrade == "MV-LV step down" and convert_MV_LV then
134             -- Register machine type
135             technic.register_LV_machine("technic:supply_converter","PR")
136             technic.register_MV_machine("technic:supply_converter","RE")
137
138             -- Power off automatically if no longer connected to a switching station
139             technic.switching_station_timeout_count(pos, "MV")
140
141             local eu_input  = meta:get_int("MV_EU_input")
142             if eu_input == 0 then
143                -- Unpowered - go idle
144                --hacky_swap_node(pos, machine_node)
145                meta:set_string("infotext", machine_name.." Unpowered")
146                meta:set_int("LV_EU_supply", 0)
147                meta:set_int("MV_EU_supply", 0)
148
149                meta:set_int("LV_EU_demand", 0)
150                meta:set_int("MV_EU_demand", max_mv_demand)
151             else
152                -- MV side has got power to spare
153                meta:set_string("infotext", machine_name.." is active (MV:"..max_mv_demand.."->LV:"..eu_input*mv_lv_factor..")");
154                meta:set_int("LV_EU_supply", eu_input*mv_lv_factor)
155             end
156             ---------------------------------------------------
157          elseif upgrade == "LV-MV step up"   and convert_LV_MV then
158             -- Register machine type
159             technic.register_LV_machine("technic:supply_converter","RE")
160             technic.register_MV_machine("technic:supply_converter","PR")
161
162             -- Power off automatically if no longer connected to a switching station
163             technic.switching_station_timeout_count(pos, "LV")
164
165             local eu_input  = meta:get_int("LV_EU_input")
166             if eu_input == 0 then
167                -- Unpowered - go idle
168                --hacky_swap_node(pos, machine_node)
169                meta:set_string("infotext", machine_name.." Unpowered")
170                meta:set_int("LV_EU_supply", 0)
171                meta:set_int("MV_EU_supply", 0)
172
173                meta:set_int("LV_EU_demand", max_lv_demand)
174                meta:set_int("MV_EU_demand", 0)
175             else
176                -- LV side has got power to spare
177                meta:set_string("infotext", machine_name.." is active (LV:"..max_lv_demand.."->MV:"..eu_input/lv_mv_factor..")");
178                meta:set_int("MV_EU_supply", eu_input/lv_mv_factor)
179             end
180             ---------------------------------------------------
181
182          elseif upgrade == "HV-MV step down" and convert_HV_MV then
183             -- Register machine type
184             technic.register_MV_machine("technic:supply_converter","PR")
185             technic.register_HV_machine("technic:supply_converter","RE")
186
187             -- Power off automatically if no longer connected to a switching station
188             technic.switching_station_timeout_count(pos, "HV")
189
190             local eu_input  = meta:get_int("HV_EU_input")
191             if eu_input == 0 then
192                -- Unpowered - go idle
193                --hacky_swap_node(pos, machine_node)
194                meta:set_string("infotext", machine_name.." Unpowered")
195                meta:set_int("MV_EU_supply", 0)
196                meta:set_int("HV_EU_supply", 0)
197
198                meta:set_int("MV_EU_demand", 0)
199                meta:set_int("HV_EU_demand", max_hv_demand)
200             else
201                -- HV side has got power to spare
202                meta:set_string("infotext", machine_name.." is active (HV:"..max_hv_demand.."->MV:"..eu_input*hv_mv_factor..")");
203                meta:set_int("MV_EU_supply", eu_input*hv_mv_factor)
204             end
205             ---------------------------------------------------
206          elseif upgrade == "MV-HV step up"   and convert_MV_HV then
207             -- Register machine type
208             technic.register_MV_machine("technic:supply_converter","RE")
209             technic.register_HV_machine("technic:supply_converter","PR")
210
211             -- Power off automatically if no longer connected to a switching station
212             technic.switching_station_timeout_count(pos, "MV")
213
214             local eu_input  = meta:get_int("MV_EU_input")
215             if eu_input == 0 then
216                -- Unpowered - go idle
217                --hacky_swap_node(pos, machine_node)
218                meta:set_string("infotext", machine_name.." Unpowered")
219                meta:set_int("MV_EU_supply", 0)
220                meta:set_int("HV_EU_supply", 0)
221
222                meta:set_int("MV_EU_demand", max_mv_demand)
223                meta:set_int("HV_EU_demand", 0)
224             else
225                -- MV side has got power to spare
226                meta:set_string("infotext", machine_name.." is active (MV:"..max_mv_demand.."->HV:"..eu_input/mv_hv_factor..")");
227                meta:set_int("HV_EU_supply", eu_input/mv_hv_factor)
228             end
229             ---------------------------------------------------
230          end
231     end,
232 })