StayAtTownBehavior.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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
  27. {
  28. Goals::TGoalVec tasks;
  29. auto towns = cb->getTownsInfo();
  30. if(!towns.size())
  31. return tasks;
  32. for(auto town : towns)
  33. {
  34. if(!town->hasBuilt(BuildingID::MAGES_GUILD_1))
  35. continue;
  36. auto paths = ai->nullkiller->pathfinder->getPathInfo(town->visitablePos());
  37. for(auto & path : paths)
  38. {
  39. if(town->visitingHero && town->visitingHero.get() != path.targetHero)
  40. continue;
  41. if(path.turn() == 0 && !path.getFirstBlockedAction() && path.exchangeCount <= 1)
  42. {
  43. if(path.targetHero->mana == path.targetHero->manaLimit())
  44. continue;
  45. Composition stayAtTown;
  46. stayAtTown.addNextSequence({
  47. sptr(ExecuteHeroChain(path)),
  48. sptr(StayAtTown(town, path))
  49. });
  50. tasks.push_back(sptr(stayAtTown));
  51. }
  52. }
  53. }
  54. return tasks;
  55. }
  56. }