2021-12-05 17:54:36 +00:00
|
|
|
/*
|
|
|
|
* Darkflame Universe
|
|
|
|
* Copyright 2019
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "MissionComponent.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "CDClientManager.h"
|
|
|
|
#include "CDMissionTasksTable.h"
|
|
|
|
#include "InventoryComponent.h"
|
|
|
|
#include "GameMessages.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "AMFFormat.h"
|
|
|
|
#include "dZoneManager.h"
|
|
|
|
#include "Mail.h"
|
|
|
|
#include "MissionPrerequisites.h"
|
2023-01-21 15:37:09 +00:00
|
|
|
#include "AchievementCacheKey.h"
|
2023-01-22 23:38:47 +00:00
|
|
|
#include "eMissionState.h"
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// MARK: Mission Component
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-01-21 15:37:09 +00:00
|
|
|
std::unordered_map<AchievementCacheKey, std::vector<uint32_t>> MissionComponent::m_AchievementCache = {};
|
2021-12-05 17:54:36 +00:00
|
|
|
|
|
|
|
//! Initializer
|
|
|
|
MissionComponent::MissionComponent(Entity* parent) : Component(parent) {
|
2022-07-31 03:56:21 +00:00
|
|
|
m_LastUsedMissionOrderUID = dZoneManager::Instance()->GetUniqueMissionIdStartingValue();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Destructor
|
|
|
|
MissionComponent::~MissionComponent() {
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& mission : m_Missions) {
|
|
|
|
delete mission.second;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
this->m_Missions.clear();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Mission* MissionComponent::GetMission(const uint32_t missionId) const {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (m_Missions.count(missionId) == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto& index = m_Missions.find(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (index == m_Missions.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return index->second;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-22 23:38:47 +00:00
|
|
|
eMissionState MissionComponent::GetMissionState(const uint32_t missionId) const {
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mission = GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
2023-01-22 23:38:47 +00:00
|
|
|
return CanAccept(missionId) ? eMissionState::AVAILABLE : eMissionState::UNKNOWN;
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return mission->GetMissionState();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::unordered_map<uint32_t, Mission*>& MissionComponent::GetMissions() const {
|
2022-07-28 13:39:57 +00:00
|
|
|
return m_Missions;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MissionComponent::CanAccept(const uint32_t missionId) const {
|
2022-07-28 13:39:57 +00:00
|
|
|
return MissionPrerequisites::CanAccept(missionId, m_Missions);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MissionComponent::AcceptMission(const uint32_t missionId, const bool skipChecks) {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!skipChecks && !CanAccept(missionId)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// If this is a daily mission, it may already be "accepted"
|
|
|
|
auto* mission = this->GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission != nullptr) {
|
|
|
|
if (mission->GetClientInfo().repeatable) {
|
|
|
|
mission->Accept();
|
2022-07-31 03:56:21 +00:00
|
|
|
if (mission->IsMission()) mission->SetUniqueMissionOrderID(++m_LastUsedMissionOrderUID);
|
2022-07-28 13:39:57 +00:00
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission = new Mission(this, missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-31 03:56:21 +00:00
|
|
|
if (mission->IsMission()) mission->SetUniqueMissionOrderID(++m_LastUsedMissionOrderUID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission->Accept();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
this->m_Missions.insert_or_assign(missionId, mission);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (missionId == 1728) {
|
|
|
|
//Needs to send a mail
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto address = m_Parent->GetSystemAddress();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
Mail::HandleNotificationRequest(address, m_Parent->GetObjectID());
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MissionComponent::CompleteMission(const uint32_t missionId, const bool skipChecks, const bool yieldRewards) {
|
2022-07-28 13:39:57 +00:00
|
|
|
// Get the mission first
|
|
|
|
auto* mission = this->GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
AcceptMission(missionId, skipChecks);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission = this->GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
//If this mission is not repeatable, and already completed, we stop here.
|
|
|
|
if (mission->IsComplete() && !mission->IsRepeatable()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission->Complete(yieldRewards);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MissionComponent::RemoveMission(uint32_t missionId) {
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mission = this->GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
delete mission;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Missions.erase(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 23:38:47 +00:00
|
|
|
void MissionComponent::Progress(eMissionTaskType type, int32_t value, LWOOBJID associate, const std::string& targets, int32_t count, bool ignoreAchievements) {
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& pair : m_Missions) {
|
|
|
|
auto* mission = pair.second;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission->IsAchievement() && ignoreAchievements) continue;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission->IsComplete()) continue;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission->Progress(type, value, associate, targets, count);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (count > 0 && !ignoreAchievements) {
|
|
|
|
LookForAchievements(type, value, true, associate, targets, count);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MissionComponent::ForceProgress(const uint32_t missionId, const uint32_t taskId, const int32_t value, const bool acceptMission) {
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mission = GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
if (!acceptMission) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
AcceptMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission = GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* element : mission->GetTasks()) {
|
|
|
|
if (element->GetClientInfo().uid != taskId) continue;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
element->AddProgress(value);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!mission->IsComplete()) {
|
|
|
|
mission->CheckCompletion();
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MissionComponent::ForceProgressTaskType(const uint32_t missionId, const uint32_t taskType, const int32_t value, const bool acceptMission) {
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mission = GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
if (!acceptMission) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
CDMissions missionInfo;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!GetMissionInfo(missionId, missionInfo)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (missionInfo.isMission) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
AcceptMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission = GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* element : mission->GetTasks()) {
|
2023-01-22 23:38:47 +00:00
|
|
|
if (element->GetType() != static_cast<eMissionTaskType>(taskType)) continue;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
element->AddProgress(value);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!mission->IsComplete()) {
|
|
|
|
mission->CheckCompletion();
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void MissionComponent::ForceProgressValue(uint32_t missionId, uint32_t taskType, int32_t value, bool acceptMission) {
|
|
|
|
auto* mission = GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
if (!acceptMission) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
CDMissions missionInfo;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!GetMissionInfo(missionId, missionInfo)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (missionInfo.isMission) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
AcceptMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission = GetMission(missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* element : mission->GetTasks()) {
|
2023-01-22 23:38:47 +00:00
|
|
|
if (element->GetType() != static_cast<eMissionTaskType>(taskType) || !element->InAllTargets(value)) continue;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
element->AddProgress(1);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!mission->IsComplete()) {
|
|
|
|
mission->CheckCompletion();
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MissionComponent::GetMissionInfo(uint32_t missionId, CDMissions& result) {
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* missionsTable = CDClientManager::Instance().GetTable<CDMissionsTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto missions = missionsTable->Query([=](const CDMissions& entry) {
|
|
|
|
return entry.id == static_cast<int>(missionId);
|
|
|
|
});
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (missions.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
result = missions[0];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#define MISSION_NEW_METHOD
|
|
|
|
|
2023-01-22 23:38:47 +00:00
|
|
|
bool MissionComponent::LookForAchievements(eMissionTaskType type, int32_t value, bool progress, LWOOBJID associate, const std::string& targets, int32_t count) {
|
2021-12-05 17:54:36 +00:00
|
|
|
#ifdef MISSION_NEW_METHOD
|
2022-07-28 13:39:57 +00:00
|
|
|
// Query for achievments, using the cache
|
|
|
|
const auto& result = QueryAchievements(type, value, targets);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool any = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const uint32_t missionID : result) {
|
|
|
|
// Check if we already have this achievement
|
|
|
|
if (GetMission(missionID) != nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Check if we can accept this achievement
|
|
|
|
if (!MissionPrerequisites::CanAccept(missionID, m_Missions)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Instantiate new mission and accept it
|
|
|
|
auto* instance = new Mission(this, missionID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Missions.insert_or_assign(missionID, instance);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-31 03:56:21 +00:00
|
|
|
if (instance->IsMission()) instance->SetUniqueMissionOrderID(++m_LastUsedMissionOrderUID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
instance->Accept();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
any = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (progress) {
|
|
|
|
// Progress mission to bring it up to speed
|
|
|
|
instance->Progress(type, value, associate, targets, count);
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return any;
|
2021-12-05 17:54:36 +00:00
|
|
|
#else
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* missionTasksTable = CDClientManager::Instance().GetTable<CDMissionTasksTable>();
|
|
|
|
auto* missionsTable = CDClientManager::Instance().GetTable<CDMissionsTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto tasks = missionTasksTable->Query([=](const CDMissionTasks& entry) {
|
|
|
|
return entry.taskType == static_cast<unsigned>(type);
|
|
|
|
});
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto any = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& task : tasks) {
|
|
|
|
if (GetMission(task.id) != nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto missionEntries = missionsTable->Query([=](const CDMissions& entry) {
|
|
|
|
return entry.id == static_cast<int>(task.id) && !entry.isMission;
|
|
|
|
});
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (missionEntries.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto mission = missionEntries[0];
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission.isMission || !MissionPrerequisites::CanAccept(mission.id, m_Missions)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (task.target != value && task.targetGroup != targets) {
|
|
|
|
auto stream = std::istringstream(task.targetGroup);
|
|
|
|
std::string token;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto found = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
while (std::getline(stream, token, ',')) {
|
|
|
|
try {
|
|
|
|
const auto target = std::stoul(token);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
found = target == value;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (found) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (std::invalid_argument& exception) {
|
|
|
|
Game::logger->Log("MissionComponent", "Failed to parse target (%s): (%s)!", token.c_str(), exception.what());
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* instance = new Mission(this, mission.id);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Missions.insert_or_assign(mission.id, instance);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-31 03:56:21 +00:00
|
|
|
if (instance->IsMission()) instance->SetUniqueMissionOrderID(++m_LastUsedMissionOrderUID);
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
instance->Accept();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
any = true;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (progress) {
|
|
|
|
instance->Progress(type, value, associate, targets, count);
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return any;
|
2021-12-05 17:54:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-22 23:38:47 +00:00
|
|
|
const std::vector<uint32_t>& MissionComponent::QueryAchievements(eMissionTaskType type, int32_t value, const std::string targets) {
|
2022-07-28 13:39:57 +00:00
|
|
|
// Create a hash which represent this query for achievements
|
2023-01-21 15:37:09 +00:00
|
|
|
AchievementCacheKey toFind;
|
|
|
|
toFind.SetType(type);
|
|
|
|
toFind.SetValue(value);
|
|
|
|
toFind.SetTargets(targets);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2023-01-21 15:37:09 +00:00
|
|
|
const auto& iter = m_AchievementCache.find(toFind);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Check if this query is cached
|
|
|
|
if (iter != m_AchievementCache.end()) {
|
|
|
|
return iter->second;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Find relevent tables
|
2023-03-17 14:36:21 +00:00
|
|
|
auto* missionTasksTable = CDClientManager::Instance().GetTable<CDMissionTasksTable>();
|
|
|
|
auto* missionsTable = CDClientManager::Instance().GetTable<CDMissionsTable>();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
std::vector<uint32_t> result;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Loop through all mission tasks, might cache this task check later
|
|
|
|
for (const auto& task : missionTasksTable->GetEntries()) {
|
|
|
|
if (task.taskType != static_cast<uint32_t>(type)) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Seek the assosicated mission
|
|
|
|
auto foundMission = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
const auto& mission = missionsTable->GetByMissionID(task.id, foundMission);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!foundMission || mission.isMission) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Compare the easy values
|
|
|
|
if (task.target == value || task.targetGroup == targets) {
|
|
|
|
result.push_back(mission.id);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
// Compare the target group, array separated by ','
|
|
|
|
auto stream = std::istringstream(task.targetGroup);
|
|
|
|
std::string token;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
while (std::getline(stream, token, ',')) {
|
|
|
|
try {
|
|
|
|
if (std::stoi(token) == value) {
|
|
|
|
result.push_back(mission.id);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (std::invalid_argument& exception) {
|
|
|
|
// Ignored
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Insert into cache
|
2023-01-21 15:37:09 +00:00
|
|
|
m_AchievementCache.insert_or_assign(toFind, result);
|
|
|
|
return m_AchievementCache.find(toFind)->second;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MissionComponent::RequiresItem(const LOT lot) {
|
2022-07-28 13:39:57 +00:00
|
|
|
auto query = CDClientDatabase::CreatePreppedStmt(
|
|
|
|
"SELECT type FROM Objects WHERE id = ?;");
|
|
|
|
query.bind(1, (int)lot);
|
2022-01-13 03:48:27 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto result = query.execQuery();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (result.eof()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!result.fieldIsNull(0)) {
|
|
|
|
const auto type = std::string(result.getStringField(0));
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
result.finalize();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (type == "Powerup") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
result.finalize();
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& pair : m_Missions) {
|
|
|
|
auto* mission = pair.second;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission->IsComplete()) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (auto* task : mission->GetTasks()) {
|
2023-01-22 23:38:47 +00:00
|
|
|
if (task->IsComplete() || task->GetType() != eMissionTaskType::GATHER) {
|
2022-07-28 13:39:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (!task->InAllTargets(lot)) {
|
|
|
|
continue;
|
|
|
|
}
|
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
|
|
|
|
2023-01-22 23:38:47 +00:00
|
|
|
const auto required = LookForAchievements(eMissionTaskType::GATHER, lot, false);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
return required;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MissionComponent::LoadFromXml(tinyxml2::XMLDocument* doc) {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (doc == nullptr) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mis = doc->FirstChildElement("obj")->FirstChildElement("mis");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mis == nullptr) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* cur = mis->FirstChildElement("cur");
|
|
|
|
auto* done = mis->FirstChildElement("done");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* doneM = done->FirstChildElement();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
while (doneM) {
|
|
|
|
int missionId;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
doneM->QueryAttribute("id", &missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mission = new Mission(this, missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission->LoadFromXml(doneM);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
doneM = doneM->NextSiblingElement();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Missions.insert_or_assign(missionId, mission);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* currentM = cur->FirstChildElement();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-31 03:56:21 +00:00
|
|
|
uint32_t missionOrder{};
|
2022-07-28 13:39:57 +00:00
|
|
|
while (currentM) {
|
|
|
|
int missionId;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
currentM->QueryAttribute("id", &missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mission = new Mission(this, missionId);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission->LoadFromXml(currentM);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-31 03:56:21 +00:00
|
|
|
if (currentM->QueryAttribute("o", &missionOrder) == tinyxml2::XML_SUCCESS && mission->IsMission()) {
|
|
|
|
mission->SetUniqueMissionOrderID(missionOrder);
|
|
|
|
if (missionOrder > m_LastUsedMissionOrderUID) m_LastUsedMissionOrderUID = missionOrder;
|
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
currentM = currentM->NextSiblingElement();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Missions.insert_or_assign(missionId, mission);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MissionComponent::UpdateXml(tinyxml2::XMLDocument* doc) {
|
2022-07-28 13:39:57 +00:00
|
|
|
if (doc == nullptr) return;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto shouldInsertMis = false;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* obj = doc->FirstChildElement("obj");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* mis = obj->FirstChildElement("mis");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mis == nullptr) {
|
|
|
|
mis = doc->NewElement("mis");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
shouldInsertMis = true;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mis->DeleteChildren();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
auto* done = doc->NewElement("done");
|
|
|
|
auto* cur = doc->NewElement("cur");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
for (const auto& pair : m_Missions) {
|
|
|
|
auto* mission = pair.second;
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (mission) {
|
|
|
|
const auto complete = mission->IsComplete();
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-31 03:56:21 +00:00
|
|
|
auto* m = doc->NewElement("m");
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-31 03:56:21 +00:00
|
|
|
if (complete) {
|
2022-07-28 13:39:57 +00:00
|
|
|
mission->UpdateXml(m);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
done->LinkEndChild(m);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-07-31 03:56:21 +00:00
|
|
|
if (mission->IsMission()) m->SetAttribute("o", mission->GetUniqueMissionOrderID());
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mission->UpdateXml(m);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
cur->LinkEndChild(m);
|
|
|
|
}
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
mis->InsertFirstChild(done);
|
|
|
|
mis->InsertEndChild(cur);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
if (shouldInsertMis) {
|
|
|
|
obj->LinkEndChild(mis);
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
void MissionComponent::AddCollectible(int32_t collectibleID) {
|
|
|
|
// Check if this collectible is already in the list
|
|
|
|
if (HasCollectible(collectibleID)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
m_Collectibles.push_back(collectibleID);
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool MissionComponent::HasCollectible(int32_t collectibleID) {
|
|
|
|
return std::find(m_Collectibles.begin(), m_Collectibles.end(), collectibleID) != m_Collectibles.end();
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:39:57 +00:00
|
|
|
bool MissionComponent::HasMission(uint32_t missionId) {
|
|
|
|
return GetMission(missionId) != nullptr;
|
|
|
|
}
|