cache: take crecipe object methods to own table and use metatables

This commit is contained in:
Alexander Weber 2017-04-29 20:02:18 +02:00
parent 48032996cf
commit f7c87eebc5

View File

@ -7,71 +7,14 @@ cache.cgroups = {}
cache.citems = {}
--+++++++++++++++++++++++++++++++++++++++++++++++++--
-- cached recipes object
--+++++++++++++++++++++++++++++++++++++++++++++++++--
local crecipes = {}
cache.crecipes = crecipes
local crecipe_class = {}
crecipe_class.__index = crecipe_class
-----------------------------------------------------
-- Get all revealed recipes with at least one item in reference_items table
-- crecipes: analyze all data. Return false if invalid recipe. true on success
-----------------------------------------------------
function crecipes.get_revealed_recipes_with_items(playername, reference_items)
local recipelist = {}
for itemname, _ in pairs(reference_items) do
if cache.citems[itemname] and cache.citems[itemname].in_craft_recipe then
for _, recipe in ipairs(cache.citems[itemname].in_craft_recipe) do
local crecipe = cache.crecipes[recipe]
if crecipe and crecipe:is_revealed(playername) then
recipelist[recipe] = crecipe
end
end
end
end
return recipelist
end
-----------------------------------------------------
-- Get all recipes with all required items in reference items
-----------------------------------------------------
function crecipes.get_recipes_craftable(playername, reference_items)
local all = crecipes.get_revealed_recipes_with_items(playername, reference_items)
local craftable = {}
for recipe, crecipe in pairs(all) do
local item_ok = false
for _, entry in pairs(crecipe._items) do
item_ok = false
for _, itemdef in pairs(entry.items) do
if reference_items[itemdef.name] then
item_ok = true
end
end
if item_ok == false then
break
end
end
if item_ok == true then
craftable[recipe] = true
end
end
return craftable
end
-----------------------------------------------------
-- Recipe object Constructor
-----------------------------------------------------
function crecipes.new(recipe)
local self = {}
self.out_item = nil
self._recipe = recipe
self.recipe_type = recipe.type
self._items = {}
-- self.recipe_items = self._items --???
-----------------------------------------------------
-- analyze all data. Return false if invalid recipe. true on success
-----------------------------------------------------
function self:analyze()
function crecipe_class:analyze()
-- check recipe output
if self._recipe.output ~= "" then
local out_itemname = self._recipe.output:gsub('"','')
@ -85,7 +28,7 @@ function crecipes.new(recipe)
end
end
if not self.out_item or not self.out_item.name then
minetest.log("[smartfs_inventory] unknown recipe result "..recipe.output)
minetest.log("[smartfs_inventory] unknown recipe result "..self._recipe.output)
return false
end
@ -160,12 +103,12 @@ function crecipes.new(recipe)
else
return true
end
end
end
-----------------------------------------------------
-- Check if the recipe is revealed to the player
-----------------------------------------------------
function self:is_revealed(playername)
-----------------------------------------------------
-- crecipes: Check if the recipe is revealed to the player
-----------------------------------------------------
function crecipe_class:is_revealed(playername)
local recipe_valid = true
for _, entry in pairs(self._items) do
recipe_valid = false
@ -180,12 +123,12 @@ function crecipes.new(recipe)
end
end
return true
end
end
-----------------------------------------------------
-- Returns recipe without groups, with replacements
-----------------------------------------------------
function self:get_with_placeholder(player, inventory_tab)
-----------------------------------------------------
-- crecipes: Returns recipe without groups, with replacements
-----------------------------------------------------
function crecipe_class:get_with_placeholder(player, inventory_tab)
local recipe = table.copy(self._recipe)
recipe.items = table.copy(recipe.items)
for key, recipe_item in pairs(recipe.items) do
@ -227,11 +170,72 @@ function crecipes.new(recipe)
end
end
return recipe
end
end
-----------------------------------------------------
-- return constructed object
return self
-----------------------------------------------------
-- Recipe class functions
-----------------------------------------------------
local crecipes = {}
cache.crecipes = crecipes
crecipes._proto = crecipe_class
-----------------------------------------------------
-- Recipe object Constructor
-----------------------------------------------------
function crecipes.new(recipe)
local def = {}
def.out_item = nil
def._recipe = recipe
def.recipe_type = recipe.type
def._items = {}
setmetatable(def, crecipe_class)
def.__index = crecipe_class
print(dump(def))
return def
end
-----------------------------------------------------
-- Get all revealed recipes with at least one item in reference_items table
-----------------------------------------------------
function crecipes.get_revealed_recipes_with_items(playername, reference_items)
local recipelist = {}
for itemname, _ in pairs(reference_items) do
if cache.citems[itemname] and cache.citems[itemname].in_craft_recipe then
for _, recipe in ipairs(cache.citems[itemname].in_craft_recipe) do
local crecipe = cache.crecipes[recipe]
if crecipe and crecipe:is_revealed(playername) then
recipelist[recipe] = crecipe
end
end
end
end
return recipelist
end
-----------------------------------------------------
-- Get all recipes with all required items in reference items
-----------------------------------------------------
function crecipes.get_recipes_craftable(playername, reference_items)
local all = crecipes.get_revealed_recipes_with_items(playername, reference_items)
local craftable = {}
for recipe, crecipe in pairs(all) do
local item_ok = false
for _, entry in pairs(crecipe._items) do
item_ok = false
for _, itemdef in pairs(entry.items) do
if reference_items[itemdef.name] then
item_ok = true
end
end
if item_ok == false then
break
end
end
if item_ok == true then
craftable[recipe] = true
end
end
return craftable
end