ThomasPixel

DFS testing for dungeon rooms

Apr 16th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. class PathNode {
  2. private:
  3.     FVector Position;
  4.     PathNode* CameFrom;
  5.  
  6. public:
  7.     // Constructor
  8.     PathNode(const FVector& pos, PathNode* cameFrom = nullptr)
  9.         : Position(pos), CameFrom(cameFrom) {}
  10.  
  11.     // Getters
  12.     FVector getPosition() const { return Position; }
  13.     PathNode* getCameFrom() const { return CameFrom; }
  14.  
  15.     // Operator to compare nodes based on position
  16.     bool operator==(const PathNode& other) const {
  17.         return Position == other.Position;
  18.     }
  19. };
  20.  
  21. TArray<FVector> UDungeonGeneratorAPI::FindPathDFS(const FVector& start, const FVector& end)
  22. {
  23.     std::stack<PathNode*> stack;
  24.     std::vector<PathNode*> visited;
  25.     stack.push(new PathNode(start));
  26.  
  27.     auto IsVisited = [&visited](const FVector& pos) {
  28.         return std::any_of(visited.begin(), visited.end(), [&pos](const PathNode* n) {
  29.             return n->getPosition() == pos;
  30.             });
  31.         };
  32.  
  33.     while (!stack.empty()) {
  34.         PathNode* currentNode = stack.top();
  35.         stack.pop();
  36.  
  37.         if (currentNode->getPosition() == end) {
  38.             // Construct the path by tracing back from the end node to the start node
  39.             TArray<FVector> path;
  40.             while (currentNode != nullptr) {
  41.                 path.Add(currentNode->getPosition());
  42.                 currentNode = currentNode->getCameFrom();
  43.             }
  44.             std::reverse(path.begin(), path.end());
  45.  
  46.             // Clean up memory used by nodes
  47.             for (auto node : visited) delete node;
  48.             delete currentNode;
  49.             while (!stack.empty()) {
  50.                 delete stack.top();
  51.                 stack.pop();
  52.             }
  53.             return path;
  54.         }
  55.  
  56.         if (!IsVisited(currentNode->getPosition())) {
  57.             visited.push_back(currentNode);
  58.  
  59.             // Explore neighbors
  60.             for (auto& dir : { FVector(1, 0, 0), FVector(-1, 0, 0), FVector(0, 1, 0), FVector(0, -1, 0) }) {
  61.                 FVector neighborPos = currentNode->getPosition() + dir * tileSize;
  62.                 if (!IsVisited(neighborPos)) {
  63.                     stack.push(new PathNode(neighborPos, currentNode));
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     // Clean up memory if no path found
  70.     for (auto node : visited) delete node;
  71.     while (!stack.empty()) {
  72.         delete stack.top();
  73.         stack.pop();
  74.     }
  75.  
  76.     return {};
  77. }
Add Comment
Please, Sign In to add comment