GatherArmy.cpp 5.7 KB

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