CraftSystem: add groupedCrafts
This commit is contained in:
@@ -8,6 +8,6 @@ local BaseCraft = {}
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class CraftSystem.BaseCraftDef
|
||||
---@field group CraftSystem.CraftGroup
|
||||
---@field group CraftSystem.CraftGroup?
|
||||
|
||||
return BaseCraft
|
||||
|
||||
@@ -5,11 +5,13 @@ local PLAYERS_KNOW_ALL_RECIPES = true
|
||||
---@field craftsByName table<string, CraftSystem.BaseCraft>
|
||||
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.BaseCraft[]>
|
||||
---@field craftsList CraftSystem.BaseCraft[]
|
||||
---@field learnedCrafts table<string, table<string, CraftSystem.BaseCraft>>
|
||||
---@field groupedCrafts (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]
|
||||
---@field learnedCrafts table<string, (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]>
|
||||
local BaseCraftHandler = {
|
||||
craftsByName = {},
|
||||
craftsByGroup = {},
|
||||
craftsList = {},
|
||||
groupedCrafts = {},
|
||||
learnedCrafts = {},
|
||||
}
|
||||
|
||||
@@ -36,14 +38,13 @@ function BaseCraftHandler:getCraftsByGroup(craftGroup)
|
||||
end
|
||||
|
||||
---@param playerName string
|
||||
---@return table<string, CraftSystem.BaseCraft>
|
||||
---@return (CraftSystem.BaseCraft[]|CraftSystem.BaseCraft)[]
|
||||
function BaseCraftHandler:getLearnedCrafts(playerName)
|
||||
if PLAYERS_KNOW_ALL_RECIPES then
|
||||
return self.craftsByName
|
||||
return self.groupedCrafts
|
||||
end
|
||||
|
||||
local crafts = self.learnedCrafts[playerName]
|
||||
print(playerName)
|
||||
|
||||
if crafts == nil then
|
||||
crafts = {}
|
||||
|
||||
@@ -29,12 +29,13 @@ function ImmersiveCraftHandler:registerCraft(craftName, def)
|
||||
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||
craftName))
|
||||
|
||||
-- The craft group to which the craft belongs.
|
||||
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
||||
def.group = group
|
||||
|
||||
-- We obtain a crafting instance according to the given parameters.
|
||||
local craft = ImmersiveCraft:new(craftName, def)
|
||||
|
||||
-- The craft group to which the craft belongs.
|
||||
local group = def.group
|
||||
|
||||
-- If no craft with this group has been registered before...
|
||||
if self.craftsByGroup[group] == nil then
|
||||
-- ...then we create a list for this group.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
local extend = Class.extend
|
||||
|
||||
local tableInsert = table.insert
|
||||
local tableInsert, tableSort, tableMerge = table.insert, table.sort, table.merge
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||
local require = modInfo.require
|
||||
@@ -14,9 +14,11 @@ local PrimitiveCraft = require('CraftSystem.PrimitiveCraft')
|
||||
---A primitive crafting processor that doesn't require much interaction and provides
|
||||
---simple crafting based on a recipe-result scheme.
|
||||
---@class CraftSystem.PrimitiveCraftHandler : CraftSystem.BaseCraftHandler
|
||||
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.PrimitiveCraft[]>
|
||||
---@field groupedCrafts (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
||||
---@field getCraftByName fun(self, craftName:string):CraftSystem.PrimitiveCraft
|
||||
---@field getCraftsByGroup fun(self, craftGroup:string):CraftSystem.PrimitiveCraft[]
|
||||
---@field getLearnedCrafts fun(self, playerName:string):table<string, CraftSystem.PrimitiveCraft>
|
||||
---@field getLearnedCrafts fun(self, playerName:string):(CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
||||
local PrimitiveCraftHandler = extend(BaseCraftHandler, {})
|
||||
|
||||
---Register a primitive craft that does not require special interaction.
|
||||
@@ -26,9 +28,10 @@ function PrimitiveCraftHandler:registerCraft(craftName, def)
|
||||
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||
craftName))
|
||||
|
||||
local craft = PrimitiveCraft:new(craftName, def)
|
||||
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
||||
def.group = group
|
||||
|
||||
local group = def.group
|
||||
local craft = PrimitiveCraft:new(craftName, def)
|
||||
|
||||
if self.craftsByGroup[group] == nil then
|
||||
self.craftsByGroup[group] = {}
|
||||
@@ -39,4 +42,30 @@ function PrimitiveCraftHandler:registerCraft(craftName, def)
|
||||
tableInsert(self.craftsList, craft)
|
||||
end
|
||||
|
||||
core.register_on_mods_loaded(function()
|
||||
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
|
||||
tableSort(crafts, function(a, b)
|
||||
if a.output < b.output then
|
||||
return true
|
||||
end
|
||||
end)
|
||||
|
||||
if groupName ~= 'craft_group:UNDEFINED' then
|
||||
tableInsert(PrimitiveCraftHandler.groupedCrafts, crafts)
|
||||
else
|
||||
-- TODO: поменять Merge на Join
|
||||
PrimitiveCraftHandler.groupedCrafts = tableMerge(PrimitiveCraftHandler.groupedCrafts, crafts, false)
|
||||
end
|
||||
end
|
||||
|
||||
tableSort(PrimitiveCraftHandler.groupedCrafts, function(a, b)
|
||||
local outputA = (a[1] and a[1].output) or a.output
|
||||
local outputB = (b[1] and b[1].output) or b.output
|
||||
|
||||
if outputA < outputB then
|
||||
return true
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
return PrimitiveCraftHandler
|
||||
|
||||
Reference in New Issue
Block a user