From 78c1556f26617d7b87f84780d31b329105968cb4 Mon Sep 17 00:00:00 2001 From: Vanic Date: Fri, 22 May 2026 22:20:31 +0300 Subject: [PATCH] `ProcessingContainerStation`: create class --- mods/CORE/ProcessingContainerStation/init.lua | 4 + mods/CORE/ProcessingContainerStation/mod.conf | 2 + .../src/ProcessingContainerStation.lua | 290 ++++++++++++++++++ .../ProcessingContainerStation/MetaData.lua | 9 + .../sheets/debug_processing_station_sheet.png | Bin 0 -> 6373 bytes 5 files changed, 305 insertions(+) create mode 100644 mods/CORE/ProcessingContainerStation/init.lua create mode 100644 mods/CORE/ProcessingContainerStation/mod.conf create mode 100644 mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation.lua create mode 100644 mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation/MetaData.lua create mode 100644 mods/CORE/ProcessingContainerStation/textures/sheets/debug_processing_station_sheet.png diff --git a/mods/CORE/ProcessingContainerStation/init.lua b/mods/CORE/ProcessingContainerStation/init.lua new file mode 100644 index 0000000..5b206c6 --- /dev/null +++ b/mods/CORE/ProcessingContainerStation/init.lua @@ -0,0 +1,4 @@ +local modInfo = Mod.getInfo('smc__core__processing_container_station') +local require = modInfo.require + +return require('ProcessingContainerStation') diff --git a/mods/CORE/ProcessingContainerStation/mod.conf b/mods/CORE/ProcessingContainerStation/mod.conf new file mode 100644 index 0000000..3d756a7 --- /dev/null +++ b/mods/CORE/ProcessingContainerStation/mod.conf @@ -0,0 +1,2 @@ +name = smc__core__processing_container_station +depends = smc__core__base_processing_station diff --git a/mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation.lua b/mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation.lua new file mode 100644 index 0000000..9e05625 --- /dev/null +++ b/mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation.lua @@ -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 diff --git a/mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation/MetaData.lua b/mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation/MetaData.lua new file mode 100644 index 0000000..fb35d0f --- /dev/null +++ b/mods/CORE/ProcessingContainerStation/src/ProcessingContainerStation/MetaData.lua @@ -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) diff --git a/mods/CORE/ProcessingContainerStation/textures/sheets/debug_processing_station_sheet.png b/mods/CORE/ProcessingContainerStation/textures/sheets/debug_processing_station_sheet.png new file mode 100644 index 0000000000000000000000000000000000000000..38cd652a1e559006ccfd1bfef9274c977f7c4575 GIT binary patch literal 6373 zcmeI0&ud*(5Xa98FMUa#)%G=DNNQUwO({wZMf@$AlG0?6)&@ac2o)DD4b&e+7Z!2j z%B>5#bSFZ0qW%M}x=>InZlq~Tsj)(1noz0tbMij7C-)|4-hJrGf&1p1GxMD}XJ*ci zTirskB4yX zljg10db9qX*C%JDLwIjAgfkz7u$&E@xe~(LV-QDv7z&qYcHKRemheu z(O(NU!a(z$I8GZX_OD(4Gwd1}iL&C)X7P0t7Zz_u{NmD5fTYMcOIyDlEK|0ehD~1MJty~uS?Fd!D z_e6`?4`8AI;g}DHM8W9n9vKeJW;0xypI;RnFjetAX%1Z}PI1mkaqi0GOv31Ni_dGo zmBU$~eVtPAkafWXmzJ0E8c-kJ2|m_VS#F$z;U|Q3DWT zRq=I-OmJ*xRd47ShnN_j1xY%jZt*>aQ|CG{>_$bo6H?wSK1Kjz(O^5Yu^0R0l$@tBaOhOU*9BYcI&mWO zAq!CF$oZ%ToL9xy4nA~X%_rH4+kgMJeqeMo&lP$4@xq00@zSL=J?|VIj#sooqQRTc zTvx%b9K~5)A;V0&(bo^lY3b%-Y?}f)?jCV+WShmuM=3_hr^Z=nnYPCv^>ohayTRv| z5!NvdSx{a|sRN4+J@>nlse-@rCd{^y5x_AYhH(fXqff=-DBYUKkyXK$-UArgs0eZH z1%zYBvw(J|_#O(!l^J>rtw-@50FxB%T6pT{hw;Gx|LNT)BFAPCUpK@Ung3%xKl{@v zb_baAxht3R`K^V=>9?=$iaNV*+=~3G{mWte`;{Xn#n;WHXZ3G$bGdOFOomBt@ud4+ zIXuOmo7oXXy^rh-XTz@n4z(`+Uc%QcHWc_ffG9|=+-M-P(k(c2i5_$Zzau)`w*T31 zJsX$LpZ%qTuWJw^h7)5tFSgwcMlP>~1kZ|b3dv(9`zhk{yTJUacsI^F0gUg3s`ye3 zq>eq~88gSl`&@nH_Tihs|NkDyYak8Xf7vd*2a3+kqt%|usp#}v*r*QH!8aQU4rHy$8GzS-A#TY3iJDogjml1UjAKOYZ?zjf0e2p^uS`-UFTD^FK~8?(8*U zE`4w?kvN(5AF*-eybaoEse)g14wzF{znODh9WYh!Yu*L!eT=%d9iwj>xuhQbRPMNA zMSST!fB~zXw%d?4WFH;L8sXf4I>DFT18y-LxD(_!z@fXq2{A^^bc)YwpqM`#LFTw9 iXPmM1