1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00
Files
vcmi/AI/Nullkiller2/Goals/CompleteQuest.cpp

216 lines
4.9 KiB
C++
Raw Normal View History

2025-08-13 14:41:15 +02:00
/*
* CompleteQuest.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CompleteQuest.h"
#include "../Behaviors/CaptureObjectsBehavior.h"
#include "../AIGateway.h"
#include "../../../lib/GameLibrary.h"
#include "../../../lib/mapObjects/CQuest.h"
#include "../../../lib/texts/CGeneralTextHandler.h"
2025-08-13 17:16:27 +02:00
namespace NK2AI
2025-08-13 14:41:15 +02:00
{
using namespace Goals;
bool isKeyMaster(const QuestInfo & q)
{
auto object = q.getObject(cbc);
2025-08-13 14:41:15 +02:00
return object && (object->ID == Obj::BORDER_GATE || object->ID == Obj::BORDERGUARD);
}
std::string CompleteQuest::toString() const
{
return "Complete quest " + questToString();
}
TGoalVec CompleteQuest::decompose(const Nullkiller * ai) const
2025-08-13 14:41:15 +02:00
{
if(isKeyMaster(q))
{
return missionKeymaster(ai);
}
logAi->debug("Trying to realize quest: %s", questToString());
auto quest = q.getQuest(cbc);
2025-08-13 14:41:15 +02:00
if(!quest->mission.artifacts.empty())
return missionArt(ai);
if(!quest->mission.heroes.empty())
return missionHero(ai);
if(!quest->mission.creatures.empty())
return missionArmy(ai);
if(quest->mission.resources.nonZero())
return missionResources(ai);
if(quest->killTarget != ObjectInstanceID::NONE)
return missionDestroyObj(ai);
for(auto & s : quest->mission.primary)
if(s)
return missionIncreasePrimaryStat(ai);
if(quest->mission.heroLevel > 0)
return missionLevel(ai);
return TGoalVec();
}
bool CompleteQuest::operator==(const CompleteQuest & other) const
{
if(isKeyMaster(q))
{
return isKeyMaster(other.q) && q.getObject(cbc)->subID == other.q.getObject(cbc)->subID;
2025-08-13 14:41:15 +02:00
}
else if(isKeyMaster(other.q))
{
return false;
}
return q.getQuest(cbc) == other.q.getQuest(cbc);
2025-08-13 14:41:15 +02:00
}
uint64_t CompleteQuest::getHash() const
{
if(isKeyMaster(q))
{
return q.getObject(cbc)->subID;
2025-08-13 14:41:15 +02:00
}
return q.getObject(cbc)->id.getNum();
2025-08-13 14:41:15 +02:00
}
std::string CompleteQuest::questToString() const
{
if(isKeyMaster(q))
{
return "find " + LIBRARY->generaltexth->tentColors[q.getObject(cbc)->subID] + " keymaster tent";
2025-08-13 14:41:15 +02:00
}
if(q.getQuest(cbc)->questName == CQuest::missionName(EQuestMission::NONE))
2025-08-13 14:41:15 +02:00
return "inactive quest";
MetaString ms;
q.getQuest(cbc)->getRolloverText(cbc, ms, false);
2025-08-13 14:41:15 +02:00
return ms.toString();
}
TGoalVec CompleteQuest::tryCompleteQuest(const Nullkiller * aiNk) const
2025-08-13 14:41:15 +02:00
{
auto paths = aiNk->pathfinder->getPathInfo(q.getObject(cbc)->visitablePos());
2025-08-13 14:41:15 +02:00
vstd::erase_if(paths, [&](const AIPath & path) -> bool
{
return !q.getQuest(cbc)->checkQuest(path.targetHero);
2025-08-13 14:41:15 +02:00
});
return CaptureObjectsBehavior::getVisitGoals(paths, aiNk, q.getObject(cbc));
2025-08-13 14:41:15 +02:00
}
TGoalVec CompleteQuest::missionArt(const Nullkiller * ai) const
2025-08-13 14:41:15 +02:00
{
TGoalVec solutions = tryCompleteQuest(ai);
if(!solutions.empty())
return solutions;
CaptureObjectsBehavior findArts;
for(auto art : q.getQuest(cbc)->mission.artifacts)
2025-08-13 14:41:15 +02:00
{
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::ARTIFACT, art.getNum())));
}
return solutions;
}
TGoalVec CompleteQuest::missionHero(const Nullkiller * ai) const
2025-08-13 14:41:15 +02:00
{
TGoalVec solutions = tryCompleteQuest(ai);
if(solutions.empty())
{
//rule of a thumb - quest heroes usually are locked in prisons
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::PRISON)));
}
return solutions;
}
TGoalVec CompleteQuest::missionArmy(const Nullkiller * aiNk) const
2025-08-13 14:41:15 +02:00
{
auto paths = aiNk->pathfinder->getPathInfo(q.getObject(cbc)->visitablePos());
2025-08-13 14:41:15 +02:00
vstd::erase_if(paths, [&](const AIPath & path) -> bool
{
return !CQuest::checkMissionArmy(q.getQuest(cbc), path.heroArmy);
2025-08-13 14:41:15 +02:00
});
return CaptureObjectsBehavior::getVisitGoals(paths, aiNk, q.getObject(cbc));
2025-08-13 14:41:15 +02:00
}
TGoalVec CompleteQuest::missionIncreasePrimaryStat(const Nullkiller * ai) const
2025-08-13 14:41:15 +02:00
{
return tryCompleteQuest(ai);
}
TGoalVec CompleteQuest::missionLevel(const Nullkiller * ai) const
2025-08-13 14:41:15 +02:00
{
return tryCompleteQuest(ai);
}
TGoalVec CompleteQuest::missionKeymaster(const Nullkiller * ai) const
2025-08-13 14:41:15 +02:00
{
if(isObjectPassable(ai, q.getObject(cbc)))
2025-08-13 14:41:15 +02:00
{
return CaptureObjectsBehavior(q.getObject(cbc)).decompose(ai);
2025-08-13 14:41:15 +02:00
}
else
{
return CaptureObjectsBehavior().ofType(Obj::KEYMASTER, q.getObject(cbc)->subID).decompose(ai);
2025-08-13 14:41:15 +02:00
}
}
TGoalVec CompleteQuest::missionResources(const Nullkiller * ai) const
2025-08-13 14:41:15 +02:00
{
TGoalVec solutions = tryCompleteQuest(ai);
return solutions;
}
TGoalVec CompleteQuest::missionDestroyObj(const Nullkiller * aiNk) const
2025-08-13 14:41:15 +02:00
{
auto obj = aiNk->cbc->getObj(q.getQuest(cbc)->killTarget);
2025-08-13 14:41:15 +02:00
if(!obj)
return CaptureObjectsBehavior(q.getObject(cbc)).decompose(aiNk);
2025-08-13 14:41:15 +02:00
auto relations = aiNk->cbc->getPlayerRelations(aiNk->playerID, obj->tempOwner);
2025-08-13 14:41:15 +02:00
//if(relations == PlayerRelations::SAME_PLAYER)
//{
// auto heroToProtect = cb->getHero(obj->id);
// //solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
//}
//else
if(relations == PlayerRelations::ENEMIES)
{
return CaptureObjectsBehavior(obj).decompose(aiNk);
2025-08-13 14:41:15 +02:00
}
return TGoalVec();
}
}