Explore.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Explore.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 "../AIUtility.h"
  14. #include "../AIhelper.h"
  15. #include "../FuzzyHelper.h"
  16. #include "../ResourceManager.h"
  17. #include "../BuildingManager.h"
  18. #include "../../../lib/mapping/CMap.h" //for victory conditions
  19. #include "../../../lib/CPathfinder.h"
  20. #include "../../../lib/StringConstants.h"
  21. extern boost::thread_specific_ptr<CCallback> cb;
  22. extern boost::thread_specific_ptr<VCAI> ai;
  23. extern FuzzyHelper * fh;
  24. using namespace Goals;
  25. bool Explore::operator==(const Explore & other) const
  26. {
  27. return other.hero.h == hero.h;
  28. }
  29. std::string Explore::completeMessage() const
  30. {
  31. return "Hero " + hero.get()->name + " completed exploration";
  32. }
  33. TSubgoal Explore::whatToDoToAchieve()
  34. {
  35. auto ret = fh->chooseSolution(getAllPossibleSubgoals());
  36. if(hero) //use best step for this hero
  37. {
  38. return ret;
  39. }
  40. else
  41. {
  42. if(ret->hero.get(true))
  43. return sptr(Explore().sethero(ret->hero.h)); //choose this hero and then continue with him
  44. else
  45. return ret; //other solutions, like buying hero from tavern
  46. }
  47. }
  48. TGoalVec Explore::getAllPossibleSubgoals()
  49. {
  50. TGoalVec ret;
  51. std::vector<const CGHeroInstance *> heroes;
  52. if(hero)
  53. {
  54. heroes.push_back(hero.h);
  55. }
  56. else
  57. {
  58. //heroes = ai->getUnblockedHeroes();
  59. heroes = cb->getHeroesInfo();
  60. vstd::erase_if(heroes, [](const HeroPtr h)
  61. {
  62. if(ai->getGoal(h)->goalType == EXPLORE) //do not reassign hero who is already explorer
  63. return true;
  64. if(!ai->isAbleToExplore(h))
  65. return true;
  66. return !h->movement; //saves time, immobile heroes are useless anyway
  67. });
  68. }
  69. //try to use buildings that uncover map
  70. std::vector<const CGObjectInstance *> objs;
  71. for(auto obj : ai->visitableObjs)
  72. {
  73. if(!vstd::contains(ai->alreadyVisited, obj))
  74. {
  75. switch(obj->ID.num)
  76. {
  77. case Obj::REDWOOD_OBSERVATORY:
  78. case Obj::PILLAR_OF_FIRE:
  79. case Obj::CARTOGRAPHER:
  80. objs.push_back(obj);
  81. break;
  82. case Obj::MONOLITH_ONE_WAY_ENTRANCE:
  83. case Obj::MONOLITH_TWO_WAY:
  84. case Obj::SUBTERRANEAN_GATE:
  85. auto tObj = dynamic_cast<const CGTeleport *>(obj);
  86. assert(ai->knownTeleportChannels.find(tObj->channel) != ai->knownTeleportChannels.end());
  87. if(TeleportChannel::IMPASSABLE != ai->knownTeleportChannels[tObj->channel]->passability)
  88. objs.push_back(obj);
  89. break;
  90. }
  91. }
  92. else
  93. {
  94. switch(obj->ID.num)
  95. {
  96. case Obj::MONOLITH_TWO_WAY:
  97. case Obj::SUBTERRANEAN_GATE:
  98. auto tObj = dynamic_cast<const CGTeleport *>(obj);
  99. if(TeleportChannel::IMPASSABLE == ai->knownTeleportChannels[tObj->channel]->passability)
  100. break;
  101. for(auto exit : ai->knownTeleportChannels[tObj->channel]->exits)
  102. {
  103. if(!cb->getObj(exit))
  104. { // Always attempt to visit two-way teleports if one of channel exits is not visible
  105. objs.push_back(obj);
  106. break;
  107. }
  108. }
  109. break;
  110. }
  111. }
  112. }
  113. auto primaryHero = ai->primaryHero().h;
  114. for(auto h : heroes)
  115. {
  116. for(auto obj : objs) //double loop, performance risk?
  117. {
  118. auto waysToVisitObj = ai->ah->howToVisitObj(h, obj);
  119. vstd::concatenate(ret, waysToVisitObj);
  120. }
  121. int3 t = whereToExplore(h);
  122. if(t.valid())
  123. {
  124. ret.push_back(sptr(VisitTile(t).sethero(h)));
  125. }
  126. else
  127. {
  128. //FIXME: possible issues when gathering army to break
  129. if(hero.h == h || //exporation is assigned to this hero
  130. (!hero && h == primaryHero)) //not assigned to specific hero, let our main hero do the job
  131. {
  132. t = ai->explorationDesperate(h); //check this only ONCE, high cost
  133. if (t.valid()) //don't waste time if we are completely blocked
  134. {
  135. auto waysToVisitTile = ai->ah->howToVisitTile(h, t);
  136. vstd::concatenate(ret, waysToVisitTile);
  137. continue;
  138. }
  139. }
  140. ai->markHeroUnableToExplore(h); //there is no freely accessible tile, do not poll this hero anymore
  141. }
  142. }
  143. //we either don't have hero yet or none of heroes can explore
  144. if((!hero || ret.empty()) && ai->canRecruitAnyHero())
  145. ret.push_back(sptr(RecruitHero()));
  146. if(ret.empty())
  147. {
  148. throw goalFulfilledException(sptr(Explore().sethero(hero)));
  149. }
  150. //throw cannotFulfillGoalException("Cannot explore - no possible ways found!");
  151. return ret;
  152. }
  153. bool Explore::fulfillsMe(TSubgoal goal)
  154. {
  155. if(goal->goalType == EXPLORE)
  156. {
  157. if(goal->hero)
  158. return hero == goal->hero;
  159. else
  160. return true; //cancel ALL exploration
  161. }
  162. return false;
  163. }