ShadowNinja
2013-07-11 5d470cd753efe8f4640099165a7bfc0c6e181c35
commit | author | age
ee5c6c 1 -- MV Electric Furnace
K 2 -- This is a faster version of the stone furnace which runs on EUs
3 -- In addition to this it can be upgraded with microcontrollers and batteries
4 -- This new version uses the batteries to lower the power consumption of the machine
5 -- Also in addition this furnace can be attached to the pipe system from the pipeworks mod.
061e6b 6
ee5c6c 7 -- FIXME: kpoppel I'd like to introduce an induction heating element here also
K 8 minetest.register_craft(
9    {output = 'technic:mv_electric_furnace',
10     recipe = {
11        {'technic:stainless_steel_ingot', 'technic:electric_furnace', 'technic:stainless_steel_ingot'},
12        {'pipeworks:tube_000000', 'technic:mv_transformer', 'pipeworks:tube_000000'},
13        {'technic:stainless_steel_ingot', 'technic:mv_cable', 'technic:stainless_steel_ingot'},
14     }
15  })
061e6b 16
ee5c6c 17 local mv_electric_furnace_formspec =
K 18    "invsize[8,10;]"..
19    "list[current_name;src;3,1;1,1;]"..
20    "list[current_name;dst;5,1;2,2;]"..
21    "list[current_player;main;0,6;8,4;]"..
22    "label[0,0;MV Electric Furnace]"..
23    "list[current_name;upgrade1;1,4;1,1;]"..
24    "list[current_name;upgrade2;2,4;1,1;]"..
25    "label[1,5;Upgrade Slots]"
b55bae 26
ee5c6c 27 minetest.register_node(
K 28    "technic:mv_electric_furnace",
29    {description = "MV Electric furnace",
30     tiles = {"technic_mv_electric_furnace_top.png", "technic_mv_electric_furnace_bottom.png", "technic_mv_electric_furnace_side_tube.png",
31          "technic_mv_electric_furnace_side_tube.png", "technic_mv_electric_furnace_side.png", "technic_mv_electric_furnace_front.png"},
32     paramtype2 = "facedir",
33     groups = {cracky=2, tubedevice=1,tubedevice_receiver=1,},
34     tube={insert_object=function(pos,node,stack,direction)
35                local meta=minetest.env:get_meta(pos)
36                local inv=meta:get_inventory()
37                return inv:add_item("src",stack)
38             end,
39       can_insert=function(pos,node,stack,direction)
061e6b 40             local meta=minetest.env:get_meta(pos)
R 41             local inv=meta:get_inventory()
42             return inv:room_for_item("src",stack)
ee5c6c 43              end,
K 44        },
45     legacy_facedir_simple = true,
46     sounds = default.node_sound_stone_defaults(),
47     on_construct = function(pos)
48               local meta = minetest.env:get_meta(pos)
49               meta:set_string("infotext", "MV Electric furnace")
50               meta:set_float("technic_mv_power_machine", 1)
51               meta:set_int("tube_time",  0)
52               meta:set_string("formspec", mv_electric_furnace_formspec)
53               local inv = meta:get_inventory()
54               inv:set_size("src", 1)
55               inv:set_size("dst", 4)
56               inv:set_size("upgrade1", 1)
57               inv:set_size("upgrade2", 1)
58            end,
59     can_dig = function(pos,player)
60          local meta = minetest.env:get_meta(pos);
61          local inv = meta:get_inventory()
62          if not inv:is_empty("src") or not inv:is_empty("dst") or not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
63             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
64             return false
65          else
66             return true
67          end
68           end,
69  })
061e6b 70
ee5c6c 71 minetest.register_node(
K 72    "technic:mv_electric_furnace_active",
73    {description = "MV Electric Furnace",
74     tiles = {"technic_mv_electric_furnace_top.png", "technic_mv_electric_furnace_bottom.png", "technic_mv_electric_furnace_side_tube.png",
75          "technic_mv_electric_furnace_side_tube.png", "technic_mv_electric_furnace_side.png", "technic_mv_electric_furnace_front_active.png"},
76     paramtype2 = "facedir",
77     light_source = 8,
78     drop = "technic:mv_electric_furnace",
79     groups = {cracky=2, tubedevice=1,tubedevice_receiver=1,not_in_creative_inventory=1},
80     tube={insert_object=function(pos,node,stack,direction)
81                local meta=minetest.env:get_meta(pos)
82                local inv=meta:get_inventory()
83                return inv:add_item("src",stack)
84             end,
85       can_insert=function(pos,node,stack,direction)
061e6b 86             local meta=minetest.env:get_meta(pos)
R 87             local inv=meta:get_inventory()
88             return inv:room_for_item("src",stack)
ee5c6c 89              end,
K 90        },
91     legacy_facedir_simple = true,
92     sounds = default.node_sound_stone_defaults(),
93     can_dig = function(pos,player)
94          local meta = minetest.env:get_meta(pos);
95          local inv = meta:get_inventory()
96          if not inv:is_empty("src") or not inv:is_empty("dst") or not inv:is_empty("upgrade1") or not inv:is_empty("upgrade2") then
97             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
98             return false
99          else
100             return true
101          end
102           end,
103     -- These three makes sure upgrades are not moved in or out while the furnace is active.
104     allow_metadata_inventory_put = function(pos, listname, index, stack, player)
105                        if listname == "src" or listname == "dst" then
106                       return 99
107                        else
108                       return 0 -- Disallow the move
109                        end
110                    end,
111     allow_metadata_inventory_take = function(pos, listname, index, stack, player)
112                        if listname == "src" or listname == "dst" then
113                       return 99
114                        else
115                       return 0 -- Disallow the move
116                        end
117                     end,
118     allow_metadata_inventory_move = function(pos, from_list, to_list, to_list, to_index, count, player)
119                     return 0
120                  end,
121  })
061e6b 122
ee5c6c 123 local send_cooked_items = function(pos,x_velocity,z_velocity)
K 124                  -- Send items on their way in the pipe system.
125                  local meta=minetest.env:get_meta(pos) 
126                  local inv = meta:get_inventory()
127                  local i=0
128                  for _,stack in ipairs(inv:get_list("dst")) do
129                 i=i+1
130                 if stack then
131                    local item0=stack:to_table()
132                 if item0 then 
133                    item0["count"]="1"
134                    local item1=tube_item({x=pos.x,y=pos.y,z=pos.z},item0)
135                    item1:get_luaentity().start_pos = {x=pos.x,y=pos.y,z=pos.z}
136                    item1:setvelocity({x=x_velocity, y=0, z=z_velocity})
137                    item1:setacceleration({x=0, y=0, z=0})
138                    stack:take_item(1);
139                    inv:set_stack("dst", i, stack)
140                    return
061e6b 141                 end
ee5c6c 142                  end
K 143               end
144                end
145
146 local smelt_item = function(pos)
147               local meta=minetest.env:get_meta(pos) 
148               local inv = meta:get_inventory()
149               meta:set_int("src_time", meta:get_int("src_time") + 3) -- Cooking time 3x faster
150               local result = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
151               if result and result.item and meta:get_int("src_time") >= result.time then
152              meta:set_int("src_time", 0)
153              -- check if there's room for output in "dst" list
154              if inv:room_for_item("dst",result) then
155                 -- take stuff from "src" list
156                 srcstack = inv:get_stack("src", 1)
157                 srcstack:take_item()
158                 inv:set_stack("src", 1, srcstack)
159                 -- Put result in "dst" list
160                 inv:add_item("dst", result.item)
161                 return 1
162              else
163                 return 0 -- done
164              end
165               end
166               return 0 -- done
167            end
168
169 minetest.register_abm(
170    {nodenames = {"technic:mv_electric_furnace","technic:mv_electric_furnace_active"},
171     interval = 1,
172     chance   = 1,
173     action = function(pos, node, active_object_count, active_object_count_wider)
174         local meta         = minetest.env:get_meta(pos)
175         local eu_input     = meta:get_int("MV_EU_input")
176         local state        = meta:get_int("state")
177         local next_state   = state
178
179         -- Machine information
180         local machine_name         = "MV Electric Furnace"
181         local machine_node         = "technic:mv_electric_furnace"
182         local machine_state_demand = { 50, 2000, 1500, 1000 }
183              
184         -- Setup meta data if it does not exist. state is used as an indicator of this
185         if state == 0 then
186            meta:set_int("state", 1)
187            meta:set_int("MV_EU_demand", machine_state_demand[1])
188            meta:set_int("MV_EU_input", 0)
189            return
061e6b 190         end
ee5c6c 191              
K 192         -- Power off automatically if no longer connected to a switching station
193         technic.switching_station_timeout_count(pos, "MV")
194         
195         -- Execute always logic
196         -- CODE HERE --
197         
198         -- State machine
199         if eu_input == 0 then
200            -- Unpowered - go idle
201            hacky_swap_node(pos, machine_node)
202            meta:set_string("infotext", machine_name.." Unpowered")
203            next_state = 1
204         elseif eu_input == machine_state_demand[state] then
205            -- Powered - do the state specific actions
206                 
207            -- Execute always if powered logic
208            local meta=minetest.env:get_meta(pos) 
209               
210            -- Get the names of the upgrades
211            local meta=minetest.env:get_meta(pos) 
212            local inv = meta:get_inventory()
213            local upg_item1
214            local upg_item1_name=""
215            local upg_item2
216            local upg_item2_name=""
217            local srcstack = inv:get_stack("upgrade1", 1)
218            if  srcstack then upg_item1=srcstack:to_table() end
219            srcstack = inv:get_stack("upgrade2", 1)
220            if  srcstack then upg_item2=srcstack:to_table() end
221            if upg_item1 then upg_item1_name=upg_item1.name end
222            if upg_item2 then upg_item2_name=upg_item2.name end
223            
224            -- Save some power by installing battery upgrades. Fully upgraded makes this
225            -- furnace use the same amount of power as the LV version
226            local EU_saving_upgrade = 0
227            if upg_item1_name=="technic:battery" then EU_saving_upgrade = EU_saving_upgrade + 1 end
228            if upg_item2_name=="technic:battery" then EU_saving_upgrade = EU_saving_upgrade + 1 end
229            
230            -- Tube loading speed can be upgraded using control logic units
231            local tube_speed_upgrade = 0
232            if upg_item1_name=="technic:control_logic_unit" then tube_speed_upgrade = tube_speed_upgrade + 1 end
233            if upg_item2_name=="technic:control_logic_unit" then tube_speed_upgrade = tube_speed_upgrade + 1 end
234            
235            -- Handle pipeworks (consumes tube_speed_upgrade)
236            local pos1={x=pos.x, y=pos.y, z=pos.z}
237            local x_velocity=0
238            local z_velocity=0
239            
240            -- Output is on the left side of the furnace
241            if node.param2==3 then pos1.z=pos1.z-1 z_velocity =-1 end
242            if node.param2==2 then pos1.x=pos1.x-1 x_velocity =-1 end
243            if node.param2==1 then pos1.z=pos1.z+1 z_velocity = 1 end
244            if node.param2==0 then pos1.x=pos1.x+1 x_velocity = 1 end
245            
246            local output_tube_connected = false
247            local meta1 = minetest.env:get_meta(pos1) 
248            if meta1:get_int("tubelike") == 1 then
249               output_tube_connected=true
250            end
251            tube_time = meta:get_int("tube_time")
252            tube_time = tube_time + tube_speed_upgrade
253            if tube_time > 3 then
254               tube_time = 0
255               if output_tube_connected then
256              send_cooked_items(pos,x_velocity,z_velocity)
257               end
258            end
259            meta:set_int("tube_time", tube_time)
260            
261            -- The machine shuts down if we have nothing to smelt and no tube is connected
262            -- or if we have nothing to send with a tube connected.
263            if    (not output_tube_connected and inv:is_empty("src"))
264            or (    output_tube_connected and inv:is_empty("dst")) then
265            next_state = 1
061e6b 266         end
ee5c6c 267         ----------------------
K 268         
269         if state == 1 then
270            hacky_swap_node(pos, machine_node)
271            meta:set_string("infotext", machine_name.." Idle")
272            
273            local meta=minetest.env:get_meta(pos) 
274            local inv = meta:get_inventory()
275            if not inv:is_empty("src") then
276               local result = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
277               if result then
278              meta:set_string("infotext", machine_name.." Active")
279              meta:set_int("src_time",     0)
280              next_state = 2+EU_saving_upgrade -- Next state is decided by the battery upgrade (state 2= 0 batteries, state 3 = 1 battery, 4 = 2 batteries)
281               end
282            else
283               meta:set_string("infotext", "Electric Furnace Idle")
284            end
285            
286         elseif state == 2 or state == 3 or state == 4 then
287            hacky_swap_node(pos, machine_node.."_active")
288            meta:set_string("infotext", machine_name.." Active")
289            result = smelt_item(pos, data)
290            if result == 0 then
291               next_state = 1
292            end
b55bae 293         end
ee5c6c 294          end
K 295          -- Change state?
296          if next_state ~= state then
297         meta:set_int("MV_EU_demand", machine_state_demand[next_state])
298         meta:set_int("state", next_state)
299          end
300       end,
301  })
612349 302
ee5c6c 303 technic.register_MV_machine ("technic:mv_electric_furnace","RE")
K 304 technic.register_MV_machine ("technic:mv_electric_furnace_active","RE")