3 Commits

3 changed files with 226 additions and 189 deletions

View File

@@ -1,4 +1,4 @@
local FUEL_ENERGY_FACTOR = 10 local FUEL_ENERGY_FACTOR = 0.1
local getInstance = Class.getInstance local getInstance = Class.getInstance
@@ -21,6 +21,7 @@ local ProcessingStationNode = Node:getExtended({
---@field name string ---@field name string
---@field timeout number ---@field timeout number
---@field enableRegression boolean? ---@field enableRegression boolean?
---@field enableIdling boolean?
---@field craftType CraftSystem.ProcessingCraftType ---@field craftType CraftSystem.ProcessingCraftType
---@field fuelGroup string ---@field fuelGroup string
---@field inputSlots integer ---@field inputSlots integer
@@ -38,6 +39,7 @@ local ProcessingStation = {}
---@class ProcessingStationDef ---@class ProcessingStationDef
---@field timeout number? ---@field timeout number?
---@field enableRegression boolean? ---@field enableRegression boolean?
---@field enableIdling boolean?
---@field craftType CraftSystem.ProcessingCraftType ---@field craftType CraftSystem.ProcessingCraftType
---@field fuelGroup string? ---@field fuelGroup string?
---@field inputSlots integer? ---@field inputSlots integer?
@@ -45,11 +47,12 @@ local ProcessingStation = {}
---@field fuelSlots integer? ---@field fuelSlots integer?
---@field tab PlayerInventoryUI.getTabFunc ---@field tab PlayerInventoryUI.getTabFunc
---@alias ProcessingStation.NodesDef {settings:Node.Settings, activeSettings?:Node.Settings, callbacks:Node.Callbacks?}
---@param name string ---@param name string
---@param def ProcessingStationDef ---@param def ProcessingStationDef
---@param nodeDef Node.NodeDefinition ---@param nodesDef ProcessingStation.NodesDef
---@param activeNodeDef Node.NodeDefinition? function ProcessingStation:new(name, def, nodesDef)
function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
local handler = CraftSystem:getProcessingCraftHandler(def.craftType) local handler = CraftSystem:getProcessingCraftHandler(def.craftType)
---@type ProcessingStation ---@type ProcessingStation
@@ -57,6 +60,7 @@ function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
instance.name = name instance.name = name
instance.timeout = def.timeout or 1 instance.timeout = def.timeout or 1
instance.enableRegression = def.enableRegression instance.enableRegression = def.enableRegression
instance.enableIdling = def.enableIdling
instance.craftType = def.craftType instance.craftType = def.craftType
instance.fuelGroup = def.fuelGroup or 'fuel' instance.fuelGroup = def.fuelGroup or 'fuel'
instance.inputSlots = def.inputSlots or 1 instance.inputSlots = def.inputSlots or 1
@@ -65,63 +69,122 @@ function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
instance.handler = handler instance.handler = handler
instance.tab = def.tab instance.tab = def.tab
if nodeDef.callbacks == nil then local settings = nodesDef.settings
---@type Node.Callbacks local activeSettings = nodesDef.activeSettings
nodeDef.callbacks = {} local callbacks = nodesDef.callbacks or {}
end
local settings = nodeDef.settings
local callbacks = nodeDef.callbacks
local oldCallback = callbacks.on_construct or function(...) end
-- When we install a station in the world, -- When we install a station in the world,
-- we initialize the metadata, set the inventory size, and start the timer. -- we initialize the metadata, set the inventory size, and start the timer.
callbacks.on_construct = function(pos) 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 = 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 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)
local meta = core.get_meta(pos) local meta = core.get_meta(pos)
meta:set_float('remaining_fuel', 0.0) meta:set_float('remaining_fuel', 0.0)
meta:set_float('processing_time', 0.0) meta:set_float('processing_time', 0.0)
local inventory = meta:get_inventory() local inventory = meta:get_inventory()
inventory:set_size('input', instance.inputSlots) inventory:set_size('input', self.inputSlots)
inventory:set_size('output', instance.outputSlots) inventory:set_size('output', self.outputSlots)
inventory:set_size('fuel', instance.fuelSlots) inventory:set_size('fuel', self.fuelSlots)
core.get_node_timer(pos):start(instance.timeout) self:startTimer(pos)
end end
-- Right-clicking on a station opens the player's inventory, adding context to the station tab. ---Starts the station timer only if it is not running.
-- Note: we do not use formspec via node metadata. ---
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) ---The timer timeout is set to the timeout specified `self.timeout`.
local playerName = clicker:get_player_name() ---
PlayerInventoryUI:setContex(playerName, 'left_tab_name', 'node_tab') ---Returns `true` if the timer has been started.
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', pos) ---@param pos MapPosition
PlayerInventoryUI:setContex(playerName, 'node_tab', instance.tab) ---@return boolean
PlayerInventoryUI:showForm(playerName) function ProcessingStation:startTimer(pos)
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)
local timer = core.get_node_timer(pos) local timer = core.get_node_timer(pos)
if not timer:is_started() then if not timer:is_started() then
timer:start(instance.timeout) timer:start(self.timeout)
end return true
end end
callbacks.on_metadata_inventory_put = function(pos, listname, index, stack, player) return false
local timer = core.get_node_timer(pos)
if not timer:is_started() then
timer:start(instance.timeout)
end
end end
callbacks.on_metadata_inventory_take = function(pos, listname, index, stack, player) ---@param pos MapPosition
local timer = core.get_node_timer(pos) ---@param elapsed number
if not timer:is_started() then ---@param node NodeTable
timer:start(instance.timeout) ---@param timeout number
end ---@return boolean
end function ProcessingStation:onTimer(pos, elapsed, node, timeout)
-- The basic logic of the station's work.
callbacks.on_timer = function(pos, elapsed, node, timeout)
---An indicator of whether the station ---An indicator of whether the station
---is processing resources in this iteration. ---is processing resources in this iteration.
local isProcessing = true local isProcessing = true
@@ -135,7 +198,7 @@ function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
---An indicator of how long the station has been processing the materials. ---An indicator of how long the station has been processing the materials.
local processingTime = meta:get_float('processing_time') local processingTime = meta:get_float('processing_time')
local craft = handler:getCraftByRecipe(inventory:get_list('input')) local craft = self.handler:getCraftByRecipe(inventory:get_list('input'))
-- If the materials in the input slot are not a recipe for any crafting, -- If the materials in the input slot are not a recipe for any crafting,
-- then we will not process them. -- then we will not process them.
@@ -160,7 +223,7 @@ function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
--- ---
-- Note: since item groups can only contain integers, -- Note: since item groups can only contain integers,
-- we divide the fuel units by a constant to convert seconds to smaller units. -- we divide the fuel units by a constant to convert seconds to smaller units.
local fuelEnergy = core.get_item_group(fuelStack:get_name(), instance.fuelGroup or 'fuel') / FUEL_ENERGY_FACTOR local fuelEnergy = core.get_item_group(fuelStack:get_name(), self.fuelGroup or 'fuel') / FUEL_ENERGY_FACTOR
if fuelEnergy > 0 then if fuelEnergy > 0 then
local itemCount = fuelStack:get_count() local itemCount = fuelStack:get_count()
@@ -181,9 +244,9 @@ function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
end end
end end
-- If we have fuel remaining, if self.enableIdling and isProcessing then
-- we subtract units for this iteration. remainingFuel = remainingFuel - timeout
if remainingFuel >= 0 then else
remainingFuel = remainingFuel - timeout remainingFuel = remainingFuel - timeout
end end
@@ -202,7 +265,7 @@ function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
-- ...else we take away progress -- ...else we take away progress
-- (if there was progress, but the fuel ran out, -- (if there was progress, but the fuel ran out,
-- the crafting progress will gradually decrease). -- the crafting progress will gradually decrease).
if instance.enableRegression then if self.enableRegression then
processingTime = processingTime - timeout processingTime = processingTime - timeout
end end
end end
@@ -222,55 +285,33 @@ function ProcessingStation:new(name, def, nodeDef, activeNodeDef)
processingTime = 0 processingTime = 0
end 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('processing_time', processingTime)
meta:set_float('remaining_fuel', remainingFuel) meta:set_float('remaining_fuel', remainingFuel)
PlayerInventoryUI:updateNodeForm(pos) PlayerInventoryUI:updateNodeForm(pos)
-- If the processing is in progress and the station has an active node type, then we use it. -- If we not processing we stop the timer so as not to overload the server.
if instance.activeNodeName ~= nil then
if isProcessing then
core.swap_node(pos, {name = instance.activeNodeName})
else
core.swap_node(pos, {name = instance.nodeName})
end
end
-- If we not processing and processing time is zero
-- we stop the timer so as not to overload the server.
-- --
-- The timer will have to start again -- The timer will have to start again
-- if the player interacts with any items in the station's inventory. -- if the player interacts with any items in the station's inventory.
if not isProcessing and processingTime == 0 then if not isProcessing and (not self.enableRegression or processingTime == 0) and (not self.enableIdling or remainingFuel == 0) then
core.log('timer stop')
return false return false
else else
core.log('timer work')
return true return true
end end
end end
-- Registering a nodes for Luanti.
local node = ProcessingStationNode:new(nodeDef)
instance.nodeName = node.itemDef.settings.name
if activeNodeDef ~= nil then
if activeNodeDef.callbacks == nil then
activeNodeDef.callbacks = {}
end
local activeNodeCallbacks = activeNodeDef.callbacks
activeNodeCallbacks.on_construct = callbacks.on_construct
activeNodeCallbacks.on_rightclick = callbacks.on_rightclick
activeNodeCallbacks.on_metadata_inventory_move = callbacks.on_metadata_inventory_move
activeNodeCallbacks.on_metadata_inventory_put = callbacks.on_metadata_inventory_put
activeNodeCallbacks.on_metadata_inventory_take = callbacks.on_metadata_inventory_take
activeNodeCallbacks.on_timer = callbacks.on_timer
local activeNode = ProcessingStationNode:new(activeNodeDef)
instance.activeNodeName = activeNode.itemDef.settings.name
end
return instance
end
--- DEBUG --- --- DEBUG ---
local Formspec = Smc.requireFormspec() local Formspec = Smc.requireFormspec()
@@ -311,7 +352,9 @@ ProcessingStation:new('test_processing_node', {
craftType = 'test', craftType = 'test',
tab = tab, tab = tab,
inputSlots = 2, inputSlots = 2,
fuelGroup = 'wood' fuelGroup = 'wood',
enableIdling = true,
enableRegression = false,
}, },
{ {
settings = { settings = {
@@ -324,10 +367,8 @@ ProcessingStation:new('test_processing_node', {
tileSheet:t(0, 2), tileSheet:t(0, 2),
tileSheet:t(0, 2) tileSheet:t(0, 2)
} }
}
}, },
{ activeSettings = {
settings = {
name = 'test_processing_node_active', name = 'test_processing_node_active',
tiles = { tiles = {
tileSheet:t(0, 0), tileSheet:t(0, 0),

View File

@@ -417,7 +417,8 @@ end
function table.remove_value(list, value) function table.remove_value(list, value)
local index = table.indexof(list, value) local index = table.indexof(list, value)
while index ~= -1 do while index ~= -1 do
list = table.remove(list, index) table.remove(list, index)
index = table.indexof(list, value)
end end
return list return list

View File

@@ -265,7 +265,7 @@ function PlayerInventoryUI:addActiveNodeForm(nodePos, playerName)
local stringPos = core.pos_to_string(nodePos) local stringPos = core.pos_to_string(nodePos)
local playerNames = self.ActiveNodeForms[stringPos] local playerNames = self.ActiveNodeForms[stringPos]
if playerNames == nil then if playerNames == nil then
self.ActiveNodeForms[stringPos] = { playerNames } self.ActiveNodeForms[stringPos] = { playerName }
else else
tableInsert(playerNames, playerName) tableInsert(playerNames, playerName)
end end
@@ -295,7 +295,7 @@ function PlayerInventoryUI:removeActiveNodeForm(nodePos, playerName)
return return
end end
tableRemoveValue(playerNames, playerName) self.ActiveNodeForms[stringPos] = tableRemoveValue(playerNames, playerName)
end end
-- We are overriding the `BaseUI.showForm` & `BaseUI.closeForm`. -- We are overriding the `BaseUI.showForm` & `BaseUI.closeForm`.
@@ -306,11 +306,6 @@ function PlayerInventoryUI:showForm(playerName)
local player = core.get_player_by_name(playerName) local player = core.get_player_by_name(playerName)
assert(player ~= nil) assert(player ~= nil)
local nodePos = self:getContex(playerName, 'node_tab_pos')
if nodePos ~= nil then
self:addActiveNodeForm(nodePos, playerName)
end
-- We overwrite the player's inventory definition so that the updated inventory -- We overwrite the player's inventory definition so that the updated inventory
-- is shown to the player when they press the "open inventory" key. -- is shown to the player when they press the "open inventory" key.
player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName)) player:set_inventory_formspec(PlayerInventoryUI:getFormSpecString(playerName))
@@ -334,7 +329,7 @@ local function closeNodeTab(player)
local playerName = player:get_player_name() local playerName = player:get_player_name()
local nodePos = PlayerInventoryUI:getContex(playerName, 'node_tab_pos') local nodePos = PlayerInventoryUI:getContex(playerName, 'node_tab_pos')
if nodePos ~= nil then if nodePos ~= nil then
PlayerInventoryUI:addActiveNodeForm(nodePos, playerName) PlayerInventoryUI:removeActiveNodeForm(nodePos, playerName)
end end
PlayerInventoryUI:setContex(playerName, 'node_tab_pos', nil) PlayerInventoryUI:setContex(playerName, 'node_tab_pos', nil)