CompleteQuestBehavior.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * CompleteQuestBehavior.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CompleteQuestBehavior.h"
  12. #include "CaptureObjectsBehavior.h"
  13. #include "../VCAI.h"
  14. #include "../AIhelper.h"
  15. #include "../../../lib/mapping/CMap.h" //for victory conditions
  16. #include "../../../lib/CPathfinder.h"
  17. extern boost::thread_specific_ptr<CCallback> cb;
  18. extern boost::thread_specific_ptr<VCAI> ai;
  19. extern FuzzyHelper * fh;
  20. using namespace Goals;
  21. std::string CompleteQuestBehavior::toString() const
  22. {
  23. return "Complete Quests";
  24. }
  25. TGoalVec CompleteQuestBehavior::decompose() const
  26. {
  27. TGoalVec solutions;
  28. auto quests = cb->getMyQuests();
  29. for(auto & q : quests)
  30. {
  31. if(q.quest->missionType == CQuest::MISSION_NONE || q.quest->progress == CQuest::COMPLETE)
  32. {
  33. continue;
  34. }
  35. vstd::concatenate(solutions, getQuestTasks(q));
  36. }
  37. return solutions;
  38. }
  39. TGoalVec CompleteQuestBehavior::getQuestTasks(const QuestInfo & q) const
  40. {
  41. logAi->debug("Trying to realize quest: %s", questToString(q));
  42. switch(q.quest->missionType)
  43. {
  44. case CQuest::MISSION_ART:
  45. return missionArt(q);
  46. case CQuest::MISSION_HERO:
  47. return missionHero(q);
  48. case CQuest::MISSION_ARMY:
  49. return missionArmy(q);
  50. case CQuest::MISSION_RESOURCES:
  51. return missionResources(q);
  52. case CQuest::MISSION_KILL_HERO:
  53. case CQuest::MISSION_KILL_CREATURE:
  54. return missionDestroyObj(q);
  55. case CQuest::MISSION_PRIMARY_STAT:
  56. return missionIncreasePrimaryStat(q);
  57. case CQuest::MISSION_LEVEL:
  58. return missionLevel(q);
  59. case CQuest::MISSION_PLAYER:
  60. if(ai->playerID.getNum() != q.quest->m13489val)
  61. logAi->debug("Can't be player of color %d", q.quest->m13489val);
  62. break;
  63. case CQuest::MISSION_KEYMASTER:
  64. return missionKeymaster(q);
  65. } //end of switch
  66. return TGoalVec();
  67. }
  68. std::string CompleteQuestBehavior::questToString(const QuestInfo & q) const
  69. {
  70. if(q.quest->missionType == CQuest::MISSION_NONE)
  71. return "inactive quest";
  72. MetaString ms;
  73. q.quest->getRolloverText(ms, false);
  74. return ms.toString();
  75. }
  76. TGoalVec CompleteQuestBehavior::tryCompleteQuest(const QuestInfo & q) const
  77. {
  78. TGoalVec solutions;
  79. auto tasks = CaptureObjectsBehavior(q.obj).decompose(); //TODO: choose best / free hero from among many possibilities?
  80. for(auto task : tasks)
  81. {
  82. if(task->hero && q.quest->checkQuest(task->hero.get()))
  83. {
  84. solutions.push_back(task);
  85. }
  86. }
  87. return solutions;
  88. }
  89. TGoalVec CompleteQuestBehavior::missionArt(const QuestInfo & q) const
  90. {
  91. TGoalVec solutions = tryCompleteQuest(q);
  92. if(!solutions.empty())
  93. return solutions;
  94. /*for(auto art : q.quest->m5arts)
  95. {
  96. solutions.push_back(sptr(GetArtOfType(art))); //TODO: transport?
  97. }*/
  98. return solutions;
  99. }
  100. TGoalVec CompleteQuestBehavior::missionHero(const QuestInfo & q) const
  101. {
  102. TGoalVec solutions = tryCompleteQuest(q);
  103. if(solutions.empty())
  104. {
  105. //rule of a thumb - quest heroes usually are locked in prisons
  106. return CaptureObjectsBehavior().ofType(Obj::PRISON).decompose();
  107. }
  108. return solutions;
  109. }
  110. TGoalVec CompleteQuestBehavior::missionArmy(const QuestInfo & q) const
  111. {
  112. TGoalVec solutions = tryCompleteQuest(q);
  113. if(!solutions.empty())
  114. return solutions;
  115. /*
  116. for(auto creature : q.quest->m6creatures)
  117. {
  118. solutions.push_back(sptr(GatherTroops(creature.type->idNumber, creature.count)));
  119. }*/
  120. return solutions;
  121. }
  122. TGoalVec CompleteQuestBehavior::missionIncreasePrimaryStat(const QuestInfo & q) const
  123. {
  124. return tryCompleteQuest(q);
  125. }
  126. TGoalVec CompleteQuestBehavior::missionLevel(const QuestInfo & q) const
  127. {
  128. return tryCompleteQuest(q);
  129. }
  130. TGoalVec CompleteQuestBehavior::missionKeymaster(const QuestInfo & q) const
  131. {
  132. TGoalVec solutions = tryCompleteQuest(q);
  133. if(solutions.empty())
  134. {
  135. return CaptureObjectsBehavior().ofType(Obj::KEYMASTER, q.obj->subID).decompose();
  136. }
  137. return solutions;
  138. }
  139. TGoalVec CompleteQuestBehavior::missionResources(const QuestInfo & q) const
  140. {
  141. TGoalVec solutions;
  142. /*auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
  143. if(heroes.size())
  144. {
  145. if(q.quest->checkQuest(heroes.front())) //it doesn't matter which hero it is
  146. {
  147. return solutions;// ai->ah->howToVisitObj(q.obj);
  148. }
  149. else
  150. {
  151. for(int i = 0; i < q.quest->m7resources.size(); ++i)
  152. {
  153. if(q.quest->m7resources[i])
  154. solutions.push_back(sptr(CollectRes(i, q.quest->m7resources[i])));
  155. }
  156. }
  157. }
  158. else
  159. {
  160. solutions.push_back(sptr(Goals::RecruitHero())); //FIXME: checkQuest requires any hero belonging to player :(
  161. }*/
  162. return solutions;
  163. }
  164. TGoalVec CompleteQuestBehavior::missionDestroyObj(const QuestInfo & q) const
  165. {
  166. auto obj = cb->getObjByQuestIdentifier(q.quest->m13489val);
  167. if(!obj)
  168. return CaptureObjectsBehavior(q.obj).decompose();
  169. if(obj->ID == Obj::HERO)
  170. {
  171. auto relations = cb->getPlayerRelations(ai->playerID, obj->tempOwner);
  172. //if(relations == PlayerRelations::SAME_PLAYER)
  173. //{
  174. // auto heroToProtect = cb->getHero(obj->id);
  175. // //solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
  176. //}
  177. //else
  178. if(relations == PlayerRelations::ENEMIES)
  179. {
  180. return CaptureObjectsBehavior(obj).decompose();
  181. }
  182. }
  183. return TGoalVec();
  184. }