particle_override.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. -- We need to override the Minetest particle API in order to
  2. -- control which clients receive particles.
  3. -- Localize vector.distance() for performance.
  4. local vector_distance = vector.distance
  5. local players = {}
  6. local modmeta = minetest.get_mod_storage()
  7. local maxdist = 50
  8. -- Save original functions.
  9. local add_particlespawner = minetest.add_particlespawner
  10. local add_particle = minetest.add_particle
  11. utility.original_add_particlespawner = add_particlespawner
  12. utility.original_add_particle = add_particle
  13. function default.particles_enabled_for(pname)
  14. local key = "particles:" .. pname
  15. if modmeta:get_int(key) == 1 then
  16. return false
  17. end
  18. return true
  19. end
  20. -- Called from the control panel GUI.
  21. function default.enable_particles_for(pname, enable)
  22. local key = "particles:" .. pname
  23. if enable == true then
  24. modmeta:set_int(key, 0)
  25. if minetest.get_player_by_name(pname) then
  26. players[pname] = true
  27. end
  28. else
  29. modmeta:set_int(key, 1)
  30. players[pname] = nil
  31. end
  32. end
  33. function minetest.add_particlespawner_single(data)
  34. local id
  35. local pname = data.playername or ""
  36. if players[pname] then
  37. local player = minetest.get_player_by_name(pname)
  38. if player then
  39. if vector_distance(player:get_pos(), data.minpos) <= maxdist then
  40. id = add_particlespawner(data)
  41. end
  42. end
  43. end
  44. return id
  45. end
  46. function minetest.add_particlespawner(data)
  47. for k, v in pairs(players) do
  48. local player = minetest.get_player_by_name(k)
  49. if player then
  50. if vector_distance(player:get_pos(), data.minpos) <= maxdist then
  51. data.playername = k
  52. add_particlespawner(data)
  53. end
  54. end
  55. end
  56. end
  57. function minetest.add_particle(data)
  58. for k, v in pairs(players) do
  59. local player = minetest.get_player_by_name(k)
  60. if player then
  61. if vector_distance(player:get_pos(), data.pos) <= maxdist then
  62. data.playername = k
  63. add_particle(data)
  64. end
  65. end
  66. end
  67. end
  68. minetest.register_on_joinplayer(function(player)
  69. local pname = player:get_player_name()
  70. local key = "particles:" .. pname
  71. if modmeta:get_int(key) == 0 then
  72. players[pname] = true
  73. else
  74. players[pname] = nil
  75. end
  76. end)
  77. minetest.register_on_leaveplayer(function(player)
  78. local pname = player:get_player_name()
  79. players[pname] = nil
  80. end)