diff --git a/mods/CORE/CraftSystem/mod.conf b/mods/CORE/CraftSystem/mod.conf index 0fa3ca2..d1168a5 100644 --- a/mods/CORE/CraftSystem/mod.conf +++ b/mods/CORE/CraftSystem/mod.conf @@ -1,2 +1,2 @@ name = smc__core__craft_system -depends = smc__system__system_loader, smc__core__creature +depends = smc__system__system_loader, smc__core__creature, smc__core__progression_system diff --git a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua index 38c0edc..b62bdac 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraft.lua @@ -3,17 +3,18 @@ local CHECK = {YES = '[x]', NO = '[ ]', EMPTY = '-'} ---@class CraftSystem.BaseCraft : Class ----@field name string ----@field recipe ItemStackString[] ----@field output ItemStackString ----@field group CraftSystem.CraftGroup +---@field name string +---@field hidden boolean? +---@field recipe ItemStackString[] +---@field output ItemStackString +---@field group CraftSystem.CraftGroup local BaseCraft = Class:extend({}) ---Definition table for the `CraftSystem.BaseCraft`. ---@class CraftSystem.BaseCraftDef ----@field recipe ItemStackString[] ----@field output ItemStackString ----@field group CraftSystem.CraftGroup? +---@field recipe ItemStackString[] +---@field output ItemStackString +---@field group CraftSystem.CraftGroup? ---@param name string ---@param def CraftSystem.BaseCraftDef diff --git a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua index 57d2a65..ea10154 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler.lua @@ -1,27 +1,35 @@ local tableInsert = table.insert local stringMatch = string.match +local require = Mod.getInfo('smc__core__craft_system').require + +---@type CraftSystem.BaseCraftHandler.PlayerCraftsCache +local PlayerCraftsCache = require('CraftSystem.BaseCraftHandler.PlayerCraftsCache') + local PLAYERS_KNOW_ALL_RECIPES = true + ---Abstract class for handling crafting. ---@class CraftSystem.BaseCraftHandler : Class ---@field baseType CraftSystem.BaseCraftType ---@field type CraftSystem.CraftType ---@field defaultCraftDef CraftSystem.BaseCraftDef --- ----@field craftsByName table ----@field craftsByGroup table ----@field craftsList CraftSystem.BaseCraft[] ----@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] ----@field learnedCrafts table ----@field learnedGroupedCrafts table +---@field craftsByName table +---@field craftsByGroup table +---@field craftsByOutput table +---@field craftsList CraftSystem.BaseCraft[] +---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] +--- +---@field playersCraftsCache table local BaseCraftHandler = Class:extend({ - craftsByName = {}, - craftsByGroup = {}, - craftsList = {}, - groupedCrafts = {}, - learnedCrafts = {}, - learnedGroupedCrafts = {}, + craftsByName = {}, + craftsByGroup = {}, + craftsByOutput = {}, + craftsList = {}, + groupedCrafts = {}, + + playersCraftsCache = {} }) ---@class CraftSystem.BaseCraftHandlerDef @@ -38,16 +46,50 @@ function BaseCraftHandler:new(baseType, type, def) instance.type = type instance.defaultCraftDef = def.defaultCraftDef or {} + core.register_on_joinplayer(function (player, last_login) + local playerName = player:get_player_name() + local cache = PlayerCraftsCache:new(playerName) + + self.playersCraftsCache[playerName] = cache + end) + + core.register_on_leaveplayer(function (player, timed_out) + local playerName = player:get_player_name() + + self.playersCraftsCache[playerName] = nil + end) + return instance end +---@param playerName string +---@return CraftSystem.BaseCraftHandler.PlayerCraftsCache +function BaseCraftHandler:getPlayerCraftsCache(playerName) + local cache = self.playersCraftsCache[playerName] + assert(cache ~= nil) + + return cache +end + ---@param craftName string ---@return CraftSystem.BaseCraft function BaseCraftHandler:getCraftByName(craftName) local craft = self.craftsByName[craftName] - assert(craft ~= nil, ('A craft `%s` not exists.'):format( - craftName)) + assert(craft ~= nil, ('A craft `%s` not exists.'):format(craftName)) + + return craft +end + +---@param craftName string +---@return CraftSystem.BaseCraft? +function BaseCraftHandler:getLearnedCraftByName(playerName, craftName) + if PLAYERS_KNOW_ALL_RECIPES then + return self.craftsByName[craftName] + end + + local cache = self:getPlayerCraftsCache(playerName) + local craft = cache.craftsByName[craftName] return craft end @@ -57,25 +99,64 @@ end function BaseCraftHandler:getCraftsByGroup(craftGroup) local crafts = self.craftsByGroup[craftGroup] - assert(crafts ~= nil, ('A craft with group `%s` not exists.'):format( - craftGroup)) + assert(crafts ~= nil, ('A craft with group `%s` not exists.'):format(craftGroup)) + + return crafts +end + +---@param craftGroup CraftSystem.CraftGroup +---@return CraftSystem.BaseCraft[]? +function BaseCraftHandler:getLearnedCraftsByGroup(playerName, craftGroup) + if PLAYERS_KNOW_ALL_RECIPES then + return self.craftsByGroup[craftGroup] + end + + local cache = self:getPlayerCraftsCache(playerName) + local crafts = cache.craftsByGroup[craftGroup] + + return crafts +end + +---@return (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] +function BaseCraftHandler:getGroupedCrafts() + return self.groupedCrafts +end + +---@param playerName string +---@return (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] +function BaseCraftHandler:getLearnedGroupedCrafts(playerName) + if PLAYERS_KNOW_ALL_RECIPES then + return self.groupedCrafts + end + + local cache = self:getPlayerCraftsCache(playerName) + local crafts = cache.groupedCrafts + + return crafts +end + +---@param output ItemStackString +---@return CraftSystem.BaseCraft[]? +function BaseCraftHandler:getCraftsByOutput(output) + output = ItemStack(output):get_name() + + local crafts = self.craftsByOutput[output] return crafts end ---@param playerName string ----@return (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] -function BaseCraftHandler:getLearnedCrafts(playerName) +---@param output ItemStackString +---@return CraftSystem.BaseCraft[]? +function BaseCraftHandler:getLearnedCraftsByOutput(playerName, output) + output = ItemStack(output):get_name() + if PLAYERS_KNOW_ALL_RECIPES then - return self.groupedCrafts + return self.craftsByOutput[output] end - local crafts = self.learnedGroupedCrafts[playerName] - - if crafts == nil then - crafts = {} - self.learnedGroupedCrafts[playerName] = crafts - end + local cache = self:getPlayerCraftsCache(playerName) + local crafts = cache.craftsByOutput[output] return crafts end @@ -103,6 +184,12 @@ local function getMathedCrafts(crafts, pattern) return matchedCrafts end +---@param pattern string +---@return CraftSystem.BaseCraft[] +function BaseCraftHandler:getCraftsMatch(pattern) + return getMathedCrafts(self.craftsList, pattern) +end + ---@param playerName string ---@param pattern string ---@return CraftSystem.BaseCraft[] @@ -111,16 +198,10 @@ function BaseCraftHandler:getLearnedCraftsMatch(playerName, pattern) return getMathedCrafts(self.craftsList, pattern) end - local crafts = self.learnedCrafts[playerName] - - if crafts == nil then - crafts = {} - self.learnedCrafts[playerName] = crafts - end + local cache = self:getPlayerCraftsCache(playerName) + local crafts = getMathedCrafts(cache.craftsList, pattern) return crafts end ---TODO: сохранять выученные крафты игроков в сторадже - return BaseCraftHandler diff --git a/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler/PlayerCraftsCache.lua b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler/PlayerCraftsCache.lua new file mode 100644 index 0000000..36722c2 --- /dev/null +++ b/mods/CORE/CraftSystem/src/CraftSystem/BaseCraftHandler/PlayerCraftsCache.lua @@ -0,0 +1,35 @@ +local ProgressionSystem = Smc.requireProgressionSystem() + +---@class CraftSystem.BaseCraftHandler.PlayerCraftsCache : Class +---@field playerName string +--- +---@field craftsByName table +---@field craftsByGroup table +---@field craftsByOutput table +---@field craftsList CraftSystem.BaseCraft[] +---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[] +local PlayerCraftsCache = Class:extend({ + craftsByName = {}, + craftsByGroup = {}, + craftsByOutput = {}, + craftsList = {}, + groupedCrafts = {}, +}) + +---@param playerName string +---@return CraftSystem.BaseCraftHandler.PlayerCraftsCache +function PlayerCraftsCache:new(playerName) + local instance = self:getInstance({}) + instance.playerName = playerName + + instance:update() + + return instance +end + +-- TODO +function PlayerCraftsCache:update() + -- ProgressionSystem: +end + +return PlayerCraftsCache diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftHandler.lua index a0af300..92b7fd4 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftHandler.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraftHandler.lua @@ -13,10 +13,7 @@ local ImmersiveCraft = require('CraftSystem.ImmersiveCraft') ---@class CraftSystem.ImmersiveCraftHandler : CraftSystem.BaseCraftHandler ---@field defaultCraftDef CraftSystem.BaseCraftDef --- ----@field new fun(self:CraftSystem.ImmersiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ImmersiveCraftType, def:CraftSystem.ImmersiveCraftHandlerDef):CraftSystem.ImmersiveCraftHandler ----@field getCraftByName fun(self, craftName:string):CraftSystem.ImmersiveCraft ----@field getCraftsByGroup fun(self, craftGroup):CraftSystem.ImmersiveCraft[] ----@field getLearnedCrafts fun(self, playerName):table +---@field new fun(self:CraftSystem.ImmersiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ImmersiveCraftType, def:CraftSystem.ImmersiveCraftHandlerDef):CraftSystem.ImmersiveCraftHandler local ImmersiveCraftHandler = BaseCraftHandler:extend({}) ---@class CraftSystem.ImmersiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef diff --git a/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraftHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraftHandler.lua index b882386..9f5f414 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraftHandler.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/PrimitiveCraftHandler.lua @@ -16,13 +16,11 @@ local PrimitiveCraft = require('CraftSystem.PrimitiveCraft') ---@class CraftSystem.PrimitiveCraftHandler : CraftSystem.BaseCraftHandler ---@field defaultCraftDef CraftSystem.PrimitiveCraftDef --- ----@field craftsByGroup table ----@field groupedCrafts (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[] +---@field craftsByGroup table +---@field craftsByOutput table +---@field groupedCrafts (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[] --- ----@field new fun(self:CraftSystem.PrimitiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.PrimitiveCraftType, def:CraftSystem.PrimitiveCraftHandlerDef):CraftSystem.PrimitiveCraftHandler ----@field getCraftByName fun(self, craftName:string):CraftSystem.PrimitiveCraft ----@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.PrimitiveCraft[] ----@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[] +---@field new fun(self:CraftSystem.PrimitiveCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.PrimitiveCraftType, def:CraftSystem.PrimitiveCraftHandlerDef):CraftSystem.PrimitiveCraftHandler local PrimitiveCraftHandler = BaseCraftHandler:extend({}) ---@class CraftSystem.PrimitiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef @@ -46,8 +44,18 @@ function PrimitiveCraftHandler:registerCraft(craftName, def) self.craftsByGroup[group] = {} end + local output = ItemStack(craft.output):get_name() + if self.craftsByOutput[output] == nil then + self.craftsByOutput[output] = {} + elseif craft.hidden == nil then + craft.hidden = true + end + self.craftsByName[craftName] = craft - tableInsert(self.craftsByGroup[group], craft) + if craft.hidden ~= true then + tableInsert(self.craftsByGroup[group], craft) + end + tableInsert(self.craftsByOutput[output], craft) tableInsert(self.craftsList, craft) end diff --git a/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua b/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua index d751d09..15015fc 100644 --- a/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua +++ b/mods/CORE/CraftSystem/src/CraftSystem/ProcessingCraftHandler.lua @@ -18,10 +18,7 @@ local ProcessingCraft = require('CraftSystem.ProcessingCraft') --- ---@field craftsByHash table --- ----@field new fun(self:CraftSystem.ProcessingCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ProcessingCraftType, def:CraftSystem.ProcessingCraftHandlerDef):CraftSystem.ProcessingCraftHandler ----@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)[] +---@field new fun(self:CraftSystem.ProcessingCraftHandler, baseType:CraftSystem.BaseCraftType, type:CraftSystem.ProcessingCraftType, def:CraftSystem.ProcessingCraftHandlerDef):CraftSystem.ProcessingCraftHandler local ProcessingCraftHandler = BaseCraftHandler:extend({ craftsByHash = {} }) diff --git a/mods/CORE/ImmersiveWorkStation/src/ImmersiveWorkStation.lua b/mods/CORE/ImmersiveWorkStation/src/ImmersiveWorkStation.lua index 1734267..015c0c1 100644 --- a/mods/CORE/ImmersiveWorkStation/src/ImmersiveWorkStation.lua +++ b/mods/CORE/ImmersiveWorkStation/src/ImmersiveWorkStation.lua @@ -170,7 +170,7 @@ function ImmersiveWorkStation:abortCraft(pos) return end - local craft = self.handler:getCraftByName(craftName) + local craft = self.handler:getCraftByName(craftName) --[[@as CraftSystem.ImmersiveCraft]] craft:abortCraft(pos) end diff --git a/mods/GAME/ITEMS/wood/init.lua b/mods/GAME/ITEMS/wood/init.lua index 188d322..f902896 100644 --- a/mods/GAME/ITEMS/wood/init.lua +++ b/mods/GAME/ITEMS/wood/init.lua @@ -271,6 +271,14 @@ local function registerWood(name, x, def) group = 'wood:logs_stripped', }) + InventoryCraftHandler:registerCraft('wood:'..name..'_log_stripped_2', { + recipe = { + 'wood:'..name..'_log 2' + }, + output = 'wood:'..name..'_log_stripped 2', + group = 'wood:logs_stripped', + }) + InventoryCraftHandler:registerCraft('wood:'..name..'_bar', { recipe = { 'wood:'..name..'_log' diff --git a/mods/SYSTEM/HelperLib/src/Lib/requires.lua b/mods/SYSTEM/HelperLib/src/Lib/requires.lua index 79a599a..acd62cd 100644 --- a/mods/SYSTEM/HelperLib/src/Lib/requires.lua +++ b/mods/SYSTEM/HelperLib/src/Lib/requires.lua @@ -64,6 +64,14 @@ function Smc.requireFormspec() return class end +---@return ProgressionSystem +function Smc.requireProgressionSystem() + ---@type ProgressionSystem + local class = Mod.getInfo('smc__core__progression_system').require('ProgressionSystem') + + return class +end + ---@return CraftSystem function Smc.requireCraftSystem() ---@type CraftSystem diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua index 9aaed12..2c5ef8d 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI.lua @@ -489,11 +489,16 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::alt_recipe_back', fu local playerName = player:get_player_name() local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) - local recipeIndex = craftTabData.selected_recipe_index or 0 + local recipeIndex = craftTabData.selected_recipe_index or 1 recipeIndex = recipeIndex - 1 - if recipeIndex < 0 then - recipeIndex = 0 + local handler = craftTabData.handler + local craft = handler:getCraftByName(craftTabData.selected_craft) + local crafts = handler:getLearnedCraftsByOutput(playerName, craft.output) + assert(crafts ~= nil) + + if recipeIndex < 1 then + recipeIndex = 1 end craftTabData.selected_recipe_index = recipeIndex @@ -508,9 +513,18 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('%S+::%S+::alt_recipe_forward', local playerName = player:get_player_name() local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) - local recipeIndex = craftTabData.selected_recipe_index or 0 + local recipeIndex = craftTabData.selected_recipe_index or 1 recipeIndex = recipeIndex + 1 + local handler = craftTabData.handler + local craft = handler:getCraftByName(craftTabData.selected_craft) + local crafts = handler:getLearnedCraftsByOutput(playerName, craft.output) + assert(crafts ~= nil) + + if recipeIndex > #crafts then + recipeIndex = #crafts + end + craftTabData.selected_recipe_index = recipeIndex PlayerInventoryUI:showForm(playerName) end) @@ -537,7 +551,7 @@ PlayerInventoryUI:registerOnFormFieldActionMatch('.+::%S+::%S+::item_button',fun local playerName = player:get_player_name() local craftTabData = PlayerInventoryUI:getTabCraftData(playerName, tabType, tabName) - craftTabData.selected_recipe_index = 0 + craftTabData.selected_recipe_index = 1 craftTabData.selected_craft = craftName PlayerInventoryUI:showForm(playerName) end) diff --git a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/baseGetCraftTab.lua b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/baseGetCraftTab.lua index 1a263be..d31b061 100644 --- a/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/baseGetCraftTab.lua +++ b/mods/UI/PlayerInventoryUI/src/PlayerInventoryUI/baseGetCraftTab.lua @@ -41,10 +41,10 @@ local function getCraftTabTemplate(self, playerName, tabType, tabName, handler) crafts = handler:getLearnedCraftsMatch(playerName, searchFieldValue) elseif selectedGroup == nil then ---@type (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[] - crafts = handler:getLearnedCrafts(playerName) + crafts = handler:getLearnedGroupedCrafts(playerName) else ---@type CraftSystem.PrimitiveCraft[] - crafts = handler:getCraftsByGroup(selectedGroup) + crafts = handler:getLearnedCraftsByGroup(playerName, selectedGroup) end local craftsListForm = Formspec:new() @@ -117,7 +117,10 @@ local function getCraftTabTemplate(self, playerName, tabType, tabName, handler) :label(2.5,8.5, ('%s/%s'):format(pageNumber, pagesMax)) if selectedCraft ~= '' then + local recipeIndex = craftTabData.selected_recipe_index or 1 local craft = handler:getCraftByName(selectedCraft) + craft = handler:getLearnedCraftsByOutput(playerName, craft.output)[recipeIndex] + local item = ItemStack(craft.output) local itemDef = item:get_definition() @@ -125,9 +128,12 @@ local function getCraftTabTemplate(self, playerName, tabType, tabName, handler) assert(player ~= nil) local mainList = player:get_inventory():get_list('main') + local craftsByOutput = handler:getLearnedCraftsByOutput(playerName, craft.output) + local recipeIndex = craftTabData.selected_recipe_index or 1 + CraftTab :itemImage(5.5, 0.5, 2, 2, craft.output) - :label(10, 0.5, ('%s/%s'):format(0+1, 'TODO')) -- TODO + :label(10, 0.5, ('%s/%s'):format(recipeIndex, #craftsByOutput)) :textArea(5.5, 3, 5, 4, nil, nil, craft:toString(mainList)) :button(5.5, 7.5, 0.5, 1, tabType..'::'..tabName..'::alt_recipe_back', '<') :button(6, 7.5, 2, 1, tabType..'::'..tabName..'::craft_item', '1')