123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- if not minetest.global_exists("randspawn") then randspawn = {} end
- randspawn.modpath = minetest.get_modpath("randspawn")
- -- Localize for performance.
- local math_random = math.random
- -- After the Outback gateway exit coordinates are changed, this is the min and
- -- max number of days until it changes again.
- randspawn.min_days = 10
- randspawn.max_days = 90
- function randspawn.check_spawn_reset()
- local meta = randspawn.modstorage
- local stime = meta:get_string("spawn_reset_timer")
- -- If timestamp is missing, then initialize it.
- -- Outback reset will be schedualed after the timeout.
- if not stime or stime == "" then
- local time = os.time()
- local days = 60*60*24*math_random(randspawn.min_days, randspawn.max_days)
- time = time + days
- stime = tostring(time)
- meta:set_string("spawn_reset_timer", stime)
- -- Find a new spawn point.
- randspawn.find_new_spawn()
- return
- end
- local now = os.time()
- local later = tonumber(stime) -- Time of future reset (or initialization).
- if now >= later then
- later = later + 60*60*24*math_random(randspawn.min_days, randspawn.max_days)
- stime = tostring(later)
- meta:set_string("spawn_reset_timer", stime)
- -- Find a new spawn point.
- randspawn.find_new_spawn()
- end
- end
- minetest.after(0, function() randspawn.check_spawn_reset() end)
- -- Used by the calendar item.
- function randspawn.get_spawn_reset_timeout()
- local meta = randspawn.modstorage
- local stime = meta:get_string("spawn_reset_timer")
- local later = tonumber(stime)
- local now = os.time()
- local diff = later - now
- if diff < 0 then diff = 0 end
- return diff
- end
- local function callback(blockpos, action, calls_remaining, param)
- -- We don't do anything until the last callback.
- if calls_remaining ~= 0 then
- return
- end
- -- Check if there was an error on the LAST call.
- -- Note: this will usually fail if the area to emerge intersects the map edge.
- -- But usually we don't try to do that, here.
- if action == core.EMERGE_CANCELLED or action == core.EMERGE_ERRORED then
- return
- end
- local pos = param.pos
- local get_node = minetest.get_node
- -- Start at sea level and check upwards 200 meters to find ground.
- for y = -10, 200, 1 do
- local thispos = {x=pos.x, y=y, z=pos.z}
- local nu = get_node(thispos)
- local na = get_node(vector.add(thispos, {x=0, y=1, z=0}))
- -- Exit if map not loaded.
- if nu.name == "ignore" or na.name == "ignore" then
- --minetest.log("hit ignore at y=" .. y .. ", aborting")
- break
- end
- if na.name == "air" and (nu.name == "default:snow" or nu.name == "default:ice") then
- thispos.y = thispos.y + 1
- -- Call `serveressentials.update_exit_location()` once we have a new spawnpoint.
- --minetest.log("found new spawn location!")
- serveressentials.update_exit_location(thispos)
- return
- end
- end
- -- We didn't find a suitable spawn location. Try again shortly.
- --minetest.log("could not find spawn location, trying again.")
- local ls = param.local_shift
- minetest.after(10, function() randspawn.find_new_spawn(ls) end)
- end
- function randspawn.find_new_spawn(local_shift)
- local pos = {x=math_random(-6000, 6000), y=0, z=math_random(-6000, 6000)}
- -- If we're only performing a local shift, adjust the coordinates randomly
- -- around the current existing coordinates (if existing coords exist!).
- if local_shift then
- local rad = 75
- local soldpos = serveressentials.get_current_exit_location()
- local oldpos = minetest.string_to_pos(soldpos)
- if oldpos then
- pos.x = math.random(oldpos.x - rad, oldpos.x + rad)
- pos.y = 0
- pos.z = math.random(oldpos.z - rad, oldpos.z + rad)
- end
- end
- local minp = vector.add(pos, {x=-7, y=-20, z=-7})
- local maxp = vector.add(pos, {x=7, y=300, z=7})
- minetest.emerge_area(minp, maxp, callback,
- {pos=table.copy(pos), local_shift=local_shift})
- end
- -- This function shall ALWAYS return the Outback's static_spawn!
- local function get_respawn_position(invoke_pos, pname)
- -- Regardless of where player dies, if they have no bed,
- -- then they respawn in the outback. Note that a player may lose their bed if
- -- killed by another player outside of the city.
- return rc.static_spawn("abyss")
- end
- randspawn.get_respawn_pos = get_respawn_position
- -- Note: this is also called from the /spawn chatcommand,
- -- but only after validation passes (distance, etc.).
- -- This API shall place player at the Outback's static_spawn, ALWAYS.
- randspawn.reposition_player = function(pname, death_pos)
- local player = minetest.get_player_by_name(pname)
- if player then
- -- Ensure teleport is forced, to prevent a cheat.
- local pos = get_respawn_position(death_pos, pname)
- pos = vector.add(pos, {x=math_random(-2, 2), y=0, z=math_random(-2, 2)})
- preload_tp.execute({
- player_name = pname,
- target_position = pos,
- emerge_radius = 32,
- post_teleport_callback = function()
- ambiance.sound_play("respawn", pos, 0.5, 10)
- end,
- force_teleport = true,
- send_blocks = false,
- particle_effects = true,
- })
- end
- end
- -- The calendar item calls this to report the location of the current spawnpoint.
- function randspawn.get_spawn_name()
- local s = serveressentials.get_current_exit_location()
- local p = minetest.string_to_pos(s)
- -- The outback spawn exit isn't fixed, so you can never really know exactly
- -- where someone will come out at ... the calendar thus can only give you the
- -- aproximate location.
- if p then
- local rd = rc.get_realm_data(rc.current_realm_at_pos(p))
- local ro = {
- x = rd.realm_origin.x % 100,
- y = rd.realm_origin.y % 10,
- z = rd.realm_origin.z % 100,
- }
- p.x = math.floor(p.x / 100) * 100 + ro.x
- p.y = math.floor(p.y / 10) * 10 + ro.y
- p.z = math.floor(p.z / 100) * 100 + ro.z
- return rc.pos_to_namestr(p)
- end
- return "Unknown Location"
- end
- if not randspawn.run_once then
- -- Reloadable.
- local file = randspawn.modpath .. "/init.lua"
- local name = "randspawn:core"
- reload.register_file(name, file, false)
- -- Shift the Outback exit 30 minutes after every use.
- portal_cb.register_after_use(function(params)
- if rc.current_realm_at_pos(params.gate_origin) == "abyss" then
- minetest.after(60*30, function()
- return randspawn.find_new_spawn(true)
- end)
- end
- end)
- randspawn.modstorage = minetest.get_mod_storage()
- randspawn.run_once = true
- end
|