Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Mission offering for NPCs #1359

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 12 additions & 42 deletions dGame/dComponents/MissionOfferComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,54 +55,39 @@ MissionOfferComponent::MissionOfferComponent(Entity* parent, const LOT parentLot
});

for (auto& mission : missions) {
auto* offeredMission = new OfferedMission(mission.missionID, mission.offersMission, mission.acceptsMission);
this->offeredMissions.push_back(offeredMission);
this->offeredMissions.emplace_back(mission.missionID, mission.offersMission, mission.acceptsMission);
}
}
}


MissionOfferComponent::~MissionOfferComponent() {
for (auto* mission : this->offeredMissions) {
if (mission) {
delete mission;
mission = nullptr;
}
}

offeredMissions.clear();
}

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 = static_cast<MissionComponent*>(entity->GetComponent(eReplicaComponentType::MISSION));
auto* missionComponent = entity->GetComponent<MissionComponent>();

if (!missionComponent) {
LOG("Unable to get mission component for Entity %llu", entity->GetObjectID());
return;
}

std::vector<uint32_t> offered{};

CDMissions info{};

if (specifiedMissionId > 0 && !Mission::IsValidMission(specifiedMissionId, info)) {
return;
}

for (auto* offeredMission : this->offeredMissions) {
for (const auto offeredMission : this->offeredMissions) {
if (specifiedMissionId > 0) {
if (offeredMission->GetMissionId() != specifiedMissionId && !info.isRandom) {
if (offeredMission.GetMissionId() != specifiedMissionId && !info.isRandom) {
continue;
}
}

// First, check if we already have the mission
const auto missionId = offeredMission->GetMissionId();
const auto missionId = offeredMission.GetMissionId();

auto* mission = missionComponent->GetMission(missionId);

Expand All @@ -118,29 +103,20 @@ void MissionOfferComponent::OfferMissions(Entity* entity, const uint32_t specifi
if (mission->IsActive() || mission->IsReadyToComplete()) {
GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), missionId, m_Parent->GetObjectID());

offered.push_back(missionId);

continue;
}
}

const auto canAccept = MissionPrerequisites::CanAccept(missionId, missionComponent->GetMissions());

// Mission has not yet been accepted - check the prereqs
if (!canAccept)
continue;

if (!Mission::IsValidMission(missionId, info)) {
continue;
}
if (!canAccept || !Mission::IsValidMission(missionId, info)) continue;

const auto& randomPool = info.randomPool;
const auto isRandom = info.isRandom;

if (isRandom && randomPool.empty()) // This means the mission is part of a random pool of missions.
{
continue;
}
// This means the mission is part of a random pool of missions.
if (isRandom && randomPool.empty()) continue;

if (isRandom && !randomPool.empty()) {
std::istringstream stream(randomPool);
Expand Down Expand Up @@ -180,9 +156,7 @@ void MissionOfferComponent::OfferMissions(Entity* entity, const uint32_t specifi
sample == specifiedMissionId) {
mission = missionComponent->GetMission(sample);

if (mission == nullptr || mission->IsAchievement()) {
continue;
}
if (mission == nullptr || mission->IsAchievement()) continue;

GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), sample, m_Parent->GetObjectID());

Expand All @@ -191,22 +165,18 @@ void MissionOfferComponent::OfferMissions(Entity* entity, const uint32_t specifi
break;
}

if (std::find(offered.begin(), offered.end(), sample) == offered.end() && MissionPrerequisites::CanAccept(sample, missionComponent->GetMissions())) {
if (MissionPrerequisites::CanAccept(sample, missionComponent->GetMissions())) {
canAcceptPool.push_back(sample);
}
}

// If the mission is already active or we already completed one of them today
if (canAcceptPool.empty())
continue;
if (canAcceptPool.empty()) continue;

const auto selected = canAcceptPool[GeneralUtils::GenerateRandomNumber<int>(0, canAcceptPool.size() - 1)];

GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), selected, m_Parent->GetObjectID());
} else if (
std::find(offered.begin(), offered.end(), missionId) == offered.end()
&&
(offeredMission->GetOffersMission() || offeredMission->GetAcceptsMission())) {
} else if (offeredMission.GetOffersMission()) {
GameMessages::SendOfferMission(entity->GetObjectID(), entity->GetSystemAddress(), missionId, m_Parent->GetObjectID());
}
}
Expand Down
3 changes: 1 addition & 2 deletions dGame/dComponents/MissionOfferComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class MissionOfferComponent : public Component {
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::MISSION_OFFER;

MissionOfferComponent(Entity* parent, LOT parentLot);
~MissionOfferComponent() override;

/**
* Handles the OnUse event triggered by some entity, determines which missions to show based on what they may
Expand All @@ -85,7 +84,7 @@ class MissionOfferComponent : public Component {
/**
* The missions this entity has to offer
*/
std::vector<OfferedMission*> offeredMissions;
std::vector<OfferedMission> offeredMissions;
};

#endif // MISSIONOFFERCOMPONENT_H
2 changes: 1 addition & 1 deletion dGame/dMission/Mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ bool Mission::IsComplete() const {
}

bool Mission::IsActive() const {
return m_State == eMissionState::ACTIVE || m_State == eMissionState::COMPLETE_AVAILABLE;
return m_State == eMissionState::ACTIVE || m_State == eMissionState::COMPLETE_ACTIVE;
}

void Mission::MakeActive() {
Expand Down
Loading