falling.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. -- Minetest: builtin/item.lua
  2. local builtin_shared = ...
  3. --
  4. -- Falling stuff
  5. --
  6. core.register_entity(":__builtin:falling_node", {
  7. initial_properties = {
  8. visual = "wielditem",
  9. visual_size = {x = 0.667, y = 0.667},
  10. textures = {},
  11. physical = true,
  12. is_visible = false,
  13. collide_with_objects = false,
  14. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  15. },
  16. node = {},
  17. meta = {},
  18. set_node = function(self, node, meta)
  19. self.node = node
  20. self.meta = meta or {}
  21. self.object:set_properties({
  22. is_visible = true,
  23. textures = {node.name},
  24. })
  25. end,
  26. get_staticdata = function(self)
  27. local ds = {
  28. node = self.node,
  29. meta = self.meta,
  30. }
  31. return core.serialize(ds)
  32. end,
  33. on_activate = function(self, staticdata)
  34. self.object:set_armor_groups({immortal = 1})
  35. local ds = core.deserialize(staticdata)
  36. if ds and ds.node then
  37. self:set_node(ds.node, ds.meta)
  38. elseif ds then
  39. self:set_node(ds)
  40. elseif staticdata ~= "" then
  41. self:set_node({name = staticdata})
  42. end
  43. end,
  44. on_step = function(self, dtime)
  45. -- Set gravity
  46. local acceleration = self.object:getacceleration()
  47. if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
  48. self.object:setacceleration({x = 0, y = -10, z = 0})
  49. end
  50. -- Turn to actual node when colliding with ground, or continue to move
  51. local pos = self.object:getpos()
  52. -- Position of bottom center point
  53. local bcp = {x = pos.x, y = pos.y - 0.7, z = pos.z}
  54. -- Avoid bugs caused by an unloaded node below
  55. local bcn = core.get_node_or_nil(bcp)
  56. local bcd = bcn and core.registered_nodes[bcn.name]
  57. if bcn and
  58. (not bcd or bcd.walkable or
  59. (core.get_item_group(self.node.name, "float") ~= 0 and
  60. bcd.liquidtype ~= "none")) then
  61. if bcd and bcd.leveled and
  62. bcn.name == self.node.name then
  63. local addlevel = self.node.level
  64. if not addlevel or addlevel <= 0 then
  65. addlevel = bcd.leveled
  66. end
  67. if core.add_node_level(bcp, addlevel) == 0 then
  68. self.object:remove()
  69. return
  70. end
  71. elseif bcd and bcd.buildable_to and
  72. (core.get_item_group(self.node.name, "float") == 0 or
  73. bcd.liquidtype == "none") then
  74. core.remove_node(bcp)
  75. return
  76. end
  77. local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
  78. -- Check what's here
  79. local n2 = core.get_node(np)
  80. local nd = core.registered_nodes[n2.name]
  81. -- If it's not air or liquid, remove node and replace it with
  82. -- it's drops
  83. if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
  84. core.remove_node(np)
  85. if nd and nd.buildable_to == false then
  86. -- Add dropped items
  87. local drops = core.get_node_drops(n2, "")
  88. for _, dropped_item in pairs(drops) do
  89. core.add_item(np, dropped_item)
  90. end
  91. end
  92. -- Run script hook
  93. for _, callback in pairs(core.registered_on_dignodes) do
  94. callback(np, n2)
  95. end
  96. end
  97. -- Create node and remove entity
  98. local def = core.registered_nodes[self.node.name]
  99. if def then
  100. core.add_node(np, self.node)
  101. if self.meta then
  102. local meta = core.get_meta(np)
  103. meta:from_table(self.meta)
  104. end
  105. if def.sounds and def.sounds.place and def.sounds.place.name then
  106. core.sound_play(def.sounds.place, {pos = np})
  107. end
  108. end
  109. self.object:remove()
  110. core.check_for_falling(np)
  111. return
  112. end
  113. local vel = self.object:getvelocity()
  114. if vector.equals(vel, {x = 0, y = 0, z = 0}) then
  115. local npos = self.object:getpos()
  116. self.object:setpos(vector.round(npos))
  117. end
  118. end
  119. })
  120. local function spawn_falling_node(p, node, meta)
  121. local obj = core.add_entity(p, "__builtin:falling_node")
  122. if obj then
  123. obj:get_luaentity():set_node(node, meta)
  124. end
  125. end
  126. function core.spawn_falling_node(pos)
  127. local node = core.get_node(pos)
  128. if node.name == "air" or node.name == "ignore" then
  129. return false
  130. end
  131. local obj = core.add_entity(pos, "__builtin:falling_node")
  132. if obj then
  133. obj:get_luaentity():set_node(node)
  134. core.remove_node(pos)
  135. return true
  136. end
  137. return false
  138. end
  139. local function drop_attached_node(p)
  140. local n = core.get_node(p)
  141. core.remove_node(p)
  142. for _, item in pairs(core.get_node_drops(n, "")) do
  143. local pos = {
  144. x = p.x + math.random()/2 - 0.25,
  145. y = p.y + math.random()/2 - 0.25,
  146. z = p.z + math.random()/2 - 0.25,
  147. }
  148. core.add_item(pos, item)
  149. end
  150. end
  151. function builtin_shared.check_attached_node(p, n)
  152. local def = core.registered_nodes[n.name]
  153. local d = {x = 0, y = 0, z = 0}
  154. if def.paramtype2 == "wallmounted" or
  155. def.paramtype2 == "colorwallmounted" then
  156. -- The fallback vector here is in case 'wallmounted to dir' is nil due
  157. -- to voxelmanip placing a wallmounted node without resetting a
  158. -- pre-existing param2 value that is out-of-range for wallmounted.
  159. -- The fallback vector corresponds to param2 = 0.
  160. d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
  161. else
  162. d.y = -1
  163. end
  164. local p2 = vector.add(p, d)
  165. local nn = core.get_node(p2).name
  166. local def2 = core.registered_nodes[nn]
  167. if def2 and not def2.walkable then
  168. return false
  169. end
  170. return true
  171. end
  172. --
  173. -- Some common functions
  174. --
  175. function core.check_single_for_falling(p)
  176. local n = core.get_node(p)
  177. if core.get_item_group(n.name, "falling_node") ~= 0 then
  178. local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
  179. -- Only spawn falling node if node below is loaded
  180. local n_bottom = core.get_node_or_nil(p_bottom)
  181. local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
  182. if d_bottom and
  183. (core.get_item_group(n.name, "float") == 0 or
  184. d_bottom.liquidtype == "none") and
  185. (n.name ~= n_bottom.name or (d_bottom.leveled and
  186. core.get_node_level(p_bottom) <
  187. core.get_node_max_level(p_bottom))) and
  188. (not d_bottom.walkable or d_bottom.buildable_to) then
  189. n.level = core.get_node_level(p)
  190. local meta = core.get_meta(p)
  191. local metatable = {}
  192. if meta ~= nil then
  193. metatable = meta:to_table()
  194. end
  195. core.remove_node(p)
  196. spawn_falling_node(p, n, metatable)
  197. return true
  198. end
  199. end
  200. if core.get_item_group(n.name, "attached_node") ~= 0 then
  201. if not builtin_shared.check_attached_node(p, n) then
  202. drop_attached_node(p)
  203. return true
  204. end
  205. end
  206. return false
  207. end
  208. -- This table is specifically ordered.
  209. -- We don't walk diagonals, only our direct neighbors, and self.
  210. -- Down first as likely case, but always before self. The same with sides.
  211. -- Up must come last, so that things above self will also fall all at once.
  212. local check_for_falling_neighbors = {
  213. {x = -1, y = -1, z = 0},
  214. {x = 1, y = -1, z = 0},
  215. {x = 0, y = -1, z = -1},
  216. {x = 0, y = -1, z = 1},
  217. {x = 0, y = -1, z = 0},
  218. {x = -1, y = 0, z = 0},
  219. {x = 1, y = 0, z = 0},
  220. {x = 0, y = 0, z = 1},
  221. {x = 0, y = 0, z = -1},
  222. {x = 0, y = 0, z = 0},
  223. {x = 0, y = 1, z = 0},
  224. }
  225. function core.check_for_falling(p)
  226. -- Round p to prevent falling entities to get stuck.
  227. p = vector.round(p)
  228. -- We make a stack, and manually maintain size for performance.
  229. -- Stored in the stack, we will maintain tables with pos, and
  230. -- last neighbor visited. This way, when we get back to each
  231. -- node, we know which directions we have already walked, and
  232. -- which direction is the next to walk.
  233. local s = {}
  234. local n = 0
  235. -- The neighbor order we will visit from our table.
  236. local v = 1
  237. while true do
  238. -- Push current pos onto the stack.
  239. n = n + 1
  240. s[n] = {p = p, v = v}
  241. -- Select next node from neighbor list.
  242. p = vector.add(p, check_for_falling_neighbors[v])
  243. -- Now we check out the node. If it is in need of an update,
  244. -- it will let us know in the return value (true = updated).
  245. if not core.check_single_for_falling(p) then
  246. -- If we don't need to "recurse" (walk) to it then pop
  247. -- our previous pos off the stack and continue from there,
  248. -- with the v value we were at when we last were at that
  249. -- node
  250. repeat
  251. local pop = s[n]
  252. p = pop.p
  253. v = pop.v
  254. s[n] = nil
  255. n = n - 1
  256. -- If there's nothing left on the stack, and no
  257. -- more sides to walk to, we're done and can exit
  258. if n == 0 and v == 11 then
  259. return
  260. end
  261. until v < 11
  262. -- The next round walk the next neighbor in list.
  263. v = v + 1
  264. else
  265. -- If we did need to walk the neighbor, then
  266. -- start walking it from the walk order start (1),
  267. -- and not the order we just pushed up the stack.
  268. v = 1
  269. end
  270. end
  271. end
  272. --
  273. -- Global callbacks
  274. --
  275. local function on_placenode(p, node)
  276. core.check_for_falling(p)
  277. end
  278. core.register_on_placenode(on_placenode)
  279. local function on_dignode(p, node)
  280. core.check_for_falling(p)
  281. end
  282. core.register_on_dignode(on_dignode)
  283. local function on_punchnode(p, node)
  284. core.check_for_falling(p)
  285. end
  286. core.register_on_punchnode(on_punchnode)