2018-12-28 03:55:00 +00:00
|
|
|
rspawn = {}
|
2018-12-28 23:31:33 +00:00
|
|
|
rspawn.playerspawns = {}
|
2018-12-28 03:55:00 +00:00
|
|
|
|
|
|
|
local mpath = minetest.get_modpath("rspawn")
|
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
local function notnil_or(d, v)
|
|
|
|
if v == nil then
|
|
|
|
return d
|
|
|
|
else
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-28 23:31:33 +00:00
|
|
|
-- Water level, plus one to ensure we are above the sea.
|
|
|
|
local water_level = tonumber(minetest.settings:get("water_level", "1") )+1
|
2018-12-28 03:55:00 +00:00
|
|
|
local radial_step = 16
|
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
-- Setting with no namespace for interoperability
|
|
|
|
local static_spawnpoint = minetest.setting_get_pos("static_spawnpoint") or {x=0, y=0, z=0}
|
2018-12-28 03:55:00 +00:00
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
-- Setting from beds mod
|
|
|
|
rspawn.bedspawn = minetest.setting_getbool("enable_bed_respawn", true) -- from beds mod
|
|
|
|
|
|
|
|
-- Detect server mode, of sorts
|
2018-12-28 23:31:33 +00:00
|
|
|
rspawn.adminname = minetest.settings:get("name", "singleplayer")
|
2016-07-09 11:22:49 -04:00
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
-- rSpawn specific settings
|
|
|
|
rspawn.debug_on = minetest.settings:get_bool("rspawn.debug")
|
|
|
|
rspawn.spawnanywhere = notnil_or(true, minetest.settings:get_bool("rspawn.spawn_anywhere") )
|
|
|
|
rspawn.kick_on_fail = notnil_or(false, minetest.settings:get_bool("rspawn.kick_on_fail"))
|
|
|
|
rspawn.max_pregen_spawns = tonumber(minetest.settings:get("rspawn.max_pregen") or 5)
|
|
|
|
rspawn.search_radius = tonumber(minetest.settings:get("rspawn.search_radius") or 32)
|
|
|
|
rspawn.gen_frequency = tonumber(minetest.settings:get("rspawn.gen_frequency") or 30)
|
2018-12-29 20:24:45 +00:00
|
|
|
rspawn.spawn_block = minetest.settings:get("rspawn.spawn_block")
|
2018-12-29 16:18:36 +00:00
|
|
|
|
2018-12-28 23:31:33 +00:00
|
|
|
dofile(mpath.."/src/data.lua")
|
|
|
|
dofile(mpath.."/src/commands.lua")
|
2018-12-29 16:18:36 +00:00
|
|
|
dofile(mpath.."/src/forceload.lua")
|
|
|
|
dofile(mpath.."/src/debugging.lua")
|
2017-01-31 18:08:53 +00:00
|
|
|
|
2018-12-29 12:54:23 +00:00
|
|
|
|
2017-01-13 16:07:17 +00:00
|
|
|
|
2018-12-28 03:55:00 +00:00
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
rspawn:spawnload()
|
2017-01-31 18:08:53 +00:00
|
|
|
|
2018-12-29 20:24:45 +00:00
|
|
|
local function set_default_node(pos)
|
|
|
|
if rspawn.spawn_block then
|
|
|
|
minetest.set_node(pos, {name=rspawn.spawn_block})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-28 03:55:00 +00:00
|
|
|
local function daylight_above(min_daylight, pos)
|
2018-12-28 04:12:50 +00:00
|
|
|
local level = minetest.get_node_light(pos, 0.5)
|
|
|
|
return min_daylight <= level
|
2017-01-13 23:00:48 +00:00
|
|
|
end
|
2017-01-13 22:15:45 +00:00
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
function rspawn:get_positions_for(pos, radius)
|
|
|
|
local breadth = radius
|
2018-12-28 03:55:00 +00:00
|
|
|
local altitude = radius*2
|
|
|
|
|
2018-12-28 23:31:33 +00:00
|
|
|
local pos1 = {x=pos.x-breadth, y=pos.y, z=pos.z-breadth}
|
2018-12-28 03:55:00 +00:00
|
|
|
local pos2 = {x=pos.x+breadth, y=pos.y+altitude, z=pos.z+breadth}
|
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
return pos1,pos2
|
|
|
|
end
|
|
|
|
|
|
|
|
function rspawn:newspawn(pos, radius)
|
|
|
|
-- Given a seed position and a radius, find an exact spawn location
|
|
|
|
-- that is walkable and with 2 air nodes above it
|
|
|
|
|
|
|
|
rspawn:debug("Trying somewhere around "..minetest.pos_to_string(pos))
|
|
|
|
|
|
|
|
local pos1,pos2 = rspawn:get_positions_for(pos, radius)
|
2018-12-28 03:55:00 +00:00
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
rspawn:debug("Searching "..minetest.pos_to_string(pos1).." to "..minetest.pos_to_string(pos2))
|
2018-12-28 03:55:00 +00:00
|
|
|
|
|
|
|
local airnodes = minetest.find_nodes_in_area(pos1, pos2, {"air"})
|
|
|
|
local validnodes = {}
|
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
rspawn:debug("Found "..tostring(#airnodes).." air nodes within "..tostring(radius))
|
2018-12-28 03:55:00 +00:00
|
|
|
for _,anode in pairs(airnodes) do
|
|
|
|
local under = minetest.get_node( {x=anode.x, y=anode.y-1, z=anode.z} ).name
|
|
|
|
local over = minetest.get_node( {x=anode.x, y=anode.y+1, z=anode.z} ).name
|
|
|
|
under = minetest.registered_nodes[under]
|
|
|
|
over = minetest.registered_nodes[over]
|
|
|
|
|
|
|
|
if under.walkable
|
|
|
|
and not over.walkable
|
|
|
|
and not minetest.is_protected(anode, rspawn.adminname)
|
|
|
|
and daylight_above(7, anode) then
|
|
|
|
validnodes[#validnodes+1] = anode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #validnodes > 0 then
|
2018-12-29 16:18:36 +00:00
|
|
|
rspawn:debug("Valid spawn points found with radius "..tostring(radius))
|
2018-12-29 20:24:45 +00:00
|
|
|
local newpos = validnodes[math.random(1,#validnodes)]
|
|
|
|
|
|
|
|
return newpos
|
2018-12-29 16:18:36 +00:00
|
|
|
else
|
|
|
|
rspawn:debug("No valid air nodes")
|
2018-12-28 03:55:00 +00:00
|
|
|
end
|
2017-01-13 23:00:48 +00:00
|
|
|
end
|
|
|
|
|
2018-12-28 03:55:00 +00:00
|
|
|
function rspawn:genpos()
|
|
|
|
-- Generate a random position, and derive a new spawn position
|
2018-12-28 23:31:33 +00:00
|
|
|
local pos = static_spawnpoint
|
2018-12-28 03:55:00 +00:00
|
|
|
|
|
|
|
if rspawn.spawnanywhere then
|
|
|
|
pos = {
|
|
|
|
x = math.random(-30000,30000),
|
2018-12-29 16:18:36 +00:00
|
|
|
--y = math.random(water_level, water_level+20),
|
|
|
|
y = water_level, -- always at waterlevel
|
2018-12-28 03:55:00 +00:00
|
|
|
z = math.random(-30000,30000),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return pos
|
2017-01-13 23:00:48 +00:00
|
|
|
end
|
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
local function confirm_new_spawn(name, newpos)
|
|
|
|
local spos = minetest.pos_to_string(newpos)
|
2018-12-28 03:55:00 +00:00
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
rspawn.debug("Saving spawn for "..name, spos)
|
|
|
|
rspawn.playerspawns[name] = newpos
|
|
|
|
rspawn:spawnsave()
|
2018-12-28 03:55:00 +00:00
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
minetest.chat_send_player(name, "New spawn set at "..spos)
|
2017-01-13 22:03:36 +00:00
|
|
|
|
2018-12-28 23:31:33 +00:00
|
|
|
minetest.get_player_by_name(name):setpos(rspawn.playerspawns[name])
|
2018-12-29 20:30:39 +00:00
|
|
|
minetest.after(0.5,function()
|
|
|
|
set_default_node({x=newpos.x,y=newpos.y-1,z=newpos.z})
|
|
|
|
end)
|
2018-12-28 23:31:33 +00:00
|
|
|
end
|
|
|
|
|
2018-12-29 16:18:36 +00:00
|
|
|
function rspawn:set_newplayer_spawn(player)
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
|
|
|
|
if not rspawn.playerspawns[playername] then
|
|
|
|
local newpos = rspawn:get_next_spawn()
|
|
|
|
|
|
|
|
if newpos then
|
|
|
|
confirm_new_spawn(playername, newpos)
|
|
|
|
|
|
|
|
else
|
|
|
|
if rspawn.adminname ~= "singleplayer" or playername ~= rspawn.adminname then
|
|
|
|
minetest.chat_send_player(playername, "Please wait until a spawn point is available ...")
|
|
|
|
minetest.after(15, function()
|
|
|
|
rspawn:set_newplayer_spawn(player)
|
|
|
|
end)
|
|
|
|
elseif rspawn.kick_on_fail then
|
|
|
|
minetest.kick_player(playername, "No personalized spawn points available - please try again later.")
|
|
|
|
|
2018-12-28 03:55:00 +00:00
|
|
|
else
|
2018-12-29 16:18:36 +00:00
|
|
|
minetest.chat_send_player(playername, "Could not get custom spawn! Retrying in "..rspawn.gen_frequency.." seconds")
|
|
|
|
|
|
|
|
minetest.after(gen_frequency, function()
|
|
|
|
rspawn:set_newplayer_spawn(player)
|
|
|
|
end)
|
2018-12-28 03:55:00 +00:00
|
|
|
end
|
2018-12-29 16:18:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function rspawn:renew_player_spawn(playername)
|
|
|
|
local player = minetest.get_player_by_name(playername)
|
|
|
|
|
|
|
|
local newpos = rspawn:get_next_spawn()
|
|
|
|
|
|
|
|
if newpos then
|
|
|
|
confirm_new_spawn(playername, newpos)
|
|
|
|
|
2018-12-28 23:31:33 +00:00
|
|
|
else
|
2018-12-29 16:18:36 +00:00
|
|
|
minetest.chat_send_player(playername, "Could not get custom spawn!")
|
2018-12-28 03:55:00 +00:00
|
|
|
end
|
|
|
|
end
|
2017-01-13 16:07:17 +00:00
|
|
|
|
2017-01-13 23:00:48 +00:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2018-12-29 16:18:36 +00:00
|
|
|
rspawn:set_newplayer_spawn(player)
|
2017-01-13 23:00:48 +00:00
|
|
|
end)
|
2017-01-13 23:16:29 +00:00
|
|
|
|
|
|
|
minetest.register_on_respawnplayer(function(player)
|
2018-12-28 03:55:00 +00:00
|
|
|
local name = player:get_player_name()
|
|
|
|
if rspawn.bedspawn == true then
|
|
|
|
local pos = beds.spawn[name]
|
|
|
|
if pos then
|
|
|
|
player:setpos(pos)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- And if no bed, nor bed spwawning not active:
|
|
|
|
player:setpos(rspawn.playerspawns[name])
|
|
|
|
return true
|
2017-01-13 23:16:29 +00:00
|
|
|
end)
|
2018-12-29 16:18:36 +00:00
|
|
|
|
|
|
|
dofile(mpath.."/src/pregeneration.lua")
|