Lazy commit: processing stations
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
local FUEL_ENERGY_FACTOR = 0.1
|
||||
|
||||
local BaseProcessingStation = Smc.requireBaseProcessingStation()
|
||||
---@type Events.ProcessingStationEvents
|
||||
local ProcessingStationEvents = Smc.requireEvents('ProcessingStationEvents')
|
||||
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
@@ -16,28 +18,35 @@ local ProcessingFuelStationNode = Node:getExtended({
|
||||
})
|
||||
|
||||
---@class ProcessingFuelStation : BaseProcessingStation
|
||||
---@field enableIdling boolean?
|
||||
---@field fuelGroup string
|
||||
---@field fuelSlots integer
|
||||
---@field enableIdling boolean?
|
||||
---@field enableContainerProcessing boolean?
|
||||
---@field fuelGroup string
|
||||
---@field fuelSlots integer
|
||||
local ProcessingFuelStation = BaseProcessingStation:extend({})
|
||||
|
||||
---Definition table for the `ProcessingFuelStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class ProcessingFuelStationDef : BaseProcessingStationDef
|
||||
---@field enableIdling boolean?
|
||||
---@field fuelGroup string?
|
||||
---@field fuelSlots integer?
|
||||
---@field enableIdling boolean?
|
||||
---@field enableContainerProcessing boolean?
|
||||
---@field fuelGroup string?
|
||||
---@field fuelSlots integer?
|
||||
|
||||
---@param name string
|
||||
---@param def ProcessingFuelStationDef
|
||||
---@param nodesDef BaseProcessingStation.NodesDef
|
||||
---@param nodesDef BaseProcessingStation.NodesDef?
|
||||
function ProcessingFuelStation:new(name, def, nodesDef)
|
||||
---@type ProcessingFuelStation
|
||||
local instance = self:getInstance(BaseProcessingStation:new(name, def, nodesDef))
|
||||
instance.enableIdling = def.enableIdling
|
||||
instance.fuelGroup = def.fuelGroup or 'fuel'
|
||||
instance.fuelSlots = def.fuelSlots or 1
|
||||
local instance = self:getInstance(BaseProcessingStation:new(name, def))
|
||||
instance.enableIdling = def.enableIdling
|
||||
instance.enableContainerProcessing = def.enableContainerProcessing
|
||||
instance.fuelGroup = def.fuelGroup or 'fuel'
|
||||
instance.fuelSlots = def.fuelSlots or 1
|
||||
|
||||
if nodesDef == nil then
|
||||
return instance
|
||||
end
|
||||
|
||||
local settings = nodesDef.settings
|
||||
local activeSettings = nodesDef.activeSettings
|
||||
@@ -104,11 +113,12 @@ function ProcessingFuelStation:new(name, def, nodesDef)
|
||||
return instance
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
---@param pos MapVector
|
||||
function ProcessingFuelStation:init(pos)
|
||||
---@type ProcessingFuelStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
meta:set_float('remaining_fuel', 0.0)
|
||||
meta:set_string('fuel_group', self.fuelGroup)
|
||||
|
||||
local inventory = meta:get_inventory()
|
||||
inventory:set_size('fuel', self.fuelSlots)
|
||||
@@ -116,11 +126,18 @@ function ProcessingFuelStation:init(pos)
|
||||
self:startTimer(pos)
|
||||
end
|
||||
|
||||
---@param remainingFuel number
|
||||
---@param inventory InvRef
|
||||
---@param timeout number
|
||||
---@return number remainingFuel
|
||||
function ProcessingFuelStation:takeFuel(remainingFuel, inventory, timeout)
|
||||
---@param isProcessing boolean
|
||||
---@param meta ProcessingFuelStation.MetaData
|
||||
---@param elapsed number
|
||||
---@return boolean isProcessing
|
||||
function ProcessingFuelStation:takeFuel(isProcessing, meta, elapsed)
|
||||
local inventory = meta:get_inventory()
|
||||
local remainingFuel = meta:get_float('remaining_fuel')
|
||||
|
||||
if remainingFuel > 0 or inventory:is_empty('fuel') then
|
||||
return isProcessing
|
||||
end
|
||||
|
||||
local fuelList = inventory:get_list('fuel')
|
||||
|
||||
for _, fuelStack in ipairs(fuelList) do
|
||||
@@ -134,8 +151,8 @@ function ProcessingFuelStation:takeFuel(remainingFuel, inventory, timeout)
|
||||
|
||||
-- If the energy of one item is not enough for one iteration,
|
||||
-- we calculate how many items we need to take
|
||||
if fuelEnergy < timeout then
|
||||
itemCount = math.min(math.ceil(timeout/fuelEnergy), itemCount)
|
||||
if fuelEnergy < elapsed then
|
||||
itemCount = math.min(math.ceil(elapsed/fuelEnergy), itemCount)
|
||||
else
|
||||
itemCount = 1
|
||||
end
|
||||
@@ -148,29 +165,27 @@ function ProcessingFuelStation:takeFuel(remainingFuel, inventory, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
return remainingFuel
|
||||
isProcessing = true
|
||||
|
||||
meta:set_float('remaining_fuel', remainingFuel)
|
||||
|
||||
return isProcessing
|
||||
end
|
||||
|
||||
---@param isProcessing boolean
|
||||
---@param meta ProcessingFuelStation.MetaData
|
||||
---@param timeout number
|
||||
---@param elapsed number
|
||||
---@return boolean isProcessing
|
||||
function ProcessingFuelStation:burnFuel(isProcessing, meta, timeout)
|
||||
local inventory = meta:get_inventory()
|
||||
|
||||
function ProcessingFuelStation:burnFuel(isProcessing, meta, elapsed)
|
||||
local remainingFuel = meta:get_float('remaining_fuel')
|
||||
|
||||
if isProcessing and remainingFuel <= 0 and not inventory:is_empty('fuel') then
|
||||
remainingFuel = self:takeFuel(remainingFuel, inventory, timeout)
|
||||
end
|
||||
|
||||
if isProcessing then
|
||||
remainingFuel = remainingFuel - timeout
|
||||
remainingFuel = remainingFuel - elapsed
|
||||
elseif self.enableIdling then
|
||||
remainingFuel = remainingFuel - timeout
|
||||
remainingFuel = remainingFuel - elapsed
|
||||
end
|
||||
|
||||
if remainingFuel < 0 then
|
||||
if remainingFuel <= 0 then
|
||||
remainingFuel = 0
|
||||
isProcessing = false
|
||||
end
|
||||
@@ -180,15 +195,39 @@ function ProcessingFuelStation:burnFuel(isProcessing, meta, timeout)
|
||||
return isProcessing or (self.enableIdling == true and remainingFuel ~= 0)
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
---@param pos MapVector
|
||||
function ProcessingFuelStation:pushContainer(pos)
|
||||
if not self.enableContainerProcessing then
|
||||
return
|
||||
end
|
||||
|
||||
local containerPos = pos + vector.new(0, 1, 0)
|
||||
---@type ProcessingContainerStation.MetaData
|
||||
local containerMeta = core.get_meta(containerPos)
|
||||
|
||||
local fuelGroup = containerMeta:get_string('fuel_group')
|
||||
|
||||
if fuelGroup ~= self.fuelGroup then
|
||||
return
|
||||
end
|
||||
|
||||
---@type ProcessingFuelStation.MetaData
|
||||
local fuelMeta = core.get_meta(pos)
|
||||
|
||||
ProcessingStationEvents:callOnPushContainer(containerPos, fuelMeta:get_float('remaining_fuel'))
|
||||
end
|
||||
|
||||
---@param pos MapVector
|
||||
---@param elapsed number
|
||||
---@param node NodeTable
|
||||
---@param timeout number
|
||||
---@param elapsed number
|
||||
---@return boolean?
|
||||
function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local isProcessing = self:burnFuel(true, meta, timeout)
|
||||
local isProcessing = self:burnFuel(true, meta, elapsed)
|
||||
isProcessing = self:takeFuel(isProcessing, meta, elapsed) -- TODO: if-else
|
||||
self:pushContainer(pos)
|
||||
|
||||
self:swapNode(isProcessing, pos)
|
||||
|
||||
@@ -198,17 +237,9 @@ function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
|
||||
end
|
||||
|
||||
--- DEBUG ---
|
||||
--[[
|
||||
|
||||
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
|
||||
@@ -224,11 +255,9 @@ local tab = function(self, playerName, tabType)
|
||||
|
||||
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.5, 3, tostring(meta:get_float('processing_time')))
|
||||
--:label(4.5, 3, tostring(meta:get_float('processing_time')))
|
||||
|
||||
return form
|
||||
end
|
||||
@@ -238,32 +267,13 @@ ProcessingFuelStation:new('test_processing_node', {
|
||||
tab = tab,
|
||||
inputSlots = 2,
|
||||
fuelGroup = 'wood',
|
||||
enableIdling = true,
|
||||
enableRegression = false,
|
||||
enableIdling = false,
|
||||
enableContainerProcessing = true,
|
||||
},
|
||||
{
|
||||
settings = {
|
||||
name = 'test_processing_node',
|
||||
tiles = {
|
||||
tileSheet:t(0, 0),
|
||||
tileSheet:t(0, 0),
|
||||
tileSheet:t(0, 1),
|
||||
tileSheet:t(0, 2),
|
||||
tileSheet:t(0, 2),
|
||||
tileSheet:t(0, 2)
|
||||
}
|
||||
},
|
||||
activeSettings = {
|
||||
name = 'test_processing_node_active',
|
||||
tiles = {
|
||||
tileSheet:t(0, 0),
|
||||
tileSheet:t(0, 0),
|
||||
tileSheet:t(1, 1),
|
||||
tileSheet:t(0, 2),
|
||||
tileSheet:t(0, 2),
|
||||
tileSheet:t(0, 2)
|
||||
}
|
||||
}
|
||||
})
|
||||
--]]
|
||||
return ProcessingFuelStation
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
---@alias ProcessingFuelStation.MetaData.FloatKey 'remaining_fuel'
|
||||
---@alias ProcessingFuelStation.MetaData.FloatKey 'remaining_fuel'
|
||||
---@alias ProcessingFuelStation.MetaData.StringKey 'fuel_group'
|
||||
|
||||
---**Only for EmmyLua.**
|
||||
---@class ProcessingFuelStation.MetaData : NodeMetaRef
|
||||
---@field get_float fun(self, key:ProcessingFuelStation.MetaData.FloatKey):number
|
||||
---@field set_float fun(self, key:ProcessingFuelStation.MetaData.FloatKey, value:number)
|
||||
---@field get_string fun(self, key:ProcessingFuelStation.MetaData.StringKey):string
|
||||
---@field set_string fun(self, key:ProcessingFuelStation.MetaData.StringKey, value:string)
|
||||
|
||||
Reference in New Issue
Block a user