ExecuteHeroChain.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if(obj)
  27. objid = obj->id.getNum();
  28. evaluationContext.danger = path.getTotalDanger(hero);
  29. evaluationContext.movementCost = path.movementCost();
  30. evaluationContext.armyLoss = path.armyLoss;
  31. evaluationContext.heroStrength = path.getHeroStrength();
  32. hero = path.targetHero;
  33. tile = path.firstTileToGet();
  34. }
  35. bool ExecuteHeroChain::operator==(const ExecuteHeroChain & other) const
  36. {
  37. return false;
  38. }
  39. TSubgoal ExecuteHeroChain::whatToDoToAchieve()
  40. {
  41. return iAmElementar();
  42. }
  43. void ExecuteHeroChain::accept(VCAI * ai)
  44. {
  45. logAi->debug("Executing hero chain towards %s", tile.toString());
  46. #ifdef VCMI_TRACE_PATHFINDER
  47. std::stringstream str;
  48. str << "Path ";
  49. for(auto node : chainPath.nodes)
  50. str << node.targetHero->name << "->" << node.coord.toString() << "; ";
  51. logAi->trace(str.str());
  52. #endif
  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. auto vt = Goals::sptr(Goals::VisitTile(node.coord).sethero(hero));
  59. if(vstd::contains(blockedIndexes, i))
  60. {
  61. blockedIndexes.insert(node.parentIndex);
  62. ai->setGoal(hero, vt);
  63. continue;
  64. }
  65. logAi->debug("Moving hero %s to %s", hero.name, node.coord.toString());
  66. try
  67. {
  68. ai->nullkiller->setActive(hero);
  69. vt->accept(ai);
  70. blockedIndexes.insert(node.parentIndex);
  71. }
  72. catch(goalFulfilledException)
  73. {
  74. if(!hero)
  75. {
  76. logAi->debug("Hero %s was killed while attempting to rich %s", hero.name, node.coord.toString());
  77. return;
  78. }
  79. }
  80. }
  81. }
  82. std::string ExecuteHeroChain::name() const
  83. {
  84. return "ExecuteHeroChain";
  85. }
  86. std::string ExecuteHeroChain::completeMessage() const
  87. {
  88. return "Hero chain completed";
  89. }