91 lines
3.3 KiB
Lua
91 lines
3.3 KiB
Lua
local extend = Class.extend
|
|
|
|
local tableInsert, tableSort, tableJoin = table.insert, table.sort, table.join
|
|
|
|
local modInfo = Mod.getInfo('smc__core__craft_system')
|
|
local require = modInfo.require
|
|
|
|
---@type CraftSystem.BaseCraftHandler
|
|
local BaseCraftHandler = require('CraftSystem.BaseCraftHandler')
|
|
|
|
---@type CraftSystem.PrimitiveCraft
|
|
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 defaultCraftDef CraftSystem.PrimitiveCraftDef
|
|
---
|
|
---@field craftsByGroup table<CraftSystem.CraftGroup, CraftSystem.PrimitiveCraft[]>
|
|
---@field groupedCrafts (CraftSystem.PrimitiveCraft[]|CraftSystem.PrimitiveCraft)[]
|
|
---
|
|
---@field new fun(self:CraftSystem.PrimitiveCraftHandler, 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)[]
|
|
local PrimitiveCraftHandler = BaseCraftHandler:extend({})
|
|
|
|
---@class CraftSystem.PrimitiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef
|
|
---@field defaultCraftDef CraftSystem.PrimitiveCraftDef?
|
|
|
|
---Register a primitive craft that does not require special interaction.
|
|
---@param craftName string
|
|
---@param def CraftSystem.PrimitiveCraftDef
|
|
function PrimitiveCraftHandler:registerCraft(craftName, def)
|
|
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
|
craftName))
|
|
|
|
local def = table.merge(def, self.defaultCraftDef)
|
|
|
|
local group = 'craft_group:' .. (def.group or 'UNDEFINED')
|
|
def.group = group
|
|
|
|
local craft = PrimitiveCraft:new(craftName, def)
|
|
|
|
if self.craftsByGroup[group] == nil then
|
|
self.craftsByGroup[group] = {}
|
|
end
|
|
|
|
self.craftsByName[craftName] = craft
|
|
tableInsert(self.craftsByGroup[group], craft)
|
|
tableInsert(self.craftsList, craft)
|
|
end
|
|
|
|
---Adds alternative recipes to the craft with the specified name.
|
|
---
|
|
---If a craft with the specified name does not exist, an error will call.
|
|
---@param craftName string
|
|
---@param recipes CraftSystem.CraftRecipe[]
|
|
function PrimitiveCraftHandler:registerAlternativeRecipes(craftName, recipes)
|
|
local craft = PrimitiveCraftHandler:getCraftByName(craftName)
|
|
|
|
craft.altRecipes = tableJoin(craft.altRecipes or {}, recipes)
|
|
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)
|
|
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
|