root/trunk/src/trap.c

Revision 895, 7.6 kB (checked in by takkaria, 5 days ago)

Fix #44: (by Rowan Beentje)

  • Replace all instances of rand_int() with randint0()
  • Replace all instances of randint() with randint1()

In addition, the patch:

  • Replaces all instances of rand_die() with randint1()
  • Replaces randint1() with randint0() on line 1649 of effects.c to fix a multihued breath effect error.
Line 
1 /*
2  * File: trap.c
3  * Purpose: Trap triggering, selection, and placement
4  *
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  *
7  * This work is free software; you can redistribute it and/or modify it
8  * under the terms of either:
9  *
10  * a) the GNU General Public License as published by the Free Software
11  *    Foundation, version 2, or
12  *
13  * b) the "Angband licence":
14  *    This software may be copied and distributed for educational, research,
15  *    and not for profit purposes provided that this copyright and statement
16  *    are included in all such copies.  Other copyrights may also apply.
17  */
18 #include "angband.h"
19
20
21 /*
22  * Determine if a trap affects the player.
23  * Always miss 5% of the time, Always hit 5% of the time.
24  * Otherwise, match trap power against player armor.
25  */
26 static bool check_hit(int power)
27 {
28         return test_hit(power, p_ptr->ac + p_ptr->to_a, TRUE);
29 }
30
31
32 /*
33  * Hack -- instantiate a trap
34  *
35  * XXX XXX XXX This routine should be redone to reflect trap "level".
36  * That is, it does not make sense to have spiked pits at 50 feet.
37  * Actually, it is not this routine, but the "trap instantiation"
38  * code, which should also check for "trap doors" on quest levels.
39  */
40 void pick_trap(int y, int x)
41 {
42         int feat;
43
44         const static int min_level[] =
45         {
46                 2,              /* Trap door */
47                 2,              /* Open pit */
48                 2,              /* Spiked pit */
49                 2,              /* Poison pit */
50                 3,              /* Summoning rune */
51                 1,              /* Teleport rune */
52                 2,              /* Fire rune */
53                 2,              /* Acid rune */
54                 2,              /* Slow rune */
55                 6,              /* Strength dart */
56                 6,              /* Dexterity dart */
57                 6,              /* Constitution dart */
58                 2,              /* Gas blind */
59                 1,              /* Gas confuse */
60                 2,              /* Gas poison */
61                 2,              /* Gas sleep */
62         };
63
64         /* Paranoia */
65         if (cave_feat[y][x] != FEAT_INVIS) return;
66
67         /* Pick a trap */
68         while (1)
69         {
70                 /* Hack -- pick a trap */
71                 feat = FEAT_TRAP_HEAD + randint0(16);
72
73                 /* Check against minimum depth */
74                 if (min_level[feat - FEAT_TRAP_HEAD] > p_ptr->depth) continue;
75
76                 /* Hack -- no trap doors on quest levels */
77                 if ((feat == FEAT_TRAP_HEAD + 0x00) && is_quest(p_ptr->depth)) continue;
78
79                 /* Hack -- no trap doors on the deepest level */
80                 if ((feat == FEAT_TRAP_HEAD + 0x00) && (p_ptr->depth >= MAX_DEPTH-1)) continue;
81
82                 /* Done */
83                 break;
84         }
85
86         /* Activate the trap */
87         cave_set_feat(y, x, feat);
88 }
89
90
91
92 /*
93  * Places a random trap at the given location.
94  *
95  * The location must be a legal, naked, floor grid.
96  *
97  * Note that all traps start out as "invisible" and "untyped", and then
98  * when they are "discovered" (by detecting them or setting them off),
99  * the trap is "instantiated" as a visible, "typed", trap.
100  */
101 void place_trap(int y, int x)
102 {
103         /* Paranoia */
104         if (!in_bounds(y, x)) return;
105
106         /* Require empty, clean, floor grid */
107         if (!cave_naked_bold(y, x)) return;
108
109         /* Place an invisible trap */
110         cave_set_feat(y, x, FEAT_INVIS);
111 }
112
113
114
115
116 /*
117  * Handle player hitting a real trap
118  */
119 void hit_trap(int y, int x)
120 {
121         int i, num, dam;
122
123         cptr name = "a trap";
124
125
126         /* Disturb the player */
127         disturb(0, 0);
128
129         /* Analyze XXX XXX XXX */
130         switch (cave_feat[y][x])
131         {
132                 case FEAT_TRAP_HEAD + 0x00:
133                 {
134                         msg_print("You fall through a trap door!");
135                         if (p_ptr->ffall)
136                         {
137                                 msg_print("You float gently down to the next level.");
138                         }
139                         else
140                         {
141                                 dam = damroll(2, 8);
142                                 take_hit(dam, name);
143                         }
144
145                         /* New depth */
146                         p_ptr->depth++;
147
148                         /* Leaving */
149                         p_ptr->leaving = TRUE;
150                        
151                         /* Don't generate connected stairs */
152                         p_ptr->create_down_stair = FALSE;
153                         p_ptr->create_up_stair = FALSE;
154
155                         break;
156                 }
157
158                 case FEAT_TRAP_HEAD + 0x01:
159                 {
160                         msg_print("You fall into a pit!");
161                         if (p_ptr->ffall)
162                         {
163                                 msg_print("You float gently to the bottom of the pit.");
164                         }
165                         else
166                         {
167                                 dam = damroll(2, 6);
168                                 take_hit(dam, name);
169                         }
170                         break;
171                 }
172
173                 case FEAT_TRAP_HEAD + 0x02:
174                 {
175                         msg_print("You fall into a spiked pit!");
176
177                         if (p_ptr->ffall)
178                         {
179                                 msg_print("You float gently to the floor of the pit.");
180                                 msg_print("You carefully avoid touching the spikes.");
181                         }
182
183                         else
184                         {
185                                 /* Base damage */
186                                 dam = damroll(2, 6);
187
188                                 /* Extra spike damage */
189                                 if (one_in_(2))
190                                 {
191                                         msg_print("You are impaled!");
192
193                                         dam = dam * 2;
194                                         (void)inc_timed(TMD_CUT, randint1(dam));
195                                 }
196
197                                 /* Take the damage */
198                                 take_hit(dam, name);
199                         }
200                         break;
201                 }
202
203                 case FEAT_TRAP_HEAD + 0x03:
204                 {
205                         msg_print("You fall into a spiked pit!");
206
207                         if (p_ptr->ffall)
208                         {
209                                 msg_print("You float gently to the floor of the pit.");
210                                 msg_print("You carefully avoid touching the spikes.");
211                         }
212
213                         else
214                         {
215                                 /* Base damage */
216                                 dam = damroll(2, 6);
217
218                                 /* Extra spike damage */
219                                 if (one_in_(2))
220                                 {
221                                         msg_print("You are impaled on poisonous spikes!");
222
223                                         dam = dam * 2;
224                                         (void)inc_timed(TMD_CUT, randint1(dam));
225
226                                         if (p_ptr->resist_pois || p_ptr->timed[TMD_OPP_POIS])
227                                         {
228                                                 msg_print("The poison does not affect you!");
229                                         }
230                                         else
231                                         {
232                                                 dam = dam * 2;
233                                                 (void)inc_timed(TMD_POISONED, randint1(dam));
234                                         }
235                                 }
236
237                                 /* Take the damage */
238                                 take_hit(dam, name);
239                         }
240
241                         break;
242                 }
243
244                 case FEAT_TRAP_HEAD + 0x04:
245                 {
246                         sound(MSG_SUM_MONSTER);
247                         msg_print("You are enveloped in a cloud of smoke!");
248                         cave_info[y][x] &= ~(CAVE_MARK);
249                         cave_set_feat(y, x, FEAT_FLOOR);
250                         num = 2 + randint1(3);
251                         for (i = 0; i < num; i++)
252                         {
253                                 (void)summon_specific(y, x, p_ptr->depth, 0);
254                         }
255                         break;
256                 }
257
258                 case FEAT_TRAP_HEAD + 0x05:
259                 {
260                         msg_print("You hit a teleport trap!");
261                         teleport_player(100);
262                         break;
263                 }
264
265                 case FEAT_TRAP_HEAD + 0x06:
266                 {
267                         msg_print("You are enveloped in flames!");
268                         dam = damroll(4, 6);
269                         fire_dam(dam, "a fire trap");
270                         break;
271                 }
272
273                 case FEAT_TRAP_HEAD + 0x07:
274                 {
275                         msg_print("You are splashed with acid!");
276                         dam = damroll(4, 6);
277                         acid_dam(dam, "an acid trap");
278                         break;
279                 }
280
281                 case FEAT_TRAP_HEAD + 0x08:
282                 {
283                         if (check_hit(125))
284                         {
285                                 msg_print("A small dart hits you!");
286                                 dam = damroll(1, 4);
287                                 take_hit(dam, name);
288                                 (void)inc_timed(TMD_SLOW, randint0(20) + 20);
289                         }
290                         else
291                         {
292                                 msg_print("A small dart barely misses you.");
293                         }
294                         break;
295                 }
296
297                 case FEAT_TRAP_HEAD + 0x09:
298                 {
299                         if (check_hit(125))
300                         {
301                                 msg_print("A small dart hits you!");
302                                 dam = damroll(1, 4);
303                                 take_hit(dam, name);
304                                 (void)do_dec_stat(A_STR, FALSE);
305                         }
306                         else
307                         {
308                                 msg_print("A small dart barely misses you.");
309                         }
310                         break;
311                 }
312
313                 case FEAT_TRAP_HEAD + 0x0A:
314                 {
315                         if (check_hit(125))
316                         {
317                                 msg_print("A small dart hits you!");
318                                 dam = damroll(1, 4);
319                                 take_hit(dam, name);
320                                 (void)do_dec_stat(A_DEX, FALSE);
321                         }
322                         else
323                         {
324                                 msg_print("A small dart barely misses you.");
325                         }
326                         break;
327                 }
328
329                 case FEAT_TRAP_HEAD + 0x0B:
330                 {
331                         if (check_hit(125))
332                         {
333                                 msg_print("A small dart hits you!");
334                                 dam = damroll(1, 4);
335                                 take_hit(dam, name);
336                                 (void)do_dec_stat(A_CON, FALSE);
337                         }
338                         else
339                         {
340                                 msg_print("A small dart barely misses you.");
341                         }
342                         break;
343                 }
344
345                 case FEAT_TRAP_HEAD + 0x0C:
346                 {
347                         msg_print("You are surrounded by a black gas!");
348                         if (!p_ptr->resist_blind)
349                         {
350                                 (void)inc_timed(TMD_BLIND, randint0(50) + 25);
351                         }
352                         break;
353                 }
354
355                 case FEAT_TRAP_HEAD + 0x0D:
356                 {
357                         msg_print("You are surrounded by a gas of scintillating colors!");
358                         if (!p_ptr->resist_confu)
359                         {
360                                 (void)inc_timed(TMD_CONFUSED, randint0(20) + 10);
361                         }
362                         break;
363                 }
364
365                 case FEAT_TRAP_HEAD + 0x0E:
366                 {
367                         msg_print("You are surrounded by a pungent green gas!");
368                         if (!p_ptr->resist_pois && !p_ptr->timed[TMD_OPP_POIS])
369                         {
370                                 (void)inc_timed(TMD_POISONED, randint0(20) + 10);
371                         }
372                         break;
373                 }
374
375                 case FEAT_TRAP_HEAD + 0x0F:
376                 {
377                         msg_print("You are surrounded by a strange white mist!");
378                         if (!p_ptr->free_act)
379                         {
380                                 (void)inc_timed(TMD_PARALYZED, randint0(10) + 5);
381                         }
382                         break;
383                 }
384         }
385 }
Note: See TracBrowser for help on using the browser.