ExecuteHeroChain.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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());
  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. }
  87. if(node.turns == 0)
  88. {
  89. auto targetNode = cb->getPathsInfo(hero.get())->getPathInfo(node.coord);
  90. if(!targetNode->accessible || targetNode->turns != 0)
  91. {
  92. logAi->error(
  93. "Enable to complete chain. Expected hero %s to arive to %s in 0 turns but he can not do this",
  94. hero.name,
  95. node.coord.toString());
  96. return;
  97. }
  98. }
  99. if(hero->movement)
  100. {
  101. try
  102. {
  103. Goals::VisitTile(node.coord).sethero(hero).accept(ai);
  104. }
  105. catch(cannotFulfillGoalException)
  106. {
  107. if(hero->movement > 0)
  108. {
  109. CGPath path;
  110. bool isOk = cb->getPathsInfo(hero.get())->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.get(), HeroLockedReason::HERO_CHAIN);
  115. return;
  116. }
  117. }
  118. throw;
  119. }
  120. }
  121. }
  122. if(node.turns == 0)
  123. {
  124. logAi->error(
  125. "Enable to complete chain. Expected hero %s to arive to %s but he is at %s",
  126. hero.name,
  127. node.coord.toString(),
  128. hero->visitablePos().toString());
  129. return;
  130. }
  131. // no exception means we were not able to rich the tile
  132. ai->nullkiller->lockHero(hero.get(), HeroLockedReason::HERO_CHAIN);
  133. blockedIndexes.insert(node.parentIndex);
  134. }
  135. catch(goalFulfilledException)
  136. {
  137. if(!hero)
  138. {
  139. logAi->debug("Hero %s was killed while attempting to rich %s", hero.name, node.coord.toString());
  140. return;
  141. }
  142. }
  143. }
  144. }
  145. std::string ExecuteHeroChain::name() const
  146. {
  147. return "ExecuteHeroChain " + targetName;
  148. }
  149. std::string ExecuteHeroChain::completeMessage() const
  150. {
  151. return "Hero chain completed";
  152. }