trap.c revision 1.8 1 1.8 dholland /* $NetBSD: trap.c,v 1.8 2008/01/14 00:23:53 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.8 dholland __RCSID("$NetBSD: trap.c,v 1.8 2008/01/14 00:23:53 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.5 hubertf 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.4 lukem short
78 1.1 cgd trap_at(row, col)
79 1.4 lukem int row, col;
80 1.1 cgd {
81 1.1 cgd short i;
82 1.1 cgd
83 1.1 cgd for (i = 0; ((i < MAX_TRAPS) && (traps[i].trap_type != NO_TRAP)); i++) {
84 1.1 cgd if ((traps[i].trap_row == row) && (traps[i].trap_col == col)) {
85 1.1 cgd return(traps[i].trap_type);
86 1.1 cgd }
87 1.1 cgd }
88 1.1 cgd return(NO_TRAP);
89 1.1 cgd }
90 1.1 cgd
91 1.4 lukem void
92 1.1 cgd trap_player(row, col)
93 1.4 lukem short row, col;
94 1.1 cgd {
95 1.1 cgd short t;
96 1.1 cgd
97 1.1 cgd if ((t = trap_at(row, col)) == NO_TRAP) {
98 1.1 cgd return;
99 1.1 cgd }
100 1.1 cgd dungeon[row][col] &= (~HIDDEN);
101 1.1 cgd if (rand_percent(rogue.exp + ring_exp)) {
102 1.7 dholland messagef(1, "the trap failed");
103 1.1 cgd return;
104 1.1 cgd }
105 1.1 cgd switch(t) {
106 1.1 cgd case TRAP_DOOR:
107 1.1 cgd trap_door = 1;
108 1.1 cgd new_level_message = trap_strings[(t*2)+1];
109 1.1 cgd break;
110 1.1 cgd case BEAR_TRAP:
111 1.7 dholland messagef(1, "%s", trap_strings[(t*2)+1]);
112 1.1 cgd bear_trap = get_rand(4, 7);
113 1.1 cgd break;
114 1.1 cgd case TELE_TRAP:
115 1.1 cgd mvaddch(rogue.row, rogue.col, '^');
116 1.1 cgd tele();
117 1.1 cgd break;
118 1.1 cgd case DART_TRAP:
119 1.7 dholland messagef(1, "%s", trap_strings[(t*2)+1]);
120 1.1 cgd rogue.hp_current -= get_damage("1d6", 1);
121 1.1 cgd if (rogue.hp_current <= 0) {
122 1.1 cgd rogue.hp_current = 0;
123 1.1 cgd }
124 1.1 cgd if ((!sustain_strength) && rand_percent(40) &&
125 1.1 cgd (rogue.str_current >= 3)) {
126 1.1 cgd rogue.str_current--;
127 1.1 cgd }
128 1.1 cgd print_stats(STAT_HP | STAT_STRENGTH);
129 1.1 cgd if (rogue.hp_current <= 0) {
130 1.8 dholland killed_by((object *)0, POISON_DART);
131 1.1 cgd }
132 1.1 cgd break;
133 1.1 cgd case SLEEPING_GAS_TRAP:
134 1.7 dholland messagef(1, "%s", trap_strings[(t*2)+1]);
135 1.1 cgd take_a_nap();
136 1.1 cgd break;
137 1.1 cgd case RUST_TRAP:
138 1.7 dholland messagef(1, "%s", trap_strings[(t*2)+1]);
139 1.8 dholland rust((object *)0);
140 1.1 cgd break;
141 1.1 cgd }
142 1.1 cgd }
143 1.1 cgd
144 1.4 lukem void
145 1.1 cgd add_traps()
146 1.1 cgd {
147 1.1 cgd short i, n, tries = 0;
148 1.1 cgd short row, col;
149 1.1 cgd
150 1.1 cgd if (cur_level <= 2) {
151 1.1 cgd n = 0;
152 1.1 cgd } else if (cur_level <= 7) {
153 1.1 cgd n = get_rand(0, 2);
154 1.1 cgd } else if (cur_level <= 11) {
155 1.1 cgd n = get_rand(1, 2);
156 1.1 cgd } else if (cur_level <= 16) {
157 1.1 cgd n = get_rand(2, 3);
158 1.1 cgd } else if (cur_level <= 21) {
159 1.1 cgd n = get_rand(2, 4);
160 1.1 cgd } else if (cur_level <= (AMULET_LEVEL + 2)) {
161 1.1 cgd n = get_rand(3, 5);
162 1.1 cgd } else {
163 1.1 cgd n = get_rand(5, MAX_TRAPS);
164 1.1 cgd }
165 1.1 cgd for (i = 0; i < n; i++) {
166 1.1 cgd traps[i].trap_type = get_rand(0, (TRAPS - 1));
167 1.1 cgd
168 1.1 cgd if ((i == 0) && (party_room != NO_ROOM)) {
169 1.1 cgd do {
170 1.1 cgd row = get_rand((rooms[party_room].top_row+1),
171 1.1 cgd (rooms[party_room].bottom_row-1));
172 1.1 cgd col = get_rand((rooms[party_room].left_col+1),
173 1.1 cgd (rooms[party_room].right_col-1));
174 1.1 cgd tries++;
175 1.1 cgd } while (((dungeon[row][col] & (OBJECT|STAIRS|TRAP|TUNNEL)) ||
176 1.1 cgd (dungeon[row][col] == NOTHING)) && (tries < 15));
177 1.1 cgd if (tries >= 15) {
178 1.1 cgd gr_row_col(&row, &col, (FLOOR | MONSTER));
179 1.1 cgd }
180 1.1 cgd } else {
181 1.1 cgd gr_row_col(&row, &col, (FLOOR | MONSTER));
182 1.1 cgd }
183 1.1 cgd traps[i].trap_row = row;
184 1.1 cgd traps[i].trap_col = col;
185 1.1 cgd dungeon[row][col] |= (TRAP | HIDDEN);
186 1.1 cgd }
187 1.1 cgd }
188 1.1 cgd
189 1.4 lukem void
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.7 dholland messagef(0, "direction? ");
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.7 dholland messagef(0, "%s", trap_strings[t*2]);
212 1.1 cgd } else {
213 1.7 dholland messagef(0, "no trap there");
214 1.1 cgd }
215 1.1 cgd }
216 1.1 cgd
217 1.4 lukem void
218 1.1 cgd show_traps()
219 1.1 cgd {
220 1.1 cgd short i, j;
221 1.1 cgd
222 1.1 cgd for (i = 0; i < DROWS; i++) {
223 1.1 cgd for (j = 0; j < DCOLS; j++) {
224 1.1 cgd if (dungeon[i][j] & TRAP) {
225 1.1 cgd mvaddch(i, j, '^');
226 1.1 cgd }
227 1.1 cgd }
228 1.1 cgd }
229 1.1 cgd }
230 1.1 cgd
231 1.4 lukem void
232 1.1 cgd search(n, is_auto)
233 1.4 lukem short n;
234 1.4 lukem boolean is_auto;
235 1.1 cgd {
236 1.1 cgd short s, i, j, row, col, t;
237 1.1 cgd short shown = 0, found = 0;
238 1.1 cgd static boolean reg_search;
239 1.1 cgd
240 1.1 cgd for (i = -1; i <= 1; i++) {
241 1.1 cgd for (j = -1; j <= 1; j++) {
242 1.1 cgd row = rogue.row + i;
243 1.1 cgd col = rogue.col + j;
244 1.1 cgd if ((row < MIN_ROW) || (row >= (DROWS-1)) ||
245 1.1 cgd (col < 0) || (col >= DCOLS)) {
246 1.1 cgd continue;
247 1.1 cgd }
248 1.1 cgd if (dungeon[row][col] & HIDDEN) {
249 1.1 cgd found++;
250 1.1 cgd }
251 1.1 cgd }
252 1.1 cgd }
253 1.1 cgd for (s = 0; s < n; s++) {
254 1.1 cgd for (i = -1; i <= 1; i++) {
255 1.1 cgd for (j = -1; j <= 1; j++) {
256 1.1 cgd row = rogue.row + i;
257 1.1 cgd col = rogue.col + j ;
258 1.1 cgd if ((row < MIN_ROW) || (row >= (DROWS-1)) ||
259 1.1 cgd (col < 0) || (col >= DCOLS)) {
260 1.1 cgd continue;
261 1.1 cgd }
262 1.1 cgd if (dungeon[row][col] & HIDDEN) {
263 1.1 cgd if (rand_percent(17 + (rogue.exp + ring_exp))) {
264 1.1 cgd dungeon[row][col] &= (~HIDDEN);
265 1.1 cgd if ((!blind) && ((row != rogue.row) ||
266 1.1 cgd (col != rogue.col))) {
267 1.1 cgd mvaddch(row, col, get_dungeon_char(row, col));
268 1.1 cgd }
269 1.1 cgd shown++;
270 1.1 cgd if (dungeon[row][col] & TRAP) {
271 1.1 cgd t = trap_at(row, col);
272 1.7 dholland messagef(1, "%s",
273 1.7 dholland trap_strings[t*2]);
274 1.1 cgd }
275 1.1 cgd }
276 1.1 cgd }
277 1.1 cgd if (((shown == found) && (found > 0)) || interrupted) {
278 1.1 cgd return;
279 1.1 cgd }
280 1.1 cgd }
281 1.1 cgd }
282 1.1 cgd if ((!is_auto) && (reg_search = !reg_search)) {
283 1.8 dholland (void)reg_move();
284 1.1 cgd }
285 1.1 cgd }
286 1.1 cgd }
287