Compare commits
5 Commits
dc0d5dc2c1
...
3eb8b375ec
| Author | SHA1 | Date | |
|---|---|---|---|
| 3eb8b375ec | |||
| 0a6d19102a | |||
| 78c1556f26 | |||
| c6e12ebb08 | |||
| 44952915e7 |
4
mods/CORE/BaseProcessingStation/init.lua
Normal file
4
mods/CORE/BaseProcessingStation/init.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
local modInfo = Mod.getInfo('smc__core__base_processing_station')
|
||||
local require = modInfo.require
|
||||
|
||||
return require('BaseProcessingStation')
|
||||
2
mods/CORE/BaseProcessingStation/mod.conf
Normal file
2
mods/CORE/BaseProcessingStation/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
||||
name = smc__core__base_processing_station
|
||||
depends = smc__core__craft_system, smc__core__node
|
||||
@@ -0,0 +1,89 @@
|
||||
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 activeNodeName string?
|
||||
local BaseProcessingStation = Class:extend({})
|
||||
|
||||
---Definition table for the `BaseProcessingStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class BaseProcessingStationDef
|
||||
---@field timeout number?
|
||||
---@field craftType CraftSystem.ProcessingCraftType
|
||||
---@field tab PlayerInventoryUI.getTabFunc
|
||||
|
||||
---@alias BaseProcessingStation.NodesDef {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)
|
||||
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
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Opens the player's inventory, adding the node tab described in `self.tab`.
|
||||
---@param pos MapPosition
|
||||
---@param player Player
|
||||
function BaseProcessingStation:showForm(pos, player)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:addActiveNodeForm(pos, playerName)
|
||||
PlayerInventoryUI:setContex(playerName, 'left_tab_name', 'node_tab')
|
||||
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', pos)
|
||||
PlayerInventoryUI:setContex(playerName, 'node_tab', self.tab)
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
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 MapPosition
|
||||
---@return boolean
|
||||
function BaseProcessingStation:startTimer(pos)
|
||||
local timer = core.get_node_timer(pos)
|
||||
if not timer:is_started() then
|
||||
timer:start(self.timeout)
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
---Replaces the node with active or inactive depending on the status of the process.
|
||||
---
|
||||
---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
|
||||
function BaseProcessingStation:swapNode(isProcessing, pos)
|
||||
if self.activeNodeName ~= nil then
|
||||
if isProcessing then
|
||||
core.swap_node(pos, {name = self.activeNodeName})
|
||||
else
|
||||
core.swap_node(pos, {name = self.nodeName})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return BaseProcessingStation
|
||||
4
mods/CORE/ProcessingContainerStation/init.lua
Normal file
4
mods/CORE/ProcessingContainerStation/init.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
local modInfo = Mod.getInfo('smc__core__processing_container_station')
|
||||
local require = modInfo.require
|
||||
|
||||
return require('ProcessingContainerStation')
|
||||
2
mods/CORE/ProcessingContainerStation/mod.conf
Normal file
2
mods/CORE/ProcessingContainerStation/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
||||
name = smc__core__processing_container_station
|
||||
depends = smc__core__base_processing_station
|
||||
@@ -0,0 +1,290 @@
|
||||
local FUEL_ENERGY_FACTOR = 0.1
|
||||
|
||||
local BaseProcessingStation = Smc.requireBaseProcessingStation()
|
||||
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
local Node = Smc.requireNode()
|
||||
|
||||
local ProcessingStationNode = Node:getExtended({
|
||||
settings = {
|
||||
name = '',
|
||||
drawtype = 'normal',
|
||||
paramtype = 'light',
|
||||
groups = { processing_station = 1, dig_immediate = 2 }
|
||||
},
|
||||
})
|
||||
|
||||
---@class ProcessingContainerStation : BaseProcessingStation
|
||||
---@field enableRegression boolean?
|
||||
---@field inputSlots integer
|
||||
---@field outputSlots integer
|
||||
local ProcessingContainerStation = BaseProcessingStation:extend({})
|
||||
|
||||
---Definition table for the `ProcessingContainerStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class ProcessingContainerStationDef : BaseProcessingStationDef
|
||||
---@field enableRegression boolean?
|
||||
---@field inputSlots integer?
|
||||
---@field outputSlots integer?
|
||||
|
||||
---@param name string
|
||||
---@param def ProcessingContainerStationDef
|
||||
---@param nodesDef BaseProcessingStation.NodesDef
|
||||
function ProcessingContainerStation:new(name, def, nodesDef)
|
||||
---@type ProcessingContainerStation
|
||||
local instance = self:getInstance(BaseProcessingStation:new(name, def, nodesDef))
|
||||
instance.enableRegression = def.enableRegression
|
||||
instance.inputSlots = def.inputSlots or 1
|
||||
instance.outputSlots = def.outputSlots or 1
|
||||
|
||||
local settings = nodesDef.settings
|
||||
local activeSettings = nodesDef.activeSettings
|
||||
local callbacks = nodesDef.callbacks or {}
|
||||
|
||||
local oldCallback = callbacks.on_construct or function(...) end
|
||||
-- When we install a station in the world,
|
||||
-- we initialize the metadata, set the inventory size, and start the timer.
|
||||
callbacks.on_construct = function(pos)
|
||||
oldCallback(pos)
|
||||
|
||||
instance:init(pos)
|
||||
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.
|
||||
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||
oldCallback(pos, node, clicker, itemstack, pointed_thing)
|
||||
|
||||
instance:showForm(pos, clicker)
|
||||
end
|
||||
|
||||
oldCallback = callbacks.on_metadata_inventory_move or function(...) end
|
||||
-- Whenever we change items, we start the station timer,
|
||||
-- because it can be stopped (if the crafting or fuel runs out).
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
end
|
||||
|
||||
oldCallback = callbacks.on_timer or function(...) end
|
||||
-- The basic logic of the station's work.
|
||||
callbacks.on_timer = function(pos, elapsed, node, timeout)
|
||||
oldCallback(pos, elapsed, node, timeout)
|
||||
|
||||
return self:onTimer(pos, elapsed, node, timeout)
|
||||
end
|
||||
|
||||
-- Registering a nodes for Luanti.
|
||||
local node = ProcessingStationNode:new({ settings = settings, callbacks = callbacks })
|
||||
instance.nodeName = node.itemDef.settings.name
|
||||
|
||||
if activeSettings ~= nil then
|
||||
local activeNode = ProcessingStationNode:new({ settings = activeSettings, callbacks = callbacks })
|
||||
instance.activeNodeName = activeNode.itemDef.settings.name
|
||||
end
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Opens the player's inventory, adding the tab described in `self.tab`.
|
||||
---@param pos MapPosition
|
||||
---@param player Player
|
||||
function ProcessingContainerStation:showForm(pos, player)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:addActiveNodeForm(pos, playerName)
|
||||
PlayerInventoryUI:setContex(playerName, 'left_tab_name', 'node_tab')
|
||||
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', pos)
|
||||
PlayerInventoryUI:setContex(playerName, 'node_tab', self.tab)
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
function ProcessingContainerStation:init(pos)
|
||||
---@type ProcessingContainerStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
meta:set_float('processing_time', 0.0)
|
||||
|
||||
local inventory = meta:get_inventory()
|
||||
inventory:set_size('input', self.inputSlots)
|
||||
inventory:set_size('output', self.outputSlots)
|
||||
|
||||
self:startTimer(pos)
|
||||
end
|
||||
|
||||
---@param isProcessing boolean
|
||||
---@param meta ProcessingContainerStation.MetaData
|
||||
---@param timeout number
|
||||
---@return boolean isProcessing
|
||||
---@return CraftSystem.ProcessingCraft?
|
||||
function ProcessingContainerStation:getCraft(isProcessing, meta, timeout)
|
||||
local inventory = meta:get_inventory()
|
||||
|
||||
local processingTime = meta:get_float('processing_time')
|
||||
local lastCraftName = meta:get_string('craft_name')
|
||||
|
||||
local craft = self.handler:getCraftByRecipe(inventory:get_list('input'))
|
||||
|
||||
if craft == nil then
|
||||
isProcessing = false
|
||||
end
|
||||
|
||||
if isProcessing and craft ~= nil and craft.name ~= lastCraftName then
|
||||
processingTime = 0
|
||||
lastCraftName = craft.name
|
||||
end
|
||||
|
||||
if isProcessing and craft ~= nil and not inventory:room_for_item('output', craft.output) then
|
||||
isProcessing = false
|
||||
end
|
||||
|
||||
meta:set_float('processing_time', processingTime)
|
||||
meta:set_string('craft_name', lastCraftName)
|
||||
|
||||
return isProcessing, craft
|
||||
end
|
||||
|
||||
---@param isProcessing boolean
|
||||
---@param meta ProcessingContainerStation.MetaData
|
||||
---@param timeout number
|
||||
---@param craft CraftSystem.ProcessingCraft?
|
||||
---@return boolean isProcessing
|
||||
function ProcessingContainerStation:processing(isProcessing, meta, timeout, craft)
|
||||
local inventory = meta:get_inventory()
|
||||
|
||||
local processingTime = meta:get_float('processing_time')
|
||||
|
||||
if isProcessing and craft ~= nil then
|
||||
processingTime = processingTime + timeout
|
||||
else
|
||||
if self.enableRegression then
|
||||
processingTime = processingTime - timeout
|
||||
end
|
||||
end
|
||||
|
||||
if isProcessing and craft ~= nil and processingTime >= craft.time then
|
||||
for _, item in ipairs(craft.recipe) do
|
||||
inventory:remove_item('input', item)
|
||||
end
|
||||
|
||||
inventory:add_item('output', craft.output)
|
||||
processingTime = 0
|
||||
end
|
||||
|
||||
if processingTime < 0 then
|
||||
processingTime = 0
|
||||
end
|
||||
|
||||
meta:set_float('processing_time', processingTime)
|
||||
|
||||
|
||||
return isProcessing or (self.enableRegression == true and processingTime ~= 0)
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
---@param elapsed number
|
||||
---@param node NodeTable
|
||||
---@param timeout number
|
||||
---@return boolean?
|
||||
function ProcessingContainerStation:onTimer(pos, elapsed, node, timeout)
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local isProcessing, craft = self:getCraft(true, meta, timeout)
|
||||
local isProcessing = self:processing(isProcessing, meta, timeout, craft)
|
||||
|
||||
self:swapNode(isProcessing, pos)
|
||||
|
||||
PlayerInventoryUI:updateNodeForm(pos)
|
||||
|
||||
return isProcessing
|
||||
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
|
||||
---@param tabType PlayerInventoryUI.TabType
|
||||
---@return Formspec
|
||||
local tab = function(self, playerName, tabType)
|
||||
local form = Formspec:new()
|
||||
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)
|
||||
|
||||
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')))
|
||||
|
||||
return form
|
||||
end
|
||||
|
||||
ProcessingContainerStation:new('test_processing_node', {
|
||||
craftType = 'test',
|
||||
tab = tab,
|
||||
inputSlots = 2,
|
||||
fuelGroup = 'wood',
|
||||
enableIdling = true,
|
||||
enableRegression = false,
|
||||
},
|
||||
{
|
||||
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 ProcessingContainerStation
|
||||
@@ -0,0 +1,9 @@
|
||||
---@alias ProcessingContainerStation.MetaData.FloatKey 'processing_time'
|
||||
---@alias ProcessingContainerStation.MetaData.StringKey 'craft_name'
|
||||
|
||||
---**Only for EmmyLua.**
|
||||
---@class ProcessingContainerStation.MetaData : NodeMetaRef
|
||||
---@field get_float fun(self, key:ProcessingContainerStation.MetaData.FloatKey):number
|
||||
---@field set_float fun(self, key:ProcessingContainerStation.MetaData.FloatKey, value:number)
|
||||
---@field get_string fun(self, key:ProcessingContainerStation.MetaData.StringKey):string
|
||||
---@field set_string fun(self, key:ProcessingContainerStation.MetaData.StringKey, value:string)
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 6.2 KiB |
4
mods/CORE/ProcessingFuelStation/init.lua
Normal file
4
mods/CORE/ProcessingFuelStation/init.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
local modInfo = Mod.getInfo('smc__core__processing_fuel_station')
|
||||
local require = modInfo.require
|
||||
|
||||
return require('ProcessingFuelStation')
|
||||
2
mods/CORE/ProcessingFuelStation/mod.conf
Normal file
2
mods/CORE/ProcessingFuelStation/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
||||
name = smc__core__processing_fuel_station
|
||||
depends = smc__core__base_processing_station
|
||||
269
mods/CORE/ProcessingFuelStation/src/ProcessingFuelStation.lua
Normal file
269
mods/CORE/ProcessingFuelStation/src/ProcessingFuelStation.lua
Normal file
@@ -0,0 +1,269 @@
|
||||
local FUEL_ENERGY_FACTOR = 0.1
|
||||
|
||||
local BaseProcessingStation = Smc.requireBaseProcessingStation()
|
||||
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
local Node = Smc.requireNode()
|
||||
|
||||
local ProcessingFuelStationNode = Node:getExtended({
|
||||
settings = {
|
||||
name = '',
|
||||
drawtype = 'normal',
|
||||
paramtype = 'light',
|
||||
groups = { processing_fuel_station = 1, dig_immediate = 2 }
|
||||
},
|
||||
})
|
||||
|
||||
---@class ProcessingFuelStation : BaseProcessingStation
|
||||
---@field enableIdling 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?
|
||||
|
||||
---@param name string
|
||||
---@param def ProcessingFuelStationDef
|
||||
---@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 settings = nodesDef.settings
|
||||
local activeSettings = nodesDef.activeSettings
|
||||
local callbacks = nodesDef.callbacks or {}
|
||||
|
||||
local oldCallback = callbacks.on_construct or function(...) end
|
||||
-- When we install a station in the world,
|
||||
-- we initialize the metadata, set the inventory size, and start the timer.
|
||||
callbacks.on_construct = function(pos)
|
||||
oldCallback(pos)
|
||||
|
||||
instance:init(pos)
|
||||
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.
|
||||
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||
oldCallback(pos, node, clicker, itemstack, pointed_thing)
|
||||
|
||||
instance:showForm(pos, clicker)
|
||||
end
|
||||
|
||||
oldCallback = callbacks.on_metadata_inventory_move or function(...) end
|
||||
-- Whenever we change items, we start the station timer,
|
||||
-- because it can be stopped (if the crafting or fuel runs out).
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
end
|
||||
|
||||
oldCallback = callbacks.on_timer or function(...) end
|
||||
-- The basic logic of the station's work.
|
||||
callbacks.on_timer = function(pos, elapsed, node, timeout)
|
||||
oldCallback(pos, elapsed, node, timeout)
|
||||
|
||||
return instance:onTimer(pos, elapsed, node, timeout)
|
||||
end
|
||||
|
||||
-- Registering a nodes for Luanti.
|
||||
local node = ProcessingFuelStationNode:new({ settings = settings, callbacks = callbacks })
|
||||
instance.nodeName = node.itemDef.settings.name
|
||||
|
||||
if activeSettings ~= nil then
|
||||
local activeNode = ProcessingFuelStationNode:new({ settings = activeSettings, callbacks = callbacks })
|
||||
instance.activeNodeName = activeNode.itemDef.settings.name
|
||||
end
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
function ProcessingFuelStation:init(pos)
|
||||
---@type ProcessingFuelStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
meta:set_float('remaining_fuel', 0.0)
|
||||
|
||||
local inventory = meta:get_inventory()
|
||||
inventory:set_size('fuel', self.fuelSlots)
|
||||
|
||||
self:startTimer(pos)
|
||||
end
|
||||
|
||||
---@param remainingFuel number
|
||||
---@param inventory InvRef
|
||||
---@param timeout number
|
||||
---@return number remainingFuel
|
||||
function ProcessingFuelStation:takeFuel(remainingFuel, inventory, timeout)
|
||||
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 = core.get_item_group(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 < timeout then
|
||||
itemCount = math.min(math.ceil(timeout/fuelEnergy), itemCount)
|
||||
else
|
||||
itemCount = 1
|
||||
end
|
||||
|
||||
remainingFuel = remainingFuel + (fuelEnergy * itemCount)
|
||||
|
||||
fuelStack:set_count(itemCount)
|
||||
inventory:remove_item('fuel', fuelStack)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return remainingFuel
|
||||
end
|
||||
|
||||
---@param isProcessing boolean
|
||||
---@param meta ProcessingFuelStation.MetaData
|
||||
---@param timeout number
|
||||
---@return boolean isProcessing
|
||||
function ProcessingFuelStation:burnFuel(isProcessing, meta, timeout)
|
||||
local inventory = meta:get_inventory()
|
||||
|
||||
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
|
||||
elseif self.enableIdling then
|
||||
remainingFuel = remainingFuel - timeout
|
||||
end
|
||||
|
||||
if remainingFuel < 0 then
|
||||
remainingFuel = 0
|
||||
isProcessing = false
|
||||
end
|
||||
|
||||
meta:set_float('remaining_fuel', remainingFuel)
|
||||
|
||||
return isProcessing or (self.enableIdling == true and remainingFuel ~= 0)
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
---@param elapsed number
|
||||
---@param node NodeTable
|
||||
---@param timeout number
|
||||
---@return boolean?
|
||||
function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local isProcessing = self:burnFuel(true, meta, timeout)
|
||||
|
||||
self:swapNode(isProcessing, pos)
|
||||
|
||||
PlayerInventoryUI:updateNodeForm(pos)
|
||||
|
||||
return isProcessing
|
||||
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
|
||||
---@param tabType PlayerInventoryUI.TabType
|
||||
---@return Formspec
|
||||
local tab = function(self, playerName, tabType)
|
||||
local form = Formspec:new()
|
||||
local pos = self:getContex(playerName, 'node_tab_pos')
|
||||
local inventoryLocation = ('nodemeta:%s,%s,%s'):format(pos.x, pos.y, pos.z)
|
||||
|
||||
---@type ProcessingFuelStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
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')))
|
||||
|
||||
return form
|
||||
end
|
||||
|
||||
ProcessingFuelStation:new('test_processing_node', {
|
||||
craftType = 'test',
|
||||
tab = tab,
|
||||
inputSlots = 2,
|
||||
fuelGroup = 'wood',
|
||||
enableIdling = true,
|
||||
enableRegression = false,
|
||||
},
|
||||
{
|
||||
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
|
||||
@@ -0,0 +1,6 @@
|
||||
---@alias ProcessingFuelStation.MetaData.FloatKey 'remaining_fuel'
|
||||
|
||||
---**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)
|
||||
@@ -1,2 +1,2 @@
|
||||
name = smc__core__processing_station
|
||||
depends = smc__core__craft_system, smc__core__node
|
||||
depends = smc__core__processing_container_station, smc__core__processing_fuel_station
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
local FUEL_ENERGY_FACTOR = 0.1
|
||||
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
local BaseProcessingStation = Smc.requireBaseProcessingStation()
|
||||
local ProcessingContainerStation, ProcessingFuelStation = Smc.requireProcessingContainerStation(), Smc.requireProcessingFuelStation()
|
||||
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
@@ -17,57 +14,25 @@ local ProcessingStationNode = Node:getExtended({
|
||||
},
|
||||
})
|
||||
|
||||
---@class ProcessingStation
|
||||
---@field name string
|
||||
---@field timeout number
|
||||
---@field enableRegression boolean?
|
||||
---@field enableIdling boolean?
|
||||
---@field craftType CraftSystem.ProcessingCraftType
|
||||
---@field fuelGroup string
|
||||
---@field inputSlots integer
|
||||
---@field outputSlots integer
|
||||
---@field fuelSlots integer
|
||||
---@field handler CraftSystem.ProcessingCraftHandler
|
||||
---@field tab PlayerInventoryUI.getTabFunc
|
||||
---@field nodeName string
|
||||
---@field activeNodeName string?
|
||||
local ProcessingStation = {}
|
||||
---@class ProcessingStation : BaseProcessingStation
|
||||
---@field containerStation ProcessingContainerStation
|
||||
---@field fuelStation ProcessingFuelStation
|
||||
local ProcessingStation = BaseProcessingStation:extend({})
|
||||
|
||||
---Definition table for the `ProcessingStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class ProcessingStationDef
|
||||
---@field timeout number?
|
||||
---@field enableRegression boolean?
|
||||
---@field enableIdling boolean?
|
||||
---@field craftType CraftSystem.ProcessingCraftType
|
||||
---@field fuelGroup string?
|
||||
---@field inputSlots integer?
|
||||
---@field outputSlots integer?
|
||||
---@field fuelSlots integer?
|
||||
---@field tab PlayerInventoryUI.getTabFunc
|
||||
|
||||
---@alias ProcessingStation.NodesDef {settings:Node.Settings, activeSettings?:Node.Settings, callbacks:Node.Callbacks?}
|
||||
---@class ProcessingStationDef : ProcessingContainerStationDef, ProcessingFuelStationDef
|
||||
|
||||
---@param name string
|
||||
---@param def ProcessingStationDef
|
||||
---@param nodesDef ProcessingStation.NodesDef
|
||||
---@param nodesDef BaseProcessingStation.NodesDef
|
||||
function ProcessingStation:new(name, def, nodesDef)
|
||||
local handler = CraftSystem:getProcessingCraftHandler(def.craftType)
|
||||
|
||||
---@type ProcessingStation
|
||||
local instance = getInstance(self, {})
|
||||
instance.name = name
|
||||
instance.timeout = def.timeout or 1
|
||||
instance.enableRegression = def.enableRegression
|
||||
instance.enableIdling = def.enableIdling
|
||||
instance.craftType = def.craftType
|
||||
instance.fuelGroup = def.fuelGroup or 'fuel'
|
||||
instance.inputSlots = def.inputSlots or 1
|
||||
instance.outputSlots = def.outputSlots or 1
|
||||
instance.fuelSlots = def.outputSlots or 1
|
||||
instance.handler = handler
|
||||
instance.tab = def.tab
|
||||
local instance = self:getInstance(BaseProcessingStation:new(name, def, nodesDef))
|
||||
instance.containerStation = ProcessingContainerStation:new(name, def, nodesDef)
|
||||
instance.fuelStation = ProcessingFuelStation:new(name, def, nodesDef)
|
||||
|
||||
local settings = nodesDef.settings
|
||||
local activeSettings = nodesDef.activeSettings
|
||||
@@ -79,7 +44,8 @@ function ProcessingStation:new(name, def, nodesDef)
|
||||
callbacks.on_construct = function(pos)
|
||||
oldCallback(pos)
|
||||
|
||||
instance:init(pos)
|
||||
instance.containerStation:init(pos)
|
||||
instance.fuelStation:init(pos)
|
||||
end
|
||||
|
||||
oldCallback = callbacks.on_rightclick or function(...) end
|
||||
@@ -134,183 +100,26 @@ function ProcessingStation:new(name, def, nodesDef)
|
||||
return instance
|
||||
end
|
||||
|
||||
---Opens the player's inventory, adding the tab described in `self.tab`.
|
||||
---@param pos MapPosition
|
||||
---@param player Player
|
||||
function ProcessingStation:showForm(pos, player)
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
PlayerInventoryUI:addActiveNodeForm(pos, playerName)
|
||||
PlayerInventoryUI:setContex(playerName, 'left_tab_name', 'node_tab')
|
||||
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', pos)
|
||||
PlayerInventoryUI:setContex(playerName, 'node_tab', self.tab)
|
||||
|
||||
PlayerInventoryUI:showForm(playerName)
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
function ProcessingStation:init(pos)
|
||||
---@type ProcessingStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
meta:set_float('remaining_fuel', 0.0)
|
||||
meta:set_float('processing_time', 0.0)
|
||||
|
||||
local inventory = meta:get_inventory()
|
||||
inventory:set_size('input', self.inputSlots)
|
||||
inventory:set_size('output', self.outputSlots)
|
||||
inventory:set_size('fuel', self.fuelSlots)
|
||||
|
||||
self:startTimer(pos)
|
||||
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 MapPosition
|
||||
---@return boolean
|
||||
function ProcessingStation:startTimer(pos)
|
||||
local timer = core.get_node_timer(pos)
|
||||
if not timer:is_started() then
|
||||
timer:start(self.timeout)
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
---@param elapsed number
|
||||
---@param node NodeTable
|
||||
---@param timeout number
|
||||
---@return boolean
|
||||
---@return boolean?
|
||||
function ProcessingStation:onTimer(pos, elapsed, node, timeout)
|
||||
---An indicator of whether the station
|
||||
---is processing resources in this iteration.
|
||||
local isProcessing = true
|
||||
|
||||
---@type ProcessingStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
local inventory = meta:get_inventory()
|
||||
|
||||
---An indicator of how many units of fuel are currently remaining.
|
||||
local remainingFuel = meta:get_float('remaining_fuel')
|
||||
---An indicator of how long the station has been processing the materials.
|
||||
local processingTime = meta:get_float('processing_time')
|
||||
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 craft = self.handler:getCraftByRecipe(inventory:get_list('input'))
|
||||
|
||||
-- If the materials in the input slot are not a recipe for any crafting,
|
||||
-- then we will not process them.
|
||||
if craft == nil then
|
||||
isProcessing = false
|
||||
end
|
||||
|
||||
-- If there is no space in the output slots for the crafting result, then we stop processing.
|
||||
if isProcessing and craft ~= nil and not inventory:room_for_item('output', craft.output) then
|
||||
isProcessing = false
|
||||
end
|
||||
|
||||
-- If we are in the process of processing and we run out of fuel,
|
||||
-- but there are still items in the fuel slot...
|
||||
if isProcessing and remainingFuel <= 0 and not inventory:is_empty('fuel') then
|
||||
-- ... then we will convert the first itemstack into station fuel
|
||||
-- if it has the required fuel group.
|
||||
local fuelList = inventory:get_list('fuel')
|
||||
for index, fuelStack in ipairs(fuelList) do
|
||||
if not fuelStack:is_empty() then
|
||||
---Fuel units contained in the itemstack.
|
||||
---
|
||||
-- Note: since item groups can only contain integers,
|
||||
-- we divide the fuel units by a constant to convert seconds to smaller units.
|
||||
local fuelEnergy = core.get_item_group(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 < timeout then
|
||||
itemCount = math.min(math.ceil(timeout/fuelEnergy), itemCount)
|
||||
else
|
||||
itemCount = 1
|
||||
end
|
||||
|
||||
remainingFuel = remainingFuel + (fuelEnergy * itemCount)
|
||||
|
||||
fuelStack:set_count(itemCount)
|
||||
inventory:remove_item('fuel', fuelStack)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if self.enableIdling and isProcessing then
|
||||
remainingFuel = remainingFuel - timeout
|
||||
else
|
||||
remainingFuel = remainingFuel - timeout
|
||||
end
|
||||
|
||||
-- If we don't have fuel remaining,
|
||||
-- we don't process the materials.
|
||||
if remainingFuel < 0 then
|
||||
remainingFuel = 0
|
||||
isProcessing = false
|
||||
end
|
||||
|
||||
-- If we process materials...
|
||||
if isProcessing == true then
|
||||
-- ...then we add the processing progress for this iteration.
|
||||
processingTime = processingTime + timeout
|
||||
else
|
||||
-- ...else we take away progress
|
||||
-- (if there was progress, but the fuel ran out,
|
||||
-- the crafting progress will gradually decrease).
|
||||
if self.enableRegression then
|
||||
processingTime = processingTime - timeout
|
||||
end
|
||||
end
|
||||
|
||||
-- If the station is working and the progress time is longer than required for crafting, we produce the craft
|
||||
-- (we take the item stacks needed for crafting and put the result of the crafting into the output)
|
||||
if isProcessing and craft ~= nil and processingTime >= craft.time then
|
||||
for _, item in ipairs(craft.recipe) do
|
||||
inventory:remove_item('input', item)
|
||||
end
|
||||
|
||||
inventory:add_item('output', craft.output)
|
||||
processingTime = 0
|
||||
end
|
||||
|
||||
if processingTime < 0 then
|
||||
processingTime = 0
|
||||
end
|
||||
|
||||
-- If the processing is in progress and the station has an active node type, then we use it.
|
||||
if self.activeNodeName ~= nil then
|
||||
if isProcessing then
|
||||
core.swap_node(pos, {name = self.activeNodeName})
|
||||
else
|
||||
core.swap_node(pos, {name = self.nodeName})
|
||||
end
|
||||
end
|
||||
|
||||
meta:set_float('processing_time', processingTime)
|
||||
meta:set_float('remaining_fuel', remainingFuel)
|
||||
self:swapNode(isProcessing, pos)
|
||||
|
||||
PlayerInventoryUI:updateNodeForm(pos)
|
||||
|
||||
-- If we not processing we stop the timer so as not to overload the server.
|
||||
--
|
||||
-- The timer will have to start again
|
||||
-- if the player interacts with any items in the station's inventory.
|
||||
if not isProcessing and (not self.enableRegression or processingTime == 0) and (not self.enableIdling or remainingFuel == 0) then
|
||||
core.log('timer stop')
|
||||
return false
|
||||
else
|
||||
core.log('timer work')
|
||||
return true
|
||||
end
|
||||
return isProcessing
|
||||
end
|
||||
|
||||
--- DEBUG ---
|
||||
@@ -318,6 +127,7 @@ end
|
||||
local Formspec = Smc.requireFormspec()
|
||||
local TileSheet = Smc.requireTileSheet()
|
||||
local tileSheet = TileSheet:new('debug_processing_station_sheet.png', 16, 16)
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
local handler = CraftSystem:getProcessingCraftHandler('test')
|
||||
|
||||
handler:registerCraft('test_pro_craft', {
|
||||
@@ -355,7 +165,7 @@ ProcessingStation:new('test_processing_node', {
|
||||
inputSlots = 2,
|
||||
fuelGroup = 'wood',
|
||||
enableIdling = true,
|
||||
enableRegression = false,
|
||||
enableRegression = true,
|
||||
},
|
||||
{
|
||||
settings = {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
---@alias ProcessingStation.MetaData.FloatKey 'remaining_fuel'|'processing_time'
|
||||
---@alias ProcessingStation.MetaData.FloatKey ProcessingContainerStation.MetaData.FloatKey|ProcessingFuelStation.MetaData.FloatKey
|
||||
---@alias ProcessingStation.MetaData.StringKey ProcessingContainerStation.MetaData.StringKey
|
||||
|
||||
|
||||
---**Only for EmmyLua.**
|
||||
---@class ProcessingStation.MetaData : NodeMetaRef
|
||||
---@field get_float fun(self, key:ProcessingStation.MetaData.FloatKey):number
|
||||
---@field set_float fun(self, key:ProcessingStation.MetaData.FloatKey, value:number)
|
||||
---@field get_float fun(self, key:ProcessingStation.MetaData.FloatKey):number
|
||||
---@field set_float fun(self, key:ProcessingStation.MetaData.FloatKey, value:number)
|
||||
---@field get_string fun(self, key:ProcessingStation.MetaData.StringKey):string
|
||||
---@field set_string fun(self, key:ProcessingStation.MetaData.StringKey, value:string)
|
||||
|
||||
@@ -79,3 +79,27 @@ function Smc.requirePlayerInventoryUI()
|
||||
|
||||
return class
|
||||
end
|
||||
|
||||
---@return BaseProcessingStation
|
||||
function Smc.requireBaseProcessingStation()
|
||||
---@type BaseProcessingStation
|
||||
local class = Mod.getInfo('smc__core__base_processing_station').require('BaseProcessingStation')
|
||||
|
||||
return class
|
||||
end
|
||||
|
||||
---@return ProcessingContainerStation
|
||||
function Smc.requireProcessingContainerStation()
|
||||
---@type ProcessingContainerStation
|
||||
local class = Mod.getInfo('smc__core__processing_container_station').require('ProcessingContainerStation')
|
||||
|
||||
return class
|
||||
end
|
||||
|
||||
---@return ProcessingFuelStation
|
||||
function Smc.requireProcessingFuelStation()
|
||||
---@type ProcessingFuelStation
|
||||
local class = Mod.getInfo('smc__core__processing_fuel_station').require('ProcessingFuelStation')
|
||||
|
||||
return class
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user