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