CompleteQuest.cpp 5.0 KB

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