Home | History | Annotate | Line # | Download | only in rogue
zap.c revision 1.7
      1 /*	$NetBSD: zap.c,v 1.7 2007/12/27 23:53:01 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.7 2007/12/27 23:53:01 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 boolean wizard = 0;
     59 
     60 void
     61 zapp()
     62 {
     63 	short wch;
     64 	boolean first_miss = 1;
     65 	object *wand;
     66 	short dir, d, row, col;
     67 	object *monster;
     68 
     69 	while (!is_direction(dir = rgetchar(), &d)) {
     70 		sound_bell();
     71 		if (first_miss) {
     72 			messagef(0, "direction? ");
     73 			first_miss = 0;
     74 		}
     75 	}
     76 	check_message();
     77 	if (dir == CANCEL) {
     78 		return;
     79 	}
     80 	if ((wch = pack_letter("zap with what?", WAND)) == CANCEL) {
     81 		return;
     82 	}
     83 	check_message();
     84 
     85 	if (!(wand = get_letter_object(wch))) {
     86 		messagef(0, "no such item.");
     87 		return;
     88 	}
     89 	if (wand->what_is != WAND) {
     90 		messagef(0, "you can't zap with that");
     91 		return;
     92 	}
     93 	if (wand->class <= 0) {
     94 		messagef(0, "nothing happens");
     95 	} else {
     96 		wand->class--;
     97 		row = rogue.row; col = rogue.col;
     98 		if ((wand->which_kind == COLD) || (wand->which_kind == FIRE)) {
     99 			bounce((short) wand->which_kind, d, row, col, 0);
    100 		} else {
    101 			monster = get_zapped_monster(d, &row, &col);
    102 			if (wand->which_kind == DRAIN_LIFE) {
    103 				wdrain_life(monster);
    104 			} else if (monster) {
    105 				wake_up(monster);
    106 				s_con_mon(monster);
    107 				zap_monster(monster, wand->which_kind);
    108 				relight();
    109 			}
    110 		}
    111 	}
    112 	(void) reg_move();
    113 }
    114 
    115 object *
    116 get_zapped_monster(dir, row, col)
    117 	short dir;
    118 	short *row, *col;
    119 {
    120 	short orow, ocol;
    121 
    122 	for (;;) {
    123 		orow = *row; ocol = *col;
    124 		get_dir_rc(dir, row, col, 0);
    125 		if (((*row == orow) && (*col == ocol)) ||
    126 		   (dungeon[*row][*col] & (HORWALL | VERTWALL)) ||
    127 		   (dungeon[*row][*col] == NOTHING)) {
    128 			return(0);
    129 		}
    130 		if (dungeon[*row][*col] & MONSTER) {
    131 			if (!imitating(*row, *col)) {
    132 				return(object_at(&level_monsters, *row, *col));
    133 			}
    134 		}
    135 	}
    136 }
    137 
    138 void
    139 zap_monster(monster, kind)
    140 	object *monster;
    141 	unsigned short kind;
    142 {
    143 	short row, col;
    144 	object *nm;
    145 	short tc;
    146 
    147 	row = monster->row;
    148 	col = monster->col;
    149 
    150 	switch(kind) {
    151 	case SLOW_MONSTER:
    152 		if (monster->m_flags & HASTED) {
    153 			monster->m_flags &= (~HASTED);
    154 		} else {
    155 			monster->slowed_toggle = 0;
    156 			monster->m_flags |= SLOWED;
    157 		}
    158 		break;
    159 	case HASTE_MONSTER:
    160 		if (monster->m_flags & SLOWED) {
    161 			monster->m_flags &= (~SLOWED);
    162 		} else {
    163 			monster->m_flags |= HASTED;
    164 		}
    165 		break;
    166 	case TELE_AWAY:
    167 		tele_away(monster);
    168 		break;
    169 	case INVISIBILITY:
    170 		monster->m_flags |= INVISIBLE;
    171 		break;
    172 	case POLYMORPH:
    173 		if (monster->m_flags & HOLDS) {
    174 			being_held = 0;
    175 		}
    176 		nm = monster->next_monster;
    177 		tc = monster->trail_char;
    178 		(void) gr_monster(monster, get_rand(0, MONSTERS-1));
    179 		monster->row = row;
    180 		monster->col = col;
    181 		monster->next_monster = nm;
    182 		monster->trail_char = tc;
    183 		if (!(monster->m_flags & IMITATES)) {
    184 			wake_up(monster);
    185 		}
    186 		break;
    187 	case MAGIC_MISSILE:
    188 		rogue_hit(monster, 1);
    189 		break;
    190 	case CANCELLATION:
    191 		if (monster->m_flags & HOLDS) {
    192 			being_held = 0;
    193 		}
    194 		if (monster->m_flags & STEALS_ITEM) {
    195 			monster->drop_percent = 0;
    196 		}
    197 		monster->m_flags &= (~(FLIES | FLITS | SPECIAL_HIT | INVISIBLE |
    198 			FLAMES | IMITATES | CONFUSES | SEEKS_GOLD | HOLDS));
    199 		break;
    200 	case DO_NOTHING:
    201 		messagef(0, "nothing happens");
    202 		break;
    203 	}
    204 }
    205 
    206 void
    207 tele_away(monster)
    208 	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()
    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 void
    251 wdrain_life(monster)
    252 	object *monster;
    253 {
    254 	short hp;
    255 	object *lmon, *nm;
    256 
    257 	hp = rogue.hp_current / 3;
    258 	rogue.hp_current = (rogue.hp_current + 1) / 2;
    259 
    260 	if (cur_room >= 0) {
    261 		lmon = level_monsters.next_monster;
    262 		while (lmon) {
    263 			nm = lmon->next_monster;
    264 			if (get_room_number(lmon->row, lmon->col) == cur_room) {
    265 				wake_up(lmon);
    266 				(void) mon_damage(lmon, hp);
    267 			}
    268 			lmon = nm;
    269 		}
    270 	} else {
    271 		if (monster) {
    272 			wake_up(monster);
    273 			(void) mon_damage(monster, hp);
    274 		}
    275 	}
    276 	print_stats(STAT_HP);
    277 	relight();
    278 }
    279 
    280 void
    281 bounce(ball, dir, row, col, r)
    282 	short ball, dir, row, col, r;
    283 {
    284 	short orow, ocol;
    285 	const char *s;
    286 	short i, ch, new_dir = -1, damage;
    287 	static short btime;
    288 
    289 	if (++r == 1) {
    290 		btime = get_rand(3, 6);
    291 	} else if (r > btime) {
    292 		return;
    293 	}
    294 
    295 	if (ball == FIRE) {
    296 		s = "fire";
    297 	} else {
    298 		s = "ice";
    299 	}
    300 	if (r > 1) {
    301 		messagef(0, "the %s bounces", s);
    302 	}
    303 	orow = row;
    304 	ocol = col;
    305 	do {
    306 		ch = mvinch(orow, ocol);
    307 		standout();
    308 		mvaddch(orow, ocol, ch);
    309 		get_dir_rc(dir, &orow, &ocol, 1);
    310 	} while (!(	(ocol <= 0) ||
    311 				(ocol >= DCOLS-1) ||
    312 				(dungeon[orow][ocol] == NOTHING) ||
    313 				(dungeon[orow][ocol] & MONSTER) ||
    314 				(dungeon[orow][ocol] & (HORWALL | VERTWALL)) ||
    315 				((orow == rogue.row) && (ocol == rogue.col))));
    316 	standend();
    317 	refresh();
    318 	do {
    319 		orow = row;
    320 		ocol = col;
    321 		ch = mvinch(row, col);
    322 		mvaddch(row, col, ch);
    323 		get_dir_rc(dir, &row, &col, 1);
    324 	} while (!(	(col <= 0) ||
    325 				(col >= DCOLS-1) ||
    326 				(dungeon[row][col] == NOTHING) ||
    327 				(dungeon[row][col] & MONSTER) ||
    328 				(dungeon[row][col] & (HORWALL | VERTWALL)) ||
    329 				((row == rogue.row) && (col == rogue.col))));
    330 
    331 	if (dungeon[row][col] & MONSTER) {
    332 		object *monster;
    333 
    334 		monster = object_at(&level_monsters, row, col);
    335 
    336 		wake_up(monster);
    337 		if (rand_percent(33)) {
    338 			messagef(0, "the %s misses the %s", s,
    339 				mon_name(monster));
    340 			goto ND;
    341 		}
    342 		if (ball == FIRE) {
    343 			if (!(monster->m_flags & RUSTS)) {
    344 				if (monster->m_flags & FREEZES) {
    345 					damage = monster->hp_to_kill;
    346 				} else if (monster->m_flags & FLAMES) {
    347 					damage = (monster->hp_to_kill / 10) + 1;
    348 				} else {
    349 					damage = get_rand((rogue.hp_current / 3), rogue.hp_max);
    350 				}
    351 			} else {
    352 				damage = (monster->hp_to_kill / 2) + 1;
    353 			}
    354 			messagef(0, "the %s hits the %s", s,
    355 				mon_name(monster));
    356 			(void) mon_damage(monster, damage);
    357 		} else {
    358 			damage = -1;
    359 			if (!(monster->m_flags & FREEZES)) {
    360 				if (rand_percent(33)) {
    361 					messagef(0, "the monster is frozen");
    362 					monster->m_flags |= (ASLEEP | NAPPING);
    363 					monster->nap_length = get_rand(3, 6);
    364 				} else {
    365 					damage = rogue.hp_current / 4;
    366 				}
    367 			} else {
    368 				damage = -2;
    369 			}
    370 			if (damage != -1) {
    371 				messagef(0, "the %s hits the %s", s,
    372 					mon_name(monster));
    373 				(void) mon_damage(monster, damage);
    374 			}
    375 		}
    376 	} else if ((row == rogue.row) && (col == rogue.col)) {
    377 		if (rand_percent(10 + (3 * get_armor_class(rogue.armor)))) {
    378 			messagef(0, "the %s misses", s);
    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 			rogue_damage(damage, (object *) 0,
    387 					((ball == FIRE) ? KFIRE : HYPOTHERMIA));
    388 			messagef(0, "the %s hits", s);
    389 		}
    390 	} else {
    391 		short nrow, ncol;
    392 
    393 ND:		for (i = 0; i < 10; i++) {
    394 			dir = get_rand(0, DIRS-1);
    395 			nrow = orow;
    396 			ncol = ocol;
    397 			get_dir_rc(dir, &nrow, &ncol, 1);
    398 			if (((ncol >= 0) && (ncol <= DCOLS-1)) &&
    399 				(dungeon[nrow][ncol] != NOTHING) &&
    400 				(!(dungeon[nrow][ncol] & (VERTWALL | HORWALL)))) {
    401 				new_dir = dir;
    402 				break;
    403 			}
    404 		}
    405 		if (new_dir != -1) {
    406 			bounce(ball, new_dir, orow, ocol, r);
    407 		}
    408 	}
    409 }
    410