Compare commits
3 Commits
592bbf9c58
...
f3c22aeb23
| Author | SHA1 | Date | |
|---|---|---|---|
| f3c22aeb23 | |||
| 98dc1e17b7 | |||
| c0cbf86c8b |
@@ -1,11 +1,13 @@
|
||||
local TICKRATE = 1
|
||||
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
local assert = assert
|
||||
local coreGetMeta, coreGetTimer, coreSwapNode = core.get_meta, core.get_node_timer, core.swap_node
|
||||
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
---@class BaseProcessingStation : Class
|
||||
---@field name string
|
||||
---An abstract class representing nodes that implement crafting of the `processing` type.
|
||||
---@class BaseProcessingStation : Class, NodeSuperClass
|
||||
---@field craftType CraftSystem.ProcessingCraftType
|
||||
---@field handler CraftSystem.ProcessingCraftHandler
|
||||
---@field tab PlayerInventoryUI.getTabFunc
|
||||
@@ -20,24 +22,31 @@ local BaseProcessingStation = Class:extend({})
|
||||
---@field craftType CraftSystem.ProcessingCraftType
|
||||
---@field tab PlayerInventoryUI.getTabFunc
|
||||
|
||||
---@alias BaseProcessingStation.NodesDef {settings:Node.Settings, activeSettings?:Node.Settings, callbacks:Node.Callbacks?}
|
||||
---@alias BaseProcessingStation.NodesDefinition {settings:Node.Settings, activeSettings:Node.Settings?, callbacks:Node.Callbacks?}
|
||||
|
||||
---@param name string
|
||||
---@param def BaseProcessingStationDef
|
||||
---@param nodesDef BaseProcessingStation.NodesDef?
|
||||
function BaseProcessingStation:new(name, def, nodesDef)
|
||||
---@param nodesDef BaseProcessingStation.NodesDefinition?
|
||||
function BaseProcessingStation:new(def, nodesDef)
|
||||
local handler = CraftSystem:getProcessingCraftHandler(def.craftType)
|
||||
|
||||
---@type BaseProcessingStation
|
||||
local instance = self:getInstance({})
|
||||
instance.name = name
|
||||
instance.craftType = def.craftType
|
||||
instance.handler = handler
|
||||
instance.tab = def.tab
|
||||
instance.craftType = def.craftType
|
||||
instance.handler = handler
|
||||
instance.tab = def.tab
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---A wrapper around `core.get_meta()` for convenience
|
||||
---and returning the correct `MetaDataRef` type.
|
||||
---@static
|
||||
---@param pos MapVector
|
||||
---@return MetaDataRef
|
||||
function BaseProcessingStation:getMeta(pos)
|
||||
return coreGetMeta(pos)
|
||||
end
|
||||
|
||||
---Opens the player's inventory, adding the node tab described in `self.tab`.
|
||||
---@param pos MapVector
|
||||
---@param player Player
|
||||
@@ -54,15 +63,16 @@ end
|
||||
|
||||
---Starts the station timer only if it is not running.
|
||||
---
|
||||
---The timer timeout is set to the timeout specified `self.timeout`.
|
||||
---
|
||||
---Returns `true` if the timer has been started.
|
||||
---@param pos MapVector
|
||||
---@return boolean
|
||||
function BaseProcessingStation:startTimer(pos)
|
||||
local timer = core.get_node_timer(pos)
|
||||
---@static
|
||||
---@param pos MapVector
|
||||
---@param timeout number? If is `nil`, the global `TICKRATE` setting will be used.
|
||||
---@return boolean
|
||||
function BaseProcessingStation:startTimer(pos, timeout)
|
||||
local timer = coreGetTimer(pos)
|
||||
|
||||
if not timer:is_started() then
|
||||
timer:start(TICKRATE)
|
||||
timer:start(timeout or TICKRATE)
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -80,9 +90,9 @@ function BaseProcessingStation:swapNode(isProcessing, pos)
|
||||
|
||||
if self.activeNodeName ~= nil then
|
||||
if isProcessing then
|
||||
core.swap_node(pos, {name = self.activeNodeName})
|
||||
coreSwapNode(pos, {name = self.activeNodeName})
|
||||
else
|
||||
core.swap_node(pos, {name = self.nodeName})
|
||||
coreSwapNode(pos, {name = self.nodeName})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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()
|
||||
---@type Events.ProcessingStationEvents
|
||||
local ProcessingStationEvents = Smc.requireEvents('ProcessingStationEvents')
|
||||
|
||||
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
local Node = Smc.requireNode()
|
||||
|
||||
local ProcessingStationNode = Node:getExtended({
|
||||
local ProcessingContainerStationNode = Node:getExtended({
|
||||
settings = {
|
||||
name = '',
|
||||
drawtype = 'normal',
|
||||
paramtype = 'light',
|
||||
groups = { processing_station = 1, dig_immediate = 2 }
|
||||
groups = { processing_container_station = 1, dig_immediate = 2 }
|
||||
},
|
||||
})
|
||||
|
||||
---@class ProcessingContainerStation : BaseProcessingStation
|
||||
---@field enableRegression boolean?
|
||||
---@field fuelGroup string
|
||||
---@field inputSlots integer
|
||||
---@field outputSlots integer
|
||||
---@field enableFuelPush boolean?
|
||||
---@field enableRegression boolean?
|
||||
---
|
||||
---@field fuelGroup string
|
||||
---@field inputSlots integer
|
||||
---@field outputSlots integer
|
||||
---
|
||||
---@field getMeta fun(self:ProcessingContainerStation, pos:MapVector):ProcessingContainerStation.MetaData
|
||||
local ProcessingContainerStation = BaseProcessingStation:extend({})
|
||||
|
||||
---Definition table for the `ProcessingContainerStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@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 fuelGroup string?
|
||||
---@field inputSlots integer?
|
||||
---@field outputSlots integer?
|
||||
---
|
||||
---@field fuelGroup string?
|
||||
---@field inputSlots integer?
|
||||
---@field outputSlots integer?
|
||||
|
||||
---@param name string
|
||||
---@param def ProcessingContainerStationDef
|
||||
---@param nodesDef BaseProcessingStation.NodesDef?
|
||||
function ProcessingContainerStation:new(name, def, nodesDef)
|
||||
---@param nodesDef BaseProcessingStation.NodesDefinition?
|
||||
function ProcessingContainerStation:new(def, nodesDef)
|
||||
---@type ProcessingContainerStation
|
||||
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
|
||||
local instance = self:getInstance(BaseProcessingStation:new(def))
|
||||
instance.enableFuelPush = def.enableFuelPush
|
||||
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
|
||||
@@ -101,18 +111,21 @@ function ProcessingContainerStation:new(name, def, nodesDef)
|
||||
end
|
||||
|
||||
-- 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
|
||||
|
||||
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
|
||||
|
||||
Smc.addNodeSuperclass(instance.activeNodeName, instance)
|
||||
end
|
||||
|
||||
Smc.addNodeSuperclass(instance.nodeName, instance)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Opens the player's inventory, adding the tab described in `self.tab`.
|
||||
---@param pos MapVector
|
||||
---@param player Player
|
||||
function ProcessingContainerStation:showForm(pos, player)
|
||||
@@ -128,11 +141,10 @@ end
|
||||
|
||||
---@param pos MapVector
|
||||
function ProcessingContainerStation:init(pos)
|
||||
---@type ProcessingContainerStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
local meta = self:getMeta(pos)
|
||||
meta:set_float('processing_time', 0.0)
|
||||
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()
|
||||
inventory:set_size('input', self.inputSlots)
|
||||
@@ -141,6 +153,11 @@ function ProcessingContainerStation:init(pos)
|
||||
self:startTimer(pos)
|
||||
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 meta ProcessingContainerStation.MetaData
|
||||
---@return boolean isProcessing
|
||||
@@ -155,6 +172,8 @@ function ProcessingContainerStation:getCraft(isProcessing, meta)
|
||||
|
||||
if craft == nil then
|
||||
isProcessing = false
|
||||
processingTime = 0
|
||||
lastCraftName = ''
|
||||
end
|
||||
|
||||
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
|
||||
isProcessing = false
|
||||
processingTime = 0
|
||||
lastCraftName = ''
|
||||
end
|
||||
|
||||
meta:set_float('processing_time', processingTime)
|
||||
@@ -172,6 +193,8 @@ function ProcessingContainerStation:getCraft(isProcessing, meta)
|
||||
return isProcessing, craft
|
||||
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 meta ProcessingContainerStation.MetaData
|
||||
---@param elapsed number
|
||||
@@ -197,7 +220,7 @@ function ProcessingContainerStation:processing(isProcessing, meta, elapsed, craf
|
||||
end
|
||||
|
||||
inventory:add_item('output', craft.output)
|
||||
processingTime = 0
|
||||
processingTime = processingTime - craft.time
|
||||
lastCraftName = ''
|
||||
end
|
||||
|
||||
@@ -212,42 +235,64 @@ function ProcessingContainerStation:processing(isProcessing, meta, elapsed, craf
|
||||
return isProcessing or (self.enableRegression == true and processingTime ~= 0)
|
||||
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 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 meta = self:getMeta(pos)
|
||||
local remainingFuel = meta:get_float('remaining_fuel')
|
||||
|
||||
local isProcessing, craft = self:getCraft(true, meta)
|
||||
|
||||
if energy <= 0 then
|
||||
if remainingFuel <= 0 then
|
||||
isProcessing = false
|
||||
end
|
||||
|
||||
isProcessing = self:processing(isProcessing, meta, elapsed, craft)
|
||||
self:pushFuelStation(meta, pos)
|
||||
|
||||
self:swapNode(isProcessing, pos)
|
||||
|
||||
PlayerInventoryUI:updateNodeForm(pos)
|
||||
|
||||
return isProcessing
|
||||
end
|
||||
|
||||
function ProcessingContainerStation:onPush(pos, energy)
|
||||
---@type ProcessingContainerStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
---@param pos MapVector
|
||||
---@param remainingFuel number
|
||||
function ProcessingContainerStation:onPush(pos, remainingFuel)
|
||||
local meta = self:getMeta(pos)
|
||||
|
||||
meta:set_float('energy', energy)
|
||||
core.log(tostring(energy))
|
||||
meta:set_float('remaining_fuel', remainingFuel)
|
||||
self:startTimer(pos)
|
||||
end
|
||||
|
||||
ProcessingStationEvents:subscribeOnPushContainer(ProcessingContainerStation.onPush)
|
||||
|
||||
--- DEBUG ---
|
||||
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
@@ -281,13 +326,13 @@ local tab = function(self, playerName, tabType)
|
||||
return form
|
||||
end
|
||||
|
||||
ProcessingContainerStation:new('test_processing_node', {
|
||||
ProcessingContainerStation:new({
|
||||
craftType = 'test',
|
||||
tab = tab,
|
||||
inputSlots = 2,
|
||||
fuelGroup = 'wood',
|
||||
enableIdling = true,
|
||||
enableRegression = false,
|
||||
enableFuelPush = true
|
||||
},
|
||||
{
|
||||
settings = {
|
||||
|
||||
@@ -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'
|
||||
|
||||
---**Only for EmmyLua.**
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
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()
|
||||
---@type Events.ProcessingStationEvents
|
||||
local ProcessingStationEvents = Smc.requireEvents('ProcessingStationEvents')
|
||||
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
local Node = Smc.requireNode()
|
||||
|
||||
local ProcessingFuelStationNode = Node:getExtended({
|
||||
@@ -18,29 +19,49 @@ local ProcessingFuelStationNode = Node:getExtended({
|
||||
})
|
||||
|
||||
---@class ProcessingFuelStation : BaseProcessingStation
|
||||
---@field alwaysEnable boolean?
|
||||
---@field enablerItemGroup string?
|
||||
---@field disablerItemGroup string?
|
||||
---@field enableIdling boolean?
|
||||
---@field enableContainerProcessing boolean?
|
||||
---@field fuelGroup string
|
||||
---@field fuelSlots integer
|
||||
---
|
||||
---@field fuelGroup string
|
||||
---@field fuelSlots integer
|
||||
---
|
||||
---@field getMeta fun(self:ProcessingFuelStation, pos:MapVector):ProcessingFuelStation.MetaData
|
||||
local ProcessingFuelStation = BaseProcessingStation:extend({})
|
||||
|
||||
---Definition table for the `ProcessingFuelStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@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 enableContainerProcessing boolean?
|
||||
---
|
||||
---@field fuelGroup string?
|
||||
---@field fuelSlots integer?
|
||||
|
||||
---@param name string
|
||||
---@param def ProcessingFuelStationDef
|
||||
---@param nodesDef BaseProcessingStation.NodesDef?
|
||||
function ProcessingFuelStation:new(name, def, nodesDef)
|
||||
---@param nodesDef BaseProcessingStation.NodesDefinition?
|
||||
function ProcessingFuelStation:new(def, nodesDef)
|
||||
---@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.enableContainerProcessing = def.enableContainerProcessing
|
||||
instance.fuelGroup = def.fuelGroup or 'fuel'
|
||||
instance.fuelSlots = def.fuelSlots or 1
|
||||
|
||||
@@ -61,6 +82,14 @@ function ProcessingFuelStation:new(name, def, nodesDef)
|
||||
instance:init(pos)
|
||||
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
|
||||
-- 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.
|
||||
@@ -108,17 +137,19 @@ function ProcessingFuelStation:new(name, def, nodesDef)
|
||||
if activeSettings ~= nil then
|
||||
local activeNode = ProcessingFuelStationNode:new({ settings = activeSettings, callbacks = callbacks })
|
||||
instance.activeNodeName = activeNode.itemDef.settings.name
|
||||
|
||||
Smc.addNodeSuperclass(instance.activeNodeName, instance)
|
||||
end
|
||||
|
||||
Smc.addNodeSuperclass(instance.nodeName, instance)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---@param pos MapVector
|
||||
function ProcessingFuelStation:init(pos)
|
||||
---@type ProcessingFuelStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
local meta = self:getMeta(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)
|
||||
@@ -126,52 +157,70 @@ function ProcessingFuelStation:init(pos)
|
||||
self:startTimer(pos)
|
||||
end
|
||||
|
||||
---@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
|
||||
---@param pos MapVector
|
||||
---@param node NodeTable
|
||||
---@param puncher ObjectRef|Player
|
||||
---@param pointedThing pointed_thing
|
||||
function ProcessingFuelStation:onPunch(pos, node, puncher, pointedThing)
|
||||
if self.alwaysEnable then
|
||||
return
|
||||
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 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 = core.get_item_group(fuelStack:get_name(), self.fuelGroup or 'fuel') / FUEL_ENERGY_FACTOR
|
||||
if not isEnable then
|
||||
if self.enablerItemGroup == nil then
|
||||
self:setMetaIsEnable(meta, true)
|
||||
self:startTimer(pos)
|
||||
|
||||
if fuelEnergy > 0 then
|
||||
local itemCount = fuelStack:get_count()
|
||||
return
|
||||
end
|
||||
|
||||
-- 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 = math.min(math.ceil(elapsed/fuelEnergy), itemCount)
|
||||
else
|
||||
itemCount = 1
|
||||
end
|
||||
local itemName = puncher:get_wielded_item():get_name()
|
||||
if coreGetItemGroup(itemName, self.enablerItemGroup) > 0 then
|
||||
self:setMetaIsEnable(meta, true)
|
||||
self:startTimer(pos)
|
||||
end
|
||||
else
|
||||
if self.disablerItemGroup == nil then
|
||||
self:setMetaIsEnable(meta, false)
|
||||
self:startTimer(pos)
|
||||
|
||||
remainingFuel = remainingFuel + (fuelEnergy * itemCount)
|
||||
return
|
||||
end
|
||||
|
||||
fuelStack:set_count(itemCount)
|
||||
inventory:remove_item('fuel', fuelStack)
|
||||
end
|
||||
local itemName = puncher:get_wielded_item():get_name()
|
||||
if coreGetItemGroup(itemName, self.disablerItemGroup) > 0 then
|
||||
self:setMetaIsEnable(meta, false)
|
||||
self:startTimer(pos)
|
||||
end
|
||||
end
|
||||
|
||||
isProcessing = true
|
||||
|
||||
meta:set_float('remaining_fuel', remainingFuel)
|
||||
|
||||
return isProcessing
|
||||
end
|
||||
|
||||
---@param meta ProcessingFuelStation.MetaData
|
||||
---@param isEnable boolean
|
||||
function ProcessingFuelStation:setMetaIsEnable(meta, isEnable)
|
||||
if isEnable then
|
||||
meta:set_string('is_enable', 'true')
|
||||
else
|
||||
meta:set_string('is_enable', 'false')
|
||||
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 meta ProcessingFuelStation.MetaData
|
||||
---@param elapsed number
|
||||
@@ -195,26 +244,83 @@ function ProcessingFuelStation:burnFuel(isProcessing, meta, elapsed)
|
||||
return isProcessing or (self.enableIdling == true and remainingFuel ~= 0)
|
||||
end
|
||||
|
||||
---@param pos MapVector
|
||||
function ProcessingFuelStation:pushContainer(pos)
|
||||
if not self.enableContainerProcessing then
|
||||
---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
|
||||
function ProcessingFuelStation:pushContainerStation(meta, pos)
|
||||
local containerPos = pos + v(0, 1, 0)
|
||||
local containerName = coreGetNode(containerPos).name
|
||||
|
||||
if coreGetItemGroup(containerName, 'processing_container_station') == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local containerPos = pos + vector.new(0, 1, 0)
|
||||
---@type ProcessingContainerStation.MetaData
|
||||
local containerMeta = core.get_meta(containerPos)
|
||||
---@type ProcessingContainerStation?
|
||||
local containerStation = Smc.getNodeSuperclassByPos(containerPos)
|
||||
assert(containerStation ~= nil)
|
||||
|
||||
local fuelGroup = containerMeta:get_string('fuel_group')
|
||||
|
||||
if fuelGroup ~= self.fuelGroup then
|
||||
if containerStation.fuelGroup ~= self.fuelGroup then
|
||||
return
|
||||
end
|
||||
|
||||
---@type ProcessingFuelStation.MetaData
|
||||
local fuelMeta = core.get_meta(pos)
|
||||
|
||||
ProcessingStationEvents:callOnPushContainer(containerPos, fuelMeta:get_float('remaining_fuel'))
|
||||
containerStation:onPush(containerPos, meta:get_float('remaining_fuel'))
|
||||
end
|
||||
|
||||
---@param pos MapVector
|
||||
@@ -223,11 +329,12 @@ end
|
||||
---@param elapsed number
|
||||
---@return boolean?
|
||||
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)
|
||||
isProcessing = self:takeFuel(isProcessing, meta, elapsed) -- TODO: if-else
|
||||
self:pushContainer(pos)
|
||||
local isProcessing = self:burnFuel(isEnable, meta, elapsed)
|
||||
isProcessing = self:takeFuel(isProcessing, meta, elapsed)
|
||||
self:pushContainerStation(meta, pos)
|
||||
|
||||
self:swapNode(isProcessing, pos)
|
||||
|
||||
@@ -236,6 +343,18 @@ function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
|
||||
return isProcessing
|
||||
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 ---
|
||||
|
||||
local Formspec = Smc.requireFormspec()
|
||||
@@ -262,11 +381,12 @@ local tab = function(self, playerName, tabType)
|
||||
return form
|
||||
end
|
||||
|
||||
ProcessingFuelStation:new('test_processing_node', {
|
||||
ProcessingFuelStation:new({
|
||||
craftType = 'test',
|
||||
tab = tab,
|
||||
inputSlots = 2,
|
||||
fuelGroup = 'wood',
|
||||
alwaysEnable = false,
|
||||
enableIdling = false,
|
||||
enableContainerProcessing = true,
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---@alias ProcessingFuelStation.MetaData.FloatKey 'remaining_fuel'
|
||||
---@alias ProcessingFuelStation.MetaData.StringKey 'fuel_group'
|
||||
---@alias ProcessingFuelStation.MetaData.StringKey 'is_enable'
|
||||
|
||||
---**Only for EmmyLua.**
|
||||
---@class ProcessingFuelStation.MetaData : NodeMetaRef
|
||||
|
||||
Reference in New Issue
Block a user