ClusterBehavior.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * RecruitHeroBehavior.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 "ClusterBehavior.h"
  12. #include "../AIGateway.h"
  13. #include "../Engine/Nullkiller.h"
  14. #include "../AIUtility.h"
  15. #include "../Markers/UnlockCluster.h"
  16. #include "../Goals/Composition.h"
  17. #include "../Behaviors/CaptureObjectsBehavior.h"
  18. namespace NKAI
  19. {
  20. extern boost::thread_specific_ptr<CCallback> cb;
  21. extern boost::thread_specific_ptr<AIGateway> ai;
  22. using namespace Goals;
  23. std::string ClusterBehavior::toString() const
  24. {
  25. return "Unlock Clusters";
  26. }
  27. Goals::TGoalVec ClusterBehavior::decompose() const
  28. {
  29. Goals::TGoalVec tasks;
  30. auto clusters = ai->nullkiller->objectClusterizer->getLockedClusters();
  31. for(auto cluster : clusters)
  32. {
  33. vstd::concatenate(tasks, decomposeCluster(cluster));
  34. }
  35. return tasks;
  36. }
  37. Goals::TGoalVec ClusterBehavior::decomposeCluster(std::shared_ptr<ObjectCluster> cluster) const
  38. {
  39. auto center = cluster->calculateCenter();
  40. auto paths = ai->nullkiller->pathfinder->getPathInfo(center->visitablePos());
  41. auto blockerPos = cluster->blocker->visitablePos();
  42. std::vector<AIPath> blockerPaths;
  43. blockerPaths.reserve(paths.size());
  44. TGoalVec goals;
  45. #if NKAI_TRACE_LEVEL >= 2
  46. logAi->trace(
  47. "Checking cluster %s %s, found %d paths",
  48. cluster->blocker->getObjectName(),
  49. cluster->blocker->visitablePos().toString(),
  50. paths.size());
  51. #endif
  52. for(auto path = paths.begin(); path != paths.end();)
  53. {
  54. #if NKAI_TRACE_LEVEL >= 2
  55. logAi->trace("Checking path %s", path->toString());
  56. #endif
  57. auto blocker = ai->nullkiller->objectClusterizer->getBlocker(*path);
  58. if(blocker != cluster->blocker)
  59. {
  60. path = paths.erase(path);
  61. continue;
  62. }
  63. blockerPaths.push_back(*path);
  64. AIPath & clonedPath = blockerPaths.back();
  65. clonedPath.nodes.clear();
  66. for(auto node = path->nodes.rbegin(); node != path->nodes.rend(); node++)
  67. {
  68. clonedPath.nodes.insert(clonedPath.nodes.begin(), *node);
  69. if(node->coord == blockerPos || cb->getGuardingCreaturePosition(node->coord) == blockerPos)
  70. break;
  71. }
  72. for(auto & node : clonedPath.nodes)
  73. node.parentIndex -= path->nodes.size() - clonedPath.nodes.size();
  74. #if NKAI_TRACE_LEVEL >= 2
  75. logAi->trace("Unlock path found %s", blockerPaths.back().toString());
  76. #endif
  77. path++;
  78. }
  79. #if NKAI_TRACE_LEVEL >= 2
  80. logAi->trace("Decompose unlock paths");
  81. #endif
  82. auto unlockTasks = CaptureObjectsBehavior::getVisitGoals(blockerPaths);
  83. for(int i = 0; i < paths.size(); i++)
  84. {
  85. if(unlockTasks[i]->invalid())
  86. continue;
  87. auto path = paths[i];
  88. auto elementarUnlock = sptr(UnlockCluster(cluster, path));
  89. goals.push_back(sptr(Composition().addNext(elementarUnlock).addNext(unlockTasks[i])));
  90. }
  91. return goals;
  92. }
  93. }