gdb - Local mods (compile)
[dragonfly.git] / games / rogue / zap.c
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  * @(#)zap.c    8.1 (Berkeley) 5/31/93
33  * $FreeBSD: src/games/rogue/zap.c,v 1.3 1999/11/30 03:49:29 billf Exp $
34  * $DragonFly: src/games/rogue/zap.c,v 1.3 2006/09/02 19:31:07 pavalos Exp $
35  */
36
37 /*
38  * zap.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 static object *get_zapped_monster(short, short *, short *);
52 static void tele_away(object *);
53 static void wdrain_life(object *);
54 static void zap_monster(object *, unsigned short);
55
56 boolean wizard = 0;
57
58 extern boolean being_held, score_only, detect_monster;
59 extern short cur_room;
60
61 void
62 zapp(void)
63 {
64         short wch;
65         boolean first_miss = 1;
66         object *wand;
67         short dir, d, row, col;
68         object *monster;
69
70         while (!is_direction(dir = rgetchar(), &d)) {
71                 sound_bell();
72                 if (first_miss) {
73                         message("direction? ", 0);
74                         first_miss = 0;
75                 }
76         }
77         check_message();
78         if (dir == CANCEL) {
79                 return;
80         }
81         if ((wch = pack_letter("zap with what?", WAND)) == CANCEL) {
82                 return;
83         }
84         check_message();
85
86         if (!(wand = get_letter_object(wch))) {
87                 message("no such item.", 0);
88                 return;
89         }
90         if (wand->what_is != WAND) {
91                 message("you can't zap with that", 0);
92                 return;
93         }
94         if (wand->class <= 0) {
95                 message("nothing happens", 0);
96         } else {
97                 wand->class--;
98                 row = rogue.row; col = rogue.col;
99                 if ((wand->which_kind == COLD) || (wand->which_kind == FIRE)) {
100                         bounce((short)wand->which_kind, d, row, col, 0);
101                 } else {
102                         monster = get_zapped_monster(d, &row, &col);
103                         if (wand->which_kind == DRAIN_LIFE) {
104                                 wdrain_life(monster);
105                         } else if (monster) {
106                                 wake_up(monster);
107                                 s_con_mon(monster);
108                                 zap_monster(monster, wand->which_kind);
109                                 relight();
110                         }
111                 }
112         }
113         reg_move();
114 }
115
116 static object *
117 get_zapped_monster(short dir, short *row, short *col)
118 {
119         short orow, ocol;
120
121         for (;;) {
122                 orow = *row; ocol = *col;
123                 get_dir_rc(dir, row, col, 0);
124                 if (((*row == orow) && (*col == ocol)) ||
125                    (dungeon[*row][*col] & (HORWALL | VERTWALL)) ||
126                    (dungeon[*row][*col] == NOTHING)) {
127                         return(0);
128                 }
129                 if (dungeon[*row][*col] & MONSTER) {
130                         if (!imitating(*row, *col)) {
131                                 return(object_at(&level_monsters, *row, *col));
132                         }
133                 }
134         }
135 }
136
137 static void
138 zap_monster(object *monster, unsigned short kind)
139 {
140         short row, col;
141         object *nm;
142         short tc;
143
144         row = monster->row;
145         col = monster->col;
146
147         switch(kind) {
148         case SLOW_MONSTER:
149                 if (monster->m_flags & HASTED) {
150                         monster->m_flags &= (~HASTED);
151                 } else {
152                         monster->slowed_toggle = 0;
153                         monster->m_flags |= SLOWED;
154                 }
155                 break;
156         case HASTE_MONSTER:
157                 if (monster->m_flags & SLOWED) {
158                         monster->m_flags &= (~SLOWED);
159                 } else {
160                         monster->m_flags |= HASTED;
161                 }
162                 break;
163         case TELE_AWAY:
164                 tele_away(monster);
165                 break;
166         case INVISIBILITY:
167                 monster->m_flags |= INVISIBLE;
168                 break;
169         case POLYMORPH:
170                 if (monster->m_flags & HOLDS) {
171                         being_held = 0;
172                 }
173                 nm = monster->next_monster;
174                 tc = monster->trail_char;
175                 gr_monster(monster, get_rand(0, MONSTERS-1));
176                 monster->row = row;
177                 monster->col = col;
178                 monster->next_monster = nm;
179                 monster->trail_char = tc;
180                 if (!(monster->m_flags & IMITATES)) {
181                         wake_up(monster);
182                 }
183                 break;
184         case MAGIC_MISSILE:
185                 rogue_hit(monster, 1);
186                 break;
187         case CANCELLATION:
188                 if (monster->m_flags & HOLDS) {
189                         being_held = 0;
190                 }
191                 if (monster->m_flags & STEALS_ITEM) {
192                         monster->drop_percent = 0;
193                 }
194                 monster->m_flags &= (~(FLIES | FLITS | SPECIAL_HIT | INVISIBLE |
195                         FLAMES | IMITATES | CONFUSES | SEEKS_GOLD | HOLDS));
196                 break;
197         case DO_NOTHING:
198                 message("nothing happens", 0);
199                 break;
200         }
201 }
202
203 static void
204 tele_away(object *monster)
205 {
206         short row, col;
207
208         if (monster->m_flags & HOLDS) {
209                 being_held = 0;
210         }
211         gr_row_col(&row, &col, (FLOOR | TUNNEL | STAIRS | OBJECT));
212         mvaddch(monster->row, monster->col, monster->trail_char);
213         dungeon[monster->row][monster->col] &= ~MONSTER;
214         monster->row = row; monster->col = col;
215         dungeon[row][col] |= MONSTER;
216         monster->trail_char = mvinch(row, col);
217         if (detect_monster || rogue_can_see(row, col)) {
218                 mvaddch(row, col, gmc(monster));
219         }
220 }
221
222 void
223 wizardize(void)
224 {
225         char buf[100];
226
227         if (wizard) {
228                 wizard = 0;
229                 message("not wizard anymore", 0);
230         } else {
231                 if (get_input_line("wizard's password:", "", buf, "", 0, 0)) {
232                         xxx(1);
233                         xxxx(buf, strlen(buf));
234                         if (!strncmp(buf, "\247\104\126\272\115\243\027", 7)) {
235                                 wizard = 1;
236                                 score_only = 1;
237                                 message("Welcome, mighty wizard!", 0);
238                         } else {
239                                 message("sorry", 0);
240                         }
241                 }
242         }
243 }
244
245 static void
246 wdrain_life(object *monster)
247 {
248         short hp;
249         object *lmon, *nm;
250
251         hp = rogue.hp_current / 3;
252         rogue.hp_current = (rogue.hp_current + 1) / 2;
253
254         if (cur_room >= 0) {
255                 lmon = level_monsters.next_monster;
256                 while (lmon) {
257                         nm = lmon->next_monster;
258                         if (get_room_number(lmon->row, lmon->col) == cur_room) {
259                                 wake_up(lmon);
260                                 mon_damage(lmon, hp);
261                         }
262                         lmon = nm;
263                 }
264         } else {
265                 if (monster) {
266                         wake_up(monster);
267                         mon_damage(monster, hp);
268                 }
269         }
270         print_stats(STAT_HP);
271         relight();
272 }
273
274 void
275 bounce(short ball, short dir, short row, short col, short r)
276 {
277         short orow, ocol;
278         char buf[DCOLS];
279         const char *s;
280         short i, ch, new_dir = -1, damage;
281         static short btime;
282
283         if (++r == 1) {
284                 btime = get_rand(3, 6);
285         } else if (r > btime) {
286                 return;
287         }
288
289         if (ball == FIRE) {
290                 s = "fire";
291         } else {
292                 s = "ice";
293         }
294         if (r > 1) {
295                 sprintf(buf, "the %s bounces", s);
296                 message(buf, 0);
297         }
298         orow = row;
299         ocol = col;
300         do {
301                 ch = mvinch(orow, ocol);
302                 standout();
303                 mvaddch(orow, ocol, ch);
304                 get_dir_rc(dir, &orow, &ocol, 1);
305         } while (!(     (ocol <= 0) ||
306                                 (ocol >= DCOLS-1) ||
307                                 (dungeon[orow][ocol] == NOTHING) ||
308                                 (dungeon[orow][ocol] & MONSTER) ||
309                                 (dungeon[orow][ocol] & (HORWALL | VERTWALL)) ||
310                                 ((orow == rogue.row) && (ocol == rogue.col))));
311         standend();
312         refresh();
313         do {
314                 orow = row;
315                 ocol = col;
316                 ch = mvinch(row, col);
317                 mvaddch(row, col, ch);
318                 get_dir_rc(dir, &row, &col, 1);
319         } while (!(     (col <= 0) ||
320                                 (col >= DCOLS-1) ||
321                                 (dungeon[row][col] == NOTHING) ||
322                                 (dungeon[row][col] & MONSTER) ||
323                                 (dungeon[row][col] & (HORWALL | VERTWALL)) ||
324                                 ((row == rogue.row) && (col == rogue.col))));
325
326         if (dungeon[row][col] & MONSTER) {
327                 object *monster;
328
329                 monster = object_at(&level_monsters, row, col);
330
331                 wake_up(monster);
332                 if (rand_percent(33)) {
333                         sprintf(buf, "the %s misses the %s", s, mon_name(monster));
334                         message(buf, 0);
335                         goto ND;
336                 }
337                 if (ball == FIRE) {
338                         if (!(monster->m_flags & RUSTS)) {
339                                 if (monster->m_flags & FREEZES) {
340                                         damage = monster->hp_to_kill;
341                                 } else if (monster->m_flags & FLAMES) {
342                                         damage = (monster->hp_to_kill / 10) + 1;
343                                 } else {
344                                         damage = get_rand((rogue.hp_current / 3), rogue.hp_max);
345                                 }
346                         } else {
347                                 damage = (monster->hp_to_kill / 2) + 1;
348                         }
349                         sprintf(buf, "the %s hits the %s", s, mon_name(monster));
350                         message(buf, 0);
351                         mon_damage(monster, damage);
352                 } else {
353                         damage = -1;
354                         if (!(monster->m_flags & FREEZES)) {
355                                 if (rand_percent(33)) {
356                                         message("the monster is frozen", 0);
357                                         monster->m_flags |= (ASLEEP | NAPPING);
358                                         monster->nap_length = get_rand(3, 6);
359                                 } else {
360                                         damage = rogue.hp_current / 4;
361                                 }
362                         } else {
363                                 damage = -2;
364                         }
365                         if (damage != -1) {
366                                 sprintf(buf, "the %s hits the %s", s, mon_name(monster));
367                                 message(buf, 0);
368                                 mon_damage(monster, damage);
369                         }
370                 }
371         } else if ((row == rogue.row) && (col == rogue.col)) {
372                 if (rand_percent(10 + (3 * get_armor_class(rogue.armor)))) {
373                         sprintf(buf, "the %s misses", s);
374                         message(buf, 0);
375                         goto ND;
376                 } else {
377                         damage = get_rand(3, (3 * rogue.exp));
378                         if (ball == FIRE) {
379                                 damage = (damage * 3) / 2;
380                                 damage -= get_armor_class(rogue.armor);
381                         }
382                         sprintf(buf, "the %s hits", s);
383                         rogue_damage(damage, NULL,
384                                         ((ball == FIRE) ? KFIRE : HYPOTHERMIA));
385                         message(buf, 0);
386                 }
387         } else {
388                 short nrow, ncol;
389
390 ND:             for (i = 0; i < 10; i++) {
391                         dir = get_rand(0, DIRS-1);
392                         nrow = orow;
393                         ncol = ocol;
394                         get_dir_rc(dir, &nrow, &ncol, 1);
395                         if (((ncol >= 0) && (ncol <= DCOLS-1)) &&
396                                 (dungeon[nrow][ncol] != NOTHING) &&
397                                 (!(dungeon[nrow][ncol] & (VERTWALL | HORWALL)))) {
398                                 new_dir = dir;
399                                 break;
400                         }
401                 }
402                 if (new_dir != -1) {
403                         bounce(ball, new_dir, orow, ocol, r);
404                 }
405         }
406 }