CompleteQuest.cpp 5.6 KB

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