2
0

GatherTroops.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * GatherTroops.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 GatherTroops::operator==(const GatherTroops & other) const
  26. {
  27. return objid == other.objid;
  28. }
  29. int GatherTroops::getCreaturesCount(const CArmedInstance * army)
  30. {
  31. int count = 0;
  32. for(auto stack : army->Slots())
  33. {
  34. if(objid == stack.second->getCreatureID().num)
  35. {
  36. count += stack.second->count;
  37. }
  38. }
  39. return count;
  40. }
  41. TSubgoal GatherTroops::whatToDoToAchieve()
  42. {
  43. logAi->trace("Entering GatherTroops::whatToDoToAchieve");
  44. auto heroes = cb->getHeroesInfo(true);
  45. for(auto hero : heroes)
  46. {
  47. if(getCreaturesCount(hero) >= this->value)
  48. {
  49. logAi->trace("Completing GATHER_TROOPS by hero %s", hero->getNameTranslated());
  50. throw goalFulfilledException(sptr(*this));
  51. }
  52. }
  53. TGoalVec solutions = getAllPossibleSubgoals();
  54. if(solutions.empty())
  55. return sptr(Explore());
  56. return fh->chooseSolution(solutions);
  57. }
  58. TGoalVec GatherTroops::getAllPossibleSubgoals()
  59. {
  60. TGoalVec solutions;
  61. for(const CGTownInstance * t : cb->getTownsInfo())
  62. {
  63. int count = getCreaturesCount(t->getUpperArmy());
  64. if(count >= this->value)
  65. {
  66. if(t->visitingHero)
  67. {
  68. solutions.push_back(sptr(VisitObj(t->id.getNum()).sethero(t->visitingHero.get())));
  69. }
  70. else
  71. {
  72. vstd::concatenate(solutions, ai->ah->howToVisitObj(t));
  73. }
  74. continue;
  75. }
  76. auto creature = VLC->creatures()->getByIndex(objid);
  77. if(t->subID == creature->getFactionIndex()) //TODO: how to force AI to build unupgraded creatures? :O
  78. {
  79. auto creatures = vstd::tryAt(t->town->creatures, creature->getLevel() - 1);
  80. if(!creatures)
  81. continue;
  82. int upgradeNumber = vstd::find_pos(*creatures, creature->getId());
  83. if(upgradeNumber < 0)
  84. continue;
  85. BuildingID bid(BuildingID::DWELL_FIRST + creature->getLevel() - 1 + upgradeNumber * GameConstants::CREATURES_PER_TOWN);
  86. if(t->hasBuilt(bid) && ai->ah->freeResources().canAfford(creature->getFullRecruitCost())) //this assumes only creatures with dwellings are assigned to faction
  87. {
  88. solutions.push_back(sptr(BuyArmy(t, creature->getAIValue() * this->value).setobjid(objid)));
  89. }
  90. /*else //disable random building requests for now - this code needs to know a lot of town/resource context to do more good than harm
  91. {
  92. return sptr(BuildThis(bid, t).setpriority(priority));
  93. }*/
  94. }
  95. }
  96. for(auto obj : ai->visitableObjs)
  97. {
  98. auto d = dynamic_cast<const CGDwelling *>(obj);
  99. if(!d || obj->ID == Obj::TOWN)
  100. continue;
  101. for(auto creature : d->creatures)
  102. {
  103. if(creature.first) //there are more than 0 creatures avaliabe
  104. {
  105. for(auto type : creature.second)
  106. {
  107. if(type == objid && ai->ah->freeResources().canAfford(VLC->creatures()->getById(type)->getFullRecruitCost()))
  108. vstd::concatenate(solutions, ai->ah->howToVisitObj(obj));
  109. }
  110. }
  111. }
  112. }
  113. CreatureID creID = CreatureID(objid);
  114. vstd::erase_if(solutions, [&](TSubgoal goal)->bool
  115. {
  116. return goal->hero && !goal->hero->getSlotFor(creID).validSlot() && !goal->hero->getFreeSlot().validSlot();
  117. });
  118. return solutions;
  119. //TODO: exchange troops between heroes
  120. }
  121. bool GatherTroops::fulfillsMe(TSubgoal goal)
  122. {
  123. if (!hero || hero == goal->hero) //we got army for desired hero or any hero
  124. if (goal->objid == objid) //same creature type //TODO: consider upgrades?
  125. if (goal->value >= value) //notify every time we get resources?
  126. return true;
  127. return false;
  128. }