CompleteQuest.cpp 5.2 KB

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