2

I have an SKTileMapNode that I am filling with rooms using a BSP algorithm I translated into Swift from this tutorial. The fully translated algorithm can be found on my github repo

I have a custom class that gives me four corner positions of a room. I use the four corner positions to fill everything in the room like so:

for row in 0..<tileMap.numberOfColumns {
    for column in 0..<tileMap.numberOfRows {
        for room in rooms {
            // iterate through each room and draw it in
            if room.minX <= column && room.maxX >= column && room.minY <= row && room.maxY >= row {
                tileMap.setTileGroup(tileGroup2, forColumn: column, row: row)
            }
        }
        for hallway in hallways {
            // iterate through each hallway and draw it in
            if hallway.minX <= column && hallway.maxX >= column && hallway.minY <= row && hallway.maxY >= row {
                tileMap.setTileGroup(tileGroup2, forColumn: column, row: row)
            }
        }    
    }
}

Where hallways and room are both arrays of my Room class that holds the four corner positions.

class Room {
    var minX:Int
    var maxX:Int
    var minY:Int
    var maxY:Int
    var center:CGPoint

    init(X: Int, Y: Int, W: Int, H: Int) {
        minX = X
        maxX = X + W
        minY = Y
        maxY = Y + H
        center = CGPoint(x: (minX + maxX) / 2, y: (minY + maxY) / 2)
    }
}

I know how to fill everything inside the rooms, but how can I fill everything else outside the rooms? I can't seem to figure it out. And doing if room.minX >= column && room.maxX <= column && room.minY >= row && room.maxY <= row always fails.

I need to fill everything that "leaks" outside the rooms because Apples SKTileMapNode auto-tiling is buggy and I need to "smooth" it out.

Examples of the buggy auto-tiling:

Image 1 Image 2

What they should be:

Image 3 Image 4

My thought process, because the auto-tiling doesn't work, is to fill everything that leaks outside the rooms and hallways with the black tiles. Hopefully, that should force the auto-tiling algorithm to properly place the tiles as 4-sided polygons. I can't figure out how to fill everything outside the rooms, however. My alternative option is to make an auto-tiling algorithm myself. I would like to avoid that because it would take days to make and it would be a hassle.

E. Huckabee
  • 1,788
  • 1
  • 13
  • 29
  • A suggestion - renaming your variables as `row`, `column` `room`, as opposed to `r`, `c`, `i` might make it easer to see what's happening. Also, `x1` and `x2` look like they should be `minX` and `maxX`, etc. Or better yet, give a room a `let rect: CGRect` – Ashley Mills Oct 10 '18 at 09:14
  • x1 would be the x value of the corner on the left. x2, the x value of the rightmost corner, is calculated by adding the width of the room to x1. the y values are the same as the x respectively, but y axis instead of x axis. I originally adapted the `Room` class from another article. It never occurred to me to rename the values. I always just knew what they were. – E. Huckabee Oct 10 '18 at 09:24
  • Renaming would also make it clearer for other people who don't necessarily have your knowledge. Probably irrelevant to the problem at hand though. – Ashley Mills Oct 10 '18 at 09:28
  • Also, I didn't use CGRects because I don't like them. They are confusing and don't provide all the necessary data like my `Room` class does. I should probably figure them out, but not for this BSP algorithm as `Room` is used to make some crucial calculations other places in the algorithm that CGRects cannot make. The question has been changed based off of your suggestions. – E. Huckabee Oct 10 '18 at 09:28

0 Answers0