Composition.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "../VCAI.h"
  13. #include "../AIUtility.h"
  14. #include "../AIhelper.h"
  15. #include "../FuzzyHelper.h"
  16. #include "../../../lib/mapping/CMap.h" //for victory conditions
  17. #include "../../../lib/CPathfinder.h"
  18. #include "../../../lib/StringConstants.h"
  19. extern boost::thread_specific_ptr<CCallback> cb;
  20. extern boost::thread_specific_ptr<VCAI> ai;
  21. extern FuzzyHelper * fh;
  22. using namespace Goals;
  23. bool Composition::operator==(const Composition & other) const
  24. {
  25. return false;
  26. }
  27. std::string Composition::toString() const
  28. {
  29. std::string result = "Composition";
  30. for(auto goal : subtasks)
  31. {
  32. result += " " + goal->toString();
  33. }
  34. return result;
  35. }
  36. void Composition::accept(VCAI * ai)
  37. {
  38. taskptr(*subtasks.back())->accept(ai);
  39. }
  40. TGoalVec Composition::decompose() const
  41. {
  42. if(isElementar())
  43. return subtasks;
  44. auto tasks = subtasks;
  45. tasks.pop_back();
  46. TSubgoal last = subtasks.back();
  47. auto decomposed = last->decompose();
  48. TGoalVec result;
  49. for(TSubgoal goal : decomposed)
  50. {
  51. if(goal->invalid() || goal == last || vstd::contains(tasks, goal))
  52. continue;
  53. auto newComposition = Composition(tasks);
  54. if(goal->goalType == COMPOSITION)
  55. {
  56. Composition & other = dynamic_cast<Composition &>(*goal);
  57. bool cancel = false;
  58. for(auto goal : other.subtasks)
  59. {
  60. if(goal == last || vstd::contains(tasks, goal))
  61. {
  62. cancel = true;
  63. break;
  64. }
  65. newComposition.addNext(goal);
  66. }
  67. if(cancel)
  68. continue;
  69. }
  70. else
  71. {
  72. newComposition.addNext(goal);
  73. }
  74. result.push_back(sptr(newComposition));
  75. }
  76. return result;
  77. }
  78. Composition & Composition::addNext(AbstractGoal & goal)
  79. {
  80. return addNext(sptr(goal));
  81. }
  82. Composition & Composition::addNext(TSubgoal goal)
  83. {
  84. subtasks.push_back(goal);
  85. return *this;
  86. }
  87. bool Composition::isElementar() const
  88. {
  89. return subtasks.back()->isElementar();
  90. }