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