2021-12-05 17:54:36 +00:00
# include "ScriptedActivityComponent.h"
# include "GameMessages.h"
# include "CDClientManager.h"
# include "MissionComponent.h"
# include "Character.h"
# include "dZoneManager.h"
# include "ZoneInstanceManager.h"
# include "Game.h"
# include "dLogger.h"
# include <WorldPackets.h>
# include "EntityManager.h"
# include "ChatPackets.h"
# include "Player.h"
# include "PacketUtils.h"
# include "dServer.h"
# include "GeneralUtils.h"
# include "dZoneManager.h"
# include "dConfig.h"
2022-12-18 15:46:04 +00:00
# include "InventoryComponent.h"
2021-12-24 00:25:52 +00:00
# include "DestroyableComponent.h"
2023-01-07 05:17:05 +00:00
# include "Loot.h"
2023-01-22 23:38:47 +00:00
# include "eMissionTaskType.h"
2023-05-02 22:39:21 +00:00
# include "eMatchUpdate.h"
2023-05-03 21:38:32 +00:00
# include "eConnectionType.h"
# include "eChatInternalMessageType.h"
2021-12-05 17:54:36 +00:00
2023-03-17 14:36:21 +00:00
# include "CDCurrencyTableTable.h"
# include "CDActivityRewardsTable.h"
# include "CDActivitiesTable.h"
2023-05-08 09:38:08 +00:00
# include "LeaderboardManager.h"
2023-03-17 14:36:21 +00:00
2022-07-28 13:39:57 +00:00
ScriptedActivityComponent : : ScriptedActivityComponent ( Entity * parent , int activityID ) : Component ( parent ) {
2022-12-04 22:25:58 +00:00
m_ActivityID = activityID ;
2023-03-17 14:36:21 +00:00
CDActivitiesTable * activitiesTable = CDClientManager : : Instance ( ) . GetTable < CDActivitiesTable > ( ) ;
2022-12-04 22:25:58 +00:00
std : : vector < CDActivities > activities = activitiesTable - > Query ( [ = ] ( CDActivities entry ) { return ( entry . ActivityID = = m_ActivityID ) ; } ) ;
2021-12-05 17:54:36 +00:00
for ( CDActivities activity : activities ) {
m_ActivityInfo = activity ;
2023-05-08 09:38:08 +00:00
if ( static_cast < LeaderboardType > ( activity . leaderboardType ) = = LeaderboardType : : Racing & & Game : : config - > GetValue ( " solo_racing " ) = = " 1 " ) {
2022-07-28 13:39:57 +00:00
m_ActivityInfo . minTeamSize = 1 ;
m_ActivityInfo . minTeams = 1 ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
const auto & transferOverride = parent - > GetVar < std : : u16string > ( u " transferZoneID " ) ;
if ( ! transferOverride . empty ( ) ) {
m_ActivityInfo . instanceMapID = std : : stoi ( GeneralUtils : : UTF16ToWTF8 ( transferOverride ) ) ;
2021-12-05 17:54:36 +00:00
// TODO: LU devs made me do it (for some reason cannon cove instancer is marked to go to GF survival)
// NOTE: 1301 is GF survival
if ( m_ActivityInfo . instanceMapID = = 1301 ) {
2022-07-28 13:39:57 +00:00
m_ActivityInfo . instanceMapID = 1302 ;
2021-12-05 17:54:36 +00:00
}
}
}
2021-12-24 00:25:52 +00:00
auto * destroyableComponent = m_Parent - > GetComponent < DestroyableComponent > ( ) ;
if ( destroyableComponent ) {
// check for LMIs and set the loot LMIs
2023-03-17 14:36:21 +00:00
CDActivityRewardsTable * activityRewardsTable = CDClientManager : : Instance ( ) . GetTable < CDActivityRewardsTable > ( ) ;
2021-12-24 00:25:52 +00:00
std : : vector < CDActivityRewards > activityRewards = activityRewardsTable - > Query ( [ = ] ( CDActivityRewards entry ) { return ( entry . LootMatrixIndex = = destroyableComponent - > GetLootMatrixID ( ) ) ; } ) ;
uint32_t startingLMI = 0 ;
if ( activityRewards . size ( ) > 0 ) {
startingLMI = activityRewards [ 0 ] . LootMatrixIndex ;
}
if ( startingLMI > 0 ) {
// now time for bodge :)
std : : vector < CDActivityRewards > objectTemplateActivities = activityRewardsTable - > Query ( [ = ] ( CDActivityRewards entry ) { return ( activityRewards [ 0 ] . objectTemplate = = entry . objectTemplate ) ; } ) ;
for ( const auto & item : objectTemplateActivities ) {
if ( item . activityRating > 0 & & item . activityRating < 5 ) {
m_ActivityLootMatrices . insert ( { item . activityRating , item . LootMatrixIndex } ) ;
}
}
}
}
2021-12-05 17:54:36 +00:00
}
ScriptedActivityComponent : : ~ ScriptedActivityComponent ( )
= default ;
void ScriptedActivityComponent : : Serialize ( RakNet : : BitStream * outBitStream , bool bIsInitialUpdate , unsigned int & flags ) const {
2022-07-28 13:39:57 +00:00
outBitStream - > Write ( true ) ;
outBitStream - > Write < uint32_t > ( m_ActivityPlayers . size ( ) ) ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
if ( ! m_ActivityPlayers . empty ( ) ) {
for ( const auto & activityPlayer : m_ActivityPlayers ) {
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
outBitStream - > Write < LWOOBJID > ( activityPlayer - > playerID ) ;
for ( const auto & activityValue : activityPlayer - > values ) {
outBitStream - > Write < float_t > ( activityValue ) ;
}
}
}
2021-12-05 17:54:36 +00:00
}
2022-12-04 22:25:58 +00:00
void ScriptedActivityComponent : : ReloadConfig ( ) {
2023-03-17 14:36:21 +00:00
CDActivitiesTable * activitiesTable = CDClientManager : : Instance ( ) . GetTable < CDActivitiesTable > ( ) ;
2022-12-04 22:25:58 +00:00
std : : vector < CDActivities > activities = activitiesTable - > Query ( [ = ] ( CDActivities entry ) { return ( entry . ActivityID = = m_ActivityID ) ; } ) ;
for ( auto activity : activities ) {
auto mapID = m_ActivityInfo . instanceMapID ;
if ( ( mapID = = 1203 | | mapID = = 1261 | | mapID = = 1303 | | mapID = = 1403 ) & & Game : : config - > GetValue ( " solo_racing " ) = = " 1 " ) {
m_ActivityInfo . minTeamSize = 1 ;
m_ActivityInfo . minTeams = 1 ;
} else {
m_ActivityInfo . minTeamSize = activity . minTeamSize ;
m_ActivityInfo . minTeams = activity . minTeams ;
}
}
}
2021-12-05 17:54:36 +00:00
void ScriptedActivityComponent : : HandleMessageBoxResponse ( Entity * player , const std : : string & id ) {
2022-07-28 13:39:57 +00:00
if ( m_ActivityInfo . ActivityID = = 103 ) {
return ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
if ( id = = " LobbyExit " ) {
PlayerLeave ( player - > GetObjectID ( ) ) ;
} else if ( id = = " PlayButton " ) {
PlayerJoin ( player ) ;
}
2021-12-05 17:54:36 +00:00
}
void ScriptedActivityComponent : : PlayerJoin ( Entity * player ) {
if ( m_ActivityInfo . ActivityID = = 103 | | PlayerIsInQueue ( player ) | | ! IsValidActivity ( player ) ) {
return ;
}
// If we have a lobby, queue the player and allow others to join, otherwise spin up an instance on the spot
if ( HasLobby ( ) ) {
2022-07-28 13:39:57 +00:00
PlayerJoinLobby ( player ) ;
2021-12-05 17:54:36 +00:00
} else if ( ! IsPlayedBy ( player ) ) {
2022-07-28 13:39:57 +00:00
auto * instance = NewInstance ( ) ;
instance - > AddParticipant ( player ) ;
2021-12-05 17:54:36 +00:00
}
EntityManager : : Instance ( ) - > SerializeEntity ( m_Parent ) ;
}
void ScriptedActivityComponent : : PlayerJoinLobby ( Entity * player ) {
2023-03-04 07:16:37 +00:00
if ( ! m_Parent - > HasComponent ( eReplicaComponentType : : QUICK_BUILD ) )
2022-07-28 13:39:57 +00:00
GameMessages : : SendMatchResponse ( player , player - > GetSystemAddress ( ) , 0 ) ; // tell the client they joined a lobby
LobbyPlayer * newLobbyPlayer = new LobbyPlayer ( ) ;
newLobbyPlayer - > entityID = player - > GetObjectID ( ) ;
Lobby * playerLobby = nullptr ;
auto * character = player - > GetCharacter ( ) ;
if ( character ! = nullptr )
character - > SetLastNonInstanceZoneID ( dZoneManager : : Instance ( ) - > GetZone ( ) - > GetWorldID ( ) ) ;
for ( Lobby * lobby : m_Queue ) {
if ( lobby - > players . size ( ) < m_ActivityInfo . maxTeamSize | | m_ActivityInfo . maxTeamSize = = 1 & & lobby - > players . size ( ) < m_ActivityInfo . maxTeams ) {
// If an empty slot in an existing lobby is found
lobby - > players . push_back ( newLobbyPlayer ) ;
playerLobby = lobby ;
// Update the joining player on players already in the lobby, and update players already in the lobby on the joining player
std : : string matchUpdateJoined = " player=9: " + std : : to_string ( player - > GetObjectID ( ) ) + " \n playerName=0: " + player - > GetCharacter ( ) - > GetName ( ) ;
for ( LobbyPlayer * joinedPlayer : lobby - > players ) {
auto * entity = joinedPlayer - > GetEntity ( ) ;
if ( entity = = nullptr ) {
continue ;
}
std : : string matchUpdate = " player=9: " + std : : to_string ( entity - > GetObjectID ( ) ) + " \n playerName=0: " + entity - > GetCharacter ( ) - > GetName ( ) ;
2023-05-02 22:39:21 +00:00
GameMessages : : SendMatchUpdate ( player , player - > GetSystemAddress ( ) , matchUpdate , eMatchUpdate : : PLAYER_ADDED ) ;
2022-07-28 13:39:57 +00:00
PlayerReady ( entity , joinedPlayer - > ready ) ;
2023-05-02 22:39:21 +00:00
GameMessages : : SendMatchUpdate ( entity , entity - > GetSystemAddress ( ) , matchUpdateJoined , eMatchUpdate : : PLAYER_ADDED ) ;
2022-07-28 13:39:57 +00:00
}
}
}
if ( ! playerLobby ) {
// If all lobbies are full
playerLobby = new Lobby ( ) ;
playerLobby - > players . push_back ( newLobbyPlayer ) ;
playerLobby - > timer = m_ActivityInfo . waitTime / 1000 ;
m_Queue . push_back ( playerLobby ) ;
}
if ( m_ActivityInfo . maxTeamSize ! = 1 & & playerLobby - > players . size ( ) > = m_ActivityInfo . minTeamSize | | m_ActivityInfo . maxTeamSize = = 1 & & playerLobby - > players . size ( ) > = m_ActivityInfo . minTeams ) {
// Update the joining player on the match timer
std : : string matchTimerUpdate = " time=3: " + std : : to_string ( playerLobby - > timer ) ;
2023-05-02 22:39:21 +00:00
GameMessages : : SendMatchUpdate ( player , player - > GetSystemAddress ( ) , matchTimerUpdate , eMatchUpdate : : PHASE_WAIT_READY ) ;
2022-07-28 13:39:57 +00:00
}
2021-12-05 17:54:36 +00:00
}
void ScriptedActivityComponent : : PlayerLeave ( LWOOBJID playerID ) {
2022-07-28 13:39:57 +00:00
// Removes the player from a lobby and notifies the others, not applicable for non-lobby instances
2021-12-05 17:54:36 +00:00
for ( Lobby * lobby : m_Queue ) {
for ( int i = 0 ; i < lobby - > players . size ( ) ; + + i ) {
if ( lobby - > players [ i ] - > entityID = = playerID ) {
std : : string matchUpdateLeft = " player=9: " + std : : to_string ( playerID ) ;
for ( LobbyPlayer * lobbyPlayer : lobby - > players ) {
auto * entity = lobbyPlayer - > GetEntity ( ) ;
if ( entity = = nullptr )
continue ;
2023-05-02 22:39:21 +00:00
GameMessages : : SendMatchUpdate ( entity , entity - > GetSystemAddress ( ) , matchUpdateLeft , eMatchUpdate : : PLAYER_REMOVED ) ;
2021-12-05 17:54:36 +00:00
}
delete lobby - > players [ i ] ;
lobby - > players [ i ] = nullptr ;
lobby - > players . erase ( lobby - > players . begin ( ) + i ) ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
return ;
}
}
}
}
void ScriptedActivityComponent : : Update ( float deltaTime ) {
2022-12-20 21:20:44 +00:00
std : : vector < Lobby * > lobbiesToRemove { } ;
2022-07-28 13:39:57 +00:00
// Ticks all the lobbies, not applicable for non-instance activities
2021-12-05 17:54:36 +00:00
for ( Lobby * lobby : m_Queue ) {
for ( LobbyPlayer * player : lobby - > players ) {
auto * entity = player - > GetEntity ( ) ;
if ( entity = = nullptr ) {
PlayerLeave ( player - > entityID ) ;
return ;
}
}
2022-12-20 21:20:44 +00:00
if ( lobby - > players . empty ( ) ) {
lobbiesToRemove . push_back ( lobby ) ;
continue ;
}
2021-12-05 17:54:36 +00:00
// Update the match time for all players
if ( m_ActivityInfo . maxTeamSize ! = 1 & & lobby - > players . size ( ) > = m_ActivityInfo . minTeamSize
2022-07-28 13:39:57 +00:00
| | m_ActivityInfo . maxTeamSize = = 1 & & lobby - > players . size ( ) > = m_ActivityInfo . minTeams ) {
2021-12-05 17:54:36 +00:00
if ( lobby - > timer = = m_ActivityInfo . waitTime / 1000 ) {
for ( LobbyPlayer * joinedPlayer : lobby - > players ) {
auto * entity = joinedPlayer - > GetEntity ( ) ;
if ( entity = = nullptr )
continue ;
std : : string matchTimerUpdate = " time=3: " + std : : to_string ( lobby - > timer ) ;
2023-05-02 22:39:21 +00:00
GameMessages : : SendMatchUpdate ( entity , entity - > GetSystemAddress ( ) , matchTimerUpdate , eMatchUpdate : : PHASE_WAIT_READY ) ;
2021-12-05 17:54:36 +00:00
}
}
lobby - > timer - = deltaTime ;
}
bool lobbyReady = true ;
for ( LobbyPlayer * player : lobby - > players ) {
if ( player - > ready ) continue ;
lobbyReady = false ;
}
// If everyone's ready, jump the timer
if ( lobbyReady & & lobby - > timer > m_ActivityInfo . startDelay / 1000 ) {
lobby - > timer = m_ActivityInfo . startDelay / 1000 ;
// Update players in lobby on switch to start delay
std : : string matchTimerUpdate = " time=3: " + std : : to_string ( lobby - > timer ) ;
for ( LobbyPlayer * player : lobby - > players ) {
auto * entity = player - > GetEntity ( ) ;
if ( entity = = nullptr )
continue ;
2023-05-02 22:39:21 +00:00
GameMessages : : SendMatchUpdate ( entity , entity - > GetSystemAddress ( ) , matchTimerUpdate , eMatchUpdate : : PHASE_WAIT_START ) ;
2021-12-05 17:54:36 +00:00
}
}
// The timer has elapsed, start the instance
if ( lobby - > timer < = 0.0f ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " ScriptedActivityComponent " , " Setting up instance. " ) ;
2021-12-05 17:54:36 +00:00
ActivityInstance * instance = NewInstance ( ) ;
LoadPlayersIntoInstance ( instance , lobby - > players ) ;
instance - > StartZone ( ) ;
2022-12-20 21:20:44 +00:00
lobbiesToRemove . push_back ( lobby ) ;
2021-12-05 17:54:36 +00:00
}
}
2022-12-20 21:20:44 +00:00
while ( ! lobbiesToRemove . empty ( ) ) {
RemoveLobby ( lobbiesToRemove . front ( ) ) ;
lobbiesToRemove . erase ( lobbiesToRemove . begin ( ) ) ;
}
2021-12-05 17:54:36 +00:00
}
void ScriptedActivityComponent : : RemoveLobby ( Lobby * lobby ) {
for ( int i = 0 ; i < m_Queue . size ( ) ; + + i ) {
if ( m_Queue [ i ] = = lobby ) {
m_Queue . erase ( m_Queue . begin ( ) + i ) ;
return ;
}
}
}
bool ScriptedActivityComponent : : HasLobby ( ) const {
2022-07-28 13:39:57 +00:00
// If the player is not in the world he has to be, create a lobby for the transfer
return m_ActivityInfo . instanceMapID ! = UINT_MAX & & m_ActivityInfo . instanceMapID ! = Game : : server - > GetZoneID ( ) ;
2021-12-05 17:54:36 +00:00
}
bool ScriptedActivityComponent : : IsValidActivity ( Entity * player ) {
2022-07-28 13:39:57 +00:00
// Makes it so that scripted activities with an unimplemented map cannot be joined
2023-03-24 23:16:45 +00:00
/*if (player->GetGMLevel() < eGameMasterLevel::DEVELOPER && (m_ActivityInfo.instanceMapID == 1302 || m_ActivityInfo.instanceMapID == 1301)) {
2022-07-28 13:39:57 +00:00
if ( m_Parent - > GetLOT ( ) = = 4860 ) {
auto * missionComponent = player - > GetComponent < MissionComponent > ( ) ;
missionComponent - > CompleteMission ( 229 ) ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
ChatPackets : : SendSystemMessage ( player - > GetSystemAddress ( ) , u " Sorry, this activity is not ready. " ) ;
static_cast < Player * > ( player ) - > SendToZone ( dZoneManager : : Instance ( ) - > GetZone ( ) - > GetWorldID ( ) ) ; // Gets them out of this stuck state
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
return false ;
} */
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
return true ;
2021-12-05 17:54:36 +00:00
}
bool ScriptedActivityComponent : : PlayerIsInQueue ( Entity * player ) {
for ( Lobby * lobby : m_Queue ) {
for ( LobbyPlayer * lobbyPlayer : lobby - > players ) {
if ( player - > GetObjectID ( ) = = lobbyPlayer - > entityID ) return true ;
}
}
return false ;
}
bool ScriptedActivityComponent : : IsPlayedBy ( Entity * player ) const {
2022-07-28 13:39:57 +00:00
for ( const auto * instance : this - > m_Instances ) {
for ( const auto * instancePlayer : instance - > GetParticipants ( ) ) {
if ( instancePlayer ! = nullptr & & instancePlayer - > GetObjectID ( ) = = player - > GetObjectID ( ) )
return true ;
}
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
return false ;
2021-12-05 17:54:36 +00:00
}
bool ScriptedActivityComponent : : IsPlayedBy ( LWOOBJID playerID ) const {
2022-07-28 13:39:57 +00:00
for ( const auto * instance : this - > m_Instances ) {
for ( const auto * instancePlayer : instance - > GetParticipants ( ) ) {
if ( instancePlayer ! = nullptr & & instancePlayer - > GetObjectID ( ) = = playerID )
return true ;
}
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
return false ;
2021-12-05 17:54:36 +00:00
}
bool ScriptedActivityComponent : : TakeCost ( Entity * player ) const {
if ( m_ActivityInfo . optionalCostLOT < = 0 | | m_ActivityInfo . optionalCostCount < = 0 )
return true ;
auto * inventoryComponent = player - > GetComponent < InventoryComponent > ( ) ;
if ( inventoryComponent = = nullptr )
return false ;
if ( inventoryComponent - > GetLotCount ( m_ActivityInfo . optionalCostLOT ) < m_ActivityInfo . optionalCostCount )
return false ;
inventoryComponent - > RemoveItem ( m_ActivityInfo . optionalCostLOT , m_ActivityInfo . optionalCostCount ) ;
return true ;
}
void ScriptedActivityComponent : : PlayerReady ( Entity * player , bool bReady ) {
for ( Lobby * lobby : m_Queue ) {
for ( LobbyPlayer * lobbyPlayer : lobby - > players ) {
if ( lobbyPlayer - > entityID = = player - > GetObjectID ( ) ) {
lobbyPlayer - > ready = bReady ;
// Update players in lobby on player being ready
std : : string matchReadyUpdate = " player=9: " + std : : to_string ( player - > GetObjectID ( ) ) ;
2023-05-02 22:39:21 +00:00
eMatchUpdate readyStatus = eMatchUpdate : : PLAYER_READY ;
if ( ! bReady ) readyStatus = eMatchUpdate : : PLAYER_NOT_READY ;
2021-12-05 17:54:36 +00:00
for ( LobbyPlayer * otherPlayer : lobby - > players ) {
auto * entity = otherPlayer - > GetEntity ( ) ;
if ( entity = = nullptr )
continue ;
GameMessages : : SendMatchUpdate ( entity , entity - > GetSystemAddress ( ) , matchReadyUpdate , readyStatus ) ;
}
}
}
}
}
ActivityInstance * ScriptedActivityComponent : : NewInstance ( ) {
auto * instance = new ActivityInstance ( m_Parent , m_ActivityInfo ) ;
m_Instances . push_back ( instance ) ;
return instance ;
}
void ScriptedActivityComponent : : LoadPlayersIntoInstance ( ActivityInstance * instance , const std : : vector < LobbyPlayer * > & lobby ) const {
2022-07-28 13:39:57 +00:00
for ( LobbyPlayer * player : lobby ) {
auto * entity = player - > GetEntity ( ) ;
if ( entity = = nullptr | | ! TakeCost ( entity ) ) {
continue ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
instance - > AddParticipant ( entity ) ;
}
2021-12-05 17:54:36 +00:00
}
const std : : vector < ActivityInstance * > & ScriptedActivityComponent : : GetInstances ( ) const {
return m_Instances ;
}
ActivityInstance * ScriptedActivityComponent : : GetInstance ( const LWOOBJID playerID ) {
2022-07-28 13:39:57 +00:00
for ( const auto * instance : GetInstances ( ) ) {
for ( const auto * participant : instance - > GetParticipants ( ) ) {
if ( participant - > GetObjectID ( ) = = playerID )
return const_cast < ActivityInstance * > ( instance ) ;
}
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
return nullptr ;
2021-12-05 17:54:36 +00:00
}
void ScriptedActivityComponent : : ClearInstances ( ) {
for ( ActivityInstance * instance : m_Instances ) {
delete instance ;
}
m_Instances . clear ( ) ;
}
2022-07-28 13:39:57 +00:00
ActivityPlayer * ScriptedActivityComponent : : GetActivityPlayerData ( LWOOBJID playerID ) {
for ( auto * activityData : m_ActivityPlayers ) {
if ( activityData - > playerID = = playerID ) {
2021-12-05 17:54:36 +00:00
return activityData ;
}
}
return nullptr ;
}
2022-07-28 13:39:57 +00:00
void ScriptedActivityComponent : : RemoveActivityPlayerData ( LWOOBJID playerID ) {
for ( size_t i = 0 ; i < m_ActivityPlayers . size ( ) ; i + + ) {
if ( m_ActivityPlayers [ i ] - > playerID = = playerID ) {
2021-12-05 17:54:36 +00:00
delete m_ActivityPlayers [ i ] ;
m_ActivityPlayers [ i ] = nullptr ;
m_ActivityPlayers . erase ( m_ActivityPlayers . begin ( ) + i ) ;
2022-07-28 13:39:57 +00:00
EntityManager : : Instance ( ) - > SerializeEntity ( m_Parent ) ;
2021-12-05 17:54:36 +00:00
return ;
}
}
}
2022-07-28 13:39:57 +00:00
ActivityPlayer * ScriptedActivityComponent : : AddActivityPlayerData ( LWOOBJID playerID ) {
2021-12-05 17:54:36 +00:00
auto * data = GetActivityPlayerData ( playerID ) ;
if ( data ! = nullptr )
2022-07-28 13:39:57 +00:00
return data ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
m_ActivityPlayers . push_back ( new ActivityPlayer { playerID , { } } ) ;
EntityManager : : Instance ( ) - > SerializeEntity ( m_Parent ) ;
2021-12-05 17:54:36 +00:00
return GetActivityPlayerData ( playerID ) ;
}
float_t ScriptedActivityComponent : : GetActivityValue ( LWOOBJID playerID , uint32_t index ) {
2022-07-28 13:39:57 +00:00
auto value = - 1.0f ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
auto * data = GetActivityPlayerData ( playerID ) ;
if ( data ! = nullptr ) {
value = data - > values [ std : : min ( index , ( uint32_t ) 9 ) ] ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
return value ;
2021-12-05 17:54:36 +00:00
}
void ScriptedActivityComponent : : SetActivityValue ( LWOOBJID playerID , uint32_t index , float_t value ) {
2022-07-28 13:39:57 +00:00
auto * data = AddActivityPlayerData ( playerID ) ;
if ( data ! = nullptr ) {
data - > values [ std : : min ( index , ( uint32_t ) 9 ) ] = value ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
EntityManager : : Instance ( ) - > SerializeEntity ( m_Parent ) ;
2021-12-05 17:54:36 +00:00
}
void ScriptedActivityComponent : : PlayerRemove ( LWOOBJID playerID ) {
2022-07-28 13:39:57 +00:00
for ( auto * instance : GetInstances ( ) ) {
auto participants = instance - > GetParticipants ( ) ;
for ( const auto * participant : participants ) {
if ( participant ! = nullptr & & participant - > GetObjectID ( ) = = playerID ) {
instance - > RemoveParticipant ( participant ) ;
RemoveActivityPlayerData ( playerID ) ;
// If the instance is empty after the delete of the participant, delete the instance too
if ( instance - > GetParticipants ( ) . empty ( ) ) {
m_Instances . erase ( std : : find ( m_Instances . begin ( ) , m_Instances . end ( ) , instance ) ) ;
delete instance ;
}
return ;
}
}
}
2021-12-05 17:54:36 +00:00
}
void ActivityInstance : : StartZone ( ) {
if ( m_Participants . empty ( ) )
2022-07-28 13:39:57 +00:00
return ;
2021-12-05 17:54:36 +00:00
const auto & participants = GetParticipants ( ) ;
if ( participants . empty ( ) )
2022-07-28 13:39:57 +00:00
return ;
2021-12-05 17:54:36 +00:00
auto * leader = participants [ 0 ] ;
2022-05-27 20:25:57 +00:00
LWOZONEID zoneId = LWOZONEID ( m_ActivityInfo . instanceMapID , 0 , leader - > GetCharacter ( ) - > GetPropertyCloneID ( ) ) ;
2021-12-05 17:54:36 +00:00
2022-05-27 20:25:57 +00:00
// only make a team if we have more than one participant
2022-07-28 13:39:57 +00:00
if ( participants . size ( ) > 1 ) {
2022-05-27 20:25:57 +00:00
CBITSTREAM ;
2023-05-03 21:38:32 +00:00
PacketUtils : : WriteHeader ( bitStream , eConnectionType : : CHAT_INTERNAL , eChatInternalMessageType : : CREATE_TEAM ) ;
2021-12-05 17:54:36 +00:00
2022-05-27 20:25:57 +00:00
bitStream . Write ( leader - > GetObjectID ( ) ) ;
bitStream . Write ( m_Participants . size ( ) ) ;
2021-12-05 17:54:36 +00:00
2022-05-27 20:25:57 +00:00
for ( const auto & participant : m_Participants ) {
bitStream . Write ( participant ) ;
}
2021-12-05 17:54:36 +00:00
2022-05-27 20:25:57 +00:00
bitStream . Write ( zoneId ) ;
2021-12-05 17:54:36 +00:00
2022-05-27 20:25:57 +00:00
Game : : chatServer - > Send ( & bitStream , SYSTEM_PRIORITY , RELIABLE , 0 , Game : : chatSysAddr , false ) ;
}
2021-12-05 17:54:36 +00:00
const auto cloneId = GeneralUtils : : GenerateRandomNumber < uint32_t > ( 1 , UINT32_MAX ) ;
for ( Entity * player : participants ) {
const auto objid = player - > GetObjectID ( ) ;
ZoneInstanceManager : : Instance ( ) - > RequestZoneTransfer ( Game : : server , m_ActivityInfo . instanceMapID , cloneId , false , [ objid ] ( bool mythranShift , uint32_t zoneID , uint32_t zoneInstance , uint32_t zoneClone , std : : string serverIP , uint16_t serverPort ) {
auto * player = EntityManager : : Instance ( ) - > GetEntity ( objid ) ;
if ( player = = nullptr )
return ;
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " UserManager " , " Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i " , player - > GetCharacter ( ) - > GetName ( ) . c_str ( ) , zoneID , zoneInstance , zoneClone , mythranShift = = true ? " true " : " false " , serverIP . c_str ( ) , serverPort ) ;
2021-12-05 17:54:36 +00:00
if ( player - > GetCharacter ( ) ) {
player - > GetCharacter ( ) - > SetZoneID ( zoneID ) ;
player - > GetCharacter ( ) - > SetZoneInstance ( zoneInstance ) ;
player - > GetCharacter ( ) - > SetZoneClone ( zoneClone ) ;
}
WorldPackets : : SendTransferToWorld ( player - > GetSystemAddress ( ) , serverIP , serverPort , mythranShift ) ;
return ;
2022-07-28 13:39:57 +00:00
} ) ;
2021-12-05 17:54:36 +00:00
}
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
m_NextZoneCloneID + + ;
}
void ActivityInstance : : RewardParticipant ( Entity * participant ) {
auto * missionComponent = participant - > GetComponent < MissionComponent > ( ) ;
if ( missionComponent ) {
2023-01-22 23:38:47 +00:00
missionComponent - > Progress ( eMissionTaskType : : ACTIVITY , m_ActivityInfo . ActivityID ) ;
2021-12-05 17:54:36 +00:00
}
// First, get the activity data
2023-03-17 14:36:21 +00:00
auto * activityRewardsTable = CDClientManager : : Instance ( ) . GetTable < CDActivityRewardsTable > ( ) ;
2021-12-05 17:54:36 +00:00
std : : vector < CDActivityRewards > activityRewards = activityRewardsTable - > Query ( [ = ] ( CDActivityRewards entry ) { return ( entry . objectTemplate = = m_ActivityInfo . ActivityID ) ; } ) ;
if ( ! activityRewards . empty ( ) ) {
uint32_t minCoins = 0 ;
uint32_t maxCoins = 0 ;
2023-03-17 14:36:21 +00:00
auto * currencyTableTable = CDClientManager : : Instance ( ) . GetTable < CDCurrencyTableTable > ( ) ;
2021-12-05 17:54:36 +00:00
std : : vector < CDCurrencyTable > currencyTable = currencyTableTable - > Query ( [ = ] ( CDCurrencyTable entry ) { return ( entry . currencyIndex = = activityRewards [ 0 ] . CurrencyIndex & & entry . npcminlevel = = 1 ) ; } ) ;
if ( ! currencyTable . empty ( ) ) {
minCoins = currencyTable [ 0 ] . minvalue ;
maxCoins = currencyTable [ 0 ] . maxvalue ;
}
2021-12-20 10:25:45 +00:00
LootGenerator : : Instance ( ) . DropLoot ( participant , m_Parent , activityRewards [ 0 ] . LootMatrixIndex , minCoins , maxCoins ) ;
2021-12-05 17:54:36 +00:00
}
}
std : : vector < Entity * > ActivityInstance : : GetParticipants ( ) const {
std : : vector < Entity * > entities ;
entities . reserve ( m_Participants . size ( ) ) ;
for ( const auto & id : m_Participants ) {
auto * entity = EntityManager : : Instance ( ) - > GetEntity ( id ) ;
if ( entity ! = nullptr )
entities . push_back ( entity ) ;
}
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
return entities ;
}
void ActivityInstance : : AddParticipant ( Entity * participant ) {
const auto id = participant - > GetObjectID ( ) ;
if ( std : : count ( m_Participants . begin ( ) , m_Participants . end ( ) , id ) )
2022-07-28 13:39:57 +00:00
return ;
2021-12-05 17:54:36 +00:00
m_Participants . push_back ( id ) ;
}
2022-07-28 13:39:57 +00:00
void ActivityInstance : : RemoveParticipant ( const Entity * participant ) {
const auto loadedParticipant = std : : find ( m_Participants . begin ( ) , m_Participants . end ( ) , participant - > GetObjectID ( ) ) ;
if ( loadedParticipant ! = m_Participants . end ( ) ) {
m_Participants . erase ( loadedParticipant ) ;
}
2021-12-05 17:54:36 +00:00
}
uint32_t ActivityInstance : : GetScore ( ) const {
2022-07-28 13:39:57 +00:00
return score ;
2021-12-05 17:54:36 +00:00
}
void ActivityInstance : : SetScore ( uint32_t score ) {
2022-07-28 13:39:57 +00:00
this - > score = score ;
2021-12-05 17:54:36 +00:00
}
Entity * LobbyPlayer : : GetEntity ( ) const {
return EntityManager : : Instance ( ) - > GetEntity ( entityID ) ;
}