falling.lua 17 KB

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