BuyArmyBehavior.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include "lib/mapping/CMap.h" //for victory conditions
  17. #include "lib/CPathfinder.h"
  18. namespace NKAI
  19. {
  20. extern boost::thread_specific_ptr<CCallback> cb;
  21. extern boost::thread_specific_ptr<AIGateway> ai;
  22. using namespace Goals;
  23. std::string BuyArmyBehavior::toString() const
  24. {
  25. return "Buy army";
  26. }
  27. Goals::TGoalVec BuyArmyBehavior::decompose() const
  28. {
  29. Goals::TGoalVec tasks;
  30. if(cb->getDate(Date::DAY) == 1)
  31. return tasks;
  32. auto heroes = cb->getHeroesInfo();
  33. if(heroes.empty())
  34. {
  35. return tasks;
  36. }
  37. for(auto town : cb->getTownsInfo())
  38. {
  39. auto townArmyAvailableToBuy = ai->nullkiller->armyManager->getArmyAvailableToBuyAsCCreatureSet(
  40. town,
  41. ai->nullkiller->getFreeResources());
  42. for(const CGHeroInstance * targetHero : heroes)
  43. {
  44. if(ai->nullkiller->buildAnalyzer->getGoldPreasure() > MAX_GOLD_PEASURE
  45. && !town->hasBuilt(BuildingID::CITY_HALL))
  46. {
  47. continue;
  48. }
  49. if(ai->nullkiller->heroManager->getHeroRole(targetHero) == HeroRole::MAIN
  50. && targetHero->getArmyStrength() >= 300)
  51. {
  52. auto reinforcement = ai->nullkiller->armyManager->howManyReinforcementsCanGet(
  53. targetHero,
  54. targetHero,
  55. &*townArmyAvailableToBuy);
  56. if(reinforcement)
  57. vstd::amin(reinforcement, ai->nullkiller->armyManager->howManyReinforcementsCanBuy(town->getUpperArmy(), town));
  58. if(reinforcement)
  59. {
  60. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(5)));
  61. }
  62. }
  63. }
  64. }
  65. return tasks;
  66. }
  67. }