3 -- Disable by mapgen, setting or if 'static_spawnpoint' is set
4 --------------------------------------------------------------
6 local mg_name = minetest.get_mapgen_setting("mg_name")
7 if mg_name == "v6" or mg_name == "singlenode" or
8 minetest.settings:get("static_spawnpoint") or
9 minetest.settings:get_bool("engine_spawn") then
17 -- Resolution of search grid in nodes.
19 -- Number of points checked in the square search grid (edge * edge).
20 local checks = 128 * 128
21 -- Starting point for biome checks. This also sets the y co-ordinate for all
22 -- points checked, so the suitable biomes must be active at this y.
23 local pos = {x = 0, y = 8, z = 0}
26 -- Table of suitable biomes
29 minetest.get_biome_id("taiga"),
30 minetest.get_biome_id("coniferous_forest"),
31 minetest.get_biome_id("deciduous_forest"),
32 minetest.get_biome_id("grassland"),
33 minetest.get_biome_id("savanna"),
43 {x = 0, y = 0, z = 1},
44 {x = -1, y = 0, z = 0},
45 {x = 0, y = 0, z = -1},
46 {x = 1, y = 0, z = 0},
56 local searched = false
61 -- Get world 'mapgen_limit' and 'chunksize' to calculate 'spawn_limit'.
62 -- This accounts for how mapchunks are not generated if they or their shell exceed
65 local mapgen_limit = tonumber(minetest.get_mapgen_setting("mapgen_limit"))
66 local chunksize = tonumber(minetest.get_mapgen_setting("chunksize"))
67 local spawn_limit = math.max(mapgen_limit - (chunksize + 1) * 16, 0)
73 -- Get next position on square search spiral
75 local function next_pos()
76 if edge_dist == edge_len then
82 dir_step = dir_step + 1
83 edge_len = math.floor(dir_step / 2) + 1
86 local dir = dirs[dir_ind]
87 local move = vector.multiply(dir, res)
89 edge_dist = edge_dist + 1
91 return vector.add(pos, move)
95 -- Spawn position search
97 local function search()
98 for iter = 1, checks do
99 local biome_data = minetest.get_biome_data(pos)
100 -- Sometimes biome_data is nil
101 local biome = biome_data and biome_data.biome
102 for id_ind = 1, #biome_ids do
103 local biome_id = biome_ids[id_ind]
104 if biome == biome_id then
105 local spawn_y = minetest.get_spawn_level(pos.x, pos.z)
107 spawn_pos = {x = pos.x, y = spawn_y, z = pos.z}
114 -- Check for position being outside world edge
115 if math.abs(pos.x) > spawn_limit or math.abs(pos.z) > spawn_limit then
124 -- On new player spawn and player respawn
126 -- Search for spawn position once per server session. If successful, store
127 -- position and reposition players, otherwise leave them at engine spawn
130 local function on_spawn(player)
136 player:set_pos(spawn_pos)
141 minetest.register_on_newplayer(function(player)
145 local enable_bed_respawn = minetest.settings:get_bool("enable_bed_respawn")
146 if enable_bed_respawn == nil then
147 enable_bed_respawn = true
150 minetest.register_on_respawnplayer(function(player)
151 -- Avoid respawn conflict with beds mod
152 if beds and enable_bed_respawn and
153 beds.spawn[player:get_player_name()] then
157 return on_spawn(player)