init.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. if not minetest.global_exists("_nodeupdate") then _nodeupdate = {} end
  2. _nodeupdate.modpath = minetest.get_modpath("nodeupdate")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. local get_node = core.get_node
  6. local get_node_drops = core.get_node_drops
  7. local get_item_group = core.get_item_group
  8. local get_node_or_nil = core.get_node_or_nil
  9. local all_nodes = minetest.registered_nodes
  10. local add_item = core.add_item
  11. local remove_node = core.remove_node
  12. -- Drop a node as an entity.
  13. function _nodeupdate.drop_node_as_entity(pos)
  14. local node = get_node(pos)
  15. if node.name == "air" then
  16. return
  17. end
  18. local def = all_nodes[node.name]
  19. if def and def.groups then
  20. local ig = (def.groups.immovable or 0)
  21. if ig > 0 then
  22. return
  23. end
  24. end
  25. -- This function takes both nodetables and nodenames.
  26. -- Pass nodenames, because passing a nodetable gives wrong results.
  27. local drops = get_node_drops(node.name, "")
  28. for _, item in pairs(drops) do
  29. local p = {
  30. x = pos.x + math_random()/2 - 0.25,
  31. y = pos.y + math_random()/2 - 0.25,
  32. z = pos.z + math_random()/2 - 0.25,
  33. }
  34. add_item(p, item)
  35. end
  36. remove_node(pos)
  37. end
  38. -- Spawn particles.
  39. function _nodeupdate.spawn_particles(pos, node)
  40. ambiance.particles_on_dig(pos, node)
  41. end
  42. local spawn_particles = _nodeupdate.spawn_particles
  43. if not _nodeupdate.run_once then
  44. local c = "nodeupdate:core"
  45. local f = _nodeupdate.modpath .. "/init.lua"
  46. reload.register_file(c, f, false)
  47. _nodeupdate.run_once = true
  48. end