BuyArmyBehavior.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 NK2AI
  17. {
  18. using namespace Goals;
  19. std::string BuyArmyBehavior::toString() const
  20. {
  21. return "Buy army";
  22. }
  23. Goals::TGoalVec BuyArmyBehavior::decompose(const Nullkiller * aiNk) const
  24. {
  25. Goals::TGoalVec tasks;
  26. const auto heroes = aiNk->cc->getHeroesInfo();
  27. if(heroes.empty())
  28. return tasks;
  29. // Simplification: Moved this call before getting into the decomposer
  30. // aiNk->dangerHitMap->updateHitMap();
  31. for(const auto town : aiNk->cc->getTownsInfo())
  32. {
  33. uint8_t closestThreat = aiNk->dangerHitMap->getTileThreat(town->visitablePos()).fastestDanger.turn;
  34. if(closestThreat >= 2 && aiNk->buildAnalyzer->isGoldPressureOverMax() && !town->hasBuilt(BuildingID::CITY_HALL) &&
  35. aiNk->cc->canBuildStructure(town, BuildingID::CITY_HALL) != EBuildingState::FORBIDDEN)
  36. {
  37. return tasks;
  38. }
  39. auto townArmyAvailableToBuy = aiNk->armyManager->getArmyAvailableToBuyAsCCreatureSet(town, aiNk->getFreeResources());
  40. for(const CGHeroInstance * targetHero : heroes)
  41. {
  42. if(aiNk->heroManager->getHeroRoleOrDefaultInefficient(targetHero) == HeroRole::MAIN)
  43. {
  44. auto reinforcement = aiNk->armyManager->howManyReinforcementsCanGet(
  45. targetHero,
  46. targetHero,
  47. &*townArmyAvailableToBuy,
  48. TerrainId::NONE);
  49. // TODO: Mircea: Shouldn't matter if hero is MAIN when buying reinforcements if there's a threat around
  50. // Evaluate the entire code with the outside towns loop too.
  51. if(reinforcement)
  52. vstd::amin(reinforcement, aiNk->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. }