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