RecruitHero.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * RecruitHero.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 "Goals.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../../../lib/mapping/CMap.h" //for victory conditions
  15. #include "../../../lib/CPathfinder.h"
  16. #include "../../../lib/StringConstants.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 RecruitHero::toString() const
  23. {
  24. return "Recruit hero at " + town->name;
  25. }
  26. void RecruitHero::accept(AIGateway * ai)
  27. {
  28. auto t = town;
  29. if(!t) t = ai->findTownWithTavern();
  30. if(!t)
  31. {
  32. throw cannotFulfillGoalException("No town to recruit hero!");
  33. }
  34. logAi->debug("Trying to recruit a hero in %s at %s", t->name, t->visitablePos().toString());
  35. auto heroes = cb->getAvailableHeroes(t);
  36. if(!heroes.size())
  37. {
  38. throw cannotFulfillGoalException("No available heroes in tavern in " + t->nodeName());
  39. }
  40. auto heroToHire = heroes[0];
  41. for(auto hero : heroes)
  42. {
  43. if(objid == hero->id.getNum())
  44. {
  45. heroToHire = hero;
  46. break;
  47. }
  48. if(hero->getTotalStrength() > heroToHire->getTotalStrength())
  49. heroToHire = hero;
  50. }
  51. if(t->visitingHero)
  52. {
  53. cb->swapGarrisonHero(t);
  54. }
  55. if(t->visitingHero)
  56. throw cannotFulfillGoalException("Town " + t->nodeName() + " is occupied. Cannot recruit hero!");
  57. cb->recruitHero(t, heroToHire);
  58. ai->nullkiller->heroManager->update();
  59. if(t->visitingHero)
  60. ai->moveHeroToTile(t->visitablePos(), t->visitingHero.get());
  61. }
  62. }