xref: /dragonfly/games/rogue/room.c (revision b4f25088)
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  * $DragonFly: src/games/rogue/room.c,v 1.3 2006/09/02 19:31:07 pavalos Exp $
35  */
36 
37 /*
38  * room.c
39  *
40  * This source herein may be modified and/or distributed by anybody who
41  * so desires, with the following restrictions:
42  *    1.)  No portion of this notice shall be removed.
43  *    2.)  Credit shall not be taken for the creation of this source.
44  *    3.)  This code is not to be traded, sold, or used for personal
45  *         gain or profit.
46  *
47  */
48 
49 #include "rogue.h"
50 
51 room rooms[MAXROOMS];
52 boolean rooms_visited[MAXROOMS];
53 
54 extern short blind;
55 extern boolean detect_monster, jump, passgo, no_skull, ask_quit, flush;
56 extern char *nick_name, *fruit, *save_file, *press_space;
57 
58 #define NOPTS 8
59 
60 struct option {
61 	const char *prompt;
62 	boolean is_bool;
63 	char **strval;
64 	boolean *bval;
65 } options[NOPTS] = {
66 	{
67 		"Flush typeahead during battle (\"flush\"): ",
68 		1, NULL, &flush
69 	},
70 	{
71 		"Show position only at end of run (\"jump\"): ",
72 		1, NULL, &jump
73 	},
74 	{
75 		"Follow turnings in passageways (\"passgo\"): ",
76 		1, NULL, &passgo
77 	},
78 	{
79 		"Don't print skull when killed (\"noskull\" or \"notombstone\"): ",
80 		1, NULL, &no_skull
81 	},
82 	{
83 		"Ask player before saying 'Okay, bye-bye!' (\"askquit\"): ",
84 		1, NULL, &ask_quit
85 	},
86 	{
87 		"Name (\"name\"): ",
88 		0, &nick_name, NULL
89 	},
90 	{
91 		"Fruit (\"fruit\"): ",
92 		0, &fruit, NULL
93 	},
94 	{
95 		"Save file (\"file\"): ",
96 		0, &save_file, NULL
97 	}
98 };
99 
100 static boolean get_oth_room(short, short *, short *);
101 static void opt_erase(int);
102 static void opt_go(int);
103 static void opt_show(int);
104 static void visit_rooms(int);
105 
106 void
107 light_up_room(int rn)
108 {
109 	short i, j;
110 
111 	if (!blind) {
112 		for (i = rooms[rn].top_row;
113 			i <= rooms[rn].bottom_row; i++) {
114 			for (j = rooms[rn].left_col;
115 				j <= rooms[rn].right_col; j++) {
116 				if (dungeon[i][j] & MONSTER) {
117 					object *monster;
118 
119 					if ((monster = object_at(&level_monsters, i, j))) {
120 						dungeon[monster->row][monster->col] &= (~MONSTER);
121 						monster->trail_char =
122 							get_dungeon_char(monster->row, monster->col);
123 						dungeon[monster->row][monster->col] |= MONSTER;
124 					}
125 				}
126 				mvaddch(i, j, get_dungeon_char(i, j));
127 			}
128 		}
129 		mvaddch(rogue.row, rogue.col, rogue.fchar);
130 	}
131 }
132 
133 void
134 light_passage(int row, int col)
135 {
136 	short i, j, i_end, j_end;
137 
138 	if (blind) {
139 		return;
140 	}
141 	i_end = (row < (DROWS-2)) ? 1 : 0;
142 	j_end = (col < (DCOLS-1)) ? 1 : 0;
143 
144 	for (i = ((row > MIN_ROW) ? -1 : 0); i <= i_end; i++) {
145 		for (j = ((col > 0) ? -1 : 0); j <= j_end; j++) {
146 			if (can_move(row, col, row+i, col+j)) {
147 				mvaddch(row+i, col+j, get_dungeon_char(row+i, col+j));
148 			}
149 		}
150 	}
151 }
152 
153 void
154 darken_room(short rn)
155 {
156 	short i, j;
157 
158 	for (i = rooms[rn].top_row + 1; i < rooms[rn].bottom_row; i++) {
159 		for (j = rooms[rn].left_col + 1; j < rooms[rn].right_col; j++) {
160 			if (blind) {
161 				mvaddch(i, j, ' ');
162 			} else {
163 				if (!(dungeon[i][j] & (OBJECT | STAIRS)) &&
164 					!(detect_monster && (dungeon[i][j] & MONSTER))) {
165 					if (!imitating(i, j)) {
166 						mvaddch(i, j, ' ');
167 					}
168 					if ((dungeon[i][j] & TRAP) && (!(dungeon[i][j] & HIDDEN))) {
169 						mvaddch(i, j, '^');
170 					}
171 				}
172 			}
173 		}
174 	}
175 }
176 
177 char
178 get_dungeon_char(int row, int col)
179 {
180 	unsigned short mask = dungeon[row][col];
181 
182 	if (mask & MONSTER) {
183 		return(gmc_row_col(row, col));
184 	}
185 	if (mask & OBJECT) {
186 		object *obj;
187 
188 		obj = object_at(&level_objects, row, col);
189 		return(get_mask_char(obj->what_is));
190 	}
191 	if (mask & (TUNNEL | STAIRS | HORWALL | VERTWALL | FLOOR | DOOR)) {
192 		if ((mask & (TUNNEL| STAIRS)) && (!(mask & HIDDEN))) {
193 			return(((mask & STAIRS) ? '%' : '#'));
194 		}
195 		if (mask & HORWALL) {
196 			return('-');
197 		}
198 		if (mask & VERTWALL) {
199 			return('|');
200 		}
201 		if (mask & FLOOR) {
202 			if (mask & TRAP) {
203 				if (!(dungeon[row][col] & HIDDEN)) {
204 					return('^');
205 				}
206 			}
207 			return('.');
208 		}
209 		if (mask & DOOR) {
210 			if (mask & HIDDEN) {
211 				if (((col > 0) && (dungeon[row][col-1] & HORWALL)) ||
212 					((col < (DCOLS-1)) && (dungeon[row][col+1] & HORWALL))) {
213 					return('-');
214 				} else {
215 					return('|');
216 				}
217 			} else {
218 				return('+');
219 			}
220 		}
221 	}
222 	return(' ');
223 }
224 
225 char
226 get_mask_char(unsigned short mask)
227 {
228 		switch(mask) {
229 		case SCROL:
230 			return('?');
231 		case POTION:
232 			return('!');
233 		case GOLD:
234 			return('*');
235 		case FOOD:
236 			return(':');
237 		case WAND:
238 			return('/');
239 		case ARMOR:
240 			return(']');
241 		case WEAPON:
242 			return(')');
243 		case RING:
244 			return('=');
245 		case AMULET:
246 			return(',');
247 		default:
248 			return('~');	/* unknown, something is wrong */
249 		}
250 }
251 
252 void
253 gr_row_col(short *row, short *col, unsigned short mask)
254 {
255 	short rn;
256 	short r, c;
257 
258 	do {
259 		r = get_rand(MIN_ROW, DROWS-2);
260 		c = get_rand(0, DCOLS-1);
261 		rn = get_room_number(r, c);
262 	} while ((rn == NO_ROOM) ||
263 		(!(dungeon[r][c] & mask)) ||
264 		(dungeon[r][c] & (~mask)) ||
265 		(!(rooms[rn].is_room & (R_ROOM | R_MAZE))) ||
266 		((r == rogue.row) && (c == rogue.col)));
267 
268 	*row = r;
269 	*col = c;
270 }
271 
272 short
273 gr_room(void)
274 {
275 	short i;
276 
277 	do {
278 		i = get_rand(0, MAXROOMS-1);
279 	} while (!(rooms[i].is_room & (R_ROOM | R_MAZE)));
280 
281 	return(i);
282 }
283 
284 short
285 party_objects(short rn)
286 {
287 	short i, j, nf = 0;
288 	object *obj;
289 	short n, N, row, col;
290 	boolean found;
291 
292 	N = ((rooms[rn].bottom_row - rooms[rn].top_row) - 1) *
293 		((rooms[rn].right_col - rooms[rn].left_col) - 1);
294 	n =  get_rand(5, 10);
295 	if (n > N) {
296 		n = N - 2;
297 	}
298 	for (i = 0; i < n; i++) {
299 		for (j = found = 0; ((!found) && (j < 250)); j++) {
300 			row = get_rand(rooms[rn].top_row+1,
301 					   rooms[rn].bottom_row-1);
302 			col = get_rand(rooms[rn].left_col+1,
303 					   rooms[rn].right_col-1);
304 			if ((dungeon[row][col] == FLOOR) || (dungeon[row][col] == TUNNEL)) {
305 				found = 1;
306 			}
307 		}
308 		if (found) {
309 			obj = gr_object();
310 			place_at(obj, row, col);
311 			nf++;
312 		}
313 	}
314 	return(nf);
315 }
316 
317 short
318 get_room_number(int row, int col)
319 {
320 	short i;
321 
322 	for (i = 0; i < MAXROOMS; i++) {
323 		if ((row >= rooms[i].top_row) && (row <= rooms[i].bottom_row) &&
324 			(col >= rooms[i].left_col) && (col <= rooms[i].right_col)) {
325 			return(i);
326 		}
327 	}
328 	return(NO_ROOM);
329 }
330 
331 boolean
332 is_all_connected(void)
333 {
334 	short i, starting_room;
335 
336 	starting_room = 0;
337 	for (i = 0; i < MAXROOMS; i++) {
338 		rooms_visited[i] = 0;
339 		if (rooms[i].is_room & (R_ROOM | R_MAZE)) {
340 			starting_room = i;
341 		}
342 	}
343 
344 	visit_rooms(starting_room);
345 
346 	for (i = 0; i < MAXROOMS; i++) {
347 		if ((rooms[i].is_room & (R_ROOM | R_MAZE)) && (!rooms_visited[i])) {
348 			return(0);
349 		}
350 	}
351 	return(1);
352 }
353 
354 static void
355 visit_rooms(int rn)
356 {
357 	short i;
358 	short oth_rn;
359 
360 	rooms_visited[rn] = 1;
361 
362 	for (i = 0; i < 4; i++) {
363 		oth_rn = rooms[rn].doors[i].oth_room;
364 		if ((oth_rn >= 0) && (!rooms_visited[oth_rn])) {
365 			visit_rooms(oth_rn);
366 		}
367 	}
368 }
369 
370 void
371 draw_magic_map(void)
372 {
373 	short i, j, ch, och;
374 	unsigned short mask = (HORWALL | VERTWALL | DOOR | TUNNEL | TRAP | STAIRS |
375 			MONSTER);
376 	unsigned short s;
377 
378 	for (i = 0; i < DROWS; i++) {
379 		for (j = 0; j < DCOLS; j++) {
380 			s = dungeon[i][j];
381 			if (s & mask) {
382 				if (((ch = mvinch(i, j)) == ' ') ||
383 					((ch >= 'A') && (ch <= 'Z')) || (s & (TRAP | HIDDEN))) {
384 					och = ch;
385 					dungeon[i][j] &= (~HIDDEN);
386 					if (s & HORWALL) {
387 						ch = '-';
388 					} else if (s & VERTWALL) {
389 						ch = '|';
390 					} else if (s & DOOR) {
391 						ch = '+';
392 					} else if (s & TRAP) {
393 						ch = '^';
394 					} else if (s & STAIRS) {
395 						ch = '%';
396 					} else if (s & TUNNEL) {
397 						ch = '#';
398 					} else {
399 						continue;
400 					}
401 					if ((!(s & MONSTER)) || (och == ' ')) {
402 						addch(ch);
403 					}
404 					if (s & MONSTER) {
405 						object *monster;
406 
407 						if ((monster = object_at(&level_monsters, i, j))) {
408 							monster->trail_char = ch;
409 						}
410 					}
411 				}
412 			}
413 		}
414 	}
415 }
416 
417 void
418 dr_course(object *monster, boolean entering, short row, short col)
419 {
420 	short i, j, k, rn;
421 	short r, rr;
422 
423 	monster->row = row;
424 	monster->col = col;
425 
426 	if (mon_sees(monster, rogue.row, rogue.col)) {
427 		monster->trow = NO_ROOM;
428 		return;
429 	}
430 	rn = get_room_number(row, col);
431 
432 	if (entering) {		/* entering room */
433 		/* look for door to some other room */
434 		r = get_rand(0, MAXROOMS-1);
435 		for (i = 0; i < MAXROOMS; i++) {
436 			rr = (r + i) % MAXROOMS;
437 			if ((!(rooms[rr].is_room & (R_ROOM | R_MAZE))) || (rr == rn)) {
438 				continue;
439 			}
440 			for (k = 0; k < 4; k++) {
441 				if (rooms[rr].doors[k].oth_room == rn) {
442 					monster->trow = rooms[rr].doors[k].oth_row;
443 					monster->tcol = rooms[rr].doors[k].oth_col;
444 					if ((monster->trow == row) &&
445 						(monster->tcol == col)) {
446 						continue;
447 					}
448 					return;
449 				}
450 			}
451 		}
452 		/* look for door to dead end */
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 (!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 static 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 					strcpy(*(options[i].strval), buf);
592 				}
593 				opt_show(i);
594 				goto CH;
595 			} else {
596 				sound_bell();
597 			}
598 			break;
599 		}
600 	}
601 
602 	for (i = 0; i < NOPTS+1; i++) {
603 		move(i, 0);
604 		for (j = 0; j < DCOLS; j++) {
605 			addch(save[i][j]);
606 		}
607 	}
608 }
609 
610 static void
611 opt_show(int i)
612 {
613 	const char *s;
614 	struct option *opt = &options[i];
615 
616 	opt_erase(i);
617 
618 	if (opt->is_bool) {
619 		s = *(opt->bval) ? "True" : "False";
620 	} else {
621 		s = *(opt->strval);
622 	}
623 	addstr(s);
624 }
625 
626 static void
627 opt_erase(int i)
628 {
629 	struct option *opt = &options[i];
630 
631 	mvaddstr(i, 0, opt->prompt);
632 	clrtoeol();
633 }
634 
635 static void
636 opt_go(int i)
637 {
638 	move(i, strlen(options[i].prompt));
639 }
640 
641 void
642 do_shell(void)
643 {
644 #ifdef UNIX
645 	const char *sh;
646 
647 	md_ignore_signals();
648 	if (!(sh = md_getenv("SHELL"))) {
649 		sh = "/bin/sh";
650 	}
651 	move(LINES-1, 0);
652 	refresh();
653 	stop_window();
654 	printf("\nCreating new shell...\n");
655 	md_shell(sh);
656 	start_window();
657 	wrefresh(curscr);
658 	md_heed_signals();
659 #endif
660 }
661