CompleteQuest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "../Behaviors/CaptureObjectsBehavior.h"
  13. #include "../AIGateway.h"
  14. #include "../../../lib/GameLibrary.h"
  15. #include "../../../lib/texts/CGeneralTextHandler.h"
  16. namespace NKAI
  17. {
  18. using namespace Goals;
  19. bool isKeyMaster(const QuestInfo & q)
  20. {
  21. return q.obj && (q.obj->ID == Obj::BORDER_GATE || q.obj->ID == Obj::BORDERGUARD);
  22. }
  23. std::string CompleteQuest::toString() const
  24. {
  25. return "Complete quest " + questToString();
  26. }
  27. TGoalVec CompleteQuest::decompose(const Nullkiller * ai) const
  28. {
  29. if(isKeyMaster(q))
  30. {
  31. return missionKeymaster(ai);
  32. }
  33. logAi->debug("Trying to realize quest: %s", questToString());
  34. if(!q.quest->mission.artifacts.empty())
  35. return missionArt(ai);
  36. if(!q.quest->mission.heroes.empty())
  37. return missionHero(ai);
  38. if(!q.quest->mission.creatures.empty())
  39. return missionArmy(ai);
  40. if(q.quest->mission.resources.nonZero())
  41. return missionResources(ai);
  42. if(q.quest->killTarget != ObjectInstanceID::NONE)
  43. return missionDestroyObj(ai);
  44. for(auto & s : q.quest->mission.primary)
  45. if(s)
  46. return missionIncreasePrimaryStat(ai);
  47. if(q.quest->mission.heroLevel > 0)
  48. return missionLevel(ai);
  49. return TGoalVec();
  50. }
  51. bool CompleteQuest::operator==(const CompleteQuest & other) const
  52. {
  53. if(isKeyMaster(q))
  54. {
  55. return isKeyMaster(other.q) && q.obj->subID == other.q.obj->subID;
  56. }
  57. else if(isKeyMaster(other.q))
  58. {
  59. return false;
  60. }
  61. return q.quest->qid == other.q.quest->qid;
  62. }
  63. uint64_t CompleteQuest::getHash() const
  64. {
  65. if(isKeyMaster(q))
  66. {
  67. return q.obj->subID;
  68. }
  69. return q.quest->qid;
  70. }
  71. std::string CompleteQuest::questToString() const
  72. {
  73. if(isKeyMaster(q))
  74. {
  75. return "find " + LIBRARY->generaltexth->tentColors[q.obj->subID] + " keymaster tent";
  76. }
  77. if(q.quest->questName == CQuest::missionName(EQuestMission::NONE))
  78. return "inactive quest";
  79. MetaString ms;
  80. q.quest->getRolloverText(q.obj->cb, ms, false);
  81. return ms.toString();
  82. }
  83. TGoalVec CompleteQuest::tryCompleteQuest(const Nullkiller * ai) const
  84. {
  85. auto paths = ai->pathfinder->getPathInfo(q.obj->visitablePos());
  86. vstd::erase_if(paths, [&](const AIPath & path) -> bool
  87. {
  88. return !q.quest->checkQuest(path.targetHero);
  89. });
  90. return CaptureObjectsBehavior::getVisitGoals(paths, ai, q.obj);
  91. }
  92. TGoalVec CompleteQuest::missionArt(const Nullkiller * ai) const
  93. {
  94. TGoalVec solutions = tryCompleteQuest(ai);
  95. if(!solutions.empty())
  96. return solutions;
  97. CaptureObjectsBehavior findArts;
  98. for(auto art : q.quest->mission.artifacts)
  99. {
  100. solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::ARTIFACT, art)));
  101. }
  102. return solutions;
  103. }
  104. TGoalVec CompleteQuest::missionHero(const Nullkiller * ai) const
  105. {
  106. TGoalVec solutions = tryCompleteQuest(ai);
  107. if(solutions.empty())
  108. {
  109. //rule of a thumb - quest heroes usually are locked in prisons
  110. solutions.push_back(sptr(CaptureObjectsBehavior().ofType(Obj::PRISON)));
  111. }
  112. return solutions;
  113. }
  114. TGoalVec CompleteQuest::missionArmy(const Nullkiller * ai) const
  115. {
  116. auto paths = ai->pathfinder->getPathInfo(q.obj->visitablePos());
  117. vstd::erase_if(paths, [&](const AIPath & path) -> bool
  118. {
  119. return !CQuest::checkMissionArmy(q.quest, path.heroArmy);
  120. });
  121. return CaptureObjectsBehavior::getVisitGoals(paths, ai, q.obj);
  122. }
  123. TGoalVec CompleteQuest::missionIncreasePrimaryStat(const Nullkiller * ai) const
  124. {
  125. return tryCompleteQuest(ai);
  126. }
  127. TGoalVec CompleteQuest::missionLevel(const Nullkiller * ai) const
  128. {
  129. return tryCompleteQuest(ai);
  130. }
  131. TGoalVec CompleteQuest::missionKeymaster(const Nullkiller * ai) const
  132. {
  133. if(isObjectPassable(ai, q.obj))
  134. {
  135. return CaptureObjectsBehavior(q.obj).decompose(ai);
  136. }
  137. else
  138. {
  139. return CaptureObjectsBehavior().ofType(Obj::KEYMASTER, q.obj->subID).decompose(ai);
  140. }
  141. }
  142. TGoalVec CompleteQuest::missionResources(const Nullkiller * ai) const
  143. {
  144. TGoalVec solutions = tryCompleteQuest(ai);
  145. /*auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
  146. if(heroes.size())
  147. {
  148. if(q.quest->checkQuest(heroes.front())) //it doesn't matter which hero it is
  149. {
  150. return solutions;// ai->ah->howToVisitObj(q.obj);
  151. }
  152. else
  153. {
  154. for(int i = 0; i < q.quest->m7resources.size(); ++i)
  155. {
  156. if(q.quest->m7resources[i])
  157. solutions.push_back(sptr(CollectRes(static_cast<EGameResID>(i), q.quest->m7resources[i])));
  158. }
  159. }
  160. }
  161. else
  162. {
  163. solutions.push_back(sptr(Goals::RecruitHero())); //FIXME: checkQuest requires any hero belonging to player :(
  164. }*/
  165. return solutions;
  166. }
  167. TGoalVec CompleteQuest::missionDestroyObj(const Nullkiller * ai) const
  168. {
  169. auto obj = ai->cb->getObj(q.quest->killTarget);
  170. if(!obj)
  171. return CaptureObjectsBehavior(q.obj).decompose(ai);
  172. auto relations = ai->cb->getPlayerRelations(ai->playerID, obj->tempOwner);
  173. //if(relations == PlayerRelations::SAME_PLAYER)
  174. //{
  175. // auto heroToProtect = cb->getHero(obj->id);
  176. // //solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
  177. //}
  178. //else
  179. if(relations == PlayerRelations::ENEMIES)
  180. {
  181. return CaptureObjectsBehavior(obj).decompose(ai);
  182. }
  183. return TGoalVec();
  184. }
  185. }