CompleteQuest.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * CompleteQuest.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 "Goals.h"
  12. #include "../VCAI.h"
  13. #include "../FuzzyHelper.h"
  14. #include "../AIhelper.h"
  15. #include "../../../lib/mapObjects/CQuest.h"
  16. using namespace Goals;
  17. bool CompleteQuest::operator==(const CompleteQuest & other) const
  18. {
  19. return q.quest->qid == other.q.quest->qid;
  20. }
  21. TGoalVec CompleteQuest::getAllPossibleSubgoals()
  22. {
  23. TGoalVec solutions;
  24. if(q.quest->progress != CQuest::COMPLETE)
  25. {
  26. logAi->debug("Trying to realize quest: %s", questToString());
  27. if(!q.quest->artifacts.empty())
  28. return missionArt();
  29. if(!q.quest->heroes.empty())
  30. return missionHero();
  31. if(!q.quest->creatures.empty())
  32. return missionArmy();
  33. if(q.quest->resources.nonZero())
  34. return missionResources();
  35. if(q.quest->killTarget >= 0)
  36. return missionDestroyObj();
  37. for(auto & s : q.quest->primary)
  38. if(s)
  39. return missionIncreasePrimaryStat();
  40. if(q.quest->heroLevel > 0)
  41. return missionLevel();
  42. if(q.quest->questName == CQuest::missionName(10))
  43. return missionKeymaster();
  44. }
  45. return TGoalVec();
  46. }
  47. TSubgoal CompleteQuest::whatToDoToAchieve()
  48. {
  49. if(q.quest->questName == CQuest::missionName(0))
  50. {
  51. throw cannotFulfillGoalException("Can not complete inactive quest");
  52. }
  53. TGoalVec solutions = getAllPossibleSubgoals();
  54. if(solutions.empty())
  55. throw cannotFulfillGoalException("Can not complete quest " + questToString());
  56. TSubgoal result = fh->chooseSolution(solutions);
  57. logAi->trace(
  58. "Returning %s, tile: %s, objid: %d, hero: %s",
  59. result->name(),
  60. result->tile.toString(),
  61. result->objid,
  62. result->hero.validAndSet() ? result->hero->getNameTranslated() : "not specified");
  63. return result;
  64. }
  65. std::string CompleteQuest::name() const
  66. {
  67. return "CompleteQuest";
  68. }
  69. std::string CompleteQuest::completeMessage() const
  70. {
  71. return "Completed quest " + questToString();
  72. }
  73. std::string CompleteQuest::questToString() const
  74. {
  75. if(q.quest->questName == CQuest::missionName(0))
  76. return "inactive quest";
  77. MetaString ms;
  78. q.quest->getRolloverText(ms, false);
  79. return ms.toString();
  80. }
  81. TGoalVec CompleteQuest::tryCompleteQuest() const
  82. {
  83. TGoalVec solutions;
  84. auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
  85. for(auto hero : heroes)
  86. {
  87. if(q.quest->checkQuest(hero))
  88. {
  89. vstd::concatenate(solutions, ai->ah->howToVisitObj(hero, ObjectIdRef(q.obj->id)));
  90. }
  91. }
  92. return solutions;
  93. }
  94. TGoalVec CompleteQuest::missionArt() const
  95. {
  96. TGoalVec solutions = tryCompleteQuest();
  97. if(!solutions.empty())
  98. return solutions;
  99. for(auto art : q.quest->artifacts)
  100. {
  101. solutions.push_back(sptr(GetArtOfType(art))); //TODO: transport?
  102. }
  103. return solutions;
  104. }
  105. TGoalVec CompleteQuest::missionHero() const
  106. {
  107. TGoalVec solutions = tryCompleteQuest();
  108. if(solutions.empty())
  109. {
  110. //rule of a thumb - quest heroes usually are locked in prisons
  111. solutions.push_back(sptr(FindObj(Obj::PRISON)));
  112. }
  113. return solutions;
  114. }
  115. TGoalVec CompleteQuest::missionArmy() const
  116. {
  117. TGoalVec solutions = tryCompleteQuest();
  118. if(!solutions.empty())
  119. return solutions;
  120. for(auto creature : q.quest->creatures)
  121. {
  122. solutions.push_back(sptr(GatherTroops(creature.type->getId(), creature.count)));
  123. }
  124. return solutions;
  125. }
  126. TGoalVec CompleteQuest::missionIncreasePrimaryStat() const
  127. {
  128. TGoalVec solutions = tryCompleteQuest();
  129. if(solutions.empty())
  130. {
  131. for(int i = 0; i < q.quest->primary.size(); ++i)
  132. {
  133. // TODO: library, school and other boost objects
  134. logAi->debug("Don't know how to increase primary stat %d", i);
  135. }
  136. }
  137. return solutions;
  138. }
  139. TGoalVec CompleteQuest::missionLevel() const
  140. {
  141. TGoalVec solutions = tryCompleteQuest();
  142. if(solutions.empty())
  143. {
  144. logAi->debug("Don't know how to reach hero level %d", q.quest->heroLevel);
  145. }
  146. return solutions;
  147. }
  148. TGoalVec CompleteQuest::missionKeymaster() const
  149. {
  150. TGoalVec solutions = tryCompleteQuest();
  151. if(solutions.empty())
  152. {
  153. solutions.push_back(sptr(Goals::FindObj(Obj::KEYMASTER, q.obj->subID)));
  154. }
  155. return solutions;
  156. }
  157. TGoalVec CompleteQuest::missionResources() const
  158. {
  159. TGoalVec solutions;
  160. auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
  161. if(heroes.size())
  162. {
  163. if(q.quest->checkQuest(heroes.front())) //it doesn't matter which hero it is
  164. {
  165. return ai->ah->howToVisitObj(q.obj);
  166. }
  167. else
  168. {
  169. for(int i = 0; i < q.quest->resources.size(); ++i)
  170. {
  171. if(q.quest->resources[i])
  172. solutions.push_back(sptr(CollectRes(static_cast<EGameResID>(i), q.quest->resources[i])));
  173. }
  174. }
  175. }
  176. else
  177. {
  178. solutions.push_back(sptr(Goals::RecruitHero())); //FIXME: checkQuest requires any hero belonging to player :(
  179. }
  180. return solutions;
  181. }
  182. TGoalVec CompleteQuest::missionDestroyObj() const
  183. {
  184. TGoalVec solutions;
  185. auto obj = cb->getObjByQuestIdentifier(q.quest->killTarget);
  186. if(!obj)
  187. return ai->ah->howToVisitObj(q.obj);
  188. if(obj->ID == Obj::HERO)
  189. {
  190. auto relations = cb->getPlayerRelations(ai->playerID, obj->tempOwner);
  191. if(relations == PlayerRelations::SAME_PLAYER)
  192. {
  193. auto heroToProtect = cb->getHero(obj->id);
  194. solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
  195. }
  196. else if(relations == PlayerRelations::ENEMIES)
  197. {
  198. solutions = ai->ah->howToVisitObj(obj);
  199. }
  200. }
  201. return solutions;
  202. }