Fix coordinates being handled incorrectly in some places

This commit is contained in:
Hugues Ross 2020-06-13 11:50:43 -04:00
parent 826d1a9a8e
commit c0e541a7e7
3 changed files with 41 additions and 31 deletions

View File

@ -116,7 +116,7 @@ local function show_map_formspec(map, player_x, player_z, player_name, height_mo
height_mode = height_mode, height_mode = height_mode,
}; };
player_x, player_z = map:to_coordinates(player_x, player_z) player_x, player_z = map:to_coordinates(player_x, player_z, true);
local formspec, formspec_width, _ = map_formspec.from_map(map, player_x, player_z, height_mode); local formspec, formspec_width, _ = map_formspec.from_map(map, player_x, player_z, height_mode);
local height_button_texture; local height_button_texture;
if height_mode then if height_mode then
@ -181,8 +181,8 @@ end
-- Get the description text for a map ID and dimensions -- Get the description text for a map ID and dimensions
-- --
-- id: The map ID -- id: The map ID
-- from_x: The x coordinate of the top-left corner of the map, in world coordinates -- from_x: The x coordinate of the top-left corner of the map, in map coordinates
-- from_z: The z coordinate of the top-left corner of the map, in world coordinates -- from_z: The z coordinate of the top-left corner of the map, in map coordinates
-- w: The width, in world coordinates -- w: The width, in world coordinates
-- h: The height, in world coordinates -- h: The height, in world coordinates
-- --
@ -191,7 +191,7 @@ local function map_description(id, from_x, from_z, w, h)
return string.format("Map #%d\n[%d,%d] - [%d,%d]", return string.format("Map #%d\n[%d,%d] - [%d,%d]",
id, id,
chunk.from(from_x), chunk.from(from_z), chunk.from(from_x), chunk.from(from_z),
chunk.from(from_x + w), chunk.from(from_z + h)); chunk.from(from_x + w + 1), chunk.from(from_z + h + 1));
end end
-- Create a map from metadata, and assign the ID to the metadata -- Create a map from metadata, and assign the ID to the metadata
@ -208,8 +208,8 @@ local function map_from_meta(meta, player_x, player_z)
local total_size = size * scale; local total_size = size * scale;
local map_x = math.floor((player_x + 10)/total_size) * total_size - 10 local map_x = math.floor(player_x / total_size) * total_size;
local map_y = math.floor((player_z + 10)/total_size) * total_size - 10; local map_y = math.floor(player_z / total_size) * total_size;
local id = maps.create(map_x, map_y, size, size, false, detail, scale); local id = maps.create(map_x, map_y, size, size, false, detail, scale);

View File

