CompleteQuest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "Goals.h"
  12. #include "../VCAI.h"
  13. #include "../FuzzyHelper.h"
  14. #include "../AIhelper.h"
  15. #include "../../../lib/mapping/CMap.h" //for victory conditions
  16. #include "../../../lib/CPathfinder.h"
  17. extern boost::thread_specific_ptr<CCallback> cb;
  18. extern boost::thread_specific_ptr<VCAI> ai;
  19. extern FuzzyHelper * fh;
  20. using namespace Goals;
  21. bool CompleteQuest::operator==(const CompleteQuest & other) const
  22. {
  23. return q.quest->qid == other.q.quest->qid;
  24. }
  25. TGoalVec CompleteQuest::getAllPossibleSubgoals()
  26. {
  27. TGoalVec solutions;
  28. if(q.quest->missionType && q.quest->progress != CQuest::COMPLETE)
  29. {
  30. logAi->debug("Trying to realize quest: %s", questToString());
  31. switch(q.quest->missionType)
  32. {
  33. case CQuest::MISSION_ART:
  34. return missionArt();
  35. case CQuest::MISSION_HERO:
  36. return missionHero();
  37. case CQuest::MISSION_ARMY:
  38. return missionArmy();
  39. case CQuest::MISSION_RESOURCES:
  40. return missionResources();
  41. case CQuest::MISSION_KILL_HERO:
  42. case CQuest::MISSION_KILL_CREATURE:
  43. return missionDestroyObj();
  44. case CQuest::MISSION_PRIMARY_STAT:
  45. return missionIncreasePrimaryStat();
  46. case CQuest::MISSION_LEVEL:
  47. return missionLevel();
  48. case CQuest::MISSION_PLAYER:
  49. if(ai->playerID.getNum() != q.quest->m13489val)
  50. logAi->debug("Can't be player of color %d", q.quest->m13489val);
  51. break;
  52. case CQuest::MISSION_KEYMASTER:
  53. return missionKeymaster();
  54. } //end of switch
  55. }
  56. return TGoalVec();
  57. }
  58. TSubgoal CompleteQuest::whatToDoToAchieve()
  59. {
  60. if(q.quest->missionType == CQuest::MISSION_NONE)
  61. {
  62. throw cannotFulfillGoalException("Can not complete inactive quest");
  63. }
  64. TGoalVec solutions = getAllPossibleSubgoals();
  65. if(solutions.empty())
  66. throw cannotFulfillGoalException("Can not complete quest " + questToString());
  67. TSubgoal result = fh->chooseSolution(solutions);
  68. logAi->trace(
  69. "Returning %s, tile: %s, objid: %d, hero: %s",
  70. result->name(),
  71. result->tile.toString(),
  72. result->objid,
  73. result->hero.validAndSet() ? result->hero->getNameTranslated() : "not specified");
  74. return result;
  75. }
  76. std::string CompleteQuest::name() const
  77. {
  78. return "CompleteQuest";
  79. }
  80. std::string CompleteQuest::completeMessage() const
  81. {
  82. return "Completed quest " + questToString();
  83. }
  84. std::string CompleteQuest::questToString() const
  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. TGoalVec solutions;
  95. auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
  96. for(auto hero : heroes)
  97. {
  98. if(q.quest->checkQuest(hero))
  99. {
  100. vstd::concatenate(solutions, ai->ah->howToVisitObj(hero, ObjectIdRef(q.obj->id)));
  101. }
  102. }
  103. return solutions;
  104. }
  105. TGoalVec CompleteQuest::missionArt() const
  106. {
  107. TGoalVec solutions = tryCompleteQuest();
  108. if(!solutions.empty())
  109. return solutions;
  110. for(auto art : q.quest->m5arts)
  111. {
  112. solutions.push_back(sptr(GetArtOfType(art))); //TODO: transport?
  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(FindObj(Obj::PRISON)));
  123. }
  124. return solutions;
  125. }
  126. TGoalVec CompleteQuest::missionArmy() const
  127. {
  128. TGoalVec solutions = tryCompleteQuest();
  129. if(!solutions.empty())
  130. return solutions;
  131. for(auto creature : q.quest->m6creatures)
  132. {
  133. solutions.push_back(sptr(GatherTroops(creature.type->getId(), creature.count)));
  134. }
  135. return solutions;
  136. }
  137. TGoalVec CompleteQuest::missionIncreasePrimaryStat() const
  138. {
  139. TGoalVec solutions = tryCompleteQuest();
  140. if(solutions.empty())
  141. {
  142. for(int i = 0; i < q.quest->m2stats.size(); ++i)
  143. {
  144. // TODO: library, school and other boost objects
  145. logAi->debug("Don't know how to increase primary stat %d", i);
  146. }
  147. }
  148. return solutions;
  149. }
  150. TGoalVec CompleteQuest::missionLevel() const
  151. {
  152. TGoalVec solutions = tryCompleteQuest();
  153. if(solutions.empty())
  154. {
  155. logAi->debug("Don't know how to reach hero level %d", q.quest->m13489val);
  156. }
  157. return solutions;
  158. }
  159. TGoalVec CompleteQuest::missionKeymaster() const
  160. {
  161. TGoalVec solutions = tryCompleteQuest();
  162. if(solutions.empty())
  163. {
  164. solutions.push_back(sptr(Goals::FindObj(Obj::KEYMASTER, q.obj->subID)));
  165. }
  166. return solutions;
  167. }
  168. TGoalVec CompleteQuest::missionResources() const
  169. {
  170. TGoalVec solutions;
  171. auto heroes = cb->getHeroesInfo(); //TODO: choose best / free hero from among many possibilities?
  172. if(heroes.size())
  173. {
  174. if(q.quest->checkQuest(heroes.front())) //it doesn't matter which hero it is
  175. {
  176. return ai->ah->howToVisitObj(q.obj);
  177. }
  178. else
  179. {
  180. for(int i = 0; i < q.quest->m7resources.size(); ++i)
  181. {
  182. if(q.quest->m7resources[i])
  183. solutions.push_back(sptr(CollectRes(static_cast<EGameResID>(i), q.quest->m7resources[i])));
  184. }
  185. }
  186. }
  187. else
  188. {
  189. solutions.push_back(sptr(Goals::RecruitHero())); //FIXME: checkQuest requires any hero belonging to player :(
  190. }
  191. return solutions;
  192. }
  193. TGoalVec CompleteQuest::missionDestroyObj() const
  194. {
  195. TGoalVec solutions;
  196. auto obj = cb->getObjByQuestIdentifier(q.quest->m13489val);
  197. if(!obj)
  198. return ai->ah->howToVisitObj(q.obj);
  199. if(obj->ID == Obj::HERO)
  200. {
  201. auto relations = cb->getPlayerRelations(ai->playerID, obj->tempOwner);
  202. if(relations == PlayerRelations::SAME_PLAYER)
  203. {
  204. auto heroToProtect = cb->getHero(obj->id);
  205. solutions.push_back(sptr(GatherArmy().sethero(heroToProtect)));
  206. }
  207. else if(relations == PlayerRelations::ENEMIES)
  208. {
  209. solutions = ai->ah->howToVisitObj(obj);
  210. }
  211. }
  212. return solutions;
  213. }