Strange random dungeon generation wrapping

I wrote a very simple dungeon generator today. I am having a strange issue where if a room goes too high or low, it wraps to the bottom or top. I have no idea at all why this would happen, as I error check to make sure its not too high/low, and the result is out of the bounds of the array. Also, to make things even weirder, it only happens vertically. Any thoughts?

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using namespace std;

#include <iostream>
#include <ctime>
#include <fstream>

//Constants
const int cwidth = 140;
const int cheight = 70;

//Enumerations
enum DIRECTION {
	D_NULL = -1,
	LEFT = 0,	
	UP,
	RIGHT,
	DOWN
};

//Map Array
int map[cwidth][cheight];

//The file
fstream map_file;

//Functions
int random (int i);
void fill (int x, int y, int w, int h, int i);
void show (void);
bool collision (int x, int y, int w, int h);

void randomize_map (int room_size_min, int room_size_max, int room_count, int hallway_max);

int main(int argc, char **argv)
{
    srand((unsigned)time(0));

	start:

	randomize_map(2, 20, 40, 5);

	show();

	int test;

	cin >> test;

	goto start;
}

int random (int n)
{
	if (n == 0)
		return 0;

	return (rand() % n) + 1;
}

void fill (int x, int y, int w, int h, int i)
{
	for (int dy = y; dy < h + y; dy++)
	{
		for (int dx = x; dx < w + x; dx++)
		{
			map[dx][dy] = i;
		}
	}
}

void show (void)
{
	for (int y = 0; y < cheight; y++)
	{
		for (int x = 0; x < cwidth; x++)
		{
			cout << map[x][y];
		}
		cout << '\n';
	}
}

bool collision (int x, int y, int w, int h)
{
	for (int dy = y - 1; dy <= h + y + 1; dy++)
	{
		for (int dx = x - 1; dx <= w + x + 1; dx++)
		{
			if (map[dx][dy] != 0)
				return true;
		}
	}
	return false;
}

void randomize_map (int room_size_min, int room_size_max, int room_count, int hallway_max)
{
	////Create the basic map
	fill(0, 0, cwidth, cheight, 0);

	//Create the first room
	fill(((cwidth / 2) - (room_size_max / 2)), ((cheight / 2) - (room_size_max / 2)), room_size_max, room_size_max, 1);

	unsigned int q = 0;

	for (int i = 1; i < room_count; i)
	{
		DIRECTION direction;
		bool loop = true;

		direction = D_NULL;

		int rw = room_size_min + random(room_size_max - room_size_min);
		if (rw < 2) rw = 2;
		int rh = room_size_min + random(room_size_max - room_size_min);
		if (rh < 2) rh = 2;

		int rx = random(cwidth);
		int ry = random(cheight);

		int offset_x = random(rw - 1);
		int offset_y = random(rh - 1);
		int hallway = random(hallway_max);

		if (map[rx][ry] == 1)
		{

			if (map[rx - 1][ry] == 0)
				direction = LEFT;
			else if (map[rx][ry - 1] == 0)
				direction = UP;
			else if (map[rx + 1][ry] == 0)
				direction = RIGHT;
			else if (map[rx][ry + 1] == 0)
				direction = DOWN;

I think this is where the problem occurs:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
			switch (direction)
			{
			case LEFT:
				if (rx - hallway - rw <= 0 || ry - offset_y <= 0)
					break;

				if (collision(rx - hallway - rw, ry - offset_y, rw, rh))
					break;

				fill(rx - hallway - rw, ry - offset_y, rw, rh, 1);

				fill(rx - hallway, ry, hallway, 1, 1);

				i++;
				break;

			case UP:
				if (rx - offset_x <= 0 || ry - hallway - rh <= 0)
					break;

				if (collision(rx - offset_x, ry - hallway - rh, rw, rh))
					break;
				
				fill(rx - offset_x, ry - hallway - rh, rw, rh, 1);

				fill(rx, ry - hallway, 1, hallway, 1);

				i++;
				break;

			case RIGHT:
				if (rx + hallway + rw >= cwidth || ry + offset_y >= cheight)
					break;
					
				if (collision(rx + hallway, ry - offset_y, rw, rh))
					break;

				fill(rx + hallway, ry - offset_y, rw, rh, 1);

				fill(rx, ry, hallway, 1, 1);

				i++;
				break;

			case DOWN:
				if (rx + offset_x >= cwidth || ry + hallway + rh >= cheight)
					break;

				if (collision(rx - offset_x, ry + hallway, rw, rh))
					break;
				
				fill(rx - offset_x, ry + hallway, rw, rh, 1);

				fill(rx, ry, 1, hallway, 1);

				i++;
				break;
			}
		}
		q++;
		if (q > 1000000000)
			i = room_count;
	}
}
Last edited on
Are you sure your output window is at least 140 characters wide?
Yes, my problem is that the array is having a block of 1's filled from the bottom to the top, having a wrap effect. I am guessing it has something to do with the compiler handling out of bounds errors with my map array.
Topic archived. No new replies allowed.