CraftSystem: reworking the code, adding something useful
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
local tableJoin = table.join
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||
local require = modInfo.require
|
||||
|
||||
@@ -25,8 +27,9 @@ local CraftSystem = {
|
||||
|
||||
--- Craft Types ---
|
||||
|
||||
---@alias CraftSystem.BaseCraftType 'primitive'|'immersive'|'processing'
|
||||
---@alias CraftSystem.PrimitiveCraftType 'inside_player_inventory'
|
||||
---@alias CraftSystem.ImmersiveCraftType 'workbench'
|
||||
---@alias CraftSystem.ImmersiveCraftType 'workbench' -- DEBUG
|
||||
---@alias CraftSystem.ProcessingCraftType 'test' -- DEBUG
|
||||
---@alias CraftSystem.CraftType CraftSystem.PrimitiveCraftType|CraftSystem.ImmersiveCraftType|CraftSystem.ProcessingCraftType
|
||||
|
||||
@@ -35,7 +38,7 @@ local CraftSystem = {
|
||||
local function registerPrimitiveCraftType(type, def)
|
||||
assert(CraftSystem.PrimitiveCraftHandlers[type] == nil, ('A primitive craft type `%s` already exists.'):format(type))
|
||||
|
||||
local handler = PrimitiveCraftHandler:new(type, def)
|
||||
local handler = PrimitiveCraftHandler:new('primitive', type, def)
|
||||
CraftSystem.PrimitiveCraftHandlers[type] = handler
|
||||
end
|
||||
|
||||
@@ -44,7 +47,7 @@ end
|
||||
local function registerImmersiveCraftType(type, def)
|
||||
assert(CraftSystem.ImmersiveCraftHandlers[type] == nil, ('A immersive craft type `%s` already exists.'):format(type))
|
||||
|
||||
local handler = ImmersiveCraftHandler:new(type, def)
|
||||
local handler = ImmersiveCraftHandler:new('immersive', type, def)
|
||||
CraftSystem.ImmersiveCraftHandlers[type] = handler
|
||||
end
|
||||
|
||||
@@ -53,7 +56,7 @@ end
|
||||
local function registerProcessingCraftType(type, def)
|
||||
assert(CraftSystem.ProcessingCraftHandlers[type] == nil, ('A processing craft type `%s` already exists.'):format(type))
|
||||
|
||||
local handler = ProcessingCraftHandler:new(type, def)
|
||||
local handler = ProcessingCraftHandler:new('processing', type, def)
|
||||
CraftSystem.ProcessingCraftHandlers[type] = handler
|
||||
end
|
||||
|
||||
@@ -120,4 +123,112 @@ function CraftSystem:registerProcessingCraft(type, craftName, def)
|
||||
handler:registerCraft(craftName, def)
|
||||
end
|
||||
|
||||
--- Methods for performing crafting ---
|
||||
|
||||
|
||||
---@param craft CraftSystem.BaseCraft
|
||||
---@param recipeIndex integer
|
||||
---@param count integer
|
||||
---@return ItemStack[] materialStacks A table containing the items required for crafting.
|
||||
---@return table<string, integer> materialGroups A table containing the item groups required for crafting.
|
||||
local function getMaterials(craft, recipeIndex, count)
|
||||
local materialStacks = {}
|
||||
local materialGroups = {}
|
||||
|
||||
for _, itemName in ipairs(craft:getRecipe(recipeIndex)) do
|
||||
local group = itemName:match('group:(%S+)')
|
||||
local materialCount = itemName:match('group:%S+ (%S+)') or 1
|
||||
|
||||
if group == nil then
|
||||
local item = ItemStack(itemName)
|
||||
item:set_count(item:get_count() * count)
|
||||
|
||||
table.insert(materialStacks, item)
|
||||
else
|
||||
materialGroups[group] = (tonumber(materialCount) or 1) * count
|
||||
end
|
||||
end
|
||||
|
||||
return materialStacks, materialGroups
|
||||
end
|
||||
|
||||
---Check if the player has the required items.
|
||||
---@param materialStacks ItemStack[]
|
||||
---@param inventory InvRef
|
||||
---@return boolean
|
||||
local function checkMaterialStacks(materialStacks, inventory)
|
||||
for _, item in ipairs(materialStacks) do
|
||||
if not inventory:contains_item('main', item) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
---Check whether the player has the required number of items with the required group for crafting.
|
||||
---@param materialGroups table<string, integer>
|
||||
---@param inventory InvRef
|
||||
---@return boolean
|
||||
---@return ItemStack[] materialStacks A list of item stacks that match the required material groups.
|
||||
local function checkMaterialGroups(materialGroups, inventory)
|
||||
local materialStacks = {}
|
||||
|
||||
local mainList = inventory:get_list('main')
|
||||
for group, count in pairs(materialGroups) do
|
||||
for _, item in ipairs(mainList) do
|
||||
if core.get_item_group(item:get_name(), group) > 0 then
|
||||
local itemCount = item:get_count()
|
||||
if count > itemCount then
|
||||
count = count - itemCount
|
||||
table.insert(materialStacks, item)
|
||||
else
|
||||
item:set_count(count)
|
||||
table.insert(materialStacks, item)
|
||||
count = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if count > 0 then
|
||||
return false, {}
|
||||
end
|
||||
end
|
||||
|
||||
return true, materialStacks
|
||||
end
|
||||
|
||||
---Checks whether the player can perform crafting (whether he has the required materials in his inventory).
|
||||
---@param player Player
|
||||
---@param craft CraftSystem.BaseCraft
|
||||
---@param recipeIndex integer
|
||||
---@param count integer
|
||||
---@return boolean check true if craft can be produced
|
||||
---@return ItemStack[] materialStacks list of itemStacks for crafting
|
||||
function CraftSystem:checkPossiblePlayerInventoryCrafting(player, craft, recipeIndex, count, flags)
|
||||
local inventory = player:get_inventory()
|
||||
|
||||
local OutputStack = ItemStack(craft.output)
|
||||
OutputStack:set_count(OutputStack:get_count() * count)
|
||||
|
||||
local materialStacks, materialGroups = getMaterials(craft, recipeIndex, count)
|
||||
|
||||
local isHaveStacks = checkMaterialStacks(materialStacks, inventory)
|
||||
if not isHaveStacks then
|
||||
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
|
||||
return false, {}
|
||||
end
|
||||
|
||||
local isHaveGroups, materialGroupStacks = checkMaterialGroups(materialGroups, inventory)
|
||||
if not isHaveGroups then
|
||||
core.chat_send_player(player:get_player_name(), 'Not enough materials for crafting!')
|
||||
return false, {}
|
||||
end
|
||||
|
||||
materialStacks = tableJoin(materialStacks, materialGroupStacks)
|
||||
|
||||
|
||||
return true, materialStacks
|
||||
end
|
||||
|
||||
return CraftSystem
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
---A text indicator of the presence or absence
|
||||
---of a material in the player's inventory.
|
||||
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 recipe ItemStackString[]
|
||||
---@field altRecipes CraftSystem.CraftRecipe[]
|
||||
---@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 altRecipes CraftSystem.CraftRecipe[]?
|
||||
---@field output ItemStackString
|
||||
---@field group CraftSystem.CraftGroup?
|
||||
|
||||
---@param name string
|
||||
---@param def CraftSystem.BaseCraftDef
|
||||
@@ -17,12 +23,134 @@ local BaseCraft = Class:extend({})
|
||||
function BaseCraft:new(name, def)
|
||||
---@type CraftSystem.BaseCraft
|
||||
local instance = self:getInstance({})
|
||||
instance.name = name
|
||||
instance.recipe = def.recipe
|
||||
instance.output = def.output
|
||||
instance.group = def.group
|
||||
instance.name = name
|
||||
instance.recipe = def.recipe
|
||||
instance.altRecipes = def.altRecipes or {}
|
||||
instance.output = def.output
|
||||
instance.group = def.group
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---@param material ItemStack
|
||||
---@param count number
|
||||
---@param list ItemStack[]?
|
||||
---@return string
|
||||
local function getCheckMaterialItem(material, count, list)
|
||||
if list == nil then
|
||||
return CHECK.EMPTY
|
||||
end
|
||||
|
||||
local materialName = material:get_name()
|
||||
|
||||
for _, item in ipairs(list) do
|
||||
if materialName == item:get_name() then
|
||||
local itemCount = item:get_count()
|
||||
if count > itemCount then
|
||||
count = count - itemCount
|
||||
else
|
||||
return CHECK.YES
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return CHECK.NO
|
||||
end
|
||||
|
||||
---@param material string
|
||||
---@param count number
|
||||
---@param list ItemStack[]?
|
||||
---@return string
|
||||
local function getCheckMaterialGroup(material, count, list)
|
||||
if list == nil then
|
||||
return CHECK.EMPTY
|
||||
end
|
||||
|
||||
for _, item in ipairs(list) do
|
||||
if core.get_item_group(item:get_name(), material) > 0 then
|
||||
local itemCount = item:get_count()
|
||||
if count > itemCount then
|
||||
count = count - itemCount
|
||||
else
|
||||
return CHECK.YES
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return CHECK.NO
|
||||
end
|
||||
|
||||
---Returns the crafting recipe corresponding to the index.
|
||||
---@param index integer Recipe number. If `0` is specified,
|
||||
--- the primary craft (`craft.recipe`) will be used;
|
||||
--- if `1` or greater, the alternative craft
|
||||
--- corresponding to the index will be used (`craft.altRecipes`).
|
||||
---@return CraftSystem.CraftRecipe
|
||||
function BaseCraft:getRecipe(index)
|
||||
local recipe
|
||||
if index == 0 or self.altRecipes == nil then
|
||||
recipe = self.recipe
|
||||
else
|
||||
if index > #self.altRecipes then
|
||||
recipe = self.altRecipes[#self.altRecipes]
|
||||
else
|
||||
recipe = self.altRecipes[index]
|
||||
end
|
||||
end
|
||||
assert(recipe ~= nil)
|
||||
|
||||
return recipe
|
||||
end
|
||||
|
||||
---@param recipeIndex integer?
|
||||
---@param mainList ItemStack[]?
|
||||
---@return string
|
||||
function BaseCraft:recipeToString(recipeIndex, mainList)
|
||||
local recipe
|
||||
if recipeIndex == nil or recipeIndex == 0 or self.altRecipes == nil then
|
||||
recipe = self.recipe
|
||||
else
|
||||
if recipeIndex > #self.altRecipes then
|
||||
recipe = self.altRecipes[#self.altRecipes]
|
||||
else
|
||||
recipe = self.altRecipes[recipeIndex]
|
||||
end
|
||||
end
|
||||
assert(recipe ~= nil)
|
||||
|
||||
local recipeDesc = ''
|
||||
for _, itemName in ipairs(recipe) do
|
||||
local group, count = itemName:match('group:(%S+)'), itemName:match('group:%S+ (%S+)') or 1
|
||||
|
||||
if group == nil then
|
||||
local recipeItem = ItemStack(itemName)
|
||||
recipeDesc = recipeDesc..('%s %s x%s\n'):format(
|
||||
getCheckMaterialItem(recipeItem, recipeItem:get_count(), mainList),
|
||||
core.strip_colors(recipeItem:get_definition()._title),
|
||||
recipeItem:get_count())
|
||||
else
|
||||
recipeDesc = recipeDesc..('%s any item from the group %s x%s\n'):format(
|
||||
getCheckMaterialGroup(group, count, mainList),
|
||||
group,
|
||||
count)
|
||||
end
|
||||
end
|
||||
|
||||
return recipeDesc
|
||||
end
|
||||
|
||||
---@param recipeIndex integer?
|
||||
---@param mainList ItemStack[]?
|
||||
---@return string
|
||||
function BaseCraft:toString(recipeIndex, mainList)
|
||||
local outputItem = ItemStack(self.output)
|
||||
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
||||
|
||||
local craftDesc = ('%s\n\nNeeded:\n%s'):format(
|
||||
outputItemSettings._title,
|
||||
self:recipeToString(recipeIndex, mainList))
|
||||
|
||||
return craftDesc
|
||||
end
|
||||
|
||||
return BaseCraft
|
||||
|
||||
@@ -5,6 +5,7 @@ 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
|
||||
---
|
||||
@@ -26,12 +27,14 @@ local BaseCraftHandler = Class:extend({
|
||||
---@class CraftSystem.BaseCraftHandlerDef
|
||||
---@field defaultCraftDef CraftSystem.BaseCraftDef?
|
||||
|
||||
---@param type CraftSystem.CraftType
|
||||
---@param def CraftSystem.BaseCraftHandlerDef
|
||||
---@return CraftSystem.BaseCraftHandler
|
||||
function BaseCraftHandler:new(type, def)
|
||||
---@param baseType CraftSystem.BaseCraftType
|
||||
---@param type CraftSystem.CraftType
|
||||
---@param def CraftSystem.BaseCraftHandlerDef
|
||||
---@return CraftSystem.BaseCraftHandler
|
||||
function BaseCraftHandler:new(baseType, type, def)
|
||||
---@type CraftSystem.BaseCraftHandler
|
||||
local instance = self:getInstance({})
|
||||
instance.baseType = baseType
|
||||
instance.type = type
|
||||
instance.defaultCraftDef = def.defaultCraftDef or {}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
local getInstance, extend = Class.getInstance, Class.extend
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||
local require = modInfo.require
|
||||
|
||||
@@ -18,7 +16,6 @@ local stageTypes = {
|
||||
---Immersive crafting aimed at a more logical process of crafting items.
|
||||
---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft
|
||||
---@field startStageName string
|
||||
---@field triggerItem ItemStackString
|
||||
---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage>
|
||||
local ImmersiveCraft = BaseCraft:extend({})
|
||||
|
||||
@@ -27,33 +24,27 @@ local ImmersiveCraft = BaseCraft:extend({})
|
||||
---**Only for EmmyLua.**
|
||||
---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef
|
||||
---@field startStageName string
|
||||
---@field triggerItem ItemStackString
|
||||
---@field stages CraftSystem.ImmersiveCraftStageDef[]
|
||||
|
||||
---@param name string
|
||||
---@param def CraftSystem.ImmersiveCraftDef
|
||||
---@return CraftSystem.ImmersiveCraft
|
||||
function ImmersiveCraft:new(name, def)
|
||||
-- We create a table of crafting stages that the player needs to complete.
|
||||
---@type table<string, CraftSystem.ImmersiveCraftStage>
|
||||
local stagesByName = {}
|
||||
|
||||
for _, stageDef in ipairs(def.stages) do
|
||||
-- We get the stage class based on its type.
|
||||
local StageClass = stageTypes[stageDef.type]
|
||||
|
||||
-- Create a stage and add the craft name to the instance.
|
||||
stageDef.craftName = name
|
||||
local stage = StageClass:new(stageDef)
|
||||
|
||||
-- We add an instance of the stage to the table by name.
|
||||
stagesByName[stageDef.name] = stage
|
||||
end
|
||||
|
||||
---@type CraftSystem.ImmersiveCraft
|
||||
local instance = BaseCraft:new(name, def)
|
||||
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||
instance.startStageName = def.startStageName
|
||||
instance.triggerItem = def.triggerItem
|
||||
instance.stagesByName = stagesByName
|
||||
|
||||
-- Subscribe to an event that will determine that the player has completed the crafting stage.
|
||||
@@ -63,27 +54,17 @@ function ImmersiveCraft:new(name, def)
|
||||
---@param pos MapPosition
|
||||
---@param Player Player
|
||||
function(eventName, stage, pos, Player)
|
||||
-- From the current stage (which has been completed) we get the name of the next one.
|
||||
local nextStageName = stage.nextStage
|
||||
|
||||
-- If there is no next stage...
|
||||
if nextStageName == nil then
|
||||
-- ...we are finishing the craft.
|
||||
instance:endCraft(pos, Player)
|
||||
else
|
||||
-- ...else we start the next stage.
|
||||
local nextStage = instance.stagesByName[nextStageName]
|
||||
|
||||
-- We get a table of temporary data.
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
meta:set_string('stage_name', nextStage.name)
|
||||
|
||||
-- We are updating the crafting stage in it.
|
||||
craftData.stageName = nextStageName
|
||||
-- We save it to the workstation metadata.
|
||||
Smc.setCraftDataByPos(pos, craftData)
|
||||
|
||||
-- We are starting the next stage of crafting.
|
||||
nextStage:startHandler(pos, Player)
|
||||
end
|
||||
end)
|
||||
@@ -99,10 +80,15 @@ end
|
||||
---@param amount integer
|
||||
---@return boolean
|
||||
function ImmersiveCraft:startCraft(pos, player, amount)
|
||||
-- Adding craft to the counter.
|
||||
-- If crafting has not been added...
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
local craftName = meta:get_string('craft_name')
|
||||
if craftName ~= '' then
|
||||
core.chat_send_player(player:get_player_name(), 'The workbench is occupied by another craft.')
|
||||
return false
|
||||
end
|
||||
|
||||
if not ImmersiveCraftCounter:addCraft(pos, player:get_player_name()) then
|
||||
-- ...then we don't let the player start a new craft.
|
||||
core.chat_send_player(player:get_player_name(),
|
||||
'The limit of simultaneously active crafts has been reached: '..
|
||||
'complete or cancel active crafts to start a new one')
|
||||
@@ -110,42 +96,13 @@ function ImmersiveCraft:startCraft(pos, player, amount)
|
||||
return false
|
||||
end
|
||||
|
||||
-- We get the material as an ItemStack.
|
||||
---@type ItemStack
|
||||
local stack = ItemStack(self.triggerItem)
|
||||
-- We edit the amount of materials in accordance with the number of crafts being performed.
|
||||
stack:set_count(stack:get_count() * amount)
|
||||
|
||||
-- We get the player's inventory, from where we must take the materials for crafting.
|
||||
local playerInventory = player:get_inventory()
|
||||
|
||||
-- If the player does not have the necessary materials for crafting...
|
||||
if not playerInventory:contains_item('main', stack) then
|
||||
-- ...we inform him about this.
|
||||
core.chat_send_player(player:get_player_name(),
|
||||
'Not enough materials for crafting')
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
-- If we have the materials, we take them and start initializing the craft.
|
||||
playerInventory:remove_item('main', stack)
|
||||
|
||||
-- We get the first stage of crafting.
|
||||
local stage = self.stagesByName[self.startStageName]
|
||||
|
||||
-- Create a temporary data table for crafting.
|
||||
---@type ImmersiveCraftData
|
||||
local craftData = {
|
||||
playerName = player:get_player_name(), -- The name of the player who started the craft.
|
||||
craftName = self.name,
|
||||
stageName = stage.name,
|
||||
amount = amount,
|
||||
}
|
||||
-- We save it to the workstation metadata.
|
||||
Smc.setCraftDataByPos(pos, craftData)
|
||||
meta:set_string('player_name', player:get_player_name())
|
||||
meta:set_string('craft_name', self.name)
|
||||
meta:set_string('stage_name', stage.name)
|
||||
meta:set_float('craft_amount', amount)
|
||||
|
||||
-- We are starting the first stage of crafting.
|
||||
stage:startHandler(pos, player)
|
||||
|
||||
return true
|
||||
@@ -155,77 +112,84 @@ end
|
||||
---@param pos MapPosition
|
||||
---@param player Player
|
||||
function ImmersiveCraft:endCraft(pos, player)
|
||||
-- We get current data on crafting.
|
||||
---@type ImmersiveCraftData?
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
local playerName = meta:get_string('player_name')
|
||||
assert(playerName ~= '')
|
||||
local amount = meta:get_float('craft_amount')
|
||||
assert(amount ~= 0)
|
||||
|
||||
-- We drop the result of crafting.
|
||||
-- Drop the result of crafting.
|
||||
local dropPos = vector.new(pos) + vector.new(0, 0.9,0)
|
||||
for i=1, craftData.amount do
|
||||
for i=1, amount do
|
||||
core.item_drop(ItemStack(self.output), nil, dropPos)
|
||||
end
|
||||
|
||||
-- Remove craft from the counter.
|
||||
ImmersiveCraftCounter:removeCraft(pos, craftData.playerName)
|
||||
ImmersiveCraftCounter:removeCraft(pos, playerName)
|
||||
|
||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
||||
Smc.setCraftDataByPos(pos)
|
||||
-- Clear craft data
|
||||
meta:set_string('player_name', '')
|
||||
meta:set_string('craft_name', '')
|
||||
meta:set_string('stage_name', '')
|
||||
meta:set_float('craft_amount', 0)
|
||||
meta:set_string('stage_data', '')
|
||||
end
|
||||
|
||||
---Stop the crafting process and free up the workbench.
|
||||
---@param pos MapPosition
|
||||
function ImmersiveCraft:abortCraft(pos)
|
||||
-- We get current data on crafting.
|
||||
---@type ImmersiveCraftData?
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
local playerName = meta:get_string('player_name')
|
||||
assert(playerName ~= '')
|
||||
local stageName = meta:get_string('stage_name')
|
||||
assert(stageName ~= '')
|
||||
|
||||
-- We get the current stage and start the abort process.
|
||||
local stage = self.stagesByName[craftData.stageName]
|
||||
local stage = self.stagesByName[stageName]
|
||||
stage:abortHandler(pos)
|
||||
|
||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
||||
Smc.setCraftDataByPos(pos)
|
||||
-- Clear craft data
|
||||
meta:set_string('player_name', '')
|
||||
meta:set_string('craft_name', '')
|
||||
meta:set_string('stage_name', '')
|
||||
meta:set_float('craft_amount', 0)
|
||||
meta:set_string('stage_data', '')
|
||||
|
||||
-- Remove craft from the counter.
|
||||
ImmersiveCraftCounter:removeCraft(pos, craftData.playerName)
|
||||
ImmersiveCraftCounter:removeCraft(pos, playerName)
|
||||
end
|
||||
|
||||
---Returns a human-readable description of the crafting process.
|
||||
---@return string
|
||||
function ImmersiveCraft:toString()
|
||||
-- We get the material as an ItemStack.
|
||||
---@type ItemStack
|
||||
local stack = ItemStack(self.triggerItem)
|
||||
-- We obtain a table of item parameters.
|
||||
---@type ItemDefinition
|
||||
local itemDef = stack:get_definition()
|
||||
|
||||
-- Let's start writing a description of the craft.
|
||||
local craftDesc = ('Material: %s x%s\n\n'):format(itemDef._title, stack:get_count())..
|
||||
'Crafting process:\n'
|
||||
|
||||
-- Let's start with the first crafting stage.
|
||||
function ImmersiveCraft:recipeToString(recipeIndex, mainList)
|
||||
local recipeDesc = ''
|
||||
---@type string?
|
||||
local stageName = self.startStageName
|
||||
local stages = self.stagesByName
|
||||
|
||||
-- While we have a stage name...
|
||||
local count = 1
|
||||
while stageName ~= nil do
|
||||
-- We get a crafting stage...
|
||||
local stage = stages[stageName]
|
||||
|
||||
-- ...and we add its description to the list of actions for crafting.
|
||||
craftDesc = craftDesc..tostring(count)..'. '..stage:toString()..'\n'
|
||||
recipeDesc = recipeDesc..tostring(count)..'. '..stage:toString()..'\n'
|
||||
|
||||
-- We also get the name of the next stage.
|
||||
stageName = stage.nextStage
|
||||
end
|
||||
|
||||
-- If the cycle is interrupted, it means we have processed all the stages.
|
||||
-- Return the craft description.
|
||||
return recipeDesc
|
||||
end
|
||||
|
||||
---@param recipeIndex integer?
|
||||
---@param mainList ItemStack[]?
|
||||
---@return string
|
||||
function ImmersiveCraft:toString(recipeIndex, mainList)
|
||||
local outputItem = ItemStack(self.output)
|
||||
local outputItemSettings = Smc.getItemDefinition(outputItem).settings
|
||||
|
||||
local craftDesc = ('%s\n\nRecipe:\n%s\nActions:\n%s'):format(
|
||||
outputItemSettings._title,
|
||||
BaseCraft.recipeToString(self, recipeIndex, mainList),
|
||||
self:recipeToString(recipeIndex, mainList))
|
||||
|
||||
return craftDesc
|
||||
end
|
||||
|
||||
|
||||
@@ -13,15 +13,11 @@ local ImmersiveCraft = require('CraftSystem.ImmersiveCraft')
|
||||
---@class CraftSystem.ImmersiveCraftHandler : CraftSystem.BaseCraftHandler
|
||||
---@field defaultCraftDef CraftSystem.BaseCraftDef
|
||||
---
|
||||
---@field craftsByTriggerItem table<string, CraftSystem.ImmersiveCraft[]>
|
||||
---
|
||||
---@field new fun(self:CraftSystem.ImmersiveCraftHandler, type:CraftSystem.ImmersiveCraftType, def:CraftSystem.ImmersiveCraftHandlerDef):CraftSystem.ImmersiveCraftHandler
|
||||
---@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<string, CraftSystem.ImmersiveCraft>
|
||||
local ImmersiveCraftHandler = BaseCraftHandler:extend({
|
||||
craftsByTriggerItem = {},
|
||||
})
|
||||
local ImmersiveCraftHandler = BaseCraftHandler:extend({})
|
||||
|
||||
---@class CraftSystem.ImmersiveCraftHandlerDef : CraftSystem.BaseCraftHandlerDef
|
||||
---@field defaultCraftDef CraftSystem.ImmersiveCraftDef?
|
||||
@@ -36,46 +32,22 @@ function ImmersiveCraftHandler:registerCraft(craftName, def)
|
||||
|
||||
local def = table.merge(def, self.defaultCraftDef)
|
||||
|
||||
-- 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)
|
||||
local craft = (ImmersiveCraft:new(craftName, def))
|
||||
|
||||
-- If no craft with this group has been registered before...
|
||||
if self.craftsByGroup[group] == nil then
|
||||
-- ...then we create a list for this group.
|
||||
self.craftsByGroup[group] = {}
|
||||
end
|
||||
|
||||
-- The material from which crafting begins (also known as the starting item).
|
||||
local triggerItem = def.triggerItem
|
||||
|
||||
-- If no crafting for such a starting item has been registered before...
|
||||
if self.craftsByTriggerItem[triggerItem] == nil then
|
||||
-- ...then we create a list for this item.
|
||||
self.craftsByTriggerItem[triggerItem] = {}
|
||||
end
|
||||
|
||||
-- Adding craft to the lists:
|
||||
self.craftsByName[craftName] = craft
|
||||
tableInsert(self.craftsByGroup[group], craft)
|
||||
tableInsert(self.craftsByTriggerItem[triggerItem], craft)
|
||||
tableInsert(self.craftsList, craft)
|
||||
end
|
||||
|
||||
---Returns a list of crafts that use the given item as a starting material.
|
||||
---
|
||||
---Will return `nil` if the item is not used in any craft as a starting item.
|
||||
---@param triggerItem string
|
||||
---@return CraftSystem.ImmersiveCraft[]?
|
||||
function ImmersiveCraftHandler:getCraftsByTriggerItem(triggerItem)
|
||||
local crafts = self.craftsByTriggerItem[triggerItem]
|
||||
|
||||
return crafts
|
||||
end
|
||||
|
||||
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||
-- Provides a convenient list of crafting items in the player's inventory.
|
||||
core.register_on_mods_loaded(function()
|
||||
for groupName, crafts in pairs(ImmersiveCraftHandler.craftsByGroup) do
|
||||
tableSort(crafts, function(a, b)
|
||||
|
||||
@@ -40,11 +40,9 @@ local tappingNode = ImmersiveCraftNode:new({
|
||||
callbacks = {
|
||||
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||
on_activate = function(luaEntity, staticdata, dtime)
|
||||
-- We obtain a table of initial parameters for the craft node entity.
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingNode.InitParams?
|
||||
local params = core.deserialize(staticdata)
|
||||
|
||||
-- We enter parameters into the entity's custom fields.
|
||||
if params ~= nil then
|
||||
luaEntity._item_name = params.targetItem
|
||||
luaEntity._craft_name = params.craftName
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
local extend = Class.extend
|
||||
|
||||
local getStageDataByPos, setStageDataByPos = Smc.getStageDataByPos, Smc.setStageDataByPos
|
||||
|
||||
local require = Mod.getInfo('smc__core__craft_system').require
|
||||
|
||||
---@type CraftSystem.ImmersiveCraftStage
|
||||
@@ -29,7 +27,7 @@ local TappingStage = extend(ImmersiveCraftStage, {})
|
||||
---Data table for transferring various time parameters between calls.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class ImmersiveStageData.TappingStageData : ImmersiveStageData
|
||||
---@class CraftSystem.ImmersiveCraftStage.TappingStageData
|
||||
---How many clicks were made by the player on the entity.
|
||||
---@field clickCount integer
|
||||
---Unique identifier of the entity (craft node).
|
||||
@@ -45,10 +43,8 @@ function TappingStage:new(def)
|
||||
|
||||
-- Registering the crafting stage start handler.
|
||||
instance.startHandler = function(tappingStage , pos, player)
|
||||
-- We determine the coordinate where we will create the crafting node (entity).
|
||||
local entityPos = vector.new(pos) + vector.new(0,0.875,0)
|
||||
|
||||
-- We create a table of initial parameters that we will pass to the entity.
|
||||
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
||||
local params = {
|
||||
targetItem = tappingStage.targetItem,
|
||||
@@ -57,89 +53,83 @@ function TappingStage:new(def)
|
||||
WorkStationPos = pos,
|
||||
}
|
||||
|
||||
-- Adding an entity to the world.
|
||||
local obj = core.add_entity(entityPos, 'craft_system:tapping_node', core.serialize(params))
|
||||
|
||||
-- If the entity was not returned, something went wrong.
|
||||
if obj == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- Create a temporary table of crafting stage data.
|
||||
---@type ImmersiveStageData.TappingStageData
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageData
|
||||
local stageData = {
|
||||
clickCount = 0,
|
||||
entityGUID = obj:get_guid()
|
||||
}
|
||||
|
||||
-- Saving temporary data in work station metadata.
|
||||
setStageDataByPos(pos, stageData)
|
||||
--@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
meta:set_string('stage_data', core.serialize(stageData))
|
||||
end
|
||||
|
||||
-- Registering the crafting stage process handler.
|
||||
-- Called when a player clicks on a crafting node (entity).
|
||||
instance.processHandler = function(tappingStage, pos, player)
|
||||
-- Obtaining stage time data from work station metadata.
|
||||
---@type ImmersiveStageData.TappingStageData?
|
||||
local stageData = getStageDataByPos(pos)
|
||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||
assert(stageData ~= nil)
|
||||
|
||||
-- Increasing the click counter
|
||||
local clickCount = stageData.clickCount
|
||||
clickCount = clickCount + 1
|
||||
|
||||
-- If the number of clicks is greater than required to complete the step...
|
||||
if clickCount >= tappingStage.clicksNeed then
|
||||
-- ...we are completing the stage.
|
||||
tappingStage:endHandler(pos, player)
|
||||
else
|
||||
-- ...else we save the number of clicks in the temporary data table.
|
||||
stageData.clickCount = clickCount
|
||||
Smc.setStageDataByPos(pos, stageData)
|
||||
meta:set_string('stage_data', core.serialize(stageData))
|
||||
end
|
||||
end
|
||||
|
||||
-- Registering the crafting stage end handler.
|
||||
instance.endHandler = function(tappingStage, pos, player)
|
||||
-- Obtaining stage time data from work station metadata.
|
||||
---@type ImmersiveStageData.TappingStageData?
|
||||
local stageData = getStageDataByPos(pos)
|
||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||
assert(stageData ~= nil)
|
||||
|
||||
|
||||
-- We get the entity object.
|
||||
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||
|
||||
-- Removing an entity from the world.
|
||||
entity:remove()
|
||||
|
||||
-- We trigger an event that the crafting stage has been completed.
|
||||
Events.call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, player)
|
||||
end
|
||||
|
||||
-- Registering the aborting the crafting process handler.
|
||||
---@param tappingStage CraftSystem.ImmersiveCraftStage.TappingStage
|
||||
instance.abortHandler = function(tappingStage, pos)
|
||||
-- We receive current data on crafting.
|
||||
---@type ImmersiveCraftData?
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||
local stageData = core.deserialize(meta:get_string('stage_data'))
|
||||
assert(stageData ~= nil)
|
||||
local amount = meta:get_float('craft_amount')
|
||||
assert(amount ~= 0)
|
||||
|
||||
-- Obtaining stage time data from work station metadata.
|
||||
---@type ImmersiveStageData.TappingStageData?
|
||||
local stageData = craftData.stageData
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageData?
|
||||
local stageData = stageData
|
||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||
|
||||
-- We get the entity object.
|
||||
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||
|
||||
-- Removing an entity from the world.
|
||||
entity:remove()
|
||||
|
||||
-- We drop the item as it was before processing.
|
||||
local dropPos = vector.new(pos)+vector.new(0, 0.9,0)
|
||||
for i=1, craftData.amount do
|
||||
for i=1, amount do
|
||||
core.item_drop(ItemStack(tappingStage.targetItem), nil, dropPos)
|
||||
end
|
||||
end
|
||||
@@ -161,12 +151,10 @@ function TappingStage:new(def)
|
||||
---@param craftNode LuaEntity
|
||||
---@param player Player
|
||||
function(eventName, pos, player, craftNode)
|
||||
-- We get the item that the player clicked on the crafting node.
|
||||
local toolDef = player:get_wielded_item():get_definition()
|
||||
|
||||
-- If a crafting stage requires a specific tool group and he doesn't have it...
|
||||
-- If a crafting stage requires a specific tool group and he doesn't have it then we don't do anything.
|
||||
if instance.toolGroup and (not toolDef.groups or not toolDef.groups[instance.toolGroup]) then
|
||||
-- ...then we don't do anything.
|
||||
return
|
||||
end
|
||||
|
||||
@@ -189,7 +177,6 @@ function TappingStage:new(def)
|
||||
core.after(0.2, function() craftNode.object:set_rotation(vector.new(0,0,0))end)
|
||||
-- DRAFT END
|
||||
|
||||
-- Calling the crafting stage process handler.
|
||||
instance:processHandler(pos, player)
|
||||
end)
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ local require = modInfo.require
|
||||
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||
|
||||
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
|
||||
---@field altRecipes CraftSystem.CraftRecipe[]
|
||||
---@field returnItems ItemStackString[]?
|
||||
local PrimitiveCraft = BaseCraft:extend({})
|
||||
|
||||
@@ -15,7 +14,6 @@ local PrimitiveCraft = BaseCraft:extend({})
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
|
||||
---@field altRecipes CraftSystem.CraftRecipe[]?
|
||||
---@field returnItems ItemStackString[]?
|
||||
|
||||
---@param name string
|
||||
@@ -23,8 +21,7 @@ local PrimitiveCraft = BaseCraft:extend({})
|
||||
---@return CraftSystem.PrimitiveCraft
|
||||
function PrimitiveCraft:new(name, def)
|
||||
---@type CraftSystem.PrimitiveCraft
|
||||
local instance = BaseCraft:new(name, def)
|
||||
instance.altRecipes = def.altRecipes or {}
|
||||
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||
instance.returnItems = def.returnItems
|
||||
|
||||
return instance
|
||||
|
||||
@@ -19,7 +19,7 @@ local PrimitiveCraft = require('CraftSystem.PrimitiveCraft')
|
||||
---@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 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)[]
|
||||
@@ -62,6 +62,8 @@ function PrimitiveCraftHandler:registerAlternativeRecipes(craftName, recipes)
|
||||
craft.altRecipes = tableJoin(craft.altRecipes or {}, recipes)
|
||||
end
|
||||
|
||||
-- Sort the list of crafts in alphabetical order by product name and combine crafts into groups.
|
||||
-- Provides a convenient list of crafting items in the player's inventory.
|
||||
core.register_on_mods_loaded(function()
|
||||
for groupName, crafts in pairs(PrimitiveCraftHandler.craftsByGroup) do
|
||||
tableSort(crafts, function(a, b)
|
||||
|
||||
@@ -29,7 +29,7 @@ local ProcessingCraft = BaseCraft:extend({})
|
||||
---@return CraftSystem.ProcessingCraft
|
||||
function ProcessingCraft:new(name, def)
|
||||
---@type CraftSystem.ProcessingCraft
|
||||
local instance = BaseCraft:new(name, def)
|
||||
local instance = self:getInstance(BaseCraft:new(name, def))
|
||||
instance.time = def.time
|
||||
instance.startHandler = def.startHandler or function() end
|
||||
instance.processHandler = def.processHandler or function() end
|
||||
|
||||
@@ -18,7 +18,7 @@ local ProcessingCraft = require('CraftSystem.ProcessingCraft')
|
||||
---
|
||||
---@field craftsByHash table<string, CraftSystem.ProcessingCraft>
|
||||
---
|
||||
---@field new fun(self:CraftSystem.ProcessingCraftHandler, type:CraftSystem.ProcessingCraftType, def:CraftSystem.ProcessingCraftHandlerDef):CraftSystem.ProcessingCraftHandler
|
||||
---@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)[]
|
||||
|
||||
Reference in New Issue
Block a user