init.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. --minetest.chat_send_player("MustTest", dump(drops))
  34. for _, item in pairs(drops) do
  35. local p = {
  36. x = pos.x + math_random()/2 - 0.25,
  37. y = pos.y + math_random()/2 - 0.25,
  38. z = pos.z + math_random()/2 - 0.25,
  39. }
  40. add_item(p, item)
  41. end
  42. remove_node(pos)
  43. end
  44. -- Spawn particles.
  45. function _nodeupdate.spawn_particles(pos, node)
  46. ambiance.particles_on_dig(pos, node)
  47. end
  48. local spawn_particles = _nodeupdate.spawn_particles
  49. -- Override core function.
  50. core.check_single_for_falling = function(p)
  51. local n = get_node(p)
  52. -- Handle hanging nodes.
  53. if get_item_group(n.name, "hanging_node") ~= 0 then
  54. local p2 = {x=p.x, y=p.y+1, z=p.z}
  55. local n2 = get_node_or_nil(p2)
  56. if n2 and n2.name == "air" then
  57. remove_node(p)
  58. -- Pass node name, because passing a node table gives wrong results.
  59. for _, item in pairs(get_node_drops(n.name, "")) do
  60. local pos = {
  61. x = p.x + math_random()/2 - 0.25,
  62. y = p.y + math_random()/2 - 0.25,
  63. z = p.z + math_random()/2 - 0.25,
  64. }
  65. add_item(pos, item)
  66. end
  67. spawn_particles(p, n)
  68. return true
  69. end
  70. end
  71. -- Fallback to builtin function.
  72. local spawned = old_nodeupdate(p)
  73. if spawned then
  74. --minetest.chat_send_player("MustTest", "# Server: Spawned particles!")
  75. spawn_particles(p, n)
  76. end
  77. return spawned
  78. end
  79. if not _nodeupdate.run_once then
  80. local c = "nodeupdate:core"
  81. local f = _nodeupdate.modpath .. "/init.lua"
  82. reload.register_file(c, f, false)
  83. _nodeupdate.run_once = true
  84. end