WorkStation: rename WorkStation -> ImmersiveWorkStation, rework code, support new node tab stuff
This commit is contained in:
4
mods/CORE/ImmersiveWorkStation/init.lua
Normal file
4
mods/CORE/ImmersiveWorkStation/init.lua
Normal file
@@ -0,0 +1,4 @@
|
||||
local modInfo = Mod.getInfo('smc__core__immersive_work_station')
|
||||
local require = modInfo.require
|
||||
|
||||
return require('ImmersiveWorkStation')
|
||||
@@ -1,2 +1,2 @@
|
||||
name = smc__core__work_station
|
||||
name = smc__core__immersive_work_station
|
||||
depends = smc__core__craft_system, smc__core__node, smc__ui__work_station
|
||||
218
mods/CORE/ImmersiveWorkStation/src/ImmersiveWorkStation.lua
Normal file
218
mods/CORE/ImmersiveWorkStation/src/ImmersiveWorkStation.lua
Normal file
@@ -0,0 +1,218 @@
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
|
||||
local PlayerInventoryUI = Smc.requirePlayerInventoryUI()
|
||||
|
||||
local Node = Smc.requireNode()
|
||||
|
||||
-- Default settings that a work station node should have.
|
||||
local ImmersiveWorkStationNode = Node:getExtended({
|
||||
settings = {
|
||||
name = '',
|
||||
drawtype = 'normal',
|
||||
paramtype = 'light',
|
||||
groups = { immersive_station = 1, dig_immediate = 2 }
|
||||
}
|
||||
})
|
||||
|
||||
---A work station (node) that enables player interaction
|
||||
---with the immersive crafting mechanics.
|
||||
---@class ImmersiveWorkStation
|
||||
---@field name string
|
||||
---@field craftType CraftSystem.ImmersiveCraftType
|
||||
---@field handler CraftSystem.ImmersiveCraftHandler
|
||||
---@field tab PlayerInventoryUI.getTabFunc
|
||||
local ImmersiveWorkStation = {}
|
||||
|
||||
---Definition table for the `ImmersiveWorkStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class ImmersiveWorkStationDef
|
||||
---@field craftType CraftSystem.ImmersiveCraftType
|
||||
---@field tab PlayerInventoryUI.getTabFunc
|
||||
|
||||
---@param name string
|
||||
---@param def ImmersiveWorkStationDef
|
||||
---@param nodeDef Node.NodeDefinition
|
||||
function ImmersiveWorkStation:new(name, def, nodeDef)
|
||||
local handler = CraftSystem:getImmersiveCraftHandler(def.craftType)
|
||||
|
||||
---@type ImmersiveWorkStation
|
||||
local instance = getInstance(self, {})
|
||||
instance.name = name
|
||||
instance.craftType = def.craftType
|
||||
instance.handler = handler
|
||||
instance.tab = def.tab
|
||||
|
||||
if nodeDef.callbacks == nil then
|
||||
---@type Node.Callbacks
|
||||
nodeDef.callbacks = {}
|
||||
end
|
||||
local settings = nodeDef.settings
|
||||
local callbacks = nodeDef.callbacks
|
||||
|
||||
-- Callback when the work station block is installed in the world
|
||||
-- Ensures initialization of necessary parameters
|
||||
callbacks.on_construct = function(pos)
|
||||
instance:init(pos)
|
||||
end
|
||||
|
||||
-- Callback for interaction with a work station (start of crafting)
|
||||
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||
instance:showForm(pos, clicker)
|
||||
end
|
||||
|
||||
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
|
||||
instance:abortCraft(pos)
|
||||
end
|
||||
|
||||
callbacks.on_dig = function(pos, node, digger)
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local craftName = meta:get_string('craft_name')
|
||||
if craftName == '' then
|
||||
return core.node_dig(pos, node, digger)
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- Registering a node for Luanti.
|
||||
ImmersiveWorkStationNode:new(nodeDef)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
function ImmersiveWorkStation:showForm(pos, player)
|
||||
local playerName = player:get_player_name()
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local craftName = meta:get_string('craft_name')
|
||||
if craftName ~= '' then
|
||||
core.chat_send_player(playerName, 'The workbench is occupied by another craft.')
|
||||
return
|
||||
end
|
||||
|
||||
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 ImmersiveWorkStation:init(pos)
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
end
|
||||
|
||||
---@param pos MapPosition
|
||||
function ImmersiveWorkStation:abortCraft(pos)
|
||||
---@type ImmersiveWorkStation.MetaData
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
local craftName = meta:get_string('craft_name')
|
||||
if craftName == '' then
|
||||
return
|
||||
end
|
||||
|
||||
local craft = self.handler:getCraftByName(craftName)
|
||||
craft:abortCraft(pos)
|
||||
end
|
||||
|
||||
-- --- DEBUG ---
|
||||
|
||||
local handler = CraftSystem:getImmersiveCraftHandler('workbench')
|
||||
|
||||
local require = Mod.getInfo('smc__ui__player_inventory').require
|
||||
local baseGetCraftTab = require('PlayerInventoryUI.baseGetCraftTab')
|
||||
|
||||
---@param self PlayerInventoryUI
|
||||
---@param playerName string
|
||||
---@param tabType PlayerInventoryUI.TabType
|
||||
---@return Formspec
|
||||
local function tab(self, playerName, tabType)
|
||||
return baseGetCraftTab(self, playerName, tabType, 'node_tab', handler)
|
||||
end
|
||||
|
||||
ImmersiveWorkStation:new('test_station', {
|
||||
craftType = 'workbench',
|
||||
tab = tab,
|
||||
},
|
||||
{
|
||||
settings = {
|
||||
name = 'test_station',
|
||||
tiles = {'test_station.png'}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
handler:registerCraft('bricks', {
|
||||
output = 'rocks:sylite_bricks',
|
||||
recipe = {'rocks:sylite'},
|
||||
startStageName = 'tapping',
|
||||
group = 'test',
|
||||
stages = {
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping',
|
||||
targetItem = 'rocks:sylite',
|
||||
type = 'tapping',
|
||||
clicksNeed = 3,
|
||||
nextStage = 'tapping_2',
|
||||
},
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping_2',
|
||||
targetItem = 'rocks:sylite_block',
|
||||
type = 'tapping',
|
||||
clicksNeed = 5,
|
||||
nextStage = 'tapping_3',
|
||||
},
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping_3',
|
||||
targetItem = 'rocks:sylite_bricks',
|
||||
type = 'tapping',
|
||||
clicksNeed = 1,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
handler:registerCraft('block', {
|
||||
output = 'rocks:sylite_block',
|
||||
recipe = {'rocks:sylite'},
|
||||
startStageName = 'tapping',
|
||||
group = 'test',
|
||||
stages = {
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping',
|
||||
targetItem = 'rocks:sylite',
|
||||
type = 'tapping',
|
||||
clicksNeed = 5,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
handler:registerCraft('marentine_planks', {
|
||||
output = 'wood:marentine_planks',
|
||||
recipe = {'rocks:sylite'},
|
||||
startStageName = 'tapping',
|
||||
group = 'test',
|
||||
stages = {
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping',
|
||||
targetItem = 'wood:marentine_log',
|
||||
type = 'tapping',
|
||||
clicksNeed = 5,
|
||||
}
|
||||
},
|
||||
})
|
||||
--]]
|
||||
return ImmersiveWorkStation
|
||||
@@ -0,0 +1,9 @@
|
||||
---@alias ImmersiveWorkStation.MetaData.StringKey 'player_name'|'craft_name'|'stage_name'|'stage_data'
|
||||
---@alias ImmersiveWorkStation.MetaData.FloatKey 'craft_amount'
|
||||
|
||||
---**Only for EmmyLua.**
|
||||
---@class ImmersiveWorkStation.MetaData : NodeMetaRef
|
||||
---@field get_string fun(self, key:ImmersiveWorkStation.MetaData.StringKey):string
|
||||
---@field set_string fun(self, key:ImmersiveWorkStation.MetaData.StringKey, value:string)
|
||||
---@field get_float fun(self, key:ImmersiveWorkStation.MetaData.FloatKey):number
|
||||
---@field set_float fun(self, key:ImmersiveWorkStation.MetaData.FloatKey, value:number)
|
||||
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
@@ -1,4 +0,0 @@
|
||||
local modInfo = Mod.getInfo('smc__core__work_station')
|
||||
local require = modInfo.require
|
||||
|
||||
return require('WorkStation')
|
||||
@@ -1,268 +0,0 @@
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
local CraftSystem = Smc.requireCraftSystem()
|
||||
|
||||
local Node = Smc.requireNode()
|
||||
|
||||
-- Default settings that a work station node should have.
|
||||
local WorkStationNode = Node:getExtended({
|
||||
settings = {
|
||||
name = 'work_station',
|
||||
drawtype = 'normal',
|
||||
paramtype = 'light',
|
||||
groups = { work_station = 1, dig_immediate = 2 }
|
||||
}
|
||||
})
|
||||
|
||||
---A work station (node) that enables player interaction
|
||||
---with the immersive crafting mechanics.
|
||||
---@class WorkStation
|
||||
---@field name string
|
||||
---@field craftType CraftSystem.ImmersiveCraftType
|
||||
---@field handler CraftSystem.ImmersiveCraftHandler
|
||||
---@field UI BaseUI
|
||||
local WorkStation = {}
|
||||
|
||||
---Definition table for the `WorkStation`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class WorkStationDef
|
||||
---@field craftType CraftSystem.ImmersiveCraftType
|
||||
---@field UI BaseUI
|
||||
|
||||
---@param name string
|
||||
---@param def WorkStationDef
|
||||
---@param nodeDef Node.NodeDefinition
|
||||
function WorkStation:new(name, def, nodeDef)
|
||||
local handler = CraftSystem:getImmersiveCraftHandler(def.craftType)
|
||||
local UI = def.UI
|
||||
|
||||
---@type WorkStation
|
||||
local instance = getInstance(self, {})
|
||||
instance.name = name
|
||||
instance.handler = handler
|
||||
instance.craftType = def.craftType
|
||||
instance.UI = UI
|
||||
|
||||
if nodeDef.callbacks == nil then
|
||||
---@type Node.Callbacks
|
||||
nodeDef.callbacks = {}
|
||||
end
|
||||
local settings = nodeDef.settings
|
||||
local callbacks = nodeDef.callbacks
|
||||
|
||||
-- Callback when the work station block is installed in the world
|
||||
-- Ensures initialization of necessary parameters
|
||||
callbacks.on_construct = function(pos)
|
||||
-- Getting node metadata
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
-- Initializing the table of temporary parameters for crafting.
|
||||
Smc.setCraftDataByMeta(meta)
|
||||
end
|
||||
|
||||
-- Callback for interaction with a work station (start of crafting)
|
||||
callbacks.on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||
-- If the workstation is occupied by another craft...
|
||||
if not Smc.workStationIsFree(pos) then
|
||||
-- ...we inform the player about this...
|
||||
core.chat_send_player(clicker:get_player_name(), 'The workbench is occupied by another craft.')
|
||||
-- ...and do nothing.
|
||||
return
|
||||
end
|
||||
|
||||
-- We get a list of crafts that can be produced using the item that the player was holding in his hand.
|
||||
local crafts = handler:getCraftsByTriggerItem(itemstack:get_name())
|
||||
|
||||
-- If the list is empty...
|
||||
if crafts == nil then
|
||||
-- ..this means there are no crafts for this item,
|
||||
-- so we don't do anything.
|
||||
return
|
||||
else
|
||||
-- ...else we show the player the formspec for choosing a craft.
|
||||
instance:showSelectionCraftForm(clicker, crafts, pos)
|
||||
end
|
||||
end
|
||||
|
||||
callbacks.on_punch = function(pos, node, puncher, pointed_thing)
|
||||
if Smc.workStationIsFree(pos) then
|
||||
return
|
||||
end
|
||||
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
|
||||
local craft = handler:getCraftByName(craftData.craftName)
|
||||
craft:abortCraft(pos)
|
||||
end
|
||||
|
||||
callbacks.on_dig = function(pos, node, digger)
|
||||
if Smc.workStationIsFree(pos) then
|
||||
return core.node_dig(pos, node, digger)
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- Registering a callback for player choice of craft (this does not start crafting).
|
||||
UI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue)
|
||||
-- We get the name of the craft from the name of the button the player pressed.
|
||||
local craftName = string.match(fieldName, '(%S+)::item_button')
|
||||
assert(craftName ~= nil, 'Craft name cannot be empty')
|
||||
|
||||
-- We get a craft object.
|
||||
local craft = handler:getCraftByName(craftName)
|
||||
-- We get the name of the player who wants to start crafting.
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We enter into the context the craft that the player has chosen.
|
||||
UI:setContex(playerName, 'craft', craft)
|
||||
-- We show the player a new interface with a crafting description tab.
|
||||
UI:showForm(playerName, true)
|
||||
end)
|
||||
|
||||
-- We register a callback when the player starts crafting.
|
||||
UI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue)
|
||||
-- We get the name of the player who wants to start crafting.
|
||||
local playerName = player:get_player_name()
|
||||
|
||||
-- We get the workstation position and craft from the context.
|
||||
local pos = UI:getContex(playerName, 'pos')
|
||||
---@type CraftSystem.ImmersiveCraft
|
||||
local craft = UI:getContex(playerName, 'craft')
|
||||
|
||||
|
||||
-- We process the amount of crafting.
|
||||
local isCraftStart = false
|
||||
if fieldValue == 'x1' then
|
||||
-- Single craft
|
||||
isCraftStart = craft:startCraft(pos, player, 1)
|
||||
elseif fieldValue == 'x10' then
|
||||
-- Ten crafts at a time.
|
||||
isCraftStart = craft:startCraft(pos, player, 10)
|
||||
end
|
||||
|
||||
-- If the craft was started...
|
||||
if isCraftStart then
|
||||
-- ...we are closing the formspec.
|
||||
UI:closeForm(playerName)
|
||||
end
|
||||
end)
|
||||
|
||||
-- Registering a node for Luanti.
|
||||
WorkStationNode:new(nodeDef)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Show the player the initial workstation interface for choosing a craft.
|
||||
---@param player Player
|
||||
---@param crafts CraftSystem.ImmersiveCraft[]
|
||||
---@param pos Position
|
||||
function WorkStation:showSelectionCraftForm(player, crafts, pos)
|
||||
-- Create a table in the format craft result - craft name.
|
||||
local outputs = {}
|
||||
for _, craft in ipairs(crafts) do
|
||||
-- TODO: добавить проверку только на известные игроку рецепты
|
||||
outputs[craft.output] = craft.name
|
||||
end
|
||||
|
||||
-- We get the name of the player who wants to start crafting.
|
||||
local playerName = player:get_player_name()
|
||||
local UI = self.UI
|
||||
|
||||
-- We set the context for transferring data about the workstation coordinates and the list of crafts.
|
||||
UI:setContex(playerName, 'pos', pos)
|
||||
UI:setContex(playerName, 'outputs', outputs)
|
||||
|
||||
-- Showing the desktop interface to the player.
|
||||
UI:showForm(playerName,false)
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- --- DEBUG ---
|
||||
|
||||
---@type WorkStationUI
|
||||
local WorkStationUI = Mod.getInfo('smc__ui__work_station').require('WorkStationUI')
|
||||
local ImmersiveCraftHandler = CraftSystem:getImmersiveCraftHandler('workbench')
|
||||
|
||||
WorkStation:new('test_station', {
|
||||
craftType = 'workbench',
|
||||
UI = WorkStationUI
|
||||
},
|
||||
{
|
||||
settings = {
|
||||
name = 'test_station',
|
||||
tiles = {'test_station.png'}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
ImmersiveCraftHandler:registerCraft('bricks', {
|
||||
triggerItem = 'rocks:sylite',
|
||||
output = 'rocks:sylite_bricks',
|
||||
startStageName = 'tapping',
|
||||
group = 'test',
|
||||
stages = {
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping',
|
||||
targetItem = 'rocks:sylite',
|
||||
type = 'tapping',
|
||||
clicksNeed = 3,
|
||||
nextStage = 'tapping_2',
|
||||
},
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping_2',
|
||||
toolGroup = 'crushing',
|
||||
targetItem = 'rocks:sylite_block',
|
||||
type = 'tapping',
|
||||
clicksNeed = 5,
|
||||
nextStage = 'tapping_3',
|
||||
},
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping_3',
|
||||
targetItem = 'rocks:sylite_bricks',
|
||||
type = 'tapping',
|
||||
clicksNeed = 1,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
ImmersiveCraftHandler:registerCraft('block', {
|
||||
triggerItem = 'rocks:sylite',
|
||||
output = 'rocks:sylite_block',
|
||||
startStageName = 'tapping',
|
||||
group = 'test',
|
||||
stages = {
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping',
|
||||
targetItem = 'rocks:sylite',
|
||||
type = 'tapping',
|
||||
clicksNeed = 5,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
ImmersiveCraftHandler:registerCraft('marentine_planks', {
|
||||
triggerItem = 'wood:marentine_log',
|
||||
output = 'wood:marentine_planks',
|
||||
startStageName = 'tapping',
|
||||
group = 'test',
|
||||
stages = {
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
{
|
||||
name = 'tapping',
|
||||
targetItem = 'wood:marentine_log',
|
||||
type = 'tapping',
|
||||
clicksNeed = 5,
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
return WorkStation
|
||||
Reference in New Issue
Block a user