falling.lua 5.4 KB

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