BuyArmyBehavior.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. {
  51. auto reinforcement = ai->nullkiller->armyManager->howManyReinforcementsCanGet(
  52. targetHero,
  53. targetHero,
  54. &*townArmyAvailableToBuy);
  55. if(reinforcement)
  56. vstd::amin(reinforcement, ai->nullkiller->armyManager->howManyReinforcementsCanBuy(town->getUpperArmy(), town));
  57. if(reinforcement)
  58. {
  59. tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(5)));
  60. }
  61. }
  62. }
  63. }
  64. return tasks;
  65. }
  66. }