12 Commits

13 changed files with 343 additions and 47 deletions

View File

@@ -1,11 +1,28 @@
local getInstance = Class.getInstance
---@class BaseUI
---Context for transferring data between displaying
---the form spec to the player and handling callbacks.
---@field name string
---@field contex table<string, table>
local BaseUI = {
contex = {}
}
---@param name string
function BaseUI:new(name)
---@type BaseUI
local instance = getInstance(self, {})
instance.name = name
-- Ensure that when the form is closed, the context is cleared.
instance:registerOnFormQuit(function(player)
instance:clearContex(player)
end)
return instance
end
---Set a value for a specific key.
---Recommended for use where `core.show_formspec` is used.
---
@@ -14,7 +31,7 @@ local BaseUI = {
---@param key string
---@param value any
function BaseUI:setContex(player, key, value)
if type(player) == 'table' then
if type(player) ~= 'string' then
player = player:get_player_name()
end
@@ -33,7 +50,7 @@ end
---@param key string
---@return any?
function BaseUI:getContex(player, key)
if type(player) == 'table' then
if type(player) ~= 'string' then
player = player:get_player_name()
end
@@ -45,11 +62,45 @@ end
---Recommended for use when closing formspec.
---@param player string|Player Player as an object (entity) or his name
function BaseUI:clearContex(player)
if type(player) == 'table' then
if type(player) ~= 'string' then
player = player:get_player_name()
end
self.contex[player] = {}
end
---Register a callback for a specific player action in FormSpec.
---
---The callback is guaranteed to be called only for the specified field.
---@param fieldName string the name of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called.
--- * `fieldValue` - the field value that is passed in the `fields` table.
function BaseUI:registerOnFormFieldAction(fieldName, callback)
Smc.registerOnFormFieldAction(self.name, fieldName, callback)
end
---Register a callback function to close the FormSpec.
---
---The callback is guaranteed to be called only for the specified form.
---@param callback fun(player:Player) Callback to handle the close. Parameters:
--- * `player` - the player who closed the form.
function BaseUI:registerOnFormQuit(callback)
Smc.registerOnFormQuit(self.name, callback)
end
---Register a callback for a specific player action in FormSpec.
---
---The callback function is guaranteed to be called only for the specified form and
---for the field whose name matches the pattern.
---@param fieldNamePattern string the name pattern of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called (will match the pattern).
--- * `fieldValue` - the field value that is passed in the `fields` table.
function BaseUI:registerOnFormFieldActionMatch( fieldNamePattern, callback)
Smc.registerOnFormFieldActionMatch(self.name, fieldNamePattern, callback)
end
return BaseUI

View File

@@ -0,0 +1,62 @@
local posToString, getSize = core.pos_to_string, table.get_size
local CRAFT_LIMIT_PER_PLAYER = 2
local CRAFT_LIMIT_PER_SERVER = 10
---Designed to keep track of active immersive
---crafts so as not to overload the server.
---
---The counter is individual for each player,
---but the total number of active crafts is also tracked.
---@class CraftSystem.ImmersiveCraftCounter
---@field craftsCountForPlayers table<string, table<string, boolean>>
---@field craftsCountForServer table<string, boolean>
local ImmersiveCraftCounter = {
craftsCountForPlayers = {},
craftsCountForServer = {}
}
---Add craft to the counter.
---
---Returns `false` if the counter is already full.
---@param pos MapPosition
---@param Player Player
---@return boolean
function ImmersiveCraftCounter:addCraft(pos, Player)
local playerName = Player:get_player_name()
if self.craftsCountForPlayers[playerName] == nil then
self.craftsCountForPlayers[playerName] = {}
end
if getSize(self.craftsCountForPlayers[playerName]) >= CRAFT_LIMIT_PER_PLAYER then
return false
end
if getSize(self.craftsCountForServer) >= CRAFT_LIMIT_PER_SERVER then
return false
end
local posString = posToString(pos)
self.craftsCountForPlayers[playerName][posString] = true
self.craftsCountForServer[posString] = true
return true
end
---Remove craft from the counter.
---@param pos MapPosition
---@param Player Player
function ImmersiveCraftCounter:removeCraft(pos, Player)
local playerCrafts = self.craftsCountForPlayers[Player:get_player_name()]
if playerCrafts == nil then
return
end
local posString = posToString(pos)
playerCrafts[posString] = nil
self.craftsCountForServer[posString] = nil
end
-- TODO: сделать сохранение данных во время перезагрузки сервера
return ImmersiveCraftCounter

View File

@@ -25,7 +25,7 @@ local tappingNode = ImmersiveCraftNode:new({
initial_properties = {
visual = 'wielditem',
visual_size = {x=0.4, y=0.4, z=0.4},
static_save = false,
--static_save = false,
},
_item_name = '',
@@ -58,11 +58,24 @@ local tappingNode = ImmersiveCraftNode:new({
on_punch = function(luaEntity, puncher, time_from_last_punch, tool_capabilities, dir, damage)
-- We simply trigger the event that the player hits the crafting node, bound to the work station with coordinates.
Events.call(('craft_system:punch_tapping_node:%s:%s'):format(
luaEntity._craft_name, luaEntity._stage_name), luaEntity._work_station_pos, puncher)
luaEntity._craft_name, luaEntity._stage_name), luaEntity._work_station_pos, puncher, luaEntity)
return false
end,
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
get_staticdata = function(luaEntity)
---@type CraftSystem.ImmersiveCraftStage.TappingNode.InitParams?
local params = {
targetItem = luaEntity._item_name,
craftName = luaEntity._craft_name,
stageName = luaEntity._stage_name,
WorkStationPos = luaEntity._work_station_pos
}
return core.serialize(params)
end,
---Sets the entity display of the item. If the item stack was not transferred, `self._item_name` is used.
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
---@param itemName ItemStackString?

View File

@@ -10,8 +10,10 @@ local getInstance = Class.getInstance
---@field startHandler fun(self, pos:Position, Player:Player)
---@field processHandler fun(self, pos:Position, Player:Player)
---@field endHandler fun(self, pos:Position, Player:Player)
---@field abortHandler fun(self, pos:Position, Player:Player)
---@field toString fun(self):string
---@field nextStage string?
---@field returnItems table? TODO: доделать тут тип
---@field returnItems ItemStackString[]?
local ImmersiveCraftStage = {}
---Definition table for the `CraftSystem.ImmersiveCraft`.
@@ -22,7 +24,7 @@ local ImmersiveCraftStage = {}
---@field type CraftSystem.ImmersiveCraftStage.Type
---@field craftName string?
---@field nextStage string?
---@field returnItems table? TODO: доделать тут тип
---@field returnItems ItemStackString[]?
---@param def CraftSystem.ImmersiveCraftStageDef
---@return CraftSystem.ImmersiveCraftStage

View File

@@ -7,18 +7,18 @@ local require = modInfo.require
local BaseCraft = require('CraftSystem.BaseCraft')
---@class CraftSystem.PrimitiveCraft : CraftSystem.BaseCraft
---@field output string
---@field recipe CraftSystem.CraftRecipe
---@field returnItems table? TODO: доделать тут тип
---@field output string
---@field recipe CraftSystem.CraftRecipe
---@field returnItems ItemStackString[]
local PrimitiveCraft = extend(BaseCraft, {})
---Definition table for the `CraftSystem.PrimitiveCraft`.
---
---**Only for EmmyLua.**
---@class CraftSystem.PrimitiveCraftDef : CraftSystem.BaseCraftDef
---@field output string
---@field recipe CraftSystem.CraftRecipe
---@field returnItems table? TODO: доделать тут тип
---@field output string
---@field recipe CraftSystem.CraftRecipe
---@field returnItems ItemStackString[]
---@param name string
---@param def CraftSystem.PrimitiveCraftDef

View File

@@ -103,17 +103,19 @@ local function itemDefToLuantiDef(itemDef)
end
if s._title_color ~= nil and s._description_color ~= nil then
description = colorize(s._title_color ,S(title))..'\n'..
colorize(s._description_color, S(description))
title = colorize(s._title_color ,S(title))
description = colorize(s._description_color, S(description))
else
error('Item description color parameters cannot be empty.')
end
assert(s._rarity ~= nil, 'The rarity of an item cannot be empty.')
description = addRarityToDesc(s._rarity, description)
local luantiDescription = addRarityToDesc(s._rarity, title..'\n'..description)
s.description = description..core.get_background_escape_sequence('#110F13')
s._title = title
s._description = description
s.description = luantiDescription..core.get_background_escape_sequence('#110F13')
local luantiDef = defMerge(s, c)

View File

@@ -9,15 +9,18 @@ local WorkStationUI = Mod.getInfo('smc__ui__work_station').require('WorkStationU
---@type Node
local Node = Mod.getInfo('smc__core__node').require('Node')
-- 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}
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 group CraftSystem.CraftGroup
@@ -36,36 +39,61 @@ local WorkStation = {}
function WorkStation:new(name, def)
local nodeDef = def.nodeDef
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)
--meta:set_string('work_station_name', name)
--meta:set_string('craft_group', def.group)
-- 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)
Logger.warningLog('Punch')
local crafts = ImmersiveCraftHandler:getCraftsByTriggerItem(itemstack:get_name())
if crafts == nil then
-- 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
self:showSelectionCraftForm(clicker, crafts)
-- We get a list of crafts that can be produced using the item that the player was holding in his hand.
local crafts = ImmersiveCraftHandler: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.
self:showSelectionCraftForm(clicker, crafts, pos)
end
end
local instance = getInstance(self, {
name = name,
group = def.group,
nodeDef = nodeDef
})
print('NODEDEF: ',dump(nodeDef.callbacks))
callbacks.on_dig = function(pos, node, digger)
if Smc.workStationIsFree(pos) then
return core.node_dig(pos, node, digger)
else
return false
end
end
---@type WorkStation
local instance = getInstance(self, {})
instance.name = name
instance.group = def.group
instance.nodeDef = nodeDef
-- Registering a node for Luanti.
WorkStationNode:new(nodeDef)
return instance
@@ -73,16 +101,48 @@ end
---@param player Player
---@param crafts CraftSystem.ImmersiveCraft[]
function WorkStation:showSelectionCraftForm(player, crafts)
---@param pos Position
function WorkStation:showSelectionCraftForm(player, crafts, pos)
local outputs = {}
for i, craft in ipairs(crafts) do
outputs[i] = craft.output
for _, craft in ipairs(crafts) do
outputs[craft.output] = craft.name
end
core.show_formspec(player:get_player_name(), 'work_station:selection_craft', WorkStationUI:selectionCraftForm(outputs))
local playerName = player:get_player_name()
WorkStationUI:setContex(playerName, 'pos', pos)
WorkStationUI:setContex(playerName, 'outputs', outputs)
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, false))
end
WorkStationUI:registerOnFormFieldActionMatch('%S+::item_button',function(player, fieldName, fieldValue)
---@type ItemStackString?
local craftName = string.match(fieldName, '(%S+)::item_button')
if craftName == nil then
return
end
local craft = ImmersiveCraftHandler:getCraftByName(craftName)
local playerName = player:get_player_name()
WorkStationUI:setContex(playerName, 'craft', craft)
core.show_formspec(playerName, 'work_station:selection_craft', WorkStationUI:selectionCraftForm(playerName, true))
end)
WorkStationUI:registerOnFormFieldAction('work_station:start_craft', function(player, fieldName, fieldValue)
local playerName = player:get_player_name()
local pos = WorkStationUI:getContex(playerName, 'pos')
local craft = WorkStationUI:getContex(playerName, 'craft')
craft:startCraft(pos, player)
core.close_formspec(playerName, 'work_station:selection_craft')
end)
-- --- DEBUG ---
WorkStation:new('test_station', {
group = 'test',
nodeDef = {
@@ -93,20 +153,52 @@ WorkStation:new('test_station', {
}
})
ImmersiveCraftHandler:addCraft('bricks', {
ImmersiveCraftHandler:registerCraft('bricks', {
triggerItem = 'rocks:sylite',
output = 'rocks:sylite_bricks',
startStageName = 'ab',
startStageName = 'tapping',
group = 'test',
stages = {},
stages = {
---@type CraftSystem.ImmersiveCraftStage.TappingStageDef
{
name = 'tapping',
targetItem = 'rocks:sylite',
type = 'tapping',
clicksNeed = 3,
}
},
})
ImmersiveCraftHandler:addCraft('block', {
ImmersiveCraftHandler:registerCraft('block', {
triggerItem = 'rocks:sylite',
output = 'rocks:sylite_block',
startStageName = 'ab',
startStageName = 'tapping',
group = 'test',
stages = {},
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

View File

@@ -7,8 +7,7 @@ local Node = Mod.getInfo('smc__core__node').require('Node')
---@type Node.NodeDefinition
local defaultRockDef = {
settings = {
name = "",
_description = "A rocks",
name = '',
tiles = {},
groups = {
dig_immediate = 2,

View File

@@ -0,0 +1,6 @@
# textdomain: rocks
sylite_block-title=Силитовый блок
sylite_block-desc=Массивный блок из цельного куска силита.
sylite_bricks-title=Силитовые кирпичи
sylite_bricks-desc=Кладка из силитовых кирпичей.

View File

@@ -7,3 +7,4 @@ require('Lib.globalStep')
require('Lib.color')
require('Lib.immersiveCraftData')
require('Lib.getObjectByGUID')
require('Lib.receiveFields')

View File

@@ -3,8 +3,9 @@ local EMPTY_FLAG = 'EMPTY_FLAG'
---@class ImmersiveStageData
---@class ImmersiveCraftData
---@field craftName string,
---@field stageName string,
---@field craftName string
---@field stageName string
---@field amount integer
---@field stageData ImmersiveStageData?
---Returns craft data for immersive crafting, which is stored in the desktop metadata.

View File

@@ -0,0 +1,67 @@
---Register a callback for a specific player action in FormSpec.
---
---The callback is guaranteed to be called only for the specified form and the specified field.
---@param formName string the name of the formspec for which the callback will be called.
---@param fieldName string the name of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called.
--- * `fieldValue` - the field value that is passed in the `fields` table.
function Smc.registerOnFormFieldAction(formName, fieldName, callback)
core.register_on_player_receive_fields(function(player, formname, fields)
-- If the form name does not match the given...
if formName ~= formname then
-- ...we do nothing.
return
end
local fieldValue = fields[fieldName]
-- If the field value is not nil (the player called this field)...
if fieldValue ~= nil then
-- ...calling a callback.
callback(player, fieldName, fieldValue)
end
end)
end
---Register a callback function to close the FormSpec.
---
---The callback is guaranteed to be called only for the specified form.
---@param formName string the name of the form that is being closed.
---@param callback fun(player:Player) Callback to handle the close. Parameters:
--- * `player` - the player who closed the form.
function Smc.registerOnFormQuit(formName, callback)
Smc.registerOnFormFieldAction(formName, 'quit', callback)
end
---Register a callback for a specific player action in FormSpec.
---
---The callback function is guaranteed to be called only for the specified form and
---for the field whose name matches the pattern.
---@param formName string the name of the formspec for which the callback will be called.
---@param fieldNamePattern string the name pattern of the field for which the callback will be called.
---@param callback fun(player:Player, fieldName:string, fieldValue:any) Callback to handle the action. Parameters:
--- * `player` - the player who performed the action.
--- * `fieldName` - the name of the field on which the callback is called (will match the pattern).
--- * `fieldValue` - the field value that is passed in the `fields` table.
function Smc.registerOnFormFieldActionMatch(formName, fieldNamePattern, callback)
core.register_on_player_receive_fields(function(player, formname, fields)
-- If the form name does not match the given...
if formName ~= formname then
-- ...we do nothing.
return
end
-- We go through each field in the list of fields.
for fieldName, fieldValue in pairs(fields) do
-- We check the field name pattern compliance.
local capturedName = string.match(fieldName, fieldNamePattern)
-- If the captured name is not nil...
if capturedName ~= nil then
-- ...calling a callback.
callback(player, fieldName, fieldValue)
end
end
end)
end