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