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