Make use of minetest.get_mapgen_edges

This commit is contained in:
Wuzzy 2024-01-11 19:35:38 +01:00
parent 9eb29b3dda
commit 26db2a0623

View File

@ -6,10 +6,22 @@ local mod_biomeinfo = minetest.get_modpath("biomeinfo") ~= nil
local mg_name = minetest.get_mapgen_setting("mg_name")
local water_level = tonumber(minetest.get_mapgen_setting("water_level"))
-- 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)
-- Calculate the playable area of the world
local playable_limit_min, playable_limit_max
if minetest.get_mapgen_edges then
-- Modern method by just asking Minetest
playable_limit_min, playable_limit_max = minetest.get_mapgen_edges()
else
-- Legacy method for older Minetest versions
-- by calculating an estimate ourself
-- (it's not perfect but close enough)
local BLOCKSIZE = 16
local mapgen_limit = tonumber(minetest.get_mapgen_setting("mapgen_limit"))
local chunksize = tonumber(minetest.get_mapgen_setting("chunksize"))
local limit_estimate = math.max(mapgen_limit - (chunksize + 1) * BLOCKSIZE, 0)
playable_limit_min = vector.new(-limit_estimate, -limit_estimate, -limit_estimate)
playable_limit_max = vector.new(limit_estimate, limit_estimate, limit_estimate)
end
-- Parameters
-------------
@ -33,7 +45,8 @@ local dirs = {
-- Returns true if pos is within the world boundaries
local function is_in_world(pos)
return not (math.abs(pos.x) > playable_limit or math.abs(pos.y) > playable_limit or math.abs(pos.z) > playable_limit)
return not (pos.x < playable_limit_min.x or pos.y < playable_limit_min.y or pos.z < playable_limit_min.z or
pos.x > playable_limit_max.x or pos.y > playable_limit_max.y or pos.z > playable_limit_max.z)
end
-- Checks if pos is within the biome's boundaries. If it isn't, places pos inside the boundaries.
@ -53,12 +66,12 @@ local function adjust_pos_to_biome_limits(pos, biome_id)
if biome[ax.."_min"] then
min = biome[ax.."_min"]
else
min = -playable_limit
min = playable_limit_min[ax]
end
if biome[ax.."_max"] then
max = biome[ax.."_max"]
else
max = playable_limit
max = playable_limit_max[ax]
end
min = tonumber(min)
max = tonumber(max)
@ -66,14 +79,14 @@ local function adjust_pos_to_biome_limits(pos, biome_id)
out_of_bounds = true
bpos[ax] = min
if max-min > 16 then
bpos[ax] = math.max(bpos[ax] + 8, -playable_limit)
bpos[ax] = math.max(bpos[ax] + 8, playable_limit_min[ax])
end
end
if bpos[ax] > max then
out_of_bounds = true
bpos[ax] = max
if max-min > 16 then
bpos[ax] = math.min(bpos[ax] - 8, playable_limit)
bpos[ax] = math.min(bpos[ax] - 8, playable_limit_max[ax])
end
end
end
@ -99,9 +112,9 @@ local function find_default_biome()
-- Just check a lot of random positions
-- It's a crappy algorithm but better than nothing.
for i=1, 100 do
pos.x = math.random(-playable_limit, playable_limit)
pos.y = math.random(-playable_limit, playable_limit)
pos.z = math.random(-playable_limit, playable_limit)
pos.x = math.random(playable_limit_min.x, playable_limit_max.x)
pos.y = math.random(playable_limit_min.y, playable_limit_max.y)
pos.z = math.random(playable_limit_min.z, playable_limit_max.z)
local biome_data = minetest.get_biome_data(pos)
if biome_data and minetest.get_biome_name(biome_data.biome) == "default" then
return pos