Add configuration settings

This commit is contained in:
Hugues Ross 2020-06-13 16:21:34 -04:00
parent bd1d21f833
commit 4fedd4275e
9 changed files with 124 additions and 62 deletions

View File

@ -2,40 +2,41 @@
-- chunk: The chunk coordinate conversion API -- chunk: The chunk coordinate conversion API
-- scanner: The map scanning API -- scanner: The map scanning API
-- maps: The map API -- maps: The map API
local chunk, scanner, maps = ...; -- settings: The mod settings
local chunk, scanner, maps, settings = ...;
-- Periodically-called function to fill in maps and queue chunks for manual -- Periodically-called function to fill in maps and queue chunks for backup
-- scanning -- scans
local function fill_loop() if settings.autofill_freq ~= 0 then
-- Fill in all player-held maps -- scanning
for _,p in ipairs(minetest.get_connected_players()) do local function fill_loop()
local inventory = p:get_inventory(); -- Fill in all player-held maps
local pos = p:get_pos(); for _,p in ipairs(minetest.get_connected_players()) do
if pos.y > -10 then local inventory = p:get_inventory();
for i = 1,inventory:get_size("main") do local pos = p:get_pos();
local stack = inventory:get_stack("main", i); if pos.y > -10 then
local map = maps.get(stack:get_meta():get_int("cartographer:map_id")); for i = 1,inventory:get_size("main") do
local stack = inventory:get_stack("main", i);
local map = maps.get(stack:get_meta():get_int("cartographer:map_id"));
if map then if map then
map:fill_local(pos.x, pos.z); map:fill_local(pos.x, pos.z);
end
end end
end for i = -2,2 do
for i = -2,2 do for j = -2,2 do
for j = -2,2 do local adjusted_pos = {
local adjusted_pos = { x = pos.x + chunk.from(i),
x = pos.x + chunk.from(i), y = pos.y,
y = pos.y, z = pos.z + chunk.from(j),
z = pos.z + chunk.from(j), }
} scanner.queue_region(adjusted_pos);
scanner.queue_region(adjusted_pos); end
end end
end end
end end
minetest.after(settings.autofill_freq, fill_loop);
end end
for _ = 1,10 do minetest.after(settings.autofill_freq, fill_loop);
scanner.scan_regions();
end
minetest.after(5, fill_loop);
end end
minetest.after(5, fill_loop);

View File

