Refactor map creation / indexing into api object

This commit is contained in:
Hugues Ross 2020-06-10 19:29:06 -04:00
parent 30249edf44
commit a2e6597763
4 changed files with 53 additions and 48 deletions

View File

@ -53,11 +53,12 @@ local audio = loadfile(modpath .. "/audio.lua") ();
local biomes = loadfile(modpath .. "/biome_api.lua") ();
local scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk);
loadfile(modpath .. "/map_api.lua") (map_data, chunk, scanner, marker_lookup);
local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk, scanner, marker_lookup);
local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, biomes);
loadfile(modpath .. "/items.lua") (chunk, marker_lookup, gui, skin, audio, map_formspec);
loadfile(modpath .. "/items.lua") (chunk, marker_lookup, gui, skin, audio, maps, map_formspec);
loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec);
loadfile(modpath .. "/table.lua") (gui, skin, audio);
loadfile(modpath .. "/table.lua") (gui, skin, audio, maps);
cartographer.skin = skin;
cartographer.biomes = biomes;
cartographer.maps = maps;

View File

@ -1,4 +1,4 @@
local chunk, marker_lookup, gui, skin, audio, map_formspec = ...;
local chunk, marker_lookup, gui, skin, audio, maps, map_formspec = ...;
-- The list of players looking at maps, and the map IDs that they're looking at
local player_maps = {};
@ -116,7 +116,7 @@ end
-- height_mode: Whether or not to display the map in height mode
-- (Optional) marker_page: The current page that the marker editor is on
local function show_map_id_formspec(id, player_x, player_z, player_name, height_mode, marker_page)
local map = cartographer.get_map(id);
local map = maps.get(id);
if not map then
return
end
@ -204,7 +204,7 @@ local function map_from_meta(meta, player_x, player_z)
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 id = cartographer.create_map(map_x, map_y, size, size, false, detail, scale);
local id = maps.create(map_x, map_y, size, size, false, detail, scale);
meta:set_int("cartographer:map_id", id);
meta:set_string("description", map_description(id, map_x, map_y, total_size, total_size));
@ -240,7 +240,7 @@ minetest.register_on_player_receive_fields(function(player, name, fields)
return;
end
local map = cartographer.get_map(data.id);
local map = maps.get(data.id);
if not map then
return;
end
@ -376,10 +376,10 @@ function cartographer.copy_map_item(stack)
local id = meta:get_int("cartographer:map_id");
if id > 0 then
local src = cartographer.get_map(id);
local src = maps.get(id);
local new_id = cartographer.create_map(src.x, src.z, src.w, src.h, false, src.detail, src.scale);
local dest = cartographer.get_map(new_id);
local new_id = maps.create(src.x, src.z, src.w, src.h, false, src.detail, src.scale);
local dest = maps.get(new_id);
for k,v in pairs(src.fill) do
dest.fill[k] = v;
end
@ -407,7 +407,7 @@ function cartographer.resize_map_item(meta, size)
local id = meta:get_int("cartographer:map_id");
if id > 0 then
local map = cartographer.get_map(id);
local map = maps.get(id);
map:resize(size, size);
meta:set_string("description", map_description(id,

View File

@ -7,36 +7,6 @@ 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)
local id = map_data.next_map_id;
local map = {
id = id,
x = x,
z = z,
w = w,
h = h,
detail = detail,
scale = scale,
fill = {},
markers = {},
};
setmetatable(map, Map);
map_data.maps[id] = map;
if filled then
map:fill(map, x, z, w, h);
end
map_data.next_map_id = map_data.next_map_id + 1;
return id;
end
function cartographer.get_map(id)
return map_data.maps[id];
end
function Map.resize(self, w, h)
if w >= self.w and h >= self.h then
self.w = w;
@ -102,6 +72,42 @@ function Map.to_coordinates(self, x, z)
return math.floor(chunk.to(x) / self.scale + 0.5), math.floor(chunk.to(z) / self.scale + 0.5);
end
function Map.is_filled(self, x, z)
return self.fill[(x - self.x) + ((z - self.z) * self.w)] ~= nil;
end
local maps = {
create = function(x, z, w, h, filled, detail, scale)
local id = map_data.next_map_id;
local map = {
id = id,
x = x,
z = z,
w = w,
h = h,
detail = detail,
scale = scale,
fill = {},
markers = {},
};
setmetatable(map, Map);
map_data.maps[id] = map;
if filled then
map:fill(map, x, z, w, h);
end
map_data.next_map_id = map_data.next_map_id + 1;
return id;
end,
get = function(id)
return map_data.maps[id];
end,
}
-- Periodically-called function to fill in maps and queue chunks for manual
-- scanning
local function fill_loop()
@ -112,7 +118,7 @@ local function fill_loop()
if pos.y > -10 then
for i = 1,inventory:get_size("main") do
local stack = inventory:get_stack("main", i);
local map = cartographer.get_map(stack:get_meta():get_int("cartographer:map_id"));
local map = maps.get(stack:get_meta():get_int("cartographer:map_id"));
if map then
map:fill_local(pos.x, pos.z);
@ -192,10 +198,6 @@ function cartographer.register_marker(id, name, textures)
end
end
function Map.is_filled(self, x, z)
return self.fill[(x - self.x) + ((z - self.z) * self.w)] ~= nil;
end
-- Get an entry from a list for a given detail level
-- textures: An array of textures
-- detail: The detail level
@ -223,3 +225,5 @@ function cartographer.get_marker_texture(id, detail)
return nil;
end
return maps;

View File

@ -1,4 +1,4 @@
local gui, gui_skin, audio = ...;
local gui, gui_skin, audio, maps = ...;
local MAP_SIZE = 40;
local SCALE_SMALL = 1;
@ -721,7 +721,7 @@ minetest.register_on_player_receive_fields(function(player, name, fields)
smeta:set_int("cartographer:detail", 1 + detail);
cartographer.resize_map_item(smeta, size);
local map = cartographer.get_map(smeta:get_int("cartographer:map_id"));
local map = maps.get(smeta:get_int("cartographer:map_id"));
if map then
map.detail = 1 + detail;
end