RecruitHeroBehavior.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * RecruitHeroBehavior.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 "RecruitHeroBehavior.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/RecruitHero.h"
  15. #include "../Goals/ExecuteHeroChain.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 RecruitHeroBehavior::toString() const
  23. {
  24. return "Recruit hero";
  25. }
  26. Goals::TGoalVec RecruitHeroBehavior::decompose() const
  27. {
  28. Goals::TGoalVec tasks;
  29. auto towns = cb->getTownsInfo();
  30. auto ourHeroes = ai->nullkiller->heroManager->getHeroRoles();
  31. auto minScoreToHireMain = std::numeric_limits<float>::max();
  32. for(auto hero : ourHeroes)
  33. {
  34. if(hero.second != HeroRole::MAIN)
  35. continue;
  36. auto newScore = ai->nullkiller->heroManager->evaluateHero(hero.first.get());
  37. if(minScoreToHireMain > newScore)
  38. {
  39. // weakest main hero score
  40. minScoreToHireMain = newScore;
  41. }
  42. }
  43. for(auto town : towns)
  44. {
  45. if(ai->nullkiller->heroManager->canRecruitHero(town))
  46. {
  47. auto availableHeroes = cb->getAvailableHeroes(town);
  48. for(auto hero : availableHeroes)
  49. {
  50. auto score = ai->nullkiller->heroManager->evaluateHero(hero);
  51. if(score > minScoreToHireMain)
  52. {
  53. tasks.push_back(Goals::sptr(Goals::RecruitHero(town, hero).setpriority(200)));
  54. break;
  55. }
  56. }
  57. if(cb->getHeroesInfo().size() < cb->getTownsInfo().size() + 1
  58. || (ai->nullkiller->getFreeResources()[EGameResID::GOLD] > 10000
  59. && ai->nullkiller->buildAnalyzer->getGoldPreasure() < MAX_GOLD_PEASURE))
  60. {
  61. tasks.push_back(Goals::sptr(Goals::RecruitHero(town).setpriority(3)));
  62. }
  63. }
  64. }
  65. return tasks;
  66. }
  67. }