ExchangeSwapTownHeroes.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. extern boost::thread_specific_ptr<CCallback> cb;
  18. extern boost::thread_specific_ptr<AIGateway> ai;
  19. using namespace Goals;
  20. ExchangeSwapTownHeroes::ExchangeSwapTownHeroes(
  21. const CGTownInstance * town,
  22. const CGHeroInstance * garrisonHero,
  23. HeroLockedReason lockingReason)
  24. :ElementarGoal(Goals::EXCHANGE_SWAP_TOWN_HEROES), town(town), garrisonHero(garrisonHero), lockingReason(lockingReason)
  25. {
  26. }
  27. std::string ExchangeSwapTownHeroes::toString() const
  28. {
  29. return "Exchange and swap heroes of " + town->name;
  30. }
  31. bool ExchangeSwapTownHeroes::operator==(const ExchangeSwapTownHeroes & other) const
  32. {
  33. return town == other.town;
  34. }
  35. void ExchangeSwapTownHeroes::accept(AIGateway * ai)
  36. {
  37. if(!garrisonHero)
  38. {
  39. auto currentGarrisonHero = town->garrisonHero;
  40. if(!currentGarrisonHero)
  41. throw cannotFulfillGoalException("Invalid configuration. There is no hero in town garrison.");
  42. cb->swapGarrisonHero(town);
  43. if(currentGarrisonHero.get() != town->visitingHero.get())
  44. {
  45. logAi->error("VisitingHero is empty, expected %s", currentGarrisonHero->name);
  46. return;
  47. }
  48. ai->buildArmyIn(town);
  49. ai->nullkiller->unlockHero(currentGarrisonHero.get());
  50. logAi->debug("Extracted hero %s from garrison of %s", currentGarrisonHero->name, town->name);
  51. return;
  52. }
  53. if(town->visitingHero && town->visitingHero.get() != garrisonHero)
  54. cb->swapGarrisonHero(town);
  55. ai->makePossibleUpgrades(town);
  56. ai->moveHeroToTile(town->visitablePos(), garrisonHero);
  57. auto upperArmy = town->getUpperArmy();
  58. if(!town->garrisonHero)
  59. {
  60. while(upperArmy->stacksCount() != 0)
  61. {
  62. cb->dismissCreature(upperArmy, upperArmy->Slots().begin()->first);
  63. }
  64. }
  65. cb->swapGarrisonHero(town);
  66. ai->nullkiller->lockHero(garrisonHero, lockingReason);
  67. if(town->visitingHero && town->visitingHero != garrisonHero)
  68. {
  69. ai->nullkiller->unlockHero(town->visitingHero.get());
  70. ai->makePossibleUpgrades(town->visitingHero);
  71. }
  72. logAi->debug("Put hero %s to garrison of %s", garrisonHero->name, town->name);
  73. }