343 lines
10 KiB
Lua
343 lines
10 KiB
Lua
local v = vector.new
|
|
local assert, ipairs = assert, ipairs
|
|
local coreGetNode, coreGetItemGroup = core.get_node, core.get_item_group
|
|
|
|
local BaseProcessingStation = Smc.requireBaseProcessingStation()
|
|
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
|
local Node = Smc.requireNode()
|
|
|
|
local ProcessingContainerStationNode = Node:getExtended({
|
|
settings = {
|
|
name = '',
|
|
drawtype = 'normal',
|
|
paramtype = 'light',
|
|
groups = { processing_container_station = 1, dig_immediate = 2 }
|
|
},
|
|
})
|
|
|
|
---@class ProcessingContainerStation : BaseProcessingStation
|
|
---@field enableFuelPush boolean?
|
|
---@field enableRegression boolean?
|
|
---
|
|
---@field fuelGroup string
|
|
---@field inputSlots integer
|
|
---@field outputSlots integer
|
|
---
|
|
---@field getMeta fun(self:ProcessingContainerStation, pos:MapVector):ProcessingContainerStation.MetaData
|
|
local ProcessingContainerStation = BaseProcessingStation:extend({})
|
|
|
|
---Definition table for the `ProcessingContainerStation`.
|
|
---
|
|
---**Only for EmmyLua.**
|
|
---@class ProcessingContainerStationDef : BaseProcessingStationDef
|
|
---If `true`, the `ContainerStation` will
|
|
---attempt to push `FuelStation` when it is ready for crafting.
|
|
---@field enableFuelPush boolean?
|
|
---
|
|
---If `true`, then crafting progress will gradually
|
|
---decrease when there are no conditions for crafting.
|
|
---@field enableRegression boolean?
|
|
---
|
|
---@field fuelGroup string?
|
|
---@field inputSlots integer?
|
|
---@field outputSlots integer?
|
|
|
|
---@param def ProcessingContainerStationDef
|
|
---@param nodesDef BaseProcessingStation.NodesDefinition?
|
|
function ProcessingContainerStation:new(def, nodesDef)
|
|
---@type ProcessingContainerStation
|
|
local instance = self:getInstance(BaseProcessingStation:new(def))
|
|
instance.enableFuelPush = def.enableFuelPush
|
|
instance.enableRegression = def.enableRegression
|
|
instance.fuelGroup = def.fuelGroup or 'fuel'
|
|
instance.inputSlots = def.inputSlots or 1
|
|
instance.outputSlots = def.outputSlots or 1
|
|
|
|
if nodesDef == nil then
|
|
return instance
|
|
end
|
|
|
|
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 --[[@as Player]])
|
|
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 = ProcessingContainerStationNode:new({ settings = settings, callbacks = callbacks })
|
|
instance.nodeName = node.itemDef.settings.name
|
|
|
|
if activeSettings ~= nil then
|
|
local activeNode = ProcessingContainerStationNode:new({ settings = activeSettings, callbacks = callbacks })
|
|
instance.activeNodeName = activeNode.itemDef.settings.name
|
|
|
|
Smc.addNodeSuperclass(instance.activeNodeName, instance)
|
|
end
|
|
|
|
Smc.addNodeSuperclass(instance.nodeName, instance)
|
|
|
|
return instance
|
|
end
|
|
|
|
---@param pos MapVector
|
|
---@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 MapVector
|
|
function ProcessingContainerStation:init(pos)
|
|
local meta = self:getMeta(pos)
|
|
meta:set_float('processing_time', 0.0)
|
|
meta:set_string('fuel_group', self.fuelGroup)
|
|
meta:set_float('remaining_fuel', 0.0)
|
|
|
|
local inventory = meta:get_inventory()
|
|
inventory:set_size('input', self.inputSlots)
|
|
inventory:set_size('output', self.outputSlots)
|
|
|
|
self:startTimer(pos)
|
|
end
|
|
|
|
---Returns the current craft that
|
|
---the station is currently engaged in.
|
|
---
|
|
---Also resets crafting progress if there is no craft,
|
|
---the craft has changed, or there is no room in the result slots.
|
|
---@param isProcessing boolean
|
|
---@param meta ProcessingContainerStation.MetaData
|
|
---@return boolean isProcessing
|
|
---@return CraftSystem.ProcessingCraft?
|
|
function ProcessingContainerStation:getCraft(isProcessing, meta)
|
|
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
|
|
processingTime = 0
|
|
lastCraftName = ''
|
|
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
|
|
processingTime = 0
|
|
lastCraftName = ''
|
|
end
|
|
|
|
meta:set_float('processing_time', processingTime)
|
|
meta:set_string('craft_name', lastCraftName)
|
|
|
|
return isProcessing, craft
|
|
end
|
|
|
|
---Under satisfactory conditions, it increases the crafting progress and
|
|
---when it reaches the required value, it takes the materials and returns the crafting result
|
|
---@param isProcessing boolean
|
|
---@param meta ProcessingContainerStation.MetaData
|
|
---@param elapsed number
|
|
---@param craft CraftSystem.ProcessingCraft?
|
|
---@return boolean isProcessing
|
|
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 + elapsed
|
|
else
|
|
if self.enableRegression then
|
|
processingTime = processingTime - elapsed
|
|
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 = processingTime - craft.time
|
|
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 meta ProcessingContainerStation.MetaData
|
|
---@param pos MapVector
|
|
function ProcessingContainerStation:pushFuelStation( meta, pos)
|
|
if not self.enableFuelPush then
|
|
return
|
|
end
|
|
|
|
local fuelPos = pos - v(0, 1, 0)
|
|
local fuelName = coreGetNode(fuelPos).name
|
|
|
|
if coreGetItemGroup(fuelName, 'processing_fuel_station') == 0 then
|
|
return
|
|
end
|
|
|
|
local fuelStation = Smc.getNodeSuperclassByPos(fuelPos) --[[@as ProcessingFuelStation?]]
|
|
assert(fuelStation ~= nil)
|
|
|
|
if fuelStation.fuelGroup ~= self.fuelGroup then
|
|
return
|
|
end
|
|
|
|
fuelStation:onPush(fuelPos, meta:get_string('craft_name') ~= '')
|
|
end
|
|
|
|
---@param pos MapVector
|
|
---@param elapsed number
|
|
---@param node NodeTable
|
|
---@param timeout number
|
|
---@return boolean?
|
|
function ProcessingContainerStation:onTimer(pos, elapsed, node, timeout)
|
|
local meta = self:getMeta(pos)
|
|
local remainingFuel = meta:get_float('remaining_fuel')
|
|
|
|
local isProcessing, craft = self:getCraft(true, meta)
|
|
|
|
if remainingFuel <= 0 then
|
|
isProcessing = false
|
|
end
|
|
|
|
isProcessing = self:processing(isProcessing, meta, elapsed, craft)
|
|
self:pushFuelStation(meta, pos)
|
|
|
|
self:swapNode(isProcessing, pos)
|
|
|
|
PlayerInventoryUI:updateNodeForm(pos)
|
|
return isProcessing
|
|
end
|
|
|
|
---@param pos MapVector
|
|
---@param remainingFuel number
|
|
function ProcessingContainerStation:onPush(pos, remainingFuel)
|
|
local meta = self:getMeta(pos)
|
|
|
|
meta:set_float('remaining_fuel', remainingFuel)
|
|
self:startTimer(pos)
|
|
end
|
|
|
|
--- DEBUG ---
|
|
|
|
local Formspec = Smc.requireFormspec()
|
|
|
|
---@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)
|
|
|
|
local meta = core.get_meta(pos) --[[@as ProcessingContainerStation.MetaData]]
|
|
local fuelMeta = core.get_meta(pos - vector.new(0, 1, 0)) --[[@as ProcessingFuelStation.MetaData]]
|
|
|
|
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)
|
|
:label(4, 3, tostring(fuelMeta:get_float('remaining_fuel')))
|
|
:label(4.5, 3, tostring(meta:get_float('processing_time')))
|
|
|
|
return form
|
|
end
|
|
|
|
ProcessingContainerStation:new({
|
|
craftType = 'test',
|
|
tab = tab,
|
|
inputSlots = 2,
|
|
fuelGroup = 'wood',
|
|
enableRegression = false,
|
|
enableFuelPush = true
|
|
},
|
|
{
|
|
settings = {
|
|
name = 'test_container_node',
|
|
drawtype = 'mesh',
|
|
mesh = 'test_pot.obj',
|
|
tiles = {'test_pot.png'},
|
|
node_box = {
|
|
|
|
}
|
|
}
|
|
})
|
|
--]]
|
|
return ProcessingContainerStation
|