ExecuteHeroChain.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. else if(i > 0 && ai->nullkiller->settings->isObjectGraphAllowed())
  91. {
  92. auto chainMask = i < chainPath.nodes.size() - 1 ? chainPath.nodes[i + 1].chainMask : node->chainMask;
  93. for(auto j = i - 1; j >= 0; j--)
  94. {
  95. auto & nextNode = chainPath.nodes[j];
  96. if(nextNode.specialAction || nextNode.chainMask != chainMask)
  97. break;
  98. auto targetNode = cb->getPathsInfo(hero)->getPathInfo(nextNode.coord);
  99. if(!targetNode->reachable()
  100. || targetNode->getCost() > nextNode.cost)
  101. break;
  102. i = j;
  103. node = &nextNode;
  104. if(targetNode->action == EPathNodeAction::BATTLE || targetNode->action == EPathNodeAction::TELEPORT_BATTLE)
  105. break;
  106. }
  107. }
  108. if(node->turns == 0 && node->coord != hero->visitablePos())
  109. {
  110. auto targetNode = cb->getPathsInfo(hero)->getPathInfo(node->coord);
  111. if(targetNode->accessible == EPathAccessibility::NOT_SET
  112. || targetNode->accessible == EPathAccessibility::BLOCKED
  113. || targetNode->accessible == EPathAccessibility::FLYABLE
  114. || targetNode->turns != 0)
  115. {
  116. logAi->error(
  117. "Unable to complete chain. Expected hero %s to arrive to %s in 0 turns but he cannot do this",
  118. hero->getNameTranslated(),
  119. node->coord.toString());
  120. return;
  121. }
  122. }
  123. if(hero->movementPointsRemaining())
  124. {
  125. try
  126. {
  127. if(moveHeroToTile(hero, node->coord))
  128. {
  129. continue;
  130. }
  131. }
  132. catch(const cannotFulfillGoalException &)
  133. {
  134. if(!heroPtr.validAndSet())
  135. {
  136. logAi->error("Hero %s was lost. Exit hero chain.", heroPtr.name);
  137. return;
  138. }
  139. if(hero->movementPointsRemaining() > 0)
  140. {
  141. CGPath path;
  142. bool isOk = cb->getPathsInfo(hero)->getPath(path, node->coord);
  143. if(isOk && path.nodes.back().turns > 0)
  144. {
  145. logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero->getNameTranslated(), hero->movementPointsRemaining(), node->coord.toString());
  146. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  147. return;
  148. }
  149. }
  150. throw;
  151. }
  152. }
  153. }
  154. if(node->coord == hero->visitablePos())
  155. continue;
  156. if(node->turns == 0)
  157. {
  158. logAi->error(
  159. "Unable to complete chain. Expected hero %s to arive to %s but he is at %s",
  160. hero->getNameTranslated(),
  161. node->coord.toString(),
  162. hero->visitablePos().toString());
  163. return;
  164. }
  165. // no exception means we were not able to reach the tile
  166. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  167. blockedIndexes.insert(node->parentIndex);
  168. }
  169. catch(const goalFulfilledException &)
  170. {
  171. if(!heroPtr.validAndSet())
  172. {
  173. logAi->debug("Hero %s was killed while attempting to reach %s", heroPtr.name, node->coord.toString());
  174. return;
  175. }
  176. }
  177. }
  178. }
  179. std::string ExecuteHeroChain::toString() const
  180. {
  181. return "ExecuteHeroChain " + targetName + " by " + chainPath.targetHero->getNameTranslated();
  182. }
  183. bool ExecuteHeroChain::moveHeroToTile(const CGHeroInstance * hero, const int3 & tile)
  184. {
  185. if(tile == hero->visitablePos() && cb->getVisitableObjs(hero->visitablePos()).size() < 2)
  186. {
  187. logAi->warn("Why do I want to move hero %s to tile %s? Already standing on that tile! ", hero->getNameTranslated(), tile.toString());
  188. return true;
  189. }
  190. return ai->moveHeroToTile(tile, hero);
  191. }
  192. }