Room generation with paths inbetween

So I have room generation working on a grid basis(plotted in a random grid(layout)). Now I am just stuck on getting the paths to connect properly. On the creation of the room position it stores it in two arrays of roomX[] and roomY[](for small and big rooms).

roomSize is the equivalent of grid size. (for a small grid and a big grid)

i pass the roomX and Y into for a function which checks them against one main room.

from here the check between two positions function should take the x and y values of two rooms. determine the left most and top most and plot a path accordingly. (it doesn't matter if it passes through other rooms)
It plots the path by changing values in the random gird.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

void tileMap:: placePaths(int roomSizeNum)
{
	for(int i = 0; i <= roomSizeNum; i++)
	{
		checkBetweenTwoPos(smallRoomX[i], smallRoomY[i], smallRoomX[0], smallRoomY[0]);
		checkBetweenTwoPos(bigRoomX[i], bigRoomY[i], smallRoomX[0], smallRoomY[0]);
	}

}
void tileMap:: checkBetweenTwoPos(int x1, int y1, int x2, int y2)
{
	int leftX, rightX, topY, bottomY;
	
	if(x1 <= x2) //////////////////////////////////////////////////////////////////////////// fix this
	{
		leftX = x1;
		rightX = x2;
	}
	else if (x1 > x2)
	{
		leftX = x2;
		rightX = x1;
	}
	if(y1 <= y2)
	{
		topY = y1;
		bottomY = y2;
	}
	else if (y1 > y2)
	{
		topY = y2;
		bottomY = y1;
	}
	
	while(leftX <= rightX)
	{
		
		randomLayout[topY][leftX] = 1;
		leftX++;
	
	}
	while(topY <= bottomY)
	{	
		
		randomLayout[topY][leftX] = 1;
		topY++;
	}
	
}


i can't seem to get this to function and any advice would be great.
Thanks
What exactly is wrong? Does it do nothing? Does it crash your program? Does it cause you to have to pay higher taxes? You need to be more specific.
Of course. My bad.

It works to some extent. And generally it will connect most rooms.
But rooms closer to the bottom of the grid can often be left out and have a random path drawn elsewhere.(the path would end on the same y axis as the room but not the correct x, or at least i think that is what is going on)

http://i877.photobucket.com/albums/ab340/zecbmo/asfdjasfd.png

the ones on the right are the rooms being generated

It doesn't crash and I don't get any errors.
Last edited on
Line 20: this should just be an else, not an else-if
Line 30: ditto

On line 4, you have <= instead of < so you may be having memory corruption issues.
Thanks LB for your help. After a while debugging I found the solution and the function was poorly written.

I changed it and now everything works as it should. This will help anyone trying to plot a straight path between points

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 	int i;
	int j;
	
	if(x1 <= x2 && y1 <= y2)
	{
		for(i = x1; i <= x2; i++)
		{
			randomLayout[y1][i] = 1;
		}
		for(j = y1; j <= y2; j++)
		{
			randomLayout[j][x2] = 1;
		}
	}
	else if(x1 >= x2 && y1 <= y2)
	{
		for(i = x2; i <= x1; i++)
		{
			randomLayout[y1][i] = 1;
		}
		for(j = y1; j <= y2; j++)
		{
			randomLayout[j][x2] = 1;
		}
	}
		
	else if(x1 <= x2 && y1 >= y2)
	{
		for(i = x1; i <= x2; i++)
		{
			randomLayout[y1][i] = 1;
		}
		for(j = y2; j <= y1; j++)
		{
			randomLayout[j][x2] = 1;
		}
	}
	else if(x1 >= x2 && y1 >= y2)
	{
		for(i = x2; i <= x1; i++)
		{
			randomLayout[y1][i] = 1;
		}
		for(j = y2; j <= y1; j++)
		{
			randomLayout[j][x2] = 1;
		}
	}



I will put this one down to inexperience!
Topic archived. No new replies allowed.