2
0

falling.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. if core.registered_nodes[self.node.name] then
  99. core.add_node(np, self.node)
  100. if self.meta then
  101. local meta = core.get_meta(np)
  102. meta:from_table(self.meta)
  103. end
  104. end
  105. self.object:remove()
  106. core.check_for_falling(np)
  107. return
  108. end
  109. local vel = self.object:getvelocity()
  110. if vector.equals(vel, {x = 0, y = 0, z = 0}) then
  111. local npos = self.object:getpos()
  112. self.object:setpos(vector.round(npos))
  113. end
  114. end
  115. })
  116. local function spawn_falling_node(p, node, meta)
  117. local obj = core.add_entity(p, "__builtin:falling_node")
  118. if obj then
  119. obj:get_luaentity():set_node(node, meta)
  120. end
  121. end
  122. function core.spawn_falling_node(pos)
  123. local node = core.get_node(pos)
  124. if node.name == "air" or node.name == "ignore" then
  125. return false
  126. end
  127. local obj = core.add_entity(pos, "__builtin:falling_node")
  128. if obj then
  129. obj:get_luaentity():set_node(node)
  130. core.remove_node(pos)
  131. return true
  132. end
  133. return false
  134. end
  135. local function drop_attached_node(p)
  136. local n = core.get_node(p)
  137. core.remove_node(p)
  138. for _, item in pairs(core.get_node_drops(n, "")) do
  139. local pos = {
  140. x = p.x + math.random()/2 - 0.25,
  141. y = p.y + math.random()/2 - 0.25,
  142. z = p.z + math.random()/2 - 0.25,
  143. }
  144. core.add_item(pos, item)
  145. end
  146. end
  147. function builtin_shared.check_attached_node(p, n)
  148. local def = core.registered_nodes[n.name]
  149. local d = {x = 0, y = 0, z = 0}
  150. if def.paramtype2 == "wallmounted" or
  151. def.paramtype2 == "colorwallmounted" then
  152. -- The fallback vector here is in case 'wallmounted to dir' is nil due
  153. -- to voxelmanip placing a wallmounted node without resetting a
  154. -- pre-existing param2 value that is out-of-range for wallmounted.
  155. -- The fallback vector corresponds to param2 = 0.
  156. d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
  157. else
  158. d.y = -1
  159. end
  160. local p2 = vector.add(p, d)
  161. local nn = core.get_node(p2).name
  162. local def2 = core.registered_nodes[nn]
  163. if def2 and not def2.walkable then
  164. return false
  165. end
  166. return true
  167. end
  168. --
  169. -- Some common functions
  170. --
  171. function core.check_single_for_falling(p)
  172. local n = core.get_node(p)
  173. if core.get_item_group(n.name, "falling_node") ~= 0 then
  174. local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
  175. -- Only spawn falling node if node below is loaded
  176. local n_bottom = core.get_node_or_nil(p_bottom)
  177. local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
  178. if d_bottom and
  179. (core.get_item_group(n.name, "float") == 0 or
  180. d_bottom.liquidtype == "none") and
  181. (n.name ~= n_bottom.name or (d_bottom.leveled and
  182. core.get_node_level(p_bottom) <
  183. core.get_node_max_level(p_bottom))) and
  184. (not d_bottom.walkable or d_bottom.buildable_to) then
  185. n.level = core.get_node_level(p)
  186. local meta = core.get_meta(p)
  187. local metatable = {}
  188. if meta ~= nil then
  189. metatable = meta:to_table()
  190. end
  191. core.remove_node(p)
  192. spawn_falling_node(p, n, metatable)
  193. return true
  194. end
  195. end
  196. if core.get_item_group(n.name, "attached_node") ~= 0 then
  197. if not builtin_shared.check_attached_node(p, n) then
  198. drop_attached_node(p)
  199. return true
  200. end
  201. end
  202. return false
  203. end
  204. -- This table is specifically ordered.
  205. -- We don't walk diagonals, only our direct neighbors, and self.
  206. -- Down first as likely case, but always before self. The same with sides.
  207. -- Up must come last, so that things above self will also fall all at once.
  208. local check_for_falling_neighbors = {
  209. {x = -1, y = -1, z = 0},
  210. {x = 1, y = -1, z = 0},
  211. {x = 0, y = -1, z = -1},
  212. {x = 0, y = -1, z = 1},
  213. {x = 0, y = -1, z = 0},
  214. {x = -1, y = 0, z = 0},
  215. {x = 1, y = 0, z = 0},
  216. {x = 0, y = 0, z = 1},
  217. {x = 0, y = 0, z = -1},
  218. {x = 0, y = 0, z = 0},
  219. {x = 0, y = 1, z = 0},
  220. }
  221. function core.check_for_falling(p)
  222. -- Round p to prevent falling entities to get stuck.
  223. p = vector.round(p)
  224. -- We make a stack, and manually maintain size for performance.
  225. -- Stored in the stack, we will maintain tables with pos, and
  226. -- last neighbor visited. This way, when we get back to each
  227. -- node, we know which directions we have already walked, and
  228. -- which direction is the next to walk.
  229. local s = {}
  230. local n = 0
  231. -- The neighbor order we will visit from our table.
  232. local v = 1
  233. while true do
  234. -- Push current pos onto the stack.
  235. n = n + 1
  236. s[n] = {p = p, v = v}
  237. -- Select next node from neighbor list.
  238. p = vector.add(p, check_for_falling_neighbors[v])
  239. -- Now we check out the node. If it is in need of an update,
  240. -- it will let us know in the return value (true = updated).
  241. if not core.check_single_for_falling(p) then
  242. -- If we don't need to "recurse" (walk) to it then pop
  243. -- our previous pos off the stack and continue from there,
  244. -- with the v value we were at when we last were at that
  245. -- node
  246. repeat
  247. local pop = s[n]
  248. p = pop.p
  249. v = pop.v
  250. s[n] = nil
  251. n = n - 1
  252. -- If there's nothing left on the stack, and no
  253. -- more sides to walk to, we're done and can exit
  254. if n == 0 and v == 11 then
  255. return
  256. end
  257. until v < 11
  258. -- The next round walk the next neighbor in list.
  259. v = v + 1
  260. else
  261. -- If we did need to walk the neighbor, then
  262. -- start walking it from the walk order start (1),
  263. -- and not the order we just pushed up the stack.
  264. v = 1
  265. end
  266. end
  267. end
  268. --
  269. -- Global callbacks
  270. --
  271. local function on_placenode(p, node)
  272. core.check_for_falling(p)
  273. end
  274. core.register_on_placenode(on_placenode)
  275. local function on_dignode(p, node)
  276. core.check_for_falling(p)
  277. end
  278. core.register_on_dignode(on_dignode)
  279. local function on_punchnode(p, node)
  280. core.check_for_falling(p)
  281. end
  282. core.register_on_punchnode(on_punchnode)
  283. --
  284. -- Globally exported functions
  285. --
  286. -- TODO remove this function after the 0.4.15 release
  287. function nodeupdate(p)
  288. core.log("deprecated", "nodeupdate: deprecated, please use core.check_for_falling instead")
  289. core.check_for_falling(p)
  290. end
  291. -- TODO remove this function after the 0.4.15 release
  292. function nodeupdate_single(p)
  293. core.log("deprecated", "nodeupdate_single: deprecated, please use core.check_single_for_falling instead")
  294. core.check_single_for_falling(p)
  295. end