CompleteQuest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. throw cannotFulfillGoalException("Can not complete quest " + questToString());
  66. /*
  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->name : "not specified");
  74. return result;*/
  75. }
  76. std::string CompleteQuest::name() const
  77. {
  78. return "CompleteQuest";
  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->idNumber, 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 solutions;// 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(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 solutions;// 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. }