ShadowNinja
2013-07-11 5d470cd753efe8f4640099165a7bfc0c6e181c35
commit | author | age
5d799e 1 -- MV alloy furnace
R 2
3 minetest.register_craft({
4     output = 'technic:mv_alloy_furnace',
5     recipe = {
6         {'technic:stainless_steel_ingot', 'technic:alloy_furnace', 'technic:stainless_steel_ingot'},
7         {'pipeworks:tube_000000', 'technic:mv_transformer', 'pipeworks:tube_000000'},
8         {'technic:stainless_steel_ingot', 'technic:mv_cable', 'technic:stainless_steel_ingot'},
9     }
10 })
11
ee5c6c 12 local mv_alloy_furnace_formspec =
5d799e 13     "invsize[8,10;]"..
R 14     "label[0,0;MV Alloy Furnace]"..
ee5c6c 15         "list[current_name;src;3,1;1,2;]"..
5d799e 16     "list[current_name;dst;5,1;2,2;]"..
R 17     "list[current_player;main;0,6;8,4;]"..
18     "list[current_name;upgrade1;1,4;1,1;]"..
19     "list[current_name;upgrade2;2,4;1,1;]"..
20     "label[1,5;Upgrade Slots]"
21     
ee5c6c 22 minetest.register_node(
K 23    "technic:mv_alloy_furnace",
24    {description = "MV Alloy Furnace",
25     tiles = {"technic_mv_alloy_furnace_top.png", "technic_mv_alloy_furnace_bottom.png", "technic_mv_alloy_furnace_side_tube.png",
26          "technic_mv_alloy_furnace_side_tube.png", "technic_mv_alloy_furnace_side.png", "technic_mv_alloy_furnace_front.png"},
27     paramtype2 = "facedir",
28     groups = {cracky=2, tubedevice=1,tubedevice_receiver=1},
29     tube={insert_object=function(pos,node,stack,direction)
30                local meta=minetest.env:get_meta(pos)
31                local inv=meta:get_inventory()
32                return inv:add_item("src",stack)
33             end,
34       can_insert=function(pos,node,stack,direction)
5d799e 35             local meta=minetest.env:get_meta(pos)
R 36             local inv=meta:get_inventory()
37             return inv:room_for_item("src",stack)
ee5c6c 38              end,
K 39        },
40     legacy_facedir_simple = true,
41     sounds = default.node_sound_stone_defaults(),
42     on_construct = function(pos)
43               local meta = minetest.env:get_meta(pos)
44               meta:set_string("infotext", "MV Alloy furnace")
45               meta:set_float("technic_mv_power_machine", 1)
46               meta:set_int("tube_time",  0)
47               meta:set_string("formspec", mv_alloy_furnace_formspec)
48               local inv = meta:get_inventory()
49               inv:set_size("src", 2)
50               inv:set_size("dst", 4)
51               inv:set_size("upgrade1", 1)
52               inv:set_size("upgrade2", 1)
53            end,
54     can_dig = function(pos,player)
55          local meta = minetest.env:get_meta(pos);
56          local inv = meta:get_inventory()
57          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
58             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
59             return false
60          else
61             return true
62          end
63           end,
5d799e 64 })
R 65
ee5c6c 66 minetest.register_node(
K 67    "technic:mv_alloy_furnace_active",
68    {description = "MV Alloy Furnace",
69     tiles = {"technic_mv_alloy_furnace_top.png", "technic_mv_alloy_furnace_bottom.png", "technic_mv_alloy_furnace_side_tube.png",
70          "technic_mv_alloy_furnace_side_tube.png", "technic_mv_alloy_furnace_side.png", "technic_mv_alloy_furnace_front_active.png"},
71     paramtype2 = "facedir",
72     light_source = 8,
73     drop = "technic:mv_alloy_furnace",
74     groups = {cracky=2, tubedevice=1,tubedevice_receiver=1,not_in_creative_inventory=1},
75     tube={insert_object=function(pos,node,stack,direction)
76                local meta=minetest.env:get_meta(pos)
77                local inv=meta:get_inventory()
78                return inv:add_item("src",stack)
79             end,
80       can_insert=function(pos,node,stack,direction)
5d799e 81             local meta=minetest.env:get_meta(pos)
R 82             local inv=meta:get_inventory()
83             return inv:room_for_item("src",stack)
ee5c6c 84              end,
K 85        },
86     legacy_facedir_simple = true,
87     sounds = default.node_sound_stone_defaults(),
88     can_dig = function(pos,player)
89          local meta = minetest.env:get_meta(pos);
90          local inv = meta:get_inventory()
91          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
92             minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty");
93             return false
94          else
95             return true
96          end
97           end,
98     -- These three makes sure upgrades are not moved in or out while the furnace is active.
99     allow_metadata_inventory_put = function(pos, listname, index, stack, player)
100                        if listname == "src" or listname == "dst" then
101                       return 99
102                        else
103                       return 0 -- Disallow the move
104                        end
105                    end,
106     allow_metadata_inventory_take = 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_move = function(pos, from_list, to_list, to_list, to_index, count, player)
114                     return 0
115                  end,
5d799e 116 })
R 117
ee5c6c 118 local send_cooked_items = function(pos,x_velocity,z_velocity)
K 119                  -- Send items on their way in the pipe system.
120                  local meta=minetest.env:get_meta(pos) 
121                  local inv = meta:get_inventory()
122                  local i=0
123                  for _,stack in ipairs(inv:get_list("dst")) do
124                 i=i+1
125                 if stack then
126                    local item0=stack:to_table()
127                 if item0 then 
128                    item0["count"]="1"
129                    local item1=tube_item({x=pos.x,y=pos.y,z=pos.z},item0)
130                    item1:get_luaentity().start_pos = {x=pos.x,y=pos.y,z=pos.z}
131                    item1:setvelocity({x=x_velocity, y=0, z=z_velocity})
132                    item1:setacceleration({x=0, y=0, z=0})
133                    stack:take_item(1);
134                    inv:set_stack("dst", i, stack)
135                    return
136                 end
137                  end
138               end
139                end
5d799e 140
ee5c6c 141 local smelt_item = function(pos)
K 142               local meta=minetest.env:get_meta(pos) 
143               local inv = meta:get_inventory()
144               meta:set_int("src_time", meta:get_int("src_time") + 3) -- Cooking time 3x faster
145               local result = minetest.get_craft_result({method = "cooking", width = 1, items = inv:get_list("src")})
146               dst_stack={}
147               dst_stack["name"]=alloy_recipes[dst_index].dst_name
148               dst_stack["count"]=alloy_recipes[dst_index].dst_count
5d799e 149
ee5c6c 150               if result and result.item and meta:get_int("src_time") >= result.time then
K 151              meta:set_int("src_time", 0)
152              -- check if there's room for output in "dst" list
153              if inv:room_for_item("dst",result) then
154                 -- take stuff from "src" list
155                 srcstack = inv:get_stack("src", 1)
156                 srcstack:take_item()
157                 inv:set_stack("src", 1, srcstack)
158                 -- Put result in "dst" list
159                 inv:add_item("dst", result.item)
160                 return 1
161              else
162                 return 0 -- done
163              end
164               end
165               return 0 -- done
166            end
5d799e 167
ee5c6c 168 minetest.register_abm(
K 169    {nodenames = {"technic:mv_alloy_furnace","technic:mv_alloy_furnace_active"},
170     interval = 1,
171     chance = 1,
5d799e 172
ee5c6c 173     action = function(pos, node, active_object_count, active_object_count_wider)
K 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
5d799e 178
ee5c6c 179         -- Machine information
K 180         local machine_name         = "MV Alloy Furnace"
181         local machine_node         = "technic:mv_alloy_furnace"
182         local machine_state_demand = { 50, 2000, 1500, 1000 }
5d799e 183         
ee5c6c 184         -- Setup meta data if it does not exist. state is used as an indicator of this
K 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
190         end
191         
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 --
5d799e 197
ee5c6c 198         -- State machine
K 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)
5d799e 260
ee5c6c 261            -- The machine shuts down if we have nothing to smelt and no tube is connected
K 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
266            end
267            ----------------------
268            local empty  = 1
269            local recipe = nil
270            local result = nil
5d799e 271
ee5c6c 272            -- Get what to cook if anything
K 273            local srcstack  = inv:get_stack("src", 1)
274            local src2stack = inv:get_stack("src", 2)
275            local src_item1 = nil
276            local src_item2 = nil
277            if srcstack and src2stack then
278               src_item1 = srcstack:to_table()
279               src_item2 = src2stack:to_table()
280               empty     = 0
281            end
282            
283            if src_item1 and src_item2 then
284               recipe = technic.get_alloy_recipe(src_item1,src_item2)
285            end
286            if recipe then
287               result = { name=recipe.dst_name, count=recipe.dst_count}
288            end
289            
290            if state == 1 then
291               hacky_swap_node(pos, machine_node)
292               meta:set_string("infotext", machine_name.." Idle")
293               
294               local meta=minetest.env:get_meta(pos) 
295               local inv = meta:get_inventory()
296               if not inv:is_empty("src") then
297              if empty == 0 and recipe and inv:room_for_item("dst", result) then
298                 meta:set_string("infotext", machine_name.." Active")
299                 meta:set_int("src_time",     0)
300                 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)
301              end
302               end
303               
304            elseif state == 2 or state == 3 or state == 4 then
305               hacky_swap_node(pos, machine_node.."_active")
306               meta:set_int("src_time", meta:get_int("src_time") + 1)
307               if meta:get_int("src_time") == 4 then -- 4 ticks per output
308              meta:set_string("src_time", 0)
309              -- check if there's room for output in "dst" list and that we have the materials
310              if recipe and inv:room_for_item("dst", result) then
311                 -- Take stuff from "src" list
312                 srcstack:take_item(recipe.src1_count)
313                 inv:set_stack("src", 1, srcstack)
314                 src2stack:take_item(recipe.src2_count)
315                 inv:set_stack("src2", 1, src2stack)
316                 -- Put result in "dst" list
317                 inv:add_item("dst",result)
318              else
319                 next_state = 1
320              end
321               end
322            end
323         end
324         -- Change state?
325         if next_state ~= state then
326            meta:set_int("MV_EU_demand", machine_state_demand[next_state])
327            meta:set_int("state", next_state)
5d799e 328         end
R 329
330
331
ee5c6c 332          ------------------------------------
5d799e 333
ee5c6c 334 --         local pos1={}
K 335 --         pos1.x=pos.x
336 --         pos1.y=pos.y
337 --         pos1.z=pos.z
338 --         local x_velocity=0
339 --         local z_velocity=0
340 --
341 --         -- output is on the left side of the furnace
342 --         if node.param2==3 then pos1.z=pos1.z-1 z_velocity =-1 end
343 --         if node.param2==2 then pos1.x=pos1.x-1 x_velocity =-1 end
344 --         if node.param2==1 then pos1.z=pos1.z+1 z_velocity = 1 end
345 --         if node.param2==0 then pos1.x=pos1.x+1 x_velocity = 1 end
346 --
347 --         local output_tube_connected = false
348 --         local meta=minetest.env:get_meta(pos1) 
349 --         if meta:get_int("tubelike")==1 then output_tube_connected=true end
350 --         meta = minetest.env:get_meta(pos)
351 --         local inv = meta:get_inventory()
352 --         local upg_item1
353 --         local upg_item1_name=""
354 --         local upg_item2
355 --         local upg_item2_name=""
356 --         local srcstack = inv:get_stack("upgrade1", 1)
357 --         if srcstack then upg_item1=srcstack:to_table() end
358 --         srcstack = inv:get_stack("upgrade2", 1)
359 --         if srcstack then upg_item2=srcstack:to_table() end
360 --         if upg_item1 then upg_item1_name=upg_item1.name end
361 --         if upg_item2 then upg_item2_name=upg_item2.name end
362 --
363 --         local speed=0
364 --         if upg_item1_name=="technic:control_logic_unit" then speed=speed+1 end
365 --         if upg_item2_name=="technic:control_logic_unit" then speed=speed+1 end
366 --         tube_time=meta:get_float("tube_time")
367 --         tube_time=tube_time+speed
368 --         if tube_time>3 then 
369 --        tube_time=0
370 --        if output_tube_connected then send_cooked_items(pos,x_velocity,z_velocity) end
371 --         end
372 --         meta:set_float("tube_time", tube_time)
373 --
374 --         local extra_buffer_size = 0
375 --         if upg_item1_name=="technic:battery" then extra_buffer_size =extra_buffer_size + 10000 end
376 --         if upg_item2_name=="technic:battery" then extra_buffer_size =extra_buffer_size + 10000 end
377 --         local internal_EU_buffer_size=2000+extra_buffer_size
378 --         meta:set_float("internal_EU_buffer_size",internal_EU_buffer_size)
379 --
380 --         internal_EU_buffer=meta:get_float("internal_EU_buffer")
381 --         if internal_EU_buffer > internal_EU_buffer_size then internal_EU_buffer = internal_EU_buffer_size end
382 --         local meta = minetest.env:get_meta(pos)
383 --         local load = math.floor(internal_EU_buffer/internal_EU_buffer_size * 100)
384 --         meta:set_string("formspec",
385 --                 MV_alloy_furnace_formspec..
386 --                "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:"..
387 --                (load)..":technic_power_meter_fg.png]")
388 --
389 --         local inv = meta:get_inventory()
390 --
391 --         local furnace_is_cookin = meta:get_int("furnace_is_cookin")
392 --
393 --         local srclist = inv:get_list("src")
394 --         local srclist2 = inv:get_list("src2")
395 --         
396 --         srcstack = inv:get_stack("src", 1)
397 --         if srcstack then src_item1=srcstack:to_table() end
398 --         srcstack = inv:get_stack("src", 2)
399 --         if srcstack then src_item2=srcstack:to_table() end
400 --         dst_index=nil
401 --
402 --         if src_item1 and src_item2 then 
403 --        dst_index=get_cook_result(src_item1,src_item2) 
404 --         end
405 --
406 --
407 --         if (furnace_is_cookin == 1) then
408 --        if internal_EU_buffer>=150 then
409 --           internal_EU_buffer=internal_EU_buffer-150;
410 --           meta:set_float("internal_EU_buffer",internal_EU_buffer)
411 --           meta:set_float("src_time", meta:get_float("src_time") + 1)
412 --           if dst_index and meta:get_float("src_time") >= 4 then
413 --              -- check if there's room for output in "dst" list
414 --              dst_stack={}
415 --              dst_stack["name"]=alloy_recipes[dst_index].dst_name
416 --              dst_stack["count"]=alloy_recipes[dst_index].dst_count
417 --              if inv:room_for_item("dst",dst_stack) then
418 --             -- Put result in "dst" list
419 --             inv:add_item("dst",dst_stack)
420 --             -- take stuff from "src" list
421 --             for i=1,alloy_recipes[dst_index].src1_count,1 do
422 --                srcstack = inv:get_stack("src", 1)
423 --                srcstack:take_item()
424 --                inv:set_stack("src", 1, srcstack)
425 --             end
426 --             for i=1,alloy_recipes[dst_index].src2_count,1 do
427 --                srcstack = inv:get_stack("src", 2)
428 --                srcstack:take_item()
429 --                inv:set_stack("src", 2, srcstack)
430 --             end
431 --
432 --              else
433 --             print("Furnace inventory full!")
434 --              end
435 --              meta:set_string("src_time", 0)
436 --           end
437 --        end
438 --         end
439 --
440 --         if dst_index and meta:get_int("furnace_is_cookin")==0 then
441 --        hacky_swap_node(pos,"technic:mv_alloy_furnace_active")
442 --        meta:set_string("infotext","MV Alloy Furnace active")
443 --        meta:set_int("furnace_is_cookin",1)
444 --        meta:set_string("src_time", 0)
445 --        return
446 --         end
447 --
448 --         if meta:get_int("furnace_is_cookin")==0 or dst_index==nil then
449 --        hacky_swap_node(pos,"technic:mv_alloy_furnace")
450 --        meta:set_string("infotext","MV Alloy Furnace inactive")
451 --        meta:set_int("furnace_is_cookin",0)
452 --        meta:set_string("src_time", 0)
453 --         end
454 --
455       end,
456  })
612349 457
ee5c6c 458 technic.register_MV_machine ("technic:mv_alloy_furnace","RE")
K 459 technic.register_MV_machine ("technic:mv_alloy_furnace_active","RE")