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,14 +1,15 @@
local TICKRATE = 1
local CraftSystem = Smc.requireCraftSystem()
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
---@class BaseProcessingStation : Class
---@field name string
---@field timeout number
---@field craftType CraftSystem.ProcessingCraftType
---@field handler CraftSystem.ProcessingCraftHandler
---@field tab PlayerInventoryUI.getTabFunc
---@field nodeName string
---@field nodeName string?
---@field activeNodeName string?
local BaseProcessingStation = Class:extend({})
@@ -16,7 +17,6 @@ local BaseProcessingStation = Class:extend({})
---
---**Only for EmmyLua.**
---@class BaseProcessingStationDef
---@field timeout number?
---@field craftType CraftSystem.ProcessingCraftType
---@field tab PlayerInventoryUI.getTabFunc
@@ -24,14 +24,13 @@ local BaseProcessingStation = Class:extend({})
---@param name string
---@param def BaseProcessingStationDef
---@param nodesDef BaseProcessingStation.NodesDef
---@param nodesDef BaseProcessingStation.NodesDef?
function BaseProcessingStation:new(name, def, nodesDef)
local handler = CraftSystem:getProcessingCraftHandler(def.craftType)
---@type BaseProcessingStation
local instance = self:getInstance({})
instance.name = name
instance.timeout = def.timeout or 1
instance.craftType = def.craftType
instance.handler = handler
instance.tab = def.tab
@@ -40,7 +39,7 @@ function BaseProcessingStation:new(name, def, nodesDef)
end
---Opens the player's inventory, adding the node tab described in `self.tab`.
---@param pos MapPosition
---@param pos MapVector
---@param player Player
function BaseProcessingStation:showForm(pos, player)
local playerName = player:get_player_name()
@@ -58,12 +57,12 @@ end
---The timer timeout is set to the timeout specified `self.timeout`.
---
---Returns `true` if the timer has been started.
---@param pos MapPosition
---@param pos MapVector
---@return boolean
function BaseProcessingStation:startTimer(pos)
local timer = core.get_node_timer(pos)
if not timer:is_started() then
timer:start(self.timeout)
timer:start(TICKRATE)
return true
end
@@ -75,8 +74,10 @@ end
---If an active node is not specified for a station,
---then no replacement will occur regardless of the status of the process.
---@param isProcessing boolean The stations process status. If `true`, the process is running.
---@param pos MapPosition
---@param pos MapVector
function BaseProcessingStation:swapNode(isProcessing, pos)
assert(self.nodeName ~= nil)
if self.activeNodeName ~= nil then
if isProcessing then
core.swap_node(pos, {name = self.activeNodeName})

View File

