2
0

StayAtTownBehavior.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * StartupBehavior.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 "StayAtTownBehavior.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../Goals/StayAtTown.h"
  15. #include "../Goals/Composition.h"
  16. #include "../Goals/ExecuteHeroChain.h"
  17. #include "lib/mapObjects/MapObjects.h" //for victory conditions
  18. #include "../Engine/Nullkiller.h"
  19. namespace NKAI
  20. {
  21. using namespace Goals;
  22. std::string StayAtTownBehavior::toString() const
  23. {
  24. return "StayAtTownBehavior";
  25. }
  26. Goals::TGoalVec StayAtTownBehavior::decompose(const Nullkiller * ai) const
  27. {
  28. Goals::TGoalVec tasks;
  29. auto towns = ai->cb->getTownsInfo();
  30. if(!towns.size())
  31. return tasks;
  32. std::vector<AIPath> paths;
  33. for(auto town : towns)
  34. {
  35. ai->pathfinder->calculatePathInfo(paths, town->visitablePos());
  36. for(auto & path : paths)
  37. {
  38. if(town->getVisitingHero() && town->getVisitingHero() != path.targetHero)
  39. continue;
  40. if(!path.getFirstBlockedAction() && path.exchangeCount <= 1)
  41. {
  42. Composition stayAtTown;
  43. stayAtTown.addNextSequence({
  44. sptr(ExecuteHeroChain(path)),
  45. sptr(StayAtTown(town, path))
  46. });
  47. tasks.push_back(sptr(stayAtTown));
  48. }
  49. }
  50. }
  51. return tasks;
  52. }
  53. }