Home | History | Annotate | Line # | Download | only in rogue
trap.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1988 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Timothy C. Stoehr.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd static char sccsid[] = "@(#)trap.c	5.3 (Berkeley) 6/1/90";
     39  1.1  cgd #endif /* not lint */
     40  1.1  cgd 
     41  1.1  cgd /*
     42  1.1  cgd  * trap.c
     43  1.1  cgd  *
     44  1.1  cgd  * This source herein may be modified and/or distributed by anybody who
     45  1.1  cgd  * so desires, with the following restrictions:
     46  1.1  cgd  *    1.)  No portion of this notice shall be removed.
     47  1.1  cgd  *    2.)  Credit shall not be taken for the creation of this source.
     48  1.1  cgd  *    3.)  This code is not to be traded, sold, or used for personal
     49  1.1  cgd  *         gain or profit.
     50  1.1  cgd  *
     51  1.1  cgd  */
     52  1.1  cgd 
     53  1.1  cgd #include "rogue.h"
     54  1.1  cgd 
     55  1.1  cgd trap traps[MAX_TRAPS];
     56  1.1  cgd boolean trap_door = 0;
     57  1.1  cgd short bear_trap = 0;
     58  1.1  cgd 
     59  1.1  cgd char *trap_strings[TRAPS * 2] = {
     60  1.1  cgd 	"trap door",
     61  1.1  cgd 			"you fell down a trap",
     62  1.1  cgd 	"bear trap",
     63  1.1  cgd 			"you are caught in a bear trap",
     64  1.1  cgd 	"teleport trap",
     65  1.1  cgd 			"teleport",
     66  1.1  cgd 	"poison dart trap",
     67  1.1  cgd 			"a small dart just hit you in the shoulder",
     68  1.1  cgd 	"sleeping gas trap",
     69  1.1  cgd 			"a strange white mist envelops you and you fall asleep",
     70  1.1  cgd 	"rust trap",
     71  1.1  cgd 			"a gush of water hits you on the head"
     72  1.1  cgd };
     73  1.1  cgd 
     74  1.1  cgd extern short cur_level, party_room;
     75  1.1  cgd extern char *new_level_message;
     76  1.1  cgd extern boolean interrupted;
     77  1.1  cgd extern short ring_exp;
     78  1.1  cgd extern boolean sustain_strength;
     79  1.1  cgd extern short blind;
     80  1.1  cgd 
     81  1.1  cgd trap_at(row, col)
     82  1.1  cgd register row, col;
     83  1.1  cgd {
     84  1.1  cgd 	short i;
     85  1.1  cgd 
     86  1.1  cgd 	for (i = 0; ((i < MAX_TRAPS) && (traps[i].trap_type != NO_TRAP)); i++) {
     87  1.1  cgd 		if ((traps[i].trap_row == row) && (traps[i].trap_col == col)) {
     88  1.1  cgd 			return(traps[i].trap_type);
     89  1.1  cgd 		}
     90  1.1  cgd 	}
     91  1.1  cgd 	return(NO_TRAP);
     92  1.1  cgd }
     93  1.1  cgd 
     94  1.1  cgd trap_player(row, col)
     95  1.1  cgd short row, col;
     96  1.1  cgd {
     97  1.1  cgd 	short t;
     98  1.1  cgd 
     99  1.1  cgd 	if ((t = trap_at(row, col)) == NO_TRAP) {
    100  1.1  cgd 		return;
    101  1.1  cgd 	}
    102  1.1  cgd 	dungeon[row][col] &= (~HIDDEN);
    103  1.1  cgd 	if (rand_percent(rogue.exp + ring_exp)) {
    104  1.1  cgd 		message("the trap failed", 1);
    105  1.1  cgd 		return;
    106  1.1  cgd 	}
    107  1.1  cgd 	switch(t) {
    108  1.1  cgd 	case TRAP_DOOR:
    109  1.1  cgd 		trap_door = 1;
    110  1.1  cgd 		new_level_message = trap_strings[(t*2)+1];
    111  1.1  cgd 		break;
    112  1.1  cgd 	case BEAR_TRAP:
    113  1.1  cgd 		message(trap_strings[(t*2)+1], 1);
    114  1.1  cgd 		bear_trap = get_rand(4, 7);
    115  1.1  cgd 		break;
    116  1.1  cgd 	case TELE_TRAP:
    117  1.1  cgd 		mvaddch(rogue.row, rogue.col, '^');
    118  1.1  cgd 		tele();
    119  1.1  cgd 		break;
    120  1.1  cgd 	case DART_TRAP:
    121  1.1  cgd 		message(trap_strings[(t*2)+1], 1);
    122  1.1  cgd 		rogue.hp_current -= get_damage("1d6", 1);
    123  1.1  cgd 		if (rogue.hp_current <= 0) {
    124  1.1  cgd 			rogue.hp_current = 0;
    125  1.1  cgd 		}
    126  1.1  cgd 		if ((!sustain_strength) && rand_percent(40) &&
    127  1.1  cgd 			(rogue.str_current >= 3)) {
    128  1.1  cgd 			rogue.str_current--;
    129  1.1  cgd 		}
    130  1.1  cgd 		print_stats(STAT_HP | STAT_STRENGTH);
    131  1.1  cgd 		if (rogue.hp_current <= 0) {
    132  1.1  cgd 			killed_by((object *) 0, POISON_DART);
    133  1.1  cgd 		}
    134  1.1  cgd 		break;
    135  1.1  cgd 	case SLEEPING_GAS_TRAP:
    136  1.1  cgd 		message(trap_strings[(t*2)+1], 1);
    137  1.1  cgd 		take_a_nap();
    138  1.1  cgd 		break;
    139  1.1  cgd 	case RUST_TRAP:
    140  1.1  cgd 		message(trap_strings[(t*2)+1], 1);
    141  1.1  cgd 		rust((object *) 0);
    142  1.1  cgd 		break;
    143  1.1  cgd 	}
    144  1.1  cgd }
    145  1.1  cgd 
    146  1.1  cgd add_traps()
    147  1.1  cgd {
    148  1.1  cgd 	short i, n, tries = 0;
    149  1.1  cgd 	short row, col;
    150  1.1  cgd 
    151  1.1  cgd 	if (cur_level <= 2) {
    152  1.1  cgd 		n = 0;
    153  1.1  cgd 	} else if (cur_level <= 7) {
    154  1.1  cgd 		n = get_rand(0, 2);
    155  1.1  cgd 	} else if (cur_level <= 11) {
    156  1.1  cgd 		n = get_rand(1, 2);
    157  1.1  cgd 	} else if (cur_level <= 16) {
    158  1.1  cgd 		n = get_rand(2, 3);
    159  1.1  cgd 	} else if (cur_level <= 21) {
    160  1.1  cgd 		n = get_rand(2, 4);
    161  1.1  cgd 	} else if (cur_level <= (AMULET_LEVEL + 2)) {
    162  1.1  cgd 		n = get_rand(3, 5);
    163  1.1  cgd 	} else {
    164  1.1  cgd 		n = get_rand(5, MAX_TRAPS);
    165  1.1  cgd 	}
    166  1.1  cgd 	for (i = 0; i < n; i++) {
    167  1.1  cgd 		traps[i].trap_type = get_rand(0, (TRAPS - 1));
    168  1.1  cgd 
    169  1.1  cgd 		if ((i == 0) && (party_room != NO_ROOM)) {
    170  1.1  cgd 			do {
    171  1.1  cgd 				row = get_rand((rooms[party_room].top_row+1),
    172  1.1  cgd 						(rooms[party_room].bottom_row-1));
    173  1.1  cgd 				col = get_rand((rooms[party_room].left_col+1),
    174  1.1  cgd 						(rooms[party_room].right_col-1));
    175  1.1  cgd 				tries++;
    176  1.1  cgd 			} while (((dungeon[row][col] & (OBJECT|STAIRS|TRAP|TUNNEL)) ||
    177  1.1  cgd 					(dungeon[row][col] == NOTHING)) && (tries < 15));
    178  1.1  cgd 			if (tries >= 15) {
    179  1.1  cgd 				gr_row_col(&row, &col, (FLOOR | MONSTER));
    180  1.1  cgd 			}
    181  1.1  cgd 		} else {
    182  1.1  cgd 			gr_row_col(&row, &col, (FLOOR | MONSTER));
    183  1.1  cgd 		}
    184  1.1  cgd 		traps[i].trap_row = row;
    185  1.1  cgd 		traps[i].trap_col = col;
    186  1.1  cgd 		dungeon[row][col] |= (TRAP | HIDDEN);
    187  1.1  cgd 	}
    188  1.1  cgd }
    189  1.1  cgd 
    190  1.1  cgd id_trap()
    191  1.1  cgd {
    192  1.1  cgd 	short dir, row, col, d, t;
    193  1.1  cgd 
    194  1.1  cgd 	message("direction? ", 0);
    195  1.1  cgd 
    196  1.1  cgd 	while (!is_direction(dir = rgetchar(), &d)) {
    197  1.1  cgd 		sound_bell();
    198  1.1  cgd 	}
    199  1.1  cgd 	check_message();
    200  1.1  cgd 
    201  1.1  cgd 	if (dir == CANCEL) {
    202  1.1  cgd 		return;
    203  1.1  cgd 	}
    204  1.1  cgd 	row = rogue.row;
    205  1.1  cgd 	col = rogue.col;
    206  1.1  cgd 
    207  1.1  cgd 	get_dir_rc(d, &row, &col, 0);
    208  1.1  cgd 
    209  1.1  cgd 	if ((dungeon[row][col] & TRAP) && (!(dungeon[row][col] & HIDDEN))) {
    210  1.1  cgd 		t = trap_at(row, col);
    211  1.1  cgd 		message(trap_strings[t*2], 0);
    212  1.1  cgd 	} else {
    213  1.1  cgd 		message("no trap there", 0);
    214  1.1  cgd 	}
    215  1.1  cgd }
    216  1.1  cgd 
    217  1.1  cgd show_traps()
    218  1.1  cgd {
    219  1.1  cgd 	short i, j;
    220  1.1  cgd 
    221  1.1  cgd 	for (i = 0; i < DROWS; i++) {
    222  1.1  cgd 		for (j = 0; j < DCOLS; j++) {
    223  1.1  cgd 			if (dungeon[i][j] & TRAP) {
    224  1.1  cgd 				mvaddch(i, j, '^');
    225  1.1  cgd 			}
    226  1.1  cgd 		}
    227  1.1  cgd 	}
    228  1.1  cgd }
    229  1.1  cgd 
    230  1.1  cgd search(n, is_auto)
    231  1.1  cgd short n;
    232  1.1  cgd boolean is_auto;
    233  1.1  cgd {
    234  1.1  cgd 	short s, i, j, row, col, t;
    235  1.1  cgd 	short shown = 0, found = 0;
    236  1.1  cgd 	static boolean reg_search;
    237  1.1  cgd 
    238  1.1  cgd 	for (i = -1; i <= 1; i++) {
    239  1.1  cgd 		for (j = -1; j <= 1; j++) {
    240  1.1  cgd 			row = rogue.row + i;
    241  1.1  cgd 			col = rogue.col + j;
    242  1.1  cgd 			if ((row < MIN_ROW) || (row >= (DROWS-1)) ||
    243  1.1  cgd 					(col < 0) || (col >= DCOLS)) {
    244  1.1  cgd 				continue;
    245  1.1  cgd 			}
    246  1.1  cgd 			if (dungeon[row][col] & HIDDEN) {
    247  1.1  cgd 				found++;
    248  1.1  cgd 			}
    249  1.1  cgd 		}
    250  1.1  cgd 	}
    251  1.1  cgd 	for (s = 0; s < n; s++) {
    252  1.1  cgd 		for (i = -1; i <= 1; i++) {
    253  1.1  cgd 			for (j = -1; j <= 1; j++) {
    254  1.1  cgd 				row = rogue.row + i;
    255  1.1  cgd 				col = rogue.col + j ;
    256  1.1  cgd 				if ((row < MIN_ROW) || (row >= (DROWS-1)) ||
    257  1.1  cgd 						(col < 0) || (col >= DCOLS)) {
    258  1.1  cgd 					continue;
    259  1.1  cgd 				}
    260  1.1  cgd 				if (dungeon[row][col] & HIDDEN) {
    261  1.1  cgd 					if (rand_percent(17 + (rogue.exp + ring_exp))) {
    262  1.1  cgd 						dungeon[row][col] &= (~HIDDEN);
    263  1.1  cgd 						if ((!blind) && ((row != rogue.row) ||
    264  1.1  cgd 								(col != rogue.col))) {
    265  1.1  cgd 							mvaddch(row, col, get_dungeon_char(row, col));
    266  1.1  cgd 						}
    267  1.1  cgd 						shown++;
    268  1.1  cgd 						if (dungeon[row][col] & TRAP) {
    269  1.1  cgd 							t = trap_at(row, col);
    270  1.1  cgd 							message(trap_strings[t*2], 1);
    271  1.1  cgd 						}
    272  1.1  cgd 					}
    273  1.1  cgd 				}
    274  1.1  cgd 				if (((shown == found) && (found > 0)) || interrupted) {
    275  1.1  cgd 					return;
    276  1.1  cgd 				}
    277  1.1  cgd 			}
    278  1.1  cgd 		}
    279  1.1  cgd 		if ((!is_auto) && (reg_search = !reg_search)) {
    280  1.1  cgd 			(void) reg_move();
    281  1.1  cgd 		}
    282  1.1  cgd 	}
    283  1.1  cgd }
    284