mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-12-03 06:38:27 +00:00
Public release of the DLU server code!
Have fun!
This commit is contained in:
585
dGame/dMission/Mission.cpp
Normal file
585
dGame/dMission/Mission.cpp
Normal file
@@ -0,0 +1,585 @@
|
||||
#include "Mission.h"
|
||||
|
||||
#include <ctime>
|
||||
|
||||
#include "CDClientManager.h"
|
||||
#include "Character.h"
|
||||
#include "CharacterComponent.h"
|
||||
#include "DestroyableComponent.h"
|
||||
#include "EntityManager.h"
|
||||
#include "Game.h"
|
||||
#include "GameMessages.h"
|
||||
#include "Mail.h"
|
||||
#include "MissionComponent.h"
|
||||
#include "dLocale.h"
|
||||
#include "dLogger.h"
|
||||
#include "dServer.h"
|
||||
|
||||
Mission::Mission(MissionComponent* missionComponent, const uint32_t missionId) {
|
||||
m_MissionComponent = missionComponent;
|
||||
|
||||
m_Completions = 0;
|
||||
|
||||
m_Timestamp = 0;
|
||||
|
||||
m_Reward = 0;
|
||||
|
||||
m_State = MissionState::MISSION_STATE_UNKNOWN;
|
||||
|
||||
auto* missionsTable = CDClientManager::Instance()->GetTable<CDMissionsTable>("Missions");
|
||||
|
||||
info = missionsTable->GetPtrByMissionID(missionId);
|
||||
|
||||
if (info == &CDMissionsTable::Default) {
|
||||
Game::logger->Log("Missions", "Failed to find mission (%i)!\n", missionId);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
auto* tasksTable = CDClientManager::Instance()->GetTable<CDMissionTasksTable>("MissionTasks");
|
||||
|
||||
auto tasks = tasksTable->GetByMissionID(missionId);
|
||||
|
||||
for (auto i = 0U; i < tasks.size(); ++i) {
|
||||
auto* info = tasks[i];
|
||||
|
||||
auto* task = new MissionTask(this, info, i);
|
||||
|
||||
m_Tasks.push_back(task);
|
||||
}
|
||||
}
|
||||
|
||||
void Mission::LoadFromXml(tinyxml2::XMLElement* element) {
|
||||
// Start custom XML
|
||||
if (element->Attribute("state") != nullptr) {
|
||||
m_State = static_cast<MissionState>(std::stoul(element->Attribute("state")));
|
||||
}
|
||||
// End custom XML
|
||||
|
||||
if (element->Attribute("cct") != nullptr) {
|
||||
m_Completions = std::stoul(element->Attribute("cct"));
|
||||
|
||||
m_Timestamp = std::stoul(element->Attribute("cts"));
|
||||
|
||||
if (IsComplete()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
auto* task = element->FirstChildElement();
|
||||
|
||||
auto index = 0U;
|
||||
|
||||
while (task != nullptr) {
|
||||
if (index >= m_Tasks.size()) {
|
||||
break;
|
||||
}
|
||||
|
||||
const auto type = m_Tasks[index]->GetType();
|
||||
|
||||
if (type == MissionTaskType::MISSION_TASK_TYPE_ENVIRONMENT ||
|
||||
type == MissionTaskType::MISSION_TASK_TYPE_VISIT_PROPERTY) {
|
||||
std::vector<uint32_t> uniques;
|
||||
|
||||
const auto value = std::stoul(task->Attribute("v"));
|
||||
|
||||
m_Tasks[index]->SetProgress(value, false);
|
||||
|
||||
task = task->NextSiblingElement();
|
||||
|
||||
while (task != nullptr) {
|
||||
const auto unique = std::stoul(task->Attribute("v"));
|
||||
|
||||
uniques.push_back(unique);
|
||||
|
||||
if (m_MissionComponent != nullptr && type == MissionTaskType::MISSION_TASK_TYPE_ENVIRONMENT) {
|
||||
m_MissionComponent->AddCollectible(unique);
|
||||
}
|
||||
|
||||
task = task->NextSiblingElement();
|
||||
}
|
||||
|
||||
m_Tasks[index]->SetUnique(uniques);
|
||||
|
||||
m_Tasks[index]->SetProgress(uniques.size(), false);
|
||||
|
||||
break;
|
||||
} else {
|
||||
const auto value = std::stoul(task->Attribute("v"));
|
||||
|
||||
m_Tasks[index]->SetProgress(value, false);
|
||||
|
||||
task = task->NextSiblingElement();
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
void Mission::UpdateXml(tinyxml2::XMLElement* element) {
|
||||
// Start custom XML
|
||||
element->SetAttribute("state", static_cast<unsigned int>(m_State));
|
||||
// End custom XML
|
||||
|
||||
element->DeleteChildren();
|
||||
|
||||
element->SetAttribute("id", static_cast<unsigned int>(info->id));
|
||||
|
||||
if (m_Completions > 0) {
|
||||
element->SetAttribute("cct", static_cast<unsigned int>(m_Completions));
|
||||
|
||||
element->SetAttribute("cts", static_cast<unsigned int>(m_Timestamp));
|
||||
|
||||
if (IsComplete()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto* task : m_Tasks) {
|
||||
if (task->GetType() == MissionTaskType::MISSION_TASK_TYPE_ENVIRONMENT ||
|
||||
task->GetType() == MissionTaskType::MISSION_TASK_TYPE_VISIT_PROPERTY) {
|
||||
|
||||
auto* child = element->GetDocument()->NewElement("sv");
|
||||
|
||||
child->SetAttribute("v", static_cast<unsigned int>(task->GetProgress()));
|
||||
|
||||
element->LinkEndChild(child);
|
||||
|
||||
for (auto unique : task->GetUnique()) {
|
||||
auto* uniqueElement = element->GetDocument()->NewElement("sv");
|
||||
|
||||
uniqueElement->SetAttribute("v", static_cast<unsigned int>(unique));
|
||||
|
||||
element->LinkEndChild(uniqueElement);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
auto* child = element->GetDocument()->NewElement("sv");
|
||||
|
||||
child->SetAttribute("v", static_cast<unsigned int>(task->GetProgress()));
|
||||
|
||||
element->LinkEndChild(child);
|
||||
}
|
||||
}
|
||||
|
||||
bool Mission::IsValidMission(const uint32_t missionId) {
|
||||
auto* table = CDClientManager::Instance()->GetTable<CDMissionsTable>("Missions");
|
||||
|
||||
const auto missions = table->Query([=](const CDMissions& entry) {
|
||||
return entry.id == static_cast<int>(missionId);
|
||||
});
|
||||
|
||||
return !missions.empty();
|
||||
}
|
||||
|
||||
bool Mission::IsValidMission(const uint32_t missionId, CDMissions& info) {
|
||||
auto* table = CDClientManager::Instance()->GetTable<CDMissionsTable>("Missions");
|
||||
|
||||
const auto missions = table->Query([=](const CDMissions& entry) {
|
||||
return entry.id == static_cast<int>(missionId);
|
||||
});
|
||||
|
||||
if (missions.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
info = missions[0];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Entity* Mission::GetAssociate() const {
|
||||
return m_MissionComponent->GetParent();
|
||||
}
|
||||
|
||||
User* Mission::GetUser() const {
|
||||
return GetAssociate()->GetParentUser();
|
||||
}
|
||||
|
||||
uint32_t Mission::GetMissionId() const {
|
||||
return info->id;
|
||||
}
|
||||
|
||||
const CDMissions& Mission::GetClientInfo() const {
|
||||
return *info;
|
||||
}
|
||||
|
||||
uint32_t Mission::GetCompletions() const {
|
||||
return m_Completions;
|
||||
}
|
||||
|
||||
uint32_t Mission::GetTimestamp() const {
|
||||
return m_Timestamp;
|
||||
}
|
||||
|
||||
LOT Mission::GetReward() const {
|
||||
return m_Reward;
|
||||
}
|
||||
|
||||
std::vector<MissionTask*> Mission::GetTasks() const {
|
||||
return m_Tasks;
|
||||
}
|
||||
|
||||
MissionState Mission::GetMissionState() const {
|
||||
return m_State;
|
||||
}
|
||||
|
||||
bool Mission::IsAchievement() const {
|
||||
return !info->isMission;
|
||||
}
|
||||
|
||||
bool Mission::IsMission() const {
|
||||
return info->isMission;
|
||||
}
|
||||
|
||||
bool Mission::IsRepeatable() const {
|
||||
return info->repeatable;
|
||||
}
|
||||
|
||||
bool Mission::IsComplete() const {
|
||||
return m_State == MissionState::MISSION_STATE_COMPLETE;
|
||||
}
|
||||
|
||||
bool Mission::IsActive() const {
|
||||
return m_State == MissionState::MISSION_STATE_ACTIVE || m_State == MissionState::MISSION_STATE_COMPLETE_AVAILABLE;
|
||||
}
|
||||
|
||||
void Mission::MakeActive() {
|
||||
SetMissionState(m_Completions == 0 ? MissionState::MISSION_STATE_ACTIVE : MissionState::MISSION_STATE_COMPLETE_ACTIVE);
|
||||
}
|
||||
|
||||
bool Mission::IsReadyToComplete() const {
|
||||
return m_State == MissionState::MISSION_STATE_READY_TO_COMPLETE || m_State == MissionState::MISSION_STATE_COMPLETE_READY_TO_COMPLETE;
|
||||
}
|
||||
|
||||
void Mission::MakeReadyToComplete() {
|
||||
SetMissionState(m_Completions == 0 ? MissionState::MISSION_STATE_READY_TO_COMPLETE : MissionState::MISSION_STATE_COMPLETE_READY_TO_COMPLETE);
|
||||
}
|
||||
|
||||
bool Mission::IsAvalible() const {
|
||||
return m_State == MissionState::MISSION_STATE_AVAILABLE || m_State == MissionState::MISSION_STATE_COMPLETE_AVAILABLE;
|
||||
}
|
||||
|
||||
bool Mission::IsFetchMission() const {
|
||||
return m_Tasks.size() == 1 && m_Tasks[0]->GetType() == MissionTaskType::MISSION_TASK_TYPE_MISSION_INTERACTION;
|
||||
}
|
||||
|
||||
void Mission::MakeAvalible() {
|
||||
SetMissionState(m_Completions == 0 ? MissionState::MISSION_STATE_AVAILABLE : MissionState::MISSION_STATE_COMPLETE_AVAILABLE);
|
||||
}
|
||||
|
||||
void Mission::Accept() {
|
||||
SetMissionTypeState(MissionLockState::MISSION_LOCK_NEW, info->defined_type, info->defined_subtype);
|
||||
|
||||
SetMissionState(m_Completions > 0 ? MissionState::MISSION_STATE_COMPLETE_ACTIVE : MissionState::MISSION_STATE_ACTIVE);
|
||||
|
||||
Catchup();
|
||||
}
|
||||
|
||||
void Mission::Complete(const bool yieldRewards) {
|
||||
if (m_State != MissionState::MISSION_STATE_ACTIVE && m_State != MissionState::MISSION_STATE_COMPLETE_ACTIVE) {
|
||||
Accept();
|
||||
}
|
||||
|
||||
for (auto* task : m_Tasks) {
|
||||
task->Complete();
|
||||
}
|
||||
|
||||
SetMissionState(MissionState::MISSION_STATE_REWARDING, true);
|
||||
|
||||
if (yieldRewards) {
|
||||
YieldRewards();
|
||||
}
|
||||
|
||||
SetMissionState(MissionState::MISSION_STATE_COMPLETE);
|
||||
|
||||
m_Completions++;
|
||||
|
||||
m_Timestamp = std::time(nullptr);
|
||||
|
||||
auto* entity = GetAssociate();
|
||||
|
||||
if (entity == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto* characterComponent = entity->GetComponent<CharacterComponent>();
|
||||
if (characterComponent != nullptr) {
|
||||
characterComponent->TrackMissionCompletion(!info->isMission);
|
||||
}
|
||||
|
||||
auto* missionComponent = entity->GetComponent<MissionComponent>();
|
||||
|
||||
missionComponent->Progress(MissionTaskType::MISSION_TASK_TYPE_MISSION_COMPLETE, info->id);
|
||||
|
||||
auto* missionEmailTable = CDClientManager::Instance()->GetTable<CDMissionEmailTable>("MissionEmail");
|
||||
|
||||
const auto missionId = GetMissionId();
|
||||
|
||||
const auto missionEmails = missionEmailTable->Query([missionId](const CDMissionEmail& entry) {
|
||||
return entry.missionID == missionId;
|
||||
});
|
||||
|
||||
for (const auto& email : missionEmails) {
|
||||
const auto missionEmailBase = "MissionEmail_" + std::to_string(email.ID) + "_";
|
||||
|
||||
const auto senderLocale = missionEmailBase + "senderName";
|
||||
const auto announceLocale = missionEmailBase + "announceText";
|
||||
|
||||
if (email.messageType == 1 && Game::locale->HasPhrase(senderLocale)) {
|
||||
const auto subject = dLocale::GetTemplate(missionEmailBase + "subjectText");
|
||||
const auto body = dLocale::GetTemplate(missionEmailBase + "bodyText");
|
||||
const auto sender = dLocale::GetTemplate(senderLocale);
|
||||
|
||||
Mail::SendMail(LWOOBJID_EMPTY, sender, GetAssociate(), subject, body, email.attachmentLOT, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Mission::CheckCompletion() {
|
||||
for (auto* task : m_Tasks) {
|
||||
if (!task->IsComplete()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (IsAchievement()) {
|
||||
Complete();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
SetMissionState(MissionState::MISSION_STATE_READY_TO_COMPLETE);
|
||||
}
|
||||
|
||||
void Mission::Catchup() {
|
||||
auto* entity = GetAssociate();
|
||||
|
||||
auto* inventory = static_cast<InventoryComponent*>(entity->GetComponent(COMPONENT_TYPE_INVENTORY));
|
||||
|
||||
for (auto* task : m_Tasks) {
|
||||
const auto type = task->GetType();
|
||||
|
||||
if (type == MissionTaskType::MISSION_TASK_TYPE_ITEM_COLLECTION) {
|
||||
for (auto target : task->GetAllTargets()) {
|
||||
const auto count = inventory->GetLotCountNonTransfer(target);
|
||||
|
||||
for (auto i = 0U; i < count; ++i) {
|
||||
task->Progress(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type == MissionTaskType::MISSION_TASK_TYPE_PLAYER_FLAG) {
|
||||
for (auto target : task->GetAllTargets()) {
|
||||
const auto flag = GetUser()->GetLastUsedChar()->GetPlayerFlag(target);
|
||||
|
||||
if (!flag) {
|
||||
continue;
|
||||
}
|
||||
|
||||
task->Progress(target);
|
||||
|
||||
if (task->IsComplete()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Mission::YieldRewards() {
|
||||
auto* entity = GetAssociate();
|
||||
|
||||
if (entity == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto* character = GetUser()->GetLastUsedChar();
|
||||
|
||||
auto* inventoryComponent = entity->GetComponent<InventoryComponent>();
|
||||
auto* characterComponent = entity->GetComponent<CharacterComponent>();
|
||||
auto* destroyableComponent = entity->GetComponent<DestroyableComponent>();
|
||||
auto* missionComponent = entity->GetComponent<MissionComponent>();
|
||||
|
||||
// Remove mission items
|
||||
for (auto* task : m_Tasks) {
|
||||
if (task->GetType() != MissionTaskType::MISSION_TASK_TYPE_ITEM_COLLECTION) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto& param = task->GetParameters();
|
||||
|
||||
if (param.empty() || (param[0] & 1) == 0) // Should items be removed?
|
||||
{
|
||||
for (const auto target : task->GetAllTargets()) {
|
||||
inventoryComponent->RemoveItem(target, task->GetClientInfo().targetValue);
|
||||
|
||||
missionComponent->Progress(MissionTaskType::MISSION_TASK_TYPE_ITEM_COLLECTION, target, LWOOBJID_EMPTY, "", -task->GetClientInfo().targetValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (info->LegoScore > 0) {
|
||||
characterComponent->SetUScore(characterComponent->GetUScore() + info->LegoScore);
|
||||
|
||||
if (info->isMission) {
|
||||
GameMessages::SendModifyLEGOScore(entity, entity->GetSystemAddress(), info->LegoScore, 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (m_Completions > 0) {
|
||||
std::vector<std::pair<LOT, uint32_t>> items;
|
||||
|
||||
items.emplace_back(info->reward_item1_repeatable, info->reward_item1_repeat_count);
|
||||
items.emplace_back(info->reward_item2_repeatable, info->reward_item2_repeat_count);
|
||||
items.emplace_back(info->reward_item3_repeatable, info->reward_item3_repeat_count);
|
||||
items.emplace_back(info->reward_item4_repeatable, info->reward_item4_repeat_count);
|
||||
|
||||
for (const auto& pair : items) {
|
||||
if (pair.second <= 0 || (m_Reward > 0 && pair.first != m_Reward)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto count = pair.second > 0 ? pair.second : 1;
|
||||
|
||||
// Sanitfy check, 6 is the max any mission yields
|
||||
if (count > 6) {
|
||||
count = 0;
|
||||
}
|
||||
|
||||
inventoryComponent->AddItem(pair.first, count);
|
||||
}
|
||||
|
||||
if (info->reward_currency_repeatable > 0) {
|
||||
character->SetCoins(character->GetCoins() + info->reward_currency_repeatable);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::pair<LOT, int32_t>> items;
|
||||
|
||||
items.emplace_back(info->reward_item1, info->reward_item1_count);
|
||||
items.emplace_back(info->reward_item2, info->reward_item2_count);
|
||||
items.emplace_back(info->reward_item3, info->reward_item3_count);
|
||||
items.emplace_back(info->reward_item4, info->reward_item4_count);
|
||||
|
||||
for (const auto& pair : items) {
|
||||
if (pair.second < 0 || (m_Reward > 0 && pair.first != m_Reward)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto count = pair.second > 0 ? pair.second : 1;
|
||||
|
||||
// Sanitfy check, 6 is the max any mission yields
|
||||
if (count > 6) {
|
||||
count = 0;
|
||||
}
|
||||
|
||||
inventoryComponent->AddItem(pair.first, count);
|
||||
}
|
||||
|
||||
if (info->reward_currency > 0) {
|
||||
character->SetCoins(character->GetCoins() + info->reward_currency, info->isMission);
|
||||
}
|
||||
|
||||
if (info->reward_maxinventory > 0) {
|
||||
auto* inventory = inventoryComponent->GetInventory(ITEMS);
|
||||
|
||||
inventory->SetSize(inventory->GetSize() + info->reward_maxinventory);
|
||||
}
|
||||
|
||||
if (info->reward_bankinventory > 0) {
|
||||
auto* inventory = inventoryComponent->GetInventory(VAULT_ITEMS);
|
||||
|
||||
inventory->SetSize(inventory->GetSize() + info->reward_bankinventory);
|
||||
}
|
||||
|
||||
if (info->reward_reputation > 0) {
|
||||
// TODO: In case of reputation, write code
|
||||
}
|
||||
|
||||
if (info->reward_maxhealth > 0) {
|
||||
destroyableComponent->SetMaxHealth(destroyableComponent->GetMaxHealth() + static_cast<float>(info->reward_maxhealth));
|
||||
}
|
||||
|
||||
if (info->reward_maximagination > 0) {
|
||||
destroyableComponent->SetMaxImagination(destroyableComponent->GetMaxImagination() + static_cast<float>(info->reward_maximagination));
|
||||
}
|
||||
|
||||
EntityManager::Instance()->SerializeEntity(entity);
|
||||
|
||||
if (info->reward_emote > 0) {
|
||||
character->UnlockEmote(info->reward_emote);
|
||||
}
|
||||
|
||||
if (info->reward_emote2 > 0) {
|
||||
character->UnlockEmote(info->reward_emote2);
|
||||
}
|
||||
|
||||
if (info->reward_emote3 > 0) {
|
||||
character->UnlockEmote(info->reward_emote3);
|
||||
}
|
||||
|
||||
if (info->reward_emote4 > 0) {
|
||||
character->UnlockEmote(info->reward_emote4);
|
||||
}
|
||||
}
|
||||
|
||||
void Mission::Progress(MissionTaskType type, int32_t value, LWOOBJID associate, const std::string& targets, int32_t count) {
|
||||
const auto isRemoval = count < 0;
|
||||
|
||||
if (isRemoval && (IsComplete() || IsAchievement())) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto* task : m_Tasks) {
|
||||
if (task->IsComplete() && !isRemoval) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (task->GetType() != type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isRemoval && !task->InAllTargets(value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
task->Progress(value, associate, targets, count);
|
||||
}
|
||||
}
|
||||
|
||||
void Mission::SetMissionState(const MissionState state, const bool sendingRewards) {
|
||||
this->m_State = state;
|
||||
|
||||
auto* entity = GetAssociate();
|
||||
|
||||
if (entity == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
GameMessages::SendNotifyMission(entity, entity->GetParentUser()->GetSystemAddress(), info->id, static_cast<int>(state), sendingRewards);
|
||||
}
|
||||
|
||||
void Mission::SetMissionTypeState(MissionLockState state, const std::string& type, const std::string& subType) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
void Mission::SetCompletions(const uint32_t value) {
|
||||
m_Completions = value;
|
||||
}
|
||||
|
||||
void Mission::SetReward(const LOT lot) {
|
||||
m_Reward = lot;
|
||||
}
|
||||
|
||||
Mission::~Mission() {
|
||||
for (auto* task : m_Tasks) {
|
||||
delete task;
|
||||
}
|
||||
|
||||
m_Tasks.clear();
|
||||
}
|
||||
261
dGame/dMission/Mission.h
Normal file
261
dGame/dMission/Mission.h
Normal file
@@ -0,0 +1,261 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "CDMissionsTable.h"
|
||||
#include "MissionTask.h"
|
||||
#include "dCommonVars.h"
|
||||
#include "Entity.h"
|
||||
#include "MissionState.h"
|
||||
#include "MissionLockState.h"
|
||||
|
||||
class MissionComponent;
|
||||
|
||||
/**
|
||||
* A mission (or achievement) that a player may unlock, progress and complete.
|
||||
*/
|
||||
class Mission final
|
||||
{
|
||||
public:
|
||||
Mission(MissionComponent* missionComponent, uint32_t missionId);
|
||||
~Mission();
|
||||
|
||||
void LoadFromXml(tinyxml2::XMLElement* element);
|
||||
void UpdateXml(tinyxml2::XMLElement* element);
|
||||
|
||||
/**
|
||||
* Returns the ID of this mission
|
||||
* @return the ID of this mission
|
||||
*/
|
||||
uint32_t GetMissionId() const;
|
||||
|
||||
/**
|
||||
* Returns the entity that is currently progressing this mission
|
||||
* @return the entity that is currently progressing this mission
|
||||
*/
|
||||
Entity* GetAssociate() const;
|
||||
|
||||
/**
|
||||
* Returns the account owns the entity that is currently progressing this mission
|
||||
* @return the account owns the entity that is currently progressing this mission
|
||||
*/
|
||||
User* GetUser() const;
|
||||
|
||||
/**
|
||||
* Returns the current state of this mission
|
||||
* @return the current state of this mission
|
||||
*/
|
||||
MissionState GetMissionState() const;
|
||||
|
||||
/**
|
||||
* Returns the database information that represents to this mission.
|
||||
* @return the database information that represents to this mission.
|
||||
*/
|
||||
const CDMissions& GetClientInfo() const;
|
||||
|
||||
/**
|
||||
* Returns the number of times the entity has completed this mission, can only be > 0 for dailies.
|
||||
* @return the number of thimes the entity has completed this mission
|
||||
*/
|
||||
uint32_t GetCompletions() const;
|
||||
|
||||
/**
|
||||
* Sets the number of times this mission has been completed
|
||||
* @param value the number of times this mission should be completed
|
||||
*/
|
||||
void SetCompletions(uint32_t value);
|
||||
|
||||
/**
|
||||
* Returns the last timestamp at which the entity completed this mission
|
||||
* @return the last timestamp at which the entity completed this mission
|
||||
*/
|
||||
uint32_t GetTimestamp() const;
|
||||
|
||||
/**
|
||||
* Returns some specific reward that should be returned from the possible rewards indicated by the client
|
||||
* @return some specific reward that should be returned from the possible rewards indicated by the client
|
||||
*/
|
||||
LOT GetReward() const;
|
||||
|
||||
/**
|
||||
* Sets an some specific reward that should be returned from the possible rewards indicated by the client
|
||||
* @param lot the reward to set
|
||||
*/
|
||||
void SetReward(LOT lot);
|
||||
|
||||
/**
|
||||
* Returns all the tasks that must be completed to mark this mission as complete
|
||||
* @return all the tasks that must be completed to mark this mission as complete
|
||||
*/
|
||||
std::vector<MissionTask*> GetTasks() const;
|
||||
|
||||
/**
|
||||
* Updates the mission state to the one provided
|
||||
* @param state the mission state to set
|
||||
* @param sendingRewards a flag indicating to the client that rewards wil lfollow
|
||||
*/
|
||||
void SetMissionState(MissionState state, bool sendingRewards = false);
|
||||
|
||||
/**
|
||||
* Currently unimplemented
|
||||
*/
|
||||
void SetMissionTypeState(MissionLockState state, const std::string& type, const std::string& subType);
|
||||
|
||||
/**
|
||||
* Returns whether this mission is an achievement
|
||||
* @return true if this mission is an achievement, false otherwise
|
||||
*/
|
||||
bool IsAchievement() const;
|
||||
|
||||
/**
|
||||
* Returns whether this mission is a mission (e.g.: not an achievement)
|
||||
* @return true if this mission is not an achievement, false otherwise
|
||||
*/
|
||||
bool IsMission() const;
|
||||
|
||||
/**
|
||||
* Returns whether this mission can be repeated (mostly used for dailies)
|
||||
* @return true if this mission can be repeated, false otherwise
|
||||
*/
|
||||
bool IsRepeatable() const;
|
||||
|
||||
/**
|
||||
* Returns whether the entity has completed this mission before
|
||||
* @return true if the mission has been completed before, false otherwise
|
||||
*/
|
||||
bool IsComplete() const;
|
||||
|
||||
/**
|
||||
* Returns whether the mission is currently active
|
||||
* @return true if the mission is currently active, false otherwise
|
||||
*/
|
||||
bool IsActive() const;
|
||||
|
||||
/**
|
||||
* Sets the mission state to active, takes into account if this is a repeatable mission.
|
||||
*/
|
||||
void MakeActive();
|
||||
|
||||
/**
|
||||
* Returns whether the entity has completed all tasks and can hand the mission in for rewards.
|
||||
* @return true if the entity can hand the mission in, false otherwise
|
||||
*/
|
||||
bool IsReadyToComplete() const;
|
||||
|
||||
/**
|
||||
* Sets the mission state to ready to complete, takes into account if this is a repeatable mission
|
||||
*/
|
||||
void MakeReadyToComplete();
|
||||
|
||||
/**
|
||||
* Returns whether this mission can be accepted by the entity
|
||||
* @return true if the mission can be accepted by the entity, false otherwise
|
||||
*/
|
||||
bool IsAvalible() const;
|
||||
|
||||
/**
|
||||
* Sets the mission state to available, takes into account if this mission is repeatable
|
||||
*/
|
||||
void MakeAvalible();
|
||||
|
||||
/**
|
||||
* Returns whether this mission is one where an entity simply has to go somewhere, but doesn't have to turn in the
|
||||
* mission tasks at the original mission giver (called a fetch mission).
|
||||
* @return true if this is a fetch mission, false otherwise
|
||||
*/
|
||||
bool IsFetchMission() const;
|
||||
|
||||
/**
|
||||
* Accepts this mission, setting it to available. Also progresses any of the tasks if the entity has already
|
||||
* progressed for them (for example "collect X bricks", will fast track for the amount of bricks the entity
|
||||
* already has).
|
||||
*/
|
||||
void Accept();
|
||||
|
||||
/**
|
||||
* Completes the mission and handles all logistics regarding that: checking all tasks, handing out rewards,
|
||||
* emailing them if the inventory is full, etc. If the mission tasks have not all been completed this is a no-op.
|
||||
* @param yieldRewards if true, rewards will be given to the entity
|
||||
*/
|
||||
void Complete(bool yieldRewards = true);
|
||||
|
||||
/**
|
||||
* Checks if this mission is ready to be completed and updates the state if so. If this is an achievement, the
|
||||
* state will automatically be updated to completed as there's nobody to hand achievements in to.
|
||||
*/
|
||||
void CheckCompletion();
|
||||
|
||||
/**
|
||||
* Gives all the rewards (items, score, stats, etc.) to the entity. Takes into account if the entity has completed
|
||||
* the mission before.
|
||||
*/
|
||||
void YieldRewards();
|
||||
|
||||
/**
|
||||
* Attempts to progress tasks of a certain type for this mission. Note that the interpretation of any of these
|
||||
* arguments is up to the mission task at hand.
|
||||
* @param type the mission task type to progress
|
||||
* @param value the value to progress the mission task with
|
||||
* @param associate optional object ID that was related to the progression
|
||||
* @param targets optional multiple targets that need to be met for progression
|
||||
* @param count optional count to progress with
|
||||
*/
|
||||
void Progress(MissionTaskType type, int32_t value, LWOOBJID associate = 0, const std::string& targets = "", int32_t count = 1);
|
||||
|
||||
/**
|
||||
* Returns if the mission ID that's given belongs to an existing mission
|
||||
* @param missionId the mission ID to check for
|
||||
* @return true if the mission exists, false otherwise
|
||||
*/
|
||||
static bool IsValidMission(uint32_t missionId);
|
||||
|
||||
/**
|
||||
* Returns if the mission ID that's given belongs to an existing mission
|
||||
* @param missionId the mission ID to check for
|
||||
* @param info variable to store the queried mission information in
|
||||
* @return true if the mission exists, false otherwise
|
||||
*/
|
||||
static bool IsValidMission(uint32_t missionId, CDMissions& info);
|
||||
private:
|
||||
/**
|
||||
* Progresses all the newly accepted tasks for this mission after it has been accepted to reflect the state of the
|
||||
* inventory of the entity.
|
||||
*/
|
||||
void Catchup();
|
||||
|
||||
/**
|
||||
* The database information that corresponds to this mission
|
||||
*/
|
||||
const CDMissions* info;
|
||||
|
||||
/**
|
||||
* The current state this mission is in
|
||||
*/
|
||||
MissionState m_State;
|
||||
|
||||
/**
|
||||
* The number of times the entity has completed this mission
|
||||
*/
|
||||
uint32_t m_Completions;
|
||||
|
||||
/**
|
||||
* The last time the entity completed this mission
|
||||
*/
|
||||
uint32_t m_Timestamp;
|
||||
|
||||
/**
|
||||
* The mission component of the entity that owns this mission
|
||||
*/
|
||||
MissionComponent* m_MissionComponent;
|
||||
|
||||
/**
|
||||
* Optionally specific reward that should be returned from the possible rewards indicated by the client
|
||||
*/
|
||||
LOT m_Reward;
|
||||
|
||||
/**
|
||||
* All the tasks that can be progressed for this mission
|
||||
*/
|
||||
std::vector<MissionTask*> m_Tasks;
|
||||
};
|
||||
8
dGame/dMission/MissionLockState.h
Normal file
8
dGame/dMission/MissionLockState.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
enum class MissionLockState : int
|
||||
{
|
||||
MISSION_LOCK_LOCKED,
|
||||
MISSION_LOCK_NEW,
|
||||
MISSION_LOCK_UNLOCKED,
|
||||
};
|
||||
185
dGame/dMission/MissionPrerequisites.cpp
Normal file
185
dGame/dMission/MissionPrerequisites.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
#include "MissionPrerequisites.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <ctime>
|
||||
|
||||
#include "CDClientManager.h"
|
||||
#include "dLogger.h"
|
||||
|
||||
|
||||
PrerequisiteExpression::PrerequisiteExpression(const std::string& str) {
|
||||
std::stringstream a;
|
||||
std::stringstream b;
|
||||
std::stringstream s;
|
||||
|
||||
auto bor = false;
|
||||
|
||||
auto sub = false;
|
||||
|
||||
auto done = false;
|
||||
|
||||
for (auto i = 0u; i < str.size(); ++i) {
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
|
||||
const auto character = str[i];
|
||||
|
||||
switch (character) {
|
||||
case '|':
|
||||
bor = true;
|
||||
b << str.substr(i + 1);
|
||||
done = true;
|
||||
break;
|
||||
case ' ':
|
||||
case ')':
|
||||
break;
|
||||
case ',':
|
||||
case '&':
|
||||
case '(':
|
||||
b << str.substr(i + 1);
|
||||
done = true;
|
||||
break;
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
if (sub) {
|
||||
s << character;
|
||||
}
|
||||
else {
|
||||
a << character;
|
||||
}
|
||||
break;
|
||||
case ':':
|
||||
sub = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this->m_or = bor;
|
||||
|
||||
const auto aString = a.str();
|
||||
|
||||
if (!aString.empty()) {
|
||||
this->a = std::stoul(a.str());
|
||||
}
|
||||
else {
|
||||
this->a = 0;
|
||||
}
|
||||
|
||||
const auto subString = s.str();
|
||||
|
||||
if (!subString.empty()) {
|
||||
this->sub = std::stoul(s.str());
|
||||
}
|
||||
else {
|
||||
this->sub = 0;
|
||||
}
|
||||
|
||||
const auto bString = b.str();
|
||||
|
||||
if (!bString.empty()) {
|
||||
this->b = new PrerequisiteExpression(bString);
|
||||
}
|
||||
else {
|
||||
this->b = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PrerequisiteExpression::Execute(const std::unordered_map<uint32_t, Mission*>& missions) const {
|
||||
auto a = this->a == 0;
|
||||
|
||||
auto b = this->b == nullptr;
|
||||
|
||||
if (!a) {
|
||||
const auto index = missions.find(this->a);
|
||||
|
||||
if (index != missions.end()) {
|
||||
const auto* mission = index->second;
|
||||
|
||||
if (this->sub != 0) {
|
||||
// Special case for one Wisp Lee repeatable mission.
|
||||
a = mission->GetClientInfo().id == 1883 ?
|
||||
mission->GetMissionState() == static_cast<MissionState>(this->sub) :
|
||||
mission->GetMissionState() >= static_cast<MissionState>(this->sub);
|
||||
}
|
||||
else if (mission->IsComplete()) {
|
||||
a = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!b) {
|
||||
b = this->b->Execute(missions);
|
||||
}
|
||||
|
||||
if (this->m_or) {
|
||||
return a || b;
|
||||
}
|
||||
|
||||
return a && b;
|
||||
}
|
||||
|
||||
|
||||
PrerequisiteExpression::~PrerequisiteExpression() {
|
||||
delete b;
|
||||
}
|
||||
|
||||
|
||||
bool MissionPrerequisites::CanAccept(const uint32_t missionId, const std::unordered_map<uint32_t, Mission*>& missions) {
|
||||
const auto& missionIndex = missions.find(missionId);
|
||||
|
||||
if (missionIndex != missions.end()) {
|
||||
auto* mission = missionIndex->second;
|
||||
const auto& info = mission->GetClientInfo();
|
||||
|
||||
if (info.repeatable) {
|
||||
const auto prerequisitesMet = CheckPrerequisites(missionId, missions);
|
||||
|
||||
// Checked by client
|
||||
const time_t time = std::time(nullptr);
|
||||
const time_t lock = mission->GetTimestamp() + info.cooldownTime * 60;
|
||||
|
||||
// If there's no time limit, just check the prerequisites, otherwise make sure both conditions are met
|
||||
return (info.cooldownTime == -1 ? prerequisitesMet : (lock - time < 0)) && prerequisitesMet;
|
||||
}
|
||||
|
||||
// Mission is already accepted and cannot be repeatedly accepted
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mission is not yet accepted, check the prerequisites
|
||||
return CheckPrerequisites(missionId, missions);
|
||||
}
|
||||
|
||||
bool MissionPrerequisites::CheckPrerequisites(uint32_t missionId, const std::unordered_map<uint32_t, Mission*>& missions) {
|
||||
const auto& index = expressions.find(missionId);
|
||||
if (index != expressions.end()) {
|
||||
return index->second->Execute(missions);
|
||||
}
|
||||
|
||||
auto* missionsTable = CDClientManager::Instance()->GetTable<CDMissionsTable>("Missions");
|
||||
const auto missionEntries = missionsTable->Query([=](const CDMissions& entry) {
|
||||
return entry.id == static_cast<int>(missionId);
|
||||
});
|
||||
|
||||
if (missionEntries.empty())
|
||||
return false;
|
||||
|
||||
auto* expression = new PrerequisiteExpression(missionEntries[0].prereqMissionID);
|
||||
expressions.insert_or_assign(missionId, expression);
|
||||
|
||||
return expression->Execute(missions);
|
||||
}
|
||||
|
||||
std::unordered_map<uint32_t, PrerequisiteExpression*> MissionPrerequisites::expressions = {};
|
||||
58
dGame/dMission/MissionPrerequisites.h
Normal file
58
dGame/dMission/MissionPrerequisites.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "Mission.h"
|
||||
|
||||
/**
|
||||
* An expression that checks if a mission may be accepted or not
|
||||
*/
|
||||
class PrerequisiteExpression final
|
||||
{
|
||||
bool m_or;
|
||||
uint32_t a;
|
||||
uint32_t sub;
|
||||
PrerequisiteExpression* b;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Executes the prerequisite, checking its contents and returning whether or not the mission may be accepted
|
||||
* @param missions the list of missions to check the prerequisites against (f.e. whether they're completed)
|
||||
* @return whether or not all the prerequisites are met
|
||||
*/
|
||||
bool Execute(const std::unordered_map<uint32_t, Mission*>& missions) const;
|
||||
|
||||
explicit PrerequisiteExpression(const std::string& str);
|
||||
~PrerequisiteExpression();
|
||||
};
|
||||
|
||||
/**
|
||||
* Utility class for checking whether or not a mission can be accepted using its prerequisites
|
||||
*/
|
||||
class MissionPrerequisites final
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Checks whether or not the mission identified by the specified ID can be accepted based on the mission inventory passed.
|
||||
* Also performs checks for daily missions (e.g. if the time out is valid).
|
||||
* @param missionId the mission ID to check prerequisites for
|
||||
* @param missions the mission inventory to check the prerequisites against
|
||||
* @return whether or not the mission identified by the specified ID can be accepted
|
||||
*/
|
||||
static bool CanAccept(uint32_t missionId, const std::unordered_map<uint32_t, Mission*>& missions);
|
||||
private:
|
||||
|
||||
/**
|
||||
* Cache of all the executed prerequisites
|
||||
*/
|
||||
static std::unordered_map<uint32_t, PrerequisiteExpression*> expressions;
|
||||
|
||||
/**
|
||||
* Checks the prerequisites for a mission
|
||||
* @param missionId the mission ID to check prerequisites for
|
||||
* @param missions the mission inventory to check the prerequisites against
|
||||
* @return whether or not the mission identified by the specified ID can be accepted
|
||||
*/
|
||||
static bool CheckPrerequisites(uint32_t missionId, const std::unordered_map<uint32_t, Mission*>& missions);
|
||||
};
|
||||
51
dGame/dMission/MissionState.h
Normal file
51
dGame/dMission/MissionState.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* Represents the possible states a mission can be in
|
||||
*/
|
||||
enum class MissionState : int {
|
||||
/**
|
||||
* The mission state is unknown
|
||||
*/
|
||||
MISSION_STATE_UNKNOWN = -1,
|
||||
|
||||
/**
|
||||
* The mission is yielding rewards
|
||||
*/
|
||||
MISSION_STATE_REWARDING = 0,
|
||||
|
||||
/**
|
||||
* The mission can be accepted
|
||||
*/
|
||||
MISSION_STATE_AVAILABLE = 1,
|
||||
|
||||
/**
|
||||
* The mission has been accepted but not yet completed
|
||||
*/
|
||||
MISSION_STATE_ACTIVE = 2,
|
||||
|
||||
/**
|
||||
* All the tasks for the mission have been completed and the entity can turn the mission in to complete it
|
||||
*/
|
||||
MISSION_STATE_READY_TO_COMPLETE = 4, //!< The mission is ready to complete
|
||||
|
||||
/**
|
||||
* The mission has been completed
|
||||
*/
|
||||
MISSION_STATE_COMPLETE = 8,
|
||||
|
||||
/**
|
||||
* The mission is available again and has been completed before. Used for daily missions.
|
||||
*/
|
||||
MISSION_STATE_COMPLETE_AVAILABLE = 9,
|
||||
|
||||
/**
|
||||
* The mission is active and has been completed before. Used for daily missions.
|
||||
*/
|
||||
MISSION_STATE_COMPLETE_ACTIVE = 10,
|
||||
|
||||
/**
|
||||
* The mission has been completed before and has now been completed again. Used for daily missions.
|
||||
*/
|
||||
MISSION_STATE_COMPLETE_READY_TO_COMPLETE = 12
|
||||
};
|
||||
491
dGame/dMission/MissionTask.cpp
Normal file
491
dGame/dMission/MissionTask.cpp
Normal file
@@ -0,0 +1,491 @@
|
||||
#include <sstream>
|
||||
|
||||
#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"
|
||||
#include "MissionComponent.h"
|
||||
|
||||
|
||||
MissionTask::MissionTask(Mission* mission, CDMissionTasks* info, uint32_t mask)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
stream = std::istringstream(info->targetGroup);
|
||||
|
||||
while (std::getline(stream, token, ',')) {
|
||||
uint32_t parameter;
|
||||
if (GeneralUtils::TryParse(token, parameter)) {
|
||||
targets.push_back(parameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MissionTaskType MissionTask::GetType() const
|
||||
{
|
||||
return static_cast<MissionTaskType>(info->taskType);
|
||||
}
|
||||
|
||||
|
||||
uint32_t MissionTask::GetProgress() const
|
||||
{
|
||||
return progress;
|
||||
}
|
||||
|
||||
|
||||
void MissionTask::SetProgress(const uint32_t value, const bool echo)
|
||||
{
|
||||
if (progress == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
progress = value;
|
||||
|
||||
if (!echo)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto* entity = mission->GetAssociate();
|
||||
|
||||
if (entity == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<float> updates;
|
||||
updates.push_back(static_cast<float>(progress));
|
||||
|
||||
GameMessages::SendNotifyMissionTask(
|
||||
entity,
|
||||
entity->GetSystemAddress(),
|
||||
static_cast<int>(info->id),
|
||||
static_cast<int>(1 << (mask + 1)),
|
||||
updates
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
void MissionTask::SetUnique(const std::vector<uint32_t>& value)
|
||||
{
|
||||
unique = value;
|
||||
}
|
||||
|
||||
|
||||
void MissionTask::AddProgress(int32_t value)
|
||||
{
|
||||
value += progress;
|
||||
|
||||
if (value > info->targetValue)
|
||||
{
|
||||
value = info->targetValue;
|
||||
}
|
||||
|
||||
if (value < 0)
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
|
||||
SetProgress(value);
|
||||
}
|
||||
|
||||
|
||||
Mission* MissionTask::GetMission() const
|
||||
{
|
||||
return mission;
|
||||
}
|
||||
|
||||
|
||||
uint32_t MissionTask::GetTarget() const
|
||||
{
|
||||
return info->target;
|
||||
}
|
||||
|
||||
|
||||
const CDMissionTasks& MissionTask::GetClientInfo() const
|
||||
{
|
||||
return *info;
|
||||
}
|
||||
|
||||
|
||||
uint32_t MissionTask::GetMask() const
|
||||
{
|
||||
return mask;
|
||||
}
|
||||
|
||||
|
||||
const std::vector<uint32_t>& MissionTask::GetUnique() const
|
||||
{
|
||||
return unique;
|
||||
}
|
||||
|
||||
|
||||
const std::vector<uint32_t>& MissionTask::GetTargets() const
|
||||
{
|
||||
return targets;
|
||||
}
|
||||
|
||||
|
||||
const std::vector<uint32_t>& MissionTask::GetParameters() const
|
||||
{
|
||||
return parameters;
|
||||
}
|
||||
|
||||
|
||||
std::vector<uint32_t> MissionTask::GetAllTargets() const
|
||||
{
|
||||
auto targets = GetTargets();
|
||||
|
||||
targets.push_back(GetTarget());
|
||||
|
||||
return targets;
|
||||
}
|
||||
|
||||
|
||||
bool MissionTask::InTargets(const uint32_t value) const
|
||||
{
|
||||
auto targets = GetTargets();
|
||||
|
||||
return std::find(targets.begin(), targets.end(), value) != targets.end();
|
||||
}
|
||||
|
||||
|
||||
bool MissionTask::InAllTargets(const uint32_t value) const
|
||||
{
|
||||
auto targets = GetAllTargets();
|
||||
|
||||
return std::find(targets.begin(), targets.end(), value) != targets.end();
|
||||
}
|
||||
|
||||
bool MissionTask::InParameters(const uint32_t value) const
|
||||
{
|
||||
auto parameters = GetParameters();
|
||||
|
||||
return std::find(parameters.begin(), parameters.end(), value) != parameters.end();
|
||||
}
|
||||
|
||||
|
||||
bool MissionTask::IsComplete() const
|
||||
{
|
||||
// Minigames are the only ones where the target value is a score they need to get but the actual target is the act ID
|
||||
return GetType() == MissionTaskType::MISSION_TASK_TYPE_MINIGAME ? progress == info->target : progress >= info->targetValue;
|
||||
}
|
||||
|
||||
|
||||
void MissionTask::Complete()
|
||||
{
|
||||
// Minigames are the only ones where the target value is a score they need to get but the actual target is the act ID
|
||||
SetProgress(GetType() == MissionTaskType::MISSION_TASK_TYPE_MINIGAME ? info->target : info->targetValue);
|
||||
}
|
||||
|
||||
|
||||
void MissionTask::CheckCompletion() const
|
||||
{
|
||||
if (IsComplete())
|
||||
{
|
||||
mission->CheckCompletion();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MissionTask::Progress(int32_t value, LWOOBJID associate, const std::string& targets, int32_t count)
|
||||
{
|
||||
if (IsComplete() && count > 0) return;
|
||||
|
||||
const auto type = GetType();
|
||||
|
||||
if (count < 0)
|
||||
{
|
||||
if (mission->IsMission() && type == MissionTaskType::MISSION_TASK_TYPE_ITEM_COLLECTION && InAllTargets(value))
|
||||
{
|
||||
if (parameters.size() > 0 && (parameters[0] & 1) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto* inventoryComponent = mission->GetAssociate()->GetComponent<InventoryComponent>();
|
||||
|
||||
if (inventoryComponent != nullptr)
|
||||
{
|
||||
int32_t itemCount = inventoryComponent->GetLotCountNonTransfer(value);
|
||||
|
||||
if (itemCount < info->targetValue)
|
||||
{
|
||||
SetProgress(itemCount);
|
||||
|
||||
if (mission->IsReadyToComplete())
|
||||
{
|
||||
mission->MakeActive();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Entity* entity;
|
||||
ScriptedActivityComponent* activity;
|
||||
uint32_t activityId;
|
||||
uint32_t lot;
|
||||
uint32_t collectionId;
|
||||
std::vector<LDFBaseData*> settings;
|
||||
|
||||
switch (type) {
|
||||
case MissionTaskType::MISSION_TASK_TYPE_UNKNOWN:
|
||||
break;
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_ACTIVITY:
|
||||
{
|
||||
if (InAllTargets(value)) {
|
||||
AddProgress(count);
|
||||
break;
|
||||
}
|
||||
|
||||
entity = EntityManager::Instance()->GetEntity(associate);
|
||||
if (entity == nullptr) {
|
||||
if (associate != LWOOBJID_EMPTY) {
|
||||
Game::logger->Log("MissionTask", "Failed to find associated entity (%llu)!\n", associate);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
activity = static_cast<ScriptedActivityComponent*>(entity->GetComponent(COMPONENT_TYPE_REBUILD));
|
||||
if (activity == nullptr)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
activityId = activity->GetActivityID();
|
||||
|
||||
const auto activityIdOverride = entity->GetVar<int32_t>(u"activityID");
|
||||
|
||||
if (activityIdOverride != 0)
|
||||
{
|
||||
activityId = activityIdOverride;
|
||||
}
|
||||
|
||||
if (!InAllTargets(activityId)) break;
|
||||
|
||||
AddProgress(count);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_FOOD:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_MISSION_INTERACTION:
|
||||
{
|
||||
if (GetTarget() != value) break;
|
||||
|
||||
AddProgress(count);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_EMOTE:
|
||||
{
|
||||
if (!InParameters(value)) break;
|
||||
|
||||
entity = EntityManager::Instance()->GetEntity(associate);
|
||||
|
||||
if (entity == nullptr)
|
||||
{
|
||||
Game::logger->Log("MissionTask", "Failed to find associated entity (%llu)!\n", associate);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
lot = static_cast<uint32_t>(entity->GetLOT());
|
||||
|
||||
if (GetTarget() != lot) break;
|
||||
|
||||
AddProgress(count);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_SKILL:
|
||||
{
|
||||
if (!InParameters(value)) break;
|
||||
|
||||
AddProgress(count);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_MINIGAME:
|
||||
{
|
||||
if (targets != info->targetGroup || info->targetValue > value)
|
||||
break;
|
||||
|
||||
auto* minigameManager = EntityManager::Instance()->GetEntity(associate);
|
||||
if (minigameManager == nullptr)
|
||||
break;
|
||||
|
||||
int32_t gameID = minigameManager->GetLOT();
|
||||
|
||||
auto* sac = minigameManager->GetComponent<ScriptedActivityComponent>();
|
||||
if (sac != nullptr) {
|
||||
gameID = sac->GetActivityID();
|
||||
}
|
||||
|
||||
if (info->target != gameID) {
|
||||
break;
|
||||
}
|
||||
|
||||
Game::logger->Log("Minigame Task", "Progressing minigame with %s %d > %d (%d)\n",
|
||||
targets.c_str(), value, info->targetValue, gameID);
|
||||
SetProgress(info->target);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_VISIT_PROPERTY:
|
||||
{
|
||||
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);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_ENVIRONMENT:
|
||||
{
|
||||
if (!InAllTargets(value)) break;
|
||||
|
||||
entity = EntityManager::Instance()->GetEntity(associate);
|
||||
|
||||
if (entity == nullptr)
|
||||
{
|
||||
Game::logger->Log("MissionTask", "Failed to find associated entity (%llu)!\n", associate);
|
||||
|
||||
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);
|
||||
|
||||
SetProgress(unique.size());
|
||||
|
||||
auto* entity = mission->GetAssociate();
|
||||
|
||||
if (entity == nullptr) break;
|
||||
|
||||
auto* missionComponent = entity->GetComponent<MissionComponent>();
|
||||
|
||||
if (missionComponent == nullptr) break;
|
||||
|
||||
missionComponent->AddCollectible(collectionId);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_LOCATION:
|
||||
{
|
||||
if (info->targetGroup != targets) break;
|
||||
|
||||
AddProgress(count);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_RACING:
|
||||
{
|
||||
if (parameters.empty()) break;
|
||||
|
||||
if (!InAllTargets(dZoneManager::Instance()->GetZone()->GetWorldID())) break;
|
||||
|
||||
if (parameters[0] != associate) break;
|
||||
|
||||
if (associate == 1 || associate == 15)
|
||||
{
|
||||
if (value > info->targetValue) break;
|
||||
|
||||
AddProgress(1);
|
||||
}
|
||||
else if (associate == 2 || associate == 3)
|
||||
{
|
||||
if (info->targetValue < value) break;
|
||||
|
||||
AddProgress(info->targetValue);
|
||||
}
|
||||
else if (associate == 10)
|
||||
{
|
||||
if (info->targetValue > value)
|
||||
{
|
||||
AddProgress(info->targetValue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
AddProgress(count);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case MissionTaskType::MISSION_TASK_TYPE_PET_TAMING:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_SCRIPT:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_NON_MISSION_INTERACTION:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_MISSION_COMPLETE:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_POWERUP:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_SMASH:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_ITEM_COLLECTION:
|
||||
case MissionTaskType::MISSION_TASK_TYPE_PLAYER_FLAG:
|
||||
{
|
||||
if (!InAllTargets(value)) break;
|
||||
|
||||
AddProgress(count);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
Game::logger->Log("MissionTask", "Invalid mission task type (%i)!\n", static_cast<int>(type));
|
||||
return;
|
||||
}
|
||||
|
||||
CheckCompletion();
|
||||
}
|
||||
|
||||
|
||||
MissionTask::~MissionTask()
|
||||
{
|
||||
targets.clear();
|
||||
|
||||
parameters.clear();
|
||||
|
||||
unique.clear();
|
||||
}
|
||||
182
dGame/dMission/MissionTask.h
Normal file
182
dGame/dMission/MissionTask.h
Normal file
@@ -0,0 +1,182 @@
|
||||
#pragma once
|
||||
|
||||
#include "CDMissionTasksTable.h"
|
||||
#include "MissionTaskType.h"
|
||||
#include "dCommonVars.h"
|
||||
|
||||
class Mission;
|
||||
|
||||
/**
|
||||
* A task that can be progressed and completed for a mission.
|
||||
*/
|
||||
class MissionTask final
|
||||
{
|
||||
public:
|
||||
MissionTask(Mission* mission, CDMissionTasks* info, uint32_t mask);
|
||||
~MissionTask();
|
||||
|
||||
/**
|
||||
* Attempts to progress this task using the provided parameters. Note that the behavior of this method is different
|
||||
* for each mission task type.
|
||||
* @param value the value to progress by
|
||||
* @param associate optional object ID of an entity that was related to the porgression
|
||||
* @param targets optional multiple targets that need to be met to progress
|
||||
* @param count a number that indicates the times to progress
|
||||
*/
|
||||
void Progress(int32_t value, LWOOBJID associate = 0, const std::string& targets = "", int32_t count = 1);
|
||||
|
||||
/**
|
||||
* Returns the current progression of this task
|
||||
* @return the current progression of this task
|
||||
*/
|
||||
uint32_t GetProgress() const;
|
||||
|
||||
/**
|
||||
* Progresses the progress of this task by the provided value. Does not exceed the target progress.
|
||||
* @param value the value to progress by
|
||||
*/
|
||||
void AddProgress(int32_t value);
|
||||
|
||||
/**
|
||||
* Sets the progress of the task and optionally notifies the client
|
||||
* @param value the value to set for the progress
|
||||
* @param echo if true, this will notify the client of the change
|
||||
*/
|
||||
void SetProgress(uint32_t value, bool echo = true);
|
||||
|
||||
/**
|
||||
* Returns the mission this task belongs to
|
||||
* @return the mission this task belongs to
|
||||
*/
|
||||
Mission* GetMission() const;
|
||||
|
||||
/**
|
||||
* Returns the type of this task
|
||||
* @return the type of this task
|
||||
*/
|
||||
MissionTaskType GetType() const;
|
||||
|
||||
/**
|
||||
* Returns the value that should be progressed to, to complete the mission (the target value)
|
||||
* @return the target value
|
||||
*/
|
||||
uint32_t GetTarget() const;
|
||||
|
||||
/**
|
||||
* Returns the database information for this mission
|
||||
* @return the database information for this mission
|
||||
*/
|
||||
const CDMissionTasks& GetClientInfo() const;
|
||||
|
||||
/**
|
||||
* Returns the mask for this mission, used for communicating updates
|
||||
* @return the mask for this mission, used for communicating updates
|
||||
*/
|
||||
uint32_t GetMask() const;
|
||||
|
||||
/**
|
||||
* Returns the currently visited list of unique locations (only used for visiting mission types)
|
||||
* @return the currently visited list of unique locations
|
||||
*/
|
||||
const std::vector<uint32_t>& GetUnique() const;
|
||||
|
||||
/**
|
||||
* Sets the uniquely visited list of locations
|
||||
* @param value the uniquely visited list of locations
|
||||
*/
|
||||
void SetUnique(const std::vector<uint32_t>& value);
|
||||
|
||||
/**
|
||||
* Returns the possibly target values for this mission task for progression
|
||||
* @return the possibly target values for this mission task for progression
|
||||
*/
|
||||
const std::vector<uint32_t>& GetTargets() const;
|
||||
|
||||
/**
|
||||
* Returns the parameters for this task: meta information that determines if the task can be progressed. Note:
|
||||
* not used by all task types.
|
||||
* @return the parameters for this task
|
||||
*/
|
||||
const std::vector<uint32_t>& GetParameters() const;
|
||||
|
||||
/**
|
||||
* Returns all the target values for this mission, including the target value concatenated by the optional list of
|
||||
* targets parsed as ints.
|
||||
* @return all the targets for this task
|
||||
*/
|
||||
std::vector<uint32_t> GetAllTargets() const;
|
||||
|
||||
/**
|
||||
* Returns whether the value is in the list of target values of this task
|
||||
* @param value the value to check for
|
||||
* @return true if the value is in the target list, false otherwise
|
||||
*/
|
||||
bool InTargets(uint32_t value) const;
|
||||
|
||||
/**
|
||||
* Returns whether the value is in one of the target values or equals the individual target value of this task
|
||||
* @param value the value to check for
|
||||
* @return true if the value is one of the targets, false otherwise
|
||||
*/
|
||||
bool InAllTargets(uint32_t value) const;
|
||||
|
||||
/**
|
||||
* Checks if the provided is one of the parameters for this task
|
||||
* @param value the value to check for
|
||||
* @return true if the value is one of the parameters, false otherwise
|
||||
*/
|
||||
bool InParameters(uint32_t value) const;
|
||||
|
||||
/**
|
||||
* Checks if this task has been completed by comparing its progress against the target value
|
||||
* @return true if the task has been completed, false otherwise
|
||||
*/
|
||||
bool IsComplete() const;
|
||||
|
||||
/**
|
||||
* Completes the mission by setting the progress to the required value
|
||||
*/
|
||||
void Complete();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Datbase information about this task
|
||||
*/
|
||||
CDMissionTasks* info;
|
||||
|
||||
/**
|
||||
* The mission this task belongs to
|
||||
*/
|
||||
Mission* mission;
|
||||
|
||||
/**
|
||||
* Mask used for communicating mission updates
|
||||
*/
|
||||
uint32_t mask;
|
||||
|
||||
/**
|
||||
* The current progression towards the target
|
||||
*/
|
||||
uint32_t progress;
|
||||
|
||||
/**
|
||||
* The list of target values for progressing this task
|
||||
*/
|
||||
std::vector<uint32_t> targets;
|
||||
|
||||
/**
|
||||
* The list of parameters for progressing this task (not used by all task types)
|
||||
*/
|
||||
std::vector<uint32_t> parameters;
|
||||
|
||||
/**
|
||||
* The unique places visited for progression (not used by all task types)
|
||||
*/
|
||||
std::vector<uint32_t> unique;
|
||||
|
||||
/**
|
||||
* Checks if the task is complete, and if so checks if the parent mission is complete
|
||||
*/
|
||||
void CheckCompletion() const;
|
||||
};
|
||||
24
dGame/dMission/MissionTaskType.h
Normal file
24
dGame/dMission/MissionTaskType.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
//! An enum for mission task types
|
||||
enum class MissionTaskType : int {
|
||||
MISSION_TASK_TYPE_UNKNOWN = -1, //!< The task type is unknown
|
||||
MISSION_TASK_TYPE_SMASH = 0, //!< A task for smashing something
|
||||
MISSION_TASK_TYPE_SCRIPT = 1, //!< A task handled by a server LUA script
|
||||
MISSION_TASK_TYPE_ACTIVITY = 2, //!< A task for completing a quickbuild
|
||||
MISSION_TASK_TYPE_ENVIRONMENT = 3, //!< A task for something in the environment
|
||||
MISSION_TASK_TYPE_MISSION_INTERACTION = 4, //!< A task for interacting with a mission
|
||||
MISSION_TASK_TYPE_EMOTE = 5, //!< A task for playing an emote
|
||||
MISSION_TASK_TYPE_FOOD = 9, //!< A task for eating food
|
||||
MISSION_TASK_TYPE_SKILL = 10, //!< A task for performing a skill
|
||||
MISSION_TASK_TYPE_ITEM_COLLECTION = 11, //!< A task for collecting an item
|
||||
MISSION_TASK_TYPE_LOCATION = 12, //!< A task for finding a location
|
||||
MISSION_TASK_TYPE_MINIGAME = 14, //!< A task for doing something in a minigame
|
||||
MISSION_TASK_TYPE_NON_MISSION_INTERACTION = 15, //!< A task for interacting with a non-mission
|
||||
MISSION_TASK_TYPE_MISSION_COMPLETE = 16, //!< A task for completing a mission
|
||||
MISSION_TASK_TYPE_POWERUP = 21, //!< A task for collecting a powerup
|
||||
MISSION_TASK_TYPE_PET_TAMING = 22, //!< A task for taming a pet
|
||||
MISSION_TASK_TYPE_RACING = 23, //!< A task for racing
|
||||
MISSION_TASK_TYPE_PLAYER_FLAG = 24, //!< A task for setting a player flag
|
||||
MISSION_TASK_TYPE_VISIT_PROPERTY = 30 //!< A task for visiting a property
|
||||
};
|
||||
Reference in New Issue
Block a user