@ -2,8 +2,8 @@
-- chunk: The chunk coordinate conversion API -- chunk: The chunk coordinate conversion API
-- audio: The audio playback API -- audio: The audio playback API
-- map_formspec: The map display API -- map_formspec: The map display API
local chunk, audio, map_formspec = ...; -- settings: The mod settings
local chunk, audio, map_formspec, settings = ...;
local MAXINT = 2147483647; local MAXINT = 2147483647;
minetest.register_privilege("cartographer", { minetest.register_privilege("cartographer", {
@ -41,7 +41,13 @@ minetest.register_chatcommand("map", {
local player_z = math.floor((chunk.to(pos.z) / scale) + 0.5); local player_z = math.floor((chunk.to(pos.z) / scale) + 0.5);
audio.play_feedback("cartographer_open_map", player); audio.play_feedback("cartographer_open_map", player);
minetest.show_formspec(name, "map", map_formspec.from_coords(player_x, player_z, 40, 40, detail, scale, true)); minetest.show_formspec(name, "map", map_formspec.from_coords(player_x,
player_z,
settings.default_size,
settings.default_size,
detail,
scale,
true));
end, end,
}) })

View File

@ -1,8 +1,16 @@
-- The path to this mod, for including files -- The path to this mod, for including files
local modpath = minetest.get_modpath("cartographer"); local modpath = minetest.get_modpath("cartographer");
local settings = {
default_size = tonumber(minetest.settings:get("default_size")) or 40,
autofill_freq = tonumber(minetest.settings:get("autofill_freq")) or 5,
autosave_freq = tonumber(minetest.settings:get("autosave_freq")) or 60,
backup_scan_freq = tonumber(minetest.settings:get("backup_scan_freq")) or 5,
backup_scan_count = tonumber(minetest.settings:get("backup_scan_count")) or 10,
};
-- Includes -- Includes
local map_data = loadfile(modpath .. "/storage.lua") (); local map_data = loadfile(modpath .. "/storage.lua") (settings);
local chunk = loadfile(modpath .. "/chunk_api.lua") (); local chunk = loadfile(modpath .. "/chunk_api.lua") ();
local gui = loadfile(modpath .. "/formspec.lua") (); local gui = loadfile(modpath .. "/formspec.lua") ();
local skin = loadfile(modpath .. "/skin_api.lua") (); local skin = loadfile(modpath .. "/skin_api.lua") ();
@ -10,27 +18,29 @@ local util = loadfile(modpath .. "/util.lua") ();
local audio = loadfile(modpath .. "/audio.lua") (); local audio = loadfile(modpath .. "/audio.lua") ();
local biomes = loadfile(modpath .. "/biome_api.lua") (util); local biomes = loadfile(modpath .. "/biome_api.lua") (util);
local markers = loadfile(modpath .. "/marker_api.lua") (); local markers = loadfile(modpath .. "/marker_api.lua") ();
local scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk); local scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk, settings);
local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk); local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk);
local materials = loadfile(modpath .. "/material_api.lua") (); local materials = loadfile(modpath .. "/material_api.lua") ();
local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, util, biomes, markers); local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, util, biomes, markers);
local map_item = loadfile(modpath .. "/items.lua") (chunk, gui, skin, audio, maps, markers, map_formspec); local map_item = loadfile(modpath .. "/items.lua") (chunk, gui, skin, audio, maps, markers, map_formspec, settings);
loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec); loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec, settings);
loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item); loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item, settings);
loadfile(modpath .. "/autofill.lua") (chunk, scanner, maps); loadfile(modpath .. "/autofill.lua") (chunk, scanner, maps, settings);
-- The API object -- The API object
cartographer = { cartographer = {
-- skin_api.lua: Allows the visual customization of formspecs -- skin_api.lua: Allows the visual customization of formspecs
skin = skin; skin = skin,
-- biome_api.lua: Allows biome data to be registered for display in maps -- biome_api.lua: Allows biome data to be registered for display in maps
biomes = biomes; biomes = biomes,
-- marker_api.lua: Allows markers to be registered for placement on maps -- marker_api.lua: Allows markers to be registered for placement on maps
markers = markers; markers = markers,
-- map_api.lua: Allows the creation, lookup, and management of map objects -- map_api.lua: Allows the creation, lookup, and management of map objects
maps = maps; maps = maps,
-- items.lua: Allows the creation of map items with proper metadata -- items.lua: Allows the creation of map items with proper metadata
map_item = map_item; map_item = map_item,
-- materials.lua: Allows items to be registered as mapmaking materials -- materials.lua: Allows items to be registered as mapmaking materials
materials = materials; materials = materials,
-- scanner.lua: Exposes functions for queuing and performing terrain scans
scanner = scanner,
}; };

View File

@ -6,7 +6,8 @@
-- maps: The map API -- maps: The map API
-- markers: The marker API -- markers: The marker API
-- map_formspec: The map display API -- map_formspec: The map display API
local chunk, gui, skin, audio, maps, markers, map_formspec = ...; -- settings: The mod settings
local chunk, gui, skin, audio, maps, markers, map_formspec, settings = ...;
-- The list of players looking at maps, and the map IDs that they're looking at -- The list of players looking at maps, and the map IDs that they're looking at
local player_maps = {}; local player_maps = {};
@ -204,7 +205,7 @@ end
local function map_from_meta(meta, player_x, player_z) local function map_from_meta(meta, player_x, player_z)
local size = meta:get_int("cartographer:size"); local size = meta:get_int("cartographer:size");
if size == 0 then if size == 0 then
size = 40; size = settings.default_size;
end end
local detail = meta:get_int("cartographer:detail"); local detail = meta:get_int("cartographer:detail");

View File

