2
0

RecruitHero.cpp 1.5 KB

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