]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rogue/room.c
Comprehensive (or at least extensive) string handling cleanup for rogue.
[bsdgames-darwin.git] / rogue / room.c
1 /* $NetBSD: room.c,v 1.10 2007/12/27 23:53:01 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Timothy C. Stoehr.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)room.c 8.1 (Berkeley) 5/31/93";
39 #else
40 __RCSID("$NetBSD: room.c,v 1.10 2007/12/27 23:53:01 dholland Exp $");
41 #endif
42 #endif /* not lint */
43
44 /*
45 * room.c
46 *
47 * This source herein may be modified and/or distributed by anybody who
48 * so desires, with the following restrictions:
49 * 1.) No portion of this notice shall be removed.
50 * 2.) Credit shall not be taken for the creation of this source.
51 * 3.) This code is not to be traded, sold, or used for personal
52 * gain or profit.
53 *
54 */
55
56 #include "rogue.h"
57
58 room rooms[MAXROOMS];
59 boolean rooms_visited[MAXROOMS];
60
61 #define NOPTS 7
62
63 const struct option {
64 const char *prompt;
65 boolean is_bool;
66 char **strval;
67 boolean *bval;
68 } options[NOPTS] = {
69 {
70 "Show position only at end of run (\"jump\"): ",
71 1, (char **) 0, &jump
72 },
73 {
74 "Follow turnings in passageways (\"passgo\"): ",
75 1, (char **) 0, &passgo
76 },
77 {
78 "Don't print skull when killed (\"noskull\" or \"notombstone\"): ",
79 1, (char **) 0, &no_skull
80 },
81 {
82 "Ask player before saying 'Okay, bye-bye!' (\"askquit\"): ",
83 1, (char **) 0, &ask_quit
84 },
85 {
86 "Name (\"name\"): ",
87 0, &nick_name, (boolean *) 0
88 },
89 {
90 "Fruit (\"fruit\"): ",
91 0, &fruit, (boolean *) 0
92 },
93 {
94 "Save file (\"file\"): ",
95 0, &save_file, (boolean *) 0
96 }
97 };
98
99 void
100 light_up_room(rn)
101 int rn;
102 {
103 short i, j;
104
105 if (!blind) {
106 for (i = rooms[rn].top_row;
107 i <= rooms[rn].bottom_row; i++) {
108 for (j = rooms[rn].left_col;
109 j <= rooms[rn].right_col; j++) {
110 if (dungeon[i][j] & MONSTER) {
111 object *monster;
112
113 if ((monster = object_at(
114 &level_monsters, i, j)) != NULL) {
115 dungeon[monster->row][monster->col] &= (~MONSTER);
116 monster->trail_char =
117 get_dungeon_char(monster->row, monster->col);
118 dungeon[monster->row][monster->col] |= MONSTER;
119 }
120 }
121 mvaddch(i, j, get_dungeon_char(i, j));
122 }
123 }
124 mvaddch(rogue.row, rogue.col, rogue.fchar);
125 }
126 }
127
128 void
129 light_passage(row, col)
130 int row, col;
131 {
132 short i, j, i_end, j_end;
133
134 if (blind) {
135 return;
136 }
137 i_end = (row < (DROWS-2)) ? 1 : 0;
138 j_end = (col < (DCOLS-1)) ? 1 : 0;
139
140 for (i = ((row > MIN_ROW) ? -1 : 0); i <= i_end; i++) {
141 for (j = ((col > 0) ? -1 : 0); j <= j_end; j++) {
142 if (can_move(row, col, row+i, col+j)) {
143 mvaddch(row+i, col+j, get_dungeon_char(row+i, col+j));
144 }
145 }
146 }
147 }
148
149 void
150 darken_room(rn)
151 short rn;
152 {
153 short i, j;
154
155 for (i = rooms[rn].top_row + 1; i < rooms[rn].bottom_row; i++) {
156 for (j = rooms[rn].left_col + 1; j < rooms[rn].right_col; j++) {
157 if (blind) {
158 mvaddch(i, j, ' ');
159 } else {
160 if (!(dungeon[i][j] & (OBJECT | STAIRS)) &&
161 !(detect_monster && (dungeon[i][j] & MONSTER))) {
162 if (!imitating(i, j)) {
163 mvaddch(i, j, ' ');
164 }
165 if ((dungeon[i][j] & TRAP) && (!(dungeon[i][j] & HIDDEN))) {
166 mvaddch(i, j, '^');
167 }
168 }
169 }
170 }
171 }
172 }
173
174 char
175 get_dungeon_char(row, col)
176 short row, col;
177 {
178 unsigned short mask = dungeon[row][col];
179
180 if (mask & MONSTER) {
181 return(gmc_row_col(row, col));
182 }
183 if (mask & OBJECT) {
184 object *obj;
185
186 obj = object_at(&level_objects, row, col);
187 return(get_mask_char(obj->what_is));
188 }
189 if (mask & (TUNNEL | STAIRS | HORWALL | VERTWALL | FLOOR | DOOR)) {
190 if ((mask & (TUNNEL| STAIRS)) && (!(mask & HIDDEN))) {
191 return(((mask & STAIRS) ? '%' : '#'));
192 }
193 if (mask & HORWALL) {
194 return('-');
195 }
196 if (mask & VERTWALL) {
197 return('|');
198 }
199 if (mask & FLOOR) {
200 if (mask & TRAP) {
201 if (!(dungeon[row][col] & HIDDEN)) {
202 return('^');
203 }
204 }
205 return('.');
206 }
207 if (mask & DOOR) {
208 if (mask & HIDDEN) {
209 if (((col > 0) && (dungeon[row][col-1] & HORWALL)) ||
210 ((col < (DCOLS-1)) && (dungeon[row][col+1] & HORWALL))) {
211 return('-');
212 } else {
213 return('|');
214 }
215 } else {
216 return('+');
217 }
218 }
219 }
220 return(' ');
221 }
222
223 char
224 get_mask_char(mask)
225 unsigned short mask;
226 {
227 switch(mask) {
228 case SCROL:
229 return('?');
230 case POTION:
231 return('!');
232 case GOLD:
233 return('*');
234 case FOOD:
235 return(':');
236 case WAND:
237 return('/');
238 case ARMOR:
239 return(']');
240 case WEAPON:
241 return(')');
242 case RING:
243 return('=');
244 case AMULET:
245 return(',');
246 default:
247 return('~'); /* unknown, something is wrong */
248 }
249 }
250
251 void
252 gr_row_col(row, col, mask)
253 short *row, *col;
254 unsigned short mask;
255 {
256 short rn;
257 short r, c;
258
259 do {
260 r = get_rand(MIN_ROW, DROWS-2);
261 c = get_rand(0, DCOLS-1);
262 rn = get_room_number(r, c);
263 } while ((rn == NO_ROOM) ||
264 (!(dungeon[r][c] & mask)) ||
265 (dungeon[r][c] & (~mask)) ||
266 (!(rooms[rn].is_room & (R_ROOM | R_MAZE))) ||
267 ((r == rogue.row) && (c == rogue.col)));
268
269 *row = r;
270 *col = c;
271 }
272
273 short
274 gr_room()
275 {
276 short i;
277
278 do {
279 i = get_rand(0, MAXROOMS-1);
280 } while (!(rooms[i].is_room & (R_ROOM | R_MAZE)));
281
282 return(i);
283 }
284
285 short
286 party_objects(rn)
287 int rn;
288 {
289 short i, j, nf = 0;
290 object *obj;
291 short n, N, row, col;
292 boolean found;
293
294 row = col = 0;
295 N = ((rooms[rn].bottom_row - rooms[rn].top_row) - 1) *
296 ((rooms[rn].right_col - rooms[rn].left_col) - 1);
297 n = get_rand(5, 10);
298 if (n > N) {
299 n = N - 2;
300 }
301 for (i = 0; i < n; i++) {
302 for (j = found = 0; ((!found) && (j < 250)); j++) {
303 row = get_rand(rooms[rn].top_row+1,
304 rooms[rn].bottom_row-1);
305 col = get_rand(rooms[rn].left_col+1,
306 rooms[rn].right_col-1);
307 if ((dungeon[row][col] == FLOOR) || (dungeon[row][col] == TUNNEL)) {
308 found = 1;
309 }
310 }
311 if (found) {
312 obj = gr_object();
313 place_at(obj, row, col);
314 nf++;
315 }
316 }
317 return(nf);
318 }
319
320 short
321 get_room_number(row, col)
322 int row, col;
323 {
324 short i;
325
326 for (i = 0; i < MAXROOMS; i++) {
327 if ((row >= rooms[i].top_row) && (row <= rooms[i].bottom_row) &&
328 (col >= rooms[i].left_col) && (col <= rooms[i].right_col)) {
329 return(i);
330 }
331 }
332 return(NO_ROOM);
333 }
334
335 boolean
336 is_all_connected()
337 {
338 short i, starting_room;
339
340 starting_room = 0;
341 for (i = 0; i < MAXROOMS; i++) {
342 rooms_visited[i] = 0;
343 if (rooms[i].is_room & (R_ROOM | R_MAZE)) {
344 starting_room = i;
345 }
346 }
347
348 visit_rooms(starting_room);
349
350 for (i = 0; i < MAXROOMS; i++) {
351 if ((rooms[i].is_room & (R_ROOM | R_MAZE)) && (!rooms_visited[i])) {
352 return(0);
353 }
354 }
355 return(1);
356 }
357
358 void
359 visit_rooms(rn)
360 int rn;
361 {
362 short i;
363 short oth_rn;
364
365 rooms_visited[rn] = 1;
366
367 for (i = 0; i < 4; i++) {
368 oth_rn = rooms[rn].doors[i].oth_room;
369 if ((oth_rn >= 0) && (!rooms_visited[oth_rn])) {
370 visit_rooms(oth_rn);
371 }
372 }
373 }
374
375 void
376 draw_magic_map()
377 {
378 short i, j, ch, och;
379 unsigned short mask = (HORWALL | VERTWALL | DOOR | TUNNEL | TRAP | STAIRS |
380 MONSTER);
381 unsigned short s;
382
383 for (i = 0; i < DROWS; i++) {
384 for (j = 0; j < DCOLS; j++) {
385 s = dungeon[i][j];
386 if (s & mask) {
387 if (((ch = mvinch(i, j)) == ' ') ||
388 ((ch >= 'A') && (ch <= 'Z')) || (s & (TRAP | HIDDEN))) {
389 och = ch;
390 dungeon[i][j] &= (~HIDDEN);
391 if (s & HORWALL) {
392 ch = '-';
393 } else if (s & VERTWALL) {
394 ch = '|';
395 } else if (s & DOOR) {
396 ch = '+';
397 } else if (s & TRAP) {
398 ch = '^';
399 } else if (s & STAIRS) {
400 ch = '%';
401 } else if (s & TUNNEL) {
402 ch = '#';
403 } else {
404 continue;
405 }
406 if ((!(s & MONSTER)) || (och == ' ')) {
407 addch(ch);
408 }
409 if (s & MONSTER) {
410 object *monster;
411
412 if ((monster = object_at(
413 &level_monsters, i, j))
414 != NULL) {
415 monster->trail_char =
416 ch;
417 }
418 }
419 }
420 }
421 }
422 }
423 }
424
425 void
426 dr_course(monster, entering, row, col)
427 object *monster;
428 boolean entering;
429 short row, col;
430 {
431 short i, j, k, rn;
432 short r, rr;
433
434 monster->row = row;
435 monster->col = col;
436
437 if (mon_sees(monster, rogue.row, rogue.col)) {
438 monster->trow = NO_ROOM;
439 return;
440 }
441 rn = get_room_number(row, col);
442
443 if (entering) { /* entering room */
444 /* look for door to some other room */
445 r = get_rand(0, MAXROOMS-1);
446 for (i = 0; i < MAXROOMS; i++) {
447 rr = (r + i) % MAXROOMS;
448 if ((!(rooms[rr].is_room & (R_ROOM | R_MAZE))) || (rr == rn)) {
449 continue;
450 }
451 for (k = 0; k < 4; k++) {
452 if (rooms[rr].doors[k].oth_room == rn) {
453 monster->trow = rooms[rr].doors[k].oth_row;
454 monster->tcol = rooms[rr].doors[k].oth_col;
455 if ((monster->trow == row) &&
456 (monster->tcol == col)) {
457 continue;
458 }
459 return;
460 }
461 }
462 }
463 /* look for door to dead end */
464 if (rn == NO_ROOM)
465 clean_up("dr_course: monster not in room");
466 for (i = rooms[rn].top_row; i <= rooms[rn].bottom_row; i++) {
467 for (j = rooms[rn].left_col; j <= rooms[rn].right_col; j++) {
468 if ((i != monster->row) && (j != monster->col) &&
469 (dungeon[i][j] & DOOR)) {
470 monster->trow = i;
471 monster->tcol = j;
472 return;
473 }
474 }
475 }
476 /* return monster to room that he came from */
477 for (i = 0; i < MAXROOMS; i++) {
478 for (j = 0; j < 4; j++) {
479 if (rooms[i].doors[j].oth_room == rn) {
480 for (k = 0; k < 4; k++) {
481 if (rooms[rn].doors[k].oth_room == i) {
482 monster->trow = rooms[rn].doors[k].oth_row;
483 monster->tcol = rooms[rn].doors[k].oth_col;
484 return;
485 }
486 }
487 }
488 }
489 }
490 /* no place to send monster */
491 monster->trow = NO_ROOM;
492 } else { /* exiting room */
493 if (rn == NO_ROOM || !get_oth_room(rn, &row, &col)) {
494 monster->trow = NO_ROOM;
495 } else {
496 monster->trow = row;
497 monster->tcol = col;
498 }
499 }
500 }
501
502 boolean
503 get_oth_room(rn, row, col)
504 short rn, *row, *col;
505 {
506 short d = -1;
507
508 if (*row == rooms[rn].top_row) {
509 d = UPWARD/2;
510 } else if (*row == rooms[rn].bottom_row) {
511 d = DOWN/2;
512 } else if (*col == rooms[rn].left_col) {
513 d = LEFT/2;
514 } else if (*col == rooms[rn].right_col) {
515 d = RIGHT/2;
516 }
517 if ((d != -1) && (rooms[rn].doors[d].oth_room >= 0)) {
518 *row = rooms[rn].doors[d].oth_row;
519 *col = rooms[rn].doors[d].oth_col;
520 return(1);
521 }
522 return(0);
523 }
524
525 void
526 edit_opts()
527 {
528 char save[NOPTS+1][DCOLS];
529 short i, j;
530 short ch;
531 boolean done = 0;
532 char buf[MAX_OPT_LEN + 2];
533
534 for (i = 0; i < NOPTS+1; i++) {
535 for (j = 0; j < DCOLS; j++) {
536 save[i][j] = mvinch(i, j);
537 }
538 if (i < NOPTS) {
539 opt_show(i);
540 }
541 }
542 opt_go(0);
543 i = 0;
544
545 while (!done) {
546 refresh();
547 ch = rgetchar();
548 CH:
549 switch(ch) {
550 case '\033':
551 done = 1;
552 break;
553 case '\012':
554 case '\015':
555 if (i == (NOPTS - 1)) {
556 mvaddstr(NOPTS, 0, press_space);
557 refresh();
558 wait_for_ack();
559 done = 1;
560 } else {
561 i++;
562 opt_go(i);
563 }
564 break;
565 case '-':
566 if (i > 0) {
567 opt_go(--i);
568 } else {
569 sound_bell();
570 }
571 break;
572 case 't':
573 case 'T':
574 case 'f':
575 case 'F':
576 if (options[i].is_bool) {
577 *(options[i].bval) = (((ch == 't') || (ch == 'T')) ? 1 : 0);
578 opt_show(i);
579 opt_go(++i);
580 break;
581 }
582 default:
583 if (options[i].is_bool) {
584 sound_bell();
585 break;
586 }
587 j = 0;
588 if ((ch == '\010') || ((ch >= ' ') && (ch <= '~'))) {
589 opt_erase(i);
590 do {
591 if ((ch >= ' ') && (ch <= '~') && (j < MAX_OPT_LEN)) {
592 buf[j++] = ch;
593 buf[j] = '\0';
594 addch(ch);
595 } else if ((ch == '\010') && (j > 0)) {
596 buf[--j] = '\0';
597 move(i, j + strlen(options[i].prompt));
598 addch(' ');
599 move(i, j + strlen(options[i].prompt));
600 }
601 refresh();
602 ch = rgetchar();
603 } while ((ch != '\012') && (ch != '\015') && (ch != '\033'));
604 if (j != 0) {
605 /*
606 * We rely on the option string being
607 * allocated to hold MAX_OPT_LEN+2
608 * bytes. This is arranged in init.c.
609 */
610 (void) strcpy(*(options[i].strval), buf);
611 }
612 opt_show(i);
613 goto CH;
614 } else {
615 sound_bell();
616 }
617 break;
618 }
619 }
620
621 for (i = 0; i < NOPTS+1; i++) {
622 move(i, 0);
623 for (j = 0; j < DCOLS; j++) {
624 addch(save[i][j]);
625 }
626 }
627 }
628
629 void
630 opt_show(i)
631 int i;
632 {
633 const char *s;
634 const struct option *opt = &options[i];
635
636 opt_erase(i);
637
638 if (opt->is_bool) {
639 s = *(opt->bval) ? "True" : "False";
640 } else {
641 s = *(opt->strval);
642 }
643 addstr(s);
644 }
645
646 void
647 opt_erase(i)
648 int i;
649 {
650 const struct option *opt = &options[i];
651
652 mvaddstr(i, 0, opt->prompt);
653 clrtoeol();
654 }
655
656 void
657 opt_go(i)
658 int i;
659 {
660 move(i, strlen(options[i].prompt));
661 }
662
663 void
664 do_shell()
665 {
666 #ifdef UNIX
667 const char *sh;
668
669 md_ignore_signals();
670 if (!(sh = md_getenv("SHELL"))) {
671 sh = "/bin/sh";
672 }
673 move(LINES-1, 0);
674 refresh();
675 stop_window();
676 printf("\nCreating new shell...\n");
677 md_shell(sh);
678 start_window();
679 wrefresh(curscr);
680 md_heed_signals();
681 #endif
682 }