@ -31,9 +31,9 @@ end
-- w: The width, in map coordinates -- w: The width, in map coordinates
-- h: The height, in map coordinates -- h: The height, in map coordinates
function Map.fill_area(self, x, z, w, h) function Map.fill_area(self, x, z, w, h)
for i = math.max(x, self.x),math.min(x + w - 1, self.x + self.w),1 do for i = math.max(x, 0),math.min(x + w - 1, self.w),1 do
for j = math.max(z, self.z),math.min(z + h - 1, self.z + self.h),1 do for j = math.max(z, 0),math.min(z + h - 1, self.h),1 do
self.fill[(i - self.x) + ((j - self.z) * self.w)] = self.detail; self.fill[i + (j * self.w)] = self.detail;
end end
end end
end end
@ -77,10 +77,10 @@ end
-- x: The x position, in world coordinates -- x: The x position, in world coordinates
-- z: The z position, in world coordinates -- z: The z position, in world coordinates
function Map.fill_local(self, x, z) function Map.fill_local(self, x, z)
x, z = self:to_coordinates(x, z); x, z = self:to_coordinates(x, z, true);
-- TODO: Adjust size to match map scale -- TODO: Adjust size to match map scale
if x >= self.x - 2 and x <= self.x + self.w + 1 and z >= self.z - 2 and z <= self.z + self.h + 1 then if x >= -2 and x <= self.w + 1 and z >= -2 and z <= self.h + 1 then
self:fill_area(x - 2, z - 2, 5, 5); self:fill_area(x - 2, z - 2, 5, 5);
end end
end end
@ -89,14 +89,22 @@ end
-- --
-- x: The x position, in world coordinates -- x: The x position, in world coordinates
-- z: The z position, in world coordinates -- z: The z position, in world coordinates
-- (Optional) relative: When true, the coordinates are relative to this map's
-- position.
-- --
-- Returns The converted x and z coordinates -- Returns The converted x and z coordinates
function Map.to_coordinates(self, x, z) function Map.to_coordinates(self, x, z, relative)
if self.scale == 0 then if self.scale == 0 then
return chunk.to(x), chunk.to(z); return chunk.to(x), chunk.to(z);
end end
return math.floor(chunk.to(x) / self.scale + 0.5), math.floor(chunk.to(z) / self.scale + 0.5); if relative then
return math.floor((chunk.to(x) - self.x) / self.scale + 0.5),
math.floor((chunk.to(z) - self.z) / self.scale + 0.5);
else
return math.floor(chunk.to(x) / self.scale + 0.5),
math.floor(chunk.to(z) / self.scale + 0.5);
end
end end
-- Check if the given position on this map is filled -- Check if the given position on this map is filled
@ -137,8 +145,8 @@ local maps = {
setmetatable(map, Map); setmetatable(map, Map);
map_data.maps[id] = map; map_data.maps[id] = map;
if filled then if filled or true then
map:fill(map, x, z, w, h); map:fill_area(0, 0, w, h);
end end
map_data.next_map_id = map_data.next_map_id + 1; map_data.next_map_id = map_data.next_map_id + 1;

View File

@ -45,8 +45,8 @@ end
-- Generate formspec markup for a given map -- Generate formspec markup for a given map
-- --
-- x: The X position of the map (in map coordinates) -- x: The X position of the map (in relative map coordinates)
-- y: The Z position of the map (in map coordinates) -- y: The Z position of the map (in relative map coordinates)
-- w: The width of the map (in map coordinates) -- w: The width of the map (in map coordinates)
-- h: The height of the map (in map coordinates) -- h: The height of the map (in map coordinates)
-- player_x: The X position of the player marker (in map coordinates) -- player_x: The X position of the player marker (in map coordinates)
@ -64,12 +64,14 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h
local str = ""; local str = "";
local noise = PerlinNoiseMap(MAP_NOISE, { x=w + 1, y=h + 1, z=1}):get_2d_map({ x=x, y=y}); local noise = PerlinNoiseMap(MAP_NOISE, { x=w + 1, y=h + 1, z=1}):get_2d_map({ x=x, y=y});
for i = x,x + w,1 do for i = 0,w,1 do
local fx = (i - x) * TILE_OFFSET; local world_i = x + (i * map_scale);
local column = map_data.generated[i * map_scale]; local fx = i * TILE_OFFSET;
for j = y + h,y,-1 do local column = map_data.generated[world_i];
local fy = (y + h - j) * TILE_OFFSET; for j = h,0,-1 do
if column == nil or column[j * map_scale] == nil or (is_visible and not is_visible(..., i, j)) then local world_j = y + (j * map_scale);
local fy = (h - j) * TILE_OFFSET;
if column == nil or column[world_j] == nil or (is_visible and not is_visible(..., x + i, y + j)) then
local unknown_tex = util.get_clamped(skin.unknown_biome_textures, detail); local unknown_tex = util.get_clamped(skin.unknown_biome_textures, detail);
str = str .. gui.image { str = str .. gui.image {
x = fx, x = fx,
@ -77,11 +79,11 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h
w = TILE_SIZE, w = TILE_SIZE,
h = TILE_SIZE, h = TILE_SIZE,
image = get_variant(unknown_tex, i - x, j - y, noise), image = get_variant(unknown_tex, i, j, noise),
}; };
else else
local name = minetest.get_biome_name(column[j * map_scale].biome); local name = minetest.get_biome_name(column[world_j].biome);
local height = column[j * map_scale].height; local height = column[world_j].height;
local biome = biomes.get_texture(name, math.floor(height + 0.5), detail); local biome = biomes.get_texture(name, math.floor(height + 0.5), detail);
if biome then if biome then
@ -115,7 +117,7 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h
w = TILE_SIZE, w = TILE_SIZE,
h = TILE_SIZE, h = TILE_SIZE,
image = get_variant(biome, i - x, j - y, noise) .. mod, image = get_variant(biome, i, j, noise) .. mod,
}; };
if get_marker then if get_marker then
@ -151,7 +153,7 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h
w = TILE_SIZE, w = TILE_SIZE,
h = TILE_SIZE, h = TILE_SIZE,
image = get_variant(unknown_tex, i - x, j - y, noise), image = get_variant(unknown_tex, i, j, noise),
}; };
end end
end end
@ -182,15 +184,15 @@ function map_formspec.from_coords(x, y, w, h, detail, scale, height_mode)
w = formspec_width, w = formspec_width,
h = formspec_height, h = formspec_height,
generate_map(x - (w * 0.5), y - (h * 0.5), w, h, x, y, detail, scale, height_mode), generate_map(x - (w * 0.5), y - (h * 0.5), w, h, w * 0.5, h * 0.5, detail, scale, height_mode),
}, formspec_width, formspec_height; }, formspec_width, formspec_height;
end end
-- Get the formspec for a given map table -- Get the formspec for a given map table
-- --
-- map: The map to use -- map: The map to use
-- x: The X position of the player marker, in map coordinates -- x: The X position of the player marker, in relative map coordinates
-- y: The Y position of the player marker, in map coordinates -- y: The Y position of the player marker, in relative map coordinates
-- height_mode: If true, displaces tiles by their height -- height_mode: If true, displaces tiles by their height
-- --
-- Returns a formspec string, the width of the formspec, and the height of the -- Returns a formspec string, the width of the formspec, and the height of the