CompleteQuest.cpp 5.0 KB

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