SaxonRahs Tutorial Thread - Random Maze Generation & Solving

Explanation of Maze Generation.
Explanations will head the Code (comments are on top of the describing line)

[]

Function GenDebugCoord() takes a float tileX and a float tileY, these are the for each side, note we use an odd number @ GenDebugCoord(x,y); ( because we have a center block/ a better way of thinking of it, the maze starts at 1,1 and goes till the maze size and each side has a bounding wall)

**You need to set your tiles, x max,y max, in the hud blueprint and link that hud blueprint to your gamemode blueprint

You should probably set the variables for MazeX and MazeY in your hud blueprint before you simulate or play as they are set to 101 the and is currently spawning StaticMeshActors NOT Instanced (huge optimization if you use InstancedStaticMeshes)**


void AMazeHud::GenMaze(float tileX, float tileY){
    float CaptureX = 0.0f;
    float CaptureY = 0.0f;
    float offset = 400.0f;
    float iter = 0;
    int tileID = 0;
    int RandomEndTileLoc = rand() % ((int)tileX - 1) + 1;

 //-------------An Array called 'grid' that will hold AStaticMeshActor*---------------
    AStaticMeshActor* grid[MazeSizeMax][MazeSizeMax];

//-------------Build a Squares X----------------
    for (int x = 0; x<tileX; x++){
//-------------Build a Squares Y---------------
        for (int y = 0; y<tileY; y++){
            if (y == 0 || x == 0 || y == tileY - 1 || x == tileX - 1 || y % 2 == 0 && x % 2 == 0){
                //                          (X.Xf,Y.Yf,Z.Zf)
                const FVector  GenSpawnLoc(CaptureX, CaptureY, 0.0f);
                const FRotator GenSpawnRot(0.0f, 0.0f, 0.0f);
                AStaticMeshActor* BlockTile = SpawnBP<AStaticMeshActor>(GetWorld(), TileBlockBP, GenSpawnLoc, GenSpawnRot);


                grid[x][y] = BlockTile;
            }
            else{
                const FVector  GenSpawnLoc(CaptureX, CaptureY, 0.0f);
                const FRotator GenSpawnRot(0.0f, 0.0f, 0.0f);


                AStaticMeshActor* GroundTile = SpawnBP<AStaticMeshActor>(GetWorld(), TileGroundBP, GenSpawnLoc, GenSpawnRot);


                grid[x][y] = GroundTile;
            }
            //-------------Starting Tile Spawn---------------
            if (CaptureX == offset && CaptureY == offset){
                grid[1][1]->Destroy();


                const FVector  GenSpawnLoc(400.0f, 400.0f, 0.0f);
                const FRotator GenSpawnRot(0.0f, 0.0f, 0.0f);
                //Tile Start
                AStaticMeshActor* StartTile = SpawnBP<AStaticMeshActor>(GetWorld(), TileStartBP, GenSpawnLoc, GenSpawnRot);


                grid[1][1] = StartTile;
            }
            //-------------Ending Tile Spawn---------------
            if (y == tileY - 1 && x == tileX - 1){


                grid[x - 1][y - 1]->Destroy();

                const FVector  GenSpawnLoc(((tileX - 2) * offset), ((tileY - 2) * offset), 0);
                const FRotator GenSpawnRot(0.0f, 0.0f, 0.0f);
                // Tile End
                AStaticMeshActor* EndTile = SpawnBP<AStaticMeshActor>(GetWorld(), TileEndBP, GenSpawnLoc, GenSpawnRot);


                grid[x - 1][y - 1] = EndTile;
            }
            CaptureY += offset;
            if (CaptureY >= offset * tileY){ CaptureY = 0; }
        }
        CaptureX += offset;
        if (CaptureX >= offset * tileX){ CaptureX = 0; }
    }
    //-----------------------------------------------------------------------------------------------------------------------------------------------
    for (int y = 2; y < tileY - 1; y += 2) {
        int dx = 2;
        int dy = y;
        int rnd4;


        switch (rnd4 = rand() % 4){
        case 0: dx++; break;
        case 1: dx--; break;
        case 2: dy++; break;
        case 3: dy--; break;
        }
        //if (bd.getPixel(dx, dy) != Status.WALL) {
        if (grid[dx][dy]->GetActorLabel() != "Block") {
            FVector f = grid[dx][dy]->GetActorLocation();
            grid[dx][dy]->Destroy();


            const FVector  GenSpawnLoc(f);
            const FRotator GenSpawnRot(0.0f, 0.0f, 0.0f);

            AStaticMeshActor* BlockTile = SpawnBP<AStaticMeshActor>(GetWorld(), TileBlockBP, GenSpawnLoc, GenSpawnRot);
            grid[dx][dy] = BlockTile;
        }
        else{
            y -= 2;
        }
    }
    for (int x = 4; x < tileX - 1; x += 2) {
        for (int y = 2; y < tileY - 1; y += 2) {
            int dx = x;
            int dy = y;
            int rnd3;

            switch (rnd3 = rand() % 3){
            case 0: dy++; break;
            case 1: dy--; break;
            case 2: dx++; break;
            }
            //if (bd.getPixel(dx, dy) != Status.WALL) {
            if (grid[dx][dy]->GetName() != "Block") {
                FVector f = grid[dx][dy]->GetActorLocation();
                grid[dx][dy]->Destroy();


                const FVector  GenSpawnLoc(f);
                const FRotator GenSpawnRot(0.0f, 0.0f, 0.0f);

                AStaticMeshActor* BlockTile = SpawnBP<AStaticMeshActor>(GetWorld(), TileBlockBP, GenSpawnLoc, GenSpawnRot);
                grid[dx][dy] = BlockTile;
            }
            else{
                y -= 2;
            }
        }
    }
}

[/]