belt.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. minetest.register_node("factory:belt", {
  2. description = "Conveyor Belt",
  3. tiles = {{name="factory_belt_top_animation.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=0.4}}, "factory_belt_bottom.png", "factory_belt_side.png",
  4. "factory_belt_side.png", "factory_belt_side.png", "factory_belt_side.png"},
  5. groups = {cracky=1},
  6. drawtype = "nodebox",
  7. paramtype = "light",
  8. paramtype2 = "facedir",
  9. is_ground_content = true,
  10. legacy_facedir_simple = true,
  11. node_box = {
  12. type = "fixed",
  13. fixed = {{-0.5,-0.5,-0.5,0.5,0.0625,0.5},}
  14. },
  15. })
  16. minetest.register_abm({
  17. nodenames = {"factory:belt"},
  18. neighbors = nil,
  19. interval = 1,
  20. chance = 1,
  21. action = function(pos, node, active_object_count, active_object_count_wider)
  22. local all_objects = minetest.get_objects_inside_radius(pos, 0.75)
  23. local _,obj
  24. for _,obj in ipairs(all_objects) do
  25. if not obj:is_player() and obj:get_luaentity() and obj:get_luaentity().name == "__builtin:item" then
  26. factory.do_moving_item({x = pos.x, y = pos.y + 0.15, z = pos.z}, obj:get_luaentity().itemstring)
  27. obj:get_luaentity().itemstring = ""
  28. obj:remove()
  29. end
  30. end
  31. end,
  32. })
  33. -- Based off of the pipeworks item code
  34. function factory.do_moving_item(pos, item)
  35. local stack = ItemStack(item)
  36. local obj = minetest.add_entity(pos, "factory:moving_item")
  37. obj:get_luaentity():set_item(stack:to_string())
  38. return obj
  39. end
  40. minetest.register_entity("factory:moving_item", {
  41. initial_properties = {
  42. hp_max = 1,
  43. physical = false,
  44. collisionbox = {0.125, 0.125, 0.125, 0.125, 0.125, 0.125},
  45. visual = "wielditem",
  46. visual_size = {x = 0.2, y = 0.2},
  47. textures = {""},
  48. spritediv = {x = 1, y = 1},
  49. initial_sprite_basepos = {x = 0, y = 0},
  50. is_visible = false,
  51. },
  52. physical_state = true,
  53. itemstring = '',
  54. set_item = function(self, itemstring)
  55. self.itemstring = itemstring
  56. local stack = ItemStack(itemstring)
  57. local count = stack:get_count()
  58. local max_count = stack:get_stack_max()
  59. if count > max_count then
  60. count = max_count
  61. self.itemstring = stack:get_name().." "..max_count
  62. end
  63. local s = 0.15 + 0.15 * (count / max_count)
  64. local c = 0.8 * s
  65. local itemtable = stack:to_table()
  66. local itemname = nil
  67. if itemtable then
  68. itemname = stack:to_table().name
  69. end
  70. local item_texture = nil
  71. local item_type = ""
  72. if core.registered_items[itemname] then
  73. item_texture = core.registered_items[itemname].inventory_image
  74. item_type = core.registered_items[itemname].type
  75. end
  76. prop = {
  77. is_visible = true,
  78. visual = "wielditem",
  79. textures = {itemname},
  80. visual_size = {x = s, y = s},
  81. collisionbox = {-c, -c, -c, c, c, c},
  82. automatic_rotate = math.pi * 0.2,
  83. }
  84. self.object:set_properties(prop)
  85. end,
  86. get_staticdata = function(self)
  87. return core.serialize({
  88. itemstring = self.itemstring
  89. })
  90. end,
  91. on_activate = function(self, staticdata, dtime_s)
  92. if string.sub(staticdata, 1, string.len("return")) == "return" then
  93. local data = core.deserialize(staticdata)
  94. if data and type(data) == "table" then
  95. self.itemstring = data.itemstring
  96. end
  97. else
  98. self.itemstring = staticdata
  99. end
  100. self.object:set_armor_groups({immortal = 1})
  101. self:set_item(self.itemstring)
  102. end,
  103. on_step = function(self, dtime)
  104. local pos = self.object:getpos()
  105. local stack = ItemStack(self.itemstring)
  106. local napos = minetest.get_node(pos)
  107. local veldir = self.object:getvelocity();
  108. if napos.name == "factory:belt" then
  109. local speed = 0.8
  110. local dir = minetest.facedir_to_dir(napos.param2)
  111. self.object:setvelocity({x = dir.x / speed, y = 0, z = dir.z / speed})
  112. else
  113. minetest.item_drop(stack, nil, {x = pos.x + veldir.x / 3, y = pos.y, z = pos.z + veldir.z / 3})
  114. self.object:remove()
  115. end
  116. end
  117. })