CompleteQuest.cpp 5.1 KB

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