RecruitHero.cpp 1.6 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/constants/StringConstants.h"
  15. namespace NKAI
  16. {
  17. using namespace Goals;
  18. std::string RecruitHero::toString() const
  19. {
  20. if(heroToBuy)
  21. return "Recruit " + heroToBuy->getNameTranslated() + " at " + town->getNameTranslated();
  22. else
  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 = heroToBuy;
  39. if(!heroToHire)
  40. {
  41. for(auto hero : heroes)
  42. {
  43. if(!heroToHire || hero->getTotalStrength() > heroToHire->getTotalStrength())
  44. heroToHire = hero;
  45. }
  46. }
  47. if(!heroToHire)
  48. throw cannotFulfillGoalException("No hero to hire!");
  49. if(t->getVisitingHero())
  50. {
  51. cb->swapGarrisonHero(t);
  52. }
  53. if(t->getVisitingHero())
  54. throw cannotFulfillGoalException("Town " + t->nodeName() + " is occupied. Cannot recruit hero!");
  55. cb->recruitHero(t, heroToHire);
  56. {
  57. std::unique_lock lockGuard(ai->nullkiller->aiStateMutex);
  58. ai->nullkiller->heroManager->update();
  59. ai->nullkiller->objectClusterizer->reset();
  60. }
  61. }
  62. }