CompleteQuest.cpp 5.2 KB

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