ExchangeSwapTownHeroes.cpp 2.5 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 "../VCAI.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<VCAI> 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(VCAI * ai)
  36. {
  37. if(!garrisonHero)
  38. {
  39. if(!town->garrisonHero)
  40. throw cannotFulfillGoalException("Invalid configuration. There is no hero in town garrison.");
  41. cb->swapGarrisonHero(town);
  42. ai->buildArmyIn(town);
  43. ai->nullkiller->unlockHero(town->visitingHero.get());
  44. logAi->debug("Extracted hero %s from garrison of %s", town->visitingHero->name, town->name);
  45. return;
  46. }
  47. if(town->visitingHero && town->visitingHero.get() != garrisonHero)
  48. cb->swapGarrisonHero(town);
  49. ai->makePossibleUpgrades(town);
  50. ai->moveHeroToTile(town->visitablePos(), garrisonHero);
  51. auto upperArmy = town->getUpperArmy();
  52. if(!town->garrisonHero && upperArmy->stacksCount() != 0)
  53. {
  54. // dismiss creatures we are not able to pick to be able to hide in garrison
  55. if(upperArmy->getArmyStrength() < 500
  56. && town->fortLevel() >= CGTownInstance::CITADEL)
  57. {
  58. for(auto slot : upperArmy->Slots())
  59. {
  60. cb->dismissCreature(upperArmy, slot.first);
  61. }
  62. cb->swapGarrisonHero(town);
  63. }
  64. }
  65. else
  66. {
  67. cb->swapGarrisonHero(town); // selected hero left in garrison with strongest army
  68. }
  69. ai->nullkiller->lockHero(garrisonHero, lockingReason);
  70. if(town->visitingHero && town->visitingHero != garrisonHero)
  71. {
  72. ai->nullkiller->unlockHero(town->visitingHero.get());
  73. ai->makePossibleUpgrades(town->visitingHero);
  74. }
  75. logAi->debug("Put hero %s to garrison of %s", garrisonHero->name, town->name);
  76. }