ExecuteHeroChain.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * ExecuteHeroChain.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 "ExecuteHeroChain.h"
  12. #include "../AIGateway.h"
  13. #include "../Engine/Nullkiller.h"
  14. namespace NKAI
  15. {
  16. using namespace Goals;
  17. ExecuteHeroChain::ExecuteHeroChain(const AIPath & path, const CGObjectInstance * obj)
  18. :ElementarGoal(Goals::EXECUTE_HERO_CHAIN), chainPath(path), closestWayRatio(1)
  19. {
  20. hero = path.targetHero;
  21. tile = path.targetTile();
  22. if(obj)
  23. {
  24. objid = obj->id.getNum();
  25. targetName = obj->getObjectName() + tile.toString();
  26. }
  27. else
  28. {
  29. targetName = "tile" + tile.toString();
  30. }
  31. }
  32. bool ExecuteHeroChain::operator==(const ExecuteHeroChain & other) const
  33. {
  34. return tile == other.tile
  35. && chainPath.targetHero == other.chainPath.targetHero
  36. && chainPath.nodes.size() == other.chainPath.nodes.size()
  37. && chainPath.chainMask == other.chainPath.chainMask;
  38. }
  39. void ExecuteHeroChain::accept(AIGateway * ai)
  40. {
  41. logAi->debug("Executing hero chain towards %s. Path %s", targetName, chainPath.toString());
  42. ai->nullkiller->setActive(chainPath.targetHero, tile);
  43. ai->nullkiller->setTargetObject(objid);
  44. auto targetObject = ai->myCb->getObj(static_cast<ObjectInstanceID>(objid), false);
  45. if(chainPath.turn() == 0 && targetObject && targetObject->ID == Obj::TOWN)
  46. {
  47. auto relations = ai->myCb->getPlayerRelations(ai->playerID, targetObject->getOwner());
  48. if(relations == PlayerRelations::ENEMIES)
  49. {
  50. ai->nullkiller->armyFormation->rearrangeArmyForSiege(
  51. dynamic_cast<const CGTownInstance *>(targetObject),
  52. chainPath.targetHero);
  53. }
  54. }
  55. std::set<int> blockedIndexes;
  56. for(int i = chainPath.nodes.size() - 1; i >= 0; i--)
  57. {
  58. auto & node = chainPath.nodes[i];
  59. const CGHeroInstance * hero = node.targetHero;
  60. HeroPtr heroPtr = hero;
  61. if(node.parentIndex >= i)
  62. {
  63. logAi->error("Invalid parentIndex while executing node " + node.coord.toString());
  64. }
  65. if(vstd::contains(blockedIndexes, i))
  66. {
  67. blockedIndexes.insert(node.parentIndex);
  68. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  69. continue;
  70. }
  71. logAi->debug("Executing chain node %d. Moving hero %s to %s", i, hero->getNameTranslated(), node.coord.toString());
  72. try
  73. {
  74. if(hero->movementPointsRemaining() > 0)
  75. {
  76. ai->nullkiller->setActive(hero, node.coord);
  77. if(node.specialAction)
  78. {
  79. if(node.actionIsBlocked)
  80. {
  81. throw cannotFulfillGoalException("Path is nondeterministic.");
  82. }
  83. node.specialAction->execute(hero);
  84. if(!heroPtr.validAndSet())
  85. {
  86. logAi->error("Hero %s was lost trying to execute special action. Exit hero chain.", heroPtr.name);
  87. return;
  88. }
  89. }
  90. if(node.turns == 0 && node.coord != hero->visitablePos())
  91. {
  92. auto targetNode = cb->getPathsInfo(hero)->getPathInfo(node.coord);
  93. if(targetNode->accessible == EPathAccessibility::NOT_SET
  94. || targetNode->accessible == EPathAccessibility::BLOCKED
  95. || targetNode->accessible == EPathAccessibility::FLYABLE
  96. || targetNode->turns != 0)
  97. {
  98. logAi->error(
  99. "Unable to complete chain. Expected hero %s to arrive to %s in 0 turns but he cannot do this",
  100. hero->getNameTranslated(),
  101. node.coord.toString());
  102. return;
  103. }
  104. }
  105. if(hero->movementPointsRemaining())
  106. {
  107. try
  108. {
  109. if(moveHeroToTile(hero, node.coord))
  110. {
  111. continue;
  112. }
  113. }
  114. catch(const cannotFulfillGoalException &)
  115. {
  116. if(!heroPtr.validAndSet())
  117. {
  118. logAi->error("Hero %s was lost. Exit hero chain.", heroPtr.name);
  119. return;
  120. }
  121. if(hero->movementPointsRemaining() > 0)
  122. {
  123. CGPath path;
  124. bool isOk = cb->getPathsInfo(hero)->getPath(path, node.coord);
  125. if(isOk && path.nodes.back().turns > 0)
  126. {
  127. logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero->getNameTranslated(), hero->movementPointsRemaining(), node.coord.toString());
  128. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  129. return;
  130. }
  131. }
  132. throw;
  133. }
  134. }
  135. }
  136. if(node.coord == hero->visitablePos())
  137. continue;
  138. if(node.turns == 0)
  139. {
  140. logAi->error(
  141. "Unable to complete chain. Expected hero %s to arive to %s but he is at %s",
  142. hero->getNameTranslated(),
  143. node.coord.toString(),
  144. hero->visitablePos().toString());
  145. return;
  146. }
  147. // no exception means we were not able to reach the tile
  148. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  149. blockedIndexes.insert(node.parentIndex);
  150. }
  151. catch(const goalFulfilledException &)
  152. {
  153. if(!heroPtr.validAndSet())
  154. {
  155. logAi->debug("Hero %s was killed while attempting to reach %s", heroPtr.name, node.coord.toString());
  156. return;
  157. }
  158. }
  159. }
  160. }
  161. std::string ExecuteHeroChain::toString() const
  162. {
  163. return "ExecuteHeroChain " + targetName + " by " + chainPath.targetHero->getNameTranslated();
  164. }
  165. bool ExecuteHeroChain::moveHeroToTile(const CGHeroInstance * hero, const int3 & tile)
  166. {
  167. if(tile == hero->visitablePos() && cb->getVisitableObjs(hero->visitablePos()).size() < 2)
  168. {
  169. logAi->warn("Why do I want to move hero %s to tile %s? Already standing on that tile! ", hero->getNameTranslated(), tile.toString());
  170. return true;
  171. }
  172. return ai->moveHeroToTile(tile, hero);
  173. }
  174. }