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