-
Notifications
You must be signed in to change notification settings - Fork 0
/
PASSAGES.C
372 lines (354 loc) · 8.45 KB
/
PASSAGES.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Draw the connecting passages
*
* passages.c 1.4 (A.I. Design) 12/14/84
*/
#include "rogue.h"
#include "curses.h"
/*
* conn:
* Draw a corridor from a room in a certain direction.
*/
conn(r1, r2)
int r1, r2;
{
struct room *rpf, *rpt;
register int rmt, rm;
int distance, turn_spot, turn_distance, index;
int direc;
coord del, curr, turn_delta, spos, epos;
if (r1 < r2) {
rm = r1;
if (r1 + 1 == r2)
direc = 'r';
else
direc = 'd';
} else {
rm = r2;
if (r2 + 1 == r1)
direc = 'r';
else
direc = 'd';
}
rpf = &rooms[rm];
/*
* Set up the movement variables, in two cases:
* first drawing one down.
*/
if (direc == 'd') {
rmt = rm + 3; /* room # of dest */
rpt = &rooms[rmt]; /* room pointer of dest */
del.x = 0; /* direction of move */
del.y = 1;
/*
* If we are drawing from/to regular or maze rooms, we have
* to pick the spot we draw from/to
*/
if ((rpf->r_flags & ISGONE) == 0 || (rpf->r_flags & ISMAZE)) {
spos.y = rpf->r_pos.y + rpf->r_max.y - 1;
do {
spos.x = rpf->r_pos.x + rnd(rpf->r_max.x - 2) + 1;
} while (chat(spos.y,spos.x) == ' ');
} else {
spos.x = rpf->r_pos.x;
spos.y = rpf->r_pos.y;
}
epos.y = rpt->r_pos.y;
if ((rpt->r_flags & ISGONE) == 0 || (rpt->r_flags & ISMAZE)) {
do {
epos.x = rpt->r_pos.x + rnd(rpt->r_max.x - 2) + 1;
} while (chat(epos.y,epos.x) == ' ');
} else
epos.x = rpt->r_pos.x;
distance = abs(spos.y - epos.y) - 1; /* distance to move */
turn_delta.y = 0; /* direction to turn */
turn_delta.x = (spos.x < epos.x ? 1 : -1);
turn_distance = abs(spos.x - epos.x); /* how far to turn */
} else if (direc == 'r') { /* setup for moving right */
rmt = rm + 1;
rpt = &rooms[rmt];
del.x = 1;
del.y = 0;
if ((rpf->r_flags & ISGONE) == 0 || (rpf->r_flags & ISMAZE)) {
spos.x = rpf->r_pos.x + rpf->r_max.x-1;
do {
spos.y = rpf->r_pos.y + rnd(rpf->r_max.y-2)+1;
} while (chat(spos.y,spos.x) == ' ');
} else {
spos.x = rpf->r_pos.x;
spos.y = rpf->r_pos.y;
}
epos.x = rpt->r_pos.x;
if ((rpt->r_flags & ISGONE) == 0 || (rpt->r_flags & ISMAZE)) {
do {
epos.y = rpt->r_pos.y + rnd(rpt->r_max.y-2)+1;
} while (chat(epos.y, epos.x) == ' ');
} else
epos.y = rpt->r_pos.y;
distance = abs(spos.x - epos.x) - 1;
turn_delta.y = (spos.y < epos.y ? 1 : -1);
turn_delta.x = 0;
turn_distance = abs(spos.y - epos.y);
}
#ifdef DEBUG
else
debug("error in connection tables");
#endif
turn_spot = rnd(distance-1) + 1;
/*
* Draw in the doors on either side of the passage or just put #'s
* if the rooms are gone.
*/
if (!(rpf->r_flags & ISGONE))
door(rpf, &spos);
else
psplat(spos.y, spos.x);
if (!(rpt->r_flags & ISGONE))
door(rpt, &epos);
else
psplat(epos.y, epos.x);
/*
* Get ready to move...
*/
curr.x = spos.x;
curr.y = spos.y;
while (distance)
{
/*
* Move to new position
*/
curr.x += del.x;
curr.y += del.y;
/*
* Check if we are at the turn place, if so do the turn
*/
if (distance == turn_spot)
{
while (turn_distance--)
{
psplat(curr.y, curr.x);
curr.x += turn_delta.x;
curr.y += turn_delta.y;
}
}
/*
* Continue digging along
*/
psplat(curr.y, curr.x);
distance--;
}
curr.x += del.x;
curr.y += del.y;
if (!ce(curr, epos)) {
epos.x -= del.x;
epos.y -= del.y;
psplat(epos.y, epos.x);
}
}
/*
* do_passages:
* Draw all the passages on a level.
*/
do_passages()
{
register int i, j;
int roomcount;
static struct rdes
{
char conn[MAXROOMS]; /* possible to connect to room i? */
char isconn[MAXROOMS]; /* connection been made to room i? */
char ingraph; /* this room in graph already? */
} rdes[MAXROOMS] = {
{ { 0, 1, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 1, 0, 1, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 0, 1, 0, 0, 0, 1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 1, 0, 0, 0, 1, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 0, 1, 0, 1, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 0, 0, 1, 0, 1, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 0, 0, 0, 1, 0, 0, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 0, 0, 0, 0, 1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
{ { 0, 0, 0, 0, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 }
};
struct rdes *r1, *r2;
/*
* reinitialize room graph description
*/
for (r1 = rdes; r1 < &rdes[MAXROOMS]; r1++)
{
for (j = 0; j < MAXROOMS; j++)
r1->isconn[j] = FALSE;
r1->ingraph = FALSE;
}
/*
* starting with one room, connect it to a random adjacent room and
* then pick a new room to start with.
*/
roomcount = 1;
r1 = &rdes[rnd(MAXROOMS)];
r1->ingraph = TRUE;
do
{
/*
* find a room to connect with
*/
j = 0;
for (i = 0; i < MAXROOMS; i++)
if (r1->conn[i] && !rdes[i].ingraph && rnd(++j) == 0)
r2 = &rdes[i];
/*
* if no adjacent rooms are outside the graph, pick a new room
* to look from
*/
if (j == 0)
{
do
r1 = &rdes[rnd(MAXROOMS)];
while (!r1->ingraph);
}
/*
* otherwise, connect new room to the graph, and draw a tunnel
* to it
*/
else
{
r2->ingraph = TRUE;
i = r1 - rdes;
j = r2 - rdes;
conn(i, j);
r1->isconn[j] = TRUE;
r2->isconn[i] = TRUE;
roomcount++;
}
} while (roomcount < MAXROOMS);
/*
* attempt to add passages to the graph a random number of times so
* that there isn't always just one unique passage through it.
*/
for (roomcount = rnd(5); roomcount > 0; roomcount--)
{
r1 = &rdes[rnd(MAXROOMS)]; /* a random room to look from */
/*
* find an adjacent room not already connected
*/
j = 0;
for (i = 0; i < MAXROOMS; i++)
if (r1->conn[i] && !r1->isconn[i] && rnd(++j) == 0)
r2 = &rdes[i];
/*
* if there is one, connect it and look for the next added
* passage
*/
if (j != 0)
{
i = r1 - rdes;
j = r2 - rdes;
conn(i, j);
r1->isconn[j] = TRUE;
r2->isconn[i] = TRUE;
}
}
passnum();
}
/*
* door:
* Add a door or possibly a secret door. Also enters the door in
* the exits array of the room.
*/
door(rm, cp)
struct room *rm;
coord *cp;
{
register int index, xit;
index = INDEX(cp->y, cp->x);
if (rnd(10) + 1 < level && rnd(5) == 0)
{
_level[index] = (cp->y == rm->r_pos.y || cp->y == rm->r_pos.y + rm->r_max.y - 1) ? HWALL : VWALL;
_flags[index] &= ~F_REAL;
}
else
_level[index] = DOOR;
xit = rm->r_nexits++;
rm->r_exit[xit].y = cp->y;
rm->r_exit[xit].x = cp->x;
}
#ifdef WIZARD
/*
* add_pass:
* Add the passages to the current window (wizard command)
*/
add_pass()
{
register int y, x, ch;
for (y = 1; y < maxrow; y++)
for (x = 0; x < COLS; x++)
if ((ch = chat(y, x)) == DOOR || ch == PASSAGE)
mvaddch(y, x, ch);
}
#endif
/*
* passnum:
* Assign a number to each passageway
*/
static int pnum;
static byte newpnum;
passnum()
{
register struct room *rp;
register int i;
pnum = 0;
newpnum = FALSE;
for (rp = passages; rp < &passages[MAXPASS]; rp++)
rp->r_nexits = 0;
for (rp = rooms; rp < &rooms[MAXROOMS]; rp++)
for (i = 0; i < rp->r_nexits; i++)
{
newpnum++;
numpass(rp->r_exit[i].y, rp->r_exit[i].x);
}
}
/*
* numpass:
* Number a passageway square and its brethren
*/
numpass(y, x)
int y, x;
{
register byte *fp;
register struct room *rp;
register byte ch;
if (offmap(y,x))
return;
fp = &flat(y, x);
if (*fp & F_PNUM)
return;
if (newpnum) {
pnum++;
newpnum = FALSE;
}
/*
* check to see if it is a door or secret door, i.e., a new exit,
* or a numerable type of place
*/
if ((ch = chat(y, x)) == DOOR || (!(*fp & F_REAL) && ch != FLOOR)) {
rp = &passages[pnum];
rp->r_exit[rp->r_nexits].y = y;
rp->r_exit[rp->r_nexits++].x = x;
} else if (!(*fp & F_PASS))
return;
*fp |= pnum;
/*
* recurse on the surrounding places
*/
numpass(y + 1, x);
numpass(y - 1, x);
numpass(y, x + 1);
numpass(y, x - 1);
}
psplat(y, x)
shint y, x;
{
register int idx;
_level[idx = INDEX(y, x)] = PASSAGE;
_flags[idx] |= F_PASS;
}