3 Commits

Author SHA1 Message Date
0e59cf799a CraftSystem: change table.merge -> table.join 2026-04-03 19:26:16 +03:00
89de48b5a8 Table: add table.join from Voxrame 2026-04-03 19:18:54 +03:00
362d626f9b CraftSystem: add groupedCrafts 2026-04-03 18:42:18 +03:00
5 changed files with 72 additions and 12 deletions

View File

@@ -8,6 +8,6 @@ local BaseCraft = {}
---
---**Only for EmmyLua.**
---@class CraftSystem.BaseCraftDef
---@field group CraftSystem.CraftGroup
---@field group CraftSystem.CraftGroup?
return BaseCraft

View File

@@ -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 = {}

View File

@@ -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.

View File

@@ -1,6 +1,6 @@
local extend = Class.extend
local tableInsert = table.insert
local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join
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,29 @@ 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
PrimitiveCraftHandler.groupedCrafts = tableJoin(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

View File

@@ -170,6 +170,36 @@ function table.merge(t1, t2, recursively)
return result
end
---Adds key-value from `table2` into `table1` only if that key does not exists in `table1`.
---Values that are tables are copied by `table.copy()`.
---If `recursively` is true, the function will be applied recursively for values
---that are tables in both `table1` and `table2`.
---
---
---@overload fun(t1:table, t2:table):table
---
---@generic T: table, U: table
---@param t1 T
---@param t2 U
---@param recursively boolean Default: false. Go recursively if both values are tables.
---
---Original source: [Voxrame](https://github.com/Voxrame/Voxrame/blob/d5255d466ca502267023c146696a655cf09595b3/helpers/src/lua_ext/table.lua#L161)
---@return T|U
function table.join(t1, t2, recursively)
recursively = recursively or false
for key, value in pairs(t2) do
if not t1[key] then
t1[key] = type(value) == 'table' and tableCopy(value) or value
else
if recursively and type(t1[key]) == 'table' and type(value) == 'table' then
table.join(t1[key], value, recursively)
end
end
end
return t1
end
--- Returns an array containing only unique values of the table `t`.
--- @param t any[] Array to check
--- @return any[]