2
0

Composition.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * BuildThis.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 "Composition.h"
  12. #include "../AIGateway.h"
  13. #include "../AIUtility.h"
  14. #include "../../../lib/mapping/CMap.h" //for victory conditions
  15. #include "../../../lib/CPathfinder.h"
  16. #include "../../../lib/StringConstants.h"
  17. namespace NKAI
  18. {
  19. extern boost::thread_specific_ptr<CCallback> cb;
  20. extern boost::thread_specific_ptr<AIGateway> ai;
  21. using namespace Goals;
  22. bool Composition::operator==(const Composition & other) const
  23. {
  24. return false;
  25. }
  26. std::string Composition::toString() const
  27. {
  28. std::string result = "Composition";
  29. for(auto goal : subtasks)
  30. {
  31. result += " " + goal->toString();
  32. }
  33. return result;
  34. }
  35. void Composition::accept(AIGateway * ai)
  36. {
  37. taskptr(*subtasks.back())->accept(ai);
  38. }
  39. TGoalVec Composition::decompose() const
  40. {
  41. return subtasks;
  42. }
  43. Composition & Composition::addNext(const AbstractGoal & goal)
  44. {
  45. return addNext(sptr(goal));
  46. }
  47. Composition & Composition::addNext(TSubgoal goal)
  48. {
  49. if(goal->goalType == COMPOSITION)
  50. {
  51. Composition & other = dynamic_cast<Composition &>(*goal);
  52. vstd::concatenate(subtasks, other.subtasks);
  53. }
  54. else
  55. {
  56. subtasks.push_back(goal);
  57. }
  58. return *this;
  59. }
  60. bool Composition::isElementar() const
  61. {
  62. return subtasks.back()->isElementar();
  63. }
  64. int Composition::getHeroExchangeCount() const
  65. {
  66. return isElementar() ? taskptr(*subtasks.back())->getHeroExchangeCount() : 0;
  67. }
  68. }