scanner.lua: Parameterize coordinate conversion methods

This commit is contained in:
Hugues Ross 2020-06-07 18:20:35 -04:00
parent c26265e2c3
commit 28360f585c
2 changed files with 27 additions and 21 deletions

View File

@ -49,10 +49,15 @@ function fromchunk(coord)
return math.floor(coord * _cartographer.CHUNK_SIZE);
end
local chunk = {
to = tochunk,
from = fromchunk,
};
-- Includes
cartographer.skin = loadfile(modpath .. "/skin_api.lua") ();
cartographer.gui = loadfile(modpath .. "/formspec.lua") ();
loadfile(modpath .. "/scanner.lua") (map_data);
loadfile(modpath .. "/scanner.lua") (map_data, chunk);
loadfile(modpath .. "/map_api.lua") ();
loadfile(modpath .. "/items.lua") ();
_cartographer.generate_marker_formspec = loadfile(modpath .. "/marker_formspec.lua") (_cartographer.marker_lookup, cartographer.gui);

View File

@ -1,9 +1,6 @@
-- Arguments
-- map_data: The cartographer map data table
local map_data = ...;
-- Constants
local CHUNK_SIZE = _cartographer.CHUNK_SIZE;
local map_data, chunk = ...;
-- Register a new tile in map data
-- x: The x position in map coordinates
@ -49,7 +46,7 @@ local function get_mapgen_biome(min, max, mmin, mmax)
local startx = min.x - mmin.x;
local startz = min.z - mmin.z;
local scan_biomes = {};
local scan_heights = {};
@ -124,18 +121,18 @@ local function get_biome(min, max)
end
local function on_generated(min, max, _)
for i = tochunk(min.x),tochunk(max.x),1 do
for j = tochunk(min.z),tochunk(max.z),1 do
for i = chunk.to(min.x),chunk.to(max.x),1 do
for j = chunk.to(min.z),chunk.to(max.z),1 do
local sub_min = {
x = i * CHUNK_SIZE,
x = chunk.from(i),
y = min.y,
z = j * CHUNK_SIZE,
z = chunk.from(j),
};
local sub_max = {
x = i * CHUNK_SIZE + CHUNK_SIZE,
x = chunk.from(i + 1),
y = max.y,
z = j * CHUNK_SIZE + CHUNK_SIZE,
z = chunk.from(j + 1),
};
local biome, height = get_mapgen_biome(sub_min, sub_max, min, max);
if biome ~= nil then
@ -166,12 +163,12 @@ end
-- pos: The position as a table, in world coordinates
function cartographer.queue_region(pos)
local converted = {
x = fromchunk(tochunk(pos.x)),
y = fromchunk(tochunk(pos.y)),
z = fromchunk(tochunk(pos.z)),
x = chunk.from(chunk.to(pos.x)),
y = chunk.from(chunk.to(pos.y)),
z = chunk.from(chunk.to(pos.z)),
};
if is_scan_handled(tochunk(pos.x), pos.y, tochunk(pos.z)) then
if is_scan_handled(chunk.to(pos.x), pos.y, chunk.to(pos.z)) then
return;
end
@ -193,20 +190,24 @@ function cartographer.scan_regions()
end
local startpos = cartographer.scan_queue[1];
local chunk_x = chunk.to(startpos.x);
local chunk_y = chunk.to(startpos.y);
local chunk_z = chunk.to(startpos.z);
local endpos = {
x = startpos.x + CHUNK_SIZE,
y = startpos.y + CHUNK_SIZE,
z = startpos.z + CHUNK_SIZE,
x = chunk.from(chunk_x + 1),
y = chunk.from(chunk_y + 1),
z = chunk.from(chunk_z + 1),
};
if is_scan_handled(tochunk(startpos.x), startpos.y, tochunk(startpos.z)) then
if is_scan_handled(chunk_x, startpos.y, chunk_z) then
table.remove(cartographer.scan_queue, 1);
return;
end
local biome,height = get_biome(startpos, endpos);
if biome ~= nil then
register_tile(tochunk(startpos.x), tochunk(startpos.z), biome, height, true)
register_tile(chunk_x, chunk_z, biome, height, true)
end
table.remove(cartographer.scan_queue, 1);