Home | History | Annotate | Line # | Download | only in hack
hack.fight.c revision 1.8.14.1
      1 /*	$NetBSD: hack.fight.c,v 1.8.14.1 2009/06/29 23:22:24 snj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
      5  * Amsterdam
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions are
     10  * met:
     11  *
     12  * - Redistributions of source code must retain the above copyright notice,
     13  * this list of conditions and the following disclaimer.
     14  *
     15  * - 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  *
     19  * - Neither the name of the Stichting Centrum voor Wiskunde en
     20  * Informatica, nor the names of its contributors may be used to endorse or
     21  * promote products derived from this software without specific prior
     22  * written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     39  * All rights reserved.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. The name of the author may not be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 #ifndef lint
     66 __RCSID("$NetBSD: hack.fight.c,v 1.8.14.1 2009/06/29 23:22:24 snj Exp $");
     67 #endif				/* not lint */
     68 
     69 #include "hack.h"
     70 #include "extern.h"
     71 
     72 static boolean  far_noise;
     73 static long     noisetime;
     74 
     75 /* hitmm returns 0 (miss), 1 (hit), or 2 (kill) */
     76 int
     77 hitmm(magr, mdef)
     78 	struct monst   *magr, *mdef;
     79 {
     80 	const struct permonst *pa = magr->data, *pd = mdef->data;
     81 	int             didhit;
     82 	schar           tmp;
     83 	boolean         vis;
     84 
     85 	if (strchr("Eauy", pa->mlet))
     86 		return (0);
     87 	if (magr->mfroz)
     88 		return (0);	/* riv05!a3 */
     89 	tmp = pd->ac + pa->mlevel;
     90 	if (mdef->mconf || mdef->mfroz || mdef->msleep) {
     91 		tmp += 4;
     92 		if (mdef->msleep)
     93 			mdef->msleep = 0;
     94 	}
     95 	didhit = (tmp > rnd(20));
     96 	if (didhit)
     97 		mdef->msleep = 0;
     98 	vis = (cansee(magr->mx, magr->my) && cansee(mdef->mx, mdef->my));
     99 	if (vis) {
    100 		char            buf[BUFSZ];
    101 		if (mdef->mimic)
    102 			seemimic(mdef);
    103 		if (magr->mimic)
    104 			seemimic(magr);
    105 		(void) snprintf(buf, sizeof(buf), "%s %s", Monnam(magr),
    106 			       didhit ? "hits" : "misses");
    107 		pline("%s %s.", buf, monnam(mdef));
    108 	} else {
    109 		boolean         far = (dist(magr->mx, magr->my) > 15);
    110 		if (far != far_noise || moves - noisetime > 10) {
    111 			far_noise = far;
    112 			noisetime = moves;
    113 			pline("You hear some noises%s.",
    114 			      far ? " in the distance" : "");
    115 		}
    116 	}
    117 	if (didhit) {
    118 		if (magr->data->mlet == 'c' && !magr->cham) {
    119 			magr->mhpmax += 3;
    120 			if (vis)
    121 				pline("%s is turned to stone!", Monnam(mdef));
    122 			else if (mdef->mtame)
    123 				pline("You have a peculiarly sad feeling for a moment, then it passes.");
    124 			monstone(mdef);
    125 			didhit = 2;
    126 		} else if ((mdef->mhp -= d(pa->damn, pa->damd)) < 1) {
    127 			magr->mhpmax += 1 + rn2(pd->mlevel + 1);
    128 			if (magr->mtame && magr->mhpmax > 8 * pa->mlevel) {
    129 				if (pa == &li_dog)
    130 					magr->data = pa = &dog;
    131 				else if (pa == &dog)
    132 					magr->data = pa = &la_dog;
    133 			}
    134 			if (vis)
    135 				pline("%s is killed!", Monnam(mdef));
    136 			else if (mdef->mtame)
    137 				pline("You have a sad feeling for a moment, then it passes.");
    138 			mondied(mdef);
    139 			didhit = 2;
    140 		}
    141 	}
    142 	return (didhit);
    143 }
    144 
    145 /* drop (perhaps) a cadaver and remove monster */
    146 void
    147 mondied(mdef)
    148 	struct monst   *mdef;
    149 {
    150 	const struct permonst *pd = mdef->data;
    151 	if (letter(pd->mlet) && rn2(3)) {
    152 		(void) mkobj_at(pd->mlet, mdef->mx, mdef->my);
    153 		if (cansee(mdef->mx, mdef->my)) {
    154 			unpmon(mdef);
    155 			atl(mdef->mx, mdef->my, fobj->olet);
    156 		}
    157 		stackobj(fobj);
    158 	}
    159 	mondead(mdef);
    160 }
    161 
    162 /* drop a rock and remove monster */
    163 void
    164 monstone(mdef)
    165 	struct monst   *mdef;
    166 {
    167 	if (strchr(mlarge, mdef->data->mlet))
    168 		mksobj_at(ENORMOUS_ROCK, mdef->mx, mdef->my);
    169 	else
    170 		mksobj_at(ROCK, mdef->mx, mdef->my);
    171 	if (cansee(mdef->mx, mdef->my)) {
    172 		unpmon(mdef);
    173 		atl(mdef->mx, mdef->my, fobj->olet);
    174 	}
    175 	mondead(mdef);
    176 }
    177 
    178 
    179 int
    180 fightm(mtmp)
    181 	struct monst   *mtmp;
    182 {
    183 	struct monst   *mon;
    184 	for (mon = fmon; mon; mon = mon->nmon)
    185 		if (mon != mtmp) {
    186 			if (DIST(mon->mx, mon->my, mtmp->mx, mtmp->my) < 3)
    187 				if (rn2(4))
    188 					return (hitmm(mtmp, mon));
    189 		}
    190 	return (-1);
    191 }
    192 
    193 /* u is hit by sth, but not a monster */
    194 int
    195 thitu(tlev, dam, name)
    196 	int tlev, dam;
    197 	const char           *name;
    198 {
    199 	char            buf[BUFSZ];
    200 
    201 	setan(name, buf, sizeof(buf));
    202 	if (u.uac + tlev <= rnd(20)) {
    203 		if (Blind)
    204 			pline("It misses.");
    205 		else
    206 			pline("You are almost hit by %s!", buf);
    207 		return (0);
    208 	} else {
    209 		if (Blind)
    210 			pline("You are hit!");
    211 		else
    212 			pline("You are hit by %s!", buf);
    213 		losehp(dam, name);
    214 		return (1);
    215 	}
    216 }
    217 
    218 char            mlarge[] = "bCDdegIlmnoPSsTUwY',&";
    219 
    220 boolean
    221 hmon(mon, obj, thrown)		/* return TRUE if mon still alive */
    222 	struct monst   *mon;
    223 	struct obj     *obj;
    224 	int thrown;
    225 {
    226 	int tmp;
    227 	boolean         hittxt = FALSE;
    228 
    229 	if (!obj) {
    230 		tmp = rnd(2);	/* attack with bare hands */
    231 		if (mon->data->mlet == 'c' && !uarmg) {
    232 			pline("You hit the cockatrice with your bare hands.");
    233 			pline("You turn to stone ...");
    234 			done_in_by(mon);
    235 		}
    236 	} else if (obj->olet == WEAPON_SYM || obj->otyp == PICK_AXE) {
    237 		if (obj == uwep && (obj->otyp > SPEAR || obj->otyp < BOOMERANG))
    238 			tmp = rnd(2);
    239 		else {
    240 			if (strchr(mlarge, mon->data->mlet)) {
    241 				tmp = rnd(objects[obj->otyp].wldam);
    242 				if (obj->otyp == TWO_HANDED_SWORD)
    243 					tmp += d(2, 6);
    244 				else if (obj->otyp == FLAIL)
    245 					tmp += rnd(4);
    246 			} else {
    247 				tmp = rnd(objects[obj->otyp].wsdam);
    248 			}
    249 			tmp += obj->spe;
    250 			if (!thrown && obj == uwep && obj->otyp == BOOMERANG
    251 			    && !rn2(3)) {
    252 				pline("As you hit %s, the boomerang breaks into splinters.",
    253 				      monnam(mon));
    254 				freeinv(obj);
    255 				setworn((struct obj *) 0, obj->owornmask);
    256 				obfree(obj, (struct obj *) 0);
    257 				obj = NULL;
    258 				tmp++;
    259 			}
    260 		}
    261 		if (mon->data->mlet == 'O' && obj != NULL &&
    262 		    obj->otyp == TWO_HANDED_SWORD &&
    263 		    !strcmp(ONAME(obj), "Orcrist"))
    264 			tmp += rnd(10);
    265 	} else
    266 		switch (obj->otyp) {
    267 		case HEAVY_IRON_BALL:
    268 			tmp = rnd(25);
    269 			break;
    270 		case EXPENSIVE_CAMERA:
    271 			pline("You succeed in destroying your camera. Congratulations!");
    272 			freeinv(obj);
    273 			if (obj->owornmask)
    274 				setworn((struct obj *) 0, obj->owornmask);
    275 			obfree(obj, (struct obj *) 0);
    276 			return (TRUE);
    277 		case DEAD_COCKATRICE:
    278 			pline("You hit %s with the cockatrice corpse.",
    279 			      monnam(mon));
    280 			if (mon->data->mlet == 'c') {
    281 				tmp = 1;
    282 				hittxt = TRUE;
    283 				break;
    284 			}
    285 			pline("%s is turned to stone!", Monnam(mon));
    286 			killed(mon);
    287 			return (FALSE);
    288 		case CLOVE_OF_GARLIC:	/* no effect against demons */
    289 			if (strchr(UNDEAD, mon->data->mlet))
    290 				mon->mflee = 1;
    291 			tmp = 1;
    292 			break;
    293 		default:
    294 			/* non-weapons can damage because of their weight */
    295 			/* (but not too much) */
    296 			tmp = obj->owt / 10;
    297 			if (tmp < 1)
    298 				tmp = 1;
    299 			else
    300 				tmp = rnd(tmp);
    301 			if (tmp > 6)
    302 				tmp = 6;
    303 		}
    304 
    305 	/****** NOTE: perhaps obj is undefined!! (if !thrown && BOOMERANG) */
    306 
    307 	tmp += u.udaminc + dbon();
    308 	if (u.uswallow) {
    309 		if ((tmp -= u.uswldtim) <= 0) {
    310 			pline("Your arms are no longer able to hit.");
    311 			return (TRUE);
    312 		}
    313 	}
    314 	if (tmp < 1)
    315 		tmp = 1;
    316 	mon->mhp -= tmp;
    317 	if (mon->mhp < 1) {
    318 		killed(mon);
    319 		return (FALSE);
    320 	}
    321 	if (mon->mtame && (!mon->mflee || mon->mfleetim)) {
    322 		mon->mflee = 1;	/* Rick Richardson */
    323 		mon->mfleetim += 10 * rnd(tmp);
    324 	}
    325 	if (!hittxt) {
    326 		if (thrown) {
    327 			/* this assumes that we cannot throw plural things */
    328 			if (obj == NULL)
    329 				panic("thrown non-object");
    330 			hit(xname(obj) /* or: objects[obj->otyp].oc_name */ ,
    331 			    mon, exclam(tmp));
    332 		} else if (Blind)
    333 			pline("You hit it.");
    334 		else
    335 			pline("You hit %s%s", monnam(mon), exclam(tmp));
    336 	}
    337 	if (u.umconf && !thrown) {
    338 		if (!Blind) {
    339 			pline("Your hands stop glowing blue.");
    340 			if (!mon->mfroz && !mon->msleep)
    341 				pline("%s appears confused.", Monnam(mon));
    342 		}
    343 		mon->mconf = 1;
    344 		u.umconf = 0;
    345 	}
    346 	return (TRUE);		/* mon still alive */
    347 }
    348 
    349 /* try to attack; return FALSE if monster evaded */
    350 /* u.dx and u.dy must be set */
    351 int
    352 attack(mtmp)
    353 	struct monst   *mtmp;
    354 {
    355 	schar           tmp;
    356 	boolean         malive = TRUE;
    357 	const struct permonst *mdat;
    358 	mdat = mtmp->data;
    359 
    360 	u_wipe_engr(3);		/* andrew@orca: prevent unlimited pick-axe
    361 				 * attacks */
    362 
    363 	if (mdat->mlet == 'L' && !mtmp->mfroz && !mtmp->msleep &&
    364 	    !mtmp->mconf && mtmp->mcansee && !rn2(7) &&
    365 	    (m_move(mtmp, 0) == 2 /* he died */ ||	/* he moved: */
    366 	     mtmp->mx != u.ux + u.dx || mtmp->my != u.uy + u.dy))
    367 		return (FALSE);
    368 
    369 	if (mtmp->mimic) {
    370 		if (!u.ustuck && !mtmp->mflee)
    371 			u.ustuck = mtmp;
    372 		switch (levl[u.ux + u.dx][u.uy + u.dy].scrsym) {
    373 		case '+':
    374 			pline("The door actually was a Mimic.");
    375 			break;
    376 		case '$':
    377 			pline("The chest was a Mimic!");
    378 			break;
    379 		default:
    380 			pline("Wait! That's a Mimic!");
    381 		}
    382 		wakeup(mtmp);	/* clears mtmp->mimic */
    383 		return (TRUE);
    384 	}
    385 	wakeup(mtmp);
    386 
    387 	if (mtmp->mhide && mtmp->mundetected) {
    388 		struct obj     *obj;
    389 
    390 		mtmp->mundetected = 0;
    391 		if ((obj = o_at(mtmp->mx, mtmp->my)) && !Blind)
    392 			pline("Wait! There's a %s hiding under %s!",
    393 			      mdat->mname, doname(obj));
    394 		return (TRUE);
    395 	}
    396 	tmp = u.uluck + u.ulevel + mdat->ac + abon();
    397 	if (uwep) {
    398 		if (uwep->olet == WEAPON_SYM || uwep->otyp == PICK_AXE)
    399 			tmp += uwep->spe;
    400 		if (uwep->otyp == TWO_HANDED_SWORD)
    401 			tmp -= 1;
    402 		else if (uwep->otyp == DAGGER)
    403 			tmp += 2;
    404 		else if (uwep->otyp == CRYSKNIFE)
    405 			tmp += 3;
    406 		else if (uwep->otyp == SPEAR &&
    407 			 strchr("XDne", mdat->mlet))
    408 			tmp += 2;
    409 	}
    410 	if (mtmp->msleep) {
    411 		mtmp->msleep = 0;
    412 		tmp += 2;
    413 	}
    414 	if (mtmp->mfroz) {
    415 		tmp += 4;
    416 		if (!rn2(10))
    417 			mtmp->mfroz = 0;
    418 	}
    419 	if (mtmp->mflee)
    420 		tmp += 2;
    421 	if (u.utrap)
    422 		tmp -= 3;
    423 
    424 	/* with a lot of luggage, your agility diminishes */
    425 	tmp -= (inv_weight() + 40) / 20;
    426 
    427 	if (tmp <= rnd(20) && !u.uswallow) {
    428 		if (Blind)
    429 			pline("You miss it.");
    430 		else
    431 			pline("You miss %s.", monnam(mtmp));
    432 	} else {
    433 		/* we hit the monster; be careful: it might die! */
    434 
    435 		if ((malive = hmon(mtmp, uwep, 0)) == TRUE) {
    436 			/* monster still alive */
    437 			if (!rn2(25) && mtmp->mhp < mtmp->mhpmax / 2) {
    438 				mtmp->mflee = 1;
    439 				if (!rn2(3))
    440 					mtmp->mfleetim = rnd(100);
    441 				if (u.ustuck == mtmp && !u.uswallow)
    442 					u.ustuck = 0;
    443 			}
    444 #ifndef NOWORM
    445 			if (mtmp->wormno)
    446 				cutworm(mtmp, u.ux + u.dx, u.uy + u.dy,
    447 					uwep ? uwep->otyp : 0);
    448 #endif	/* NOWORM */
    449 		}
    450 		if (mdat->mlet == 'a') {
    451 			if (rn2(2)) {
    452 				pline("You are splashed by the blob's acid!");
    453 				losehp_m(rnd(6), mtmp);
    454 				if (!rn2(30))
    455 					corrode_armor();
    456 			}
    457 			if (!rn2(6))
    458 				corrode_weapon();
    459 		}
    460 	}
    461 	if (malive && mdat->mlet == 'E' && canseemon(mtmp)
    462 	    && !mtmp->mcan && rn2(3)) {
    463 		if (mtmp->mcansee) {
    464 			pline("You are frozen by the floating eye's gaze!");
    465 			nomul((u.ulevel > 6 || rn2(4)) ? rn1(20, -21) : -200);
    466 		} else {
    467 			pline("The blinded floating eye cannot defend itself.");
    468 			if (!rn2(500))
    469 				if ((int) u.uluck > LUCKMIN)
    470 					u.uluck--;
    471 		}
    472 	}
    473 	return (TRUE);
    474 }
    475