OO-ifiy map tables

This commit is contained in:
Hugues Ross 2020-06-09 20:17:39 -04:00
parent 490104f646
commit 43406ded7e
4 changed files with 54 additions and 42 deletions

View File

@ -125,7 +125,7 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_
}; };
local map = cartographer.get_map(id); local map = cartographer.get_map(id);
player_x, player_z = cartographer.to_map_coordinates(map, player_x, player_z) player_x, player_z = map:to_coordinates(player_x, player_z)
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);
if #marker_lookup > 0 then if #marker_lookup > 0 then
local height_button_texture; local height_button_texture;
@ -147,7 +147,7 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_
y = 1, y = 1,
bg = skin.marker_bg, bg = skin.marker_bg,
marker_formspec(cartographer.get_marker(map, player_x, player_z), map.detail, marker_page or 1), marker_formspec(map:get_marker(player_x, player_z), map.detail, marker_page or 1),
}, },
gui.container { gui.container {
x = formspec_width - 0.01, x = formspec_width - 0.01,
@ -245,8 +245,8 @@ minetest.register_on_player_receive_fields(function(player, name, fields)
local marker = k:match("marker%-(.+)"); local marker = k:match("marker%-(.+)");
local pos = player:get_pos(); local pos = player:get_pos();
if marker or k == "clear_marker" then if marker or k == "clear_marker" then
local player_x, player_z = cartographer.to_map_coordinates(map, pos.x, pos.z); local player_x, player_z = map:to_coordinates(pos.x, pos.z);
cartographer.set_marker(map, player_x, player_z, marker); map:set_marker(player_x, player_z, marker);
cartographer.map_sound("cartographer_write", player); cartographer.map_sound("cartographer_write", player);
show_map_id_formspec(map.id, pos.x, pos.z, player:get_player_name(), data.page); show_map_id_formspec(map.id, pos.x, pos.z, player:get_player_name(), data.page);
@ -404,7 +404,7 @@ function cartographer.resize_map_item(meta, size)
local id = meta:get_int("cartographer:map_id"); local id = meta:get_int("cartographer:map_id");
if id > 0 then if id > 0 then
local map = cartographer.get_map(id); local map = cartographer.get_map(id);
cartographer.resize_map(id, size, size); map:resize(size, size);
meta:set_string("description", map_description(id, meta:set_string("description", map_description(id,
chunk.from(map.x), chunk.from(map.z), chunk.from(map.x), chunk.from(map.z),

View File

@ -1,5 +1,12 @@
local map_data, chunk, scanner, biome_lookup, marker_lookup = ...; local map_data, chunk, scanner, biome_lookup, marker_lookup = ...;
local Map = {};
Map.__index = Map;
for _,loaded_map in ipairs(map_data.maps) do
setmetatable(loaded_map, Map);
end
function cartographer.create_map(x, z, w, h, filled, detail, scale) function cartographer.create_map(x, z, w, h, filled, detail, scale)
local id = map_data.next_map_id; local id = map_data.next_map_id;
@ -14,10 +21,11 @@ function cartographer.create_map(x, z, w, h, filled, detail, scale)
fill = {}, fill = {},
markers = {}, markers = {},
}; };
setmetatable(map, Map);
map_data.maps[id] = map; map_data.maps[id] = map;
if filled then if filled then
cartographer.fill(map, x, z, w, h); map:fill(map, x, z, 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;
@ -29,46 +37,42 @@ function cartographer.get_map(id)
return map_data.maps[id]; return map_data.maps[id];
end end
function cartographer.resize_map(id, w, h) function Map.resize(self, w, h)
local map = cartographer.get_map(id); if w >= self.w and h >= self.h then
if map ~= nil and w >= map.w and h >= map.h then self.w = w;
map.w = w; self.h = h;
map.h = h; -- FIXME: Is this really correct? Seems questionable.
end end
end end
function cartographer.fill(map, x, z, w, h) function Map.fill_area(self, x, z, w, h)
if map == nil then for i = math.max(x, self.x),math.min(x + w - 1, self.x + self.w),1 do
return; for j = math.max(z, self.z),math.min(z + h - 1, self.z + self.h),1 do
end self.fill[(i - self.x) + ((j - self.z) * self.w)] = self.detail;
for i = math.max(x, map.x),math.min(x + w - 1, map.x + map.w),1 do
for j = math.max(z, map.z),math.min(z + h - 1, map.z + map.h),1 do
map.fill[(i - map.x) + ((j - map.z) * map.w)] = map.detail;
end end
end end
end end
function cartographer.set_marker(map, x, z, marker) function Map.set_marker(self, x, z, marker)
if map == nil or x < map.x or x > map.x + map.w or z < map.z or z > map.z + map.h then if x < self.x or x > self.x + self.w or z < self.z or z > self.z + self.h then
return; return;
end end
if not map.markers[x] then if not self.markers[x] then
map.markers[x] = { self.markers[x] = {
[z] = marker, [z] = marker,
}; };
else else
map.markers[x][z] = marker; self.markers[x][z] = marker;
end end
end end
function cartographer.get_marker(map, x, z) function Map.get_marker(self, x, z)
if map == nil or x < map.x or x > map.x + map.w or z < map.z or z > map.z + map.h or not map.markers[x] then if x < self.x or x > self.x + self.w or z < self.z or z > self.z + self.h or not self.markers[x] then
return nil; return nil;
end end
return map.markers[x][z]; return self.markers[x][z];
end end
-- Fill in the local area of a map around a position -- Fill in the local area of a map around a position
@ -78,26 +82,30 @@ end
function cartographer.fill_local(id, x, z) function cartographer.fill_local(id, x, z)
local map = cartographer.get_map(id); local map = cartographer.get_map(id);
x, z = cartographer.to_map_coordinates(map, x, z); if not map then
return;
end
x, z = map:to_coordinates(x, z);
-- TODO: Adjust size to match map scale -- TODO: Adjust size to match map scale
if map and x >= map.x - 2 and x <= map.x + map.w + 1 and z >= map.z - 2 and z <= map.z + map.h + 1 then if x >= map.x - 2 and x <= map.x + map.w + 1 and z >= map.z - 2 and z <= map.z + map.h + 1 then
cartographer.fill(map, x - 2, z - 2, 5, 5); map:fill_area(x - 2, z - 2, 5, 5);
end end
end end
-- Convert a position in world coordinates to the given map's coordinate system -- Convert a position in world coordinates to the given map's coordinate system
-- map: The map to use as reference, or nil to use the default size (map scale of 1) -- self: The map to use as reference
-- 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
-- --
-- Returns The converted x and z coordinates -- Returns The converted x and z coordinates
function cartographer.to_map_coordinates(map, x, z) function Map.to_coordinates(self, x, z)
if not map or map.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) / map.scale + 0.5), math.floor(chunk.to(z) / map.scale + 0.5); return math.floor(chunk.to(x) / self.scale + 0.5), math.floor(chunk.to(z) / self.scale + 0.5);
end end
-- Periodically-called function to fill in maps and queue chunks for manual -- Periodically-called function to fill in maps and queue chunks for manual
@ -205,12 +213,8 @@ function cartographer.register_marker(id, name, textures)
end end
end end
function cartographer.is_filled(map, x, z) function Map.is_filled(self, x, z)
if map == nil then return self.fill[(x - self.x) + ((z - self.z) * self.w)] ~= nil;
return false;
end
return map.fill[(x - map.x) + ((z - map.z) * map.w)] ~= nil;
end end
-- Get an entry from a list for a given detail level -- Get an entry from a list for a given detail level

View File

@ -18,6 +18,14 @@ local MAP_NOISE = {
flags = "defaults, absvalue", flags = "defaults, absvalue",
}; };
local function map_is_filled(map, x, y)
return map:is_filled(x, y);
end
local function map_get_marker(map, x, y)
return map:get_marker(x, y);
end
-- Get the variant of the tile at a given position -- Get the variant of the tile at a given position
-- prefix: The part of the tile texture name before the variant -- prefix: The part of the tile texture name before the variant
-- x: The X position of the tile (in map coordinates) -- x: The X position of the tile (in map coordinates)
@ -185,7 +193,7 @@ function map_formspec.from_map(map, x, y, height_mode)
w = formspec_width, w = formspec_width,
h = formspec_height, h = formspec_height,
generate_map(map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, height_mode, cartographer.is_filled, cartographer.get_marker, map), generate_map(map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, height_mode, map_is_filled, map_get_marker, map),
}, formspec_width, formspec_height; }, formspec_width, formspec_height;
end end

View File

@ -94,7 +94,7 @@ end
-- stack: The itemstack to convert -- stack: The itemstack to convert
-- --
-- Returns a table with the material values -- Returns a table with the material values
function get_material_value(stack) local function get_material_value(stack)
local item_name = stack:get_name(); local item_name = stack:get_name();
local item_count = stack:get_count(); local item_count = stack:get_count();