@ -145,7 +145,7 @@ local maps = {
setmetatable(map, Map); setmetatable(map, Map);
map_data.maps[id] = map; map_data.maps[id] = map;
if filled or true then if filled then
map:fill_area(0, 0, w, h); map:fill_area(0, 0, w, h);
end end

View File

@ -1,7 +1,8 @@
-- Arguments -- Arguments
-- map_data: The cartographer map data table -- map_data: The cartographer map data table
-- chunk: The chunk coordinate conversion API -- chunk: The chunk coordinate conversion API
local map_data, chunk = ...; -- settings: The mod settings
local map_data, chunk, settings = ...;
local scan_queue = {}; local scan_queue = {};
@ -207,14 +208,7 @@ function scanner.queue_region(pos)
scan_queue[#scan_queue + 1] = converted; scan_queue[#scan_queue + 1] = converted;
end end
-- Scan the next tile on the queue, and remove it local function scan_internal()
function scanner.scan_regions()
local len = #scan_queue;
if len == 0 then
return;
end
local startpos = scan_queue[1]; local startpos = scan_queue[1];
local chunk_x = chunk.to(startpos.x); local chunk_x = chunk.to(startpos.x);
local chunk_y = chunk.to(startpos.y); local chunk_y = chunk.to(startpos.y);
@ -239,4 +233,34 @@ function scanner.scan_regions()
table.remove(scan_queue, 1); table.remove(scan_queue, 1);
end end
-- Scan the next N tiles on the queue, and remove them
-- N is determined by backup_scan_count
--
-- flush: Flush the entire scan queue, scanning all queued regions
function scanner.scan_regions(flush)
local len = #scan_queue;
if len == 0 then
return;
end
if settings.backup_scan_count == 0 or flush then
while #scan_queue > 0 do
scan_internal();
end
else
for _=1,settings.backup_scan_count do
scan_internal();
end
end
end
if settings.backup_scan_freq ~= 0 then
local function periodic_scan()
scanner.scan_regions();
minetest.after(settings.backup_scan_freq, periodic_scan);
end
minetest.after(settings.backup_scan_freq, periodic_scan);
end
return scanner; return scanner;

14
settingtypes.txt Normal file
View File

@ -0,0 +1,14 @@
# The default size of new maps, in tiles
default_size (Default Map Size) int 40 5 80
# The frequency that maps are filled in as players explore, in seconds. Set to 0 to disable.
autofill_freq (Map Auto-Fill Frequency) int 5 0 60
# The frequency that this mod saves its data to mod storage, in seconds. Set to 0 to disable.
autosave_freq (Data Auto-Save Frequency) int 60 0 3600
# The frequency that this mod scans already-generated map data to fill in existing terrain, in seconds. Set to 0 to disable.
backup_scan_freq (Map Scan Frequency) int 5 0 3600
# The number of map sections that this mod scans at a time. Set to 0 to scan all relevant areas
backup_scan_count (Map Scan Rate) int 10 0 100

View File

@ -1,3 +1,7 @@
-- Arguments
-- settings: The mod settings
local settings = ...;
-- Storage and saving -- Storage and saving
local mod_storage = minetest.get_mod_storage(); local mod_storage = minetest.get_mod_storage();
local map_data = { local map_data = {
@ -31,10 +35,12 @@ end
minetest.register_on_shutdown(save); minetest.register_on_shutdown(save);
minetest.register_on_leaveplayer(save); minetest.register_on_leaveplayer(save);
local function periodic_save() if settings.autosave_freq ~= 0 then
save(); local function periodic_save()
minetest.after(60, periodic_save); save();
minetest.after(settings.autosave_freq, periodic_save);
end
minetest.after(settings.autosave_freq, periodic_save);
end end
minetest.after(60, periodic_save);
return map_data; return map_data;

View File

@ -5,9 +5,9 @@
-- maps: The map API -- maps: The map API
-- materials: The material API -- materials: The material API
-- map_item: The map item API -- map_item: The map item API
local gui, gui_skin, audio, maps, materials, map_item = ...; -- settings: The mod settings
local gui, gui_skin, audio, maps, materials, map_item, settings = ...;
local MAP_SIZE = 40;
local SCALE_SMALL = 1; local SCALE_SMALL = 1;
local SCALE_MEDIUM = 2; local SCALE_MEDIUM = 2;
local SCALE_LARGE = 4; local SCALE_LARGE = 4;
@ -780,7 +780,7 @@ local function setup_table_node(pos)
meta:get_inventory():set_size("copy_input", 1); meta:get_inventory():set_size("copy_input", 1);
meta:get_inventory():set_size("copy_output", 1); meta:get_inventory():set_size("copy_output", 1);
meta:set_int("size", MAP_SIZE); meta:set_int("size", settings.default_size);
meta:set_int("scale", SCALE_SMALL); meta:set_int("scale", SCALE_SMALL);
meta:set_int("detail", 0); meta:set_int("detail", 0);
end end