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