ExecuteHeroChain.cpp 4.7 KB

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