2
0

ExecuteHeroChain.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. if(obj)
  33. {
  34. objid = obj->id.getNum();
  35. targetName = obj->getObjectName() + tile.toString();
  36. }
  37. else
  38. {
  39. targetName = "tile" + tile.toString();
  40. }
  41. }
  42. bool ExecuteHeroChain::operator==(const ExecuteHeroChain & other) const
  43. {
  44. return false;
  45. }
  46. TSubgoal ExecuteHeroChain::whatToDoToAchieve()
  47. {
  48. return iAmElementar();
  49. }
  50. void ExecuteHeroChain::accept(VCAI * ai)
  51. {
  52. logAi->debug("Executing hero chain towards %s. Path %s", targetName, chainPath.toString());
  53. std::set<int> blockedIndexes;
  54. for(int i = chainPath.nodes.size() - 1; i >= 0; i--)
  55. {
  56. auto & node = chainPath.nodes[i];
  57. HeroPtr hero = node.targetHero;
  58. if(vstd::contains(blockedIndexes, i))
  59. {
  60. blockedIndexes.insert(node.parentIndex);
  61. ai->nullkiller->lockHero(hero.get());
  62. continue;
  63. }
  64. logAi->debug("Executing chain node %d. Moving hero %s to %s", i, hero.name, node.coord.toString());
  65. try
  66. {
  67. if(hero->movement)
  68. {
  69. ai->nullkiller->setActive(hero.get());
  70. if(node.specialAction)
  71. {
  72. if(node.specialAction->canAct(hero.get()))
  73. {
  74. auto specialGoal = node.specialAction->whatToDo(hero);
  75. specialGoal->accept(ai);
  76. }
  77. else
  78. {
  79. //TODO: decompose
  80. }
  81. }
  82. if(node.turns == 0)
  83. {
  84. auto targetNode = cb->getPathsInfo(hero.get())->getPathInfo(node.coord);
  85. if(!targetNode->accessible || targetNode->turns != 0)
  86. {
  87. logAi->error(
  88. "Enable to complete chain. Expected hero %s to arive to %s in 0 turns but he can not do this",
  89. hero.name,
  90. node.coord.toString());
  91. return;
  92. }
  93. }
  94. if(hero->movement)
  95. {
  96. try
  97. {
  98. Goals::VisitTile(node.coord).sethero(hero).accept(ai);
  99. }
  100. catch(cannotFulfillGoalException)
  101. {
  102. if(hero->movement > 0)
  103. {
  104. CGPath path;
  105. bool isOk = cb->getPathsInfo(hero.get())->getPath(path, node.coord);
  106. if(isOk && path.nodes.back().turns > 0)
  107. {
  108. logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero.name, hero->movement, node.coord.toString());
  109. ai->nullkiller->lockHero(hero.get());
  110. return;
  111. }
  112. }
  113. throw;
  114. }
  115. }
  116. }
  117. if(node.turns == 0)
  118. {
  119. logAi->error(
  120. "Enable to complete chain. Expected hero %s to arive to %s but he is at %s",
  121. hero.name,
  122. node.coord.toString(),
  123. hero->visitablePos().toString());
  124. return;
  125. }
  126. // do not lock hero if it is simple one hero chain
  127. if(chainPath.exchangeCount == 1)
  128. return;
  129. // no exception means we were not able to rich the tile
  130. ai->nullkiller->lockHero(hero.get());
  131. blockedIndexes.insert(node.parentIndex);
  132. }
  133. catch(goalFulfilledException)
  134. {
  135. if(!hero)
  136. {
  137. logAi->debug("Hero %s was killed while attempting to rich %s", hero.name, node.coord.toString());
  138. return;
  139. }
  140. }
  141. }
  142. }
  143. std::string ExecuteHeroChain::name() const
  144. {
  145. return "ExecuteHeroChain " + targetName;
  146. }
  147. std::string ExecuteHeroChain::completeMessage() const
  148. {
  149. return "Hero chain completed";
  150. }