Composition.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/constants/StringConstants.h"
  15. namespace NKAI
  16. {
  17. using namespace Goals;
  18. bool Composition::operator==(const Composition & other) const
  19. {
  20. return false;
  21. }
  22. std::string Composition::toString() const
  23. {
  24. std::string result = "Composition";
  25. for(auto step : subtasks)
  26. {
  27. result += "[";
  28. for(auto goal : step)
  29. {
  30. if(goal->isElementar())
  31. result += goal->toString() + " => ";
  32. else
  33. result += goal->toString() + ", ";
  34. }
  35. result += "] ";
  36. }
  37. return result;
  38. }
  39. void Composition::accept(AIGateway * ai)
  40. {
  41. for(auto task : subtasks.back())
  42. {
  43. if(task->isElementar())
  44. {
  45. taskptr(*task)->accept(ai);
  46. }
  47. else
  48. {
  49. break;
  50. }
  51. }
  52. }
  53. TGoalVec Composition::decompose(const Nullkiller * ai) const
  54. {
  55. TGoalVec result;
  56. for(const TGoalVec & step : subtasks)
  57. vstd::concatenate(result, step);
  58. return result;
  59. }
  60. Composition & Composition::addNextSequence(const TGoalVec & taskSequence)
  61. {
  62. subtasks.push_back(taskSequence);
  63. return *this;
  64. }
  65. Composition & Composition::addNext(TSubgoal goal)
  66. {
  67. if(goal->goalType == COMPOSITION)
  68. {
  69. Composition & other = dynamic_cast<Composition &>(*goal);
  70. vstd::concatenate(subtasks, other.subtasks);
  71. }
  72. else
  73. {
  74. subtasks.push_back({goal});
  75. }
  76. return *this;
  77. }
  78. Composition & Composition::addNext(const AbstractGoal & goal)
  79. {
  80. return addNext(sptr(goal));
  81. }
  82. bool Composition::isElementar() const
  83. {
  84. return subtasks.back().front()->isElementar();
  85. }
  86. int Composition::getHeroExchangeCount() const
  87. {
  88. auto result = 0;
  89. for(auto task : subtasks.back())
  90. {
  91. if(task->isElementar())
  92. {
  93. result += taskptr(*task)->getHeroExchangeCount();
  94. }
  95. }
  96. return result;
  97. }
  98. std::vector<ObjectInstanceID> Composition::getAffectedObjects() const
  99. {
  100. std::vector<ObjectInstanceID> affectedObjects;
  101. for(auto sequence : subtasks)
  102. {
  103. for(auto task : sequence)
  104. {
  105. if(task->isElementar())
  106. vstd::concatenate(affectedObjects, task->asTask()->getAffectedObjects());
  107. }
  108. }
  109. vstd::removeDuplicates(affectedObjects);
  110. return affectedObjects;
  111. }
  112. bool Composition::isObjectAffected(ObjectInstanceID id) const
  113. {
  114. for(auto sequence : subtasks)
  115. {
  116. for(auto task : sequence)
  117. {
  118. if(task->isElementar() && task->asTask()->isObjectAffected(id))
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. }