falling.lua 17 KB

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