]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.fight.c
ANSIfy function declarations. All object file diffs inspected.
[bsdgames-darwin.git] / hack / hack.fight.c
1 /* $NetBSD: hack.fight.c,v 1.9 2009/06/07 18:30:39 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 <sys/cdefs.h>
65 #ifndef lint
66 __RCSID("$NetBSD: hack.fight.c,v 1.9 2009/06/07 18:30:39 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) sprintf(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 setan(name, buf);
195 if (u.uac + tlev <= rnd(20)) {
196 if (Blind)
197 pline("It misses.");
198 else
199 pline("You are almost hit by %s!", buf);
200 return (0);
201 } else {
202 if (Blind)
203 pline("You are hit!");
204 else
205 pline("You are hit by %s!", buf);
206 losehp(dam, name);
207 return (1);
208 }
209 }
210
211 char mlarge[] = "bCDdegIlmnoPSsTUwY',&";
212
213 /* return TRUE if mon still alive */
214 boolean
215 hmon(struct monst *mon, struct obj *obj, int thrown)
216 {
217 int tmp;
218 boolean hittxt = FALSE;
219
220 if (!obj) {
221 tmp = rnd(2); /* attack with bare hands */
222 if (mon->data->mlet == 'c' && !uarmg) {
223 pline("You hit the cockatrice with your bare hands.");
224 pline("You turn to stone ...");
225 done_in_by(mon);
226 }
227 } else if (obj->olet == WEAPON_SYM || obj->otyp == PICK_AXE) {
228 if (obj == uwep && (obj->otyp > SPEAR || obj->otyp < BOOMERANG))
229 tmp = rnd(2);
230 else {
231 if (strchr(mlarge, mon->data->mlet)) {
232 tmp = rnd(objects[obj->otyp].wldam);
233 if (obj->otyp == TWO_HANDED_SWORD)
234 tmp += d(2, 6);
235 else if (obj->otyp == FLAIL)
236 tmp += rnd(4);
237 } else {
238 tmp = rnd(objects[obj->otyp].wsdam);
239 }
240 tmp += obj->spe;
241 if (!thrown && obj == uwep && obj->otyp == BOOMERANG
242 && !rn2(3)) {
243 pline("As you hit %s, the boomerang breaks into splinters.",
244 monnam(mon));
245 freeinv(obj);
246 setworn((struct obj *) 0, obj->owornmask);
247 obfree(obj, (struct obj *) 0);
248 obj = NULL;
249 tmp++;
250 }
251 }
252 if (mon->data->mlet == 'O' && obj != NULL &&
253 obj->otyp == TWO_HANDED_SWORD &&
254 !strcmp(ONAME(obj), "Orcrist"))
255 tmp += rnd(10);
256 } else
257 switch (obj->otyp) {
258 case HEAVY_IRON_BALL:
259 tmp = rnd(25);
260 break;
261 case EXPENSIVE_CAMERA:
262 pline("You succeed in destroying your camera. Congratulations!");
263 freeinv(obj);
264 if (obj->owornmask)
265 setworn((struct obj *) 0, obj->owornmask);
266 obfree(obj, (struct obj *) 0);
267 return (TRUE);
268 case DEAD_COCKATRICE:
269 pline("You hit %s with the cockatrice corpse.",
270 monnam(mon));
271 if (mon->data->mlet == 'c') {
272 tmp = 1;
273 hittxt = TRUE;
274 break;
275 }
276 pline("%s is turned to stone!", Monnam(mon));
277 killed(mon);
278 return (FALSE);
279 case CLOVE_OF_GARLIC: /* no effect against demons */
280 if (strchr(UNDEAD, mon->data->mlet))
281 mon->mflee = 1;
282 tmp = 1;
283 break;
284 default:
285 /* non-weapons can damage because of their weight */
286 /* (but not too much) */
287 tmp = obj->owt / 10;
288 if (tmp < 1)
289 tmp = 1;
290 else
291 tmp = rnd(tmp);
292 if (tmp > 6)
293 tmp = 6;
294 }
295
296 /****** NOTE: perhaps obj is undefined!! (if !thrown && BOOMERANG) */
297
298 tmp += u.udaminc + dbon();
299 if (u.uswallow) {
300 if ((tmp -= u.uswldtim) <= 0) {
301 pline("Your arms are no longer able to hit.");
302 return (TRUE);
303 }
304 }
305 if (tmp < 1)
306 tmp = 1;
307 mon->mhp -= tmp;
308 if (mon->mhp < 1) {
309 killed(mon);
310 return (FALSE);
311 }
312 if (mon->mtame && (!mon->mflee || mon->mfleetim)) {
313 mon->mflee = 1; /* Rick Richardson */
314 mon->mfleetim += 10 * rnd(tmp);
315 }
316 if (!hittxt) {
317 if (thrown) {
318 /* this assumes that we cannot throw plural things */
319 if (obj == NULL)
320 panic("thrown non-object");
321 hit(xname(obj) /* or: objects[obj->otyp].oc_name */ ,
322 mon, exclam(tmp));
323 } else if (Blind)
324 pline("You hit it.");
325 else
326 pline("You hit %s%s", monnam(mon), exclam(tmp));
327 }
328 if (u.umconf && !thrown) {
329 if (!Blind) {
330 pline("Your hands stop glowing blue.");
331 if (!mon->mfroz && !mon->msleep)
332 pline("%s appears confused.", Monnam(mon));
333 }
334 mon->mconf = 1;
335 u.umconf = 0;
336 }
337 return (TRUE); /* mon still alive */
338 }
339
340 /* try to attack; return FALSE if monster evaded */
341 /* u.dx and u.dy must be set */
342 int
343 attack(struct monst *mtmp)
344 {
345 schar tmp;
346 boolean malive = TRUE;
347 const struct permonst *mdat;
348 mdat = mtmp->data;
349
350 u_wipe_engr(3); /* andrew@orca: prevent unlimited pick-axe
351 * attacks */
352
353 if (mdat->mlet == 'L' && !mtmp->mfroz && !mtmp->msleep &&
354 !mtmp->mconf && mtmp->mcansee && !rn2(7) &&
355 (m_move(mtmp, 0) == 2 /* he died */ || /* he moved: */
356 mtmp->mx != u.ux + u.dx || mtmp->my != u.uy + u.dy))
357 return (FALSE);
358
359 if (mtmp->mimic) {
360 if (!u.ustuck && !mtmp->mflee)
361 u.ustuck = mtmp;
362 switch (levl[u.ux + u.dx][u.uy + u.dy].scrsym) {
363 case '+':
364 pline("The door actually was a Mimic.");
365 break;
366 case '$':
367 pline("The chest was a Mimic!");
368 break;
369 default:
370 pline("Wait! That's a Mimic!");
371 }
372 wakeup(mtmp); /* clears mtmp->mimic */
373 return (TRUE);
374 }
375 wakeup(mtmp);
376
377 if (mtmp->mhide && mtmp->mundetected) {
378 struct obj *obj;
379
380 mtmp->mundetected = 0;
381 if ((obj = o_at(mtmp->mx, mtmp->my)) && !Blind)
382 pline("Wait! There's a %s hiding under %s!",
383 mdat->mname, doname(obj));
384 return (TRUE);
385 }
386 tmp = u.uluck + u.ulevel + mdat->ac + abon();
387 if (uwep) {
388 if (uwep->olet == WEAPON_SYM || uwep->otyp == PICK_AXE)
389 tmp += uwep->spe;
390 if (uwep->otyp == TWO_HANDED_SWORD)
391 tmp -= 1;
392 else if (uwep->otyp == DAGGER)
393 tmp += 2;
394 else if (uwep->otyp == CRYSKNIFE)
395 tmp += 3;
396 else if (uwep->otyp == SPEAR &&
397 strchr("XDne", mdat->mlet))
398 tmp += 2;
399 }
400 if (mtmp->msleep) {
401 mtmp->msleep = 0;
402 tmp += 2;
403 }
404 if (mtmp->mfroz) {
405 tmp += 4;
406 if (!rn2(10))
407 mtmp->mfroz = 0;
408 }
409 if (mtmp->mflee)
410 tmp += 2;
411 if (u.utrap)
412 tmp -= 3;
413
414 /* with a lot of luggage, your agility diminishes */
415 tmp -= (inv_weight() + 40) / 20;
416
417 if (tmp <= rnd(20) && !u.uswallow) {
418 if (Blind)
419 pline("You miss it.");
420 else
421 pline("You miss %s.", monnam(mtmp));
422 } else {
423 /* we hit the monster; be careful: it might die! */
424
425 if ((malive = hmon(mtmp, uwep, 0)) == TRUE) {
426 /* monster still alive */
427 if (!rn2(25) && mtmp->mhp < mtmp->mhpmax / 2) {
428 mtmp->mflee = 1;
429 if (!rn2(3))
430 mtmp->mfleetim = rnd(100);
431 if (u.ustuck == mtmp && !u.uswallow)
432 u.ustuck = 0;
433 }
434 #ifndef NOWORM
435 if (mtmp->wormno)
436 cutworm(mtmp, u.ux + u.dx, u.uy + u.dy,
437 uwep ? uwep->otyp : 0);
438 #endif /* NOWORM */
439 }
440 if (mdat->mlet == 'a') {
441 if (rn2(2)) {
442 pline("You are splashed by the blob's acid!");
443 losehp_m(rnd(6), mtmp);
444 if (!rn2(30))
445 corrode_armor();
446 }
447 if (!rn2(6))
448 corrode_weapon();
449 }
450 }
451 if (malive && mdat->mlet == 'E' && canseemon(mtmp)
452 && !mtmp->mcan && rn2(3)) {
453 if (mtmp->mcansee) {
454 pline("You are frozen by the floating eye's gaze!");
455 nomul((u.ulevel > 6 || rn2(4)) ? rn1(20, -21) : -200);
456 } else {
457 pline("The blinded floating eye cannot defend itself.");
458 if (!rn2(500))
459 if ((int) u.uluck > LUCKMIN)
460 u.uluck--;
461 }
462 }
463 return (TRUE);
464 }