GatherArmy.cpp 5.7 KB

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