From c0e541a7e73244540d73b7fede8eedde800530f6 Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Sat, 13 Jun 2020 11:50:43 -0400 Subject: [PATCH] Fix coordinates being handled incorrectly in some places --- items.lua | 12 ++++++------ map_api.lua | 26 +++++++++++++++++--------- map_formspec.lua | 34 ++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/items.lua b/items.lua index ad05968..18d7fd5 100644 --- a/items.lua +++ b/items.lua @@ -116,7 +116,7 @@ local function show_map_formspec(map, player_x, player_z, player_name, height_mo 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 height_button_texture; if height_mode then @@ -181,8 +181,8 @@ end -- Get the description text for a map ID and dimensions -- -- id: The map ID --- from_x: The x 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 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 map coordinates -- w: The width, 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]", id, 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 -- 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 map_x = math.floor((player_x + 10)/total_size) * total_size - 10 - local map_y = math.floor((player_z + 10)/total_size) * total_size - 10; + local map_x = math.floor(player_x / total_size) * total_size; + local map_y = math.floor(player_z / total_size) * total_size; local id = maps.create(map_x, map_y, size, size, false, detail, scale); diff --git a/map_api.lua b/map_api.lua index 43cdc55..af076f4 100644 --- a/map_api.lua +++ b/map_api.lua @@ -31,9 +31,9 @@ end -- w: The width, in map coordinates -- h: The height, in map coordinates 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 j = math.max(z, self.z),math.min(z + h - 1, self.z + self.h),1 do - self.fill[(i - self.x) + ((j - self.z) * self.w)] = self.detail; + for i = math.max(x, 0),math.min(x + w - 1, self.w),1 do + for j = math.max(z, 0),math.min(z + h - 1, self.h),1 do + self.fill[i + (j * self.w)] = self.detail; end end end @@ -77,10 +77,10 @@ end -- x: The x position, in world coordinates -- z: The z position, in world coordinates 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 - 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); end end @@ -89,14 +89,22 @@ end -- -- x: The x 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 -function Map.to_coordinates(self, x, z) +function Map.to_coordinates(self, x, z, relative) if self.scale == 0 then return chunk.to(x), chunk.to(z); 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 -- Check if the given position on this map is filled @@ -137,8 +145,8 @@ local maps = { setmetatable(map, Map); map_data.maps[id] = map; - if filled then - map:fill(map, x, z, w, h); + if filled or true then + map:fill_area(0, 0, w, h); end map_data.next_map_id = map_data.next_map_id + 1; diff --git a/map_formspec.lua b/map_formspec.lua index bd48ff6..326b913 100644 --- a/map_formspec.lua +++ b/map_formspec.lua @@ -45,8 +45,8 @@ end -- Generate formspec markup for a given map -- --- x: The X position of the map (in map coordinates) --- y: The Z 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 relative map coordinates) -- w: The width 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) @@ -64,12 +64,14 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h local str = ""; 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 - local fx = (i - x) * TILE_OFFSET; - local column = map_data.generated[i * map_scale]; - for j = y + h,y,-1 do - local fy = (y + h - j) * TILE_OFFSET; - if column == nil or column[j * map_scale] == nil or (is_visible and not is_visible(..., i, j)) then + for i = 0,w,1 do + local world_i = x + (i * map_scale); + local fx = i * TILE_OFFSET; + local column = map_data.generated[world_i]; + for j = h,0,-1 do + 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); str = str .. gui.image { 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, h = TILE_SIZE, - image = get_variant(unknown_tex, i - x, j - y, noise), + image = get_variant(unknown_tex, i, j, noise), }; else - local name = minetest.get_biome_name(column[j * map_scale].biome); - local height = column[j * map_scale].height; + local name = minetest.get_biome_name(column[world_j].biome); + local height = column[world_j].height; local biome = biomes.get_texture(name, math.floor(height + 0.5), detail); 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, 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 @@ -151,7 +153,7 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h w = 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 @@ -182,15 +184,15 @@ function map_formspec.from_coords(x, y, w, h, detail, scale, height_mode) w = formspec_width, 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; end -- Get the formspec for a given map table -- -- map: The map to use --- x: The X position of the player marker, in map coordinates --- y: The Y 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 relative map coordinates -- height_mode: If true, displaces tiles by their height -- -- Returns a formspec string, the width of the formspec, and the height of the