DarkflameServer/dGame/dComponents/MissionOfferComponent.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
5.5 KiB
C++
Raw Normal View History

/*
* Darkflame Universe
2023-07-06 03:30:52 +00:00
* Copyright 2023
*/
#include <sstream>
#include "MissionOfferComponent.h"
#include "CDClientManager.h"
#include "CDMissionsTable.h"
#include "CDMissionNPCComponentTable.h"
#include "GameMessages.h"
#include "Entity.h"
#include "MissionComponent.h"
#include "dLogger.h"
#include "Game.h"
#include "MissionPrerequisites.h"
#include "eMissionState.h"
#include "CDComponentsRegistryTable.h"
2023-07-06 03:30:52 +00:00
MissionOfferComponent::MissionOfferComponent(Entity* parent, const int32_t componentId) : Component(parent) {
m_ComponentId = componentId;
}
2023-07-06 03:30:52 +00:00
void MissionOfferComponent::LoadTemplateData() {
if (m_ComponentId == -1) return;
// Now lookup the missions in the MissionNPCComponent table
auto* missionNpcComponentTable = CDClientManager::Instance().GetTable<CDMissionNPCComponentTable>();
2023-07-06 03:30:52 +00:00
auto missions = missionNpcComponentTable->Query([=](const CDMissionNPCComponent& entry) {
return entry.id == static_cast<int32_t>(m_ComponentId);
});
2023-07-06 03:30:52 +00:00
for (const auto& mission : missions) {
this->offeredMissions.emplace_back(
std::make_unique<OfferedMission>(mission.missionID, mission.offersMission, mission.acceptsMission)
);
}
}
void MissionOfferComponent::OnUse(Entity* originator) {
OfferMissions(originator);
}
void MissionOfferComponent::OfferMissions(Entity* entity, const uint32_t specifiedMissionId) {
// First, get the entity's MissionComponent. If there is not one, then we cannot offer missions to this entity.
auto* missionComponent = entity->GetComponent<MissionComponent>();
2022-07-28 13:39:57 +00:00
if (!missionComponent) {
Game::logger->Log("MissionOfferComponent", "Unable to get mission component for Entity %llu", entity->GetObjectID());
return;
}
2022-07-28 13:39:57 +00:00
std::vector<uint32_t> offered{};
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
CDMissions missionInfo{};
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
if (specifiedMissionId > 0 && !Mission::IsValidMission(specifiedMissionId, missionInfo)) {
return;
}
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
for (const auto& offeredMission : offeredMissions) {
if (specifiedMissionId > 0 && (offeredMission->GetMissionId() != specifiedMissionId && !missionInfo.isRandom)) {
continue;
}
2022-07-28 13:39:57 +00:00
// First, check if we already have the mission
const auto missionId = offeredMission->GetMissionId();
2022-07-28 13:39:57 +00:00
auto* mission = missionComponent->GetMission(missionId);
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
if (mission) {
if (specifiedMissionId <= 0) {
// Handles the odd case where the offer object should not display the mission again
2023-07-06 03:30:52 +00:00
if (!mission->IsComplete() &&
mission->GetClientInfo().offer_objectID == m_ParentEntity->GetLOT() &&
mission->GetClientInfo().target_objectID != m_ParentEntity->GetLOT() &&
mission->IsFetchMission()) {
continue;
2022-07-28 13:39:57 +00:00
}
}
2022-07-28 13:39:57 +00:00
// We have the mission, if it is not complete, offer it
if (mission->IsActive() || mission->IsReadyToComplete()) {
GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), missionId, m_ParentEntity->GetObjectID());
2022-07-28 13:39:57 +00:00
offered.push_back(missionId);
2022-07-28 13:39:57 +00:00
continue;
2022-07-28 13:39:57 +00:00
}
}
2022-07-28 13:39:57 +00:00
const auto canAccept = MissionPrerequisites::CanAccept(missionId, missionComponent->GetMissions());
2022-07-28 13:39:57 +00:00
// Mission has not yet been accepted - check the prereqs
2023-07-06 03:30:52 +00:00
if (!canAccept || !Mission::IsValidMission(missionId, missionInfo)) continue;
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
// This means the mission is part of a random pool of missions.
if (missionInfo.isRandom && missionInfo.randomPool.empty()) continue;
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
if (missionInfo.isRandom && !missionInfo.randomPool.empty()) {
auto randomMissionPoolStr = GeneralUtils::SplitString(missionInfo.randomPool, ',');
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
std::vector<uint32_t> randomMissions;
for (const auto& randomMissionStr : randomMissionPoolStr) {
uint32_t randomMission;
if (GeneralUtils::TryParse(randomMissionStr, randomMission)) randomMissions.push_back(randomMission);
}
2022-07-28 13:39:57 +00:00
if (specifiedMissionId > 0) {
2023-07-06 03:30:52 +00:00
if (std::find(randomMissions.begin(), randomMissions.end(), specifiedMissionId) != randomMissions.end() &&
MissionPrerequisites::CanAccept(specifiedMissionId, missionComponent->GetMissions())) {
GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), specifiedMissionId, m_ParentEntity->GetObjectID());
return;
2022-07-28 13:39:57 +00:00
}
}
2022-07-28 13:39:57 +00:00
std::vector<uint32_t> canAcceptPool;
2023-07-06 03:30:52 +00:00
for (const auto& sample : randomMissions) {
const auto state = missionComponent->GetMissionState(sample);
2022-07-28 13:39:57 +00:00
if (state == eMissionState::ACTIVE ||
state == eMissionState::COMPLETE_ACTIVE ||
state == eMissionState::READY_TO_COMPLETE ||
state == eMissionState::COMPLETE_READY_TO_COMPLETE ||
sample == specifiedMissionId) {
mission = missionComponent->GetMission(sample);
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
if (!mission || mission->IsAchievement()) continue;
2022-07-28 13:39:57 +00:00
GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), sample, m_ParentEntity->GetObjectID());
2022-07-28 13:39:57 +00:00
canAcceptPool.clear();
2022-07-28 13:39:57 +00:00
break;
}
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
if (std::find(offered.begin(), offered.end(), sample) == offered.end() &&
MissionPrerequisites::CanAccept(sample, missionComponent->GetMissions())) {
canAcceptPool.push_back(sample);
}
}
2022-07-28 13:39:57 +00:00
// If the mission is already active or we already completed one of them today
2023-07-06 03:30:52 +00:00
if (canAcceptPool.empty()) continue;
2022-07-28 13:39:57 +00:00
2023-07-06 03:30:52 +00:00
const auto selected = canAcceptPool[GeneralUtils::GenerateRandomNumber<int32_t>(0, canAcceptPool.size() - 1)];
2022-07-28 13:39:57 +00:00
GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), selected, m_ParentEntity->GetObjectID());
} else if (std::find(offered.begin(), offered.end(), missionId) == offered.end() && offeredMission->GetOfferMission()) {
GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), missionId, m_ParentEntity->GetObjectID());
}
}
}