falling.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. local builtin_shared = ...
  2. local SCALE = 0.667
  3. local facedir_to_euler = {
  4. {y = 0, x = 0, z = 0},
  5. {y = -math.pi/2, x = 0, z = 0},
  6. {y = math.pi, x = 0, z = 0},
  7. {y = math.pi/2, x = 0, z = 0},
  8. {y = math.pi/2, x = -math.pi/2, z = math.pi/2},
  9. {y = math.pi/2, x = math.pi, z = math.pi/2},
  10. {y = math.pi/2, x = math.pi/2, z = math.pi/2},
  11. {y = math.pi/2, x = 0, z = math.pi/2},
  12. {y = -math.pi/2, x = math.pi/2, z = math.pi/2},
  13. {y = -math.pi/2, x = 0, z = math.pi/2},
  14. {y = -math.pi/2, x = -math.pi/2, z = math.pi/2},
  15. {y = -math.pi/2, x = math.pi, z = math.pi/2},
  16. {y = 0, x = 0, z = math.pi/2},
  17. {y = 0, x = -math.pi/2, z = math.pi/2},
  18. {y = 0, x = math.pi, z = math.pi/2},
  19. {y = 0, x = math.pi/2, z = math.pi/2},
  20. {y = math.pi, x = math.pi, z = math.pi/2},
  21. {y = math.pi, x = math.pi/2, z = math.pi/2},
  22. {y = math.pi, x = 0, z = math.pi/2},
  23. {y = math.pi, x = -math.pi/2, z = math.pi/2},
  24. {y = math.pi, x = math.pi, z = 0},
  25. {y = -math.pi/2, x = math.pi, z = 0},
  26. {y = 0, x = math.pi, z = 0},
  27. {y = math.pi/2, x = math.pi, z = 0}
  28. }
  29. local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
  30. --
  31. -- Falling stuff
  32. --
  33. core.register_entity(":__builtin:falling_node", {
  34. initial_properties = {
  35. visual = "item",
  36. visual_size = vector.new(SCALE, SCALE, SCALE),
  37. textures = {},
  38. physical = true,
  39. is_visible = false,
  40. collide_with_objects = true,
  41. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  42. },
  43. node = {},
  44. meta = {},
  45. floats = false,
  46. set_node = function(self, node, meta)
  47. node.param2 = node.param2 or 0
  48. self.node = node
  49. meta = meta or {}
  50. if type(meta.to_table) == "function" then
  51. meta = meta:to_table()
  52. end
  53. for _, list in pairs(meta.inventory or {}) do
  54. for i, stack in pairs(list) do
  55. if type(stack) == "userdata" then
  56. list[i] = stack:to_string()
  57. end
  58. end
  59. end
  60. local def = core.registered_nodes[node.name]
  61. if not def then
  62. -- Don't allow unknown nodes to fall
  63. core.log("info",
  64. "Unknown falling node removed at "..
  65. core.pos_to_string(self.object:get_pos()))
  66. self.object:remove()
  67. return
  68. end
  69. self.meta = meta
  70. -- Cache whether we're supposed to float on water
  71. self.floats = core.get_item_group(node.name, "float") ~= 0
  72. -- Save liquidtype for falling water
  73. self.liquidtype = def.liquidtype
  74. -- Set entity visuals
  75. if def.drawtype == "torchlike" or def.drawtype == "signlike" then
  76. local textures
  77. if def.tiles and def.tiles[1] then
  78. local tile = def.tiles[1]
  79. if type(tile) == "table" then
  80. tile = tile.name
  81. end
  82. if def.drawtype == "torchlike" then
  83. textures = { "("..tile..")^[transformFX", tile }
  84. else
  85. textures = { tile, "("..tile..")^[transformFX" }
  86. end
  87. end
  88. local vsize
  89. if def.visual_scale then
  90. local s = def.visual_scale
  91. vsize = vector.new(s, s, s)
  92. end
  93. self.object:set_properties({
  94. is_visible = true,
  95. visual = "upright_sprite",
  96. visual_size = vsize,
  97. textures = textures,
  98. glow = def.light_source,
  99. })
  100. elseif def.drawtype ~= "airlike" then
  101. local itemstring = node.name
  102. if core.is_colored_paramtype(def.paramtype2) then
  103. itemstring = core.itemstring_with_palette(itemstring, node.param2)
  104. end
  105. -- FIXME: solution needed for paramtype2 == "leveled"
  106. -- Calculate size of falling node
  107. local s = {}
  108. s.x = (def.visual_scale or 1) * SCALE
  109. s.y = s.x
  110. s.z = s.x
  111. -- Compensate for wield_scale
  112. if def.wield_scale then
  113. s.x = s.x / def.wield_scale.x
  114. s.y = s.y / def.wield_scale.y
  115. s.z = s.z / def.wield_scale.z
  116. end
  117. self.object:set_properties({
  118. is_visible = true,
  119. wield_item = itemstring,
  120. visual_size = s,
  121. glow = def.light_source,
  122. })
  123. end
  124. -- Set collision box (certain nodeboxes only for now)
  125. local nb_types = {fixed=true, leveled=true, connected=true}
  126. if def.drawtype == "nodebox" and def.node_box and
  127. nb_types[def.node_box.type] and def.node_box.fixed then
  128. local box = table.copy(def.node_box.fixed)
  129. if type(box[1]) == "table" then
  130. box = #box == 1 and box[1] or nil -- We can only use a single box
  131. end
  132. if box then
  133. if def.paramtype2 == "leveled" and (self.node.level or 0) > 0 then
  134. box[5] = -0.5 + self.node.level / 64
  135. end
  136. self.object:set_properties({
  137. collisionbox = box
  138. })
  139. end
  140. end
  141. -- Rotate entity
  142. if def.drawtype == "torchlike" then
  143. if (def.paramtype2 == "wallmounted" or def.paramtype2 == "colorwallmounted")
  144. and node.param2 % 8 == 7 then
  145. self.object:set_yaw(-math.pi*0.25)
  146. else
  147. self.object:set_yaw(math.pi*0.25)
  148. end
  149. elseif ((node.param2 ~= 0 or def.drawtype == "nodebox" or def.drawtype == "mesh")
  150. and (def.wield_image == "" or def.wield_image == nil))
  151. or def.drawtype == "signlike"
  152. or def.drawtype == "mesh"
  153. or def.drawtype == "normal"
  154. or def.drawtype == "nodebox" then
  155. if (def.paramtype2 == "facedir" or def.paramtype2 == "colorfacedir") then
  156. local fdir = node.param2 % 32 % 24
  157. -- Get rotation from a precalculated lookup table
  158. local euler = facedir_to_euler[fdir + 1]
  159. if euler then
  160. self.object:set_rotation(euler)
  161. end
  162. elseif (def.paramtype2 == "4dir" or def.paramtype2 == "color4dir") then
  163. local fdir = node.param2 % 4
  164. -- Get rotation from a precalculated lookup table
  165. local euler = facedir_to_euler[fdir + 1]
  166. if euler then
  167. self.object:set_rotation(euler)
  168. end
  169. elseif (def.drawtype ~= "plantlike" and def.drawtype ~= "plantlike_rooted" and
  170. (def.paramtype2 == "wallmounted" or def.paramtype2 == "colorwallmounted" or def.drawtype == "signlike")) then
  171. local rot = node.param2 % 8
  172. if (def.drawtype == "signlike" and def.paramtype2 ~= "wallmounted" and def.paramtype2 ~= "colorwallmounted") then
  173. -- Change rotation to "floor" by default for non-wallmounted paramtype2
  174. rot = 1
  175. end
  176. local pitch, yaw, roll = 0, 0, 0
  177. if def.drawtype == "nodebox" or def.drawtype == "mesh" then
  178. if rot == 0 then
  179. pitch, yaw = math.pi/2, 0
  180. elseif rot == 1 then
  181. pitch, yaw = -math.pi/2, math.pi
  182. elseif rot == 2 then
  183. pitch, yaw = 0, math.pi/2
  184. elseif rot == 3 then
  185. pitch, yaw = 0, -math.pi/2
  186. elseif rot == 4 then
  187. pitch, yaw = 0, math.pi
  188. elseif rot == 6 then
  189. pitch, yaw = math.pi/2, 0
  190. elseif rot == 7 then
  191. pitch, yaw = -math.pi/2, math.pi
  192. end
  193. else
  194. if rot == 1 then
  195. pitch, yaw = math.pi, math.pi
  196. elseif rot == 2 then
  197. pitch, yaw = math.pi/2, math.pi/2
  198. elseif rot == 3 then
  199. pitch, yaw = math.pi/2, -math.pi/2
  200. elseif rot == 4 then
  201. pitch, yaw = math.pi/2, math.pi
  202. elseif rot == 5 then
  203. pitch, yaw = math.pi/2, 0
  204. elseif rot == 6 then
  205. pitch, yaw = math.pi, -math.pi/2
  206. elseif rot == 7 then
  207. pitch, yaw = 0, -math.pi/2
  208. end
  209. end
  210. if def.drawtype == "signlike" then
  211. pitch = pitch - math.pi/2
  212. if rot == 0 then
  213. yaw = yaw + math.pi/2
  214. elseif rot == 1 then
  215. yaw = yaw - math.pi/2
  216. elseif rot == 6 then
  217. yaw = yaw - math.pi/2
  218. pitch = pitch + math.pi
  219. elseif rot == 7 then
  220. yaw = yaw + math.pi/2
  221. pitch = pitch + math.pi
  222. end
  223. elseif def.drawtype == "mesh" or def.drawtype == "normal" or def.drawtype == "nodebox" then
  224. if rot == 0 or rot == 1 then
  225. roll = roll + math.pi
  226. elseif rot == 6 or rot == 7 then
  227. if def.drawtype ~= "normal" then
  228. roll = roll - math.pi/2
  229. end
  230. else
  231. yaw = yaw + math.pi
  232. end
  233. end
  234. self.object:set_rotation({x=pitch, y=yaw, z=roll})
  235. elseif (def.drawtype == "mesh" and def.paramtype2 == "degrotate") then
  236. local p2 = (node.param2 - (def.place_param2 or 0)) % 240
  237. local yaw = (p2 / 240) * (math.pi * 2)
  238. self.object:set_yaw(yaw)
  239. elseif (def.drawtype == "mesh" and def.paramtype2 == "colordegrotate") then
  240. local p2 = (node.param2 % 32 - (def.place_param2 or 0) % 32) % 24
  241. local yaw = (p2 / 24) * (math.pi * 2)
  242. self.object:set_yaw(yaw)
  243. end
  244. end
  245. end,
  246. get_staticdata = function(self)
  247. local ds = {
  248. node = self.node,
  249. meta = self.meta,
  250. }
  251. return core.serialize(ds)
  252. end,
  253. on_activate = function(self, staticdata)
  254. self.object:set_armor_groups({immortal = 1})
  255. self.object:set_acceleration(vector.new(0, -gravity, 0))
  256. local ds = core.deserialize(staticdata)
  257. if ds and ds.node then
  258. self:set_node(ds.node, ds.meta)
  259. elseif ds then
  260. self:set_node(ds)
  261. elseif staticdata ~= "" then
  262. self:set_node({name = staticdata})
  263. end
  264. end,
  265. try_place = function(self, bcp, bcn)
  266. local bcd = core.registered_nodes[bcn.name]
  267. -- Add levels if dropped on same leveled node
  268. if bcd and bcd.paramtype2 == "leveled" and
  269. bcn.name == self.node.name then
  270. local addlevel = self.node.level
  271. if (addlevel or 0) <= 0 then
  272. addlevel = bcd.leveled
  273. end
  274. if core.add_node_level(bcp, addlevel) < addlevel then
  275. return true
  276. elseif bcd.buildable_to then
  277. -- Node level has already reached max, don't place anything
  278. return true
  279. end
  280. end
  281. -- Decide if we're replacing the node or placing on top
  282. -- This condition is very similar to the check in core.check_single_for_falling(p)
  283. local np = vector.copy(bcp)
  284. if bcd and bcd.buildable_to
  285. and -- Take "float" group into consideration:
  286. (
  287. -- Fall through non-liquids
  288. not self.floats or bcd.liquidtype == "none" or
  289. -- Only let sources fall through flowing liquids
  290. (self.floats and self.liquidtype ~= "none" and bcd.liquidtype ~= "source")
  291. ) then
  292. core.remove_node(bcp)
  293. else
  294. -- We are placing on top so check what's there
  295. np.y = np.y + 1
  296. local n2 = core.get_node(np)
  297. local nd = core.registered_nodes[n2.name]
  298. if not nd or nd.buildable_to then
  299. core.remove_node(np)
  300. else
  301. -- 'walkable' is used to mean "falling nodes can't replace this"
  302. -- here. Normally we would collide with the walkable node itself
  303. -- and place our node on top (so `n2.name == "air"`), but we
  304. -- re-check this in case we ended up inside a node.
  305. if not nd.diggable or nd.walkable then
  306. return false
  307. end
  308. nd.on_dig(np, n2, nil)
  309. -- If it's still there, it might be protected
  310. if core.get_node(np).name == n2.name then
  311. return false
  312. end
  313. end
  314. end
  315. -- Create node
  316. local def = core.registered_nodes[self.node.name]
  317. if def then
  318. core.add_node(np, self.node)
  319. if self.meta then
  320. core.get_meta(np):from_table(self.meta)
  321. end
  322. if def.sounds and def.sounds.place then
  323. core.sound_play(def.sounds.place, {pos = np}, true)
  324. end
  325. end
  326. core.check_for_falling(np)
  327. return true
  328. end,
  329. on_step = function(self, dtime, moveresult)
  330. -- Fallback code since collision detection can't tell us
  331. -- about liquids (which do not collide)
  332. if self.floats then
  333. local pos = self.object:get_pos()
  334. local bcp = pos:offset(0, -0.7, 0):round()
  335. local bcn = core.get_node(bcp)
  336. local bcd = core.registered_nodes[bcn.name]
  337. if bcd and bcd.liquidtype ~= "none" then
  338. if self:try_place(bcp, bcn) then
  339. self.object:remove()
  340. return
  341. end
  342. end
  343. end
  344. assert(moveresult)
  345. if not moveresult.collides then
  346. return -- Nothing to do :)
  347. end
  348. local bcp, bcn
  349. local player_collision
  350. if moveresult.touching_ground then
  351. for _, info in ipairs(moveresult.collisions) do
  352. if info.type == "object" then
  353. if info.axis == "y" and info.object:is_player() then
  354. player_collision = info
  355. end
  356. elseif info.axis == "y" then
  357. bcp = info.node_pos
  358. bcn = core.get_node(bcp)
  359. break
  360. end
  361. end
  362. end
  363. if not bcp then
  364. -- We're colliding with something, but not the ground. Irrelevant to us.
  365. if player_collision then
  366. -- Continue falling through players by moving a little into
  367. -- their collision box
  368. -- TODO: this hack could be avoided in the future if objects
  369. -- could choose who to collide with
  370. local vel = self.object:get_velocity()
  371. self.object:set_velocity(vector.new(
  372. vel.x,
  373. player_collision.old_velocity.y,
  374. vel.z
  375. ))
  376. self.object:set_pos(self.object:get_pos():offset(0, -0.5, 0))
  377. end
  378. return
  379. elseif bcn.name == "ignore" then
  380. -- Delete on contact with ignore at world edges
  381. self.object:remove()
  382. return
  383. end
  384. local failure = false
  385. local pos = self.object:get_pos()
  386. local distance = vector.apply(vector.subtract(pos, bcp), math.abs)
  387. if distance.x >= 1 or distance.z >= 1 then
  388. -- We're colliding with some part of a node that's sticking out
  389. -- Since we don't want to visually teleport, drop as item
  390. failure = true
  391. elseif distance.y >= 2 then
  392. -- Doors consist of a hidden top node and a bottom node that is
  393. -- the actual door. Despite the top node being solid, the moveresult
  394. -- almost always indicates collision with the bottom node.
  395. -- Compensate for this by checking the top node
  396. bcp.y = bcp.y + 1
  397. bcn = core.get_node(bcp)
  398. local def = core.registered_nodes[bcn.name]
  399. if not (def and def.walkable) then
  400. failure = true -- This is unexpected, fail
  401. end
  402. end
  403. -- Try to actually place ourselves
  404. if not failure then
  405. failure = not self:try_place(bcp, bcn)
  406. end
  407. if failure then
  408. local drops = core.get_node_drops(self.node, "")
  409. for _, item in pairs(drops) do
  410. core.add_item(pos, item)
  411. end
  412. end
  413. self.object:remove()
  414. end
  415. })
  416. local function convert_to_falling_node(pos, node)
  417. local obj = core.add_entity(pos, "__builtin:falling_node")
  418. if not obj then
  419. return false
  420. end
  421. -- remember node level, the entities' set_node() uses this
  422. node.level = core.get_node_level(pos)
  423. local meta = core.get_meta(pos)
  424. local metatable = meta and meta:to_table() or {}
  425. local def = core.registered_nodes[node.name]
  426. if def and def.sounds and def.sounds.fall then
  427. core.sound_play(def.sounds.fall, {pos = pos}, true)
  428. end
  429. obj:get_luaentity():set_node(node, metatable)
  430. core.remove_node(pos)
  431. return true, obj
  432. end
  433. function core.spawn_falling_node(pos)
  434. local node = core.get_node(pos)
  435. if node.name == "air" or node.name == "ignore" then
  436. return false
  437. end
  438. return convert_to_falling_node(pos, node)
  439. end
  440. local function drop_attached_node(p)
  441. local n = core.get_node(p)
  442. local drops = core.get_node_drops(n, "")
  443. local def = core.registered_items[n.name]
  444. if def and def.preserve_metadata then
  445. local oldmeta = core.get_meta(p):to_table().fields
  446. -- Copy pos and node because the callback can modify them.
  447. local pos_copy = vector.copy(p)
  448. local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
  449. local drop_stacks = {}
  450. for k, v in pairs(drops) do
  451. drop_stacks[k] = ItemStack(v)
  452. end
  453. drops = drop_stacks
  454. def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
  455. end
  456. if def and def.sounds and def.sounds.fall then
  457. core.sound_play(def.sounds.fall, {pos = p}, true)
  458. end
  459. core.remove_node(p)
  460. for _, item in pairs(drops) do
  461. local pos = {
  462. x = p.x + math.random()/2 - 0.25,
  463. y = p.y + math.random()/2 - 0.25,
  464. z = p.z + math.random()/2 - 0.25,
  465. }
  466. core.add_item(pos, item)
  467. end
  468. end
  469. function builtin_shared.check_attached_node(p, n, group_rating)
  470. local def = core.registered_nodes[n.name]
  471. local d = vector.zero()
  472. if group_rating == 3 then
  473. -- always attach to floor
  474. d.y = -1
  475. elseif group_rating == 4 then
  476. -- always attach to ceiling
  477. d.y = 1
  478. elseif group_rating == 2 then
  479. -- attach to facedir or 4dir direction
  480. if (def.paramtype2 == "facedir" or
  481. def.paramtype2 == "colorfacedir") then
  482. -- Attach to whatever facedir is "mounted to".
  483. -- For facedir, this is where tile no. 5 point at.
  484. -- The fallback vector here is in case 'facedir to dir' is nil due
  485. -- to voxelmanip placing a wallmounted node without resetting a
  486. -- pre-existing param2 value that is out-of-range for facedir.
  487. -- The fallback vector corresponds to param2 = 0.
  488. d = core.facedir_to_dir(n.param2) or vector.new(0, 0, 1)
  489. elseif (def.paramtype2 == "4dir" or
  490. def.paramtype2 == "color4dir") then
  491. -- Similar to facedir handling
  492. d = core.fourdir_to_dir(n.param2) or vector.new(0, 0, 1)
  493. end
  494. elseif def.paramtype2 == "wallmounted" or
  495. def.paramtype2 == "colorwallmounted" then
  496. -- Attach to whatever this node is "mounted to".
  497. -- This where tile no. 2 points at.
  498. -- The fallback vector here is used for the same reason as
  499. -- for facedir nodes.
  500. d = core.wallmounted_to_dir(n.param2) or vector.new(0, 1, 0)
  501. else
  502. d.y = -1
  503. end
  504. local p2 = vector.add(p, d)
  505. local nn = core.get_node(p2).name
  506. local def2 = core.registered_nodes[nn]
  507. if def2 and not def2.walkable then
  508. return false
  509. end
  510. return true
  511. end
  512. --
  513. -- Some common functions
  514. --
  515. function core.check_single_for_falling(p)
  516. local n = core.get_node(p)
  517. if core.get_item_group(n.name, "falling_node") ~= 0 then
  518. local p_bottom = vector.offset(p, 0, -1, 0)
  519. -- Only spawn falling node if node below is loaded
  520. local n_bottom = core.get_node_or_nil(p_bottom)
  521. local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
  522. if d_bottom then
  523. local same = n.name == n_bottom.name
  524. -- Let leveled nodes fall if it can merge with the bottom node
  525. if same and d_bottom.paramtype2 == "leveled" and
  526. core.get_node_level(p_bottom) <
  527. core.get_node_max_level(p_bottom) then
  528. local success, _ = convert_to_falling_node(p, n)
  529. return success
  530. end
  531. local d_falling = core.registered_nodes[n.name]
  532. local do_float = core.get_item_group(n.name, "float") > 0
  533. -- Otherwise only if the bottom node is considered "fall through"
  534. if not same and
  535. (not d_bottom.walkable or d_bottom.buildable_to)
  536. and -- Take "float" group into consideration:
  537. (
  538. -- Fall through non-liquids
  539. not do_float or d_bottom.liquidtype == "none" or
  540. -- Only let sources fall through flowing liquids
  541. (do_float and d_falling.liquidtype == "source" and d_bottom.liquidtype ~= "source")
  542. ) then
  543. local success, _ = convert_to_falling_node(p, n)
  544. return success
  545. end
  546. end
  547. end
  548. local an = core.get_item_group(n.name, "attached_node")
  549. if an ~= 0 then
  550. if not builtin_shared.check_attached_node(p, n, an) then
  551. drop_attached_node(p)
  552. return true
  553. end
  554. end
  555. return false
  556. end
  557. -- This table is specifically ordered.
  558. -- We don't walk diagonals, only our direct neighbors, and self.
  559. -- Down first as likely case, but always before self. The same with sides.
  560. -- Up must come last, so that things above self will also fall all at once.
  561. local check_for_falling_neighbors = {
  562. vector.new(-1, -1, 0),
  563. vector.new( 1, -1, 0),
  564. vector.new( 0, -1, -1),
  565. vector.new( 0, -1, 1),
  566. vector.new( 0, -1, 0),
  567. vector.new(-1, 0, 0),
  568. vector.new( 1, 0, 0),
  569. vector.new( 0, 0, 1),
  570. vector.new( 0, 0, -1),
  571. vector.new( 0, 0, 0),
  572. vector.new( 0, 1, 0),
  573. }
  574. function core.check_for_falling(p)
  575. -- Round p to prevent falling entities to get stuck.
  576. p = vector.round(p)
  577. -- We make a stack, and manually maintain size for performance.
  578. -- Stored in the stack, we will maintain tables with pos, and
  579. -- last neighbor visited. This way, when we get back to each
  580. -- node, we know which directions we have already walked, and
  581. -- which direction is the next to walk.
  582. local s = {}
  583. local n = 0
  584. -- The neighbor order we will visit from our table.
  585. local v = 1
  586. while true do
  587. -- Push current pos onto the stack.
  588. n = n + 1
  589. s[n] = {p = p, v = v}
  590. -- Select next node from neighbor list.
  591. p = vector.add(p, check_for_falling_neighbors[v])
  592. -- Now we check out the node. If it is in need of an update,
  593. -- it will let us know in the return value (true = updated).
  594. if not core.check_single_for_falling(p) then
  595. -- If we don't need to "recurse" (walk) to it then pop
  596. -- our previous pos off the stack and continue from there,
  597. -- with the v value we were at when we last were at that
  598. -- node
  599. repeat
  600. local pop = s[n]
  601. p = pop.p
  602. v = pop.v
  603. s[n] = nil
  604. n = n - 1
  605. -- If there's nothing left on the stack, and no
  606. -- more sides to walk to, we're done and can exit
  607. if n == 0 and v == 11 then
  608. return
  609. end
  610. until v < 11
  611. -- The next round walk the next neighbor in list.
  612. v = v + 1
  613. else
  614. -- If we did need to walk the neighbor, then
  615. -- start walking it from the walk order start (1),
  616. -- and not the order we just pushed up the stack.
  617. v = 1
  618. end
  619. end
  620. end
  621. --
  622. -- Global callbacks
  623. --
  624. local function on_placenode(p, node)
  625. core.check_for_falling(p)
  626. end
  627. core.register_on_placenode(on_placenode)
  628. local function on_dignode(p, node)
  629. core.check_for_falling(p)
  630. end
  631. core.register_on_dignode(on_dignode)
  632. local function on_punchnode(p, node)
  633. core.check_for_falling(p)
  634. end
  635. core.register_on_punchnode(on_punchnode)