ProcessingFuelStation: create class
This commit is contained in:
4
mods/CORE/ProcessingFuelStation/init.lua
Normal file
4
mods/CORE/ProcessingFuelStation/init.lua
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
local modInfo = Mod.getInfo('smc__core__processing_fuel_station')
|
||||||
|
local require = modInfo.require
|
||||||
|
|
||||||
|
return require('ProcessingFuelStation')
|
||||||
2
mods/CORE/ProcessingFuelStation/mod.conf
Normal file
2
mods/CORE/ProcessingFuelStation/mod.conf
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
name = smc__core__processing_fuel_station
|
||||||
|
depends = smc__core__base_processing_station
|
||||||
269
mods/CORE/ProcessingFuelStation/src/ProcessingFuelStation.lua
Normal file
269
mods/CORE/ProcessingFuelStation/src/ProcessingFuelStation.lua
Normal file
@@ -0,0 +1,269 @@
|
|||||||
|
local FUEL_ENERGY_FACTOR = 0.1
|
||||||
|
|
||||||
|
local BaseProcessingStation = Smc.requireBaseProcessingStation()
|
||||||
|
|
||||||
|
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||||
|
|
||||||
|
local Node = Smc.requireNode()
|
||||||
|
|
||||||
|
local ProcessingFuelStationNode = Node:getExtended({
|
||||||
|
settings = {
|
||||||
|
name = '',
|
||||||
|
drawtype = 'normal',
|
||||||
|
paramtype = 'light',
|
||||||
|
groups = { processing_fuel_station = 1, dig_immediate = 2 }
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
---@class ProcessingFuelStation : BaseProcessingStation
|
||||||
|
---@field enableIdling boolean?
|
||||||
|
---@field fuelGroup string
|
||||||
|
---@field fuelSlots integer
|
||||||
|
local ProcessingFuelStation = BaseProcessingStation:extend({})
|
||||||
|
|
||||||
|
---Definition table for the `ProcessingFuelStation`.
|
||||||
|
---
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class ProcessingFuelStationDef : BaseProcessingStationDef
|
||||||
|
---@field enableIdling boolean?
|
||||||
|
---@field fuelGroup string?
|
||||||
|
---@field fuelSlots integer?
|
||||||
|
|
||||||
|
---@param name string
|
||||||
|
---@param def ProcessingFuelStationDef
|
||||||
|
---@param nodesDef BaseProcessingStation.NodesDef
|
||||||
|
function ProcessingFuelStation:new(name, def, nodesDef)
|
||||||
|
---@type ProcessingFuelStation
|
||||||
|
local instance = self:getInstance(BaseProcessingStation:new(name, def, nodesDef))
|
||||||
|
instance.enableIdling = def.enableIdling
|
||||||
|
instance.fuelGroup = def.fuelGroup or 'fuel'
|
||||||
|
instance.fuelSlots = def.fuelSlots 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 instance:onTimer(pos, elapsed, node, timeout)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Registering a nodes for Luanti.
|
||||||
|
local node = ProcessingFuelStationNode:new({ settings = settings, callbacks = callbacks })
|
||||||
|
instance.nodeName = node.itemDef.settings.name
|
||||||
|
|
||||||
|
if activeSettings ~= nil then
|
||||||
|
local activeNode = ProcessingFuelStationNode:new({ settings = activeSettings, callbacks = callbacks })
|
||||||
|
instance.activeNodeName = activeNode.itemDef.settings.name
|
||||||
|
end
|
||||||
|
|
||||||
|
return instance
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param pos MapPosition
|
||||||
|
function ProcessingFuelStation:init(pos)
|
||||||
|
---@type ProcessingFuelStation.MetaData
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
meta:set_float('remaining_fuel', 0.0)
|
||||||
|
|
||||||
|
local inventory = meta:get_inventory()
|
||||||
|
inventory:set_size('fuel', self.fuelSlots)
|
||||||
|
|
||||||
|
self:startTimer(pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param remainingFuel number
|
||||||
|
---@param inventory InvRef
|
||||||
|
---@param timeout number
|
||||||
|
---@return number remainingFuel
|
||||||
|
function ProcessingFuelStation:takeFuel(remainingFuel, inventory, timeout)
|
||||||
|
local fuelList = inventory:get_list('fuel')
|
||||||
|
|
||||||
|
for _, fuelStack in ipairs(fuelList) do
|
||||||
|
if not fuelStack:is_empty() then
|
||||||
|
-- Note: since item groups can only contain integers,
|
||||||
|
-- we divide the fuel units by a constant `FUEL_ENERGY_FACTOR` to convert seconds to smaller units.
|
||||||
|
local fuelEnergy = core.get_item_group(fuelStack:get_name(), self.fuelGroup or 'fuel') / FUEL_ENERGY_FACTOR
|
||||||
|
|
||||||
|
if fuelEnergy > 0 then
|
||||||
|
local itemCount = fuelStack:get_count()
|
||||||
|
|
||||||
|
-- If the energy of one item is not enough for one iteration,
|
||||||
|
-- we calculate how many items we need to take
|
||||||
|
if fuelEnergy < timeout then
|
||||||
|
itemCount = math.min(math.ceil(timeout/fuelEnergy), itemCount)
|
||||||
|
else
|
||||||
|
itemCount = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
remainingFuel = remainingFuel + (fuelEnergy * itemCount)
|
||||||
|
|
||||||
|
fuelStack:set_count(itemCount)
|
||||||
|
inventory:remove_item('fuel', fuelStack)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return remainingFuel
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param isProcessing boolean
|
||||||
|
---@param meta ProcessingFuelStation.MetaData
|
||||||
|
---@param timeout number
|
||||||
|
---@return boolean isProcessing
|
||||||
|
function ProcessingFuelStation:burnFuel(isProcessing, meta, timeout)
|
||||||
|
local inventory = meta:get_inventory()
|
||||||
|
|
||||||
|
local remainingFuel = meta:get_float('remaining_fuel')
|
||||||
|
|
||||||
|
if isProcessing and remainingFuel <= 0 and not inventory:is_empty('fuel') then
|
||||||
|
remainingFuel = self:takeFuel(remainingFuel, inventory, timeout)
|
||||||
|
end
|
||||||
|
|
||||||
|
if isProcessing then
|
||||||
|
remainingFuel = remainingFuel - timeout
|
||||||
|
elseif self.enableIdling then
|
||||||
|
remainingFuel = remainingFuel - timeout
|
||||||
|
end
|
||||||
|
|
||||||
|
if remainingFuel < 0 then
|
||||||
|
remainingFuel = 0
|
||||||
|
isProcessing = false
|
||||||
|
end
|
||||||
|
|
||||||
|
meta:set_float('remaining_fuel', remainingFuel)
|
||||||
|
|
||||||
|
return isProcessing or (self.enableIdling == true and remainingFuel ~= 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param pos MapPosition
|
||||||
|
---@param elapsed number
|
||||||
|
---@param node NodeTable
|
||||||
|
---@param timeout number
|
||||||
|
---@return boolean?
|
||||||
|
function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
|
||||||
|
local meta = core.get_meta(pos)
|
||||||
|
|
||||||
|
local isProcessing = self:burnFuel(true, meta, timeout)
|
||||||
|
|
||||||
|
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 ProcessingFuelStation.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
|
||||||
|
|
||||||
|
ProcessingFuelStation: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 ProcessingFuelStation
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
---@alias ProcessingFuelStation.MetaData.FloatKey 'remaining_fuel'
|
||||||
|
|
||||||
|
---**Only for EmmyLua.**
|
||||||
|
---@class ProcessingFuelStation.MetaData : NodeMetaRef
|
||||||
|
---@field get_float fun(self, key:ProcessingFuelStation.MetaData.FloatKey):number
|
||||||
|
---@field set_float fun(self, key:ProcessingFuelStation.MetaData.FloatKey, value:number)
|
||||||
Reference in New Issue
Block a user