2021-05-16 14:38:26 +03:00
|
|
|
/*
|
2021-05-16 14:39:18 +03:00
|
|
|
* CompleteQuest.cpp, part of VCMI engine
|
2021-05-16 14:38:26 +03:00
|
|
|
*
|
|
|
|
* 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"
|
2021-05-16 14:56:35 +03:00
|
|
|
#include "CompleteQuest.h"
|
|
|
|
#include "../Behaviors/CaptureObjectsBehavior.h"
|
2021-05-16 15:39:38 +03:00
|
|
|
#include "../AIGateway.h"
|
2025-02-14 16:23:37 +00:00
|
|
|
#include "../../../lib/GameLibrary.h"
|
2025-03-09 22:44:36 +00:00
|
|
|
#include "../../../lib/mapObjects/CQuest.h"
|
2024-07-20 12:55:17 +00:00
|
|
|
#include "../../../lib/texts/CGeneralTextHandler.h"
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2022-09-26 21:01:07 +03:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2021-05-16 14:38:26 +03:00
|
|
|
using namespace Goals;
|
|
|
|
|
2021-05-16 14:56:42 +03:00
|
|
|
bool isKeyMaster(const QuestInfo & q)
|
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
auto object = q.getObject(cb);
|
|
|
|
return object && (object->ID == Obj::BORDER_GATE || object->ID == Obj::BORDERGUARD);
|
2021-05-16 14:56:42 +03:00
|
|
|
}
|
|
|
|
|
2021-05-16 14:39:18 +03:00
|
|
|
std::string CompleteQuest::toString() const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2021-05-16 14:39:18 +03:00
|
|
|
return "Complete quest " + questToString();
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::decompose(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2021-05-16 14:56:42 +03:00
|
|
|
if(isKeyMaster(q))
|
2021-05-16 14:39:18 +03:00
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionKeymaster(ai);
|
2021-05-16 14:39:18 +03:00
|
|
|
}
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2021-05-16 14:56:27 +03:00
|
|
|
logAi->debug("Trying to realize quest: %s", questToString());
|
2025-03-18 19:54:09 +00:00
|
|
|
auto quest = q.getQuest(cb);
|
|
|
|
|
|
|
|
if(!quest->mission.artifacts.empty())
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionArt(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
if(!quest->mission.heroes.empty())
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionHero(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
if(!quest->mission.creatures.empty())
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionArmy(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
if(quest->mission.resources.nonZero())
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionResources(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
if(quest->killTarget != ObjectInstanceID::NONE)
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionDestroyObj(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
for(auto & s : quest->mission.primary)
|
2023-10-09 19:15:34 +02:00
|
|
|
if(s)
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionIncreasePrimaryStat(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
if(quest->mission.heroLevel > 0)
|
2024-03-31 18:39:00 +03:00
|
|
|
return missionLevel(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
|
|
|
return TGoalVec();
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:39:18 +03:00
|
|
|
bool CompleteQuest::operator==(const CompleteQuest & other) const
|
|
|
|
{
|
2021-05-16 14:56:42 +03:00
|
|
|
if(isKeyMaster(q))
|
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
return isKeyMaster(other.q) && q.getObject(cb)->subID == other.q.getObject(cb)->subID;
|
2021-05-16 14:56:42 +03:00
|
|
|
}
|
|
|
|
else if(isKeyMaster(other.q))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
return q.getQuest(cb) == other.q.getQuest(cb);
|
2021-05-16 14:39:18 +03:00
|
|
|
}
|
|
|
|
|
2021-05-16 14:56:42 +03:00
|
|
|
uint64_t CompleteQuest::getHash() const
|
|
|
|
{
|
|
|
|
if(isKeyMaster(q))
|
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
return q.getObject(cb)->subID;
|
2021-05-16 14:56:42 +03:00
|
|
|
}
|
|
|
|
|
2025-04-19 16:12:07 +03:00
|
|
|
return q.getObject(cb)->id.getNum();
|
2021-05-16 14:56:42 +03:00
|
|
|
}
|
|
|
|
|
2021-05-16 14:39:18 +03:00
|
|
|
std::string CompleteQuest::questToString() const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2021-05-16 14:56:42 +03:00
|
|
|
if(isKeyMaster(q))
|
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
return "find " + LIBRARY->generaltexth->tentColors[q.getObject(cb)->subID] + " keymaster tent";
|
2021-05-16 14:56:42 +03:00
|
|
|
}
|
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
if(q.getQuest(cb)->questName == CQuest::missionName(EQuestMission::NONE))
|
2021-05-16 14:38:26 +03:00
|
|
|
return "inactive quest";
|
|
|
|
|
|
|
|
MetaString ms;
|
2025-03-18 19:54:09 +00:00
|
|
|
q.getQuest(cb)->getRolloverText(cb, ms, false);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
|
|
|
return ms.toString();
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::tryCompleteQuest(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
auto paths = ai->pathfinder->getPathInfo(q.getObject(cb)->visitablePos());
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2021-05-16 14:56:49 +03:00
|
|
|
vstd::erase_if(paths, [&](const AIPath & path) -> bool
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
return !q.getQuest(cb)->checkQuest(path.targetHero);
|
2021-05-16 14:56:49 +03:00
|
|
|
});
|
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
return CaptureObjectsBehavior::getVisitGoals(paths, ai, q.getObject(cb));
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionArt(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec solutions = tryCompleteQuest(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
|
|
|
if(!solutions.empty())
|
|
|
|
return solutions;
|
|
|
|
|
2021-05-16 14:39:47 +03:00
|
|
|
CaptureObjectsBehavior findArts;
|
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
for(auto art : q.getQuest(cb)->mission.artifacts)
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-04-19 16:12:07 +03:00
|
|
|
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::ARTIFACT, art.getNum())));
|
2021-05-16 14:39:47 +03:00
|
|
|
}
|
2021-05-16 14:38:26 +03:00
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionHero(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec solutions = tryCompleteQuest(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
{
|
|
|
|
//rule of a thumb - quest heroes usually are locked in prisons
|
2021-05-16 14:56:49 +03:00
|
|
|
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::PRISON)));
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionArmy(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
auto paths = ai->pathfinder->getPathInfo(q.getObject(cb)->visitablePos());
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2021-05-23 14:28:43 +03:00
|
|
|
vstd::erase_if(paths, [&](const AIPath & path) -> bool
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
return !CQuest::checkMissionArmy(q.getQuest(cb), path.heroArmy);
|
2021-05-23 14:28:43 +03:00
|
|
|
});
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2025-03-18 19:54:09 +00:00
|
|
|
return CaptureObjectsBehavior::getVisitGoals(paths, ai, q.getObject(cb));
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionIncreasePrimaryStat(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
return tryCompleteQuest(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionLevel(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
return tryCompleteQuest(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionKeymaster(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
if(isObjectPassable(ai, q.getObject(cb)))
|
2021-05-16 14:56:27 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
return CaptureObjectsBehavior(q.getObject(cb)).decompose(ai);
|
2021-05-16 14:56:27 +03:00
|
|
|
}
|
|
|
|
else
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
return CaptureObjectsBehavior().ofType(Obj::KEYMASTER, q.getObject(cb)->subID).decompose(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionResources(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec solutions = tryCompleteQuest(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
TGoalVec CompleteQuest::missionDestroyObj(const Nullkiller * ai) const
|
2021-05-16 14:38:26 +03:00
|
|
|
{
|
2025-03-18 19:54:09 +00:00
|
|
|
auto obj = ai->cb->getObj(q.getQuest(cb)->killTarget);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
|
|
|
if(!obj)
|
2025-03-18 19:54:09 +00:00
|
|
|
return CaptureObjectsBehavior(q.getObject(cb)).decompose(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2024-03-31 18:39:00 +03:00
|
|
|
auto relations = ai->cb->getPlayerRelations(ai->playerID, obj->tempOwner);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2021-05-16 14:39:47 +03:00
|
|
|
//if(relations == PlayerRelations::SAME_PLAYER)
|
|
|
|
//{
|
|
|
|
// auto heroToProtect = cb->getHero(obj->id);
|
2021-05-16 14:38:26 +03:00
|
|
|
|
2021-05-16 14:39:47 +03:00
|
|
|
// //solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
|
|
|
|
//}
|
|
|
|
//else
|
|
|
|
if(relations == PlayerRelations::ENEMIES)
|
|
|
|
{
|
2024-03-31 18:39:00 +03:00
|
|
|
return CaptureObjectsBehavior(obj).decompose(ai);
|
2021-05-16 14:38:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return TGoalVec();
|
2022-09-26 21:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|