ExchangeSwapTownHeroes.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * ExchangeSwapTownHeroes.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 "ExchangeSwapTownHeroes.h"
  12. #include "ExecuteHeroChain.h"
  13. #include "../AIGateway.h"
  14. #include "../../../lib/mapping/CMap.h" //for victory conditions
  15. #include "../../../lib/CPathfinder.h"
  16. #include "../Engine/Nullkiller.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. ExchangeSwapTownHeroes::ExchangeSwapTownHeroes(
  23. const CGTownInstance * town,
  24. const CGHeroInstance * garrisonHero,
  25. HeroLockedReason lockingReason)
  26. :ElementarGoal(Goals::EXCHANGE_SWAP_TOWN_HEROES), town(town), garrisonHero(garrisonHero), lockingReason(lockingReason)
  27. {
  28. }
  29. std::string ExchangeSwapTownHeroes::toString() const
  30. {
  31. return "Exchange and swap heroes of " + town->name;
  32. }
  33. bool ExchangeSwapTownHeroes::operator==(const ExchangeSwapTownHeroes & other) const
  34. {
  35. return town == other.town;
  36. }
  37. void ExchangeSwapTownHeroes::accept(AIGateway * ai)
  38. {
  39. if(!garrisonHero)
  40. {
  41. auto currentGarrisonHero = town->garrisonHero;
  42. if(!currentGarrisonHero)
  43. throw cannotFulfillGoalException("Invalid configuration. There is no hero in town garrison.");
  44. cb->swapGarrisonHero(town);
  45. if(currentGarrisonHero.get() != town->visitingHero.get())
  46. {
  47. logAi->error("VisitingHero is empty, expected %s", currentGarrisonHero->name);
  48. return;
  49. }
  50. ai->buildArmyIn(town);
  51. ai->nullkiller->unlockHero(currentGarrisonHero.get());
  52. logAi->debug("Extracted hero %s from garrison of %s", currentGarrisonHero->name, town->name);
  53. return;
  54. }
  55. if(town->visitingHero && town->visitingHero.get() != garrisonHero)
  56. cb->swapGarrisonHero(town);
  57. ai->makePossibleUpgrades(town);
  58. ai->moveHeroToTile(town->visitablePos(), garrisonHero);
  59. auto upperArmy = town->getUpperArmy();
  60. if(!town->garrisonHero)
  61. {
  62. while(upperArmy->stacksCount() != 0)
  63. {
  64. cb->dismissCreature(upperArmy, upperArmy->Slots().begin()->first);
  65. }
  66. }
  67. cb->swapGarrisonHero(town);
  68. ai->nullkiller->lockHero(garrisonHero, lockingReason);
  69. if(town->visitingHero && town->visitingHero != garrisonHero)
  70. {
  71. ai->nullkiller->unlockHero(town->visitingHero.get());
  72. ai->makePossibleUpgrades(town->visitingHero);
  73. }
  74. logAi->debug("Put hero %s to garrison of %s", garrisonHero->name, town->name);
  75. }
  76. }