GatherArmy.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * GatherArmy.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 GatherArmy::operator==(const GatherArmy & other) const
  26. {
  27. return other.hero.h == hero.h || town == other.town;
  28. }
  29. std::string GatherArmy::completeMessage() const
  30. {
  31. return "Hero " + hero.get()->name + " gathered army of value " + boost::lexical_cast<std::string>(value);
  32. }
  33. TSubgoal GatherArmy::whatToDoToAchieve()
  34. {
  35. //TODO: find hero if none set
  36. assert(hero.h);
  37. return fh->chooseSolution(getAllPossibleSubgoals()); //find dwelling. use current hero to prevent him from doing nothing.
  38. }
  39. TGoalVec GatherArmy::getAllPossibleSubgoals()
  40. {
  41. //get all possible towns, heroes and dwellings we may use
  42. TGoalVec ret;
  43. if(!hero.validAndSet())
  44. {
  45. return ret;
  46. }
  47. //TODO: include evaluation of monsters gather in calculation
  48. for(auto t : cb->getTownsInfo())
  49. {
  50. auto waysToVisit = ai->ah->howToVisitObj(hero, t);
  51. if(waysToVisit.size())
  52. {
  53. //grab army from town
  54. if(!t->visitingHero && ai->ah->howManyReinforcementsCanGet(hero.get(), t))
  55. {
  56. if(!vstd::contains(ai->townVisitsThisWeek[hero], t))
  57. vstd::concatenate(ret, waysToVisit);
  58. }
  59. //buy army in town
  60. if (!t->visitingHero || t->visitingHero == hero.get(true))
  61. {
  62. std::vector<int> values = {
  63. value,
  64. (int)ai->ah->howManyReinforcementsCanBuy(t->getUpperArmy(), t),
  65. (int)ai->ah->howManyReinforcementsCanBuy(hero.get(), t) };
  66. int val = *std::min_element(values.begin(), values.end());
  67. if (val)
  68. {
  69. auto goal = sptr(BuyArmy(t, val).sethero(hero));
  70. if(!ai->ah->containsObjective(goal)) //avoid loops caused by reserving same objective twice
  71. ret.push_back(goal);
  72. else
  73. logAi->debug("Can not buy army, because of ai->ah->containsObjective");
  74. }
  75. }
  76. //build dwelling
  77. //TODO: plan building over multiple turns?
  78. //auto bid = ah->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 8 - cb->getDate(Date::DAY_OF_WEEK));
  79. //Do not use below code for now, rely on generic Build. Code below needs to know a lot of town/resource context to do more good than harm
  80. /*auto bid = ai->ah->canBuildAnyStructure(t, std::vector<BuildingID>(unitsSource, unitsSource + ARRAY_COUNT(unitsSource)), 1);
  81. if (bid.is_initialized())
  82. {
  83. auto goal = sptr(BuildThis(bid.get(), t).setpriority(priority));
  84. if (!ai->ah->containsObjective(goal)) //avoid loops caused by reserving same objective twice
  85. ret.push_back(goal);
  86. else
  87. logAi->debug("Can not build a structure, because of ai->ah->containsObjective");
  88. }*/
  89. }
  90. }
  91. auto otherHeroes = cb->getHeroesInfo();
  92. auto heroDummy = hero;
  93. vstd::erase_if(otherHeroes, [heroDummy](const CGHeroInstance * h)
  94. {
  95. if(h == heroDummy.h)
  96. return true;
  97. else if(!ai->isAccessibleForHero(heroDummy->visitablePos(), h, true))
  98. return true;
  99. else if(!ai->ah->canGetArmy(heroDummy.h, h)) //TODO: return actual aiValue
  100. return true;
  101. else if(ai->getGoal(h)->goalType == GATHER_ARMY)
  102. return true;
  103. else
  104. return false;
  105. });
  106. for(auto h : otherHeroes)
  107. {
  108. // Go to the other hero if we are faster
  109. if(!vstd::contains(ai->visitedHeroes[hero], h))
  110. {
  111. vstd::concatenate(ret, ai->ah->howToVisitObj(hero, h, false));
  112. }
  113. // Go to the other hero if we are faster
  114. if(!vstd::contains(ai->visitedHeroes[h], hero))
  115. {
  116. vstd::concatenate(ret, ai->ah->howToVisitObj(h, hero.get(), false));
  117. }
  118. }
  119. std::vector<const CGObjectInstance *> objs;
  120. for(auto obj : ai->visitableObjs)
  121. {
  122. if(obj->ID == Obj::CREATURE_GENERATOR1)
  123. {
  124. auto relationToOwner = cb->getPlayerRelations(obj->getOwner(), ai->playerID);
  125. //Use flagged dwellings only when there are available creatures that we can afford
  126. if(relationToOwner == PlayerRelations::SAME_PLAYER)
  127. {
  128. auto dwelling = dynamic_cast<const CGDwelling *>(obj);
  129. ui32 val = std::min((ui32)value, (ui32)ai->ah->howManyReinforcementsCanBuy(hero.get(), dwelling));
  130. if(val)
  131. {
  132. for(auto & creLevel : dwelling->creatures)
  133. {
  134. if(creLevel.first)
  135. {
  136. for(auto & creatureID : creLevel.second)
  137. {
  138. auto creature = VLC->creh->objects[creatureID];
  139. if(ai->ah->freeResources().canAfford(creature->cost))
  140. objs.push_back(obj); //TODO: reserve resources?
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. for(auto h : cb->getHeroesInfo())
  149. {
  150. for(auto obj : objs)
  151. {
  152. //find safe dwelling
  153. if(ai->isGoodForVisit(obj, h))
  154. {
  155. vstd::concatenate(ret, ai->ah->howToVisitObj(h, obj));
  156. }
  157. }
  158. }
  159. if(ai->canRecruitAnyHero() && ai->ah->freeGold() > GameConstants::HERO_GOLD_COST) //this is not stupid in early phase of game
  160. {
  161. if(auto t = ai->findTownWithTavern())
  162. {
  163. for(auto h : cb->getAvailableHeroes(t)) //we assume that all towns have same set of heroes
  164. {
  165. if(h && h->getTotalStrength() > 500) //do not buy heroes with single creatures for GatherArmy
  166. {
  167. ret.push_back(sptr(RecruitHero()));
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. if(ret.empty())
  174. {
  175. const bool allowGatherArmy = false;
  176. if(hero == ai->primaryHero())
  177. ret.push_back(sptr(Explore(allowGatherArmy)));
  178. else
  179. throw cannotFulfillGoalException("No ways to gather army");
  180. }
  181. return ret;
  182. }