| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- /*
- * CompleteQuestBehavior.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 "CompleteQuestBehavior.h"
- #include "CaptureObjectsBehavior.h"
- #include "../VCAI.h"
- #include "../AIhelper.h"
- #include "../../../lib/mapping/CMap.h" //for victory conditions
- #include "../../../lib/CPathfinder.h"
- extern boost::thread_specific_ptr<CCallback> cb;
- extern boost::thread_specific_ptr<VCAI> ai;
- extern FuzzyHelper * fh;
- using namespace Goals;
- std::string CompleteQuestBehavior::toString() const
- {
- return "Complete Quests";
- }
- TGoalVec CompleteQuestBehavior::decompose() const
- {
- TGoalVec solutions;
- auto quests = cb->getMyQuests();
- for(auto & q : quests)
- {
- if(q.quest->missionType == CQuest::MISSION_NONE || q.quest->progress == CQuest::COMPLETE)
- {
- continue;
- }
- vstd::concatenate(solutions, getQuestTasks(q));
- }
- return solutions;
- }
- TGoalVec CompleteQuestBehavior::getQuestTasks(const QuestInfo & q) const
- {
- logAi->debug("Trying to realize quest: %s", questToString(q));
- switch(q.quest->missionType)
- {
- case CQuest::MISSION_ART:
- return missionArt(q);
- case CQuest::MISSION_HERO:
- return missionHero(q);
- case CQuest::MISSION_ARMY:
- return missionArmy(q);
- case CQuest::MISSION_RESOURCES:
- return missionResources(q);
- case CQuest::MISSION_KILL_HERO:
- case CQuest::MISSION_KILL_CREATURE:
- return missionDestroyObj(q);
- case CQuest::MISSION_PRIMARY_STAT:
- return missionIncreasePrimaryStat(q);
- case CQuest::MISSION_LEVEL:
- return missionLevel(q);
- case CQuest::MISSION_PLAYER:
- if(ai->playerID.getNum() != q.quest->m13489val)
- logAi->debug("Can't be player of color %d", q.quest->m13489val);
- break;
- case CQuest::MISSION_KEYMASTER:
- return missionKeymaster(q);
- } //end of switch
- return TGoalVec();
- }
- std::string CompleteQuestBehavior::questToString(const QuestInfo & q) const
- {
- if(q.quest->missionType == CQuest::MISSION_NONE)
- return "inactive quest";
- MetaString ms;
- q.quest->getRolloverText(ms, false);
- return ms.toString();
- }
- TGoalVec CompleteQuestBehavior::tryCompleteQuest(const QuestInfo & q) const
- {
- TGoalVec solutions;
- auto tasks = CaptureObjectsBehavior(q.obj).decompose(); //TODO: choose best / free hero from among many possibilities?
- for(auto task : tasks)
- {
- if(task->hero && q.quest->checkQuest(task->hero.get()))
- {
- solutions.push_back(task);
- }
- }
- return solutions;
- }
- TGoalVec CompleteQuestBehavior::missionArt(const QuestInfo & q) const
- {
- TGoalVec solutions = tryCompleteQuest(q);
- if(!solutions.empty())
- return solutions;
- /*for(auto art : q.quest->m5arts)
- {
- solutions.push_back(sptr(GetArtOfType(art))); //TODO: transport?
- }*/
- return solutions;
- }
- TGoalVec CompleteQuestBehavior::missionHero(const QuestInfo & q) const
- {
- TGoalVec solutions = tryCompleteQuest(q);
- if(solutions.empty())
- {
- //rule of a thumb - quest heroes usually are locked in prisons
- return CaptureObjectsBehavior().ofType(Obj::PRISON).decompose();
- }
- return solutions;
- }
- TGoalVec CompleteQuestBehavior::missionArmy(const QuestInfo & q) const
- {
- TGoalVec solutions = tryCompleteQuest(q);
- if(!solutions.empty())
- return solutions;
- /*
- for(auto creature : q.quest->m6creatures)
- {
- solutions.push_back(sptr(GatherTroops(creature.type->idNumber, creature.count)));
- }*/
- return solutions;
- }
- TGoalVec CompleteQuestBehavior::missionIncreasePrimaryStat(const QuestInfo & q) const
- {
- return tryCompleteQuest(q);
- }
- TGoalVec CompleteQuestBehavior::missionLevel(const QuestInfo & q) const
- {
- return tryCompleteQuest(q);
- }
- TGoalVec CompleteQuestBehavior::missionKeymaster(const QuestInfo & q) const
- {
- TGoalVec solutions = tryCompleteQuest(q);
- if(solutions.empty())
- {
- return CaptureObjectsBehavior().ofType(Obj::KEYMASTER, q.obj->subID).decompose();
- }
- return solutions;
- }
- TGoalVec CompleteQuestBehavior::missionResources(const QuestInfo & q) 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 solutions;// ai->ah->howToVisitObj(q.obj);
- }
- else
- {
- for(int i = 0; i < q.quest->m7resources.size(); ++i)
- {
- if(q.quest->m7resources[i])
- solutions.push_back(sptr(CollectRes(i, q.quest->m7resources[i])));
- }
- }
- }
- else
- {
- solutions.push_back(sptr(Goals::RecruitHero())); //FIXME: checkQuest requires any hero belonging to player :(
- }*/
- return solutions;
- }
- TGoalVec CompleteQuestBehavior::missionDestroyObj(const QuestInfo & q) const
- {
- auto obj = cb->getObjByQuestIdentifier(q.quest->m13489val);
- if(!obj)
- return CaptureObjectsBehavior(q.obj).decompose();
- 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)
- {
- return CaptureObjectsBehavior(obj).decompose();
- }
- }
- return TGoalVec();
- }
|