ExecuteHeroChain.cpp 5.1 KB

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