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