falling.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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:get_acceleration()
  47. if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
  48. self.object:set_acceleration({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:get_pos()
  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:get_velocity()
  119. if vector.equals(vel, {x = 0, y = 0, z = 0}) then
  120. local npos = self.object:get_pos()
  121. self.object:set_pos(vector.round(npos))
  122. end
  123. end
  124. })
  125. local function convert_to_falling_node(pos, node)
  126. local obj = core.add_entity(pos, "__builtin:falling_node")
  127. if not obj then
  128. return false
  129. end
  130. node.level = core.get_node_level(pos)
  131. local meta = core.get_meta(pos)
  132. local metatable = meta and meta:to_table() or {}
  133. obj:get_luaentity():set_node(node, metatable)
  134. core.remove_node(pos)
  135. return true
  136. end
  137. function core.spawn_falling_node(pos)
  138. local node = core.get_node(pos)
  139. if node.name == "air" or node.name == "ignore" then
  140. return false
  141. end
  142. return convert_to_falling_node(pos, node)
  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. convert_to_falling_node(p, n)
  209. return true
  210. end
  211. end
  212. if core.get_item_group(n.name, "attached_node") ~= 0 then
  213. if not builtin_shared.check_attached_node(p, n) then
  214. drop_attached_node(p)
  215. return true
  216. end
  217. end
  218. return false
  219. end
  220. -- This table is specifically ordered.
  221. -- We don't walk diagonals, only our direct neighbors, and self.
  222. -- Down first as likely case, but always before self. The same with sides.
  223. -- Up must come last, so that things above self will also fall all at once.
  224. local check_for_falling_neighbors = {
  225. {x = -1, y = -1, z = 0},
  226. {x = 1, y = -1, z = 0},
  227. {x = 0, y = -1, z = -1},
  228. {x = 0, y = -1, z = 1},
  229. {x = 0, y = -1, z = 0},
  230. {x = -1, y = 0, z = 0},
  231. {x = 1, y = 0, z = 0},
  232. {x = 0, y = 0, z = 1},
  233. {x = 0, y = 0, z = -1},
  234. {x = 0, y = 0, z = 0},
  235. {x = 0, y = 1, z = 0},
  236. }
  237. function core.check_for_falling(p)
  238. -- Round p to prevent falling entities to get stuck.
  239. p = vector.round(p)
  240. -- We make a stack, and manually maintain size for performance.
  241. -- Stored in the stack, we will maintain tables with pos, and
  242. -- last neighbor visited. This way, when we get back to each
  243. -- node, we know which directions we have already walked, and
  244. -- which direction is the next to walk.
  245. local s = {}
  246. local n = 0
  247. -- The neighbor order we will visit from our table.
  248. local v = 1
  249. while true do
  250. -- Push current pos onto the stack.
  251. n = n + 1
  252. s[n] = {p = p, v = v}
  253. -- Select next node from neighbor list.
  254. p = vector.add(p, check_for_falling_neighbors[v])
  255. -- Now we check out the node. If it is in need of an update,
  256. -- it will let us know in the return value (true = updated).
  257. if not core.check_single_for_falling(p) then
  258. -- If we don't need to "recurse" (walk) to it then pop
  259. -- our previous pos off the stack and continue from there,
  260. -- with the v value we were at when we last were at that
  261. -- node
  262. repeat
  263. local pop = s[n]
  264. p = pop.p
  265. v = pop.v
  266. s[n] = nil
  267. n = n - 1
  268. -- If there's nothing left on the stack, and no
  269. -- more sides to walk to, we're done and can exit
  270. if n == 0 and v == 11 then
  271. return
  272. end
  273. until v < 11
  274. -- The next round walk the next neighbor in list.
  275. v = v + 1
  276. else
  277. -- If we did need to walk the neighbor, then
  278. -- start walking it from the walk order start (1),
  279. -- and not the order we just pushed up the stack.
  280. v = 1
  281. end
  282. end
  283. end
  284. --
  285. -- Global callbacks
  286. --
  287. local function on_placenode(p, node)
  288. core.check_for_falling(p)
  289. end
  290. core.register_on_placenode(on_placenode)
  291. local function on_dignode(p, node)
  292. core.check_for_falling(p)
  293. end
  294. core.register_on_dignode(on_dignode)
  295. local function on_punchnode(p, node)
  296. core.check_for_falling(p)
  297. end
  298. core.register_on_punchnode(on_punchnode)