Initial import from FreeBSD RELENG_4:
[games.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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)zap.c       8.1 (Berkeley) 5/31/93";
40 #endif
41 static const char rcsid[] =
42  "$FreeBSD: src/games/rogue/zap.c,v 1.3 1999/11/30 03:49:29 billf Exp $";
43 #endif /* not lint */
44
45 /*
46  * zap.c
47  *
48  * This source herein may be modified and/or distributed by anybody who
49  * so desires, with the following restrictions:
50  *    1.)  No portion of this notice shall be removed.
51  *    2.)  Credit shall not be taken for the creation of this source.
52  *    3.)  This code is not to be traded, sold, or used for personal
53  *         gain or profit.
54  *
55  */
56
57 #include "rogue.h"
58
59 boolean wizard = 0;
60
61 extern boolean being_held, score_only, detect_monster;
62 extern short cur_room;
63
64 zapp()
65 {
66         short wch;
67         boolean first_miss = 1;
68         object *wand;
69         short dir, d, row, col;
70         object *monster;
71
72         while (!is_direction(dir = rgetchar(), &d)) {
73                 sound_bell();
74                 if (first_miss) {
75                         message("direction? ", 0);
76                         first_miss = 0;
77                 }
78         }
79         check_message();
80         if (dir == CANCEL) {
81                 return;
82         }
83         if ((wch = pack_letter("zap with what?", WAND)) == CANCEL) {
84                 return;
85         }
86         check_message();
87
88         if (!(wand = get_letter_object(wch))) {
89                 message("no such item.", 0);
90                 return;
91         }
92         if (wand->what_is != WAND) {
93                 message("you can't zap with that", 0);
94                 return;
95         }
96         if (wand->class <= 0) {
97                 message("nothing happens", 0);
98         } else {
99                 wand->class--;
100                 row = rogue.row; col = rogue.col;
101                 if ((wand->which_kind == COLD) || (wand->which_kind == FIRE)) {
102                         bounce((short) wand->which_kind, d, row, col, 0);
103                 } else {
104                         monster = get_zapped_monster(d, &row, &col);
105                         if (wand->which_kind == DRAIN_LIFE) {
106                                 wdrain_life(monster);
107                         } else if (monster) {
108                                 wake_up(monster);
109                                 s_con_mon(monster);
110                                 zap_monster(monster, wand->which_kind);
111                                 relight();
112                         }
113                 }
114         }
115         (void) reg_move();
116 }
117
118 object *
119 get_zapped_monster(dir, row, col)
120 short dir;
121 short *row, *col;
122 {
123         short orow, ocol;
124
125         for (;;) {
126                 orow = *row; ocol = *col;
127                 get_dir_rc(dir, row, col, 0);
128                 if (((*row == orow) && (*col == ocol)) ||
129                    (dungeon[*row][*col] & (HORWALL | VERTWALL)) ||
130                    (dungeon[*row][*col] == NOTHING)) {
131                         return(0);
132                 }
133                 if (dungeon[*row][*col] & MONSTER) {
134                         if (!imitating(*row, *col)) {
135                                 return(object_at(&level_monsters, *row, *col));
136                         }
137                 }
138         }
139 }
140
141 zap_monster(monster, kind)
142 object *monster;
143 unsigned short kind;
144 {
145         short row, col;
146         object *nm;
147         short tc;
148
149         row = monster->row;
150         col = monster->col;
151
152         switch(kind) {
153         case SLOW_MONSTER:
154                 if (monster->m_flags & HASTED) {
155                         monster->m_flags &= (~HASTED);
156                 } else {
157                         monster->slowed_toggle = 0;
158                         monster->m_flags |= SLOWED;
159                 }
160                 break;
161         case HASTE_MONSTER:
162                 if (monster->m_flags & SLOWED) {
163                         monster->m_flags &= (~SLOWED);
164                 } else {
165                         monster->m_flags |= HASTED;
166                 }
167                 break;
168         case TELE_AWAY:
169                 tele_away(monster);
170                 break;
171         case INVISIBILITY:
172                 monster->m_flags |= INVISIBLE;
173                 break;
174         case POLYMORPH:
175                 if (monster->m_flags & HOLDS) {
176                         being_held = 0;
177                 }
178                 nm = monster->next_monster;
179                 tc = monster->trail_char;
180                 (void) gr_monster(monster, get_rand(0, MONSTERS-1));
181                 monster->row = row;
182                 monster->col = col;
183                 monster->next_monster = nm;
184                 monster->trail_char = tc;
185                 if (!(monster->m_flags & IMITATES)) {
186                         wake_up(monster);
187                 }
188                 break;
189         case MAGIC_MISSILE:
190                 rogue_hit(monster, 1);
191                 break;
192         case CANCELLATION:
193                 if (monster->m_flags & HOLDS) {
194                         being_held = 0;
195                 }
196                 if (monster->m_flags & STEALS_ITEM) {
197                         monster->drop_percent = 0;
198                 }
199                 monster->m_flags &= (~(FLIES | FLITS | SPECIAL_HIT | INVISIBLE |
200                         FLAMES | IMITATES | CONFUSES | SEEKS_GOLD | HOLDS));
201                 break;
202         case DO_NOTHING:
203                 message("nothing happens", 0);
204                 break;
205         }
206 }
207
208 tele_away(monster)
209 object *monster;
210 {
211         short row, col;
212
213         if (monster->m_flags & HOLDS) {
214                 being_held = 0;
215         }
216         gr_row_col(&row, &col, (FLOOR | TUNNEL | STAIRS | OBJECT));
217         mvaddch(monster->row, monster->col, monster->trail_char);
218         dungeon[monster->row][monster->col] &= ~MONSTER;
219         monster->row = row; monster->col = col;
220         dungeon[row][col] |= MONSTER;
221         monster->trail_char = mvinch(row, col);
222         if (detect_monster || rogue_can_see(row, col)) {
223                 mvaddch(row, col, gmc(monster));
224         }
225 }
226
227 wizardize()
228 {
229         char buf[100];
230
231         if (wizard) {
232                 wizard = 0;
233                 message("not wizard anymore", 0);
234         } else {
235                 if (get_input_line("wizard's password:", "", buf, "", 0, 0)) {
236                         (void) xxx(1);
237                         xxxx(buf, strlen(buf));
238                         if (!strncmp(buf, "\247\104\126\272\115\243\027", 7)) {
239                                 wizard = 1;
240                                 score_only = 1;
241                                 message("Welcome, mighty wizard!", 0);
242                         } else {
243                                 message("sorry", 0);
244                         }
245                 }
246         }
247 }
248
249 wdrain_life(monster)
250 object *monster;
251 {
252         short hp;
253         object *lmon, *nm;
254
255         hp = rogue.hp_current / 3;
256         rogue.hp_current = (rogue.hp_current + 1) / 2;
257
258         if (cur_room >= 0) {
259                 lmon = level_monsters.next_monster;
260                 while (lmon) {
261                         nm = lmon->next_monster;
262                         if (get_room_number(lmon->row, lmon->col) == cur_room) {
263                                 wake_up(lmon);
264                                 (void) mon_damage(lmon, hp);
265                         }
266                         lmon = nm;
267                 }
268         } else {
269                 if (monster) {
270                         wake_up(monster);
271                         (void) mon_damage(monster, hp);
272                 }
273         }
274         print_stats(STAT_HP);
275         relight();
276 }
277
278 bounce(ball, dir, row, col, r)
279 short ball, dir, row, col, r;
280 {
281         short orow, ocol;
282         char buf[DCOLS];
283         const char *s;
284         short i, ch, new_dir = -1, damage;
285         static short btime;
286
287         if (++r == 1) {
288                 btime = get_rand(3, 6);
289         } else if (r > btime) {
290                 return;
291         }
292
293         if (ball == FIRE) {
294                 s = "fire";
295         } else {
296                 s = "ice";
297         }
298         if (r > 1) {
299                 sprintf(buf, "the %s bounces", s);
300                 message(buf, 0);
301         }
302         orow = row;
303         ocol = col;
304         do {
305                 ch = mvinch(orow, ocol);
306                 standout();
307                 mvaddch(orow, ocol, ch);
308                 get_dir_rc(dir, &orow, &ocol, 1);
309         } while (!(     (ocol <= 0) ||
310                                 (ocol >= DCOLS-1) ||
311                                 (dungeon[orow][ocol] == NOTHING) ||
312                                 (dungeon[orow][ocol] & MONSTER) ||
313                                 (dungeon[orow][ocol] & (HORWALL | VERTWALL)) ||
314                                 ((orow == rogue.row) && (ocol == rogue.col))));
315         standend();
316         refresh();
317         do {
318                 orow = row;
319                 ocol = col;
320                 ch = mvinch(row, col);
321                 mvaddch(row, col, ch);
322                 get_dir_rc(dir, &row, &col, 1);
323         } while (!(     (col <= 0) ||
324                                 (col >= DCOLS-1) ||
325                                 (dungeon[row][col] == NOTHING) ||
326                                 (dungeon[row][col] & MONSTER) ||
327                                 (dungeon[row][col] & (HORWALL | VERTWALL)) ||
328                                 ((row == rogue.row) && (col == rogue.col))));
329
330         if (dungeon[row][col] & MONSTER) {
331                 object *monster;
332
333                 monster = object_at(&level_monsters, row, col);
334
335                 wake_up(monster);
336                 if (rand_percent(33)) {
337                         sprintf(buf, "the %s misses the %s", s, mon_name(monster));
338                         message(buf, 0);
339                         goto ND;
340                 }
341                 if (ball == FIRE) {
342                         if (!(monster->m_flags & RUSTS)) {
343                                 if (monster->m_flags & FREEZES) {
344                                         damage = monster->hp_to_kill;
345                                 } else if (monster->m_flags & FLAMES) {
346                                         damage = (monster->hp_to_kill / 10) + 1;
347                                 } else {
348                                         damage = get_rand((rogue.hp_current / 3), rogue.hp_max);
349                                 }
350                         } else {
351                                 damage = (monster->hp_to_kill / 2) + 1;
352                         }
353                         sprintf(buf, "the %s hits the %s", s, mon_name(monster));
354                         message(buf, 0);
355                         (void) mon_damage(monster, damage);
356                 } else {
357                         damage = -1;
358                         if (!(monster->m_flags & FREEZES)) {
359                                 if (rand_percent(33)) {
360                                         message("the monster is frozen", 0);
361                                         monster->m_flags |= (ASLEEP | NAPPING);
362                                         monster->nap_length = get_rand(3, 6);
363                                 } else {
364                                         damage = rogue.hp_current / 4;
365                                 }
366                         } else {
367                                 damage = -2;
368                         }
369                         if (damage != -1) {
370                                 sprintf(buf, "the %s hits the %s", s, mon_name(monster));
371                                 message(buf, 0);
372                                 (void) mon_damage(monster, damage);
373                         }
374                 }
375         } else if ((row == rogue.row) && (col == rogue.col)) {
376                 if (rand_percent(10 + (3 * get_armor_class(rogue.armor)))) {
377                         sprintf(buf, "the %s misses", s);
378                         message(buf, 0);
379                         goto ND;
380                 } else {
381                         damage = get_rand(3, (3 * rogue.exp));
382                         if (ball == FIRE) {
383                                 damage = (damage * 3) / 2;
384                                 damage -= get_armor_class(rogue.armor);
385                         }
386                         sprintf(buf, "the %s hits", s);
387                         rogue_damage(damage, (object *) 0,
388                                         ((ball == FIRE) ? KFIRE : HYPOTHERMIA));
389                         message(buf, 0);
390                 }
391         } else {
392                 short nrow, ncol;
393
394 ND:             for (i = 0; i < 10; i++) {
395                         dir = get_rand(0, DIRS-1);
396                         nrow = orow;
397                         ncol = ocol;
398                         get_dir_rc(dir, &nrow, &ncol, 1);
399                         if (((ncol >= 0) && (ncol <= DCOLS-1)) &&
400                                 (dungeon[nrow][ncol] != NOTHING) &&
401                                 (!(dungeon[nrow][ncol] & (VERTWALL | HORWALL)))) {
402                                 new_dir = dir;
403                                 break;
404                         }
405                 }
406                 if (new_dir != -1) {
407                         bounce(ball, new_dir, orow, ocol, r);
408                 }
409         }
410 }