falling.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. np.y = np.y + 1
  295. end
  296. -- Check what's here
  297. local n2 = core.get_node(np)
  298. local nd = core.registered_nodes[n2.name]
  299. -- If it's not air or liquid, remove node and replace it with
  300. -- it's drops
  301. if n2.name ~= "air" and (not nd or nd.liquidtype ~= "source") then
  302. if nd and nd.buildable_to == false then
  303. nd.on_dig(np, n2, nil)
  304. -- If it's still there, it might be protected
  305. if core.get_node(np).name == n2.name then
  306. return false
  307. end
  308. else
  309. core.remove_node(np)
  310. end
  311. end
  312. -- Create node
  313. local def = core.registered_nodes[self.node.name]
  314. if def then
  315. core.add_node(np, self.node)
  316. if self.meta then
  317. core.get_meta(np):from_table(self.meta)
  318. end
  319. if def.sounds and def.sounds.place then
  320. core.sound_play(def.sounds.place, {pos = np}, true)
  321. end
  322. end
  323. core.check_for_falling(np)
  324. return true
  325. end,
  326. on_step = function(self, dtime, moveresult)
  327. -- Fallback code since collision detection can't tell us
  328. -- about liquids (which do not collide)
  329. if self.floats then
  330. local pos = self.object:get_pos()
  331. local bcp = pos:offset(0, -0.7, 0):round()
  332. local bcn = core.get_node(bcp)
  333. local bcd = core.registered_nodes[bcn.name]
  334. if bcd and bcd.liquidtype ~= "none" then
  335. if self:try_place(bcp, bcn) then
  336. self.object:remove()
  337. return
  338. end
  339. end
  340. end
  341. assert(moveresult)
  342. if not moveresult.collides then
  343. return -- Nothing to do :)
  344. end
  345. local bcp, bcn
  346. local player_collision
  347. if moveresult.touching_ground then
  348. for _, info in ipairs(moveresult.collisions) do
  349. if info.type == "object" then
  350. if info.axis == "y" and info.object:is_player() then
  351. player_collision = info
  352. end
  353. elseif info.axis == "y" then
  354. bcp = info.node_pos
  355. bcn = core.get_node(bcp)
  356. break
  357. end
  358. end
  359. end
  360. if not bcp then
  361. -- We're colliding with something, but not the ground. Irrelevant to us.
  362. if player_collision then
  363. -- Continue falling through players by moving a little into
  364. -- their collision box
  365. -- TODO: this hack could be avoided in the future if objects
  366. -- could choose who to collide with
  367. local vel = self.object:get_velocity()
  368. self.object:set_velocity(vector.new(
  369. vel.x,
  370. player_collision.old_velocity.y,
  371. vel.z
  372. ))
  373. self.object:set_pos(self.object:get_pos():offset(0, -0.5, 0))
  374. end
  375. return
  376. elseif bcn.name == "ignore" then
  377. -- Delete on contact with ignore at world edges
  378. self.object:remove()
  379. return
  380. end
  381. local failure = false
  382. local pos = self.object:get_pos()
  383. local distance = vector.apply(vector.subtract(pos, bcp), math.abs)
  384. if distance.x >= 1 or distance.z >= 1 then
  385. -- We're colliding with some part of a node that's sticking out
  386. -- Since we don't want to visually teleport, drop as item
  387. failure = true
  388. elseif distance.y >= 2 then
  389. -- Doors consist of a hidden top node and a bottom node that is
  390. -- the actual door. Despite the top node being solid, the moveresult
  391. -- almost always indicates collision with the bottom node.
  392. -- Compensate for this by checking the top node
  393. bcp.y = bcp.y + 1
  394. bcn = core.get_node(bcp)
  395. local def = core.registered_nodes[bcn.name]
  396. if not (def and def.walkable) then
  397. failure = true -- This is unexpected, fail
  398. end
  399. end
  400. -- Try to actually place ourselves
  401. if not failure then
  402. failure = not self:try_place(bcp, bcn)
  403. end
  404. if failure then
  405. local drops = core.get_node_drops(self.node, "")
  406. for _, item in pairs(drops) do
  407. core.add_item(pos, item)
  408. end
  409. end
  410. self.object:remove()
  411. end
  412. })
  413. local function convert_to_falling_node(pos, node)
  414. local obj = core.add_entity(pos, "__builtin:falling_node")
  415. if not obj then
  416. return false
  417. end
  418. -- remember node level, the entities' set_node() uses this
  419. node.level = core.get_node_level(pos)
  420. local meta = core.get_meta(pos)
  421. local metatable = meta and meta:to_table() or {}
  422. local def = core.registered_nodes[node.name]
  423. if def and def.sounds and def.sounds.fall then
  424. core.sound_play(def.sounds.fall, {pos = pos}, true)
  425. end
  426. obj:get_luaentity():set_node(node, metatable)
  427. core.remove_node(pos)
  428. return true, obj
  429. end
  430. function core.spawn_falling_node(pos)
  431. local node = core.get_node(pos)
  432. if node.name == "air" or node.name == "ignore" then
  433. return false
  434. end
  435. return convert_to_falling_node(pos, node)
  436. end
  437. local function drop_attached_node(p)
  438. local n = core.get_node(p)
  439. local drops = core.get_node_drops(n, "")
  440. local def = core.registered_items[n.name]
  441. if def and def.preserve_metadata then
  442. local oldmeta = core.get_meta(p):to_table().fields
  443. -- Copy pos and node because the callback can modify them.
  444. local pos_copy = vector.copy(p)
  445. local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
  446. local drop_stacks = {}
  447. for k, v in pairs(drops) do
  448. drop_stacks[k] = ItemStack(v)
  449. end
  450. drops = drop_stacks
  451. def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
  452. end
  453. if def and def.sounds and def.sounds.fall then
  454. core.sound_play(def.sounds.fall, {pos = p}, true)
  455. end
  456. core.remove_node(p)
  457. for _, item in pairs(drops) do
  458. local pos = {
  459. x = p.x + math.random()/2 - 0.25,
  460. y = p.y + math.random()/2 - 0.25,
  461. z = p.z + math.random()/2 - 0.25,
  462. }
  463. core.add_item(pos, item)
  464. end
  465. end
  466. function builtin_shared.check_attached_node(p, n, group_rating)
  467. local def = core.registered_nodes[n.name]
  468. local d = vector.zero()
  469. if group_rating == 3 then
  470. -- always attach to floor
  471. d.y = -1
  472. elseif group_rating == 4 then
  473. -- always attach to ceiling
  474. d.y = 1
  475. elseif group_rating == 2 then
  476. -- attach to facedir or 4dir direction
  477. if (def.paramtype2 == "facedir" or
  478. def.paramtype2 == "colorfacedir") then
  479. -- Attach to whatever facedir is "mounted to".
  480. -- For facedir, this is where tile no. 5 point at.
  481. -- The fallback vector here is in case 'facedir to dir' is nil due
  482. -- to voxelmanip placing a wallmounted node without resetting a
  483. -- pre-existing param2 value that is out-of-range for facedir.
  484. -- The fallback vector corresponds to param2 = 0.
  485. d = core.facedir_to_dir(n.param2) or vector.new(0, 0, 1)
  486. elseif (def.paramtype2 == "4dir" or
  487. def.paramtype2 == "color4dir") then
  488. -- Similar to facedir handling
  489. d = core.fourdir_to_dir(n.param2) or vector.new(0, 0, 1)
  490. end
  491. elseif def.paramtype2 == "wallmounted" or
  492. def.paramtype2 == "colorwallmounted" then
  493. -- Attach to whatever this node is "mounted to".
  494. -- This where tile no. 2 points at.
  495. -- The fallback vector here is used for the same reason as
  496. -- for facedir nodes.
  497. d = core.wallmounted_to_dir(n.param2) or vector.new(0, 1, 0)
  498. else
  499. d.y = -1
  500. end
  501. local p2 = vector.add(p, d)
  502. local nn = core.get_node(p2).name
  503. local def2 = core.registered_nodes[nn]
  504. if def2 and not def2.walkable then
  505. return false
  506. end
  507. return true
  508. end
  509. --
  510. -- Some common functions
  511. --
  512. function core.check_single_for_falling(p)
  513. local n = core.get_node(p)
  514. if core.get_item_group(n.name, "falling_node") ~= 0 then
  515. local p_bottom = vector.offset(p, 0, -1, 0)
  516. -- Only spawn falling node if node below is loaded
  517. local n_bottom = core.get_node_or_nil(p_bottom)
  518. local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
  519. if d_bottom then
  520. local same = n.name == n_bottom.name
  521. -- Let leveled nodes fall if it can merge with the bottom node
  522. if same and d_bottom.paramtype2 == "leveled" and
  523. core.get_node_level(p_bottom) <
  524. core.get_node_max_level(p_bottom) then
  525. local success, _ = convert_to_falling_node(p, n)
  526. return success
  527. end
  528. local d_falling = core.registered_nodes[n.name]
  529. local do_float = core.get_item_group(n.name, "float") > 0
  530. -- Otherwise only if the bottom node is considered "fall through"
  531. if not same and
  532. (not d_bottom.walkable or d_bottom.buildable_to)
  533. and -- Take "float" group into consideration:
  534. (
  535. -- Fall through non-liquids
  536. not do_float or d_bottom.liquidtype == "none" or
  537. -- Only let sources fall through flowing liquids
  538. (do_float and d_falling.liquidtype == "source" and d_bottom.liquidtype ~= "source")
  539. ) then
  540. local success, _ = convert_to_falling_node(p, n)
  541. return success
  542. end
  543. end
  544. end
  545. local an = core.get_item_group(n.name, "attached_node")
  546. if an ~= 0 then
  547. if not builtin_shared.check_attached_node(p, n, an) then
  548. drop_attached_node(p)
  549. return true
  550. end
  551. end
  552. return false
  553. end
  554. -- This table is specifically ordered.
  555. -- We don't walk diagonals, only our direct neighbors, and self.
  556. -- Down first as likely case, but always before self. The same with sides.
  557. -- Up must come last, so that things above self will also fall all at once.
  558. local check_for_falling_neighbors = {
  559. vector.new(-1, -1, 0),
  560. vector.new( 1, -1, 0),
  561. vector.new( 0, -1, -1),
  562. vector.new( 0, -1, 1),
  563. vector.new( 0, -1, 0),
  564. vector.new(-1, 0, 0),
  565. vector.new( 1, 0, 0),
  566. vector.new( 0, 0, 1),
  567. vector.new( 0, 0, -1),
  568. vector.new( 0, 0, 0),
  569. vector.new( 0, 1, 0),
  570. }
  571. function core.check_for_falling(p)
  572. -- Round p to prevent falling entities to get stuck.
  573. p = vector.round(p)
  574. -- We make a stack, and manually maintain size for performance.
  575. -- Stored in the stack, we will maintain tables with pos, and
  576. -- last neighbor visited. This way, when we get back to each
  577. -- node, we know which directions we have already walked, and
  578. -- which direction is the next to walk.
  579. local s = {}
  580. local n = 0
  581. -- The neighbor order we will visit from our table.
  582. local v = 1
  583. while true do
  584. -- Push current pos onto the stack.
  585. n = n + 1
  586. s[n] = {p = p, v = v}
  587. -- Select next node from neighbor list.
  588. p = vector.add(p, check_for_falling_neighbors[v])
  589. -- Now we check out the node. If it is in need of an update,
  590. -- it will let us know in the return value (true = updated).
  591. if not core.check_single_for_falling(p) then
  592. -- If we don't need to "recurse" (walk) to it then pop
  593. -- our previous pos off the stack and continue from there,
  594. -- with the v value we were at when we last were at that
  595. -- node
  596. repeat
  597. local pop = s[n]
  598. p = pop.p
  599. v = pop.v
  600. s[n] = nil
  601. n = n - 1
  602. -- If there's nothing left on the stack, and no
  603. -- more sides to walk to, we're done and can exit
  604. if n == 0 and v == 11 then
  605. return
  606. end
  607. until v < 11
  608. -- The next round walk the next neighbor in list.
  609. v = v + 1
  610. else
  611. -- If we did need to walk the neighbor, then
  612. -- start walking it from the walk order start (1),
  613. -- and not the order we just pushed up the stack.
  614. v = 1
  615. end
  616. end
  617. end
  618. --
  619. -- Global callbacks
  620. --
  621. local function on_placenode(p, node)
  622. core.check_for_falling(p)
  623. end
  624. core.register_on_placenode(on_placenode)
  625. local function on_dignode(p, node)
  626. core.check_for_falling(p)
  627. end
  628. core.register_on_dignode(on_dignode)
  629. local function on_punchnode(p, node)
  630. core.check_for_falling(p)
  631. end
  632. core.register_on_punchnode(on_punchnode)