Compare commits
10 Commits
25f38d1acd
...
1d5f93c35a
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d5f93c35a | |||
| 46427f565f | |||
| 8fb4e4db00 | |||
| 9c56db6ac4 | |||
| 15bb906df7 | |||
| e9818fba24 | |||
| a6fa46bb96 | |||
| 44adcf9535 | |||
| 97292d6205 | |||
| f2af5e725a |
@@ -2,7 +2,6 @@
|
||||
local Control = {}
|
||||
|
||||
-- control.on_press
|
||||
Events.registerEvent('control.on_press')
|
||||
|
||||
---@param callbackFunc fun(player:Player, controlName:string)
|
||||
function Control.subcribeOnPress(callbackFunc)
|
||||
@@ -11,7 +10,6 @@ end
|
||||
|
||||
|
||||
-- control.on_release
|
||||
Events.registerEvent('control.on_release')
|
||||
|
||||
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number)
|
||||
function Control.subcribeOnRelease(callbackFunc)
|
||||
@@ -20,7 +18,6 @@ end
|
||||
|
||||
|
||||
-- control.on_hold
|
||||
Events.registerEvent('control.on_hold')
|
||||
|
||||
---@param callbackFunc fun(player:Player, controlName:string, holdTime:number, dtime:number)
|
||||
function Control.subcribeOnHold(callbackFunc)
|
||||
@@ -29,7 +26,6 @@ end
|
||||
|
||||
|
||||
-- control.on_wield_change
|
||||
Events.registerEvent('control.on_wield_change')
|
||||
|
||||
---@param callbackFunc fun(player:Player, player_wield_index:number, player_last_wield_index:number)
|
||||
function Control.subcribeOnWieldChange(callbackFunc)
|
||||
|
||||
@@ -5,8 +5,6 @@ local require = modInfo.require
|
||||
local PrimitiveCraftHandler = require('CraftSystem.PrimitiveCraftHandler')
|
||||
---@type CraftSystem.ImmersiveCraftHandler
|
||||
local ImmersiveCraftHandler= require('CraftSystem.ImmersiveCraftHandler')
|
||||
---@type CraftSystem.ImmersiveCraftNode
|
||||
local ImmersiveCraftNode= require('CraftSystem.ImmersiveCraftNode')
|
||||
|
||||
---@alias CraftSystem.CraftGroup string
|
||||
---@alias CraftSystem.CraftRecipe string[]
|
||||
@@ -18,13 +16,13 @@ local CraftSystem = {}
|
||||
---@param craftName string
|
||||
---@param def CraftSystem.PrimitiveCraftDef
|
||||
function CraftSystem.registerPrimitiveCraft(craftName, def)
|
||||
PrimitiveCraftHandler:addCraft(craftName, def)
|
||||
PrimitiveCraftHandler:registerCraft(craftName, def)
|
||||
end
|
||||
|
||||
---@param craftName string
|
||||
---@param def CraftSystem.ImmersiveCraftDef
|
||||
function CraftSystem.registerImmersiveCraft(craftName, def)
|
||||
ImmersiveCraftHandler:addCraft(craftName, def)
|
||||
ImmersiveCraftHandler:registerCraft(craftName, def)
|
||||
end
|
||||
|
||||
return CraftSystem
|
||||
|
||||
@@ -5,13 +5,19 @@ local require = modInfo.require
|
||||
|
||||
---@type CraftSystem.BaseCraft
|
||||
local BaseCraft = require('CraftSystem.BaseCraft')
|
||||
---@type CraftSystem.ImmersiveCraftStage
|
||||
local ImmersiveCraftStage = require('CraftSystem.ImmersiveCraftStage')
|
||||
|
||||
---Types of immersive stages.
|
||||
---@class CraftSystem.StagesTypes : table<CraftSystem.ImmersiveCraftStage.Type, CraftSystem.ImmersiveCraftStage>
|
||||
local stageTypes = {
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStage
|
||||
tapping = require('CraftSystem.ImmersiveCraftStage.TappingStage'),
|
||||
}
|
||||
|
||||
---Immersive crafting aimed at a more logical process of crafting items.
|
||||
---@class CraftSystem.ImmersiveCraft : CraftSystem.BaseCraft
|
||||
---@field startStageName string
|
||||
---@field triggerItem string
|
||||
---@field output string
|
||||
---@field output ItemStackString
|
||||
---@field stagesByName table<string, CraftSystem.ImmersiveCraftStage>
|
||||
local ImmersiveCraft = extend(BaseCraft, {})
|
||||
|
||||
@@ -28,24 +34,98 @@ local ImmersiveCraft = extend(BaseCraft, {})
|
||||
---@param def CraftSystem.ImmersiveCraftDef
|
||||
---@return CraftSystem.ImmersiveCraft
|
||||
function ImmersiveCraft:new(name, def)
|
||||
-- We create a table of crafting stages that the player needs to complete.
|
||||
---@type table<string, CraftSystem.ImmersiveCraftStage>
|
||||
local stagesByName = {}
|
||||
|
||||
for _, stageDef in ipairs(def.stages) do
|
||||
local stage = ImmersiveCraftStage:new(stageDef)
|
||||
-- We get the stage class based on its type.
|
||||
local StageClass = stageTypes[stageDef.type]
|
||||
|
||||
-- Create a stage and add the craft name to the instance.
|
||||
stageDef.craftName = name
|
||||
local stage = StageClass:new(stageDef)
|
||||
|
||||
-- We add an instance of the stage to the table by name.
|
||||
stagesByName[stageDef.name] = stage
|
||||
end
|
||||
|
||||
local instance = getInstance(self, {
|
||||
name = name,
|
||||
group = def.group,
|
||||
startStageName = def.startStageName,
|
||||
triggerItem = def.triggerItem,
|
||||
output = def.output,
|
||||
stagesByName = stagesByName,
|
||||
})
|
||||
---@type CraftSystem.ImmersiveCraft
|
||||
local instance = getInstance(self, {})
|
||||
instance.name = name
|
||||
instance.group = def.group
|
||||
instance.startStageName = def.startStageName
|
||||
instance.triggerItem = def.triggerItem
|
||||
instance.output = def.output
|
||||
instance.stagesByName = stagesByName
|
||||
|
||||
-- Subscribe to an event that will determine that the player has completed the crafting stage.
|
||||
Events.subscribe('craft_system:next_immersive_stage:'..name,
|
||||
---@param eventName string
|
||||
---@param stage CraftSystem.ImmersiveCraftStage
|
||||
---@param pos MapPosition
|
||||
---@param Player Player
|
||||
function(eventName, stage, pos, Player)
|
||||
-- From the current stage (which has been completed) we get the name of the next one.
|
||||
local nextStageName = stage.nextStage
|
||||
|
||||
-- If there is no next stage...
|
||||
if nextStageName == nil then
|
||||
-- ...we are finishing the craft.
|
||||
instance:endCraft(pos, Player)
|
||||
else
|
||||
-- ...else we start the next stage.
|
||||
local nextStage = instance.stagesByName[nextStageName]
|
||||
|
||||
-- We get a table of temporary data.
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
|
||||
-- We are updating the crafting stage in it.
|
||||
craftData.stageName = nextStageName
|
||||
-- We save it to the workstation metadata.
|
||||
Smc.setCraftDataByPos(pos, craftData)
|
||||
|
||||
-- We are starting the next stage of crafting.
|
||||
nextStage:startHandler(pos, Player)
|
||||
end
|
||||
end)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Initialize the start of the immersive crafting process.
|
||||
---@param pos MapPosition
|
||||
---@param Player Player
|
||||
function ImmersiveCraft:startCraft(pos, Player)
|
||||
-- TODO: забирать из инвентаря игрока материалы для крафта
|
||||
|
||||
-- We get the first stage of crafting.
|
||||
local stage = self.stagesByName[self.startStageName]
|
||||
|
||||
-- Create a temporary data table for crafting.
|
||||
---@type ImmersiveCraftData
|
||||
local craftData = {
|
||||
craftName = self.name,
|
||||
stageName = stage.name,
|
||||
}
|
||||
-- We save it to the workstation metadata.
|
||||
Smc.setCraftDataByPos(pos, craftData)
|
||||
|
||||
-- We are starting the first stage of crafting.
|
||||
stage:startHandler(pos, Player)
|
||||
end
|
||||
|
||||
---Complete the immersive crafting process.
|
||||
---@param pos MapPosition
|
||||
---@param Player Player
|
||||
function ImmersiveCraft:endCraft(pos, Player)
|
||||
-- Clearing the temporary data table for crafting, freeing up the work station for new crafts.
|
||||
Smc.setCraftDataByPos(pos)
|
||||
|
||||
-- We drop away the result of crafting.
|
||||
-- TODO: переписать определение этого метода в хелпере: itemstack не может быть строкой, а dropper опционален
|
||||
core.item_drop(ItemStack(self.output), nil, vector.new(pos)+vector.new(0, 0.9,0))
|
||||
end
|
||||
|
||||
return ImmersiveCraft
|
||||
|
||||
@@ -10,6 +10,8 @@ local BaseCraftHandler = require('CraftSystem.BaseCraftHandler')
|
||||
---@type CraftSystem.ImmersiveCraft
|
||||
local ImmersiveCraft = require('CraftSystem.ImmersiveCraft')
|
||||
|
||||
---A handler for immersive crafting that requires the player to complete
|
||||
---crafting stages and interact with entities (crafting nodes)
|
||||
---@class CraftSystem.ImmersiveCraftHandler : CraftSystem.BaseCraftHandler
|
||||
---@field craftsByTriggerItem table<string, CraftSystem.ImmersiveCraft[]>
|
||||
---@field getCraftByName fun(self, craftName:string):CraftSystem.ImmersiveCraft
|
||||
@@ -19,32 +21,45 @@ local ImmersiveCraftHandler = extend(BaseCraftHandler, {
|
||||
craftsByTriggerItem = {},
|
||||
})
|
||||
|
||||
---Register an immersive crafting experience that requires the player to complete
|
||||
---one or more crafting stages that involve interaction with entities (crafting nodes)
|
||||
---@param craftName string
|
||||
---@param def CraftSystem.ImmersiveCraftDef
|
||||
function ImmersiveCraftHandler:addCraft(craftName, def)
|
||||
function ImmersiveCraftHandler:registerCraft(craftName, def)
|
||||
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||
craftName))
|
||||
|
||||
-- We obtain a crafting instance according to the given parameters.
|
||||
local craft = ImmersiveCraft:new(craftName, def)
|
||||
|
||||
-- The craft group to which the craft belongs.
|
||||
local group = def.group
|
||||
|
||||
-- If no craft with this group has been registered before...
|
||||
if self.craftsByGroup[group] == nil then
|
||||
-- ...then we create a list for this group.
|
||||
self.craftsByGroup[group] = {}
|
||||
end
|
||||
|
||||
-- The material from which crafting begins (also known as the starting item).
|
||||
local triggerItem = def.triggerItem
|
||||
|
||||
-- If no crafting for such a starting item has been registered before...
|
||||
if self.craftsByTriggerItem[triggerItem] == nil then
|
||||
-- ...then we create a list for this item.
|
||||
self.craftsByTriggerItem[triggerItem] = {}
|
||||
end
|
||||
|
||||
-- Adding craft to the lists:
|
||||
self.craftsByName[craftName] = craft
|
||||
tableInsert(self.craftsByGroup[group], craft)
|
||||
tableInsert(self.craftsByTriggerItem[triggerItem], craft)
|
||||
tableInsert(self.craftsList, craft)
|
||||
end
|
||||
|
||||
---Returns a list of crafts that use the given item as a starting material.
|
||||
---
|
||||
---Will return `nil` if the item is not used in any craft as a starting item.
|
||||
---@param triggerItem string
|
||||
---@return CraftSystem.ImmersiveCraft[]?
|
||||
function ImmersiveCraftHandler:getCraftsByTriggerItem(triggerItem)
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
---@type Creature
|
||||
local Creature = Mod.getInfo('smc__core__entity').require('Creature')
|
||||
local Creature = Mod.getInfo('smc__core__creature').require('Creature')
|
||||
|
||||
---The entity the player interacts with during immersive crafting.
|
||||
---@class CraftSystem.ImmersiveCraftNode : Creature
|
||||
local ImmersiveCraftNode = Creature:getExtended({
|
||||
settings = {
|
||||
_name = 'craft_system:immersive_craft_node',
|
||||
_add_mod_name = false,
|
||||
initial_properties = {
|
||||
|
||||
}
|
||||
initial_properties = {},
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
local extend = Class.extend
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||
local require = modInfo.require
|
||||
|
||||
---@type CraftSystem.ImmersiveCraftNode
|
||||
local ImmersiveCraftNode = require('CraftSystem.ImmersiveCraftNode')
|
||||
|
||||
---@class CraftSystem.ImmersiveCraftNode.TapperNode : CraftSystem.ImmersiveCraftNode
|
||||
local TapperNode = extend(ImmersiveCraftNode, {})
|
||||
|
||||
return TapperNode
|
||||
@@ -0,0 +1,78 @@
|
||||
local modInfo = Mod.getInfo('smc__core__craft_system')
|
||||
local require = modInfo.require
|
||||
|
||||
---@type CraftSystem.ImmersiveCraftNode
|
||||
local ImmersiveCraftNode = require('CraftSystem.ImmersiveCraftNode')
|
||||
|
||||
---A class that extends `Creature.LuaEntity` to annotate custom fields needed for immersive crafting.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class CraftSystem.ImmersiveCraftNode.LuaTappingNode : Creature.LuaEntity
|
||||
---The name of the item that the crafting node display.
|
||||
---@field _item_name ItemStackString
|
||||
---Sets the entity display of the item. If the item stack was not transferred, `self._item_name` is used.
|
||||
---@field _set_item fun(self:CraftSystem.ImmersiveCraftNode.LuaTappingNode, itemName:ItemStackString?)
|
||||
---@field _craft_name string
|
||||
---@field _stage_name string
|
||||
---@field _work_station_pos MapPosition
|
||||
|
||||
-- A crafting node (entity) that is used in the tapping crafting stage.
|
||||
local tappingNode = ImmersiveCraftNode:new({
|
||||
settings = {
|
||||
_name = 'craft_system:tapping_node',
|
||||
_add_mod_name = false,
|
||||
|
||||
initial_properties = {
|
||||
visual = 'wielditem',
|
||||
visual_size = {x=0.4, y=0.4, z=0.4},
|
||||
static_save = false,
|
||||
},
|
||||
|
||||
_item_name = '',
|
||||
_craft_name = '',
|
||||
_stage_name = '',
|
||||
_work_station_pos = {},
|
||||
},
|
||||
callbacks = {
|
||||
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||
on_activate = function(luaEntity, staticdata, dtime)
|
||||
-- We obtain a table of initial parameters for the craft node entity.
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingNode.InitParams?
|
||||
local params = core.deserialize(staticdata)
|
||||
|
||||
-- We enter parameters into the entity's custom fields.
|
||||
if params ~= nil then
|
||||
luaEntity._item_name = params.targetItem
|
||||
luaEntity._craft_name = params.craftName
|
||||
luaEntity._stage_name = params.stageName
|
||||
luaEntity._work_station_pos = params.WorkStationPos
|
||||
end
|
||||
|
||||
-- we set the appearance of the item to be processed on the crafting node.
|
||||
luaEntity:_set_item(luaEntity._item_name)
|
||||
-- We add an immortal group to the entity so that it cannot be killed.
|
||||
luaEntity.object:set_armor_groups({immortal = 1})
|
||||
end,
|
||||
|
||||
---@param luaEntity CraftSystem.ImmersiveCraftNode.LuaTappingNode
|
||||
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)
|
||||
|
||||
return false
|
||||
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?
|
||||
_set_item = function(luaEntity, itemName)
|
||||
-- We change the entity field so that the engine itself displays it as a given item.
|
||||
luaEntity.object:set_properties({
|
||||
wield_item = itemName or luaEntity._item_name,
|
||||
})
|
||||
end,
|
||||
}
|
||||
})
|
||||
|
||||
return tappingNode
|
||||
@@ -1,36 +1,39 @@
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
---@alias CraftSystem.ImmersiveCraftStage.HandlerFunc fun(self, Player)
|
||||
---@alias CraftSystem.ImmersiveCraftStage.Type 'tapping'
|
||||
|
||||
---One of the stages of immersive crafting.
|
||||
---@class CraftSystem.ImmersiveCraftStage
|
||||
---@field name string
|
||||
---@field startHandler CraftSystem.ImmersiveCraftStage.HandlerFunc
|
||||
---@field processHandler CraftSystem.ImmersiveCraftStage.HandlerFunc
|
||||
---@field endHandler CraftSystem.ImmersiveCraftStage.HandlerFunc
|
||||
---@field type CraftSystem.ImmersiveCraftStage.Type
|
||||
---@field craftName string
|
||||
---@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 nextStage string?
|
||||
---@field returnItems table? TODO: доделать тут тип
|
||||
local ImmersiveCraftStage = {
|
||||
data = {}
|
||||
}
|
||||
local ImmersiveCraftStage = {}
|
||||
|
||||
---Definition table for the `CraftSystem.ImmersiveCraft`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class CraftSystem.ImmersiveCraftStageDef
|
||||
---@field name string
|
||||
---@field startHandler CraftSystem.ImmersiveCraftStage.HandlerFunc
|
||||
---@field processHandler CraftSystem.ImmersiveCraftStage.HandlerFunc
|
||||
---@field endHandler CraftSystem.ImmersiveCraftStage.HandlerFunc
|
||||
---@field returnItems table? TODO: доделать тут тип
|
||||
---@field name string
|
||||
---@field type CraftSystem.ImmersiveCraftStage.Type
|
||||
---@field craftName string?
|
||||
---@field nextStage string?
|
||||
---@field returnItems table? TODO: доделать тут тип
|
||||
|
||||
---@param def CraftSystem.ImmersiveCraftStageDef
|
||||
---@return CraftSystem.ImmersiveCraftStage
|
||||
---@param def CraftSystem.ImmersiveCraftStageDef
|
||||
---@return CraftSystem.ImmersiveCraftStage
|
||||
function ImmersiveCraftStage:new(def)
|
||||
local instance = getInstance(self, {
|
||||
name = def.name,
|
||||
startHandler = def.startHandler,
|
||||
processHandler = def.processHandler,
|
||||
endHandler = def.endHandler
|
||||
})
|
||||
---@type CraftSystem.ImmersiveCraftStage
|
||||
local instance = getInstance(self, {})
|
||||
instance.name = def.name
|
||||
instance.type = def.type
|
||||
instance.craftName = def.craftName
|
||||
instance.nextStage = def.nextStage
|
||||
instance.returnItems = def.returnItems
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
local getInstance, extend = Class.getInstance, Class.extend
|
||||
|
||||
---@type CraftSystem.ImmersiveCraftStage
|
||||
local ImmersiveCraftStage = Mod.getInfo('smc__core__craft_system').require('ImmersiveCraftStage')
|
||||
|
||||
---@class CraftSystem.ImmersiveCraftStage.Tapping : CraftSystem.ImmersiveCraftStage
|
||||
---@field targetItem string
|
||||
---@field startHandler fun(self:CraftSystem.ImmersiveCraftStage.Tapping, pos:Position, Player:Player)
|
||||
---@field processHandler fun(self:CraftSystem.ImmersiveCraftStage.Tapping, pos:Position, Player:Player)
|
||||
---@field endHabdler fun(self:CraftSystem.ImmersiveCraftStage.Tapping, pos:Position, Player:Player)
|
||||
---@field entity Entity
|
||||
local Tapping = extend(ImmersiveCraftStage, {})
|
||||
|
||||
---@class CraftSystem.ImmersiveCraftStage.TappingDef
|
||||
---@field targetItem string
|
||||
|
||||
---@param def CraftSystem.ImmersiveCraftStage.TappingDef
|
||||
function Tapping:new(def)
|
||||
local instance = getInstance(self, {})
|
||||
|
||||
instance.targetItem = def.targetItem
|
||||
|
||||
instance.startHandler = function(self, pos, Player)
|
||||
|
||||
end
|
||||
|
||||
instance.processHandler = function(self, pos, Player)
|
||||
|
||||
end
|
||||
|
||||
instance.endHabdler = function(self, pos, Player)
|
||||
|
||||
end
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
return Tapping
|
||||
@@ -0,0 +1,130 @@
|
||||
local extend = Class.extend
|
||||
|
||||
local getStageDataByPos, setStageDataByPos = Smc.getStageDataByPos, Smc.setStageDataByPos
|
||||
|
||||
local require = Mod.getInfo('smc__core__craft_system').require
|
||||
|
||||
---@type CraftSystem.ImmersiveCraftStage
|
||||
local ImmersiveCraftStage = require('CraftSystem.ImmersiveCraftStage')
|
||||
|
||||
---A crafting step that requires the player to hit a crafting node (entity) a certain number of times.
|
||||
---@class CraftSystem.ImmersiveCraftStage.TappingStage : CraftSystem.ImmersiveCraftStage
|
||||
---@field targetItem string
|
||||
---@field clicksNeed string
|
||||
---@field startHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
||||
---@field processHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
||||
---@field endHandler fun(self:CraftSystem.ImmersiveCraftStage.TappingStage, pos:MapPosition, Player:Player)
|
||||
local TappingStage = extend(ImmersiveCraftStage, {})
|
||||
|
||||
---Definition table for the `CraftSystem.ImmersiveCraftStage.TappingStage`.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class CraftSystem.ImmersiveCraftStage.TappingStageDef : CraftSystem.ImmersiveCraftStageDef
|
||||
---@field targetItem string
|
||||
---@field clicksNeed string
|
||||
|
||||
---Data table for transferring various time parameters between calls.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class ImmersiveStageData.TappingStageData : ImmersiveStageData
|
||||
---How many clicks were made by the player on the entity.
|
||||
---@field clickCount integer
|
||||
---Unique identifier of the entity (craft node).
|
||||
---@field entityGUID string
|
||||
|
||||
---@param def CraftSystem.ImmersiveCraftStage.TappingStageDef
|
||||
function TappingStage:new(def)
|
||||
---@type CraftSystem.ImmersiveCraftStage.TappingStage
|
||||
local instance = ImmersiveCraftStage:new(def)
|
||||
instance.targetItem = def.targetItem
|
||||
instance.clicksNeed = def.clicksNeed
|
||||
|
||||
-- Registering the crafting stage start handler.
|
||||
instance.startHandler = function(tappingStage , pos, Player)
|
||||
-- We determine the coordinate where we will create the crafting node (entity).
|
||||
local entityPos = vector.new(pos) + vector.new(0,0.9,0)
|
||||
|
||||
-- We create a table of initial parameters that we will pass to the entity.
|
||||
---@class CraftSystem.ImmersiveCraftStage.TappingNode.InitParams
|
||||
local params = {
|
||||
targetItem = tappingStage.targetItem,
|
||||
craftName = tappingStage.craftName,
|
||||
stageName = tappingStage.name,
|
||||
WorkStationPos = pos,
|
||||
}
|
||||
|
||||
-- Adding an entity to the world.
|
||||
local obj = core.add_entity(entityPos, 'craft_system:tapping_node', core.serialize(params))
|
||||
|
||||
-- If the entity was not returned, something went wrong.
|
||||
if obj == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- Create a temporary table of crafting stage data.
|
||||
---@type ImmersiveStageData.TappingStageData
|
||||
local stageData = {
|
||||
clickCount = 0,
|
||||
entityGUID = obj:get_guid()
|
||||
}
|
||||
|
||||
-- Saving temporary data in work station metadata.
|
||||
setStageDataByPos(pos, stageData)
|
||||
print(pos, dump(Smc.getCraftDataByPos(pos)), 'ONE')
|
||||
end
|
||||
|
||||
-- Registering the crafting stage process handler.
|
||||
-- Called when a player clicks on a crafting node (entity).
|
||||
instance.processHandler = function(tappingStage, pos, Player)
|
||||
-- Obtaining stage time data from work station metadata.
|
||||
---@type ImmersiveStageData.TappingStageData?
|
||||
local stageData = getStageDataByPos(pos)
|
||||
print(dump(pos), dump(Smc.getCraftDataByPos(pos)), 'TWO')
|
||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||
|
||||
-- Increasing the click counter
|
||||
local clickCount = stageData.clickCount
|
||||
clickCount = clickCount + 1
|
||||
|
||||
-- If the number of clicks is greater than required to complete the step...
|
||||
if clickCount >= tappingStage.clicksNeed then
|
||||
-- ...we are completing the stage.
|
||||
tappingStage:endHandler(pos, Player)
|
||||
else
|
||||
-- ...else we save the number of clicks in the temporary data table.
|
||||
stageData.clickCount = clickCount
|
||||
Smc.setStageDataByPos(pos, stageData)
|
||||
end
|
||||
end
|
||||
|
||||
-- Registering the crafting stage end handler.
|
||||
instance.endHandler = function(tappingStage, pos, Player)
|
||||
-- Obtaining stage time data from work station metadata.
|
||||
---@type ImmersiveStageData.TappingStageData?
|
||||
local stageData = getStageDataByPos(pos)
|
||||
assert(stageData ~= nil, 'Immersive stage data is empty.')
|
||||
|
||||
-- We get the entity object.
|
||||
local entity = Smc.getObjectByGUID(stageData.entityGUID)
|
||||
assert(entity ~= nil, 'The entity for immersive crafting is nil.')
|
||||
|
||||
-- Removing an entity from the world.
|
||||
entity:remove()
|
||||
|
||||
-- We trigger an event that the crafting stage has been completed.
|
||||
Events.call('craft_system:next_immersive_stage:'..tappingStage.craftName, tappingStage, pos, Player)
|
||||
end
|
||||
|
||||
-- Subscribe to an event that means the player hit the entity.
|
||||
Events.subscribe(('craft_system:punch_tapping_node:%s:%s'):format(instance.craftName, instance.name),
|
||||
function(eventName, pos, Player)
|
||||
instance:processHandler(pos, Player)
|
||||
end)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
-- Registering an entity for immersive tapping crafting.
|
||||
require('CraftSystem.ImmersiveCraftNode.tappingNode')
|
||||
|
||||
return TappingStage
|
||||
@@ -24,6 +24,7 @@ local PrimitiveCraft = extend(BaseCraft, {})
|
||||
---@param def CraftSystem.PrimitiveCraftDef
|
||||
---@return CraftSystem.PrimitiveCraft
|
||||
function PrimitiveCraft:new(name, def)
|
||||
---@type CraftSystem.PrimitiveCraft
|
||||
local instance = getInstance(self, {
|
||||
name = name,
|
||||
group = def.group,
|
||||
|
||||
@@ -11,15 +11,18 @@ local BaseCraftHandler = require('CraftSystem.BaseCraftHandler')
|
||||
---@type CraftSystem.PrimitiveCraft
|
||||
local PrimitiveCraft = require('CraftSystem.PrimitiveCraft')
|
||||
|
||||
---A primitive crafting processor that doesn't require much interaction and provides
|
||||
---simple crafting based on a recipe-result scheme.
|
||||
---@class CraftSystem.PrimitiveCraftHandler : CraftSystem.BaseCraftHandler
|
||||
---@field getCraftByName fun(self, craftName:string):CraftSystem.PrimitiveCraft
|
||||
---@field getCraftsByGroup fun(self, craftGroup):CraftSystem.PrimitiveCraft[]
|
||||
---@field getLearnedCrafts fun(self, playerName):table<string, CraftSystem.PrimitiveCraft>
|
||||
local PrimitiveCraftHandler = extend(BaseCraftHandler, {})
|
||||
|
||||
---Register a primitive craft that does not require special interaction.
|
||||
---@param craftName string
|
||||
---@param def CraftSystem.PrimitiveCraftDef
|
||||
function PrimitiveCraftHandler:addCraft(craftName, def)
|
||||
function PrimitiveCraftHandler:registerCraft(craftName, def)
|
||||
assert(self.craftsByName[craftName] == nil, ('A craft named `%s` already exists.'):format(
|
||||
craftName))
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ function Creature:new(entityDef)
|
||||
|
||||
-- We rewrite the entity initialization function so that custom fields
|
||||
-- can be passed to it directly via `core.add_entity(pos, name, [staticdata])`.
|
||||
c.on_activate = function(self, staticdata, dtime)
|
||||
c.on_activate = function(luaEntity, staticdata, dtime)
|
||||
local initCustomFields = {}
|
||||
|
||||
if staticdata and staticdata ~= '' then
|
||||
@@ -71,7 +71,7 @@ function Creature:new(entityDef)
|
||||
|
||||
-- If an initialization callback was specified for the entity, then we call it.
|
||||
if c.on_activate ~= nil then
|
||||
onActivate(self, staticdata, dtime)
|
||||
onActivate(luaEntity, staticdata, dtime)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -83,7 +83,7 @@ function Creature:new(entityDef)
|
||||
instance.entityDef = entityDef
|
||||
instance.luantiDef = luantiDef
|
||||
|
||||
core.register_entity(s._name, luantiDef)
|
||||
core.register_entity(':'..s._name, luantiDef)
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
9
mods/CORE/Creature/src/Creature/LuaEntity.lua
Normal file
9
mods/CORE/Creature/src/Creature/LuaEntity.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
---A class that extends `LuaEntity` to annotate custom fields.
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class Creature.LuaEntity : LuaEntity
|
||||
---@field _name string
|
||||
---@field _add_mod_name boolean
|
||||
local LuaEntity = {}
|
||||
|
||||
return LuaEntity
|
||||
@@ -193,7 +193,7 @@ end
|
||||
---@param defaultDef Item.ItemDefinition
|
||||
---@return Item
|
||||
function Item:getExtended(defaultDef)
|
||||
defaultDef = defMerge(self.defaultDef, defaultDef)
|
||||
defaultDef = defMerge(defaultDef, self.defaultDef)
|
||||
|
||||
---@type Item
|
||||
local ChildClass = extend(self, {
|
||||
|
||||
@@ -133,7 +133,7 @@ end
|
||||
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
|
||||
---@param layer string|MapGen.Layer The layer or name of the layer in which the peak is located.
|
||||
---@param category MapGen.Layer.Category The category in which the peak is located.
|
||||
---@param pos Position Coordinate of the desired peak.
|
||||
---@param pos MapPosition Coordinate of the desired peak.
|
||||
---@return MapGen.Peak?
|
||||
function MapGen:getPeakByPos(layer, category, pos)
|
||||
if type(layer) == 'string' then
|
||||
@@ -177,7 +177,7 @@ end
|
||||
---**Important:** works only in the XZ plane.
|
||||
---@param layer string|MapGen.Layer The layer or name of the layer the triangle is in.
|
||||
---@param category MapGen.Layer.Category The category in which the triangle is in.
|
||||
---@param pos Position Coordinate of the desired peak.
|
||||
---@param pos MapPosition Coordinate of the desired peak.
|
||||
---@return MapGen.Triangle?
|
||||
function MapGen:getTriangleByPos2D(layer, category, pos)
|
||||
if type(layer) == 'string' then
|
||||
@@ -196,7 +196,7 @@ end
|
||||
---If the point is inside the region, it will also return the distance to the nearest border.
|
||||
---@param layer MapGen.Layer The name of the layer in which the region is located.
|
||||
---@param regionName string The name of the region in which the location of the point is checked.
|
||||
---@param point Position Point position.
|
||||
---@param point MapPosition Point position.
|
||||
---@return boolean, number?
|
||||
function MapGen:pointInRegion2D(layer, regionName, point)
|
||||
local region = layer.regionsByName[regionName]
|
||||
@@ -315,7 +315,7 @@ function MapGen:getTriangulateBoundaryVertices(triangles)
|
||||
end
|
||||
|
||||
-- Walk along boundary edges to create ordered vertex list
|
||||
---@type Position[]
|
||||
---@type MapPosition[]
|
||||
local boundary = {}
|
||||
---@type table<string, boolean>
|
||||
local visited = {}
|
||||
@@ -425,9 +425,9 @@ end
|
||||
function MapGen:registerPeak(layerName, category, def)
|
||||
local layer = self:getLayerByName(layerName)
|
||||
|
||||
assert(self:getPeakByPos(layer, category, def.pos) == nil,
|
||||
('In the `%s` layer in the `%s` category there is already a peak with coordinates: %s, %s, %s.'):format(
|
||||
layerName, category, def.pos.x, def.pos.y, def.pos.z))
|
||||
--assert(self:getPeakByPos(layer, category, def.pos) == nil,
|
||||
--('In the `%s` layer in the `%s` category there is already a peak with coordinates: %s, %s, %s.'):format(
|
||||
-- layerName, category, def.pos.x, def.pos.y, def.pos.z))
|
||||
|
||||
---@type MapGen.Peak
|
||||
local peak = Peak:new(def)
|
||||
@@ -473,7 +473,7 @@ end
|
||||
---@param layerName string The name of the layer in which the new peaks will be included.
|
||||
---@param map string The name of the image from which the peaks will be extracted. It should be located in the `<mod_name>/textures/maps` folder and have the following format `<image_name>.ff`
|
||||
---@param scale number Multiplier of how many blocks (nodes) will be between image pixels. It is not recommended to set the value below 50 for performance reasons.
|
||||
---@param zeroPos Position2d The position in pixels where the zero world coordinate will be in the image.
|
||||
---@param zeroPos IntegerPosition2d The position in pixels where the zero world coordinate will be in the image.
|
||||
---@param category MapGen.Layer.Category Category indicating what will be generated by peaks.
|
||||
---@param groups? table<string, number> Groups that will be added to all peaks.
|
||||
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
|
||||
@@ -495,16 +495,17 @@ function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category,
|
||||
|
||||
for color, peaksPos in pairs(peaks) do
|
||||
for _, peakPos in ipairs(peaksPos) do
|
||||
---@type integer, integer
|
||||
x, z = (peakPos.x - zeroPos.x - 1) * scale, (peakPos.y - zeroPos.y - 1) * -scale
|
||||
|
||||
-- Converting a rectangular grid into an isometric-like one.
|
||||
if peakPos.y % 2 == 1 then
|
||||
x = x + (scale/2)
|
||||
x = mathRound(x + (scale/2))
|
||||
end
|
||||
|
||||
---@type MapGen.PeakDef
|
||||
local def = {
|
||||
pos = vector.new(x, 0, z),
|
||||
pos = {x=x, y=0, z=z},
|
||||
color = color,
|
||||
groups = groups,
|
||||
}
|
||||
@@ -518,7 +519,7 @@ end
|
||||
---If the peak does not exist, an error will be raised.
|
||||
---@param layerName string The name of the layer in which the new peak will be included.
|
||||
---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
|
||||
---@param pos Position The position of the peak to which the group should be added.
|
||||
---@param pos MapPosition The position of the peak to which the group should be added.
|
||||
---@param groups table<string, number> Groups to be added.
|
||||
function MapGen:addGroupsToPeakByPos(layerName, category, pos, groups)
|
||||
local layer = self:getLayerByName(layerName)
|
||||
@@ -546,7 +547,7 @@ end
|
||||
---@param layerName string The name of the layer in which the new cavern will be included.
|
||||
---@param cavernName string The name to be assigned to the cavern. The name must be unique for each cavern.
|
||||
---@param def MapGen.CavernDef Cavern description table, see details `MapGen.CavernDef`
|
||||
---@param regionVertices Position[]? The region to which the cave generation will be limited.
|
||||
---@param regionVertices MapPosition[]? The region to which the cave generation will be limited.
|
||||
function MapGen:registerCavern(layerName, cavernName, def, regionVertices)
|
||||
local layer = self:getLayerByName(layerName)
|
||||
|
||||
@@ -1023,8 +1024,8 @@ end
|
||||
|
||||
---Chunk generation handler.
|
||||
---@param voxelManip VoxelManip
|
||||
---@param minPos Position
|
||||
---@param maxPos Position
|
||||
---@param minPos MapPosition
|
||||
---@param maxPos MapPosition
|
||||
---@param blockseed number
|
||||
function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
||||
-- We obtain a generation area for further manipulations
|
||||
|
||||
@@ -16,14 +16,14 @@ local Peak = {
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class MapGen.PeakDef
|
||||
---@field pos vector
|
||||
---@field pos MapPosition|vector
|
||||
---@field color ColorString
|
||||
---@field groups? table<string, number>
|
||||
|
||||
---@param def MapGen.PeakDef
|
||||
---@return MapGen.Peak
|
||||
function Peak:new(def)
|
||||
local _pos = def.pos
|
||||
local _pos = vector.new(def.pos)
|
||||
local _color = def.color
|
||||
local _groups
|
||||
|
||||
|
||||
@@ -4,11 +4,11 @@ local getInstance = Class.getInstance
|
||||
|
||||
---@class MapGen.Region
|
||||
---@field name string
|
||||
---@field vertices Position[]
|
||||
---@field vertices MapPosition[]
|
||||
local Region = {}
|
||||
|
||||
---@class MapGen.RegionDef
|
||||
---@field vertices Position[]
|
||||
---@field vertices MapPosition[]
|
||||
|
||||
---@param def MapGen.RegionDef
|
||||
---@return MapGen.Region
|
||||
@@ -50,7 +50,7 @@ local function calcDistanceSq(px, pz, x1, z1, x2, z2)
|
||||
end
|
||||
|
||||
---Checks if a point is inside a `MapGen.Region` (2D check using x and z coordinates).
|
||||
---@param point Position
|
||||
---@param point MapPosition
|
||||
---@return boolean, number?
|
||||
function Region:pointInRegion(point)
|
||||
local vertices = self.vertices
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
---@diagnostic disable: param-type-not-match
|
||||
local mathAbs = math.abs
|
||||
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
---@diagnostic disable: param-type-not-match
|
||||
-- Tringulation lib
|
||||
-- Original code: https://github.com/iskolbin/Triangulation/blob/master/Triangulation.lua
|
||||
-- Date of copying: 07.11.2025
|
||||
|
||||
@@ -14,7 +14,7 @@ local FakePeak = Class.extend(Peak, {})
|
||||
|
||||
---Returns a fake `MapGen.Peak` that has no noise parameters.
|
||||
---Note that the initialized peak should not be used in generation.
|
||||
---@param peakPos vector
|
||||
---@param peakPos MapPosition|vector
|
||||
---@return MapGen.Peak
|
||||
function FakePeak:new(peakPos)
|
||||
local instance = Peak:new({pos = peakPos, color = '#000000'})
|
||||
|
||||
@@ -35,7 +35,7 @@ end
|
||||
---@param defaultDef Node.NodeDefinition
|
||||
---@return Node
|
||||
function Node:getExtended(defaultDef)
|
||||
defaultDef = defMerge(self.defaultDef, defaultDef)
|
||||
defaultDef = defMerge(defaultDef, self.defaultDef)
|
||||
|
||||
---@type Node
|
||||
local ChildClass = Class.extend(self, {
|
||||
|
||||
@@ -5,20 +5,16 @@ Events = {}
|
||||
---@type table<string, Events.callback[]>
|
||||
local registeredEvents = {}
|
||||
|
||||
---@param eventName string
|
||||
function Events.registerEvent(eventName)
|
||||
assert(registeredEvents[eventName] == nil, ('Event %s is already registered.'):format(eventName))
|
||||
|
||||
registeredEvents[eventName] = {}
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param callback Events.callback
|
||||
function Events.subscribe(eventName, callback)
|
||||
local callbacks = registeredEvents[eventName]
|
||||
assert(callbacks ~= nil, ('Event %s does not exist'):format(eventName))
|
||||
|
||||
table.insert(callbacks, callback)
|
||||
if callbacks == nil then
|
||||
registeredEvents[eventName] = {callback}
|
||||
else
|
||||
table.insert(callbacks, callback)
|
||||
end
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
@@ -30,4 +26,4 @@ function Events.call(eventName, ...)
|
||||
for _, func in ipairs(callbacks) do
|
||||
func(eventName, ...)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,3 +5,5 @@ local require = modInfo.require
|
||||
|
||||
require('Lib.globalStep')
|
||||
require('Lib.color')
|
||||
require('Lib.immersiveCraftData')
|
||||
require('Lib.getObjectByGUID')
|
||||
16
mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua
Normal file
16
mods/SYSTEM/HelperLib/src/Lib/getObjectByGUID.lua
Normal file
@@ -0,0 +1,16 @@
|
||||
---Returns an active object by unique identifier (GUID).
|
||||
---* For players, this is a player name.
|
||||
---* For Lua entities, this is a uniquely generated string, guaranteed not to collide with player names.
|
||||
---
|
||||
---**Note**: If an object with such an identifier does not exist or is inactive, the value `nil` will be returned.
|
||||
---@param guid string only use printable ASCII characters. Example: `@bGh3p2AbRE29Mb4biqX6OA`.
|
||||
---@return ObjectRef|Player|Entity|nil
|
||||
function Smc.getObjectByGUID(guid)
|
||||
local obj = core.objects_by_guid[guid]
|
||||
|
||||
if obj == nil then
|
||||
return
|
||||
else
|
||||
return obj
|
||||
end
|
||||
end
|
||||
92
mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua
Normal file
92
mods/SYSTEM/HelperLib/src/Lib/immersiveCraftData.lua
Normal file
@@ -0,0 +1,92 @@
|
||||
local EMPTY_FLAG = 'EMPTY_FLAG'
|
||||
|
||||
---@class ImmersiveStageData
|
||||
|
||||
---@class ImmersiveCraftData
|
||||
---@field craftName string,
|
||||
---@field stageName string,
|
||||
---@field stageData ImmersiveStageData?
|
||||
|
||||
---Returns craft data for immersive crafting, which is stored in the desktop metadata.
|
||||
---@param meta NodeMetaRef
|
||||
---@return ImmersiveCraftData?
|
||||
function Smc.getCraftDataByMeta(meta)
|
||||
local dataString = meta:get_string('craft_data')
|
||||
|
||||
if dataString == EMPTY_FLAG then
|
||||
return
|
||||
end
|
||||
|
||||
return core.deserialize(dataString)
|
||||
end
|
||||
|
||||
---Sets craft data for immersive crafting that is stored in the work station metadata.
|
||||
---
|
||||
---If `data` is `nil`, the data is cleared.
|
||||
---@param meta NodeMetaRef
|
||||
---@param craftData ImmersiveCraftData?
|
||||
function Smc.setCraftDataByMeta(meta, craftData)
|
||||
if craftData == nil then
|
||||
meta:set_string('craft_data', EMPTY_FLAG)
|
||||
end
|
||||
|
||||
meta:set_string('craft_data', core.serialize(craftData))
|
||||
end
|
||||
|
||||
---Returns craft data for immersive crafting, which is stored in the work station metadata.
|
||||
---@param pos MapPosition
|
||||
---@return ImmersiveCraftData?
|
||||
function Smc.getCraftDataByPos(pos)
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
return Smc.getCraftDataByMeta(meta)
|
||||
end
|
||||
|
||||
---Sets craft data for immersive crafting that is stored in the work station metadata.
|
||||
---
|
||||
---If `data` is `nil`, the data is cleared.
|
||||
---@param pos MapPosition
|
||||
---@param craftData ImmersiveCraftData?
|
||||
function Smc.setCraftDataByPos(pos, craftData)
|
||||
local meta = core.get_meta(pos)
|
||||
|
||||
Smc.setCraftDataByMeta(meta, craftData)
|
||||
end
|
||||
|
||||
---Returns stage data for immersive crafting, which is stored in the work station metadata.
|
||||
---
|
||||
---**Note**: If the workbench does not have crafting data, an error will be thrown.
|
||||
---@param pos MapPosition
|
||||
---@return ImmersiveStageData?
|
||||
function Smc.getStageDataByPos(pos)
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
|
||||
return craftData.stageData
|
||||
end
|
||||
|
||||
---Sets stage data for immersive crafting that is stored in the work station metadata.
|
||||
---@param pos MapPosition
|
||||
---@param stageData ImmersiveStageData
|
||||
function Smc.setStageDataByPos(pos, stageData)
|
||||
local craftData = Smc.getCraftDataByPos(pos)
|
||||
assert(craftData ~= nil, 'Immersive crafting data is empty.')
|
||||
|
||||
craftData.stageData = stageData
|
||||
Smc.setCraftDataByPos(pos, craftData)
|
||||
end
|
||||
|
||||
---Checks if the workstation is busy or free for new crafting.
|
||||
---
|
||||
---The verification is based on `ImmersiveCraftData`.
|
||||
---@param pos MapPosition
|
||||
---@return boolean
|
||||
function Smc.workStationIsFree(pos)
|
||||
local data = Smc.getCraftDataByPos(pos)
|
||||
|
||||
if data == nil then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
Submodule util/ide-helper updated: e5a12d31ba...f2e3431251
Reference in New Issue
Block a user