@@ -5,7 +5,7 @@ local Control = {}
---@param callbackFunc fun(player:Player, controlName:string)
function Control.subcribeOnPress(callbackFunc)
Events.subscribe('control.on_press', callbackFunc)
Events:subscribe('control.on_press', callbackFunc)
end
@@ -13,7 +13,7 @@ end
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number)
function Control.subcribeOnRelease(callbackFunc)
Events.subscribe('control.on_release', callbackFunc)
Events:subscribe('control.on_release', callbackFunc)
end
@@ -21,7 +21,7 @@ end
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number, dtime:number)
function Control.subcribeOnHold(callbackFunc)
Events.subscribe('control.on_hold', callbackFunc)
Events:subscribe('control.on_hold', callbackFunc)
end
@@ -29,7 +29,7 @@ end
---@param callbackFunc fun(player:Player, player_wield_index:number, player_last_wield_index:number)
function Control.subcribeOnWieldChange(callbackFunc)
Events.subscribe('control.on_wield_change', callbackFunc)
Events:subscribe('control.on_wield_change', callbackFunc)
end
@@ -107,19 +107,19 @@ Smc.registerGlobalStepForEachPlayer(0, function(deltaTime, player)
-- on_press
if not lastPressed and nowPressed then
Events.call('control.on_press', key)
Events:call('control.on_press', key)
controlTimer[key] = 0
-- on_release
elseif lastPressed and not nowPressed then
Events.call('control.on_release', key, controlTimer[key])
Events:call('control.on_release', key, controlTimer[key])
controlTimer[key] = 0
-- on_hold
elseif lastPressed and nowPressed then
Events.call('control.on_hold', key, controlTimer[key], deltaTime)
Events:call('control.on_hold', key, controlTimer[key], deltaTime)
controlTimer[key] = controlTimer[key] + deltaTime
end

View File

@@ -48,7 +48,7 @@ function ImmersiveCraft:new(name, def)
instance.stagesByName = stagesByName
-- Subscribe to an event that will determine that the player has completed the crafting stage.
Events.subscribe('craft_system:next_immersive_stage:'..name,
Events:subscribe('craft_system:next_immersive_stage:'..name,
---@param eventName string
---@param stage CraftSystem.ImmersiveCraftStage
---@param pos MapVector

View File

@@ -59,7 +59,7 @@ local tappingNode = ImmersiveCraftNode:new({
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
on_punch = function(luaEntity, puncher, time_from_last_punch, tool_capabilities, dir, damage)
-- We simply trigger the event that the player hits the crafting node, bound to the work station with coordinates.
Events.call(('craft_system:punch_tapping_node:%s:%s'):format(
Events:call(('craft_system:punch_tapping_node:%s:%s'):format(
luaEntity._craft_name, luaEntity._stage_name), luaEntity._work_station_pos, puncher, luaEntity)
return false

View File

@@ -104,7 +104,7 @@ function TappingStage:new(def)
entity:remove()
Events.call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, player)
Events:call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, player)
end
-- Registering the aborting the crafting process handler.
@@ -147,7 +147,7 @@ function TappingStage:new(def)
end
-- Subscribe to an event that means the player hit the entity.
Events.subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name),
Events:subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name),
---@param craftNode LuaEntity
---@param player Player
function(eventName, pos, player, craftNode)

View File

@@ -13,7 +13,7 @@ local Callbacks = Class.extend(ItemCallbacks, {})
---
---default: nil
---* `pos`: node position
---@type fun(pos:MapPosition)?
---@type fun(pos:MapVector)?
Callbacks.on_construct = nil
---Node destructor; called before removing node.
@@ -21,7 +21,7 @@ Callbacks.on_construct = nil
---
---default: nil
---* `pos`: node position
---@type fun(pos:MapPosition)?
---@type fun(pos:MapVector)?
Callbacks.on_destruct = nil
---Node destructor; called after removing node.
@@ -30,7 +30,7 @@ Callbacks.on_destruct = nil
---default: nil
---* `pos`: node position
---* `oldnode`: node table of node before it was deleted
---@type fun(pos:MapPosition, old_node)?
---@type fun(pos:MapVector, old_node)?
Callbacks.after_destruct = nil
---Called when a liquid (newnode) is about to flood oldnode, if it has
@@ -45,7 +45,7 @@ Callbacks.after_destruct = nil
---* `pos`: table node position
---* `oldnode`: table node table of node before it was deleted
---* `newnode`: TODO: какой тут тип?
---@type fun(pos:MapPosition, old_node, new_node)?
---@type fun(pos:MapVector, old_node, new_node)?
Callbacks.on_flood = nil
@@ -61,7 +61,7 @@ Callbacks.on_flood = nil
---* `drops`: a table of `ItemStack`s, so any metadata to be preserved can
--- be added directly to one or more of the dropped items. See
--- "ItemStackMetaRef".
---@type fun(pos:MapPosition, old_node, new_node, old_meta, drops:ItemStack[])?
---@type fun(pos:MapVector, old_node, new_node, old_meta, drops:ItemStack[])?
Callbacks.preserve_metadata = nil
---Called after constructing node when node was placed using
@@ -73,7 +73,7 @@ Callbacks.preserve_metadata = nil
---* `placer`: may be any valid ObjectRef or nil
---* `itemstack`: TODO: сделать описание
---* `pointed_thing`: Vector of the direction of the gaze of the person who placed the block
---@type fun(pos:MapPosition, placer:Player|ObjectRef|nil, itemstack:ItemStack, pointed_thing:pointed_thing)?
---@type fun(pos:MapVector, placer:Player|ObjectRef|nil, itemstack:ItemStack, pointed_thing:pointed_thing)?
Callbacks.after_place_node = nil
---Called after destructing the node when node was dug using
@@ -92,7 +92,7 @@ Callbacks.after_dig_node = nil
---default: nil
---* `pos`: node position
---* player: ObjectRef of digger
---@type fun(pos:MapPosition, player:Player|ObjectRef|nil)?
---@type fun(pos:MapVector, player:Player|ObjectRef|nil)?
Callbacks.can_dig = nil
---Called when puncher (an ObjectRef) punches the node at pos.
@@ -103,7 +103,7 @@ Callbacks.can_dig = nil
---* `node`: node being hit
---* `puncher`: ObjectRef of punch
---* `pointed_thing`: Vector of the direction of the gaze of the person who placed the block
---@type fun(pos:MapPosition, node:NodeTable, puncher:Player|ObjectRef|nil, pointed_thing:pointed_thing)?
---@type fun(pos:MapVector, node:NodeTable, puncher:Player|ObjectRef|nil, pointed_thing:pointed_thing)?
Callbacks.on_punch = nil
---Called when clicker (an ObjectRef) used the 'place/build' key
@@ -115,7 +115,7 @@ Callbacks.on_punch = nil
---Note: pointed_thing can be nil, if a mod calls this function.
---
---default: nil
---@type (fun(pos:MapPosition, node:NodeTable, clicker:Player|ObjectRef, itemstack:ItemStack, pointed_thing:pointed_thing|nil):ItemStack|nil)?
---@type (fun(pos:MapVector, node:NodeTable, clicker:Player|ObjectRef, itemstack:ItemStack, pointed_thing:pointed_thing|nil):ItemStack|nil)?
Callbacks.on_rightclick = core.node_punch
@@ -124,7 +124,7 @@ Callbacks.on_rightclick = core.node_punch
---Deprecated: returning nil is the same as returning true.
---
---default: core.node_dig
---@type (fun(pos:MapPosition, node:NodeTable, digger:Player): boolean)?
---@type (fun(pos:MapVector, node:NodeTable, digger:Player): boolean)?
Callbacks.on_dig = minetest.node_dig
---called by NodeTimers, see core.get_node_timer and NodeTimerRef.
@@ -134,7 +134,7 @@ Callbacks.on_dig = minetest.node_dig
---return true to run the timer for another cycle with the same timeout value.
---
---default: nil
---@type (fun(pos:MapPosition, elapsed:number, node:NodeTable, timeout:number):boolean?)?
---@type (fun(pos:MapVector, elapsed:number, node:NodeTable, timeout:number):boolean?)?
Callbacks.on_timer = nil
---fields = {name1 = value1, name2 = value2, ...}
@@ -144,7 +144,7 @@ Callbacks.on_timer = nil
---See `core.register_on_player_receive_fields` for more info.
---
---default: nil
---@type fun(pos:MapPosition, formname:string, fields:table, sender:Player)?
---@type fun(pos:MapVector, formname:string, fields:table, sender:Player)?
Callbacks.on_receive_fields = nil
---Called when a player wants to put something into the inventory.
@@ -152,7 +152,7 @@ Callbacks.on_receive_fields = nil
---Return value: number of items allowed to put.
---
---Return value -1: Allow and don't modify item count in inventory.
---@type (fun(pos:MapPosition, from_list:string, from_index:number, to_list:string, to_index:number, count:number, player:Player): number)?
---@type (fun(pos:MapVector, from_list:string, from_index:number, to_list:string, to_index:number, count:number, player:Player): number)?
Callbacks.allow_metadata_inventory_move = nil
---Called when a player wants to take something out of the inventory.
@@ -160,31 +160,31 @@ Callbacks.allow_metadata_inventory_move = nil
---Return value: number of items allowed to take.
---
---Return value -1: Allow and don't modify item count in inventory.
---@type (fun(pos:MapPosition, listname:string, index:number, stack:ItemStack, player:Player): number)?
---@type (fun(pos:MapVector, listname:string, index:number, stack:ItemStack, player:Player): number)?
Callbacks.allow_metadata_inventory_take = nil
---Called after the actual action has happened, according to what was allowed.
---
---No return value.
---@type (fun(pos:MapPosition, from_list:string, from_index:number, to_list:string, to_index:number, count:number, player:Player): void)?
---@type (fun(pos:MapVector, from_list:string, from_index:number, to_list:string, to_index:number, count:number, player:Player): void)?
Callbacks.on_metadata_inventory_move = nil
---Called after the actual action has happened, according to what was allowed.
---
---No return value.
---@type (fun(pos:MapPosition, listname:string, index:number, stack:ItemStack, player:Player): void)?
---@type (fun(pos:MapVector, listname:string, index:number, stack:ItemStack, player:Player): void)?
Callbacks.on_metadata_inventory_put = nil
---Called after the actual action has happened, according to what was allowed.
---
---No return value.
---@type (fun(pos:MapPosition, listname:string, index:number, stack:ItemStack, player:Player): void)?
---@type (fun(pos:MapVector, listname:string, index:number, stack:ItemStack, player:Player): void)?
Callbacks.on_metadata_inventory_take = nil
---If defined, called when an explosion touches the node, instead of
---removing the node.
---* `intensity`: 1.0 = mid range of regular TNT.
---@type (fun(pos:MapPosition, intensity:number): void)?
---@type (fun(pos:MapVector, intensity:number): void)?
Callbacks.on_blast = nil
return Callbacks

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)
}
}--]]
}
})
--]]

