diff --git a/mods/CORE/CraftSystem/src/CraftSystem.lua b/mods/CORE/CraftSystem/src/CraftSystem.lua index 528f16b..7f24920 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem.lua @@ -9,7 +9,7 @@ local ImmersiveCraftHandler= require('CraftSystem.ImmersiveCraftHandler') local ProcessingCraftHandler= require('CraftSystem.ProcessingCraftHandler') ---@alias CraftSystem.CraftGroup string ----@alias CraftSystem.CraftRecipe string[] +---@alias CraftSystem.CraftRecipe ItemStackString[] ---@class CraftSystem ---@field PrimitiveCraftHandlers table @@ -27,7 +27,7 @@ local CraftSystem = { ---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory' ---@alias CraftSystem.ImmersiveCraftType 'workbench' ----@alias CraftSystem.ProcessingCraftType '' +---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG ---@param type CraftSystem.PrimitiveCraftType local function registerPrimitiveCraftType(type) @@ -83,6 +83,7 @@ end --Registration of craft types: registerPrimitiveCraftType('inside_player_inventory') registerImmersiveCraftType('workbench') +registerProcessingCraftType('test') -- DEBUG --- Registration methods --- diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraft.lua b/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraft.lua index a4781be..c818357 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraft.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraft.lua @@ -7,6 +7,7 @@ local require = modInfo.require local BaseCraft = require('CraftSystem.BaseCraft') ---@class CraftSystem.ProcessingCraft : CraftSystem.BaseCraft +---@field recipe CraftSystem.CraftRecipe ---@field time number ---@field startHandler fun() ---@field processHandler fun() @@ -17,6 +18,7 @@ local ProcessingCraft = extend(BaseCraft, {}) --- ---**Only for EmmyLua.** ---@class CraftSystem.ProcessingCraftDef : CraftSystem.BaseCraftDef +---@field recipe CraftSystem.CraftRecipe ---@field time number ---@field startHandler fun()? ---@field processHandler fun()? @@ -29,6 +31,7 @@ function ProcessingCraft:new(name, def) ---@type CraftSystem.ProcessingCraft local instance = getInstance(self, {}) instance.name = name + instance.recipe = def.recipe instance.group = def.group instance.output = def.output instance.time = def.time diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua index 452ddfc..1bd4d9a 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua @@ -1,6 +1,8 @@ +local HASH_SEPARATOR = ',' + local extend = Class.extend -local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join +local tableInsert, tableCopy, tableSort, tableConcat, tableIsEmpty = table.insert, table.copy, table.sort, table.concat, table.is_empty local modInfo = Mod.getInfo('smc__core__craft_system') local require = modInfo.require @@ -14,12 +16,21 @@ local ProcessingCraft = require('CraftSystem.ProcessingCraft') ---A primitive crafting processor that doesn't require much interaction and provides ---simple crafting based on a recipe-result scheme. ---@class CraftSystem.ProcessingCraftHandler : CraftSystem.BaseCraftHandler ----@field craftsByGroup table ----@field groupedCrafts (CraftSystem.ProcessingCraft[]|CraftSystem.ProcessingCraft)[] +---@field craftsByHash table ---@field getCraftByName fun(self, craftName:string):CraftSystem.ProcessingCraft ---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.ProcessingCraft[] ---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.ProcessingCraft[]|CraftSystem.ProcessingCraft)[] -local ProcessingCraftHandler = extend(BaseCraftHandler, {}) +local ProcessingCraftHandler = extend(BaseCraftHandler, { + craftsByHash = {} +}) + +---@param recipe CraftSystem.CraftRecipe +local function getCraftHash(recipe) + recipe = tableCopy(recipe) + + tableSort(recipe) + return tableConcat(recipe, HASH_SEPARATOR) +end ---Register a primitive craft that does not require special interaction. ---@param craftName string @@ -37,9 +48,33 @@ function ProcessingCraftHandler:registerCraft(craftName, def) self.craftsByGroup[group] = {} end + local craftHash = getCraftHash(craft.recipe) + assert(self.craftsByHash[craftHash] == nil, 'A craft with this recipe already exists.') + self.craftsByName[craftName] = craft tableInsert(self.craftsByGroup[group], craft) tableInsert(self.craftsList, craft) + self.craftsByHash[craftHash] = craft +end + +---@param input ItemStack[] +---@return CraftSystem.ProcessingCraft? +function ProcessingCraftHandler:getCraftByRecipe(input) + local recipe = {} + for _, item in ipairs(input) do + if not item:is_empty() then + tableInsert(recipe, item:get_name()) + end + end + + if tableIsEmpty(recipe) then + return nil + end + + local craftHash = getCraftHash(recipe) + local craft = self.craftsByHash[craftHash] + + return craft end return ProcessingCraftHandler