-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAZE.C
177 lines (160 loc) · 3.63 KB
/
MAZE.C
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Maze drawing routines. Based on the algorithm presented in the
* December 1981 Byte "How to Build a Maze" by David Matuszek.
*
* maze.c 1.4 (A.I. Design) 12/14/84
*/
#include "rogue.h"
#include "curses.h"
extern int maxrow;
#define MAXFRNT 100
#define FRONTIER 'F'
#define NOTHING ' '
static shint frcnt, ny, nx, topy, topx;
static shint maxx, maxy;
static shint *fr_y, *fr_x;
draw_maze(rp)
struct room *rp;
{
register int y, x;
shint fy[MAXFRNT], fx[MAXFRNT];
int psgcnt;
coord spos;
fr_y = fy;
fr_x = fx;
maxx = maxy = 0;
topy = rp->r_pos.y;
if (topy == 0)
topy = ++rp->r_pos.y;
topx = rp->r_pos.x;
/*
* Choose a random spot in the maze and initialize the frontier
* to be the immediate neighbors of this random spot.
*/
y = topy;
x = topx;
splat(y,x);
new_frontier(y, x);
/*
* While there are new frontiers, connect them to the path and
* possibly expand the frontier even more.
*/
while(frcnt)
{
con_frnt();
new_frontier(ny, nx);
}
/*
* According to the Grand Beeking, every maze should have a loop
* Don't worry if you don't understand this.
*/
rp->r_max.x = maxx - rp->r_pos.x + 1;
rp->r_max.y = maxy - rp->r_pos.y + 1;
do {
static coord ld[4] = {
-1, 0,
0, 1,
1, 0,
0, -1
};
coord *cp;
int sh;
rnd_pos(rp, &spos);
for (psgcnt = 0,cp = ld,sh = 1; cp < &ld[4]; sh <<= 1,cp++) {
y = cp->y + spos.y; x = cp->x + spos.x;
if (!offmap(y, x) && chat(y, x) == PASSAGE)
psgcnt += sh;
}
} while (chat(spos.y, spos.x) == PASSAGE || psgcnt % 5);
splat(spos.y, spos.x);
}
new_frontier(y, x)
int y, x;
{
add_frnt(y-2, x);
add_frnt(y+2, x);
add_frnt(y, x-2);
add_frnt(y, x+2);
}
add_frnt(y, x)
int y, x;
{
#ifdef DEBUG
if (frcnt == MAXFRNT - 1)
debug("MAZE DRAWING ERROR #3\n");
#endif
if (inrange(y, x) && chat(y, x) == NOTHING)
{
chat(y, x) = FRONTIER;
fr_y[frcnt] = y;
fr_x[frcnt++] = x;
}
}
/*
* Connect randomly to one of the adjacent points in the spanning tree
*/
con_frnt()
{
register int n, which, ydelt = 0, xdelt = 0;
int choice[4];
int cnt = 0, y, x;
/*
* Choose a random frontier
*/
n = rnd(frcnt);
ny = fr_y[n];
nx = fr_x[n];
fr_y[n] = fr_y[frcnt-1];
fr_x[n] = fr_x[--frcnt];
/*
* Count and collect the adjacent points we can connect to
*/
if (maze_at(ny-2, nx) > 0)
choice[cnt++] = 0;
if (maze_at(ny+2, nx) > 0)
choice[cnt++] = 1;
if (maze_at(ny, nx-2) > 0)
choice[cnt++] = 2;
if (maze_at(ny, nx+2) > 0)
choice[cnt++] = 3;
/*
* Choose one of the open places, connect to it and
* then the task is complete
*/
which = choice[rnd(cnt)];
splat(ny, nx);
switch(which)
{
when 0: which = 1; ydelt = -1;
when 1: which = 0; ydelt = 1;
when 2: which = 3; xdelt = -1;
when 3: which = 2; xdelt = 1;
}
y = ny + ydelt;
x = nx + xdelt;
if (inrange(y, x))
splat(y, x);
}
maze_at(y, x)
{
if (inrange(y, x) && chat(y, x) == PASSAGE)
return 1;
else
return 0;
}
splat(y, x)
{
chat(y, x) = PASSAGE;
flat(y, x) = F_MAZE|F_REAL;
if (x > maxx)
maxx = x;
if (y > maxy)
maxy = y;
}
#define MAXY (topy+((maxrow+1)/3))
#define MAXX (topx+COLS/3)
inrange(y, x)
int x, y;
{
return(y >= topy && y < MAXY && x >= topx && x < MAXX);
}