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