2
0

ExecuteHeroChain.cpp 5.4 KB

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