Home | History | Annotate | Line # | Download | only in hack
hack.fight.c revision 1.11
      1 /*	$NetBSD: hack.fight.c,v 1.11 2009/06/07 20:31:10 dholland 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.11 2009/06/07 20:31:10 dholland 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(struct monst *magr, struct monst *mdef)
     78 {
     79 	const struct permonst *pa = magr->data, *pd = mdef->data;
     80 	int             didhit;
     81 	schar           tmp;
     82 	boolean         vis;
     83 
     84 	if (strchr("Eauy", pa->mlet))
     85 		return (0);
     86 	if (magr->mfroz)
     87 		return (0);	/* riv05!a3 */
     88 	tmp = pd->ac + pa->mlevel;
     89 	if (mdef->mconf || mdef->mfroz || mdef->msleep) {
     90 		tmp += 4;
     91 		if (mdef->msleep)
     92 			mdef->msleep = 0;
     93 	}
     94 	didhit = (tmp > rnd(20));
     95 	if (didhit)
     96 		mdef->msleep = 0;
     97 	vis = (cansee(magr->mx, magr->my) && cansee(mdef->mx, mdef->my));
     98 	if (vis) {
     99 		char            buf[BUFSZ];
    100 		if (mdef->mimic)
    101 			seemimic(mdef);
    102 		if (magr->mimic)
    103 			seemimic(magr);
    104 		(void) snprintf(buf, sizeof(buf), "%s %s", Monnam(magr),
    105 			       didhit ? "hits" : "misses");
    106 		pline("%s %s.", buf, monnam(mdef));
    107 	} else {
    108 		boolean         far = (dist(magr->mx, magr->my) > 15);
    109 		if (far != far_noise || moves - noisetime > 10) {
    110 			far_noise = far;
    111 			noisetime = moves;
    112 			pline("You hear some noises%s.",
    113 			      far ? " in the distance" : "");
    114 		}
    115 	}
    116 	if (didhit) {
    117 		if (magr->data->mlet == 'c' && !magr->cham) {
    118 			magr->mhpmax += 3;
    119 			if (vis)
    120 				pline("%s is turned to stone!", Monnam(mdef));
    121 			else if (mdef->mtame)
    122 				pline("You have a peculiarly sad feeling for a moment, then it passes.");
    123 			monstone(mdef);
    124 			didhit = 2;
    125 		} else if ((mdef->mhp -= d(pa->damn, pa->damd)) < 1) {
    126 			magr->mhpmax += 1 + rn2(pd->mlevel + 1);
    127 			if (magr->mtame && magr->mhpmax > 8 * pa->mlevel) {
    128 				if (pa == &li_dog)
    129 					magr->data = pa = &dog;
    130 				else if (pa == &dog)
    131 					magr->data = pa = &la_dog;
    132 			}
    133 			if (vis)
    134 				pline("%s is killed!", Monnam(mdef));
    135 			else if (mdef->mtame)
    136 				pline("You have a sad feeling for a moment, then it passes.");
    137 			mondied(mdef);
    138 			didhit = 2;
    139 		}
    140 	}
    141 	return (didhit);
    142 }
    143 
    144 /* drop (perhaps) a cadaver and remove monster */
    145 void
    146 mondied(struct monst *mdef)
    147 {
    148 	const struct permonst *pd = mdef->data;
    149 	if (letter(pd->mlet) && rn2(3)) {
    150 		(void) mkobj_at(pd->mlet, mdef->mx, mdef->my);
    151 		if (cansee(mdef->mx, mdef->my)) {
    152 			unpmon(mdef);
    153 			atl(mdef->mx, mdef->my, fobj->olet);
    154 		}
    155 		stackobj(fobj);
    156 	}
    157 	mondead(mdef);
    158 }
    159 
    160 /* drop a rock and remove monster */
    161 void
    162 monstone(struct monst *mdef)
    163 {
    164 	if (strchr(mlarge, mdef->data->mlet))
    165 		mksobj_at(ENORMOUS_ROCK, mdef->mx, mdef->my);
    166 	else
    167 		mksobj_at(ROCK, mdef->mx, mdef->my);
    168 	if (cansee(mdef->mx, mdef->my)) {
    169 		unpmon(mdef);
    170 		atl(mdef->mx, mdef->my, fobj->olet);
    171 	}
    172 	mondead(mdef);
    173 }
    174 
    175 
    176 int
    177 fightm(struct monst *mtmp)
    178 {
    179 	struct monst   *mon;
    180 	for (mon = fmon; mon; mon = mon->nmon)
    181 		if (mon != mtmp) {
    182 			if (DIST(mon->mx, mon->my, mtmp->mx, mtmp->my) < 3)
    183 				if (rn2(4))
    184 					return (hitmm(mtmp, mon));
    185 		}
    186 	return (-1);
    187 }
    188 
    189 /* u is hit by sth, but not a monster */
    190 int
    191 thitu(int tlev, int dam, const char *name)
    192 {
    193 	char            buf[BUFSZ];
    194 
    195 	setan(name, buf, sizeof(buf));
    196 	if (u.uac + tlev <= rnd(20)) {
    197 		if (Blind)
    198 			pline("It misses.");
    199 		else
    200 			pline("You are almost hit by %s!", buf);
    201 		return (0);
    202 	} else {
    203 		if (Blind)
    204 			pline("You are hit!");
    205 		else
    206 			pline("You are hit by %s!", buf);
    207 		losehp(dam, name);
    208 		return (1);
    209 	}
    210 }
    211 
    212 const char mlarge[] = "bCDdegIlmnoPSsTUwY',&";
    213 
    214 /* return TRUE if mon still alive */
    215 boolean
    216 hmon(struct monst *mon, struct obj *obj, int thrown)
    217 {
    218 	int tmp;
    219 	boolean         hittxt = FALSE;
    220 
    221 	if (!obj) {
    222 		tmp = rnd(2);	/* attack with bare hands */
    223 		if (mon->data->mlet == 'c' && !uarmg) {
    224 			pline("You hit the cockatrice with your bare hands.");
    225 			pline("You turn to stone ...");
    226 			done_in_by(mon);
    227 		}
    228 	} else if (obj->olet == WEAPON_SYM || obj->otyp == PICK_AXE) {
    229 		if (obj == uwep && (obj->otyp > SPEAR || obj->otyp < BOOMERANG))
    230 			tmp = rnd(2);
    231 		else {
    232 			if (strchr(mlarge, mon->data->mlet)) {
    233 				tmp = rnd(objects[obj->otyp].wldam);
    234 				if (obj->otyp == TWO_HANDED_SWORD)
    235 					tmp += d(2, 6);
    236 				else if (obj->otyp == FLAIL)
    237 					tmp += rnd(4);
    238 			} else {
    239 				tmp = rnd(objects[obj->otyp].wsdam);
    240 			}
    241 			tmp += obj->spe;
    242 			if (!thrown && obj == uwep && obj->otyp == BOOMERANG
    243 			    && !rn2(3)) {
    244 				pline("As you hit %s, the boomerang breaks into splinters.",
    245 				      monnam(mon));
    246 				freeinv(obj);
    247 				setworn((struct obj *) 0, obj->owornmask);
    248 				obfree(obj, (struct obj *) 0);
    249 				obj = NULL;
    250 				tmp++;
    251 			}
    252 		}
    253 		if (mon->data->mlet == 'O' && obj != NULL &&
    254 		    obj->otyp == TWO_HANDED_SWORD &&
    255 		    !strcmp(ONAME(obj), "Orcrist"))
    256 			tmp += rnd(10);
    257 	} else
    258 		switch (obj->otyp) {
    259 		case HEAVY_IRON_BALL:
    260 			tmp = rnd(25);
    261 			break;
    262 		case EXPENSIVE_CAMERA:
    263 			pline("You succeed in destroying your camera. Congratulations!");
    264 			freeinv(obj);
    265 			if (obj->owornmask)
    266 				setworn((struct obj *) 0, obj->owornmask);
    267 			obfree(obj, (struct obj *) 0);
    268 			return (TRUE);
    269 		case DEAD_COCKATRICE:
    270 			pline("You hit %s with the cockatrice corpse.",
    271 			      monnam(mon));
    272 			if (mon->data->mlet == 'c') {
    273 				tmp = 1;
    274 				hittxt = TRUE;
    275 				break;
    276 			}
    277 			pline("%s is turned to stone!", Monnam(mon));
    278 			killed(mon);
    279 			return (FALSE);
    280 		case CLOVE_OF_GARLIC:	/* no effect against demons */
    281 			if (strchr(UNDEAD, mon->data->mlet))
    282 				mon->mflee = 1;
    283 			tmp = 1;
    284 			break;
    285 		default:
    286 			/* non-weapons can damage because of their weight */
    287 			/* (but not too much) */
    288 			tmp = obj->owt / 10;
    289 			if (tmp < 1)
    290 				tmp = 1;
    291 			else
    292 				tmp = rnd(tmp);
    293 			if (tmp > 6)
    294 				tmp = 6;
    295 		}
    296 
    297 	/****** NOTE: perhaps obj is undefined!! (if !thrown && BOOMERANG) */
    298 
    299 	tmp += u.udaminc + dbon();
    300 	if (u.uswallow) {
    301 		if ((tmp -= u.uswldtim) <= 0) {
    302 			pline("Your arms are no longer able to hit.");
    303 			return (TRUE);
    304 		}
    305 	}
    306 	if (tmp < 1)
    307 		tmp = 1;
    308 	mon->mhp -= tmp;
    309 	if (mon->mhp < 1) {
    310 		killed(mon);
    311 		return (FALSE);
    312 	}
    313 	if (mon->mtame && (!mon->mflee || mon->mfleetim)) {
    314 		mon->mflee = 1;	/* Rick Richardson */
    315 		mon->mfleetim += 10 * rnd(tmp);
    316 	}
    317 	if (!hittxt) {
    318 		if (thrown) {
    319 			/* this assumes that we cannot throw plural things */
    320 			if (obj == NULL)
    321 				panic("thrown non-object");
    322 			hit(xname(obj) /* or: objects[obj->otyp].oc_name */ ,
    323 			    mon, exclam(tmp));
    324 		} else if (Blind)
    325 			pline("You hit it.");
    326 		else
    327 			pline("You hit %s%s", monnam(mon), exclam(tmp));
    328 	}
    329 	if (u.umconf && !thrown) {
    330 		if (!Blind) {
    331 			pline("Your hands stop glowing blue.");
    332 			if (!mon->mfroz && !mon->msleep)
    333 				pline("%s appears confused.", Monnam(mon));
    334 		}
    335 		mon->mconf = 1;
    336 		u.umconf = 0;
    337 	}
    338 	return (TRUE);		/* mon still alive */
    339 }
    340 
    341 /* try to attack; return FALSE if monster evaded */
    342 /* u.dx and u.dy must be set */
    343 int
    344 attack(struct monst *mtmp)
    345 {
    346 	schar           tmp;
    347 	boolean         malive = TRUE;
    348 	const struct permonst *mdat;
    349 	mdat = mtmp->data;
    350 
    351 	u_wipe_engr(3);		/* andrew@orca: prevent unlimited pick-axe
    352 				 * attacks */
    353 
    354 	if (mdat->mlet == 'L' && !mtmp->mfroz && !mtmp->msleep &&
    355 	    !mtmp->mconf && mtmp->mcansee && !rn2(7) &&
    356 	    (m_move(mtmp, 0) == 2 /* he died */ ||	/* he moved: */
    357 	     mtmp->mx != u.ux + u.dx || mtmp->my != u.uy + u.dy))
    358 		return (FALSE);
    359 
    360 	if (mtmp->mimic) {
    361 		if (!u.ustuck && !mtmp->mflee)
    362 			u.ustuck = mtmp;
    363 		switch (levl[u.ux + u.dx][u.uy + u.dy].scrsym) {
    364 		case '+':
    365 			pline("The door actually was a Mimic.");
    366 			break;
    367 		case '$':
    368 			pline("The chest was a Mimic!");
    369 			break;
    370 		default:
    371 			pline("Wait! That's a Mimic!");
    372 		}
    373 		wakeup(mtmp);	/* clears mtmp->mimic */
    374 		return (TRUE);
    375 	}
    376 	wakeup(mtmp);
    377 
    378 	if (mtmp->mhide && mtmp->mundetected) {
    379 		struct obj     *obj;
    380 
    381 		mtmp->mundetected = 0;
    382 		if ((obj = o_at(mtmp->mx, mtmp->my)) && !Blind)
    383 			pline("Wait! There's a %s hiding under %s!",
    384 			      mdat->mname, doname(obj));
    385 		return (TRUE);
    386 	}
    387 	tmp = u.uluck + u.ulevel + mdat->ac + abon();
    388 	if (uwep) {
    389 		if (uwep->olet == WEAPON_SYM || uwep->otyp == PICK_AXE)
    390 			tmp += uwep->spe;
    391 		if (uwep->otyp == TWO_HANDED_SWORD)
    392 			tmp -= 1;
    393 		else if (uwep->otyp == DAGGER)
    394 			tmp += 2;
    395 		else if (uwep->otyp == CRYSKNIFE)
    396 			tmp += 3;
    397 		else if (uwep->otyp == SPEAR &&
    398 			 strchr("XDne", mdat->mlet))
    399 			tmp += 2;
    400 	}
    401 	if (mtmp->msleep) {
    402 		mtmp->msleep = 0;
    403 		tmp += 2;
    404 	}
    405 	if (mtmp->mfroz) {
    406 		tmp += 4;
    407 		if (!rn2(10))
    408 			mtmp->mfroz = 0;
    409 	}
    410 	if (mtmp->mflee)
    411 		tmp += 2;
    412 	if (u.utrap)
    413 		tmp -= 3;
    414 
    415 	/* with a lot of luggage, your agility diminishes */
    416 	tmp -= (inv_weight() + 40) / 20;
    417 
    418 	if (tmp <= rnd(20) && !u.uswallow) {
    419 		if (Blind)
    420 			pline("You miss it.");
    421 		else
    422 			pline("You miss %s.", monnam(mtmp));
    423 	} else {
    424 		/* we hit the monster; be careful: it might die! */
    425 
    426 		if ((malive = hmon(mtmp, uwep, 0)) == TRUE) {
    427 			/* monster still alive */
    428 			if (!rn2(25) && mtmp->mhp < mtmp->mhpmax / 2) {
    429 				mtmp->mflee = 1;
    430 				if (!rn2(3))
    431 					mtmp->mfleetim = rnd(100);
    432 				if (u.ustuck == mtmp && !u.uswallow)
    433 					u.ustuck = 0;
    434 			}
    435 #ifndef NOWORM
    436 			if (mtmp->wormno)
    437 				cutworm(mtmp, u.ux + u.dx, u.uy + u.dy,
    438 					uwep ? uwep->otyp : 0);
    439 #endif	/* NOWORM */
    440 		}
    441 		if (mdat->mlet == 'a') {
    442 			if (rn2(2)) {
    443 				pline("You are splashed by the blob's acid!");
    444 				losehp_m(rnd(6), mtmp);
    445 				if (!rn2(30))
    446 					corrode_armor();
    447 			}
    448 			if (!rn2(6))
    449 				corrode_weapon();
    450 		}
    451 	}
    452 	if (malive && mdat->mlet == 'E' && canseemon(mtmp)
    453 	    && !mtmp->mcan && rn2(3)) {
    454 		if (mtmp->mcansee) {
    455 			pline("You are frozen by the floating eye's gaze!");
    456 			nomul((u.ulevel > 6 || rn2(4)) ? rn1(20, -21) : -200);
    457 		} else {
    458 			pline("The blinded floating eye cannot defend itself.");
    459 			if (!rn2(500))
    460 				if ((int) u.uluck > LUCKMIN)
    461 					u.uluck--;
    462 		}
    463 	}
    464 	return (TRUE);
    465 }
    466