CompleteQuest.cpp 5.9 KB

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