BuyArmyBehavior.cpp 1.7 KB

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