Composition.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/StringConstants.h"
  15. namespace NKAI
  16. {
  17. extern boost::thread_specific_ptr<CCallback> cb;
  18. extern boost::thread_specific_ptr<AIGateway> ai;
  19. using namespace Goals;
  20. bool Composition::operator==(const Composition & other) const
  21. {
  22. return false;
  23. }
  24. std::string Composition::toString() const
  25. {
  26. std::string result = "Composition";
  27. for(auto step : subtasks)
  28. {
  29. result += "[";
  30. for(auto goal : step)
  31. {
  32. if(goal->isElementar())
  33. result += goal->toString() + " => ";
  34. else
  35. result += goal->toString() + ", ";
  36. }
  37. result += "] ";
  38. }
  39. return result;
  40. }
  41. void Composition::accept(AIGateway * ai)
  42. {
  43. for(auto task : subtasks.back())
  44. {
  45. if(task->isElementar())
  46. {
  47. taskptr(*task)->accept(ai);
  48. }
  49. else
  50. {
  51. break;
  52. }
  53. }
  54. }
  55. TGoalVec Composition::decompose() const
  56. {
  57. TGoalVec result;
  58. for(const TGoalVec & step : subtasks)
  59. vstd::concatenate(result, step);
  60. return result;
  61. }
  62. Composition & Composition::addNextSequence(const TGoalVec & taskSequence)
  63. {
  64. subtasks.push_back(taskSequence);
  65. return *this;
  66. }
  67. Composition & Composition::addNext(TSubgoal goal)
  68. {
  69. if(goal->goalType == COMPOSITION)
  70. {
  71. Composition & other = dynamic_cast<Composition &>(*goal);
  72. vstd::concatenate(subtasks, other.subtasks);
  73. }
  74. else
  75. {
  76. subtasks.push_back({goal});
  77. }
  78. return *this;
  79. }
  80. Composition & Composition::addNext(const AbstractGoal & goal)
  81. {
  82. return addNext(sptr(goal));
  83. }
  84. bool Composition::isElementar() const
  85. {
  86. return subtasks.back().front()->isElementar();
  87. }
  88. int Composition::getHeroExchangeCount() const
  89. {
  90. auto result = 0;
  91. for(auto task : subtasks.back())
  92. {
  93. if(task->isElementar())
  94. {
  95. result += taskptr(*task)->getHeroExchangeCount();
  96. }
  97. }
  98. return result;
  99. }
  100. }