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