2
0

falling.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. -- 'bcn' is nil for unloaded nodes
  55. local bcn = core.get_node_or_nil(bcp)
  56. -- Delete on contact with ignore at world edges
  57. if bcn and bcn.name == "ignore" then
  58. self.object:remove()
  59. return
  60. end
  61. local bcd = bcn and core.registered_nodes[bcn.name]
  62. if bcn and
  63. (not bcd or bcd.walkable or
  64. (core.get_item_group(self.node.name, "float") ~= 0 and
  65. bcd.liquidtype ~= "none")) then
  66. if bcd and bcd.leveled and
  67. bcn.name == self.node.name then
  68. local addlevel = self.node.level
  69. if not addlevel or addlevel <= 0 then
  70. addlevel = bcd.leveled
  71. end
  72. if core.add_node_level(bcp, addlevel) == 0 then
  73. self.object:remove()
  74. return
  75. end
  76. elseif bcd and bcd.buildable_to and
  77. (core.get_item_group(self.node.name, "float") == 0 or
  78. bcd.liquidtype == "none") then
  79. core.remove_node(bcp)
  80. return
  81. end
  82. local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
  83. -- Check what's here
  84. local n2 = core.get_node(np)
  85. local nd = core.registered_nodes[n2.name]
  86. -- If it's not air or liquid, remove node and replace it with
  87. -- it's drops
  88. if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
  89. core.remove_node(np)
  90. if nd and nd.buildable_to == false then
  91. -- Add dropped items
  92. local drops = core.get_node_drops(n2, "")
  93. for _, dropped_item in pairs(drops) do
  94. core.add_item(np, dropped_item)
  95. end
  96. end
  97. -- Run script hook
  98. for _, callback in pairs(core.registered_on_dignodes) do
  99. callback(np, n2)
  100. end
  101. end
  102. -- Create node and remove entity
  103. local def = core.registered_nodes[self.node.name]
  104. if def then
  105. core.add_node(np, self.node)
  106. if self.meta then
  107. local meta = core.get_meta(np)
  108. meta:from_table(self.meta)
  109. end
  110. if def.sounds and def.sounds.place and def.sounds.place.name then
  111. core.sound_play(def.sounds.place, {pos = np})
  112. end
  113. end
  114. self.object:remove()
  115. core.check_for_falling(np)
  116. return
  117. end
  118. local vel = self.object:getvelocity()
  119. if vector.equals(vel, {x = 0, y = 0, z = 0}) then
  120. local npos = self.object:getpos()
  121. self.object:setpos(vector.round(npos))
  122. end
  123. end
  124. })
  125. local function spawn_falling_node(p, node, meta)
  126. local obj = core.add_entity(p, "__builtin:falling_node")
  127. if obj then
  128. obj:get_luaentity():set_node(node, meta)
  129. end
  130. end
  131. function core.spawn_falling_node(pos)
  132. local node = core.get_node(pos)
  133. if node.name == "air" or node.name == "ignore" then
  134. return false
  135. end
  136. local obj = core.add_entity(pos, "__builtin:falling_node")
  137. if obj then
  138. obj:get_luaentity():set_node(node)
  139. core.remove_node(pos)
  140. return true
  141. end
  142. return false
  143. end
  144. local function drop_attached_node(p)
  145. local n = core.get_node(p)
  146. local drops = core.get_node_drops(n, "")
  147. local def = core.registered_items[n.name]
  148. if def and def.preserve_metadata then
  149. local oldmeta = core.get_meta(p):to_table().fields
  150. -- Copy pos and node because the callback can modify them.
  151. local pos_copy = {x=p.x, y=p.y, z=p.z}
  152. local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
  153. local drop_stacks = {}
  154. for k, v in pairs(drops) do
  155. drop_stacks[k] = ItemStack(v)
  156. end
  157. drops = drop_stacks
  158. def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
  159. end
  160. core.remove_node(p)
  161. for _, item in pairs(drops) do
  162. local pos = {
  163. x = p.x + math.random()/2 - 0.25,
  164. y = p.y + math.random()/2 - 0.25,
  165. z = p.z + math.random()/2 - 0.25,
  166. }
  167. core.add_item(pos, item)
  168. end
  169. end
  170. function builtin_shared.check_attached_node(p, n)
  171. local def = core.registered_nodes[n.name]
  172. local d = {x = 0, y = 0, z = 0}
  173. if def.paramtype2 == "wallmounted" or
  174. def.paramtype2 == "colorwallmounted" then
  175. -- The fallback vector here is in case 'wallmounted to dir' is nil due
  176. -- to voxelmanip placing a wallmounted node without resetting a
  177. -- pre-existing param2 value that is out-of-range for wallmounted.
  178. -- The fallback vector corresponds to param2 = 0.
  179. d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
  180. else
  181. d.y = -1
  182. end
  183. local p2 = vector.add(p, d)
  184. local nn = core.get_node(p2).name
  185. local def2 = core.registered_nodes[nn]
  186. if def2 and not def2.walkable then
  187. return false
  188. end
  189. return true
  190. end
  191. --
  192. -- Some common functions
  193. --
  194. function core.check_single_for_falling(p)
  195. local n = core.get_node(p)
  196. if core.get_item_group(n.name, "falling_node") ~= 0 then
  197. local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
  198. -- Only spawn falling node if node below is loaded
  199. local n_bottom = core.get_node_or_nil(p_bottom)
  200. local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
  201. if d_bottom and
  202. (core.get_item_group(n.name, "float") == 0 or
  203. d_bottom.liquidtype == "none") and
  204. (n.name ~= n_bottom.name or (d_bottom.leveled and
  205. core.get_node_level(p_bottom) <
  206. core.get_node_max_level(p_bottom))) and
  207. (not d_bottom.walkable or d_bottom.buildable_to) then
  208. n.level = core.get_node_level(p)
  209. local meta = core.get_meta(p)
  210. local metatable = {}
  211. if meta ~= nil then
  212. metatable = meta:to_table()
  213. end
  214. core.remove_node(p)
  215. spawn_falling_node(p, n, metatable)
  216. return true
  217. end
  218. end
  219. if core.get_item_group(n.name, "attached_node") ~= 0 then
  220. if not builtin_shared.check_attached_node(p, n) then
  221. drop_attached_node(p)
  222. return true
  223. end
  224. end
  225. return false
  226. end
  227. -- This table is specifically ordered.
  228. -- We don't walk diagonals, only our direct neighbors, and self.
  229. -- Down first as likely case, but always before self. The same with sides.
  230. -- Up must come last, so that things above self will also fall all at once.
  231. local check_for_falling_neighbors = {
  232. {x = -1, y = -1, z = 0},
  233. {x = 1, y = -1, z = 0},
  234. {x = 0, y = -1, z = -1},
  235. {x = 0, y = -1, z = 1},
  236. {x = 0, y = -1, z = 0},
  237. {x = -1, y = 0, z = 0},
  238. {x = 1, y = 0, z = 0},
  239. {x = 0, y = 0, z = 1},
  240. {x = 0, y = 0, z = -1},
  241. {x = 0, y = 0, z = 0},
  242. {x = 0, y = 1, z = 0},
  243. }
  244. function core.check_for_falling(p)
  245. -- Round p to prevent falling entities to get stuck.
  246. p = vector.round(p)
  247. -- We make a stack, and manually maintain size for performance.
  248. -- Stored in the stack, we will maintain tables with pos, and
  249. -- last neighbor visited. This way, when we get back to each
  250. -- node, we know which directions we have already walked, and
  251. -- which direction is the next to walk.
  252. local s = {}
  253. local n = 0
  254. -- The neighbor order we will visit from our table.
  255. local v = 1
  256. while true do
  257. -- Push current pos onto the stack.
  258. n = n + 1
  259. s[n] = {p = p, v = v}
  260. -- Select next node from neighbor list.
  261. p = vector.add(p, check_for_falling_neighbors[v])
  262. -- Now we check out the node. If it is in need of an update,
  263. -- it will let us know in the return value (true = updated).
  264. if not core.check_single_for_falling(p) then
  265. -- If we don't need to "recurse" (walk) to it then pop
  266. -- our previous pos off the stack and continue from there,
  267. -- with the v value we were at when we last were at that
  268. -- node
  269. repeat
  270. local pop = s[n]
  271. p = pop.p
  272. v = pop.v
  273. s[n] = nil
  274. n = n - 1
  275. -- If there's nothing left on the stack, and no
  276. -- more sides to walk to, we're done and can exit
  277. if n == 0 and v == 11 then
  278. return
  279. end
  280. until v < 11
  281. -- The next round walk the next neighbor in list.
  282. v = v + 1
  283. else
  284. -- If we did need to walk the neighbor, then
  285. -- start walking it from the walk order start (1),
  286. -- and not the order we just pushed up the stack.
  287. v = 1
  288. end
  289. end
  290. end
  291. --
  292. -- Global callbacks
  293. --
  294. local function on_placenode(p, node)
  295. core.check_for_falling(p)
  296. end
  297. core.register_on_placenode(on_placenode)
  298. local function on_dignode(p, node)
  299. core.check_for_falling(p)
  300. end
  301. core.register_on_dignode(on_dignode)
  302. local function on_punchnode(p, node)
  303. core.check_for_falling(p)
  304. end
  305. core.register_on_punchnode(on_punchnode)