BuyArmyBehavior.cpp 1.7 KB

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