CompleteQuest.cpp 5.2 KB

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