Home | History | Annotate | Line # | Download | only in rogue
zap.c revision 1.3
      1 /*	$NetBSD: zap.c,v 1.3 1995/04/22 10:28:41 cgd 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)zap.c	8.1 (Berkeley) 5/31/93";
     42 #else
     43 static char rcsid[] = "$NetBSD: zap.c,v 1.3 1995/04/22 10:28:41 cgd Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * zap.c
     49  *
     50  * This source herein may be modified and/or distributed by anybody who
     51  * so desires, with the following restrictions:
     52  *    1.)  No portion of this notice shall be removed.
     53  *    2.)  Credit shall not be taken for the creation of this source.
     54  *    3.)  This code is not to be traded, sold, or used for personal
     55  *         gain or profit.
     56  *
     57  */
     58 
     59 #include "rogue.h"
     60 
     61 boolean wizard = 0;
     62 
     63 extern boolean being_held, score_only, detect_monster;
     64 extern short cur_room;
     65 
     66 zapp()
     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 			message("direction? ", 0);
     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 		message("no such item.", 0);
     92 		return;
     93 	}
     94 	if (wand->what_is != WAND) {
     95 		message("you can't zap with that", 0);
     96 		return;
     97 	}
     98 	if (wand->class <= 0) {
     99 		message("nothing happens", 0);
    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 object *
    121 get_zapped_monster(dir, row, col)
    122 short dir;
    123 short *row, *col;
    124 {
    125 	short orow, ocol;
    126 
    127 	for (;;) {
    128 		orow = *row; ocol = *col;
    129 		get_dir_rc(dir, row, col, 0);
    130 		if (((*row == orow) && (*col == ocol)) ||
    131 		   (dungeon[*row][*col] & (HORWALL | VERTWALL)) ||
    132 		   (dungeon[*row][*col] == NOTHING)) {
    133 			return(0);
    134 		}
    135 		if (dungeon[*row][*col] & MONSTER) {
    136 			if (!imitating(*row, *col)) {
    137 				return(object_at(&level_monsters, *row, *col));
    138 			}
    139 		}
    140 	}
    141 }
    142 
    143 zap_monster(monster, kind)
    144 object *monster;
    145 unsigned short kind;
    146 {
    147 	short row, col;
    148 	object *nm;
    149 	short tc;
    150 
    151 	row = monster->row;
    152 	col = monster->col;
    153 
    154 	switch(kind) {
    155 	case SLOW_MONSTER:
    156 		if (monster->m_flags & HASTED) {
    157 			monster->m_flags &= (~HASTED);
    158 		} else {
    159 			monster->slowed_toggle = 0;
    160 			monster->m_flags |= SLOWED;
    161 		}
    162 		break;
    163 	case HASTE_MONSTER:
    164 		if (monster->m_flags & SLOWED) {
    165 			monster->m_flags &= (~SLOWED);
    166 		} else {
    167 			monster->m_flags |= HASTED;
    168 		}
    169 		break;
    170 	case TELE_AWAY:
    171 		tele_away(monster);
    172 		break;
    173 	case INVISIBILITY:
    174 		monster->m_flags |= INVISIBLE;
    175 		break;
    176 	case POLYMORPH:
    177 		if (monster->m_flags & HOLDS) {
    178 			being_held = 0;
    179 		}
    180 		nm = monster->next_monster;
    181 		tc = monster->trail_char;
    182 		(void) gr_monster(monster, get_rand(0, MONSTERS-1));
    183 		monster->row = row;
    184 		monster->col = col;
    185 		monster->next_monster = nm;
    186 		monster->trail_char = tc;
    187 		if (!(monster->m_flags & IMITATES)) {
    188 			wake_up(monster);
    189 		}
    190 		break;
    191 	case MAGIC_MISSILE:
    192 		rogue_hit(monster, 1);
    193 		break;
    194 	case CANCELLATION:
    195 		if (monster->m_flags & HOLDS) {
    196 			being_held = 0;
    197 		}
    198 		if (monster->m_flags & STEALS_ITEM) {
    199 			monster->drop_percent = 0;
    200 		}
    201 		monster->m_flags &= (~(FLIES | FLITS | SPECIAL_HIT | INVISIBLE |
    202 			FLAMES | IMITATES | CONFUSES | SEEKS_GOLD | HOLDS));
    203 		break;
    204 	case DO_NOTHING:
    205 		message("nothing happens", 0);
    206 		break;
    207 	}
    208 }
    209 
    210 tele_away(monster)
    211 object *monster;
    212 {
    213 	short row, col;
    214 
    215 	if (monster->m_flags & HOLDS) {
    216 		being_held = 0;
    217 	}
    218 	gr_row_col(&row, &col, (FLOOR | TUNNEL | STAIRS | OBJECT));
    219 	mvaddch(monster->row, monster->col, monster->trail_char);
    220 	dungeon[monster->row][monster->col] &= ~MONSTER;
    221 	monster->row = row; monster->col = col;
    222 	dungeon[row][col] |= MONSTER;
    223 	monster->trail_char = mvinch(row, col);
    224 	if (detect_monster || rogue_can_see(row, col)) {
    225 		mvaddch(row, col, gmc(monster));
    226 	}
    227 }
    228 
    229 wizardize()
    230 {
    231 	char buf[100];
    232 
    233 	if (wizard) {
    234 		wizard = 0;
    235 		message("not wizard anymore", 0);
    236 	} else {
    237 		if (get_input_line("wizard's password:", "", buf, "", 0, 0)) {
    238 			(void) xxx(1);
    239 			xxxx(buf, strlen(buf));
    240 			if (!strncmp(buf, "\247\104\126\272\115\243\027", 7)) {
    241 				wizard = 1;
    242 				score_only = 1;
    243 				message("Welcome, mighty wizard!", 0);
    244 			} else {
    245 				message("sorry", 0);
    246 			}
    247 		}
    248 	}
    249 }
    250 
    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 bounce(ball, dir, row, col, r)
    281 short ball, dir, row, col, r;
    282 {
    283 	short orow, ocol;
    284 	char buf[DCOLS], *s;
    285 	short i, ch, new_dir = -1, damage;
    286 	static short btime;
    287 
    288 	if (++r == 1) {
    289 		btime = get_rand(3, 6);
    290 	} else if (r > btime) {
    291 		return;
    292 	}
    293 
    294 	if (ball == FIRE) {
    295 		s = "fire";
    296 	} else {
    297 		s = "ice";
    298 	}
    299 	if (r > 1) {
    300 		sprintf(buf, "the %s bounces", s);
    301 		message(buf, 0);
    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 			sprintf(buf, "the %s misses the %s", s, mon_name(monster));
    339 			message(buf, 0);
    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 			sprintf(buf, "the %s hits the %s", s, mon_name(monster));
    355 			message(buf, 0);
    356 			(void) mon_damage(monster, damage);
    357 		} else {
    358 			damage = -1;
    359 			if (!(monster->m_flags & FREEZES)) {
    360 				if (rand_percent(33)) {
    361 					message("the monster is frozen", 0);
    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 				sprintf(buf, "the %s hits the %s", s, mon_name(monster));
    372 				message(buf, 0);
    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 			sprintf(buf, "the %s misses", s);
    379 			message(buf, 0);
    380 			goto ND;
    381 		} else {
    382 			damage = get_rand(3, (3 * rogue.exp));
    383 			if (ball == FIRE) {
    384 				damage = (damage * 3) / 2;
    385 				damage -= get_armor_class(rogue.armor);
    386 			}
    387 			sprintf(buf, "the %s hits", s);
    388 			rogue_damage(damage, (object *) 0,
    389 					((ball == FIRE) ? KFIRE : HYPOTHERMIA));
    390 			message(buf, 0);
    391 		}
    392 	} else {
    393 		short nrow, ncol;
    394 
    395 ND:		for (i = 0; i < 10; i++) {
    396 			dir = get_rand(0, DIRS-1);
    397 			nrow = orow;
    398 			ncol = ocol;
    399 			get_dir_rc(dir, &nrow, &ncol, 1);
    400 			if (((ncol >= 0) && (ncol <= DCOLS-1)) &&
    401 				(dungeon[nrow][ncol] != NOTHING) &&
    402 				(!(dungeon[nrow][ncol] & (VERTWALL | HORWALL)))) {
    403 				new_dir = dir;
    404 				break;
    405 			}
    406 		}
    407 		if (new_dir != -1) {
    408 			bounce(ball, new_dir, orow, ocol, r);
    409 		}
    410 	}
    411 }
    412