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