2021-05-16 13:38:26 +02:00
|
|
|
/*
|
2021-05-16 13:39:18 +02:00
|
|
|
* CompleteQuest.cpp, part of VCMI engine
|
2021-05-16 13:38:26 +02: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 13:56:35 +02:00
|
|
|
#include "CompleteQuest.h"
|
|
|
|
#include "../Behaviors/CaptureObjectsBehavior.h"
|
2021-05-16 14:39:38 +02:00
|
|
|
#include "../AIGateway.h"
|
2021-05-16 13:56:42 +02:00
|
|
|
#include "../../../lib/VCMI_Lib.h"
|
|
|
|
#include "../../../lib/CGeneralTextHandler.h"
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2021-05-16 13:38:26 +02:00
|
|
|
using namespace Goals;
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
bool isKeyMaster(const QuestInfo & q)
|
|
|
|
{
|
|
|
|
return q.obj && (q.obj->ID == Obj::BORDER_GATE || q.obj->ID == Obj::BORDERGUARD);
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
std::string CompleteQuest::toString() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:39:18 +02:00
|
|
|
return "Complete quest " + questToString();
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::decompose() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
if(isKeyMaster(q))
|
2021-05-16 13:39:18 +02:00
|
|
|
{
|
|
|
|
return missionKeymaster();
|
|
|
|
}
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
logAi->debug("Trying to realize quest: %s", questToString());
|
2023-10-09 19:15:34 +02:00
|
|
|
|
|
|
|
if(!q.quest->artifacts.empty())
|
2021-05-16 13:39:18 +02:00
|
|
|
return missionArt();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(!q.quest->heroes.empty())
|
2021-05-16 13:39:18 +02:00
|
|
|
return missionHero();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(!q.quest->creatures.empty())
|
2021-05-16 13:39:18 +02:00
|
|
|
return missionArmy();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->resources.nonZero())
|
2021-05-16 13:39:18 +02:00
|
|
|
return missionResources();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->killTarget >= 0)
|
2021-05-16 13:39:18 +02:00
|
|
|
return missionDestroyObj();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
for(auto & s : q.quest->primary)
|
|
|
|
if(s)
|
|
|
|
return missionIncreasePrimaryStat();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->heroLevel > 0)
|
2021-05-16 13:39:18 +02:00
|
|
|
return missionLevel();
|
2023-10-09 19:15:34 +02:00
|
|
|
|
|
|
|
if(q.quest->questName == CQuest::missionName(10))
|
2021-05-16 13:39:18 +02:00
|
|
|
return missionKeymaster();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
|
|
|
return TGoalVec();
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
bool CompleteQuest::operator==(const CompleteQuest & other) const
|
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
if(isKeyMaster(q))
|
|
|
|
{
|
|
|
|
return isKeyMaster(other.q) && q.obj->subID == other.q.obj->subID;
|
|
|
|
}
|
|
|
|
else if(isKeyMaster(other.q))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
return q.quest->qid == other.q.quest->qid;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
uint64_t CompleteQuest::getHash() const
|
|
|
|
{
|
|
|
|
if(isKeyMaster(q))
|
|
|
|
{
|
|
|
|
return q.obj->subID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return q.quest->qid;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
std::string CompleteQuest::questToString() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
if(isKeyMaster(q))
|
|
|
|
{
|
|
|
|
return "find " + VLC->generaltexth->tentColors[q.obj->subID] + " keymaster tent";
|
|
|
|
}
|
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->questName == CQuest::missionName(0))
|
2021-05-16 13:38:26 +02:00
|
|
|
return "inactive quest";
|
|
|
|
|
|
|
|
MetaString ms;
|
|
|
|
q.quest->getRolloverText(ms, false);
|
|
|
|
|
|
|
|
return ms.toString();
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::tryCompleteQuest() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:56:49 +02:00
|
|
|
auto paths = ai->nullkiller->pathfinder->getPathInfo(q.obj->visitablePos());
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2021-05-16 13:56:49 +02:00
|
|
|
vstd::erase_if(paths, [&](const AIPath & path) -> bool
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:56:49 +02:00
|
|
|
return !q.quest->checkQuest(path.targetHero);
|
|
|
|
});
|
|
|
|
|
|
|
|
return CaptureObjectsBehavior::getVisitGoals(paths, q.obj);
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionArt() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
|
|
|
if(!solutions.empty())
|
|
|
|
return solutions;
|
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
CaptureObjectsBehavior findArts;
|
|
|
|
|
2023-10-09 05:24:40 +02:00
|
|
|
for(auto art : q.quest->artifacts)
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::ARTIFACT, art)));
|
|
|
|
}
|
2021-05-16 13:38:26 +02:00
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionHero() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
{
|
|
|
|
//rule of a thumb - quest heroes usually are locked in prisons
|
2021-05-16 13:56:49 +02:00
|
|
|
solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::PRISON)));
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionArmy() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-23 13:28:43 +02:00
|
|
|
auto paths = ai->nullkiller->pathfinder->getPathInfo(q.obj->visitablePos());
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2021-05-23 13:28:43 +02:00
|
|
|
vstd::erase_if(paths, [&](const AIPath & path) -> bool
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-23 13:28:43 +02:00
|
|
|
return !CQuest::checkMissionArmy(q.quest, path.heroArmy);
|
|
|
|
});
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2021-05-23 13:28:43 +02:00
|
|
|
return CaptureObjectsBehavior::getVisitGoals(paths, q.obj);
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionIncreasePrimaryStat() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:39:18 +02:00
|
|
|
return tryCompleteQuest();
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionLevel() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:39:18 +02:00
|
|
|
return tryCompleteQuest();
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionKeymaster() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 13:56:27 +02:00
|
|
|
if(isObjectPassable(q.obj))
|
|
|
|
{
|
|
|
|
return CaptureObjectsBehavior(q.obj).decompose();
|
|
|
|
}
|
|
|
|
else
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
|
|
|
return CaptureObjectsBehavior().ofType(Obj::KEYMASTER, q.obj->subID).decompose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionResources() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2021-05-16 14:00:24 +02:00
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
2021-05-16 13:38:26 +02:00
|
|
|
|
|
|
|
/*auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
|
|
|
|
|
|
|
|
if(heroes.size())
|
|
|
|
{
|
|
|
|
if(q.quest->checkQuest(heroes.front())) //it doesn't matter which hero it is
|
|
|
|
{
|
|
|
|
return solutions;// ai->ah->howToVisitObj(q.obj);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int i = 0; i < q.quest->m7resources.size(); ++i)
|
|
|
|
{
|
|
|
|
if(q.quest->m7resources[i])
|
2023-04-05 02:26:29 +02:00
|
|
|
solutions.push_back(sptr(CollectRes(static_cast<EGameResID>(i), q.quest->m7resources[i])));
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solutions.push_back(sptr(Goals::RecruitHero())); //FIXME: checkQuest requires any hero belonging to player :(
|
|
|
|
}*/
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:39:18 +02:00
|
|
|
TGoalVec CompleteQuest::missionDestroyObj() const
|
2021-05-16 13:38:26 +02:00
|
|
|
{
|
2023-10-09 05:24:40 +02:00
|
|
|
auto obj = cb->getObjByQuestIdentifier(q.quest->killTarget);
|
2021-05-16 13:38:26 +02:00
|
|
|
|
|
|
|
if(!obj)
|
|
|
|
return CaptureObjectsBehavior(q.obj).decompose();
|
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
auto relations = cb->getPlayerRelations(ai->playerID, obj->tempOwner);
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
//if(relations == PlayerRelations::SAME_PLAYER)
|
|
|
|
//{
|
|
|
|
// auto heroToProtect = cb->getHero(obj->id);
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
// //solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
|
|
|
|
//}
|
|
|
|
//else
|
|
|
|
if(relations == PlayerRelations::ENEMIES)
|
|
|
|
{
|
|
|
|
return CaptureObjectsBehavior(obj).decompose();
|
2021-05-16 13:38:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TGoalVec();
|
2022-09-26 20:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|