ExecuteHeroChain.cpp 8.0 KB

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