Random Room Generator

Member
Posts: 102
Joined: 2005.01
Post: #1
i need to make a random room generator

i have two arrays

int board[20][20]; that hold what is in places

NSRect[20]20]; that holds the positions of all the squares.

i know i need to fill the entire board with walls, then take a random number of rooms with random size and location and fill those positions on the board with floor. then to connect them i need to pick random places on the rooms, and move horisontaly until they are on the same row, then move verticaly until they are on the same column, is that all, and how can i do that, i could probably figure it out by myself.
Quote this message in a reply
Moderator
Posts: 691
Joined: 2002.04
Post: #2
Well, you could randomly select which rooms are to be connected, and then use perhaps a variant of A* path-finding to create the corridors between them (although without a little more thought, you'll end up with corridors which intersect/overlap.)

Mark Bishop
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
i dont mind if they intersect, say it randomly picks 6 rooms and they all overlap thats fine. it would just be one big odd looking room, and randomly picking rooms to be connected is bad because it might leave a few a out, and it would be bad if that was the one with the objective in it

here is my code so far

Code:
- (void)randGenMap
{
    int numRooms = 3 + rand() % 6;
    int x, y;
    int v, h, v1, h1, v2, h2;
    NSRect roomRect[numRooms];
    for (x = 0 ; x < 20 ; ++ x)    {
        for (y = 0 ; y < 20 ; ++ y)    {
            board[x][y] = WALL;
        }
    }
}


that fills the entire board with Walls and makes an array with the number of rooms there will be, not exactly sure what to do next.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #4
Do you want all of your rooms to be rectangular? Are they tightly packed in the 20x20 grid?
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #5
yes i want them tightly packed, the 20x20 will be expanded later this is just for testing it.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #6
Then one way to create a bunch of rooms packed into a rectangle is to start with one big empty room with walls only on the border of your grid. Then randomly draw a line going from one side of your big room to the other. Now you have two rooms. Pick one of your new rooms and draw a line across it. Now you have three rooms. You can recursively divide your existing rooms into new, smaller rooms until you have as many as you desire. Then punch out doors in the walls to connect them all up.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
thats a good plan, i think ill do that one. Thanks.

started and i got an error in a weird place, never got an error doing this operation before

Code:
- (void)drawBoard
{
    NSRect modal;
    modal.origin = NSZeroPoint;
    int x, y;
    for (x = 0 ; x < 20 ; x++)       {
        for (y = 0 ; y < 20 ; y++)       {
            switch (board[x][y])    {
                case WALL:
                    modal.size = [wallImage size];
                    [wallImage drawInRect:board[x][y] fromRect:modal operation:NSCompositeSourceOver fraction:1];
                    break;
            }
        }
    }
}


its getting an error on the line


Code:
[wallImage drawInRect:board[x][y] fromRect:modal operation:NSCompositeSourceOver fraction:1];


error: incompatible type for argument 3 of indirect function call.

what?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Random game generator Najdorf 3 4,256 Nov 18, 2005 05:08 PM
Last Post: Najdorf
  game levels: hand design or random generator? Najdorf 15 5,765 Mar 31, 2003 04:55 PM
Last Post: Komick