Home | History | Annotate | Line # | Download | only in rogue
room.c revision 1.12
      1 /*	$NetBSD: room.c,v 1.12 2008/01/14 03:50:02 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.12 2008/01/14 03:50:02 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 
     60 static boolean rooms_visited[MAXROOMS];
     61 
     62 #define NOPTS 7
     63 static 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, NULL, &jump
     72 	},
     73 	{
     74 		"Follow turnings in passageways (\"passgo\"): ",
     75 		1, NULL, &passgo
     76 	},
     77 	{
     78 		"Don't print skull when killed (\"noskull\" or \"notombstone\"): ",
     79 		1, NULL, &no_skull
     80 	},
     81 	{
     82 		"Ask player before saying 'Okay, bye-bye!' (\"askquit\"): ",
     83 		1, NULL, &ask_quit
     84 	},
     85 	{
     86 		"Name (\"name\"): ",
     87 		0, &nick_name, NULL
     88 	},
     89 	{
     90 		"Fruit (\"fruit\"): ",
     91 		0, &fruit, NULL
     92 	},
     93 	{
     94 		"Save file (\"file\"): ",
     95 		0, &save_file, NULL
     96 	}
     97 };
     98 
     99 void
    100 light_up_room(int rn)
    101 {
    102 	short i, j;
    103 
    104 	if (!blind) {
    105 		for (i = rooms[rn].top_row;
    106 			i <= rooms[rn].bottom_row; i++) {
    107 			for (j = rooms[rn].left_col;
    108 				j <= rooms[rn].right_col; j++) {
    109 				if (dungeon[i][j] & MONSTER) {
    110 					object *monster;
    111 
    112 					if ((monster = object_at(
    113 					    &level_monsters, i, j)) != NULL) {
    114 						dungeon[monster->row][monster->col] &= (~MONSTER);
    115 						monster->trail_char =
    116 							get_dungeon_char(monster->row, monster->col);
    117 						dungeon[monster->row][monster->col] |= MONSTER;
    118 					}
    119 				}
    120 				mvaddch(i, j, get_dungeon_char(i, j));
    121 			}
    122 		}
    123 		mvaddch(rogue.row, rogue.col, rogue.fchar);
    124 	}
    125 }
    126 
    127 void
    128 light_passage(int row, int col)
    129 {
    130 	short i, j, i_end, j_end;
    131 
    132 	if (blind) {
    133 		return;
    134 	}
    135 	i_end = (row < (DROWS-2)) ? 1 : 0;
    136 	j_end = (col < (DCOLS-1)) ? 1 : 0;
    137 
    138 	for (i = ((row > MIN_ROW) ? -1 : 0); i <= i_end; i++) {
    139 		for (j = ((col > 0) ? -1 : 0); j <= j_end; j++) {
    140 			if (can_move(row, col, row+i, col+j)) {
    141 				mvaddch(row+i, col+j, get_dungeon_char(row+i, col+j));
    142 			}
    143 		}
    144 	}
    145 }
    146 
    147 void
    148 darken_room(short rn)
    149 {
    150 	short i, j;
    151 
    152 	for (i = rooms[rn].top_row + 1; i < rooms[rn].bottom_row; i++) {
    153 		for (j = rooms[rn].left_col + 1; j < rooms[rn].right_col; j++) {
    154 			if (blind) {
    155 				mvaddch(i, j, ' ');
    156 			} else {
    157 				if (!(dungeon[i][j] & (OBJECT | STAIRS)) &&
    158 					!(detect_monster && (dungeon[i][j] & MONSTER))) {
    159 					if (!imitating(i, j)) {
    160 						mvaddch(i, j, ' ');
    161 					}
    162 					if ((dungeon[i][j] & TRAP) && (!(dungeon[i][j] & HIDDEN))) {
    163 						mvaddch(i, j, '^');
    164 					}
    165 				}
    166 			}
    167 		}
    168 	}
    169 }
    170 
    171 char
    172 get_dungeon_char(short row, short col)
    173 {
    174 	unsigned short mask = dungeon[row][col];
    175 
    176 	if (mask & MONSTER) {
    177 		return(gmc_row_col(row, col));
    178 	}
    179 	if (mask & OBJECT) {
    180 		object *obj;
    181 
    182 		obj = object_at(&level_objects, row, col);
    183 		return(get_mask_char(obj->what_is));
    184 	}
    185 	if (mask & (TUNNEL | STAIRS | HORWALL | VERTWALL | FLOOR | DOOR)) {
    186 		if ((mask & (TUNNEL| STAIRS)) && (!(mask & HIDDEN))) {
    187 			return(((mask & STAIRS) ? '%' : '#'));
    188 		}
    189 		if (mask & HORWALL) {
    190 			return('-');
    191 		}
    192 		if (mask & VERTWALL) {
    193 			return('|');
    194 		}
    195 		if (mask & FLOOR) {
    196 			if (mask & TRAP) {
    197 				if (!(dungeon[row][col] & HIDDEN)) {
    198 					return('^');
    199 				}
    200 			}
    201 			return('.');
    202 		}
    203 		if (mask & DOOR) {
    204 			if (mask & HIDDEN) {
    205 				if (((col > 0) && (dungeon[row][col-1] & HORWALL)) ||
    206 					((col < (DCOLS-1)) && (dungeon[row][col+1] & HORWALL))) {
    207 					return('-');
    208 				} else {
    209 					return('|');
    210 				}
    211 			} else {
    212 				return('+');
    213 			}
    214 		}
    215 	}
    216 	return(' ');
    217 }
    218 
    219 char
    220 get_mask_char(unsigned short mask)
    221 {
    222 		switch(mask) {
    223 		case SCROL:
    224 			return('?');
    225 		case POTION:
    226 			return('!');
    227 		case GOLD:
    228 			return('*');
    229 		case FOOD:
    230 			return(':');
    231 		case WAND:
    232 			return('/');
    233 		case ARMOR:
    234 			return(']');
    235 		case WEAPON:
    236 			return(')');
    237 		case RING:
    238 			return('=');
    239 		case AMULET:
    240 			return(',');
    241 		default:
    242 			return('~');	/* unknown, something is wrong */
    243 		}
    244 }
    245 
    246 void
    247 gr_row_col(short *row, short *col, unsigned short mask)
    248 {
    249 	short rn;
    250 	short r, c;
    251 
    252 	do {
    253 		r = get_rand(MIN_ROW, DROWS-2);
    254 		c = get_rand(0, DCOLS-1);
    255 		rn = get_room_number(r, c);
    256 	} while ((rn == NO_ROOM) ||
    257 		(!(dungeon[r][c] & mask)) ||
    258 		(dungeon[r][c] & (~mask)) ||
    259 		(!(rooms[rn].is_room & (R_ROOM | R_MAZE))) ||
    260 		((r == rogue.row) && (c == rogue.col)));
    261 
    262 	*row = r;
    263 	*col = c;
    264 }
    265 
    266 short
    267 gr_room(void)
    268 {
    269 	short i;
    270 
    271 	do {
    272 		i = get_rand(0, MAXROOMS-1);
    273 	} while (!(rooms[i].is_room & (R_ROOM | R_MAZE)));
    274 
    275 	return(i);
    276 }
    277 
    278 short
    279 party_objects(int rn)
    280 {
    281 	short i, j, nf = 0;
    282 	object *obj;
    283 	short n, N, row, col;
    284 	boolean found;
    285 
    286 	row = col = 0;
    287 	N = ((rooms[rn].bottom_row - rooms[rn].top_row) - 1) *
    288 		((rooms[rn].right_col - rooms[rn].left_col) - 1);
    289 	n =  get_rand(5, 10);
    290 	if (n > N) {
    291 		n = N - 2;
    292 	}
    293 	for (i = 0; i < n; i++) {
    294 		for (j = found = 0; ((!found) && (j < 250)); j++) {
    295 			row = get_rand(rooms[rn].top_row+1,
    296 					   rooms[rn].bottom_row-1);
    297 			col = get_rand(rooms[rn].left_col+1,
    298 					   rooms[rn].right_col-1);
    299 			if ((dungeon[row][col] == FLOOR) || (dungeon[row][col] == TUNNEL)) {
    300 				found = 1;
    301 			}
    302 		}
    303 		if (found) {
    304 			obj = gr_object();
    305 			place_at(obj, row, col);
    306 			nf++;
    307 		}
    308 	}
    309 	return(nf);
    310 }
    311 
    312 short
    313 get_room_number(int row, int col)
    314 {
    315 	short i;
    316 
    317 	for (i = 0; i < MAXROOMS; i++) {
    318 		if ((row >= rooms[i].top_row) && (row <= rooms[i].bottom_row) &&
    319 			(col >= rooms[i].left_col) && (col <= rooms[i].right_col)) {
    320 			return(i);
    321 		}
    322 	}
    323 	return(NO_ROOM);
    324 }
    325 
    326 boolean
    327 is_all_connected(void)
    328 {
    329 	short i, starting_room;
    330 
    331 	starting_room = 0;
    332 	for (i = 0; i < MAXROOMS; i++) {
    333 		rooms_visited[i] = 0;
    334 		if (rooms[i].is_room & (R_ROOM | R_MAZE)) {
    335 			starting_room = i;
    336 		}
    337 	}
    338 
    339 	visit_rooms(starting_room);
    340 
    341 	for (i = 0; i < MAXROOMS; i++) {
    342 		if ((rooms[i].is_room & (R_ROOM | R_MAZE)) && (!rooms_visited[i])) {
    343 			return(0);
    344 		}
    345 	}
    346 	return(1);
    347 }
    348 
    349 void
    350 visit_rooms(int rn)
    351 {
    352 	short i;
    353 	short oth_rn;
    354 
    355 	rooms_visited[rn] = 1;
    356 
    357 	for (i = 0; i < 4; i++) {
    358 		oth_rn = rooms[rn].doors[i].oth_room;
    359 		if ((oth_rn >= 0) && (!rooms_visited[oth_rn])) {
    360 			visit_rooms(oth_rn);
    361 		}
    362 	}
    363 }
    364 
    365 void
    366 draw_magic_map(void)
    367 {
    368 	short i, j, ch, och;
    369 	unsigned short mask = (HORWALL | VERTWALL | DOOR | TUNNEL | TRAP | STAIRS |
    370 			MONSTER);
    371 	unsigned short s;
    372 
    373 	for (i = 0; i < DROWS; i++) {
    374 		for (j = 0; j < DCOLS; j++) {
    375 			s = dungeon[i][j];
    376 			if (s & mask) {
    377 				if (((ch = mvinch(i, j)) == ' ') ||
    378 					((ch >= 'A') && (ch <= 'Z')) || (s & (TRAP | HIDDEN))) {
    379 					och = ch;
    380 					dungeon[i][j] &= (~HIDDEN);
    381 					if (s & HORWALL) {
    382 						ch = '-';
    383 					} else if (s & VERTWALL) {
    384 						ch = '|';
    385 					} else if (s & DOOR) {
    386 						ch = '+';
    387 					} else if (s & TRAP) {
    388 						ch = '^';
    389 					} else if (s & STAIRS) {
    390 						ch = '%';
    391 					} else if (s & TUNNEL) {
    392 						ch = '#';
    393 					} else {
    394 						continue;
    395 					}
    396 					if ((!(s & MONSTER)) || (och == ' ')) {
    397 						addch(ch);
    398 					}
    399 					if (s & MONSTER) {
    400 						object *monster;
    401 
    402 						if ((monster = object_at(
    403 						    &level_monsters, i, j))
    404 						    != NULL) {
    405 							monster->trail_char =
    406 							    ch;
    407 						}
    408 					}
    409 				}
    410 			}
    411 		}
    412 	}
    413 }
    414 
    415 void
    416 dr_course(object *monster, boolean entering, short row, short col)
    417 {
    418 	short i, j, k, rn;
    419 	short r, rr;
    420 
    421 	monster->row = row;
    422 	monster->col = col;
    423 
    424 	if (mon_sees(monster, rogue.row, rogue.col)) {
    425 		monster->trow = NO_ROOM;
    426 		return;
    427 	}
    428 	rn = get_room_number(row, col);
    429 
    430 	if (entering) {		/* entering room */
    431 		/* look for door to some other room */
    432 		r = get_rand(0, MAXROOMS-1);
    433 		for (i = 0; i < MAXROOMS; i++) {
    434 			rr = (r + i) % MAXROOMS;
    435 			if ((!(rooms[rr].is_room & (R_ROOM | R_MAZE))) || (rr == rn)) {
    436 				continue;
    437 			}
    438 			for (k = 0; k < 4; k++) {
    439 				if (rooms[rr].doors[k].oth_room == rn) {
    440 					monster->trow = rooms[rr].doors[k].oth_row;
    441 					monster->tcol = rooms[rr].doors[k].oth_col;
    442 					if ((monster->trow == row) &&
    443 						(monster->tcol == col)) {
    444 						continue;
    445 					}
    446 					return;
    447 				}
    448 			}
    449 		}
    450 		/* look for door to dead end */
    451 		if (rn == NO_ROOM)
    452 			clean_up("dr_course:  monster not in room");
    453 		for (i = rooms[rn].top_row; i <= rooms[rn].bottom_row; i++) {
    454 			for (j = rooms[rn].left_col; j <= rooms[rn].right_col; j++) {
    455 				if ((i != monster->row) && (j != monster->col) &&
    456 					(dungeon[i][j] & DOOR)) {
    457 					monster->trow = i;
    458 					monster->tcol = j;
    459 					return;
    460 				}
    461 			}
    462 		}
    463 		/* return monster to room that he came from */
    464 		for (i = 0; i < MAXROOMS; i++) {
    465 			for (j = 0; j < 4; j++) {
    466 				if (rooms[i].doors[j].oth_room == rn) {
    467 					for (k = 0; k < 4; k++) {
    468 						if (rooms[rn].doors[k].oth_room == i) {
    469 							monster->trow = rooms[rn].doors[k].oth_row;
    470 							monster->tcol = rooms[rn].doors[k].oth_col;
    471 							return;
    472 						}
    473 					}
    474 				}
    475 			}
    476 		}
    477 		/* no place to send monster */
    478 		monster->trow = NO_ROOM;
    479 	} else {		/* exiting room */
    480 		if (rn == NO_ROOM || !get_oth_room(rn, &row, &col)) {
    481 			monster->trow = NO_ROOM;
    482 		} else {
    483 			monster->trow = row;
    484 			monster->tcol = col;
    485 		}
    486 	}
    487 }
    488 
    489 boolean
    490 get_oth_room(short rn, short *row, short *col)
    491 {
    492 	short d = -1;
    493 
    494 	if (*row == rooms[rn].top_row) {
    495 		d = UPWARD/2;
    496 	} else if (*row == rooms[rn].bottom_row) {
    497 		d = DOWN/2;
    498 	} else if (*col == rooms[rn].left_col) {
    499 		d = LEFT/2;
    500 	} else if (*col == rooms[rn].right_col) {
    501 		d = RIGHT/2;
    502 	}
    503 	if ((d != -1) && (rooms[rn].doors[d].oth_room >= 0)) {
    504 		*row = rooms[rn].doors[d].oth_row;
    505 		*col = rooms[rn].doors[d].oth_col;
    506 		return(1);
    507 	}
    508 	return(0);
    509 }
    510 
    511 void
    512 edit_opts(void)
    513 {
    514 	char save[NOPTS+1][DCOLS];
    515 	short i, j;
    516 	short ch;
    517 	boolean done = 0;
    518 	char buf[MAX_OPT_LEN + 2];
    519 
    520 	for (i = 0; i < NOPTS+1; i++) {
    521 		for (j = 0; j < DCOLS; j++) {
    522 			save[i][j] = mvinch(i, j);
    523 		}
    524 		if (i < NOPTS) {
    525 			opt_show(i);
    526 		}
    527 	}
    528 	opt_go(0);
    529 	i = 0;
    530 
    531 	while (!done) {
    532 		refresh();
    533 		ch = rgetchar();
    534 CH:
    535 		switch(ch) {
    536 		case '\033':
    537 			done = 1;
    538 			break;
    539 		case '\012':
    540 		case '\015':
    541 			if (i == (NOPTS - 1)) {
    542 				mvaddstr(NOPTS, 0, press_space);
    543 				refresh();
    544 				wait_for_ack();
    545 				done = 1;
    546 			} else {
    547 				i++;
    548 				opt_go(i);
    549 			}
    550 			break;
    551 		case '-':
    552 			if (i > 0) {
    553 				opt_go(--i);
    554 			} else {
    555 				sound_bell();
    556 			}
    557 			break;
    558 		case 't':
    559 		case 'T':
    560 		case 'f':
    561 		case 'F':
    562 			if (options[i].is_bool) {
    563 				*(options[i].bval) = (((ch == 't') || (ch == 'T')) ? 1 : 0);
    564 				opt_show(i);
    565 				opt_go(++i);
    566 				break;
    567 			}
    568 		default:
    569 			if (options[i].is_bool) {
    570 				sound_bell();
    571 				break;
    572 			}
    573 			j = 0;
    574 			if ((ch == '\010') || ((ch >= ' ') && (ch <= '~'))) {
    575 				opt_erase(i);
    576 				do {
    577 					if ((ch >= ' ') && (ch <= '~') && (j < MAX_OPT_LEN)) {
    578 						buf[j++] = ch;
    579 						buf[j] = '\0';
    580 						addch(ch);
    581 					} else if ((ch == '\010') && (j > 0)) {
    582 						buf[--j] = '\0';
    583 						move(i, j + strlen(options[i].prompt));
    584 						addch(' ');
    585 						move(i, j + strlen(options[i].prompt));
    586 					}
    587 					refresh();
    588 					ch = rgetchar();
    589 				} while ((ch != '\012') && (ch != '\015') && (ch != '\033'));
    590 				if (j != 0) {
    591 					/*
    592 					 * We rely on the option string being
    593 					 * allocated to hold MAX_OPT_LEN+2
    594 					 * bytes. This is arranged in init.c.
    595 					 */
    596 					(void)strcpy(*(options[i].strval), buf);
    597 				}
    598 				opt_show(i);
    599 				goto CH;
    600 			} else {
    601 				sound_bell();
    602 			}
    603 			break;
    604 		}
    605 	}
    606 
    607 	for (i = 0; i < NOPTS+1; i++) {
    608 		move(i, 0);
    609 		for (j = 0; j < DCOLS; j++) {
    610 			addch(save[i][j]);
    611 		}
    612 	}
    613 }
    614 
    615 void
    616 opt_show(int i)
    617 {
    618 	const char *s;
    619 	const struct option *opt = &options[i];
    620 
    621 	opt_erase(i);
    622 
    623 	if (opt->is_bool) {
    624 		s = *(opt->bval) ? "True" : "False";
    625 	} else {
    626 		s = *(opt->strval);
    627 	}
    628 	addstr(s);
    629 }
    630 
    631 void
    632 opt_erase(int i)
    633 {
    634 	const struct option *opt = &options[i];
    635 
    636 	mvaddstr(i, 0, opt->prompt);
    637 	clrtoeol();
    638 }
    639 
    640 void
    641 opt_go(int i)
    642 {
    643 	move(i, strlen(options[i].prompt));
    644 }
    645 
    646 void
    647 do_shell(void)
    648 {
    649 #ifdef UNIX
    650 	const char *sh;
    651 
    652 	md_ignore_signals();
    653 	if (!(sh = md_getenv("SHELL"))) {
    654 		sh = "/bin/sh";
    655 	}
    656 	move(LINES-1, 0);
    657 	refresh();
    658 	stop_window();
    659 	printf("\nCreating new shell...\n");
    660 	md_shell(sh);
    661 	start_window();
    662 	wrefresh(curscr);
    663 	md_heed_signals();
    664 #endif
    665 }
    666