ExecuteHeroChain.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. std::set<int> blockedIndexes;
  47. for(int i = chainPath.nodes.size() - 1; i >= 0; i--)
  48. {
  49. auto & node = chainPath.nodes[i];
  50. const CGHeroInstance * hero = node.targetHero;
  51. HeroPtr heroPtr = hero;
  52. if(node.parentIndex >= i)
  53. {
  54. logAi->error("Invalid parentIndex while executing node " + node.coord.toString());
  55. }
  56. if(vstd::contains(blockedIndexes, i))
  57. {
  58. blockedIndexes.insert(node.parentIndex);
  59. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  60. continue;
  61. }
  62. logAi->debug("Executing chain node %d. Moving hero %s to %s", i, hero->getNameTranslated(), node.coord.toString());
  63. try
  64. {
  65. if(hero->movementPointsRemaining() > 0)
  66. {
  67. ai->nullkiller->setActive(hero, node.coord);
  68. if(node.specialAction)
  69. {
  70. if(node.actionIsBlocked)
  71. {
  72. throw cannotFulfillGoalException("Path is nondeterministic.");
  73. }
  74. node.specialAction->execute(hero);
  75. if(!heroPtr.validAndSet())
  76. {
  77. logAi->error("Hero %s was lost trying to execute special action. Exit hero chain.", heroPtr.name);
  78. return;
  79. }
  80. }
  81. if(node.turns == 0 && node.coord != hero->visitablePos())
  82. {
  83. auto targetNode = cb->getPathsInfo(hero)->getPathInfo(node.coord);
  84. if(targetNode->accessible == EPathAccessibility::NOT_SET
  85. || targetNode->accessible == EPathAccessibility::BLOCKED
  86. || targetNode->accessible == EPathAccessibility::FLYABLE
  87. || targetNode->turns != 0)
  88. {
  89. logAi->error(
  90. "Unable to complete chain. Expected hero %s to arrive to %s in 0 turns but he cannot do this",
  91. hero->getNameTranslated(),
  92. node.coord.toString());
  93. return;
  94. }
  95. }
  96. if(hero->movementPointsRemaining())
  97. {
  98. try
  99. {
  100. if(moveHeroToTile(hero, node.coord))
  101. {
  102. continue;
  103. }
  104. }
  105. catch(const cannotFulfillGoalException &)
  106. {
  107. if(!heroPtr.validAndSet())
  108. {
  109. logAi->error("Hero %s was lost. Exit hero chain.", heroPtr.name);
  110. return;
  111. }
  112. if(hero->movementPointsRemaining() > 0)
  113. {
  114. CGPath path;
  115. bool isOk = cb->getPathsInfo(hero)->getPath(path, node.coord);
  116. if(isOk && path.nodes.back().turns > 0)
  117. {
  118. logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero->getNameTranslated(), hero->movementPointsRemaining(), node.coord.toString());
  119. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  120. return;
  121. }
  122. }
  123. throw;
  124. }
  125. }
  126. }
  127. if(node.coord == hero->visitablePos())
  128. continue;
  129. if(node.turns == 0)
  130. {
  131. logAi->error(
  132. "Unable to complete chain. Expected hero %s to arive to %s but he is at %s",
  133. hero->getNameTranslated(),
  134. node.coord.toString(),
  135. hero->visitablePos().toString());
  136. return;
  137. }
  138. // no exception means we were not able to reach the tile
  139. ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
  140. blockedIndexes.insert(node.parentIndex);
  141. }
  142. catch(const goalFulfilledException &)
  143. {
  144. if(!heroPtr.validAndSet())
  145. {
  146. logAi->debug("Hero %s was killed while attempting to reach %s", heroPtr.name, node.coord.toString());
  147. return;
  148. }
  149. }
  150. }
  151. }
  152. std::string ExecuteHeroChain::toString() const
  153. {
  154. return "ExecuteHeroChain " + targetName + " by " + chainPath.targetHero->getNameTranslated();
  155. }
  156. bool ExecuteHeroChain::moveHeroToTile(const CGHeroInstance * hero, const int3 & tile)
  157. {
  158. if(tile == hero->visitablePos() && cb->getVisitableObjs(hero->visitablePos()).size() < 2)
  159. {
  160. logAi->warn("Why do I want to move hero %s to tile %s? Already standing on that tile! ", hero->getNameTranslated(), tile.toString());
  161. return true;
  162. }
  163. return ai->moveHeroToTile(tile, hero);
  164. }
  165. }