ExecuteHeroChain.cpp 4.9 KB

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