Move autofill code into its own file

This commit is contained in:
Hugues Ross 2020-06-10 20:49:10 -04:00
parent 412e214f88
commit a895acb978
4 changed files with 95 additions and 37 deletions

37
autofill.lua Normal file
View File

@ -0,0 +1,37 @@
chunk, scanner = ...;
-- Periodically-called function to fill in maps and queue chunks for manual
-- 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);
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);
end
end
end
end
for _ = 1,10 do
scanner.scan_regions();
end
minetest.after(5, fill_loop);
end
minetest.after(5, fill_loop);

View File

@ -8,7 +8,7 @@ local map_data = {
maps = minetest.deserialize(mod_storage:get_string("maps")) or {},
next_map_id = mod_storage:get_int("next_map_id"),
}
};
if map_data.next_map_id == 0 then
map_data.next_map_id = 1;
@ -57,6 +57,7 @@ local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, sk
local map_item = 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, maps, materials, map_item);
loadfile(modpath .. "/autofill.lua") (chunk, scanner);
-- The API object
cartographer = {

View File

@ -108,40 +108,4 @@ local maps = {
end,
}
-- Periodically-called function to fill in maps and queue chunks for manual
-- 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);
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);
end
end
end
end
for _ = 1,10 do
scanner.scan_regions();
end
minetest.after(5, fill_loop);
end
minetest.after(5, fill_loop);
return maps;

56
material_api.lua Normal file
View File

@ -0,0 +1,56 @@
local materials_by_name = {};
local materials_by_group = {};
return {
-- Get the converted material value of the given itemstack
-- stack: The itemstack to convert
--
-- Returns a table with the material values
get_stack_value = function(stack)
local item_name = stack:get_name();
local item_count = stack:get_count();
for name,mats in pairs(materials_by_name) do
if name == item_name then
return {
paper = (mats.paper or 0) * item_count,
pigment = (mats.pigment or 0) * item_count,
}
end
end
for group,mats in pairs(materials_by_group) do
if minetest.get_item_group(item_name, group) ~= 0 then
return {
paper = (mats.paper or 0) * item_count,
pigment = (mats.pigment or 0) * item_count,
}
end
end
return {
paper = 0,
pigment = 0,
};
end,
register_by_name = function(name, material, value)
if materials_by_name[name] then
materials_by_name[name][material] = value or 1;
else
materials_by_name[name] = {
[material] = value or 1,
};
end
end,
register_by_group = function(name, material, value)
if materials_by_group[name] then
materials_by_group[name][material] = value or 1;
else
materials_by_group[name] = {
[material] = value or 1,
};
end
end,
};