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 "../../../lib/GameLibrary.h"
|
|
|
|
|
#include "../../../lib/mapObjects/CQuest.h"
|
|
|
|
|
#include "../../../lib/texts/CGeneralTextHandler.h"
|
2025-09-12 19:25:56 +02:00
|
|
|
#include "../AIGateway.h"
|
|
|
|
|
#include "../Behaviors/CaptureObjectsBehavior.h"
|
|
|
|
|
#include "CompleteQuest.h"
|
2025-08-13 14:41:15 +02:00
|
|
|
|
2025-08-13 17:16:27 +02:00
|
|
|
namespace NK2AI
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
|
2025-09-12 19:25:56 +02:00
|
|
|
bool isKeyMaster(const QuestInfo & q, CCallback & cc)
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
const auto * const object = q.getObject(&cc);
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec CompleteQuest::decompose(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
if(isKeyMaster(q, *aiNk->cc))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionKeymaster(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logAi->debug("Trying to realize quest: %s", questToString());
|
2025-09-05 18:10:14 +02:00
|
|
|
const auto quest = q.getQuest(aiNk->cc.get());
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(!quest->mission.artifacts.empty())
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionArt(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(!quest->mission.heroes.empty())
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionHero(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(!quest->mission.creatures.empty())
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionArmy(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(quest->mission.resources.nonZero())
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionResources(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(quest->killTarget != ObjectInstanceID::NONE)
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionDestroyObj(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
for(auto & s : quest->mission.primary)
|
|
|
|
|
if(s)
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionIncreasePrimaryStat(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(quest->mission.heroLevel > 0)
|
2025-08-24 09:12:34 +02:00
|
|
|
return missionLevel(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
return TGoalVec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CompleteQuest::operator==(const CompleteQuest & other) const
|
|
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
if(isKeyMaster(q, cc))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
return isKeyMaster(other.q, cc) && q.getObject(&cc)->subID == other.q.getObject(&cc)->subID;
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
2025-09-12 19:25:56 +02:00
|
|
|
else if(isKeyMaster(other.q, cc))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-12 19:25:56 +02:00
|
|
|
return q.getQuest(&cc) == other.q.getQuest(&cc);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t CompleteQuest::getHash() const
|
|
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
if(isKeyMaster(q, cc))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
return q.getObject(&cc)->subID;
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-12 19:25:56 +02:00
|
|
|
return q.getObject(&cc)->id.getNum();
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CompleteQuest::questToString() const
|
|
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
if(isKeyMaster(q, cc))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-12 19:25:56 +02:00
|
|
|
return "find " + LIBRARY->generaltexth->tentColors[q.getObject(&cc)->subID] + " keymaster tent";
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-12 19:25:56 +02:00
|
|
|
if(q.getQuest(&cc)->questName == CQuest::missionName(EQuestMission::NONE))
|
2025-08-13 14:41:15 +02:00
|
|
|
return "inactive quest";
|
|
|
|
|
|
|
|
|
|
MetaString ms;
|
2025-09-12 19:25:56 +02:00
|
|
|
q.getQuest(&cc)->getRolloverText(&cc, ms, false);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
return ms.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 18:30:28 +02:00
|
|
|
TGoalVec CompleteQuest::tryCompleteQuest(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-05 18:10:14 +02:00
|
|
|
auto paths = aiNk->pathfinder->getPathInfo(q.getObject(aiNk->cc.get())->visitablePos());
|
2025-08-13 14:41:15 +02:00
|
|
|
|
2025-09-12 19:25:56 +02:00
|
|
|
vstd::erase_if(
|
|
|
|
|
paths,
|
|
|
|
|
[&](const AIPath & path) -> bool
|
|
|
|
|
{
|
|
|
|
|
return !q.getQuest(aiNk->cc.get())->checkQuest(path.targetHero);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-05 18:10:14 +02:00
|
|
|
return CaptureObjectsBehavior::getVisitGoals(paths, aiNk, q.getObject(aiNk->cc.get()));
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec CompleteQuest::missionArt(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec solutions = tryCompleteQuest(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(!solutions.empty())
|
|
|
|
|
return solutions;
|
|
|
|
|
|
|
|
|
|
CaptureObjectsBehavior findArts;
|
|
|
|
|
|
2025-09-05 18:10:14 +02:00
|
|
|
for(auto art : q.getQuest(aiNk->cc.get())->mission.artifacts)
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
|
|
|
|
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::ARTIFACT, art.getNum())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec CompleteQuest::missionHero(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec solutions = tryCompleteQuest(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
|
{
|
|
|
|
|
//rule of a thumb - quest heroes usually are locked in prisons
|
|
|
|
|
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::PRISON)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 18:30:28 +02:00
|
|
|
TGoalVec CompleteQuest::missionArmy(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-05 18:10:14 +02:00
|
|
|
auto paths = aiNk->pathfinder->getPathInfo(q.getObject(aiNk->cc.get())->visitablePos());
|
2025-08-13 14:41:15 +02:00
|
|
|
|
2025-09-12 19:25:56 +02:00
|
|
|
vstd::erase_if(
|
|
|
|
|
paths,
|
|
|
|
|
[&](const AIPath & path) -> bool
|
|
|
|
|
{
|
|
|
|
|
return !CQuest::checkMissionArmy(q.getQuest(aiNk->cc.get()), path.heroArmy);
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
2025-09-05 18:10:14 +02:00
|
|
|
return CaptureObjectsBehavior::getVisitGoals(paths, aiNk, q.getObject(aiNk->cc.get()));
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec CompleteQuest::missionIncreasePrimaryStat(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-08-24 09:12:34 +02:00
|
|
|
return tryCompleteQuest(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec CompleteQuest::missionLevel(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-08-24 09:12:34 +02:00
|
|
|
return tryCompleteQuest(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec CompleteQuest::missionKeymaster(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-05 18:10:14 +02:00
|
|
|
if(isObjectPassable(aiNk, q.getObject(aiNk->cc.get())))
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-05 18:10:14 +02:00
|
|
|
return CaptureObjectsBehavior(q.getObject(aiNk->cc.get())).decompose(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-05 18:10:14 +02:00
|
|
|
return CaptureObjectsBehavior().ofType(Obj::KEYMASTER, q.getObject(aiNk->cc.get())->subID).decompose(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec CompleteQuest::missionResources(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-08-24 09:12:34 +02:00
|
|
|
TGoalVec solutions = tryCompleteQuest(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
return solutions;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 18:30:28 +02:00
|
|
|
TGoalVec CompleteQuest::missionDestroyObj(const Nullkiller * aiNk) const
|
2025-08-13 14:41:15 +02:00
|
|
|
{
|
2025-09-05 18:10:14 +02:00
|
|
|
const auto obj = aiNk->cc->getObj(q.getQuest(aiNk->cc.get())->killTarget);
|
2025-08-13 14:41:15 +02:00
|
|
|
if(!obj)
|
2025-09-05 18:10:14 +02:00
|
|
|
return CaptureObjectsBehavior(q.getObject(aiNk->cc.get())).decompose(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
|
2025-09-05 18:10:14 +02:00
|
|
|
const auto relations = aiNk->cc->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)));
|
|
|
|
|
//}
|
2025-09-12 19:25:56 +02:00
|
|
|
//else
|
2025-08-13 14:41:15 +02:00
|
|
|
if(relations == PlayerRelations::ENEMIES)
|
|
|
|
|
{
|
2025-08-15 18:30:28 +02:00
|
|
|
return CaptureObjectsBehavior(obj).decompose(aiNk);
|
2025-08-13 14:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TGoalVec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|