BuyArmyBehavior.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  24. {
  25. Goals::TGoalVec tasks;
  26. if(cb->getDate(Date::DAY) == 1)
  27. return tasks;
  28. auto heroes = cb->getHeroesInfo();
  29. if(heroes.empty())
  30. {
  31. return tasks;
  32. }
  33. for(auto town : cb->getTownsInfo())
  34. {
  35. auto townArmyAvailableToBuy = ai->nullkiller->armyManager->getArmyAvailableToBuyAsCCreatureSet(
  36. town,
  37. ai->nullkiller->getFreeResources());
  38. for(const CGHeroInstance * targetHero : heroes)
  39. {
  40. if(ai->nullkiller->buildAnalyzer->isGoldPreasureHigh() && !town->hasBuilt(BuildingID::CITY_HALL))
  41. {
  42. continue;
  43. }
  44. if(ai->nullkiller->heroManager->getHeroRole(targetHero) == HeroRole::MAIN)
  45. {
  46. auto reinforcement = ai->nullkiller->armyManager->howManyReinforcementsCanGet(
  47. targetHero,
  48. targetHero,
  49. &*townArmyAvailableToBuy);
  50. if(reinforcement)
  51. vstd::amin(reinforcement, ai->nullkiller->armyManager->howManyReinforcementsCanBuy(town->getUpperArmy(), town));
  52. if(reinforcement)
  53. {
  54. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(5)));
  55. }
  56. }
  57. }
  58. }
  59. return tasks;
  60. }
  61. }