functions.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. -- Localize for performance.
  2. local math_random = math.random
  3. local math_min = math.min
  4. local math_max = math.max
  5. function utility.detach_player_with_message(player)
  6. local k = default.detach_player_if_attached(player)
  7. if k then
  8. local t = player:get_player_name()
  9. if k == "cart" then
  10. minetest.chat_send_all("# Server: Someone threw <" .. rename.gpn(t) .. "> out of a minecart.")
  11. elseif k == "boat" then
  12. minetest.chat_send_all("# Server: Boater <" .. rename.gpn(t) .. "> was tossed overboard.")
  13. elseif k == "sled" then
  14. minetest.chat_send_all("# Server: Someone kicked <" .. rename.gpn(t) .. "> off a sled.")
  15. elseif k == "bed" then
  16. minetest.chat_send_all("# Server: <" .. rename.gpn(t) .. "> was rudely kicked out of bed.")
  17. end
  18. end
  19. end
  20. -- Function to find an ignore node NOT beyond the world edge.
  21. -- This is useful when we must check for `ignore`, but don't want to be confused at the edge of the world.
  22. function utility.find_node_near_not_world_edge(pos, rad, node)
  23. local minp = vector.subtract(pos, rad)
  24. local maxp = vector.add(pos, rad)
  25. minp.x = math_max(minp.x, -30912)
  26. minp.y = math_max(minp.y, -30912)
  27. minp.z = math_max(minp.z, -30912)
  28. maxp.x = math_min(maxp.x, 30927)
  29. maxp.y = math_min(maxp.y, 30927)
  30. maxp.z = math_min(maxp.z, 30927)
  31. local positions = minetest.find_nodes_in_area(minp, maxp, node)
  32. if (#positions > 0) then
  33. return positions[math_random(1, #positions)]
  34. end
  35. end
  36. local function add_effects(pos, radius)
  37. minetest.add_particlespawner({
  38. amount = 8,
  39. time = 0.5,
  40. minpos = vector.subtract(pos, radius / 2),
  41. maxpos = vector.add(pos, radius / 2),
  42. minvel = {x=-10, y=-10, z=-10},
  43. maxvel = {x=10, y=10, z=10},
  44. minacc = vector.new(),
  45. maxacc = vector.new(),
  46. minexptime = 0.5,
  47. maxexptime = 1,
  48. minsize = 0.5,
  49. maxsize = 1,
  50. texture = "tnt_smoke.png",
  51. })
  52. end
  53. function utility.shell_boom(pos)
  54. minetest.sound_play("throwing_shell_explode", {pos=pos, gain=1.5, max_hear_distance=2*64})
  55. -- Don't destroy things.
  56. if minetest.get_node(pos).name == "air" then
  57. minetest.set_node(pos, {name="tnt:boom"})
  58. minetest.get_node_timer(pos):start(0.5)
  59. end
  60. add_effects(pos, 1)
  61. end
  62. -- Note: this works for boats, too.
  63. -- Returns [""] if player wasn't attached. Otherwise, name of previous attachment type.
  64. function default.detach_player_if_attached(player)
  65. local pname = player:get_player_name()
  66. -- Player might be in bed! Get them out properly.
  67. if beds.kick_one_player(pname) then
  68. return "bed"
  69. end
  70. local ents = minetest.get_objects_inside_radius(utility.get_foot_pos(player:get_pos()), 2)
  71. local result = ""
  72. for k, obj in ipairs(ents) do
  73. local ent = obj:get_luaentity()
  74. if ent and ent.name == "carts:cart" then
  75. -- Must detach player from cart.
  76. carts:manage_attachment(player, nil)
  77. result = "cart"
  78. elseif ent and ent.name == "boats:boat" then
  79. if ent.driver and ent.driver == player then
  80. -- Since boat driver == player, this should always detach.
  81. boats.on_rightclick(ent, player)
  82. result = "boat"
  83. end
  84. elseif ent and ent.name == "sleds:sled" then
  85. if ent.driver and ent.driver == player then
  86. sleds.on_rightclick(ent, player)
  87. result = "sled"
  88. end
  89. end
  90. end
  91. return result
  92. end