ProcessingContainerStation: create class
This commit is contained in:
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 |
Reference in New Issue
Block a user