BuyArmyBehavior.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. ai->dangerHitMap->updateHitMap();
  32. for(auto town : cb->getTownsInfo())
  33. {
  34. uint8_t closestThreat = ai->dangerHitMap->getTileThreat(town->visitablePos()).fastestDanger.turn;
  35. if (closestThreat >=2 && ai->buildAnalyzer->isGoldPressureHigh() && !town->hasBuilt(BuildingID::CITY_HALL) && cb->canBuildStructure(town, BuildingID::CITY_HALL) != EBuildingState::FORBIDDEN)
  36. {
  37. return tasks;
  38. }
  39. auto townArmyAvailableToBuy = ai->armyManager->getArmyAvailableToBuyAsCCreatureSet(
  40. town,
  41. ai->getFreeResources());
  42. for(const CGHeroInstance * targetHero : heroes)
  43. {
  44. if(ai->heroManager->getHeroRole(targetHero) == HeroRole::MAIN)
  45. {
  46. auto reinforcement = ai->armyManager->howManyReinforcementsCanGet(
  47. targetHero,
  48. targetHero,
  49. &*townArmyAvailableToBuy,
  50. TerrainId::NONE);
  51. if(reinforcement)
  52. vstd::amin(reinforcement, ai->armyManager->howManyReinforcementsCanBuy(town->getUpperArmy(), town));
  53. if(reinforcement)
  54. {
  55. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(reinforcement)));
  56. }
  57. }
  58. }
  59. }
  60. return tasks;
  61. }
  62. }