RecruitHero.cpp 1.6 KB

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