SpecialAction.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * SpecialAction.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 "SpecialAction.h"
  12. #include "../../AIGateway.h"
  13. #include "../../Goals/CGoal.h"
  14. #include "../../Goals/Invalid.h"
  15. namespace NKAI
  16. {
  17. Goals::TSubgoal SpecialAction::decompose(const Nullkiller * ai, const CGHeroInstance * hero) const
  18. {
  19. return Goals::sptr(Goals::Invalid());
  20. }
  21. void SpecialAction::execute(AIGateway * ai, const CGHeroInstance * hero) const
  22. {
  23. throw cannotFulfillGoalException("Can not execute " + toString());
  24. }
  25. bool CompositeAction::canAct(const Nullkiller * ai, const AIPathNode * source) const
  26. {
  27. for(auto part : parts)
  28. {
  29. if(!part->canAct(ai, source)) return false;
  30. }
  31. return true;
  32. }
  33. Goals::TSubgoal CompositeAction::decompose(const Nullkiller * ai, const CGHeroInstance * hero) const
  34. {
  35. for(auto part : parts)
  36. {
  37. auto goal = part->decompose(ai, hero);
  38. if(!goal->invalid()) return goal;
  39. }
  40. return SpecialAction::decompose(ai, hero);
  41. }
  42. void CompositeAction::execute(AIGateway * ai, const CGHeroInstance * hero) const
  43. {
  44. for(auto part : parts)
  45. {
  46. part->execute(ai, hero);
  47. }
  48. }
  49. void CompositeAction::applyOnDestination(
  50. const CGHeroInstance * hero,
  51. CDestinationNodeInfo & destination,
  52. const PathNodeInfo & source,
  53. AIPathNode * dstNode,
  54. const AIPathNode * srcNode) const
  55. {
  56. for(auto part : parts)
  57. {
  58. part->applyOnDestination(hero, destination, source, dstNode, srcNode);
  59. }
  60. }
  61. std::string CompositeAction::toString() const
  62. {
  63. std::string result = "";
  64. for(auto part : parts)
  65. {
  66. result += ", " + part->toString();
  67. }
  68. return result;
  69. }
  70. const CGObjectInstance * CompositeAction::targetObject() const
  71. {
  72. if(parts.empty())
  73. return nullptr;
  74. return parts.front()->targetObject();
  75. }
  76. }