GatherTroops.cpp 4.2 KB

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