Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ======================================================
- //
- // Corridor generations
- //
- // ======================================================
- struct PathNode {
- FVector Position;
- PathNode* CameFrom;
- PathNode(const FVector& pos, PathNode* cameFrom = nullptr)
- : Position(pos), CameFrom(cameFrom) {}
- };
- TArray<FVector> UDungeonGeneratorAPI::FindPathDFS(const FVector& start, const FVector& end)
- {
- TArray<PathNode> stack;
- TArray<PathNode> visited;
- stack.Emplace(start);
- // While we are still searching...
- while (stack.Num() > 0) {
- PathNode currentNode = stack.Pop();
- // Terminate, reached end vector position
- if (currentNode.Position == end) {
- // Build return path
- TArray<FVector> path;
- const PathNode* pathNode = ¤tNode;
- while (pathNode != nullptr) {
- path.Add(pathNode->Position);
- pathNode = pathNode->CameFrom;
- }
- Algo::Reverse(path);
- return path;
- }
- // Check if a position has been visited
- auto IsVisited = [&visited](const FVector& pos) {
- return visited.ContainsByPredicate([&pos](const PathNode& node) {
- return node.Position == pos;
- });
- };
- if (!IsVisited(currentNode.Position)) {
- // Add to visited
- visited.Add(currentNode);
- // Explore neighbors
- TArray<FVector> directions = { FVector(1, 0, 0), FVector(-1, 0, 0), FVector(0, 1, 0), FVector(0, -1, 0) };
- for (const FVector& dir : directions) {
- FVector neighborPos = currentNode.Position + dir * UDungeonGeneratorAPI::grid.GetWidth();
- if (!IsVisited(neighborPos)) {
- // Make last item in stack last visited
- stack.Emplace(neighborPos, &visited.Last());
- }
- }
- }
- }
- return {};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement