Use ^ rather than & so the player can't be generated 'too boring' for any monster...
[rogue-pphs.git] / passages.c
1 /*
2 * Draw the connecting passages
3 *
4 * @(#)passages.c 3.4 (Berkeley) 6/15/81
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include "curses.h"
14 #include "rogue.h"
15
16 /*
17 * do_passages:
18 * Draw all the passages on a level.
19 */
20
21 do_passages()
22 {
23 register struct rdes *r1, *r2;
24 register int i, j;
25 register int roomcount;
26 static struct rdes
27 {
28 bool conn[MAXROOMS]; /* possible to connect to room i? */
29 bool isconn[MAXROOMS]; /* connection been made to room i? */
30 bool ingraph; /* this room in graph already? */
31 } rdes[MAXROOMS] = {
32 { { 0, 1, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
33 { { 1, 0, 1, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
34 { { 0, 1, 0, 0, 0, 1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
35 { { 1, 0, 0, 0, 1, 0, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
36 { { 0, 1, 0, 1, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
37 { { 0, 0, 1, 0, 1, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
38 { { 0, 0, 0, 1, 0, 0, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
39 { { 0, 0, 0, 0, 1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
40 { { 0, 0, 0, 0, 0, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0 },
41 };
42
43 /*
44 * reinitialize room graph description
45 */
46 for (r1 = rdes; r1 <= &rdes[MAXROOMS-1]; r1++)
47 {
48 for (j = 0; j < MAXROOMS; j++)
49 r1->isconn[j] = FALSE;
50 r1->ingraph = FALSE;
51 }
52
53 /*
54 * starting with one room, connect it to a random adjacent room and
55 * then pick a new room to start with.
56 */
57 roomcount = 1;
58 r1 = &rdes[rnd(MAXROOMS)];
59 r1->ingraph = TRUE;
60 do
61 {
62 /*
63 * find a room to connect with
64 */
65 j = 0;
66 for (i = 0; i < MAXROOMS; i++)
67 if (r1->conn[i] && !rdes[i].ingraph && rnd(++j) == 0)
68 r2 = &rdes[i];
69 /*
70 * if no adjacent rooms are outside the graph, pick a new room
71 * to look from
72 */
73 if (j == 0)
74 {
75 do
76 r1 = &rdes[rnd(MAXROOMS)];
77 until (r1->ingraph);
78 }
79 /*
80 * otherwise, connect new room to the graph, and draw a tunnel
81 * to it
82 */
83 else
84 {
85 r2->ingraph = TRUE;
86 i = r1 - rdes;
87 j = r2 - rdes;
88 conn(i, j);
89 r1->isconn[j] = TRUE;
90 r2->isconn[i] = TRUE;
91 roomcount++;
92 }
93 } while (roomcount < MAXROOMS);
94
95 /*
96 * attempt to add passages to the graph a random number of times so
97 * that there isn't just one unique passage through it.
98 */
99 for (roomcount = rnd(5); roomcount > 0; roomcount--)
100 {
101 r1 = &rdes[rnd(MAXROOMS)]; /* a random room to look from */
102 /*
103 * find an adjacent room not already connected
104 */
105 j = 0;
106 for (i = 0; i < MAXROOMS; i++)
107 if (r1->conn[i] && !r1->isconn[i] && rnd(++j) == 0)
108 r2 = &rdes[i];
109 /*
110 * if there is one, connect it and look for the next added
111 * passage
112 */
113 if (j != 0)
114 {
115 i = r1 - rdes;
116 j = r2 - rdes;
117 conn(i, j);
118 r1->isconn[j] = TRUE;
119 r2->isconn[i] = TRUE;
120 }
121 }
122 }
123
124 /*
125 * conn:
126 * Draw a corridor from a room in a certain direction.
127 */
128
129 conn(r1, r2)
130 int r1, r2;
131 {
132 register struct room *rpf, *rpt;
133 register char rmt;
134 register int distance, turn_spot, turn_distance;
135 register int rm;
136 register char direc;
137 coord delta, curr, turn_delta, spos, epos;
138
139 if (r1 < r2)
140 {
141 rm = r1;
142 if (r1 + 1 == r2)
143 direc = 'r';
144 else
145 direc = 'd';
146 }
147 else
148 {
149 rm = r2;
150 if (r2 + 1 == r1)
151 direc = 'r';
152 else
153 direc = 'd';
154 }
155 rpf = &rooms[rm];
156 /*
157 * Set up the movement variables, in two cases:
158 * first drawing one down.
159 */
160 if (direc == 'd')
161 {
162 rmt = rm + 3; /* room # of dest */
163 rpt = &rooms[rmt]; /* room pointer of dest */
164 delta.x = 0; /* direction of move */
165 delta.y = 1;
166 spos.x = rpf->r_pos.x; /* start of move */
167 spos.y = rpf->r_pos.y;
168 epos.x = rpt->r_pos.x; /* end of move */
169 epos.y = rpt->r_pos.y;
170 if (!(rpf->r_flags & ISGONE)) /* if not gone pick door pos */
171 {
172 spos.x += rnd(rpf->r_max.x-2)+1;
173 spos.y += rpf->r_max.y-1;
174 }
175 if (!(rpt->r_flags & ISGONE))
176 epos.x += rnd(rpt->r_max.x-2)+1;
177 distance = abs(spos.y - epos.y) - 1; /* distance to move */
178 turn_delta.y = 0; /* direction to turn */
179 turn_delta.x = (spos.x < epos.x ? 1 : -1);
180 turn_distance = abs(spos.x - epos.x); /* how far to turn */
181 turn_spot = rnd(distance-1) + 1; /* where turn starts */
182 }
183 else if (direc == 'r') /* setup for moving right */
184 {
185 rmt = rm + 1;
186 rpt = &rooms[rmt];
187 delta.x = 1;
188 delta.y = 0;
189 spos.x = rpf->r_pos.x;
190 spos.y = rpf->r_pos.y;
191 epos.x = rpt->r_pos.x;
192 epos.y = rpt->r_pos.y;
193 if (!(rpf->r_flags & ISGONE))
194 {
195 spos.x += rpf->r_max.x-1;
196 spos.y += rnd(rpf->r_max.y-2)+1;
197 }
198 if (!(rpt->r_flags & ISGONE))
199 epos.y += rnd(rpt->r_max.y-2)+1;
200 distance = abs(spos.x - epos.x) - 1;
201 turn_delta.y = (spos.y < epos.y ? 1 : -1);
202 turn_delta.x = 0;
203 turn_distance = abs(spos.y - epos.y);
204 turn_spot = rnd(distance-1) + 1;
205 }
206 else
207 fatal("error in connection tables");
208 /*
209 * Draw in the doors on either side of the passage or just put #'s
210 * if the rooms are gone.
211 */
212 if (!(rpf->r_flags & ISGONE)) door(rpf, &spos);
213 else
214 {
215 cmov(spos);
216 addch('#');
217 }
218 if (!(rpt->r_flags & ISGONE)) door(rpt, &epos);
219 else
220 {
221 cmov(epos);
222 addch('#');
223 }
224 /*
225 * Get ready to move...
226 */
227 curr.x = spos.x;
228 curr.y = spos.y;
229 while(distance)
230 {
231 /*
232 * Move to new position
233 */
234 curr.x += delta.x;
235 curr.y += delta.y;
236 /*
237 * Check if we are at the turn place, if so do the turn
238 */
239 if (distance == turn_spot && turn_distance > 0)
240 while(turn_distance--)
241 {
242 cmov(curr);
243 addch(PASSAGE);
244 curr.x += turn_delta.x;
245 curr.y += turn_delta.y;
246 }
247 /*
248 * Continue digging along
249 */
250 cmov(curr);
251 addch(PASSAGE);
252 distance--;
253 }
254 curr.x += delta.x;
255 curr.y += delta.y;
256 if (!ce(curr, epos))
257 msg("Warning, connectivity problem on this level.");
258 }
259
260 /*
261 * Add a door or possibly a secret door
262 * also enters the door in the exits array of the room.
263 */
264
265 door(rm, cp)
266 register struct room *rm;
267 register coord *cp;
268 {
269 cmov(*cp);
270 addch( (rnd(10) < level - 1 && rnd(100) < 20 ? SECRETDOOR : DOOR) );
271 rm->r_exit[rm->r_nexits++] = *cp;
272 }
273
274 /*
275 * add_pass:
276 * add the passages to the current window (wizard command)
277 */
278
279 add_pass()
280 {
281 register int y, x, ch;
282
283 for (y = 1; y < LINES - 2; y++)
284 for (x = 0; x < COLS; x++)
285 if ((ch=mvinch(y, x)) == PASSAGE || ch == DOOR || ch == SECRETDOOR)
286 mvwaddch(cw, y, x, ch);
287 }