CompleteQuest.cpp 5.7 KB

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