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