2018-12-09 15:20:46 +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 "Goals.h"
|
|
|
|
#include "../VCAI.h"
|
|
|
|
#include "../FuzzyHelper.h"
|
|
|
|
#include "../AIhelper.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../../../lib/mapObjects/CQuest.h"
|
2018-12-09 15:20:46 +02:00
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
|
|
|
bool CompleteQuest::operator==(const CompleteQuest & other) const
|
|
|
|
{
|
|
|
|
return q.quest->qid == other.q.quest->qid;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::getAllPossibleSubgoals()
|
|
|
|
{
|
|
|
|
TGoalVec solutions;
|
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->progress != CQuest::COMPLETE)
|
2018-12-09 15:20:46 +02:00
|
|
|
{
|
|
|
|
logAi->debug("Trying to realize quest: %s", questToString());
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
if(!q.quest->mission.artifacts.empty())
|
2018-12-09 15:20:46 +02:00
|
|
|
return missionArt();
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
if(!q.quest->mission.heroes.empty())
|
2018-12-09 15:20:46 +02:00
|
|
|
return missionHero();
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
if(!q.quest->mission.creatures.empty())
|
2018-12-09 15:20:46 +02:00
|
|
|
return missionArmy();
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
if(q.quest->mission.resources.nonZero())
|
2018-12-09 15:20:46 +02:00
|
|
|
return missionResources();
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
if(q.quest->killTarget != ObjectInstanceID::NONE)
|
2018-12-09 15:20:46 +02:00
|
|
|
return missionDestroyObj();
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
for(auto & s : q.quest->mission.primary)
|
2023-10-09 19:15:34 +02:00
|
|
|
if(s)
|
|
|
|
return missionIncreasePrimaryStat();
|
2018-12-09 15:20:46 +02:00
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
if(q.quest->mission.heroLevel > 0)
|
2018-12-09 15:20:46 +02:00
|
|
|
return missionLevel();
|
|
|
|
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->questName == CQuest::missionName(10))
|
2018-12-09 15:20:46 +02:00
|
|
|
return missionKeymaster();
|
|
|
|
}
|
|
|
|
|
|
|
|
return TGoalVec();
|
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal CompleteQuest::whatToDoToAchieve()
|
|
|
|
{
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->questName == CQuest::missionName(0))
|
2018-12-09 15:20:46 +02:00
|
|
|
{
|
|
|
|
throw cannotFulfillGoalException("Can not complete inactive quest");
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec solutions = getAllPossibleSubgoals();
|
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
throw cannotFulfillGoalException("Can not complete quest " + questToString());
|
|
|
|
|
|
|
|
TSubgoal result = fh->chooseSolution(solutions);
|
|
|
|
|
|
|
|
logAi->trace(
|
|
|
|
"Returning %s, tile: %s, objid: %d, hero: %s",
|
|
|
|
result->name(),
|
|
|
|
result->tile.toString(),
|
|
|
|
result->objid,
|
2023-01-02 13:27:03 +02:00
|
|
|
result->hero.validAndSet() ? result->hero->getNameTranslated() : "not specified");
|
2018-12-09 15:20:46 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CompleteQuest::name() const
|
|
|
|
{
|
|
|
|
return "CompleteQuest";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CompleteQuest::completeMessage() const
|
|
|
|
{
|
|
|
|
return "Completed quest " + questToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CompleteQuest::questToString() const
|
|
|
|
{
|
2023-10-09 19:15:34 +02:00
|
|
|
if(q.quest->questName == CQuest::missionName(0))
|
2018-12-09 15:20:46 +02:00
|
|
|
return "inactive quest";
|
|
|
|
|
|
|
|
MetaString ms;
|
|
|
|
q.quest->getRolloverText(ms, false);
|
|
|
|
|
|
|
|
return ms.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::tryCompleteQuest() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions;
|
|
|
|
|
|
|
|
auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
|
|
|
|
|
|
|
|
for(auto hero : heroes)
|
|
|
|
{
|
|
|
|
if(q.quest->checkQuest(hero))
|
|
|
|
{
|
|
|
|
vstd::concatenate(solutions, ai->ah->howToVisitObj(hero, ObjectIdRef(q.obj->id)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionArt() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
|
|
|
|
|
|
|
if(!solutions.empty())
|
|
|
|
return solutions;
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
for(auto art : q.quest->mission.artifacts)
|
2018-12-09 15:20:46 +02:00
|
|
|
{
|
|
|
|
solutions.push_back(sptr(GetArtOfType(art))); //TODO: transport?
|
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionHero() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
{
|
|
|
|
//rule of a thumb - quest heroes usually are locked in prisons
|
|
|
|
solutions.push_back(sptr(FindObj(Obj::PRISON)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionArmy() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
|
|
|
|
|
|
|
if(!solutions.empty())
|
|
|
|
return solutions;
|
|
|
|
|
2023-10-11 00:47:19 +02:00
|
|
|
for(auto creature : q.quest->mission.creatures)
|
2018-12-09 15:20:46 +02:00
|
|
|
{
|
2023-04-05 02:26:29 +02:00
|
|
|
solutions.push_back(sptr(GatherTroops(creature.type->getId(), creature.count)));
|
2018-12-09 15:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionIncreasePrimaryStat() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
{
|
2023-10-11 00:47:19 +02:00
|
|
|
for(int i = 0; i < q.quest->mission.primary.size(); ++i)
|
2018-12-09 15:20:46 +02:00
|
|
|
{
|
|
|
|
// TODO: library, school and other boost objects
|
|
|
|
logAi->debug("Don't know how to increase primary stat %d", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionLevel() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
{
|
2023-10-11 00:47:19 +02:00
|
|
|
logAi->debug("Don't know how to reach hero level %d", q.quest->mission.heroLevel);
|
2018-12-09 15:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionKeymaster() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions = tryCompleteQuest();
|
|
|
|
|
|
|
|
if(solutions.empty())
|
|
|
|
{
|
|
|
|
solutions.push_back(sptr(Goals::FindObj(Obj::KEYMASTER, q.obj->subID)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionResources() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions;
|
|
|
|
|
|
|
|
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 ai->ah->howToVisitObj(q.obj);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-11 00:47:19 +02:00
|
|
|
for(int i = 0; i < q.quest->mission.resources.size(); ++i)
|
2018-12-09 15:20:46 +02:00
|
|
|
{
|
2023-10-11 00:47:19 +02:00
|
|
|
if(q.quest->mission.resources[i])
|
|
|
|
solutions.push_back(sptr(CollectRes(static_cast<EGameResID>(i), q.quest->mission.resources[i])));
|
2018-12-09 15:20:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solutions.push_back(sptr(Goals::RecruitHero())); //FIXME: checkQuest requires any hero belonging to player :(
|
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec CompleteQuest::missionDestroyObj() const
|
|
|
|
{
|
|
|
|
TGoalVec solutions;
|
|
|
|
|
2023-10-09 05:24:40 +02:00
|
|
|
auto obj = cb->getObjByQuestIdentifier(q.quest->killTarget);
|
2018-12-09 15:20:46 +02:00
|
|
|
|
|
|
|
if(!obj)
|
|
|
|
return ai->ah->howToVisitObj(q.obj);
|
|
|
|
|
|
|
|
if(obj->ID == Obj::HERO)
|
|
|
|
{
|
|
|
|
auto relations = cb->getPlayerRelations(ai->playerID, obj->tempOwner);
|
|
|
|
|
|
|
|
if(relations == PlayerRelations::SAME_PLAYER)
|
|
|
|
{
|
|
|
|
auto heroToProtect = cb->getHero(obj->id);
|
|
|
|
|
|
|
|
solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
|
|
|
|
}
|
|
|
|
else if(relations == PlayerRelations::ENEMIES)
|
|
|
|
{
|
|
|
|
solutions = ai->ah->howToVisitObj(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return solutions;
|
2023-01-02 13:27:03 +02:00
|
|
|
}
|