init.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. randspawn = randspawn or {}
  2. randspawn.modpath = minetest.get_modpath("randspawn")
  3. -- Localize for performance.
  4. local math_random = math.random
  5. -- After the Outback gateway exit coordinates are changed, this is the min and
  6. -- max number of days until it changes again.
  7. randspawn.min_days = 10
  8. randspawn.max_days = 90
  9. function randspawn.check_spawn_reset()
  10. local meta = randspawn.modstorage
  11. local stime = meta:get_string("spawn_reset_timer")
  12. -- If timestamp is missing, then initialize it.
  13. -- Outback reset will be schedualed after the timeout.
  14. if not stime or stime == "" then
  15. local time = os.time()
  16. local days = 60*60*24*math_random(randspawn.min_days, randspawn.max_days)
  17. time = time + days
  18. stime = tostring(time)
  19. meta:set_string("spawn_reset_timer", stime)
  20. -- Find a new spawn point.
  21. randspawn.find_new_spawn()
  22. return
  23. end
  24. local now = os.time()
  25. local later = tonumber(stime) -- Time of future reset (or initialization).
  26. if now >= later then
  27. later = later + 60*60*24*math_random(randspawn.min_days, randspawn.max_days)
  28. stime = tostring(later)
  29. meta:set_string("spawn_reset_timer", stime)
  30. -- Find a new spawn point.
  31. randspawn.find_new_spawn()
  32. end
  33. end
  34. minetest.after(0, function() randspawn.check_spawn_reset() end)
  35. -- Used by the calendar item.
  36. function randspawn.get_spawn_reset_timeout()
  37. local meta = randspawn.modstorage
  38. local stime = meta:get_string("spawn_reset_timer")
  39. local later = tonumber(stime)
  40. local now = os.time()
  41. local diff = later - now
  42. if diff < 0 then diff = 0 end
  43. return diff
  44. end
  45. local function callback(blockpos, action, calls_remaining, param)
  46. -- We don't do anything until the last callback.
  47. if calls_remaining ~= 0 then
  48. return
  49. end
  50. -- Check if there was an error on the LAST call.
  51. -- Note: this will usually fail if the area to emerge intersects the map edge.
  52. -- But usually we don't try to do that, here.
  53. if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
  54. return
  55. end
  56. local pos = param.pos
  57. local get_node = minetest.get_node
  58. -- Start at sea level and check upwards 200 meters to find ground.
  59. for y = -10, 200, 1 do
  60. pos.y = y
  61. local nu = get_node(pos)
  62. pos.y = y + 1
  63. local na = get_node(pos)
  64. -- Exit if map not loaded.
  65. if nu.name == "ignore" or na.name == "ignore" then
  66. break
  67. end
  68. if na.name == "air" and (nu.name == "default:snow" or nu.name == "default:ice") then
  69. pos.y = pos.y + 1
  70. serveressentials.update_exit_location(pos)
  71. return
  72. end
  73. end
  74. -- We didn't find a suitable spawn location. Try again shortly.
  75. minetest.after(60, function() randspawn.find_new_spawn() end)
  76. end
  77. function randspawn.find_new_spawn()
  78. -- Call `serveressentials.update_exit_location()` once we have a new spawnpoint.
  79. local pos = {x=math_random(-6000, 6000), y=0, z=math_random(-6000, 6000)}
  80. local minp = vector.add(pos, {x=-7, y=-7, z=-7})
  81. local maxp = vector.add(pos, {x=7, y=200, z=7})
  82. minetest.emerge_area(minp, maxp, callback, {pos=table.copy(pos)})
  83. end
  84. -- This function shall ALWAYS return the Outback's static_spawn!
  85. local function get_respawn_position(invoke_pos, pname)
  86. -- Regardless of where player dies, if they have no bed,
  87. -- then they respawn in the outback. Note that a player may lose their bed if
  88. -- killed by another player outside of the city.
  89. return rc.static_spawn("abyss")
  90. end
  91. randspawn.get_respawn_pos = get_respawn_position
  92. -- Note: this is also called from the /spawn chatcommand,
  93. -- but only after validation passes (distance, etc.).
  94. -- This API shall place player at the Outback's static_spawn, ALWAYS.
  95. randspawn.reposition_player = function(pname, death_pos)
  96. local player = minetest.get_player_by_name(pname)
  97. if player then
  98. -- Ensure teleport is forced, to prevent a cheat.
  99. local pos = get_respawn_position(death_pos, pname)
  100. pos = vector.add(pos, {x=math_random(-2, 2), y=0, z=math_random(-2, 2)})
  101. preload_tp.execute({
  102. player_name = pname,
  103. target_position = pos,
  104. emerge_radius = 32,
  105. post_teleport_callback = function()
  106. ambiance.sound_play("respawn", pos, 0.5, 10)
  107. end,
  108. force_teleport = true,
  109. send_blocks = false,
  110. particle_effects = true,
  111. })
  112. end
  113. end
  114. -- The calendar item calls this to report the location of the current spawnpoint.
  115. function randspawn.get_spawn_name()
  116. local s = serveressentials.get_current_exit_location()
  117. local p = minetest.string_to_pos(s)
  118. if p then
  119. return rc.pos_to_namestr(p)
  120. end
  121. return "Unknown Location"
  122. end
  123. if not randspawn.run_once then
  124. -- Reloadable.
  125. local file = randspawn.modpath .. "/init.lua"
  126. local name = "randspawn:core"
  127. reload.register_file(name, file, false)
  128. randspawn.modstorage = minetest.get_mod_storage()
  129. randspawn.run_once = true
  130. end