RecruitHero.cpp 1.6 KB

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