2022-08-06 03:01:59 +00:00
# include <sstream>
2021-12-05 17:54:36 +00:00
# include "MissionTask.h"
# include "Game.h"
# include "dLogger.h"
# include "Mission.h"
# include "Character.h"
# include "dServer.h"
# include "EntityManager.h"
# include "ScriptedActivityComponent.h"
# include "GameMessages.h"
# include "dZoneManager.h"
2022-12-18 15:46:04 +00:00
# include "InventoryComponent.h"
2021-12-05 17:54:36 +00:00
# include "MissionComponent.h"
2023-01-22 23:38:47 +00:00
# include "eMissionTaskType.h"
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
MissionTask : : MissionTask ( Mission * mission , CDMissionTasks * info , uint32_t mask ) {
2021-12-05 17:54:36 +00:00
this - > info = info ;
this - > mission = mission ;
this - > mask = mask ;
progress = 0 ;
std : : istringstream stream ( info - > taskParam1 ) ;
std : : string token ;
while ( std : : getline ( stream , token , ' , ' ) ) {
uint32_t parameter ;
if ( GeneralUtils : : TryParse ( token , parameter ) ) {
parameters . push_back ( parameter ) ;
}
}
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
stream = std : : istringstream ( info - > targetGroup ) ;
while ( std : : getline ( stream , token , ' , ' ) ) {
uint32_t parameter ;
if ( GeneralUtils : : TryParse ( token , parameter ) ) {
targets . push_back ( parameter ) ;
}
}
}
2023-01-22 23:38:47 +00:00
eMissionTaskType MissionTask : : GetType ( ) const {
return static_cast < eMissionTaskType > ( info - > taskType ) ;
2021-12-05 17:54:36 +00:00
}
2022-07-28 13:39:57 +00:00
uint32_t MissionTask : : GetProgress ( ) const {
2021-12-05 17:54:36 +00:00
return progress ;
}
2022-07-28 13:39:57 +00:00
void MissionTask : : SetProgress ( const uint32_t value , const bool echo ) {
if ( progress = = value ) {
2021-12-05 17:54:36 +00:00
return ;
}
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
progress = value ;
2022-07-28 13:39:57 +00:00
if ( ! echo ) {
2021-12-05 17:54:36 +00:00
return ;
}
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
auto * entity = mission - > GetAssociate ( ) ;
2022-07-28 13:39:57 +00:00
if ( entity = = nullptr ) {
2021-12-05 17:54:36 +00:00
return ;
}
std : : vector < float > updates ;
updates . push_back ( static_cast < float > ( progress ) ) ;
2022-02-21 04:01:55 +00:00
GameMessages : : SendNotifyMissionTask ( entity , entity - > GetSystemAddress ( ) , static_cast < int > ( info - > id ) , static_cast < int > ( 1 < < ( mask + 1 ) ) , updates ) ;
2021-12-05 17:54:36 +00:00
}
2022-07-28 13:39:57 +00:00
void MissionTask : : SetUnique ( const std : : vector < uint32_t > & value ) {
2021-12-05 17:54:36 +00:00
unique = value ;
}
2022-07-28 13:39:57 +00:00
void MissionTask : : AddProgress ( int32_t value ) {
2021-12-05 17:54:36 +00:00
value + = progress ;
2022-07-28 13:39:57 +00:00
if ( value > info - > targetValue ) {
2021-12-05 17:54:36 +00:00
value = info - > targetValue ;
}
2022-07-28 13:39:57 +00:00
if ( value < 0 ) {
2021-12-05 17:54:36 +00:00
value = 0 ;
}
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
SetProgress ( value ) ;
}
2022-07-28 13:39:57 +00:00
Mission * MissionTask : : GetMission ( ) const {
2021-12-05 17:54:36 +00:00
return mission ;
}
2022-07-28 13:39:57 +00:00
uint32_t MissionTask : : GetTarget ( ) const {
2021-12-05 17:54:36 +00:00
return info - > target ;
}
2022-07-28 13:39:57 +00:00
const CDMissionTasks & MissionTask : : GetClientInfo ( ) const {
2021-12-05 17:54:36 +00:00
return * info ;
}
2022-07-28 13:39:57 +00:00
uint32_t MissionTask : : GetMask ( ) const {
2021-12-05 17:54:36 +00:00
return mask ;
}
2022-07-28 13:39:57 +00:00
const std : : vector < uint32_t > & MissionTask : : GetUnique ( ) const {
2021-12-05 17:54:36 +00:00
return unique ;
}
2022-07-28 13:39:57 +00:00
const std : : vector < uint32_t > & MissionTask : : GetTargets ( ) const {
2021-12-05 17:54:36 +00:00
return targets ;
}
2022-07-28 13:39:57 +00:00
const std : : vector < uint32_t > & MissionTask : : GetParameters ( ) const {
2021-12-05 17:54:36 +00:00
return parameters ;
}
2022-07-28 13:39:57 +00:00
std : : vector < uint32_t > MissionTask : : GetAllTargets ( ) const {
2021-12-05 17:54:36 +00:00
auto targets = GetTargets ( ) ;
targets . push_back ( GetTarget ( ) ) ;
return targets ;
}
2022-07-28 13:39:57 +00:00
bool MissionTask : : InTargets ( const uint32_t value ) const {
2021-12-05 17:54:36 +00:00
auto targets = GetTargets ( ) ;
return std : : find ( targets . begin ( ) , targets . end ( ) , value ) ! = targets . end ( ) ;
}
2022-07-28 13:39:57 +00:00
bool MissionTask : : InAllTargets ( const uint32_t value ) const {
2021-12-05 17:54:36 +00:00
auto targets = GetAllTargets ( ) ;
return std : : find ( targets . begin ( ) , targets . end ( ) , value ) ! = targets . end ( ) ;
}
2022-07-28 13:39:57 +00:00
bool MissionTask : : InParameters ( const uint32_t value ) const {
2021-12-05 17:54:36 +00:00
auto parameters = GetParameters ( ) ;
return std : : find ( parameters . begin ( ) , parameters . end ( ) , value ) ! = parameters . end ( ) ;
}
2022-07-28 13:39:57 +00:00
bool MissionTask : : IsComplete ( ) const {
2022-04-18 08:04:29 +00:00
// Mission 668 has task uid 984 which is a bit mask. Its completion value is 3.
if ( info - > uid = = 984 ) {
return progress > = 3 ;
2022-07-28 13:39:57 +00:00
} else {
2022-04-18 08:04:29 +00:00
return progress > = info - > targetValue ;
}
2021-12-05 17:54:36 +00:00
}
2022-07-28 13:39:57 +00:00
void MissionTask : : Complete ( ) {
2022-02-10 00:42:17 +00:00
SetProgress ( info - > targetValue ) ;
2021-12-05 17:54:36 +00:00
}
2022-07-28 13:39:57 +00:00
void MissionTask : : CheckCompletion ( ) const {
if ( IsComplete ( ) ) {
2021-12-05 17:54:36 +00:00
mission - > CheckCompletion ( ) ;
}
}
2022-07-28 13:39:57 +00:00
void MissionTask : : Progress ( int32_t value , LWOOBJID associate , const std : : string & targets , int32_t count ) {
2021-12-05 17:54:36 +00:00
if ( IsComplete ( ) & & count > 0 ) return ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
const auto type = GetType ( ) ;
2022-07-28 13:39:57 +00:00
if ( count < 0 ) {
2023-01-22 23:38:47 +00:00
if ( mission - > IsMission ( ) & & type = = eMissionTaskType : : GATHER & & InAllTargets ( value ) ) {
2022-07-28 13:39:57 +00:00
if ( parameters . size ( ) > 0 & & ( parameters [ 0 ] & 1 ) ! = 0 ) {
2021-12-05 17:54:36 +00:00
return ;
}
auto * inventoryComponent = mission - > GetAssociate ( ) - > GetComponent < InventoryComponent > ( ) ;
2022-07-28 13:39:57 +00:00
if ( inventoryComponent ! = nullptr ) {
2021-12-05 17:54:36 +00:00
int32_t itemCount = inventoryComponent - > GetLotCountNonTransfer ( value ) ;
2022-07-28 13:39:57 +00:00
if ( itemCount < info - > targetValue ) {
2021-12-05 17:54:36 +00:00
SetProgress ( itemCount ) ;
2022-07-28 13:39:57 +00:00
if ( mission - > IsReadyToComplete ( ) ) {
2021-12-05 17:54:36 +00:00
mission - > MakeActive ( ) ;
}
}
}
}
return ;
}
Entity * entity ;
ScriptedActivityComponent * activity ;
uint32_t activityId ;
uint32_t lot ;
uint32_t collectionId ;
std : : vector < LDFBaseData * > settings ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
switch ( type ) {
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : UNKNOWN :
2021-12-05 17:54:36 +00:00
break ;
2022-07-25 02:26:51 +00:00
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : ACTIVITY :
2021-12-05 17:54:36 +00:00
{
if ( InAllTargets ( value ) ) {
AddProgress ( count ) ;
break ;
}
entity = EntityManager : : Instance ( ) - > GetEntity ( associate ) ;
if ( entity = = nullptr ) {
if ( associate ! = LWOOBJID_EMPTY ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " MissionTask " , " Failed to find associated entity (%llu)! " , associate ) ;
2021-12-05 17:54:36 +00:00
}
break ;
}
activity = static_cast < ScriptedActivityComponent * > ( entity - > GetComponent ( COMPONENT_TYPE_REBUILD ) ) ;
2022-07-28 13:39:57 +00:00
if ( activity = = nullptr ) {
2021-12-05 17:54:36 +00:00
break ;
}
activityId = activity - > GetActivityID ( ) ;
const auto activityIdOverride = entity - > GetVar < int32_t > ( u " activityID " ) ;
2022-07-28 13:39:57 +00:00
if ( activityIdOverride ! = 0 ) {
2021-12-05 17:54:36 +00:00
activityId = activityIdOverride ;
}
if ( ! InAllTargets ( activityId ) ) break ;
AddProgress ( count ) ;
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : USE_ITEM :
case eMissionTaskType : : TALK_TO_NPC :
2021-12-05 17:54:36 +00:00
{
if ( GetTarget ( ) ! = value ) break ;
AddProgress ( count ) ;
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : EMOTE :
2021-12-05 17:54:36 +00:00
{
if ( ! InParameters ( value ) ) break ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
entity = EntityManager : : Instance ( ) - > GetEntity ( associate ) ;
2022-07-28 13:39:57 +00:00
if ( entity = = nullptr ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " MissionTask " , " Failed to find associated entity (%llu)! " , associate ) ;
2021-12-05 17:54:36 +00:00
break ;
}
lot = static_cast < uint32_t > ( entity - > GetLOT ( ) ) ;
if ( GetTarget ( ) ! = lot ) break ;
AddProgress ( count ) ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
break ;
}
2022-07-25 02:26:51 +00:00
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : USE_SKILL :
2021-12-05 17:54:36 +00:00
{
2022-04-25 10:25:07 +00:00
// This is a complicated check because for some missions we need to check for the associate being in the parameters instead of the value being in the parameters.
if ( associate = = LWOOBJID_EMPTY & & GetAllTargets ( ) . size ( ) = = 1 & & GetAllTargets ( ) [ 0 ] = = - 1 ) {
if ( InParameters ( value ) ) AddProgress ( count ) ;
} else {
if ( InParameters ( associate ) & & InAllTargets ( value ) ) AddProgress ( count ) ;
2022-07-25 02:26:51 +00:00
}
2021-12-05 17:54:36 +00:00
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : PERFORM_ACTIVITY :
2021-12-05 17:54:36 +00:00
{
auto * minigameManager = EntityManager : : Instance ( ) - > GetEntity ( associate ) ;
if ( minigameManager = = nullptr )
2022-07-28 13:39:57 +00:00
break ;
2021-12-05 17:54:36 +00:00
int32_t gameID = minigameManager - > GetLOT ( ) ;
auto * sac = minigameManager - > GetComponent < ScriptedActivityComponent > ( ) ;
if ( sac ! = nullptr ) {
gameID = sac - > GetActivityID ( ) ;
}
if ( info - > target ! = gameID ) {
break ;
}
2022-02-21 04:00:56 +00:00
// This special case is for shooting gallery missions that want their
// progress value set to 1 instead of being set to the target value.
2022-07-28 13:39:57 +00:00
if ( info - > targetGroup = = targets & & value > = info - > targetValue & & GetMission ( ) - > IsMission ( ) & & info - > target = = 1864 & & info - > targetGroup = = " performact_score " ) {
2022-02-10 11:08:47 +00:00
SetProgress ( 1 ) ;
break ;
}
2022-07-28 13:39:57 +00:00
if ( info - > targetGroup = = targets & & value > = info - > targetValue ) {
2022-02-10 00:42:17 +00:00
SetProgress ( info - > targetValue ) ;
2022-02-05 11:54:12 +00:00
break ;
}
2021-12-05 17:54:36 +00:00
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : VISIT_PROPERTY :
2021-12-05 17:54:36 +00:00
{
if ( ! InAllTargets ( value ) ) break ;
if ( std : : find ( unique . begin ( ) , unique . end ( ) , static_cast < uint32_t > ( associate ) ) ! = unique . end ( ) ) break ;
AddProgress ( count ) ;
unique . push_back ( associate ) ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : COLLECTION :
2021-12-05 17:54:36 +00:00
{
if ( ! InAllTargets ( value ) ) break ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
entity = EntityManager : : Instance ( ) - > GetEntity ( associate ) ;
2022-07-28 13:39:57 +00:00
if ( entity = = nullptr ) {
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " MissionTask " , " Failed to find associated entity (%llu)! " , associate ) ;
2021-12-05 17:54:36 +00:00
break ;
}
collectionId = entity - > GetCollectibleID ( ) ;
collectionId = static_cast < uint32_t > ( collectionId ) + static_cast < uint32_t > ( Game : : server - > GetZoneID ( ) < < 8 ) ;
if ( std : : find ( unique . begin ( ) , unique . end ( ) , collectionId ) ! = unique . end ( ) ) break ;
unique . push_back ( collectionId ) ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
SetProgress ( unique . size ( ) ) ;
auto * entity = mission - > GetAssociate ( ) ;
if ( entity = = nullptr ) break ;
auto * missionComponent = entity - > GetComponent < MissionComponent > ( ) ;
if ( missionComponent = = nullptr ) break ;
missionComponent - > AddCollectible ( collectionId ) ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : EXPLORE :
2021-12-05 17:54:36 +00:00
{
if ( info - > targetGroup ! = targets ) break ;
AddProgress ( count ) ;
2022-07-25 02:26:51 +00:00
2021-12-05 17:54:36 +00:00
break ;
}
2022-07-25 02:26:51 +00:00
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : RACING :
2021-12-05 17:54:36 +00:00
{
2023-01-22 23:38:47 +00:00
// The meaning of associate can be found in eRacingTaskParam.h
2021-12-05 17:54:36 +00:00
if ( parameters . empty ( ) ) break ;
2022-02-05 11:28:17 +00:00
if ( ! InAllTargets ( dZoneManager : : Instance ( ) - > GetZone ( ) - > GetWorldID ( ) ) & & ! ( parameters [ 0 ] = = 4 | | parameters [ 0 ] = = 5 ) & & ! InAllTargets ( value ) ) break ;
2021-12-05 17:54:36 +00:00
if ( parameters [ 0 ] ! = associate ) break ;
2022-07-28 13:39:57 +00:00
if ( associate = = 1 | | associate = = 2 | | associate = = 3 ) {
2021-12-05 17:54:36 +00:00
if ( value > info - > targetValue ) break ;
AddProgress ( info - > targetValue ) ;
}
2022-04-18 08:04:29 +00:00
// task 15 is a bit mask!
else if ( associate = = 15 ) {
if ( ! InAllTargets ( value ) ) break ;
auto tempProgress = GetProgress ( ) ;
// If we won at Nimbus Station, set bit 0
if ( value = = 1203 ) SetProgress ( tempProgress | = 1 < < 0 ) ;
// If we won at Gnarled Forest, set bit 1
else if ( value = = 1303 ) SetProgress ( tempProgress | = 1 < < 1 ) ;
// If both bits are set, then the client sees the mission as complete.
2022-07-28 13:39:57 +00:00
} else if ( associate = = 10 ) {
2022-02-05 11:28:17 +00:00
// If the player did not crash during the race, progress this task by count.
if ( value ! = 0 ) break ;
2022-07-25 02:26:51 +00:00
2022-02-05 11:28:17 +00:00
AddProgress ( count ) ;
2022-07-28 13:39:57 +00:00
} else if ( associate = = 4 | | associate = = 5 | | associate = = 14 ) {
2022-02-05 11:28:17 +00:00
if ( ! InAllTargets ( value ) ) break ;
AddProgress ( count ) ;
2022-07-28 13:39:57 +00:00
} else if ( associate = = 17 ) {
2022-02-06 22:28:27 +00:00
if ( ! InAllTargets ( value ) ) break ;
AddProgress ( count ) ;
2022-07-28 13:39:57 +00:00
} else {
2021-12-05 17:54:36 +00:00
AddProgress ( count ) ;
}
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : PET_TAMING :
case eMissionTaskType : : SCRIPT :
case eMissionTaskType : : INTERACT :
case eMissionTaskType : : META :
case eMissionTaskType : : POWERUP :
case eMissionTaskType : : SMASH :
case eMissionTaskType : : GATHER :
case eMissionTaskType : : PLAYER_FLAG :
case eMissionTaskType : : EARN_REPUTATION :
2021-12-05 17:54:36 +00:00
{
if ( ! InAllTargets ( value ) ) break ;
AddProgress ( count ) ;
break ;
}
2023-01-22 23:38:47 +00:00
case eMissionTaskType : : PLACE_MODEL :
2022-02-05 12:08:40 +00:00
{
AddProgress ( count ) ;
break ;
}
2021-12-05 17:54:36 +00:00
default :
2022-07-25 02:26:51 +00:00
Game : : logger - > Log ( " MissionTask " , " Invalid mission task type (%i)! " , static_cast < int > ( type ) ) ;
2021-12-05 17:54:36 +00:00
return ;
}
CheckCompletion ( ) ;
}
2022-07-28 13:39:57 +00:00
MissionTask : : ~ MissionTask ( ) {
2021-12-05 17:54:36 +00:00
targets . clear ( ) ;
parameters . clear ( ) ;
unique . clear ( ) ;
}