ExecuteHeroChain.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. std::vector<ObjectInstanceID> ExecuteHeroChain::getAffectedObjects() const
  40. {
  41. std::vector<ObjectInstanceID> affectedObjects = { chainPath.targetHero->id };
  42. if(objid != -1)
  43. affectedObjects.push_back(ObjectInstanceID(objid));
  44. for(auto & node : chainPath.nodes)
  45. {
  46. if(node.targetHero)
  47. affectedObjects.push_back(node.targetHero->id);
  48. }
  49. vstd::removeDuplicates(affectedObjects);
  50. return affectedObjects;
  51. }
  52. bool ExecuteHeroChain::isObjectAffected(ObjectInstanceID id) const
  53. {
  54. if(chainPath.targetHero->id == id || objid == id)
  55. return true;
  56. for(auto & node : chainPath.nodes)
  57. {
  58. if(node.targetHero && node.targetHero->id == id)
  59. return true;
  60. }
  61. return false;
  62. }
  63. void ExecuteHeroChain::accept(AIGateway * ai)
  64. {
  65. logAi->debug("Executing hero chain towards %s. Path %s", targetName, chainPath.toString());
  66. ai->nullkiller->setActive(chainPath.targetHero, tile);
  67. ai->nullkiller->setTargetObject(objid);
  68. auto targetObject = ai->myCb->getObj(static_cast<ObjectInstanceID>(objid), false);
  69. if(chainPath.turn() == 0 && targetObject && targetObject->ID == Obj::TOWN)
  70. {
  71. auto relations = ai->myCb->getPlayerRelations(ai->playerID, targetObject->getOwner());
  72. if(relations == PlayerRelations::ENEMIES)
  73. {
  74. ai->nullkiller->armyFormation->rearrangeArmyForSiege(
  75. dynamic_cast<const CGTownInstance *>(targetObject),
  76. chainPath.targetHero);
  77. }
  78. }
  79. std::set<int> blockedIndexes;
  80. for(int i = chainPath.nodes.size() - 1; i >= 0; i--)
  81. {
  82. auto * node = &chainPath.nodes[i];
  83. const CGHeroInstance * hero = node->targetHero;
  84. HeroPtr heroPtr = hero;
  85. if(!heroPtr.validAndSet())
  86. {
  87. logAi->error("Hero %s was lost. Exit hero chain.", heroPtr.name);
  88. return;
  89. }
  90. if(node->parentIndex >= i)
  91. {
  92. logAi->error("Invalid parentIndex while executing node " + node->coord.toString());
  93. }
  94. if(vstd::contains(blockedIndexes, i))
  95. {
  96. blockedIndexes.insert(node->parentIndex);
  97. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  98. continue;
  99. }
  100. logAi->debug("Executing chain node %d. Moving hero %s to %s", i, hero->getNameTranslated(), node->coord.toString());
  101. try
  102. {
  103. if(hero->movementPointsRemaining() > 0)
  104. {
  105. ai->nullkiller->setActive(hero, node->coord);
  106. if(node->specialAction)
  107. {
  108. if(node->actionIsBlocked)
  109. {
  110. throw cannotFulfillGoalException("Path is nondeterministic.");
  111. }
  112. node->specialAction->execute(ai, hero);
  113. if(!heroPtr.validAndSet())
  114. {
  115. logAi->error("Hero %s was lost trying to execute special action. Exit hero chain.", heroPtr.name);
  116. return;
  117. }
  118. }
  119. else if(i > 0 && ai->nullkiller->isObjectGraphAllowed())
  120. {
  121. auto chainMask = i < chainPath.nodes.size() - 1 ? chainPath.nodes[i + 1].chainMask : node->chainMask;
  122. for(auto j = i - 1; j >= 0; j--)
  123. {
  124. auto & nextNode = chainPath.nodes[j];
  125. if(nextNode.specialAction || nextNode.chainMask != chainMask)
  126. break;
  127. auto targetNode = cb->getPathsInfo(hero)->getPathInfo(nextNode.coord);
  128. if(!targetNode->reachable()
  129. || targetNode->getCost() > nextNode.cost)
  130. break;
  131. i = j;
  132. node = &nextNode;
  133. if(targetNode->action == EPathNodeAction::BATTLE || targetNode->action == EPathNodeAction::TELEPORT_BATTLE)
  134. break;
  135. }
  136. }
  137. if(node->turns == 0 && node->coord != hero->visitablePos())
  138. {
  139. auto targetNode = cb->getPathsInfo(hero)->getPathInfo(node->coord);
  140. if(targetNode->accessible == EPathAccessibility::NOT_SET
  141. || targetNode->accessible == EPathAccessibility::BLOCKED
  142. || targetNode->accessible == EPathAccessibility::FLYABLE
  143. || targetNode->turns != 0)
  144. {
  145. logAi->error(
  146. "Unable to complete chain. Expected hero %s to arrive to %s in 0 turns but he cannot do this",
  147. hero->getNameTranslated(),
  148. node->coord.toString());
  149. return;
  150. }
  151. }
  152. if(hero->movementPointsRemaining())
  153. {
  154. try
  155. {
  156. if(moveHeroToTile(ai, hero, node->coord))
  157. {
  158. continue;
  159. }
  160. }
  161. catch(const cannotFulfillGoalException &)
  162. {
  163. if(!heroPtr.validAndSet())
  164. {
  165. logAi->error("Hero %s was lost. Exit hero chain.", heroPtr.name);
  166. return;
  167. }
  168. if(hero->movementPointsRemaining() > 0)
  169. {
  170. CGPath path;
  171. bool isOk = cb->getPathsInfo(hero)->getPath(path, node->coord);
  172. if(isOk && path.nodes.back().turns > 0)
  173. {
  174. logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero->getNameTranslated(), hero->movementPointsRemaining(), node->coord.toString());
  175. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  176. return;
  177. }
  178. }
  179. throw;
  180. }
  181. }
  182. }
  183. if(node->coord == hero->visitablePos())
  184. continue;
  185. if(node->turns == 0)
  186. {
  187. logAi->error(
  188. "Unable to complete chain. Expected hero %s to arrive to %s but he is at %s",
  189. hero->getNameTranslated(),
  190. node->coord.toString(),
  191. hero->visitablePos().toString());
  192. return;
  193. }
  194. // no exception means we were not able to reach the tile
  195. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  196. blockedIndexes.insert(node->parentIndex);
  197. }
  198. catch(const goalFulfilledException &)
  199. {
  200. if(!heroPtr.validAndSet())
  201. {
  202. logAi->debug("Hero %s was killed while attempting to reach %s", heroPtr.name, node->coord.toString());
  203. return;
  204. }
  205. }
  206. }
  207. }
  208. std::string ExecuteHeroChain::toString() const
  209. {
  210. return "ExecuteHeroChain " + targetName + " by " + chainPath.targetHero->getNameTranslated();
  211. }
  212. bool ExecuteHeroChain::moveHeroToTile(AIGateway * ai, const CGHeroInstance * hero, const int3 & tile)
  213. {
  214. if(tile == hero->visitablePos() && ai->myCb->getVisitableObjs(hero->visitablePos()).size() < 2)
  215. {
  216. logAi->warn("Why do I want to move hero %s to tile %s? Already standing on that tile! ", hero->getNameTranslated(), tile.toString());
  217. return true;
  218. }
  219. return ai->moveHeroToTile(tile, hero);
  220. }
  221. }