diff --git a/autofill.lua b/autofill.lua new file mode 100644 index 0000000..58502e2 --- /dev/null +++ b/autofill.lua @@ -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); diff --git a/init.lua b/init.lua index 1df584a..3909a96 100644 --- a/init.lua +++ b/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 = { diff --git a/map_api.lua b/map_api.lua index b45f69f..1ea2f8f 100644 --- a/map_api.lua +++ b/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; diff --git a/material_api.lua b/material_api.lua new file mode 100644 index 0000000..c3a855d --- /dev/null +++ b/material_api.lua @@ -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, +};