Refactor out global variable 'data'

This commit is contained in:
Hugues Ross 2020-06-01 07:59:34 -04:00
parent acf04377bf
commit 7c667b04e7
5 changed files with 37 additions and 32 deletions

View File

@ -27,7 +27,7 @@ minetest.register_chatcommand("map", {
end
cartographer.map_sound("cartographer_open_map", player);
minetest.show_formspec(name, "map", cartographer.get_map_formspec(data, math.floor((player_x / scale) + 0.5), math.floor((player_z / scale) + 0.5), 40, 40, detail, scale, true));
minetest.show_formspec(name, "map", cartographer.get_map_formspec(math.floor((player_x / scale) + 0.5), math.floor((player_z / scale) + 0.5), 40, 40, detail, scale, true));
end,
})

View File

@ -20,14 +20,14 @@ if _cartographer.next_map_id == 0 then
_cartographer.next_map_id = 1;
end
data = {
local map_data = {
generated = minetest.deserialize(mod_storage:get_string("map")) or {},
}
local function save()
mod_storage:set_string("maps", minetest.serialize(_cartographer.maps));
mod_storage:set_int("next_map_id", _cartographer.next_map_id);
mod_storage:set_string("map", minetest.serialize(data.generated));
mod_storage:set_string("map", minetest.serialize(map_data.generated));
end
minetest.register_on_shutdown(save);
minetest.register_on_leaveplayer(save);
@ -50,11 +50,11 @@ end
local modpath = minetest.get_modpath("cartographer");
-- Includes
dofile(modpath .. "/commands.lua");
dofile(modpath .. "/scanner.lua");
dofile(modpath .. "/skin_api.lua");
dofile(modpath .. "/map_api.lua");
dofile(modpath .. "/map_formspec.lua");
dofile(modpath .. "/marker_formspec.lua");
dofile(modpath .. "/items.lua");
dofile(modpath .. "/table.lua");
loadfile(modpath .. "/skin_api.lua") ();
loadfile(modpath .. "/scanner.lua") (map_data);
loadfile(modpath .. "/map_api.lua") ();
loadfile(modpath .. "/items.lua") ();
loadfile(modpath .. "/marker_formspec.lua") ();
loadfile(modpath .. "/map_formspec.lua") (map_data);
loadfile(modpath .. "/commands.lua") ();
loadfile(modpath .. "/table.lua") ();

View File

@ -19,7 +19,7 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_
local map = cartographer.get_map(id);
player_x, player_z = cartographer.to_map_coordinates(map, player_x, player_z)
local formspec, formspec_width, formspec_height = cartographer.get_map_formspec_map(data, map, player_x, player_z, height_mode);
local formspec, formspec_width, formspec_height = cartographer.get_map_formspec_map(map, player_x, player_z, height_mode);
if #_cartographer.marker_lookup > 0 then
formspec = formspec .. string.format("style_type[button,image_button,label;noclip=true] container[%f,1.0]", formspec_width - 0.01)
.. _cartographer.generate_marker_formspec(cartographer.get_marker(map, player_x, player_z), map.detail, marker_page or 1, cartographer.skin.marker_bg, cartographer.skin.marker_button)

View File

@ -1,3 +1,8 @@
-- Arguments
-- map_data: The cartographer map data table
local map_data = ...;
-- Constants
local TILE_SIZE = 0.25;
local TILE_OFFSET = 0.24; -- Slightly smaller than TILE_SIZE. We overlap tiles slightly to minimize seams
@ -49,7 +54,6 @@ local function unknown_biome_tile(x, y, detail, variant)
end
-- Generate formspec markup for a given map
-- data: The mapping data
-- x: The X position of the map (in map coordinates)
-- y: The Z position of the map (in map coordinates)
-- w: The width of the map (in map coordinates)
@ -64,13 +68,13 @@ end
-- (Optional) user: userdata passed to the is_visible/get_marker callbacks
--
-- Returns a formspec string
local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_scale, height_mode, is_visible, get_marker, user)
local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, height_mode, is_visible, get_marker, user)
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 = data.generated[i * map_scale];
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(user, i, j)) then
@ -126,7 +130,6 @@ local function generate_map(data, x, y, w, h, player_x, player_y, detail, map_sc
end
-- Get the formspec for a given map segment
-- data: The map data
-- x: The X position of the map, in map coordinates
-- y: The Y position of the map, in map coordinates
-- w: The width of the map, in map coordinates
@ -137,17 +140,15 @@ end
--
-- Returns a formspec string, the width of the formspec, and the height of the
-- formspec
-- TODO: Remove data argument
function cartographer.get_map_formspec(data, x, y, w, h, detail, scale, height_mode)
function cartographer.get_map_formspec(x, y, w, h, detail, scale, height_mode)
local formspec_width = (w + 1) * TILE_OFFSET + 0.01;
local formspec_height = (h + 1) * TILE_OFFSET + 0.01;
return map_formspec_prefix:format(formspec_width, formspec_height)..generate_map(data, x - (w * 0.5), y - (h * 0.5), w, h, x, y, detail, scale, height_mode),
return map_formspec_prefix:format(formspec_width, formspec_height)..generate_map(x - (w * 0.5), y - (h * 0.5), w, h, x, y, detail, scale, height_mode),
formspec_width,
formspec_height;
end
-- Get the formspec for a given map table
-- data: The map data
-- 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
@ -155,11 +156,10 @@ end
--
-- Returns a formspec string, the width of the formspec, and the height of the
-- formspec
-- TODO: Remove data argument
function cartographer.get_map_formspec_map(data, map, x, y, height_mode)
function cartographer.get_map_formspec_map(map, x, y, height_mode)
local formspec_width = (map.w + 1) * TILE_OFFSET + 0.01;
local formspec_height = (map.h + 1) * TILE_OFFSET + 0.01;
return map_formspec_prefix:format(formspec_width, formspec_height)..generate_map(data, map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, height_mode, cartographer.is_filled, cartographer.get_marker, map),
return map_formspec_prefix:format(formspec_width, 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),
formspec_width,
formspec_height;
end

View File

@ -1,3 +1,8 @@
-- Arguments
-- map_data: The cartographer map data table
local map_data = ...;
-- Constants
local CHUNK_SIZE = _cartographer.CHUNK_SIZE;
-- Register a new tile in map data
@ -9,23 +14,23 @@ local CHUNK_SIZE = _cartographer.CHUNK_SIZE;
-- scan. Manual scans are overridden by generated
-- scans under normal circumstances.
local function register_tile(x, y, biome, height, manual_scan)
if not data.generated[x] then
data.generated[x] = {
if not map_data.generated[x] then
map_data.generated[x] = {
[y] = {
biome = biome,
height = height,
}
};
if manual_scan ~= nil then
data.generated[x][y].manual_scan = manual_scan;
map_data.generated[x][y].manual_scan = manual_scan;
end
elseif not data.generated[x][y] or data.generated[x][y].height < height then
data.generated[x][y] = {
elseif not map_data.generated[x][y] or map_data.generated[x][y].height < height then
map_data.generated[x][y] = {
biome = biome,
height = height,
};
if manual_scan ~= nil or data.generated[x][y].manual_scan then
data.generated[x][y].manual_scan = manual_scan;
if manual_scan ~= nil or map_data.generated[x][y].manual_scan then
map_data.generated[x][y].manual_scan = manual_scan;
end
end
end
@ -161,11 +166,11 @@ end
--
-- Returns true if the position is handled by the current map data
local function is_scan_handled(x, y, z)
if not data.generated[x] then
if not map_data.generated[x] then
return false;
end
local tile = data.generated[x][z];
local tile = map_data.generated[x][z];
return tile and ((not tile.manual_scan and tile.height > 0) or tile.height >= y);
end