Files
simple-makes-complex/mods/CORE/CraftSystem/src/CraftSystem/ImmersiveCraft.lua

238 lines
7.9 KiB
Lua

local getInstance, extend = Class.getInstance, Class.extend
local modInfo = Mod.getInfo('smc__core__craft_system')
local require = modInfo.require
---@type CraftSystem.BaseCraft
local BaseCraft = require('CraftSystem.BaseCraft')
---@type CraftSystem.ImmersiveCraftCounter
local ImmersiveCraftCounter = require('CraftSystem.ImmersiveCraftCounter')
---Types of immersive stages.
---@class CraftSystem.StagesTypes : table<CraftSystem.ImmersiveCraftStage.Type, CraftSystem.ImmersiveCraftStage>
local stageTypes = {
---@type CraftSystem.ImmersiveCraftStage.TappingStage
tapping = require('CraftSystem.ImmersiveCraftStage.TappingStage'),
}
---Immersive crafting aimed at a more logical process of crafting items.
---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft
---@field startStageName string
---@field triggerItem ItemStackString
---@field output ItemStackString
---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage>
local ImmersiveCraft = extend(BaseCraft, {})
---Definition table for the `CraftSystem.ImmersiveCraft`.
---
---**Only for EmmyLua.**
---@class CraftSystem.ImmersiveCraftDef : CraftSystem.BaseCraftDef
---@field startStageName string
---@field triggerItem ItemStackString
---@field output 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 = getInstance(self, {})
instance.name = name
instance.group = def.group
instance.startStageName = def.startStageName
instance.triggerItem = def.triggerItem
instance.output = def.output
instance.stagesByName = stagesByName
-- Subscribe to an event that will determine that the player has completed the crafting stage.
Events.subscribe('craft_system:next_immersive_stage:'..name,
---@param eventName string
---@param stage CraftSystem.ImmersiveCraftStage
---@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.')
-- 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)
return instance
end
---Initialize the start of the immersive crafting process.
---
---Returns `true` if the crafting process was running.
---@param pos MapPosition
---@param player Player
---@param amount integer
---@return boolean
function ImmersiveCraft:startCraft(pos, player, amount)
-- Adding craft to the counter.
-- If crafting has not been added...
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')
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)
-- We are starting the first stage of crafting.
stage:startHandler(pos, player)
return true
end
---Complete the immersive crafting process.
---@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.')
-- We drop the result of crafting.
local dropPos = vector.new(pos) + vector.new(0, 0.9,0)
for i=1, craftData.amount do
core.item_drop(ItemStack(self.output), nil, dropPos)
end
-- Remove craft from the counter.
ImmersiveCraftCounter:removeCraft(pos, craftData.playerName)
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
Smc.setCraftDataByPos(pos)
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.')
-- We get the current stage and start the abort process.
local stage = self.stagesByName[craftData.stageName]
stage:abortHandler(pos)
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
Smc.setCraftDataByPos(pos)
-- Remove craft from the counter.
ImmersiveCraftCounter:removeCraft(pos, craftData.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.
---@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'
-- 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 craftDesc
end
return ImmersiveCraft