CompleteQuest.cpp 5.5 KB

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