Original Post
Hello. I've been screwing around with trying to make my own roguelike for a couple of weeks, on and off. I have a good event/message system and display going on, but I've become stuck at generating the dungeon. I've tried BSP trees, cellular automata (cave like), and a few others. I found the BSP trees to be good (using that to generate rooms), but I didn't like the corridors produced. My 'current' method of producing corridors is to: Go through each room (have a vector of them), and connect it to another random room. This should make everything connected, but it's failing for some reason. Generate a number of corridors between random rooms. I generate corridors by: (in pseudocode. My code is hard to read. D:)
This is what is generated. Some of the rooms aren't connected, however when I generate corridors between rooms I set the isConnected of both rooms to true. I've checked, and everything *is* connected, but the corridors have not joined. So I think it's an algorithm issue, but I can't see why. Thank you very much for your time GD'ers. -Mark Here's my proper code, if you can read it. (sorry for the lack of comments, pseudo might be easier) [Edited by - The Communist Duck on May 22, 2010 5:17:45 AM]
dy = first.y - second.y
dx = first.x - second.x
remainingY = dy
remainingX = dx
curX = first.centre.x
curY = first.centre.y //start at the centre of the room
//Distance to travel
container = abs(dx) + abs(dy)
if(dx == 0 or dy is 0)
just move straight up or straight across
while(remainingY and remainingX and container are not 0)
if(random between 1 and container <= remainingY) //proportional and random
//move in y
make this tile a corridor tile
remainingY--; container--; curY -- or ++ depending if dy is positive or negative
else
make this tile a corridor tile
remainingX--; container--; curX -- or ++ depending if dx is positive or negative
end while
This is what is generated. Some of the rooms aren't connected, however when I generate corridors between rooms I set the isConnected of both rooms to true. I've checked, and everything *is* connected, but the corridors have not joined. So I think it's an algorithm issue, but I can't see why. Thank you very much for your time GD'ers. -Mark Here's my proper code, if you can read it. (sorry for the lack of comments, pseudo might be easier)
int8 dy = two->GetCentre().y - one->GetCentre().y;
int8 dx = two->GetCentre().x - one->GetCentre().x;
int8 remDX = dx, remDY = dy; //How much to move.
uint16 container = std::abs(dy) + std::abs(dx);
//First dy values are dy, and the rest are dx. Helps for proportionate
//Random number generation without needing integration.
Point point(one->GetCentre());//Start
Point finish(two->GetCentre());//end
std::cout << "foo";
while(remDX != 0 && remDY != 0 && container != 0)//While we need to move
{
if(dy == 0 || dx == 0) //Straight across or straight down; rare.
{
while(point != finish)//until we hit finish.
{
level->ReplaceTile(point.x, point.y, new FloorTile(point.x, point.y));
if(dy == 0) { point.x++; }
if(dx == 0) { point.y++; }
}
remDX = 0; remDY = 0; //Hack.
}
else if(random->Random(1, container) <= remDY)
{
if(dy > 0) //We're moving in Y. This sees if it's up or down (true == down)
{
if(level->GetTile(point.x, point.y)->id != FLOOR)
level->ReplaceTile(point.x, point.y, new FloorTile(point.x, point.y));
point.y++;
remDY--; container--;
}
else if(dy < 0)
{
if(level->GetTile(point.x, point.y)->id != FLOOR) // move up.
level->ReplaceTile(point.x, point.y, new FloorTile(point.x, point.y));
point.y--;
remDY--; container--;
}
}
else //X moving.
{
if(dx > 0) //We're moving in X. This sees if it's left or right (true == right)
{
if(level->GetTile(point.x, point.y)->id != FLOOR)
level->ReplaceTile(point.x, point.y, new FloorTile(point.x, point.y));
point.x++;
remDX--; container--;
}
else if(dx < 0)
{
if(level->GetTile(point.x, point.y)->id != FLOOR) // move left
level->ReplaceTile(point.x, point.y, new FloorTile(point.x, point.y));
point.x--;
remDX--; container--;
}
}
one->isConnected = true;
two->isConnected = true;
}