ExecuteHeroChain.cpp 4.9 KB

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