ProcessingFuelStation: final changes

This commit is contained in:
Van
2026-06-06 21:35:06 +03:00
parent 98dc1e17b7
commit 0a20ac8987
2 changed files with 191 additions and 72 deletions

View File

@@ -1,11 +1,12 @@
local FUEL_ENERGY_FACTOR = 0.1 local FUEL_ENERGY_FACTOR = 0.1
local v = vector.new
local assert, ipairs = assert, ipairs
local mathMin, mathCeil = math.min, math.ceil
local coreNodePunch, coreGetNode, coreGetItemGroup = core.node_punch, 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 ProcessingFuelStationNode = Node:getExtended({ local ProcessingFuelStationNode = Node:getExtended({
@@ -18,29 +19,49 @@ local ProcessingFuelStationNode = Node:getExtended({
}) })
---@class ProcessingFuelStation : BaseProcessingStation ---@class ProcessingFuelStation : BaseProcessingStation
---@field alwaysEnable boolean?
---@field enablerItemGroup string?
---@field disablerItemGroup string?
---@field enableIdling boolean? ---@field enableIdling boolean?
---@field enableContainerProcessing boolean? ---
---@field fuelGroup string ---@field fuelGroup string
---@field fuelSlots integer ---@field fuelSlots integer
---
---@field getMeta fun(self:ProcessingFuelStation, pos:MapVector):ProcessingFuelStation.MetaData
local ProcessingFuelStation = BaseProcessingStation:extend({}) local ProcessingFuelStation = BaseProcessingStation:extend({})
---Definition table for the `ProcessingFuelStation`. ---Definition table for the `ProcessingFuelStation`.
--- ---
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class ProcessingFuelStationDef : BaseProcessingStationDef ---@class ProcessingFuelStationDef : BaseProcessingStationDef
---If `true`, the station will always be enable
---(that is, if there is fuel, it will always try to burn it).
---
---**Note**: This does not mean the station timer will run always:
---`enable` and `active` are different states.
---@field alwaysEnable boolean?
---
---The item group that will turn on the station when item hits it.
---If is `nil`, punch with any item will enable the station.
---@field enablerItemGroup string?
---
---Same as `enablerItemGroup` field, only it turns off the station.
---@field disablerItemGroup string?
---
---If `true`, it will burn the remaining fuel even if the station is disable.
---@field enableIdling boolean? ---@field enableIdling boolean?
---@field enableContainerProcessing boolean? ---
---@field fuelGroup string? ---@field fuelGroup string?
---@field fuelSlots integer? ---@field fuelSlots integer?
---@param name string
---@param def ProcessingFuelStationDef ---@param def ProcessingFuelStationDef
---@param nodesDef BaseProcessingStation.NodesDef? ---@param nodesDef BaseProcessingStation.NodesDefinition?
function ProcessingFuelStation:new(name, def, nodesDef) function ProcessingFuelStation:new(def, nodesDef)
---@type ProcessingFuelStation ---@type ProcessingFuelStation
local instance = self:getInstance(BaseProcessingStation:new(name, def)) local instance = self:getInstance(BaseProcessingStation:new(def))
instance.alwaysEnable = def.alwaysEnable
instance.enablerItemGroup = def.enablerItemGroup
instance.enableIdling = def.enableIdling instance.enableIdling = def.enableIdling
instance.enableContainerProcessing = def.enableContainerProcessing
instance.fuelGroup = def.fuelGroup or 'fuel' instance.fuelGroup = def.fuelGroup or 'fuel'
instance.fuelSlots = def.fuelSlots or 1 instance.fuelSlots = def.fuelSlots or 1
@@ -61,6 +82,14 @@ function ProcessingFuelStation:new(name, def, nodesDef)
instance:init(pos) instance:init(pos)
end end
oldCallback = callbacks.on_punch or function(...) end
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
oldCallback(pos, node, puncher, pointed_thing)
instance:onPunch(pos, node, puncher, pointed_thing)
coreNodePunch(pos, node, puncher, pointed_thing)
end
oldCallback = callbacks.on_rightclick or function(...) end oldCallback = callbacks.on_rightclick or function(...) end
-- Right-clicking on a station opens the player's inventory, adding context to the station tab. -- Right-clicking on a station opens the player's inventory, adding context to the station tab.
-- Note: we do not use formspec via node metadata. -- Note: we do not use formspec via node metadata.
@@ -108,17 +137,19 @@ function ProcessingFuelStation:new(name, def, nodesDef)
if activeSettings ~= nil then if activeSettings ~= nil then
local activeNode = ProcessingFuelStationNode:new({ settings = activeSettings, callbacks = callbacks }) local activeNode = ProcessingFuelStationNode: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
---@param pos MapVector ---@param pos MapVector
function ProcessingFuelStation:init(pos) function ProcessingFuelStation:init(pos)
---@type ProcessingFuelStation.MetaData local meta = self:getMeta(pos)
local meta = core.get_meta(pos)
meta:set_float('remaining_fuel', 0.0) meta:set_float('remaining_fuel', 0.0)
meta:set_string('fuel_group', self.fuelGroup)
local inventory = meta:get_inventory() local inventory = meta:get_inventory()
inventory:set_size('fuel', self.fuelSlots) inventory:set_size('fuel', self.fuelSlots)
@@ -126,52 +157,70 @@ function ProcessingFuelStation:init(pos)
self:startTimer(pos) self:startTimer(pos)
end end
---@param isProcessing boolean ---@param pos MapVector
---@param meta ProcessingFuelStation.MetaData ---@param node NodeTable
---@param elapsed number ---@param puncher ObjectRef|Player
---@return boolean isProcessing ---@param pointedThing pointed_thing
function ProcessingFuelStation:takeFuel(isProcessing, meta, elapsed) function ProcessingFuelStation:onPunch(pos, node, puncher, pointedThing)
local inventory = meta:get_inventory() if self.alwaysEnable then
local remainingFuel = meta:get_float('remaining_fuel') return
if remainingFuel > 0 or inventory:is_empty('fuel') then
return isProcessing
end end
local fuelList = inventory:get_list('fuel') local meta = self:getMeta(pos)
local isEnable = self:getMetaIsEnable(meta)
for _, fuelStack in ipairs(fuelList) do if not isEnable then
if not fuelStack:is_empty() then if self.enablerItemGroup == nil then
-- Note: since item groups can only contain integers, self:setMetaIsEnable(meta, true)
-- we divide the fuel units by a constant `FUEL_ENERGY_FACTOR` to convert seconds to smaller units. self:startTimer(pos)
local fuelEnergy = core.get_item_group(fuelStack:get_name(), self.fuelGroup or 'fuel') / FUEL_ENERGY_FACTOR
if fuelEnergy > 0 then return
local itemCount = fuelStack:get_count() end
-- If the energy of one item is not enough for one iteration, local itemName = puncher:get_wielded_item():get_name()
-- we calculate how many items we need to take if coreGetItemGroup(itemName, self.enablerItemGroup) > 0 then
if fuelEnergy < elapsed then self:setMetaIsEnable(meta, true)
itemCount = math.min(math.ceil(elapsed/fuelEnergy), itemCount) self:startTimer(pos)
end
else else
itemCount = 1 if self.disablerItemGroup == nil then
self:setMetaIsEnable(meta, false)
self:startTimer(pos)
return
end end
remainingFuel = remainingFuel + (fuelEnergy * itemCount) local itemName = puncher:get_wielded_item():get_name()
if coreGetItemGroup(itemName, self.disablerItemGroup) > 0 then
fuelStack:set_count(itemCount) self:setMetaIsEnable(meta, false)
inventory:remove_item('fuel', fuelStack) self:startTimer(pos)
end end
end end
end end
isProcessing = true ---@param meta ProcessingFuelStation.MetaData
---@param isEnable boolean
meta:set_float('remaining_fuel', remainingFuel) function ProcessingFuelStation:setMetaIsEnable(meta, isEnable)
if isEnable then
return isProcessing meta:set_string('is_enable', 'true')
else
meta:set_string('is_enable', 'false')
end
end end
---@param meta ProcessingFuelStation.MetaData
---@return boolean
function ProcessingFuelStation:getMetaIsEnable(meta)
local isEnableString = meta:get_string('is_enable')
if isEnableString == 'true' then
return true
else
return false
end
end
---Subtracts the elapsed time from the remaining fuel value.
---@param isProcessing boolean ---@param isProcessing boolean
---@param meta ProcessingFuelStation.MetaData ---@param meta ProcessingFuelStation.MetaData
---@param elapsed number ---@param elapsed number
@@ -195,26 +244,83 @@ function ProcessingFuelStation:burnFuel(isProcessing, meta, elapsed)
return isProcessing or (self.enableIdling == true and remainingFuel ~= 0) return isProcessing or (self.enableIdling == true and remainingFuel ~= 0)
end end
---If required, takes an item from the fuel slot
---and converts it into the remaining fuel in the station.
---@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')
local isEnable = self:getMetaIsEnable(meta)
if not (self.alwaysEnable or isEnable) then
return isProcessing
end
if remainingFuel > 0 then
return isProcessing
end
if inventory:is_empty('fuel') then
self:setMetaIsEnable(meta, false)
return isProcessing
end
local fuelList = inventory:get_list('fuel')
for _, fuelStack in ipairs(fuelList) do
if not fuelStack:is_empty() then
-- Note: since item groups can only contain integers,
-- we divide the fuel units by a constant `FUEL_ENERGY_FACTOR` to convert seconds to smaller units.
local fuelEnergy = coreGetItemGroup(fuelStack:get_name(), self.fuelGroup or 'fuel') / FUEL_ENERGY_FACTOR
if fuelEnergy > 0 then
local itemCount = fuelStack:get_count()
-- If the energy of one item is not enough for one iteration,
-- we calculate how many items we need to take
if fuelEnergy < elapsed then
itemCount = mathMin(mathCeil(elapsed / fuelEnergy), itemCount)
else
itemCount = 1
end
remainingFuel = remainingFuel + (fuelEnergy * itemCount)
fuelStack:set_count(itemCount)
inventory:remove_item('fuel', fuelStack)
end
end
end
isProcessing = true
meta:set_float('remaining_fuel', remainingFuel)
return isProcessing
end
---@param meta ProcessingFuelStation.MetaData
---@param pos MapVector ---@param pos MapVector
function ProcessingFuelStation:pushContainer(pos) function ProcessingFuelStation:pushContainerStation(meta, pos)
if not self.enableContainerProcessing then local containerPos = pos + v(0, 1, 0)
local containerName = coreGetNode(containerPos).name
if coreGetItemGroup(containerName, 'processing_container_station') == 0 then
return return
end end
local containerPos = pos + vector.new(0, 1, 0) ---@type ProcessingContainerStation?
---@type ProcessingContainerStation.MetaData local containerStation = Smc.getNodeSuperclassByPos(containerPos)
local containerMeta = core.get_meta(containerPos) assert(containerStation ~= nil)
local fuelGroup = containerMeta:get_string('fuel_group') if containerStation.fuelGroup ~= self.fuelGroup then
if fuelGroup ~= self.fuelGroup then
return return
end end
---@type ProcessingFuelStation.MetaData containerStation:onPush(containerPos, meta:get_float('remaining_fuel'))
local fuelMeta = core.get_meta(pos)
ProcessingStationEvents:callOnPushContainer(containerPos, fuelMeta:get_float('remaining_fuel'))
end end
---@param pos MapVector ---@param pos MapVector
@@ -223,11 +329,12 @@ end
---@param elapsed number ---@param elapsed number
---@return boolean? ---@return boolean?
function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout) function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
local meta = core.get_meta(pos) local meta = self:getMeta(pos)
local isEnable = self:getMetaIsEnable(meta)
local isProcessing = self:burnFuel(true, meta, elapsed) local isProcessing = self:burnFuel(self.alwaysEnable or isEnable, meta, elapsed)
isProcessing = self:takeFuel(isProcessing, meta, elapsed) -- TODO: if-else isProcessing = self:takeFuel(isProcessing, meta, elapsed)
self:pushContainer(pos) self:pushContainerStation(meta, pos)
self:swapNode(isProcessing, pos) self:swapNode(isProcessing, pos)
@@ -236,6 +343,18 @@ function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
return isProcessing return isProcessing
end end
---@param pos MapVector
---@param enable boolean
function ProcessingFuelStation:onPush(pos, enable)
local meta = self:getMeta(pos)
self:setMetaIsEnable(meta, enable)
if enable then
self:startTimer(pos)
end
end
--- DEBUG --- --- DEBUG ---
local Formspec = Smc.requireFormspec() local Formspec = Smc.requireFormspec()
@@ -262,13 +381,13 @@ local tab = function(self, playerName, tabType)
return form return form
end end
ProcessingFuelStation:new('test_processing_node', { ProcessingFuelStation:new({
craftType = 'test', craftType = 'test',
tab = tab, tab = tab,
inputSlots = 2, inputSlots = 2,
fuelGroup = 'wood', fuelGroup = 'wood',
enableIdling = false, alwaysEnable = false,
enableContainerProcessing = true, enableIdling = true,
}, },
{ {
settings = { settings = {

View File

@@ -1,5 +1,5 @@
---@alias ProcessingFuelStation.MetaData.FloatKey 'remaining_fuel' ---@alias ProcessingFuelStation.MetaData.FloatKey 'remaining_fuel'
---@alias ProcessingFuelStation.MetaData.StringKey 'fuel_group' ---@alias ProcessingFuelStation.MetaData.StringKey 'is_enable'
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class ProcessingFuelStation.MetaData : NodeMetaRef ---@class ProcessingFuelStation.MetaData : NodeMetaRef