ProcessingContainerStation: final changes

This commit is contained in:
Van
2026-06-06 20:22:57 +03:00
parent c0cbf86c8b
commit 98dc1e17b7
2 changed files with 89 additions and 44 deletions

View File

@@ -1,47 +1,57 @@
local v = vector.new
local assert, ipairs = assert, ipairs
local coreGetNode, coreGetItemGroup = core.get_node, core.get_item_group
local BaseProcessingStation = Smc.requireBaseProcessingStation() local BaseProcessingStation = Smc.requireBaseProcessingStation()
---@type Events.ProcessingStationEvents
local ProcessingStationEvents = Smc.requireEvents('ProcessingStationEvents')
local PlayerInventoryUI = Smc.requirePlayerInventoryUI() local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
local Node = Smc.requireNode() local Node = Smc.requireNode()
local ProcessingStationNode = Node:getExtended({ local ProcessingContainerStationNode = Node:getExtended({
settings = { settings = {
name = '', name = '',
drawtype = 'normal', drawtype = 'normal',
paramtype = 'light', paramtype = 'light',
groups = { processing_station = 1, dig_immediate = 2 } groups = { processing_container_station = 1, dig_immediate = 2 }
}, },
}) })
---@class ProcessingContainerStation : BaseProcessingStation ---@class ProcessingContainerStation : BaseProcessingStation
---@field enableRegression boolean? ---@field enableFuelPush boolean?
---@field fuelGroup string ---@field enableRegression boolean?
---@field inputSlots integer ---
---@field outputSlots integer ---@field fuelGroup string
---@field inputSlots integer
---@field outputSlots integer
---
---@field getMeta fun(self:ProcessingContainerStation, pos:MapVector):ProcessingContainerStation.MetaData
local ProcessingContainerStation = BaseProcessingStation:extend({}) local ProcessingContainerStation = BaseProcessingStation:extend({})
---Definition table for the `ProcessingContainerStation`. ---Definition table for the `ProcessingContainerStation`.
--- ---
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class ProcessingContainerStationDef : BaseProcessingStationDef ---@class ProcessingContainerStationDef : BaseProcessingStationDef
---If `true`, the `ContainerStation` will
---attempt to push `FuelStation` when it is ready for crafting.
---@field enableFuelPush boolean?
---
---If `true`, then crafting progress will gradually
---decrease when there are no conditions for crafting.
---@field enableRegression boolean? ---@field enableRegression boolean?
---@field fuelGroup string? ---
---@field inputSlots integer? ---@field fuelGroup string?
---@field outputSlots integer? ---@field inputSlots integer?
---@field outputSlots integer?
---@param name string
---@param def ProcessingContainerStationDef ---@param def ProcessingContainerStationDef
---@param nodesDef BaseProcessingStation.NodesDef? ---@param nodesDef BaseProcessingStation.NodesDefinition?
function ProcessingContainerStation:new(name, def, nodesDef) function ProcessingContainerStation:new(def, nodesDef)
---@type ProcessingContainerStation ---@type ProcessingContainerStation
local instance = self:getInstance(BaseProcessingStation:new(name, def)) local instance = self:getInstance(BaseProcessingStation:new(def))
instance.enableRegression = def.enableRegression instance.enableFuelPush = def.enableFuelPush
instance.fuelGroup = def.fuelGroup or 'fuel' instance.enableRegression = def.enableRegression
instance.inputSlots = def.inputSlots or 1 instance.fuelGroup = def.fuelGroup or 'fuel'
instance.outputSlots = def.outputSlots or 1 instance.inputSlots = def.inputSlots or 1
instance.outputSlots = def.outputSlots or 1
if nodesDef == nil then if nodesDef == nil then
return instance return instance
@@ -101,18 +111,21 @@ function ProcessingContainerStation:new(name, def, nodesDef)
end end
-- Registering a nodes for Luanti. -- Registering a nodes for Luanti.
local node = ProcessingStationNode:new({ settings = settings, callbacks = callbacks }) local node = ProcessingContainerStationNode:new({ settings = settings, callbacks = callbacks })
instance.nodeName = node.itemDef.settings.name instance.nodeName = node.itemDef.settings.name
if activeSettings ~= nil then if activeSettings ~= nil then
local activeNode = ProcessingStationNode:new({ settings = activeSettings, callbacks = callbacks }) local activeNode = ProcessingContainerStationNode:new({ settings = activeSettings, callbacks = callbacks })
instance.activeNodeName = activeNode.itemDef.settings.name instance.activeNodeName = activeNode.itemDef.settings.name
Smc.addNodeSuperclass(instance.activeNodeName, instance)
end end
Smc.addNodeSuperclass(instance.nodeName, instance)
return instance return instance
end end
---Opens the player's inventory, adding the tab described in `self.tab`.
---@param pos MapVector ---@param pos MapVector
---@param player Player ---@param player Player
function ProcessingContainerStation:showForm(pos, player) function ProcessingContainerStation:showForm(pos, player)
@@ -128,11 +141,10 @@ end
---@param pos MapVector ---@param pos MapVector
function ProcessingContainerStation:init(pos) function ProcessingContainerStation:init(pos)
---@type ProcessingContainerStation.MetaData local meta = self:getMeta(pos)
local meta = core.get_meta(pos)
meta:set_float('processing_time', 0.0) meta:set_float('processing_time', 0.0)
meta:set_string('fuel_group', self.fuelGroup) meta:set_string('fuel_group', self.fuelGroup)
meta:set_float('energy', 0.0) meta:set_float('remaining_fuel', 0.0)
local inventory = meta:get_inventory() local inventory = meta:get_inventory()
inventory:set_size('input', self.inputSlots) inventory:set_size('input', self.inputSlots)
@@ -141,6 +153,11 @@ function ProcessingContainerStation:init(pos)
self:startTimer(pos) self:startTimer(pos)
end end
---Returns the current craft that
---the station is currently engaged in.
---
---Also resets crafting progress if there is no craft,
---the craft has changed, or there is no room in the result slots.
---@param isProcessing boolean ---@param isProcessing boolean
---@param meta ProcessingContainerStation.MetaData ---@param meta ProcessingContainerStation.MetaData
---@return boolean isProcessing ---@return boolean isProcessing
@@ -155,6 +172,8 @@ function ProcessingContainerStation:getCraft(isProcessing, meta)
if craft == nil then if craft == nil then
isProcessing = false isProcessing = false
processingTime = 0
lastCraftName = ''
end end
if isProcessing and craft ~= nil and craft.name ~= lastCraftName then if isProcessing and craft ~= nil and craft.name ~= lastCraftName then
@@ -164,6 +183,8 @@ function ProcessingContainerStation:getCraft(isProcessing, meta)
if isProcessing and craft ~= nil and not inventory:room_for_item('output', craft.output) then if isProcessing and craft ~= nil and not inventory:room_for_item('output', craft.output) then
isProcessing = false isProcessing = false
processingTime = 0
lastCraftName = ''
end end
meta:set_float('processing_time', processingTime) meta:set_float('processing_time', processingTime)
@@ -172,6 +193,8 @@ function ProcessingContainerStation:getCraft(isProcessing, meta)
return isProcessing, craft return isProcessing, craft
end end
---Under satisfactory conditions, it increases the crafting progress and
---when it reaches the required value, it takes the materials and returns the crafting result
---@param isProcessing boolean ---@param isProcessing boolean
---@param meta ProcessingContainerStation.MetaData ---@param meta ProcessingContainerStation.MetaData
---@param elapsed number ---@param elapsed number
@@ -197,7 +220,7 @@ function ProcessingContainerStation:processing(isProcessing, meta, elapsed, craf
end end
inventory:add_item('output', craft.output) inventory:add_item('output', craft.output)
processingTime = 0 processingTime = processingTime - craft.time
lastCraftName = '' lastCraftName = ''
end end
@@ -212,42 +235,64 @@ function ProcessingContainerStation:processing(isProcessing, meta, elapsed, craf
return isProcessing or (self.enableRegression == true and processingTime ~= 0) return isProcessing or (self.enableRegression == true and processingTime ~= 0)
end end
---@param meta ProcessingContainerStation.MetaData
---@param pos MapVector
function ProcessingContainerStation:pushFuelStation( meta, pos)
if not self.enableFuelPush then
return
end
local fuelPos = pos - v(0, 1, 0)
local fuelName = coreGetNode(fuelPos).name
if coreGetItemGroup(fuelName, 'processing_fuel_station') == 0 then
return
end
---@type ProcessingFuelStation?
local fuelStation = Smc.getNodeSuperclassByPos(fuelPos)
assert(fuelStation ~= nil)
if fuelStation.fuelGroup ~= self.fuelGroup then
return
end
fuelStation:onPush(fuelPos, meta:get_string('craft_name') ~= '')
end
---@param pos MapVector ---@param pos MapVector
---@param elapsed number ---@param elapsed number
---@param node NodeTable ---@param node NodeTable
---@param timeout number ---@param timeout number
---@return boolean? ---@return boolean?
function ProcessingContainerStation:onTimer(pos, elapsed, node, timeout) function ProcessingContainerStation:onTimer(pos, elapsed, node, timeout)
---@type ProcessingContainerStation.MetaData local meta = self:getMeta(pos)
local meta = core.get_meta(pos) local remainingFuel = meta:get_float('remaining_fuel')
local energy = meta:get_float('energy')
local isProcessing, craft = self:getCraft(true, meta) local isProcessing, craft = self:getCraft(true, meta)
if energy <= 0 then if remainingFuel <= 0 then
isProcessing = false isProcessing = false
end end
isProcessing = self:processing(isProcessing, meta, elapsed, craft) isProcessing = self:processing(isProcessing, meta, elapsed, craft)
self:pushFuelStation(meta, pos)
self:swapNode(isProcessing, pos) self:swapNode(isProcessing, pos)
PlayerInventoryUI:updateNodeForm(pos) PlayerInventoryUI:updateNodeForm(pos)
return isProcessing return isProcessing
end end
function ProcessingContainerStation:onPush(pos, energy) ---@param pos MapVector
---@type ProcessingContainerStation.MetaData ---@param remainingFuel number
local meta = core.get_meta(pos) function ProcessingContainerStation:onPush(pos, remainingFuel)
local meta = self:getMeta(pos)
meta:set_float('energy', energy) meta:set_float('remaining_fuel', remainingFuel)
core.log(tostring(energy))
self:startTimer(pos) self:startTimer(pos)
end end
ProcessingStationEvents:subscribeOnPushContainer(ProcessingContainerStation.onPush)
--- DEBUG --- --- DEBUG ---
local CraftSystem = Smc.requireCraftSystem() local CraftSystem = Smc.requireCraftSystem()
@@ -281,13 +326,13 @@ local tab = function(self, playerName, tabType)
return form return form
end end
ProcessingContainerStation:new('test_processing_node', { ProcessingContainerStation:new({
craftType = 'test', craftType = 'test',
tab = tab, tab = tab,
inputSlots = 2, inputSlots = 2,
fuelGroup = 'wood', fuelGroup = 'wood',
enableIdling = true,
enableRegression = false, enableRegression = false,
enableFuelPush = true
}, },
{ {
settings = { settings = {

View File

@@ -1,4 +1,4 @@
---@alias ProcessingContainerStation.MetaData.FloatKey 'processing_time'|'energy' ---@alias ProcessingContainerStation.MetaData.FloatKey 'processing_time'|'remaining_fuel'
---@alias ProcessingContainerStation.MetaData.StringKey 'craft_name'|'fuel_group' ---@alias ProcessingContainerStation.MetaData.StringKey 'craft_name'|'fuel_group'
---**Only for EmmyLua.** ---**Only for EmmyLua.**