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