swapper.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. factory.swapper_formspec =
  2. "size[8,9.5]"..
  3. factory_gui_bg..
  4. factory_gui_bg_img_2..
  5. factory_gui_slots..
  6. "list[current_name;left;0,1.25;2,4;]"..
  7. "list[current_name;right;6,1.25;2,4;]"..
  8. "list[current_name;loverflow;0,0;3,1;]"..
  9. "list[current_name;roverflow;5,0;3,1;]"..
  10. "list[current_name;overflow;3,1.25;2,1;]"..
  11. "list[current_name;input;3,3.25;2,2;]"..
  12. "image[3.5,0;1,1;factory_square_white.png]"..
  13. "image[2,1.25;1,1;factory_square_yellow.png]"..
  14. "image[5,1.25;1,1;factory_square_blue.png]"..
  15. "list[current_player;main;0,5.5;8,1;]"..
  16. "list[current_player;main;0,6.75;8,3;8]"
  17. minetest.register_node("factory:swapper", {
  18. description = "Swapper",
  19. tiles = {"factory_machine_steel_dark.png", "factory_machine_steel_dark.png", "factory_machine_steel_dark.png^factory_square_blue.png",
  20. "factory_machine_steel_dark.png^factory_square_yellow.png", "factory_machine_steel_dark.png^factory_square_white.png", "factory_machine_steel_dark.png^factory_square_red.png"},
  21. paramtype2 = "facedir",
  22. groups = {cracky=3},
  23. legacy_facedir_simple = true,
  24. is_ground_content = false,
  25. on_construct = function(pos)
  26. local meta = minetest.get_meta(pos)
  27. meta:set_string("formspec", factory.swapper_formspec)
  28. meta:set_string("infotext", "Swapper")
  29. local inv = meta:get_inventory()
  30. inv:set_size("left", 8)
  31. inv:set_size("right", 8)
  32. inv:set_size("loverflow", 3)
  33. inv:set_size("roverflow", 3)
  34. inv:set_size("overflow", 2)
  35. inv:set_size("input", 4)
  36. end,
  37. can_dig = function(pos,player)
  38. local meta = minetest.get_meta(pos);
  39. local inv = meta:get_inventory()
  40. if not inv:is_empty("left") then
  41. return false
  42. elseif not inv:is_empty("right") then
  43. return false
  44. elseif not inv:is_empty("loverflow") then
  45. return false
  46. elseif not inv:is_empty("roverflow") then
  47. return false
  48. elseif not inv:is_empty("overflow") then
  49. return false
  50. elseif not inv:is_empty("input") then
  51. return false
  52. end
  53. return true
  54. end
  55. })
  56. minetest.register_abm({
  57. nodenames = {"factory:swapper"},
  58. neighbors = nil,
  59. interval = 1,
  60. chance = 1,
  61. action = function(pos, node, active_object_count, active_object_count_wider)
  62. local meta = minetest.get_meta(pos)
  63. local inv = meta:get_inventory()
  64. if not inv:is_empty("input") then
  65. for i,item in ipairs(inv:get_list("input")) do
  66. if not item:is_empty() and item:get_name() ~= "" then
  67. local item_to_move = item:peek_item()
  68. for _,litem in ipairs(inv:get_list("left")) do
  69. if litem:get_name() == item:get_name() and inv:room_for_item("loverflow", item_to_move) then
  70. item:take_item()
  71. inv:set_stack("input", i, item)
  72. inv:add_item("loverflow", item_to_move)
  73. return
  74. end
  75. end
  76. for _,ritem in ipairs(inv:get_list("right")) do
  77. if ritem:get_name() == item:get_name() and inv:room_for_item("roverflow", item_to_move) then
  78. item:take_item()
  79. inv:set_stack("input", i, item)
  80. inv:add_item("roverflow", item_to_move)
  81. return
  82. end
  83. end
  84. if inv:room_for_item("overflow", item_to_move) then
  85. item:take_item()
  86. inv:set_stack("input", i, item)
  87. inv:add_item("overflow", item_to_move)
  88. return
  89. end
  90. end
  91. end
  92. end
  93. end
  94. })