falling.lua 5.7 KB

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