429 lines
12 KiB
Lua
429 lines
12 KiB
Lua
local FUEL_ENERGY_FACTOR = 0.1
|
|
|
|
local v = vector.new
|
|
local assert, ipairs = assert, ipairs
|
|
local mathMin, mathCeil = math.min, math.ceil
|
|
local coreNodePunch, coreGetNode, coreGetItemGroup = core.node_punch, core.get_node, core.get_item_group
|
|
|
|
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 alwaysEnable boolean?
|
|
---@field enablerItemGroup string?
|
|
---@field disablerItemGroup string?
|
|
---@field enableIdling boolean?
|
|
---
|
|
---@field fuelGroup string
|
|
---@field fuelSlots integer
|
|
---
|
|
---@field getMeta fun(self:ProcessingFuelStation, pos:MapVector):ProcessingFuelStation.MetaData
|
|
local ProcessingFuelStation = BaseProcessingStation:extend({})
|
|
|
|
---Definition table for the `ProcessingFuelStation`.
|
|
---
|
|
---**Only for EmmyLua.**
|
|
---@class ProcessingFuelStationDef : BaseProcessingStationDef
|
|
---If `true`, the station will always be enable
|
|
---(that is, if there is fuel, it will always try to burn it).
|
|
---
|
|
---**Note**: This does not mean the station timer will run always:
|
|
---`enable` and `active` are different states.
|
|
---@field alwaysEnable boolean?
|
|
---
|
|
---The item group that will turn on the station when item hits it.
|
|
---If is `nil`, punch with any item will enable the station.
|
|
---@field enablerItemGroup string?
|
|
---
|
|
---Same as `enablerItemGroup` field, only it turns off the station.
|
|
---@field disablerItemGroup string?
|
|
---
|
|
---If `true`, it will burn the remaining fuel even if the station is disable.
|
|
---@field enableIdling boolean?
|
|
---
|
|
---@field fuelGroup string?
|
|
---@field fuelSlots integer?
|
|
|
|
---@param def ProcessingFuelStationDef
|
|
---@param nodesDef BaseProcessingStation.NodesDefinition?
|
|
function ProcessingFuelStation:new(def, nodesDef)
|
|
---@type ProcessingFuelStation
|
|
local instance = self:getInstance(BaseProcessingStation:new(def))
|
|
instance.alwaysEnable = def.alwaysEnable
|
|
instance.enablerItemGroup = def.enablerItemGroup
|
|
instance.enableIdling = def.enableIdling
|
|
instance.fuelGroup = def.fuelGroup or 'fuel'
|
|
instance.fuelSlots = def.fuelSlots 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_punch or function(...) end
|
|
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
|
|
oldCallback(pos, node, puncher, pointed_thing)
|
|
|
|
instance: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 --[[@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 = 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
|
|
|
|
Smc.addNodeSuperclass(instance.activeNodeName, instance)
|
|
end
|
|
|
|
Smc.addNodeSuperclass(instance.nodeName, instance)
|
|
|
|
return instance
|
|
end
|
|
|
|
---@param pos MapVector
|
|
function ProcessingFuelStation:init(pos)
|
|
local meta = self:getMeta(pos)
|
|
meta:set_float('remaining_fuel', 0.0)
|
|
|
|
local inventory = meta:get_inventory()
|
|
inventory:set_size('fuel', self.fuelSlots)
|
|
|
|
self:startTimer(pos)
|
|
end
|
|
|
|
---@param pos MapVector
|
|
---@param node NodeTable
|
|
---@param puncher ObjectRef|Player
|
|
---@param pointedThing pointed_thing
|
|
function ProcessingFuelStation:onPunch(pos, node, puncher, pointedThing)
|
|
if self.alwaysEnable then
|
|
return
|
|
end
|
|
|
|
local meta = self:getMeta(pos)
|
|
local isEnable = self:getMetaIsEnable(meta)
|
|
|
|
if not isEnable then
|
|
if self.enablerItemGroup == nil then
|
|
self:setMetaIsEnable(meta, true)
|
|
self:startTimer(pos)
|
|
|
|
return
|
|
end
|
|
|
|
local itemName = puncher:get_wielded_item():get_name()
|
|
if coreGetItemGroup(itemName, self.enablerItemGroup) > 0 then
|
|
self:setMetaIsEnable(meta, true)
|
|
self:startTimer(pos)
|
|
end
|
|
else
|
|
if self.disablerItemGroup == nil then
|
|
self:setMetaIsEnable(meta, false)
|
|
self:startTimer(pos)
|
|
|
|
return
|
|
end
|
|
|
|
local itemName = puncher:get_wielded_item():get_name()
|
|
if coreGetItemGroup(itemName, self.disablerItemGroup) > 0 then
|
|
self:setMetaIsEnable(meta, false)
|
|
self:startTimer(pos)
|
|
end
|
|
end
|
|
end
|
|
|
|
---@param meta ProcessingFuelStation.MetaData
|
|
---@param isEnable boolean
|
|
function ProcessingFuelStation:setMetaIsEnable(meta, isEnable)
|
|
if isEnable then
|
|
meta:set_string('is_enable', 'true')
|
|
else
|
|
meta:set_string('is_enable', 'false')
|
|
end
|
|
end
|
|
|
|
---@param meta ProcessingFuelStation.MetaData
|
|
---@return boolean
|
|
function ProcessingFuelStation:getMetaIsEnable(meta)
|
|
local isEnableString = meta:get_string('is_enable')
|
|
|
|
if isEnableString == 'true' then
|
|
return true
|
|
else
|
|
return false
|
|
end
|
|
end
|
|
|
|
---Subtracts the elapsed time from the remaining fuel value.
|
|
---@param isProcessing boolean
|
|
---@param meta ProcessingFuelStation.MetaData
|
|
---@param elapsed number
|
|
---@return boolean isProcessing
|
|
function ProcessingFuelStation:burnFuel(isProcessing, meta, elapsed)
|
|
local remainingFuel = meta:get_float('remaining_fuel')
|
|
|
|
if isProcessing then
|
|
remainingFuel = remainingFuel - elapsed
|
|
elseif self.enableIdling then
|
|
remainingFuel = remainingFuel - elapsed
|
|
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
|
|
|
|
---If required, takes an item from the fuel slot
|
|
---and converts it into the remaining fuel in the station.
|
|
---@param isProcessing boolean
|
|
---@param meta ProcessingFuelStation.MetaData
|
|
---@param elapsed number
|
|
---@return boolean isProcessing
|
|
function ProcessingFuelStation:takeFuel(isProcessing, meta, elapsed)
|
|
local inventory = meta:get_inventory()
|
|
local remainingFuel = meta:get_float('remaining_fuel')
|
|
local isEnable = self:getMetaIsEnable(meta)
|
|
|
|
if not (self.alwaysEnable or isEnable) then
|
|
return isProcessing
|
|
end
|
|
|
|
if remainingFuel > 0 then
|
|
return isProcessing
|
|
end
|
|
|
|
if inventory:is_empty('fuel') then
|
|
self:setMetaIsEnable(meta, false)
|
|
return isProcessing
|
|
end
|
|
|
|
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 = coreGetItemGroup(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 < elapsed then
|
|
itemCount = mathMin(mathCeil(elapsed / fuelEnergy), itemCount)
|
|
else
|
|
itemCount = 1
|
|
end
|
|
|
|
remainingFuel = remainingFuel + (fuelEnergy * itemCount)
|
|
|
|
fuelStack:set_count(itemCount)
|
|
inventory:remove_item('fuel', fuelStack)
|
|
end
|
|
end
|
|
end
|
|
|
|
isProcessing = true
|
|
|
|
meta:set_float('remaining_fuel', remainingFuel)
|
|
|
|
return isProcessing
|
|
end
|
|
|
|
---@param meta ProcessingFuelStation.MetaData
|
|
---@param pos MapVector
|
|
function ProcessingFuelStation:pushContainerStation(meta, pos)
|
|
local containerPos = pos + v(0, 1, 0)
|
|
local containerName = coreGetNode(containerPos).name
|
|
|
|
if coreGetItemGroup(containerName, 'processing_container_station') == 0 then
|
|
return
|
|
end
|
|
|
|
local containerStation = Smc.getNodeSuperclassByPos(containerPos) --[[@as ProcessingContainerStation?]]
|
|
assert(containerStation ~= nil)
|
|
|
|
if containerStation.fuelGroup ~= self.fuelGroup then
|
|
return
|
|
end
|
|
|
|
containerStation:onPush(containerPos, meta:get_float('remaining_fuel'))
|
|
end
|
|
|
|
---@param pos MapVector
|
|
---@param elapsed number
|
|
---@param node NodeTable
|
|
---@return boolean?
|
|
function ProcessingFuelStation:onTimer(pos, elapsed, node, timeout)
|
|
local meta = self:getMeta(pos)
|
|
local isEnable = self:getMetaIsEnable(meta)
|
|
|
|
local isProcessing = self:burnFuel(self.alwaysEnable or isEnable, meta, elapsed)
|
|
isProcessing = self:takeFuel(isProcessing, meta, elapsed)
|
|
self:pushContainerStation(meta, pos)
|
|
|
|
self:swapNode(isProcessing, pos)
|
|
|
|
PlayerInventoryUI:updateNodeForm(pos)
|
|
|
|
return isProcessing
|
|
end
|
|
|
|
---@param pos MapVector
|
|
---@param enable boolean
|
|
function ProcessingFuelStation:onPush(pos, enable)
|
|
local meta = self:getMeta(pos)
|
|
|
|
self:setMetaIsEnable(meta, enable)
|
|
|
|
if enable then
|
|
self:startTimer(pos)
|
|
end
|
|
end
|
|
|
|
--- DEBUG ---
|
|
|
|
local Formspec = Smc.requireFormspec()
|
|
local TileSheet = Smc.requireTileSheet()
|
|
|
|
---@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)
|
|
|
|
local meta = core.get_meta(pos) --[[@as ProcessingFuelStation.MetaData]]
|
|
|
|
form
|
|
:image(0, 0, 11, 9, 'player_inventory_tab_background.png')
|
|
: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({
|
|
craftType = 'test',
|
|
tab = tab,
|
|
inputSlots = 2,
|
|
fuelGroup = 'wood',
|
|
alwaysEnable = false,
|
|
enableIdling = true,
|
|
},
|
|
{
|
|
settings = {
|
|
name = 'test_processing_node',
|
|
drawtype = 'mesh',
|
|
mesh = 'test_bonfire.obj',
|
|
tiles = {'test_bonfire.png'},
|
|
},
|
|
})
|
|
|
|
core.register_abm({
|
|
interval = 1.0,
|
|
chance = 1,
|
|
nodenames = {'processing_fuel_station:test_processing_node'},
|
|
---@param pos MapVector
|
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
|
local meta = core.get_meta(pos) --[[@as ProcessingFuelStation.MetaData]]
|
|
if meta:get_float("remaining_fuel") > 0 then
|
|
core.add_particlespawner({
|
|
amount = 10,
|
|
time = 1,
|
|
size = 2,
|
|
pos = {
|
|
min = pos - v(0.4, 0.4, 0.4) + v(0, 0.3, 0),
|
|
max = pos + v(0.4, 0.4, 0.4) + v(0, 0.3, 0),
|
|
},
|
|
|
|
texture = {
|
|
name = 'test_fire_part.png',
|
|
animation = {
|
|
type = "vertical_frames",
|
|
aspect_w = 3,
|
|
aspect_h = 4,
|
|
length = -1,
|
|
},},
|
|
})
|
|
end--]]
|
|
end
|
|
})
|
|
|
|
return ProcessingFuelStation
|