Files
simple-makes-complex/mods/CORE/ProcessingStation/src/ProcessingStation.lua

223 lines
6.9 KiB
Lua

local coreNodePunch = core.node_punch
local BaseProcessingStation = Smc.requireBaseProcessingStation()
local ProcessingContainerStation, ProcessingFuelStation = Smc.requireProcessingContainerStation(), Smc.requireProcessingFuelStation()
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 ProcessingStation : BaseProcessingStation
---@field containerStation ProcessingContainerStation
---@field fuelStation ProcessingFuelStation
---
---@field getMeta fun(self:ProcessingStation, pos:MapVector):ProcessingStation.MetaData
local ProcessingStation = BaseProcessingStation:extend({})
---Definition table for the `ProcessingStation`.
---
---**Only for EmmyLua.**
---@class ProcessingStationDef : ProcessingContainerStationDef, ProcessingFuelStationDef
---@param def ProcessingStationDef
---@param nodesDef BaseProcessingStation.NodesDefinition
function ProcessingStation:new(def, nodesDef)
---@type ProcessingStation
local instance = self:getInstance(BaseProcessingStation:new(def))
instance.containerStation = ProcessingContainerStation:new(def)
instance.fuelStation = ProcessingFuelStation:new(def)
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.containerStation:init(pos)
instance.fuelStation:init(pos)
end
oldCallback = callbacks.on_punch or function(...) end
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
oldCallback(pos, node, puncher, pointed_thing)
instance.fuelStation:onPunch(pos, node, puncher, pointed_thing)
coreNodePunch(pos, node, puncher, pointed_thing)
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.fuelStation: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.fuelStation: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.fuelStation: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
Smc.addNodeSuperclass(instance.activeNodeName, instance)
end
Smc.addNodeSuperclass(instance.nodeName, instance)
return instance
end
---@param pos MapVector
---@param elapsed number
---@param node NodeTable
---@param timeout number
---@return boolean?
function ProcessingStation:onTimer(pos, elapsed, node, timeout)
local meta = self:getMeta(pos)
local isProcessing, craft = self.containerStation:getCraft(true, meta)
if self.containerStation.enableFuelPush then
self.fuelStation:setMetaIsEnable(meta, meta:get_string('craft_name') ~= '')
end
isProcessing = self.fuelStation:burnFuel(isProcessing, meta, elapsed)
isProcessing = self.fuelStation:takeFuel(isProcessing, meta, elapsed)
isProcessing = self.containerStation:processing(isProcessing, meta, elapsed, craft)
if self.containerStation.enableFuelPush then
self.fuelStation:setMetaIsEnable(meta, meta:get_string('craft_name') ~= '')
end
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 CraftSystem = Smc.requireCraftSystem()
local handler = CraftSystem:getProcessingCraftHandler('test')
handler:registerCraft('test_pro_craft', {
output = 'wood:allwhere_log',
recipe = {'wood:allwhere_stick', 'rocks:sylite'},
time = 5,
})
handler:registerCraft('test_craft', {
output = 'wood:allwhere_log',
recipe = {'wood:dallalia_stick', 'rocks:sylite'},
time = 2.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 ProcessingStation.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(math.round(meta:get_float('remaining_fuel'))))
:label(4.5, 3, tostring(math.round(meta:get_float('processing_time'))))
return form
end
ProcessingStation:new({
craftType = 'test',
tab = tab,
inputSlots = 2,
fuelGroup = 'wood',
alwaysEnable = false,
enableFuelPush = true,
enableIdling = true,
enableRegression = true,
},
{
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 ProcessingStation