GatherTroops.cpp 4.1 KB

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