Composition.cpp 2.0 KB

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