falling.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. -- Minetest: builtin/item.lua
  2. --
  3. -- Falling stuff
  4. --
  5. minetest.register_entity("__builtin:falling_node", {
  6. initial_properties = {
  7. physical = true,
  8. collide_with_objects = false,
  9. collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
  10. visual = "wielditem",
  11. textures = {},
  12. visual_size = {x=0.667, y=0.667},
  13. },
  14. node = {},
  15. set_node = function(self, node)
  16. self.node = node
  17. local stack = ItemStack(node.name)
  18. local itemtable = stack:to_table()
  19. local itemname = nil
  20. if itemtable then
  21. itemname = stack:to_table().name
  22. end
  23. local item_texture = nil
  24. local item_type = ""
  25. if minetest.registered_items[itemname] then
  26. item_texture = minetest.registered_items[itemname].inventory_image
  27. item_type = minetest.registered_items[itemname].type
  28. end
  29. prop = {
  30. is_visible = true,
  31. textures = {node.name},
  32. }
  33. self.object:set_properties(prop)
  34. end,
  35. get_staticdata = function(self)
  36. return self.node.name
  37. end,
  38. on_activate = function(self, staticdata)
  39. self.object:set_armor_groups({immortal=1})
  40. --self.object:setacceleration({x=0, y=-10, z=0})
  41. self:set_node({name=staticdata})
  42. end,
  43. on_step = function(self, dtime)
  44. -- Set gravity
  45. self.object:setacceleration({x=0, y=-10, z=0})
  46. -- Turn to actual sand when collides to ground or just move
  47. local pos = self.object:getpos()
  48. local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
  49. local bcn = minetest.get_node(bcp)
  50. local bcd = minetest.registered_nodes[bcn.name]
  51. -- Note: walkable is in the node definition, not in item groups
  52. if not bcd or
  53. (bcd.walkable or
  54. (minetest.get_node_group(self.node.name, "float") ~= 0 and
  55. bcd.liquidtype ~= "none")) then
  56. if bcd and bcd.leveled and
  57. bcn.name == self.node.name then
  58. local addlevel = self.node.level
  59. if addlevel == nil or addlevel <= 0 then
  60. addlevel = bcd.leveled
  61. end
  62. if minetest.add_node_level(bcp, addlevel) == 0 then
  63. self.object:remove()
  64. return
  65. end
  66. elseif bcd and bcd.buildable_to and
  67. (minetest.get_node_group(self.node.name, "float") == 0 or
  68. bcd.liquidtype == "none") then
  69. minetest.remove_node(bcp)
  70. return
  71. end
  72. local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
  73. -- Check what's here
  74. local n2 = minetest.get_node(np)
  75. -- If it's not air or liquid, remove node and replace it with
  76. -- it's drops
  77. if n2.name ~= "air" and (not minetest.registered_nodes[n2.name] or
  78. minetest.registered_nodes[n2.name].liquidtype == "none") then
  79. local drops = minetest.get_node_drops(n2.name, "")
  80. minetest.remove_node(np)
  81. -- Add dropped items
  82. local _, dropped_item
  83. for _, dropped_item in ipairs(drops) do
  84. minetest.add_item(np, dropped_item)
  85. end
  86. -- Run script hook
  87. local _, callback
  88. for _, callback in ipairs(minetest.registered_on_dignodes) do
  89. callback(np, n2, nil)
  90. end
  91. end
  92. -- Create node and remove entity
  93. minetest.add_node(np, self.node)
  94. self.object:remove()
  95. nodeupdate(np)
  96. else
  97. -- Do nothing
  98. end
  99. end
  100. })
  101. function spawn_falling_node(p, node)
  102. obj = minetest.add_entity(p, "__builtin:falling_node")
  103. obj:get_luaentity():set_node(node)
  104. end
  105. function drop_attached_node(p)
  106. local nn = minetest.get_node(p).name
  107. minetest.remove_node(p)
  108. for _,item in ipairs(minetest.get_node_drops(nn, "")) do
  109. local pos = {
  110. x = p.x + math.random()/2 - 0.25,
  111. y = p.y + math.random()/2 - 0.25,
  112. z = p.z + math.random()/2 - 0.25,
  113. }
  114. minetest.add_item(pos, item)
  115. end
  116. end
  117. function check_attached_node(p, n)
  118. local def = minetest.registered_nodes[n.name]
  119. local d = {x=0, y=0, z=0}
  120. if def.paramtype2 == "wallmounted" then
  121. if n.param2 == 0 then
  122. d.y = 1
  123. elseif n.param2 == 1 then
  124. d.y = -1
  125. elseif n.param2 == 2 then
  126. d.x = 1
  127. elseif n.param2 == 3 then
  128. d.x = -1
  129. elseif n.param2 == 4 then
  130. d.z = 1
  131. elseif n.param2 == 5 then
  132. d.z = -1
  133. end
  134. else
  135. d.y = -1
  136. end
  137. local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
  138. local nn = minetest.get_node(p2).name
  139. local def2 = minetest.registered_nodes[nn]
  140. if def2 and not def2.walkable then
  141. return false
  142. end
  143. return true
  144. end
  145. --
  146. -- Some common functions
  147. --
  148. function nodeupdate_single(p, delay)
  149. n = minetest.get_node(p)
  150. if minetest.get_node_group(n.name, "falling_node") ~= 0 then
  151. p_bottom = {x=p.x, y=p.y-1, z=p.z}
  152. n_bottom = minetest.get_node(p_bottom)
  153. -- Note: walkable is in the node definition, not in item groups
  154. if minetest.registered_nodes[n_bottom.name] and
  155. (minetest.get_node_group(n.name, "float") == 0 or minetest.registered_nodes[n_bottom.name].liquidtype == "none") and
  156. (n.name ~= n_bottom.name or (minetest.registered_nodes[n_bottom.name].leveled and minetest.env:get_node_level(p_bottom) < minetest.env:get_node_max_level(p_bottom))) and
  157. (not minetest.registered_nodes[n_bottom.name].walkable or
  158. minetest.registered_nodes[n_bottom.name].buildable_to) then
  159. if delay then
  160. minetest.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false)
  161. else
  162. n.level = minetest.env:get_node_level(p)
  163. minetest.remove_node(p)
  164. spawn_falling_node(p, n)
  165. nodeupdate(p)
  166. end
  167. end
  168. end
  169. if minetest.get_node_group(n.name, "attached_node") ~= 0 then
  170. if not check_attached_node(p, n) then
  171. drop_attached_node(p)
  172. nodeupdate(p)
  173. end
  174. end
  175. end
  176. function nodeupdate(p, delay)
  177. -- Round p to prevent falling entities to get stuck
  178. p.x = math.floor(p.x+0.5)
  179. p.y = math.floor(p.y+0.5)
  180. p.z = math.floor(p.z+0.5)
  181. for x = -1,1 do
  182. for y = -1,1 do
  183. for z = -1,1 do
  184. nodeupdate_single({x=p.x+x, y=p.y+y, z=p.z+z}, delay or not (x==0 and y==0 and z==0))
  185. end
  186. end
  187. end
  188. end
  189. --
  190. -- Global callbacks
  191. --
  192. function on_placenode(p, node)
  193. nodeupdate(p)
  194. end
  195. minetest.register_on_placenode(on_placenode)
  196. function on_dignode(p, node)
  197. nodeupdate(p)
  198. end
  199. minetest.register_on_dignode(on_dignode)