Browse Source

Add configuration settings

tags/1.0
Hugues Ross 1 month ago
parent
commit
4fedd4275e
9 changed files with 124 additions and 59 deletions
  1. +29
    -26
      autofill.lua
  2. +9
    -3
      commands.lua
  3. +22
    -12
      init.lua
  4. +3
    -2
      items.lua
  5. +1
    -1
      map_api.lua
  6. +33
    -8
      scanner.lua
  7. +14
    -0
      settingtypes.txt
  8. +10
    -4
      storage.lua
  9. +3
    -3
      table.lua

+ 29
- 26
autofill.lua View File

@@ -2,40 +2,41 @@
-- chunk: The chunk coordinate conversion API
-- scanner: The map scanning API
-- maps: The map API
local chunk, scanner, maps = ...;
-- settings: The mod settings
local chunk, scanner, maps, settings = ...;

local function fill_loop()
-- Fill in all player-held maps
for _,p in ipairs(minetest.get_connected_players()) do
local inventory = p:get_inventory();
local pos = p:get_pos();
if pos.y > -10 then
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"));
-- Periodically-called function to fill in maps and queue chunks for backup
-- scans
if settings.autofill_freq ~= 0 then
-- scanning
local function fill_loop()
-- Fill in all player-held maps
for _,p in ipairs(minetest.get_connected_players()) do
local inventory = p:get_inventory();
local pos = p:get_pos();
if pos.y > -10 then
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
map:fill_local(pos.x, pos.z);
if map then
map:fill_local(pos.x, pos.z);
end
end
end
for i = -2,2 do
for j = -2,2 do
local adjusted_pos = {
x = pos.x + chunk.from(i),
y = pos.y,
z = pos.z + chunk.from(j),
}
scanner.queue_region(adjusted_pos);
for i = -2,2 do
for j = -2,2 do
local adjusted_pos = {
x = pos.x + chunk.from(i),
y = pos.y,
z = pos.z + chunk.from(j),
}
scanner.queue_region(adjusted_pos);
end
end
end
end
minetest.after(settings.autofill_freq, fill_loop);
end

for _ = 1,10 do
scanner.scan_regions();
end
minetest.after(5, fill_loop);
minetest.after(settings.autofill_freq, fill_loop);
end
minetest.after(5, fill_loop);


+ 9
- 3
commands.lua View File

@@ -2,8 +2,8 @@
-- chunk: The chunk coordinate conversion API
-- audio: The audio playback 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;

minetest.register_privilege("cartographer", {
@@ -41,7 +41,13 @@ minetest.register_chatcommand("map", {
local player_z = math.floor((chunk.to(pos.z) / scale) + 0.5);

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,
})



+ 22
- 12
init.lua View File

@@ -1,8 +1,16 @@
-- The path to this mod, for including files
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
local map_data = loadfile(modpath .. "/storage.lua") ();
local map_data = loadfile(modpath .. "/storage.lua") (settings);
local chunk = loadfile(modpath .. "/chunk_api.lua") ();
local gui = loadfile(modpath .. "/formspec.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 biomes = loadfile(modpath .. "/biome_api.lua") (util);
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 materials = loadfile(modpath .. "/material_api.lua") ();
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);
loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec);
loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item);
loadfile(modpath .. "/autofill.lua") (chunk, scanner, maps);
local map_item = loadfile(modpath .. "/items.lua") (chunk, gui, skin, audio, maps, markers, map_formspec, settings);
loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec, settings);
loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item, settings);
loadfile(modpath .. "/autofill.lua") (chunk, scanner, maps, settings);

-- The API object
cartographer = {
-- 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
biomes = biomes;
biomes = biomes,
-- 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
maps = maps;
maps = maps,
-- 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 = materials;
materials = materials,
-- scanner.lua: Exposes functions for queuing and performing terrain scans
scanner = scanner,
};

+ 3
- 2
items.lua View File

@@ -6,7 +6,8 @@
-- maps: The map API
-- markers: The marker 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
local player_maps = {};
@@ -204,7 +205,7 @@ end
local function map_from_meta(meta, player_x, player_z)
local size = meta:get_int("cartographer:size");
if size == 0 then
size = 40;
size = settings.default_size;
end

local detail = meta:get_int("cartographer:detail");


+ 1
- 1
map_api.lua View File

@@ -145,7 +145,7 @@ local maps = {
setmetatable(map, Map);

map_data.maps[id] = map;
if filled or true then
if filled then
map:fill_area(0, 0, w, h);
end



+ 33
- 8
scanner.lua View File

@@ -1,7 +1,8 @@
-- Arguments
-- map_data: The cartographer map data table
-- chunk: The chunk coordinate conversion API
local map_data, chunk = ...;
-- settings: The mod settings
local map_data, chunk, settings = ...;

local scan_queue = {};

@@ -207,14 +208,7 @@ function scanner.queue_region(pos)
scan_queue[#scan_queue + 1] = converted;
end

function scanner.scan_regions()
local len = #scan_queue;

if len == 0 then
return;
end

local function scan_internal()
local startpos = scan_queue[1];
local chunk_x = chunk.to(startpos.x);
local chunk_y = chunk.to(startpos.y);
@@ -239,4 +233,34 @@ function scanner.scan_regions()
table.remove(scan_queue, 1);
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;

+ 14
- 0
settingtypes.txt 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

+ 10
- 4
storage.lua View File

@@ -1,3 +1,7 @@
-- Arguments
-- settings: The mod settings
local settings = ...;

-- Storage and saving
local mod_storage = minetest.get_mod_storage();
local map_data = {
@@ -31,10 +35,12 @@ end
minetest.register_on_shutdown(save);
minetest.register_on_leaveplayer(save);

local function periodic_save()
save();
minetest.after(60, periodic_save);
if settings.autosave_freq ~= 0 then
local function periodic_save()
save();
minetest.after(settings.autosave_freq, periodic_save);
end
minetest.after(settings.autosave_freq, periodic_save);
end
minetest.after(60, periodic_save);

return map_data;

+ 3
- 3
table.lua View File

@@ -5,9 +5,9 @@
-- maps: The map API
-- materials: The material 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_MEDIUM = 2;
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_output", 1);

meta:set_int("size", MAP_SIZE);
meta:set_int("size", settings.default_size);
meta:set_int("scale", SCALE_SMALL);
meta:set_int("detail", 0);
end


Loading…
Cancel
Save