From 90993908424a89477693fd270f28f0ee75de6526 Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Wed, 10 Jun 2020 20:01:58 -0400 Subject: [PATCH] Move material methods into separate api object --- init.lua | 4 +++- table.lua | 61 +++---------------------------------------------------- 2 files changed, 6 insertions(+), 59 deletions(-) diff --git a/init.lua b/init.lua index 76a0b48..d69f041 100644 --- a/init.lua +++ b/init.lua @@ -55,14 +55,16 @@ local biomes = loadfile(modpath .. "/biome_api.lua") (util); local scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk); local maps = loadfile(modpath .. "/map_api.lua") (map_data, chunk, scanner); +local materials = loadfile(modpath .. "/material_api.lua") (); local markers = loadfile(modpath .. "/marker_api.lua") (marker_lookup); local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, util, biomes, markers); 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, map_item); +loadfile(modpath .. "/table.lua") (gui, skin, audio, maps, materials, map_item); cartographer.skin = skin; cartographer.biomes = biomes; cartographer.markers = markers; cartographer.maps = maps; cartographer.map_item = map_item; +cartographer.materials = materials; diff --git a/table.lua b/table.lua index 94f8c44..cd734e2 100644 --- a/table.lua +++ b/table.lua @@ -1,4 +1,4 @@ -local gui, gui_skin, audio, maps, map_item = ...; +local gui, gui_skin, audio, maps, materials, map_item = ...; local MAP_SIZE = 40; local SCALE_SMALL = 1; @@ -6,9 +6,6 @@ local SCALE_MEDIUM = 2; local SCALE_LARGE = 4; local SCALE_HUGE = 8; -local materials_by_name = {}; -local materials_by_group = {}; - -- Get the material cost for the given map scale and detail level -- scale: The map scale -- detail: The detail level @@ -90,38 +87,6 @@ local function get_copy_material_cost(meta) }; end --- Get the converted material value of the given itemstack --- stack: The itemstack to convert --- --- Returns a table with the material values -local function get_material_value(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 - local fs = {}; -- Draw a 1px thick horizontal separator formspec element @@ -380,7 +345,7 @@ end -- Returns a formspec string function fs.convert(x, y, pos, skin) local meta = minetest.get_meta(pos); - local value = get_material_value(meta:get_inventory():get_stack("input", 1)); + local value = materials.get_stack_value(meta:get_inventory():get_stack("input", 1)); return gui.container { x = x, @@ -694,7 +659,7 @@ minetest.register_on_player_receive_fields(function(player, name, fields) local inv = meta:get_inventory(); local stack = inv:get_stack("input", 1); - local value = get_material_value(stack); + local value = materials.get_stack_value(stack); if value.paper + value.pigment > 0 then meta:set_int("paper", meta:get_int("paper") + value.paper); @@ -955,23 +920,3 @@ minetest.register_node("cartographer:advanced_table", { on_metadata_inventory_put = table_on_items_changed, on_metadata_inventory_take = table_on_items_changed, }); - -function cartographer.register_map_material_name(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 - -function cartographer.register_map_material_group(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