Move autofill code into its own file
This commit is contained in:
parent
412e214f88
commit
a895acb978
37
autofill.lua
Normal file
37
autofill.lua
Normal 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);
|
3
init.lua
3
init.lua
@ -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 = {
|
||||
|
36
map_api.lua
36
map_api.lua
@ -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
56
material_api.lua
Normal 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,
|
||||
};
|
Loading…
Reference in New Issue
Block a user