Home | History | Annotate | Line # | Download | only in rogue
room.c revision 1.7
      1 /*	$NetBSD: room.c,v 1.7 2003/08/07 09:37:40 agc 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.7 2003/08/07 09:37:40 agc 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 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
     88 	},
     89 	{
     90 		"Fruit (\"fruit\"): ",
     91 		0, &fruit
     92 	},
     93 	{
     94 		"Save file (\"file\"): ",
     95 		0, &save_file
     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 		for (i = rooms[rn].top_row; i <= rooms[rn].bottom_row; i++) {
    465 			for (j = rooms[rn].left_col; j <= rooms[rn].right_col; j++) {
    466 				if ((i != monster->row) && (j != monster->col) &&
    467 					(dungeon[i][j] & DOOR)) {
    468 					monster->trow = i;
    469 					monster->tcol = j;
    470 					return;
    471 				}
    472 			}
    473 		}
    474 		/* return monster to room that he came from */
    475 		for (i = 0; i < MAXROOMS; i++) {
    476 			for (j = 0; j < 4; j++) {
    477 				if (rooms[i].doors[j].oth_room == rn) {
    478 					for (k = 0; k < 4; k++) {
    479 						if (rooms[rn].doors[k].oth_room == i) {
    480 							monster->trow = rooms[rn].doors[k].oth_row;
    481 							monster->tcol = rooms[rn].doors[k].oth_col;
    482 							return;
    483 						}
    484 					}
    485 				}
    486 			}
    487 		}
    488 		/* no place to send monster */
    489 		monster->trow = NO_ROOM;
    490 	} else {		/* exiting room */
    491 		if (!get_oth_room(rn, &row, &col)) {
    492 			monster->trow = NO_ROOM;
    493 		} else {
    494 			monster->trow = row;
    495 			monster->tcol = col;
    496 		}
    497 	}
    498 }
    499 
    500 boolean
    501 get_oth_room(rn, row, col)
    502 	short rn, *row, *col;
    503 {
    504 	short d = -1;
    505 
    506 	if (*row == rooms[rn].top_row) {
    507 		d = UPWARD/2;
    508 	} else if (*row == rooms[rn].bottom_row) {
    509 		d = DOWN/2;
    510 	} else if (*col == rooms[rn].left_col) {
    511 		d = LEFT/2;
    512 	} else if (*col == rooms[rn].right_col) {
    513 		d = RIGHT/2;
    514 	}
    515 	if ((d != -1) && (rooms[rn].doors[d].oth_room >= 0)) {
    516 		*row = rooms[rn].doors[d].oth_row;
    517 		*col = rooms[rn].doors[d].oth_col;
    518 		return(1);
    519 	}
    520 	return(0);
    521 }
    522 
    523 void
    524 edit_opts()
    525 {
    526 	char save[NOPTS+1][DCOLS];
    527 	short i, j;
    528 	short ch;
    529 	boolean done = 0;
    530 	char buf[MAX_OPT_LEN + 2];
    531 
    532 	for (i = 0; i < NOPTS+1; i++) {
    533 		for (j = 0; j < DCOLS; j++) {
    534 			save[i][j] = mvinch(i, j);
    535 		}
    536 		if (i < NOPTS) {
    537 			opt_show(i);
    538 		}
    539 	}
    540 	opt_go(0);
    541 	i = 0;
    542 
    543 	while (!done) {
    544 		refresh();
    545 		ch = rgetchar();
    546 CH:
    547 		switch(ch) {
    548 		case '\033':
    549 			done = 1;
    550 			break;
    551 		case '\012':
    552 		case '\015':
    553 			if (i == (NOPTS - 1)) {
    554 				mvaddstr(NOPTS, 0, press_space);
    555 				refresh();
    556 				wait_for_ack();
    557 				done = 1;
    558 			} else {
    559 				i++;
    560 				opt_go(i);
    561 			}
    562 			break;
    563 		case '-':
    564 			if (i > 0) {
    565 				opt_go(--i);
    566 			} else {
    567 				sound_bell();
    568 			}
    569 			break;
    570 		case 't':
    571 		case 'T':
    572 		case 'f':
    573 		case 'F':
    574 			if (options[i].is_bool) {
    575 				*(options[i].bval) = (((ch == 't') || (ch == 'T')) ? 1 : 0);
    576 				opt_show(i);
    577 				opt_go(++i);
    578 				break;
    579 			}
    580 		default:
    581 			if (options[i].is_bool) {
    582 				sound_bell();
    583 				break;
    584 			}
    585 			j = 0;
    586 			if ((ch == '\010') || ((ch >= ' ') && (ch <= '~'))) {
    587 				opt_erase(i);
    588 				do {
    589 					if ((ch >= ' ') && (ch <= '~') && (j < MAX_OPT_LEN)) {
    590 						buf[j++] = ch;
    591 						buf[j] = '\0';
    592 						addch(ch);
    593 					} else if ((ch == '\010') && (j > 0)) {
    594 						buf[--j] = '\0';
    595 						move(i, j + strlen(options[i].prompt));
    596 						addch(' ');
    597 						move(i, j + strlen(options[i].prompt));
    598 					}
    599 					refresh();
    600 					ch = rgetchar();
    601 				} while ((ch != '\012') && (ch != '\015') && (ch != '\033'));
    602 				if (j != 0) {
    603 					(void) strcpy(*(options[i].strval), buf);
    604 				}
    605 				opt_show(i);
    606 				goto CH;
    607 			} else {
    608 				sound_bell();
    609 			}
    610 			break;
    611 		}
    612 	}
    613 
    614 	for (i = 0; i < NOPTS+1; i++) {
    615 		move(i, 0);
    616 		for (j = 0; j < DCOLS; j++) {
    617 			addch(save[i][j]);
    618 		}
    619 	}
    620 }
    621 
    622 void
    623 opt_show(i)
    624 	int i;
    625 {
    626 	const char *s;
    627 	struct option *opt = &options[i];
    628 
    629 	opt_erase(i);
    630 
    631 	if (opt->is_bool) {
    632 		s = *(opt->bval) ? "True" : "False";
    633 	} else {
    634 		s = *(opt->strval);
    635 	}
    636 	addstr(s);
    637 }
    638 
    639 void
    640 opt_erase(i)
    641 	int i;
    642 {
    643 	struct option *opt = &options[i];
    644 
    645 	mvaddstr(i, 0, opt->prompt);
    646 	clrtoeol();
    647 }
    648 
    649 void
    650 opt_go(i)
    651 	int i;
    652 {
    653 	move(i, strlen(options[i].prompt));
    654 }
    655 
    656 void
    657 do_shell()
    658 {
    659 #ifdef UNIX
    660 	const char *sh;
    661 
    662 	md_ignore_signals();
    663 	if (!(sh = md_getenv("SHELL"))) {
    664 		sh = "/bin/sh";
    665 	}
    666 	move(LINES-1, 0);
    667 	refresh();
    668 	stop_window();
    669 	printf("\nCreating new shell...\n");
    670 	md_shell(sh);
    671 	start_window();
    672 	wrefresh(curscr);
    673 	md_heed_signals();
    674 #endif
    675 }
    676