View File

@@ -1,5 +1,5 @@
---@alias ProcessingContainerStation.MetaData.FloatKey 'processing_time'
---@alias ProcessingContainerStation.MetaData.StringKey 'craft_name'
---@alias ProcessingContainerStation.MetaData.FloatKey 'processing_time'|'energy'
---@alias ProcessingContainerStation.MetaData.StringKey 'craft_name'|'fuel_group'
---**Only for EmmyLua.**
---@class ProcessingContainerStation.MetaData : NodeMetaRef

View File

@@ -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

View File

@@ -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)

View File

@@ -28,11 +28,10 @@ local ProcessingStation = BaseProcessingStation:extend({})
---@param def ProcessingStationDef
---@param nodesDef BaseProcessingStation.NodesDef
function ProcessingStation:new(name, def, nodesDef)
---@type ProcessingStation
local instance = self:getInstance(BaseProcessingStation:new(name, def, nodesDef))
instance.containerStation = ProcessingContainerStation:new(name, def, nodesDef)
instance.fuelStation = ProcessingFuelStation:new(name, def, nodesDef)
local instance = self:getInstance(BaseProcessingStation:new(name, def))
instance.containerStation = ProcessingContainerStation:new(name, def)
instance.fuelStation = ProcessingFuelStation:new(name, def)
local settings = nodesDef.settings
local activeSettings = nodesDef.activeSettings
@@ -63,21 +62,21 @@ function ProcessingStation:new(name, def, nodesDef)
callbacks.on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
oldCallback(pos, from_list, from_index, to_list, to_index, count, player)
instance:startTimer(pos)
instance.fuelStation:startTimer(pos)
end
oldCallback = callbacks.on_metadata_inventory_put or function(...) end
callbacks.on_metadata_inventory_put = function(pos, listname, index, stack, player)
oldCallback(pos, listname, index, stack, player)
instance:startTimer(pos)
instance.fuelStation:startTimer(pos)
end
oldCallback = callbacks.on_metadata_inventory_take or function(...) end
callbacks.on_metadata_inventory_take = function(pos, listname, index, stack, player)
oldCallback(pos, listname, index, stack, player)
instance:startTimer(pos)
instance.fuelStation:startTimer(pos)
end
oldCallback = callbacks.on_timer or function(...) end
@@ -100,7 +99,7 @@ function ProcessingStation:new(name, def, nodesDef)
return instance
end
---@param pos MapPosition
---@param pos MapVector
---@param elapsed number
---@param node NodeTable
---@param timeout number
@@ -108,12 +107,12 @@ end
function ProcessingStation:onTimer(pos, elapsed, node, timeout)
local meta = core.get_meta(pos)
local isProcessing, craft = self.containerStation:getCraft(true, meta, timeout)
core.log(tostring(isProcessing))
isProcessing = self.fuelStation:burnFuel(isProcessing, meta, timeout)
core.log(tostring(isProcessing))
isProcessing = self.containerStation:processing(isProcessing, meta, timeout, craft)
core.log(tostring(isProcessing))
local isProcessing, craft = self.containerStation:getCraft(true, meta)
isProcessing = self.fuelStation:burnFuel(isProcessing, meta, elapsed)
if craft ~= nil then
isProcessing = self.fuelStation:takeFuel(isProcessing, meta, elapsed)
end
isProcessing = self.containerStation:processing(isProcessing, meta, elapsed, craft)
self:swapNode(isProcessing, pos)
@@ -153,8 +152,8 @@ local tab = function(self, playerName, tabType)
: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, 3, tostring(math.round(meta:get_float('remaining_fuel'))))
:label(4.5, 3, tostring(math.round(meta:get_float('processing_time'))))
return form
end
@@ -165,7 +164,8 @@ ProcessingStation:new('test_processing_node', {
inputSlots = 2,
fuelGroup = 'wood',
enableIdling = true,
enableRegression = true,
enableContainerProcessing = true,
--enableRegression = true,
},
{
settings = {

View File

@@ -1,31 +1,4 @@
Events = {}
local modInfo = Mod.getInfo('smc__system__events')
local require = modInfo.require
---@alias Events.callback fun(eventName, ...)
---@type table<string, Events.callback[]>
local registeredEvents = {}
---@param eventName string
---@param callback Events.callback
function Events.subscribe(eventName, callback)
local callbacks = registeredEvents[eventName]
if callbacks == nil then
registeredEvents[eventName] = {callback}
else
table.insert(callbacks, callback)
end
end
---@param eventName string
---@param ... any
function Events.call(eventName, ...)
local callbacks = registeredEvents[eventName]
if callbacks == nil then
return
end
for _, func in ipairs(callbacks) do
func(eventName, ...)
end
end
return require('Events')

View File

@@ -1,2 +1,2 @@
name = smc__system__events
depends = smc__system__table
depends = smc__system__table, smc__system__require, smc__system__class

View File

@@ -0,0 +1,37 @@
---@alias Events.Callback fun(eventName:string, ...)
---@alias Events.Types
--- 'ProcessingStationEvents'
---@class Events : Class
---@field registeredEvents table<string, Events.Callback[]>
Events = Class:extend({
registeredEvents = {}
})
---@param eventName string
---@param callback Events.Callback
function Events:subscribe(eventName, callback)
local callbacks = self.registeredEvents[eventName]
if callbacks == nil then
self.registeredEvents[eventName] = {callback}
else
table.insert(callbacks, callback)
end
end
---@param eventName string
---@param ... any
function Events:call(eventName, ...)
local callbacks = self.registeredEvents[eventName]
if callbacks == nil then
return
end
for _, func in ipairs(callbacks) do
func(eventName, ...)
end
end
return Events

View File

@@ -0,0 +1,21 @@
---@alias Events.ProcessingStationEvents.EventName 'on_push_container'
---@alias Events.ProcessingStationEvents.Callback fun(eventName:Events.ProcessingStationEvents.EventName, ...)
---@class Events.ProcessingStationEvents : Events
---@field subscribe fun(self:Events.ProcessingStationEvents,eventName:Events.ProcessingStationEvents.EventName, callback:Events.ProcessingStationEvents.Callback)
---@field call fun(self:Events.ProcessingStationEvents,eventName:Events.ProcessingStationEvents.EventName, ...)
local ProcessingStationEvents = Events:extend()
---@param callback fun(eventName:Events.ProcessingStationEvents.EventName, pos:MapVector, energy:number)
function ProcessingStationEvents:subscribeOnPushContainer(callback)
self:subscribe('on_push_container', callback)
end
---@param pos MapVector
---@param energy number
function ProcessingStationEvents:callOnPushContainer(pos, energy)
self:call('on_push_container', pos, energy)
end
return ProcessingStationEvents

View File

@@ -103,3 +103,12 @@ function Smc.requireProcessingFuelStation()
return class
end
---@param eventsType Events.Types
---@return Events
function Smc.requireEvents(eventsType)
---@type Events
local class = Mod.getInfo('smc__system__events').require('Events.'..eventsType)
return class
end