RecruitHero.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if(heroToBuy)
  23. return "Recruit " + heroToBuy->getNameTranslated() + " at " + town->getNameTranslated();
  24. else
  25. return "Recruit hero at " + town->getNameTranslated();
  26. }
  27. void RecruitHero::accept(AIGateway * ai)
  28. {
  29. auto t = town;
  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->getNameTranslated(), 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 = heroToBuy;
  41. if(!heroToHire)
  42. {
  43. for(auto hero : heroes)
  44. {
  45. if(!heroToHire || hero->getTotalStrength() > heroToHire->getTotalStrength())
  46. heroToHire = hero;
  47. }
  48. }
  49. if(!heroToHire)
  50. throw cannotFulfillGoalException("No hero to hire!");
  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. }