Lazy commit: processing stations

This commit is contained in:
Van
2026-06-03 21:46:40 +03:00
parent 3854410041
commit 592bbf9c58
16 changed files with 266 additions and 185 deletions

View File

@@ -1,6 +1,7 @@
local FUEL_ENERGY_FACTOR = 0.1
local BaseProcessingStation = Smc.requireBaseProcessingStation()
---@type Events.ProcessingStationEvents
local ProcessingStationEvents = Smc.requireEvents('ProcessingStationEvents')
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
@@ -17,6 +18,7 @@ local ProcessingStationNode = Node:getExtended({
---@class ProcessingContainerStation : BaseProcessingStation
---@field enableRegression boolean?
---@field fuelGroup string
---@field inputSlots integer
---@field outputSlots integer
local ProcessingContainerStation = BaseProcessingStation:extend({})
@@ -26,19 +28,25 @@ local ProcessingContainerStation = BaseProcessingStation:extend({})
---**Only for EmmyLua.**
---@class ProcessingContainerStationDef : BaseProcessingStationDef
---@field enableRegression boolean?
---@field fuelGroup string?
---@field inputSlots integer?
---@field outputSlots integer?
---@param name string
---@param def ProcessingContainerStationDef
---@param nodesDef BaseProcessingStation.NodesDef
---@param nodesDef BaseProcessingStation.NodesDef?
function ProcessingContainerStation:new(name, def, nodesDef)
---@type ProcessingContainerStation
local instance = self:getInstance(BaseProcessingStation:new(name, def, nodesDef))
local instance = self:getInstance(BaseProcessingStation:new(name, def))
instance.enableRegression = def.enableRegression
instance.fuelGroup = def.fuelGroup or 'fuel'
instance.inputSlots = def.inputSlots or 1
instance.outputSlots = def.outputSlots or 1
if nodesDef == nil then
return instance
end
local settings = nodesDef.settings
local activeSettings = nodesDef.activeSettings
local callbacks = nodesDef.callbacks or {}
@@ -89,7 +97,7 @@ function ProcessingContainerStation:new(name, def, nodesDef)
callbacks.on_timer = function(pos, elapsed, node, timeout)
oldCallback(pos, elapsed, node, timeout)
return self:onTimer(pos, elapsed, node, timeout)
return instance:onTimer(pos, elapsed, node, timeout)
end
-- Registering a nodes for Luanti.
@@ -105,7 +113,7 @@ function ProcessingContainerStation:new(name, def, nodesDef)
end
---Opens the player's inventory, adding the tab described in `self.tab`.
---@param pos MapPosition
---@param pos MapVector
---@param player Player
function ProcessingContainerStation:showForm(pos, player)
local playerName = player:get_player_name()
@@ -118,11 +126,13 @@ function ProcessingContainerStation:showForm(pos, player)
PlayerInventoryUI:showForm(playerName)
end
---@param pos MapPosition
---@param pos MapVector
function ProcessingContainerStation:init(pos)
---@type ProcessingContainerStation.MetaData
local meta = core.get_meta(pos)
meta:set_float('processing_time', 0.0)
meta:set_string('fuel_group', self.fuelGroup)
meta:set_float('energy', 0.0)
local inventory = meta:get_inventory()
inventory:set_size('input', self.inputSlots)
@@ -133,10 +143,9 @@ end
---@param isProcessing boolean
---@param meta ProcessingContainerStation.MetaData
---@param timeout number
---@return boolean isProcessing
---@return CraftSystem.ProcessingCraft?
function ProcessingContainerStation:getCraft(isProcessing, meta, timeout)
function ProcessingContainerStation:getCraft(isProcessing, meta)
local inventory = meta:get_inventory()
local processingTime = meta:get_float('processing_time')
@@ -165,19 +174,20 @@ end
---@param isProcessing boolean
---@param meta ProcessingContainerStation.MetaData
---@param timeout number
---@param elapsed number
---@param craft CraftSystem.ProcessingCraft?
---@return boolean isProcessing
function ProcessingContainerStation:processing(isProcessing, meta, timeout, craft)
function ProcessingContainerStation:processing(isProcessing, meta, elapsed, craft)
local inventory = meta:get_inventory()
local processingTime = meta:get_float('processing_time')
local lastCraftName = meta:get_string('craft_name')
if isProcessing and craft ~= nil then
processingTime = processingTime + timeout
processingTime = processingTime + elapsed
else
if self.enableRegression then
processingTime = processingTime - timeout
processingTime = processingTime - elapsed
end
end
@@ -188,28 +198,37 @@ function ProcessingContainerStation:processing(isProcessing, meta, timeout, craf
inventory:add_item('output', craft.output)
processingTime = 0
lastCraftName = ''
end
if processingTime < 0 then
processingTime = 0
lastCraftName = ''
end
meta:set_float('processing_time', processingTime)
meta:set_string('craft_name', lastCraftName)
return isProcessing or (self.enableRegression == true and processingTime ~= 0)
end
---@param pos MapPosition
---@param pos MapVector
---@param elapsed number
---@param node NodeTable
---@param timeout number
---@return boolean?
function ProcessingContainerStation:onTimer(pos, elapsed, node, timeout)
---@type ProcessingContainerStation.MetaData
local meta = core.get_meta(pos)
local energy = meta:get_float('energy')
local isProcessing, craft = self:getCraft(true, meta, timeout)
local isProcessing = self:processing(isProcessing, meta, timeout, craft)
local isProcessing, craft = self:getCraft(true, meta)
if energy <= 0 then
isProcessing = false
end
isProcessing = self:processing(isProcessing, meta, elapsed, craft)
self:swapNode(isProcessing, pos)
@@ -218,37 +237,45 @@ function ProcessingContainerStation:onTimer(pos, elapsed, node, timeout)
return isProcessing
end
function ProcessingContainerStation:onPush(pos, energy)
---@type ProcessingContainerStation.MetaData
local meta = core.get_meta(pos)
meta:set_float('energy', energy)
core.log(tostring(energy))
self:startTimer(pos)
end
ProcessingStationEvents:subscribeOnPushContainer(ProcessingContainerStation.onPush)
--- DEBUG ---
--[[
local CraftSystem = Smc.requireCraftSystem()
local Formspec = Smc.requireFormspec()
local TileSheet = Smc.requireTileSheet()
local tileSheet = TileSheet:new('debug_processing_station_sheet.png', 16, 16)
local handler = CraftSystem:getProcessingCraftHandler('test')
handler:registerCraft('test_pro_craft', {
output = 'wood:allwhere_log',
recipe = {'wood:allwhere_stick', 'rocks:sylite'},
time = 5,
})
---@param self PlayerInventoryUI
---@param playerName string
---@param tabType PlayerInventoryUI.TabType
---@return Formspec
local tab = function(self, playerName, tabType)
local form = Formspec:new()
---@type MapVector
local pos = self:getContex(playerName, 'node_tab_pos')
local inventoryLocation = ('nodemeta:%s,%s,%s'):format(pos.x, pos.y, pos.z)
---@type ProcessingContainerStation.MetaData
local meta = core.get_meta(pos)
---@type ProcessingFuelStation.MetaData
local fuelMeta = core.get_meta(pos - vector.new(0, 1, 0))
form
:image(0, 0, 11, 9, 'player_inventory_tab_background.png')
:list(inventoryLocation, 'input', 1, 1, 1, 2)
:list(inventoryLocation, 'output', 3, 1, 1, 1)
:list(inventoryLocation, 'fuel', 1, 4, 1, 1)
:label(4, 3, tostring(meta:get_float('remaining_fuel')))
:label(4, 3, tostring(fuelMeta:get_float('remaining_fuel')))
:label(4.5, 3, tostring(meta:get_float('processing_time')))
return form
@@ -264,8 +291,8 @@ ProcessingContainerStation:new('test_processing_node', {
},
{
settings = {
name = 'test_processing_node',
tiles = {
name = 'test_container_node',
--[[tiles = {
tileSheet:t(0, 0),
tileSheet:t(0, 0),
tileSheet:t(0, 1),
@@ -283,7 +310,7 @@ ProcessingContainerStation:new('test_processing_node', {
tileSheet:t(0, 2),
tileSheet:t(0, 2),
tileSheet:t(0, 2)
}
}--]]
}
})
--]]