ExchangeSwapTownHeroes.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "../Engine/Nullkiller.h"
  15. namespace NKAI
  16. {
  17. using namespace Goals;
  18. ExchangeSwapTownHeroes::ExchangeSwapTownHeroes(
  19. const CGTownInstance * town,
  20. const CGHeroInstance * garrisonHero,
  21. HeroLockedReason lockingReason)
  22. :ElementarGoal(Goals::EXCHANGE_SWAP_TOWN_HEROES), town(town), garrisonHero(garrisonHero), lockingReason(lockingReason)
  23. {
  24. }
  25. std::vector<ObjectInstanceID> ExchangeSwapTownHeroes::getAffectedObjects() const
  26. {
  27. std::vector<ObjectInstanceID> affectedObjects = { town->id };
  28. if(town->getGarrisonHero())
  29. affectedObjects.push_back(town->getGarrisonHero()->id);
  30. if(town->getVisitingHero())
  31. affectedObjects.push_back(town->getVisitingHero()->id);
  32. return affectedObjects;
  33. }
  34. bool ExchangeSwapTownHeroes::isObjectAffected(ObjectInstanceID id) const
  35. {
  36. return town->id == id
  37. || (town->getVisitingHero() && town->getVisitingHero()->id == id)
  38. || (town->getGarrisonHero() && town->getGarrisonHero()->id == id);
  39. }
  40. std::string ExchangeSwapTownHeroes::toString() const
  41. {
  42. return "Exchange and swap heroes of " + town->getNameTranslated();
  43. }
  44. bool ExchangeSwapTownHeroes::operator==(const ExchangeSwapTownHeroes & other) const
  45. {
  46. return town == other.town;
  47. }
  48. void ExchangeSwapTownHeroes::accept(AIGateway * ai)
  49. {
  50. if(!getGarrisonHero())
  51. {
  52. auto currentGarrisonHero = town->getGarrisonHero();
  53. if(!currentGarrisonHero)
  54. throw cannotFulfillGoalException("Invalid configuration. There is no hero in town garrison.");
  55. cb->swapGarrisonHero(town);
  56. if(currentGarrisonHero != town->getVisitingHero())
  57. {
  58. logAi->error("VisitingHero is empty, expected %s", currentGarrisonHero->getNameTranslated());
  59. return;
  60. }
  61. ai->buildArmyIn(town);
  62. ai->nullkiller->unlockHero(currentGarrisonHero);
  63. logAi->debug("Extracted hero %s from garrison of %s", currentGarrisonHero->getNameTranslated(), town->getNameTranslated());
  64. return;
  65. }
  66. if(town->getVisitingHero() && town->getVisitingHero() != getGarrisonHero())
  67. cb->swapGarrisonHero(town);
  68. ai->makePossibleUpgrades(town);
  69. ai->moveHeroToTile(town->visitablePos(), getGarrisonHero());
  70. auto upperArmy = town->getUpperArmy();
  71. if(!town->getGarrisonHero())
  72. {
  73. if (!getGarrisonHero()->canBeMergedWith(*town))
  74. {
  75. while (upperArmy->stacksCount() != 0)
  76. {
  77. cb->dismissCreature(upperArmy, upperArmy->Slots().begin()->first);
  78. }
  79. }
  80. }
  81. cb->swapGarrisonHero(town);
  82. if(lockingReason != HeroLockedReason::NOT_LOCKED)
  83. {
  84. ai->nullkiller->lockHero(getGarrisonHero(), lockingReason);
  85. }
  86. if(town->getVisitingHero() && town->getVisitingHero() != getGarrisonHero())
  87. {
  88. ai->nullkiller->unlockHero(town->getVisitingHero());
  89. ai->makePossibleUpgrades(town->getVisitingHero());
  90. }
  91. logAi->debug("Put hero %s to garrison of %s", getGarrisonHero()->getNameTranslated(), town->getNameTranslated());
  92. }
  93. }