cartographer/scanner.lua

224 lines
6.4 KiB
Lua
Raw Normal View History

2020-02-16 18:55:07 +01:00
local CHUNK_SIZE = _cartographer.CHUNK_SIZE;
2020-04-11 20:02:12 +02:00
-- Register a new tile in map data
-- x: The x position in map coordinates
-- y: The y position in map coordinates
-- biome: The tile's biome id
-- height: The tile's height
-- (Optional): manual_scan: Indicates if this was a 'manual' (non-generated)
-- scan. Manual scans are overridden by generated
-- scans under normal circumstances.
local function register_tile(x, y, biome, height, manual_scan)
2020-02-16 18:55:07 +01:00
if not data.generated[x] then
data.generated[x] = {
[y] = {
biome = biome,
height = height,
}
2020-02-16 18:55:07 +01:00
};
2020-04-11 20:02:12 +02:00
if manual_scan ~= nil then
data.generated[x][y].manual_scan = manual_scan;
end
2020-03-07 16:26:24 +01:00
elseif not data.generated[x][y] or data.generated[x][y].height < height then
data.generated[x][y] = {
biome = biome,
height = height,
};
2020-04-11 20:02:12 +02:00
if manual_scan ~= nil or data.generated[x][y].manual_scan then
data.generated[x][y].manual_scan = manual_scan;
end
2020-02-16 18:55:07 +01:00
end
end
local function get_mapgen_biome(min, max, mmin, mmax)
local UNDERGROUND = minetest.get_biome_id("underground");
local DEFAULT = minetest.get_biome_id("default");
local biomes = minetest.get_mapgen_object("biomemap");
local heights = minetest.get_mapgen_object("heightmap");
2020-02-16 18:55:07 +01:00
local xx = max.x - min.x;
local yy = max.y - min.y;
local zz = max.z - min.z;
local xxx = mmax.x - mmin.x;
local yyy = mmax.y - mmin.y;
local zzz = mmax.z - mmin.z;
local startx = min.x - mmin.x;
local starty = min.y - mmin.y;
local startz = min.z - mmin.z;
local scan_biomes = {};
local scan_heights = {};
2020-02-16 18:55:07 +01:00
for i = startx,startx + xx,1 do
for k = startz,startz + zz,1 do
local b = biomes[i + (k * (xxx + 1))];
2020-02-16 18:55:07 +01:00
if b ~= nil and b ~= UNDERGROUND and b ~= DEFAULT then
scan_biomes[b] = (scan_biomes[b] or 0) + 1;
scan_heights[b] = (scan_heights[b] or 0) + heights[i + (k * (xxx + 1))];
2020-02-16 18:55:07 +01:00
end
end
end
local biome = nil;
local high = 0;
for k,v in pairs(scan_biomes) do
if v > high then
biome = k;
high = v;
end
end
local avg_height = 0;
if high > 0 then
avg_height = scan_heights[biome] / high;
end
return biome, avg_height;
2020-02-16 18:55:07 +01:00
end
local function get_biome(min, max)
local UNDERGROUND = minetest.get_biome_id("underground");
local DEFAULT = minetest.get_biome_id("default");
local WATER_SOURCE = minetest.registered_aliases["mapgen_water_source"];
local xx = max.x - min.x;
local yy = max.y - min.y;
local zz = max.z - min.z;
local scan_biomes = {};
local scan_heights = {};
2020-02-16 18:55:07 +01:00
for i = min.x,max.x,1 do
for j = min.y,max.y,1 do
for k = min.z,max.z,1 do
local pos = { x=i, y=j, z=k };
local b = minetest.get_biome_data(pos).biome;
local node = minetest.get_node(pos).name;
if b ~= nil and b ~= UNDERGROUND and b ~= DEFAULT and node ~= "air" and node ~= WATER_SOURCE then
pos.y = pos.y + 1;
node = minetest.get_node(pos).name;
if node == "air" or node == WATER_SOURCE then
scan_biomes[b] = (scan_biomes[b] or 0) + 1;
scan_heights[b] = (scan_heights[b] or 0) + j;
2020-02-16 18:55:07 +01:00
end
end
end
end
end
local biome = nil;
local high = 0;
for k,v in pairs(scan_biomes) do
if v > high then
biome = k;
high = v;
end
end
local avg_height = 0;
if high > 0 then
avg_height = scan_heights[biome] / high;
end
return biome, avg_height;
2020-02-16 18:55:07 +01:00
end
local function on_generated(min, max, blockseed)
local chunk = {
x = tochunk(min.x),
y = tochunk(min.z),
};
for i = tochunk(min.x),tochunk(max.x),1 do
for j = tochunk(min.z),tochunk(max.z),1 do
local sub_min = {
x = i * CHUNK_SIZE,
y = min.y,
z = j * CHUNK_SIZE,
};
local sub_max = {
x = i * CHUNK_SIZE + CHUNK_SIZE,
y = max.y,
z = j * CHUNK_SIZE + CHUNK_SIZE,
};
local biome, height = get_mapgen_biome(sub_min, sub_max, min, max);
2020-02-16 18:55:07 +01:00
if biome ~= nil then
2020-04-11 20:02:12 +02:00
register_tile(i, j, biome, height)
2020-02-16 18:55:07 +01:00
end
end
end
end
2020-04-11 20:02:12 +02:00
-- Is the scan of this position already handled?
-- x: The x position, in map coordinates
-- y: The y position, in world coordinates
-- x: The z position, in map coordinates
--
-- Returns true if the position is handled by the current map data
local function is_scan_handled(x, y, z)
if not data.generated[x] then
return false;
end
local tile = data.generated[x][z];
return tile and ((not tile.manual_scan and tile.height > 0) or tile.height >= y);
end
-- Queue a tile for manual scanning
-- pos: The position as a table, in world coordinates
2020-02-16 18:55:07 +01:00
function cartographer.queue_region(pos)
local converted = {
2020-04-11 20:02:12 +02:00
x = fromchunk(tochunk(pos.x)),
y = fromchunk(tochunk(pos.y)),
z = fromchunk(tochunk(pos.z)),
2020-02-16 18:55:07 +01:00
};
2020-04-11 20:02:12 +02:00
if is_scan_handled(tochunk(pos.x), pos.y, tochunk(pos.z)) then
2020-02-16 18:55:07 +01:00
return;
end
for _,queued_pos in ipairs(cartographer.scan_queue) do
if vector.equals(converted, queued_pos) then
return;
end
end
cartographer.scan_queue[#cartographer.scan_queue + 1] = converted;
end
2020-04-11 20:02:12 +02:00
-- Scan the next tile on the queue, and remove it
2020-02-16 18:55:07 +01:00
function cartographer.scan_regions()
local len = #cartographer.scan_queue;
if len == 0 then
return;
end
2020-04-11 20:02:12 +02:00
local startpos = cartographer.scan_queue[1];
2020-02-16 18:55:07 +01:00
local endpos = {
x = startpos.x + CHUNK_SIZE,
y = startpos.y + CHUNK_SIZE,
z = startpos.z + CHUNK_SIZE,
};
2020-04-11 20:02:12 +02:00
if is_scan_handled(tochunk(startpos.x), startpos.y, tochunk(startpos.z)) then
table.remove(cartographer.scan_queue, 1);
2020-02-16 18:55:07 +01:00
return;
end
local biome,height = get_biome(startpos, endpos);
2020-02-16 18:55:07 +01:00
if biome ~= nil then
2020-04-11 20:02:12 +02:00
register_tile(tochunk(startpos.x), tochunk(startpos.z), biome, height, true)
2020-02-16 18:55:07 +01:00
end
2020-04-11 20:02:12 +02:00
table.remove(cartographer.scan_queue, 1);
2020-02-16 18:55:07 +01:00
end
minetest.register_on_generated(on_generated);