2017-11-24 06:04:03 +00:00
|
|
|
mcl_spawn = {}
|
|
|
|
|
2019-03-13 22:26:33 +00:00
|
|
|
local S = minetest.get_translator("mcl_spawn")
|
2019-02-08 21:44:26 +00:00
|
|
|
local mg_name = minetest.get_mapgen_setting("mg_name")
|
|
|
|
|
2019-03-09 09:04:54 +00:00
|
|
|
local cached_world_spawn
|
|
|
|
|
|
|
|
mcl_spawn.get_world_spawn_pos = function()
|
|
|
|
local spawn
|
|
|
|
spawn = minetest.setting_get_pos("static_spawnpoint")
|
|
|
|
if spawn then
|
|
|
|
return spawn
|
|
|
|
end
|
|
|
|
if cached_world_spawn then
|
|
|
|
return cached_world_spawn
|
|
|
|
end
|
|
|
|
-- 32 attempts to find a suitable spawn point
|
|
|
|
spawn = { x=math.random(-16, 16), y=8, z=math.random(-16, 16) }
|
|
|
|
for i=1, 32 do
|
|
|
|
local y = minetest.get_spawn_level(spawn.x, spawn.z)
|
|
|
|
if y then
|
|
|
|
spawn.y = y
|
|
|
|
cached_world_spawn = spawn
|
|
|
|
minetest.log("action", "[mcl_spawn] Dynamic world spawn determined to be "..minetest.pos_to_string(spawn))
|
|
|
|
return spawn
|
|
|
|
end
|
|
|
|
-- Random walk
|
|
|
|
spawn.x = spawn.x + math.random(-64, 64)
|
|
|
|
spawn.z = spawn.z + math.random(-64, 64)
|
|
|
|
end
|
|
|
|
minetest.log("action", "[mcl_spawn] Failed to determine dynamic world spawn!")
|
|
|
|
-- Use dummy position if nothing found
|
|
|
|
return { x=math.random(-16, 16), y=8, z=math.random(-16, 16) }
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns a spawn position of player.
|
|
|
|
-- If player is nil or not a player, a world spawn point is returned.
|
|
|
|
-- The second return value is true if returned spawn point is player-chosen,
|
2019-02-07 18:54:12 +00:00
|
|
|
-- false otherwise.
|
2017-11-24 06:04:03 +00:00
|
|
|
mcl_spawn.get_spawn_pos = function(player)
|
2019-02-11 14:49:36 +00:00
|
|
|
local spawn, custom_spawn = nil, false
|
2017-11-24 06:04:03 +00:00
|
|
|
if player ~= nil and player:is_player() then
|
2019-03-06 04:45:16 +00:00
|
|
|
local attr = player:get_meta():get_string("mcl_beds:spawn")
|
2019-02-11 14:49:36 +00:00
|
|
|
if attr ~= nil and attr ~= "" then
|
|
|
|
spawn = minetest.string_to_pos(attr)
|
|
|
|
custom_spawn = true
|
|
|
|
end
|
2017-11-24 06:04:03 +00:00
|
|
|
end
|
|
|
|
if not spawn or spawn == "" then
|
2019-03-09 09:04:54 +00:00
|
|
|
spawn = mcl_spawn.get_world_spawn_pos()
|
2019-02-07 18:54:12 +00:00
|
|
|
custom_spawn = false
|
2017-11-24 06:04:03 +00:00
|
|
|
end
|
2019-02-07 18:54:12 +00:00
|
|
|
return spawn, custom_spawn
|
2017-11-24 06:04:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Sets the player's spawn position to pos.
|
|
|
|
-- Set pos to nil to clear the spawn position.
|
2019-02-20 18:39:12 +00:00
|
|
|
-- If message is set, informs the player with a chat message when the spawn position
|
|
|
|
-- changed.
|
|
|
|
mcl_spawn.set_spawn_pos = function(player, pos, message)
|
|
|
|
local spawn_changed = false
|
2019-03-06 04:45:16 +00:00
|
|
|
local meta = player:get_meta()
|
2017-11-24 06:04:03 +00:00
|
|
|
if pos == nil then
|
2019-03-06 04:45:16 +00:00
|
|
|
if meta:get_string("mcl_beds:spawn") ~= "" then
|
2019-02-20 18:39:12 +00:00
|
|
|
spawn_changed = true
|
|
|
|
if message then
|
2019-03-13 22:26:33 +00:00
|
|
|
minetest.chat_send_player(player:get_player_name(), S("Respawn position cleared!"))
|
2019-02-20 18:39:12 +00:00
|
|
|
end
|
|
|
|
end
|
2019-03-06 04:45:16 +00:00
|
|
|
meta:set_string("mcl_beds:spawn", "")
|
2017-11-24 06:04:03 +00:00
|
|
|
else
|
2019-03-06 04:45:16 +00:00
|
|
|
local oldpos = minetest.string_to_pos(meta:get_string("mcl_beds:spawn"))
|
2020-09-09 10:35:44 +00:00
|
|
|
meta:set_string("mcl_beds:spawn", minetest.pos_to_string(pos))
|
2019-02-20 18:39:12 +00:00
|
|
|
if oldpos then
|
|
|
|
-- We don't bother sending a message if the new spawn pos is basically the same
|
2020-09-09 10:35:44 +00:00
|
|
|
spawn_changed = vector.distance(pos, oldpos) > 0.1
|
|
|
|
else
|
|
|
|
-- If it wasn't set and now it will be set, it means it is changed
|
|
|
|
spawn_changed = true
|
|
|
|
end
|
|
|
|
if spawn_changed and message then
|
|
|
|
minetest.chat_send_player(player:get_player_name(), S("New respawn position set!"))
|
2019-02-20 18:39:12 +00:00
|
|
|
end
|
2017-11-24 06:04:03 +00:00
|
|
|
end
|
2019-02-20 18:39:12 +00:00
|
|
|
return spawn_changed
|
2017-11-24 06:04:03 +00:00
|
|
|
end
|
|
|
|
|
2019-02-28 14:35:18 +00:00
|
|
|
local function get_far_node(pos)
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
if node.name ~= "ignore" then
|
|
|
|
return node
|
|
|
|
end
|
|
|
|
minetest.get_voxel_manip():read_from_map(pos, pos)
|
|
|
|
return minetest.get_node(pos)
|
|
|
|
end
|
|
|
|
|
2020-09-09 10:35:44 +00:00
|
|
|
local function good_for_respawn(pos)
|
|
|
|
local node0 = get_far_node({x = pos.x, y = pos.y - 1, z = pos.z})
|
|
|
|
local node1 = get_far_node({x = pos.x, y = pos.y, z = pos.z})
|
|
|
|
local node2 = get_far_node({x = pos.x, y = pos.y + 1, z = pos.z})
|
|
|
|
local def0 = minetest.registered_nodes[node0.name]
|
|
|
|
local def1 = minetest.registered_nodes[node1.name]
|
|
|
|
local def2 = minetest.registered_nodes[node2.name]
|
|
|
|
return def0.walkable and (not def1.walkable) and (not def2.walkable) and
|
|
|
|
(def1.damage_per_second == nil or def2.damage_per_second <= 0) and
|
|
|
|
(def1.damage_per_second == nil or def2.damage_per_second <= 0)
|
|
|
|
end
|
|
|
|
|
2017-11-24 06:04:03 +00:00
|
|
|
-- Respawn player at specified respawn position
|
|
|
|
minetest.register_on_respawnplayer(function(player)
|
2019-02-07 18:54:12 +00:00
|
|
|
local pos, custom_spawn = mcl_spawn.get_spawn_pos(player)
|
2019-02-11 14:49:36 +00:00
|
|
|
if pos and custom_spawn then
|
2019-02-04 15:53:06 +00:00
|
|
|
-- Check if bed is still there
|
2019-02-28 14:35:18 +00:00
|
|
|
local node_bed = get_far_node(pos)
|
2019-02-04 15:53:06 +00:00
|
|
|
local bgroup = minetest.get_item_group(node_bed.name, "bed")
|
2020-09-09 10:35:44 +00:00
|
|
|
if bgroup ~= 1 and bgroup ~= 2 then
|
|
|
|
-- Bed is destroyed:
|
|
|
|
if player ~= nil and player:is_player() then
|
|
|
|
player:get_meta():set_string("mcl_beds:spawn", "")
|
2019-02-04 23:07:39 +00:00
|
|
|
end
|
2019-03-13 22:26:33 +00:00
|
|
|
minetest.chat_send_player(player:get_player_name(), S("Your spawn bed was missing or blocked."))
|
2020-09-09 10:35:44 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- Find spawning position on/near the bed free of solid or damaging blocks iterating a square spiral 15x15:
|
|
|
|
local x, z, dx, dz = 0, 0, 0, -1
|
|
|
|
for i = 1, 225 do
|
|
|
|
if x > -8 and x < 8 and z > -8 and z < 8 then
|
|
|
|
for _,y in ipairs({0, 1, -1, 2, -2, 3, -3, 4, -4}) do
|
|
|
|
local spawn_pos = {x = pos.x - z, y=pos.y + y, z = pos.z - x}
|
|
|
|
if good_for_respawn(spawn_pos) then
|
|
|
|
player:set_pos(spawn_pos)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if x == z or (x < 0 and x == -z) or (x > 0 and x == 1 - z) then
|
|
|
|
dx, dz = -dz, dx
|
|
|
|
end
|
|
|
|
x, z = x + dx, z + dz
|
2019-02-04 15:53:06 +00:00
|
|
|
end
|
2020-09-09 10:35:44 +00:00
|
|
|
-- We here if we didn't find suitable place for respawn:
|
|
|
|
return false
|
2017-11-24 06:04:03 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|