From 5c3e4b1d20ca2584b4930cd05989e680425717f9 Mon Sep 17 00:00:00 2001
From: Paramat <paramat@users.noreply.github.com>
Date: Wed, 28 Aug 2019 03:19:34 +0100
Subject: [PATCH] Spawn: Avoid spawning outside small worlds

Previously, the value of 'mapgen_limit' was not used to limit the
spawn position.
If a friendly-biome spawn point is not found within a small world,
spawn point falls back to the engine spawn point, which has a larger
chance of success.
---
 mods/spawn/init.lua | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/mods/spawn/init.lua b/mods/spawn/init.lua
index 6707932c..7bbec86e 100644
--- a/mods/spawn/init.lua
+++ b/mods/spawn/init.lua
@@ -56,6 +56,15 @@ local success = false
 local spawn_pos = {}
 
 
+-- Get world 'mapgen_limit' and 'chunksize' to calculate 'spawn_limit'.
+-- This accounts for how mapchunks are not generated if they or their shell exceed
+-- 'mapgen_limit'.
+
+local mapgen_limit = tonumber(minetest.get_mapgen_setting("mapgen_limit"))
+local chunksize = tonumber(minetest.get_mapgen_setting("chunksize"))
+local spawn_limit = math.max(mapgen_limit - (chunksize + 1) * 16, 0)
+
+
 --Functions
 -----------
 
@@ -100,6 +109,10 @@ local function search()
 		end
 
 		pos = next_pos()
+		-- Check for position being outside world edge
+		if math.abs(pos.x) > spawn_limit or math.abs(pos.z) > spawn_limit then
+			return false
+		end
 	end
 
 	return false
-- 
GitLab