init.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. _nodeupdate = _nodeupdate or {}
  2. _nodeupdate.modpath = minetest.get_modpath("nodeupdate")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. -- Grab old update function and save it.
  6. if not _nodeupdate.old_update then
  7. _nodeupdate.old_update = core.check_single_for_falling
  8. end
  9. local old_nodeupdate = _nodeupdate.old_update
  10. local get_node = core.get_node
  11. local get_node_drops = core.get_node_drops
  12. local get_item_group = core.get_item_group
  13. local get_node_or_nil = core.get_node_or_nil
  14. local all_nodes = minetest.registered_nodes
  15. local add_item = core.add_item
  16. local remove_node = core.remove_node
  17. -- Drop a node as an entity.
  18. function _nodeupdate.drop_node_as_entity(pos)
  19. local node = get_node(pos)
  20. if node.name == "air" then
  21. return
  22. end
  23. local def = all_nodes[node.name]
  24. if def and def.groups then
  25. local ig = (def.groups.immovable or 0)
  26. if ig > 0 then
  27. return
  28. end
  29. end
  30. -- This function takes both nodetables and nodenames.
  31. -- Pass nodenames, because passing a nodetable gives wrong results.
  32. local drops = get_node_drops(node.name, "")
  33. for _, item in pairs(drops) do
  34. local p = {
  35. x = pos.x + math_random()/2 - 0.25,
  36. y = pos.y + math_random()/2 - 0.25,
  37. z = pos.z + math_random()/2 - 0.25,
  38. }
  39. add_item(p, item)
  40. end
  41. remove_node(pos)
  42. end
  43. -- Spawn particles.
  44. function _nodeupdate.spawn_particles(pos, node)
  45. ambiance.particles_on_dig(pos, node)
  46. end
  47. local spawn_particles = _nodeupdate.spawn_particles
  48. -- Override core function.
  49. core.check_single_for_falling = function(p)
  50. local n = get_node(p)
  51. -- Handle hanging nodes.
  52. if get_item_group(n.name, "hanging_node") ~= 0 then
  53. local p2 = {x=p.x, y=p.y+1, z=p.z}
  54. local n2 = get_node_or_nil(p2)
  55. if n2 and n2.name == "air" then
  56. remove_node(p)
  57. -- Pass node name, because passing a node table gives wrong results.
  58. for _, item in pairs(get_node_drops(n.name, "")) do
  59. local pos = {
  60. x = p.x + math_random()/2 - 0.25,
  61. y = p.y + math_random()/2 - 0.25,
  62. z = p.z + math_random()/2 - 0.25,
  63. }
  64. add_item(pos, item)
  65. end
  66. spawn_particles(p, n)
  67. return true
  68. end
  69. end
  70. -- Fallback to builtin function.
  71. local spawned = old_nodeupdate(p)
  72. if spawned then
  73. spawn_particles(p, n)
  74. end
  75. return spawned
  76. end
  77. if not _nodeupdate.run_once then
  78. local c = "nodeupdate:core"
  79. local f = _nodeupdate.modpath .. "/init.lua"
  80. reload.register_file(c, f, false)
  81. _nodeupdate.run_once = true
  82. end