From 0d433daa238139a90ab5cf38da4430bad8b5d354 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 27 Aug 2019 06:42:14 +0200 Subject: [PATCH] Refuse to teleport outside of map --- README.md | 1 - init.lua | 22 +++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 683146f..e6f84ed 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ Version: 0.2.0 ## Known bugs and limitations - Will only search for biomes in the same height as you. -- Might teleport you outside the map boundaries if you're close to one. ## Authors - paramat (MIT License) diff --git a/init.lua b/init.lua index 06f87d0..f1c6a15 100644 --- a/init.lua +++ b/init.lua @@ -1,6 +1,12 @@ local S = minetest.get_translator("findbiome") -local mg_name = minetest.get_mapgen_setting("mg_name") + local mod_biomeinfo = minetest.get_modpath("biomeinfo") ~= nil +local mg_name = minetest.get_mapgen_setting("mg_name") + +-- Calculate the maximum playable limit +local mapgen_limit = tonumber(minetest.get_mapgen_setting("mapgen_limit")) +local chunksize = tonumber(minetest.get_mapgen_setting("chunksize")) +local playable_limit = math.max(mapgen_limit - (chunksize + 1) * 16, 0) -- Parameters ------------- @@ -22,6 +28,10 @@ local dirs = { {x = 1, y = 0, z = 0}, } +local function is_valid_pos(pos) + return math.abs(pos.x) > playable_limit or math.abs(pos.y) > playable_limit or math.abs(pos.z) > playable_limit +end + function find_biome(pos, biomes) pos = vector.round(pos) -- Pos: Starting point for biome checks. This also sets the y co-ordinate for all @@ -69,8 +79,9 @@ function find_biome(pos, biomes) local spawn_y = minetest.get_spawn_level(pos.x, pos.z) if spawn_y then spawn_pos = {x = pos.x, y = spawn_y, z = pos.z} - -- FIXME: Don't return true when spawn_pos is out of map bounds - return true + if is_valid_pos(spawn_pos) then + return true + end end end end @@ -95,8 +106,9 @@ function find_biome(pos, biomes) local spawn_y = minetest.get_spawn_level(pos.x, pos.z) if spawn_y then spawn_pos = {x = pos.x, y = spawn_y, z = pos.z} - -- FIXME: Don't return true when spawn_pos is out of map bounds - return true + if is_valid_pos(spawn_pos) then + return true + end end end end