RecruitHeroBehavior.cpp 1.9 KB

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