BuyArmyBehavior.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * BuyArmyBehavior.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 "BuyArmyBehavior.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/BuyArmy.h"
  15. #include "../Engine/Nullkiller.h"
  16. namespace NKAI
  17. {
  18. using namespace Goals;
  19. std::string BuyArmyBehavior::toString() const
  20. {
  21. return "Buy army";
  22. }
  23. Goals::TGoalVec BuyArmyBehavior::decompose(const Nullkiller * ai) const
  24. {
  25. Goals::TGoalVec tasks;
  26. auto heroes = cb->getHeroesInfo();
  27. if(heroes.empty())
  28. {
  29. return tasks;
  30. }
  31. for(auto town : cb->getTownsInfo())
  32. {
  33. //If we can recruit a hero that comes with more army than he costs, we are better off spending our gold on them
  34. if (ai->heroManager->canRecruitHero(town))
  35. {
  36. auto availableHeroes = ai->cb->getAvailableHeroes(town);
  37. for (auto hero : availableHeroes)
  38. {
  39. if (hero->getArmyCost() > GameConstants::HERO_GOLD_COST)
  40. return tasks;
  41. }
  42. }
  43. if (ai->buildAnalyzer->isGoldPressureHigh() && !town->hasBuilt(BuildingID::CITY_HALL))
  44. {
  45. return tasks;
  46. }
  47. auto townArmyAvailableToBuy = ai->armyManager->getArmyAvailableToBuyAsCCreatureSet(
  48. town,
  49. ai->getFreeResources());
  50. for(const CGHeroInstance * targetHero : heroes)
  51. {
  52. if(ai->heroManager->getHeroRole(targetHero) == HeroRole::MAIN)
  53. {
  54. auto reinforcement = ai->armyManager->howManyReinforcementsCanGet(
  55. targetHero,
  56. targetHero,
  57. &*townArmyAvailableToBuy);
  58. if(reinforcement)
  59. vstd::amin(reinforcement, ai->armyManager->howManyReinforcementsCanBuy(town->getUpperArmy(), town));
  60. if(reinforcement)
  61. {
  62. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(5)));
  63. }
  64. }
  65. }
  66. }
  67. return tasks;
  68. }
  69. }