CompleteQuest.cpp 5.3 KB

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