Skip to content

Commit

Permalink
fix: mission offering (#1359)
Browse files Browse the repository at this point in the history
fixes an issue where NPCs would offer the incorrect missions which caused odd blocks.  Consolidated logic for mission offering and removed redundant code.
  • Loading branch information
EmosewaMC authored Dec 26, 2023
1 parent ffa2f99 commit 46ac039
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 45 deletions.
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

0 comments on commit 46ac039

Please sign in to comment.