简体   繁体   中英

Room placement in a maze using Prim's algorithm

I am attempting to place rooms on an ASCII screen, and then use Prim's algorithm to "fill" the space between rooms with maze, but without actually breaking into the rooms. I have been tinkering for a few hours, and I can't figure out a way to stop my algorithm from breaking into my rooms.

Can anyone help me? I'm pretty lost. I'm practicing map generation techniques, and this is my 5th one I'm on. No, I don't want to do it another way, I simply want to do it this way - but right.

Below is both a photo of my current output with rooms, my current output without rooms, and a link to the relevant source code (AKA the Prim's algorithm section). Thanks again if you can actually help me!

Note: All the opposite method does is figure out which cell the "parent" cell is, and determine direction based off of that. So if parent cell's x value is 7, and the child's x value is 6, then it know that's the new child.

start = new Point(x,y, null);
map[start.x][start.y] = Tile.STAIRS_DOWN;

for(int nx = -1; nx <= 1; nx++){
    for(int ny = -1; ny <= 1; ny++){
        if((nx == 0 && ny == 0) || (nx != 0 && ny != 0)){
            continue;
        }
        try{
            if(map[start.x + nx][start.y + ny] == Tile.FLOOR){
                continue;
            }
            frontier.add(new Point(start.x+nx, start.y + ny, start));
        }
        catch(Exception e){
            continue;
        }
    }
}

Point last = null;
while(!frontier.isEmpty()){
    Point cu = frontier.remove(RandomGen.rand(0, frontier.size() - 1));
    Point op = cu.opposite();
    try{
        if((map[cu.x][cu.y] == Tile.WALL) && (map[op.x][op.y] == Tile.WALL)){
            for (int bx = -1; bx <= 1; bx++)
                for (int by = -1; by <= 1; by++) {
                    boolean failed = false;
                    if (bx == 0 && by == 0 || bx != 0 && by != 0)
                        continue;
                    try {
                        if(map[op.x + bx][op.y + by] == Tile.FLOOR){
                            break;
                        }
                        last = op;
                        if(!failed){
                        map[cu.x][cu.y] = Tile.FLOOR;
                        map[op.x][op.y] = Tile.FLOOR;
                        frontier.add(new Point(op.x + bx, op.y + by, op));
                        }
                    }
                    catch(Exception e){
                        continue;
                    }
                }
        }
    }
    catch(Exception e){}
}

Without Rooms

With Rooms

Solved: I needed to check my forward facing corners for open spaces. So if I'm going "east" then I need to check Northeast and Southeast tiles as well, or else I will potentially burrow into the rooms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM