-O3 for a program from 1981 is ridiculous.
[rogue-pphs.git] / fight.c
1 /*
2 * All the fighting gets done here
3 *
4 * @(#)fight.c 3.28 (Berkeley) 6/15/81
5 *
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
8 * All rights reserved.
9 *
10 * See the file LICENSE.TXT for full copyright and licensing information.
11 */
12
13 #include "curses.h"
14 #include <ctype.h>
15 #include <string.h>
16 #include "rogue.h"
17
18 long e_levels[] = {
19 10L,20L,40L,80L,160L,320L,640L,1280L,2560L,5120L,10240L,20480L,
20 40920L, 81920L, 163840L, 327680L, 655360L, 1310720L, 2621440L, 0L };
21
22 /*
23 * fight:
24 * The player attacks the monster.
25 */
26
27 fight(mp, mn, weap, thrown)
28 register coord *mp;
29 char mn;
30 struct object *weap;
31 bool thrown;
32 {
33 register struct thing *tp;
34 register struct linked_list *item;
35 register bool did_hit = TRUE;
36
37 /*
38 * Find the monster we want to fight
39 */
40 if ((item = find_mons(mp->y, mp->x)) == NULL)
41 {
42 debug("Fight what @ %d,%d", mp->y, mp->x);
43 return(0);
44 }
45 tp = (struct thing *) ldata(item);
46 /*
47 * Since we are fighting, things are not quiet so no healing takes
48 * place.
49 */
50 quiet = 0;
51 runto(mp, &hero);
52 /*
53 * Let him know it was really a mimic (if it was one).
54 */
55 if (tp->t_type == 'M' && tp->t_disguise != 'M' && off(player, ISBLIND))
56 {
57 msg("Wait! That's a mimic!");
58 tp->t_disguise = 'M';
59 did_hit = thrown;
60 }
61 if (did_hit)
62 {
63 register char *mname;
64
65 did_hit = FALSE;
66 if (on(player, ISBLIND))
67 mname = "it";
68 else
69 mname = monsters[mn-'A'].m_name;
70 if (roll_em(&pstats, &tp->t_stats, weap, thrown))
71 {
72 did_hit = TRUE;
73 if (thrown)
74 thunk(weap, mname);
75 else
76 hit(NULL, mname);
77 if (on(player, CANHUH))
78 {
79 msg("Your hands stop glowing red");
80 msg("The %s appears confused.", mname);
81 tp->t_flags |= ISHUH;
82 player.t_flags &= ~CANHUH;
83 }
84 if (tp->t_stats.s_hpt <= 0)
85 killed(item, TRUE);
86 }
87 else
88 if (thrown)
89 bounce(weap, mname);
90 else
91 miss(NULL, mname);
92 }
93 count = 0;
94 return did_hit;
95 }
96
97 /*
98 * attack:
99 * The monster attacks the player
100 */
101
102 attack(mp)
103 register struct thing *mp;
104 {
105 register char *mname;
106
107 /*
108 * Since this is an attack, stop running and any healing that was
109 * going on at the time.
110 */
111 running = FALSE;
112 quiet = 0;
113 if (mp->t_type == 'M' && off(player, ISBLIND))
114 mp->t_disguise = 'M';
115 if (on(player, ISBLIND))
116 mname = "it";
117 else
118 mname = monsters[mp->t_type-'A'].m_name;
119 if (roll_em(&mp->t_stats, &pstats, NULL, FALSE))
120 {
121 if (mp->t_type != 'E')
122 hit(mname, NULL);
123 if (pstats.s_hpt <= 0)
124 death(mp->t_type); /* Bye bye life ... */
125 if (off(*mp, ISCANC))
126 switch (mp->t_type)
127 {
128 case 'R':
129 /*
130 * If a rust monster hits, you lose armor
131 */
132 if (cur_armor != NULL && cur_armor->o_ac < 9)
133 {
134 if (!terse)
135 msg("Your armor appears to be weaker now. Oh my!");
136 else
137 msg("Your armor weakens");
138 cur_armor->o_ac++;
139 }
140 when 'E':
141 /*
142 * The gaze of the floating eye hypnotizes you
143 */
144 if (on(player, ISBLIND))
145 break;
146 if (!no_command)
147 {
148 addmsg("You are transfixed");
149 if (!terse)
150 addmsg(" by the gaze of the floating eye.");
151 endmsg();
152 }
153 no_command += rnd(2)+2;
154 when 'A':
155 /*
156 * Ants have poisonous bites
157 */
158 if (!save(VS_POISON))
159 if (!ISWEARING(R_SUSTSTR))
160 {
161 chg_str(-1);
162 if (!terse)
163 msg("You feel a sting in your arm and now feel weaker");
164 else
165 msg("A sting has weakened you");
166 }
167 else
168 if (!terse)
169 msg("A sting momentarily weakens you");
170 else
171 msg("Sting has no effect");
172 when 'W':
173 /*
174 * Wraiths might drain energy levels
175 */
176 if (rnd(100) < 15)
177 {
178 int fewer;
179
180 if (pstats.s_exp == 0)
181 death('W'); /* All levels gone */
182 msg("You suddenly feel weaker.");
183 if (--pstats.s_lvl == 0)
184 {
185 pstats.s_exp = 0;
186 pstats.s_lvl = 1;
187 }
188 else
189 pstats.s_exp = e_levels[pstats.s_lvl-1]+1;
190 fewer = roll(1, 10);
191 pstats.s_hpt -= fewer;
192 max_hp -= fewer;
193 if (pstats.s_hpt < 1)
194 pstats.s_hpt = 1;
195 if (max_hp < 1)
196 death('W');
197 }
198 when 'F':
199 /*
200 * Violet fungi stops the poor guy from moving
201 */
202 player.t_flags |= ISHELD;
203 sprintf(monsters['F'-'A'].m_stats.s_dmg,"%dd1",++fung_hit);
204 when 'L':
205 {
206 /*
207 * Leperachaun steals some gold
208 */
209 register long lastpurse;
210
211 lastpurse = purse;
212 purse -= GOLDCALC;
213 if (!save(VS_MAGIC))
214 purse -= GOLDCALC + GOLDCALC + GOLDCALC + GOLDCALC;
215 if (purse < 0)
216 purse = 0;
217 if (purse != lastpurse)
218 msg("Your purse feels lighter");
219 remove_monster(&mp->t_pos, find_mons(mp->t_pos.y, mp->t_pos.x));
220 mp = NULL;
221 }
222 when 'N':
223 {
224 register struct linked_list *list, *steal;
225 register struct object *obj;
226 register int nobj;
227
228 /*
229 * Nymph's steal a magic item, look through the pack
230 * and pick out one we like.
231 */
232 steal = NULL;
233 for (nobj = 0, list = pack; list != NULL; list = next(list))
234 {
235 obj = (struct object *) ldata(list);
236 if (obj != cur_armor &&
237 obj != cur_weapon &&
238 obj != cur_ring[LEFT] &&
239 obj != cur_ring[RIGHT] && /* Nymph bug fix */
240 is_magic(obj) &&
241 rnd(++nobj) == 0)
242 steal = list;
243 }
244 if (steal != NULL)
245 {
246 register struct object *obj;
247
248 obj = (struct object *) ldata(steal);
249 remove_monster(&mp->t_pos, find_mons(mp->t_pos.y, mp->t_pos.x));
250 mp = NULL;
251 if (obj->o_count > 1 && obj->o_group == 0)
252 {
253 register int oc;
254
255 oc = obj->o_count;
256 obj->o_count = 1;
257 msg("She stole %s!", inv_name(obj, TRUE));
258 obj->o_count = oc - 1;
259 }
260 else
261 {
262 msg("She stole %s!", inv_name(obj, TRUE));
263 detach(pack, steal);
264 discard(steal);
265 }
266 inpack--;
267 }
268 }
269 otherwise:
270 break;
271 }
272 }
273 else if (mp->t_type != 'E')
274 {
275 if (mp->t_type == 'F')
276 {
277 pstats.s_hpt -= fung_hit;
278 if (pstats.s_hpt <= 0)
279 death(mp->t_type); /* Bye bye life ... */
280 }
281 miss(mname, NULL);
282 }
283 /*
284 * Check to see if this is a regenerating monster and let it heal if
285 * it is.
286 */
287 if ((mp != NULL) && (on(*mp, ISREGEN) && rnd(100) < 33))
288 mp->t_stats.s_hpt++;
289 if (fight_flush)
290 {
291 flush_type(); /* flush typeahead */
292 }
293 count = 0;
294 status();
295
296 if (mp == NULL)
297 return(-1);
298 else
299 return(0);
300 }
301
302 /*
303 * swing:
304 * returns true if the swing hits
305 */
306
307 swing(at_lvl, op_arm, wplus)
308 int at_lvl, op_arm, wplus;
309 {
310 register int res = rnd(20)+1;
311 register int need = (21-at_lvl)-op_arm;
312
313 return (res+wplus >= need);
314 }
315
316 /*
317 * check_level:
318 * Check to see if the guy has gone up a level.
319 */
320
321 check_level()
322 {
323 register int i, add;
324
325 for (i = 0; e_levels[i] != 0; i++)
326 if (e_levels[i] > pstats.s_exp)
327 break;
328 i++;
329 if (i > pstats.s_lvl)
330 {
331 add = roll(i-pstats.s_lvl,10);
332 max_hp += add;
333 if ((pstats.s_hpt += add) > max_hp)
334 pstats.s_hpt = max_hp;
335 msg("Welcome to level %d", i);
336 }
337 pstats.s_lvl = i;
338 }
339
340 /*
341 * roll_em:
342 * Roll several attacks
343 */
344
345 roll_em(att, def, weap, hurl)
346 struct stats *att, *def;
347 struct object *weap;
348 bool hurl;
349 {
350 register char *cp;
351 register int ndice, nsides, def_arm;
352 register bool did_hit = FALSE;
353 register int prop_hplus, prop_dplus;
354
355 prop_hplus = prop_dplus = 0;
356 if (weap == NULL)
357 cp = att->s_dmg;
358 else if (hurl)
359 if ((weap->o_flags&ISMISL) && cur_weapon != NULL &&
360 cur_weapon->o_which == weap->o_launch)
361 {
362 cp = weap->o_hurldmg;
363 prop_hplus = cur_weapon->o_hplus;
364 prop_dplus = cur_weapon->o_dplus;
365 }
366 else
367 cp = (weap->o_flags&ISMISL ? weap->o_damage : weap->o_hurldmg);
368 else
369 {
370 cp = weap->o_damage;
371 /*
372 * Drain a staff of striking
373 */
374 if (weap->o_type == STICK && weap->o_which == WS_HIT
375 && weap->o_charges == 0)
376 {
377 strcpy(weap->o_damage,"0d0");
378 weap->o_hplus = weap->o_dplus = 0;
379 }
380 }
381 for (;;)
382 {
383 int damage;
384 int hplus = prop_hplus + (weap == NULL ? 0 : weap->o_hplus);
385 int dplus = prop_dplus + (weap == NULL ? 0 : weap->o_dplus);
386
387 if (weap == cur_weapon)
388 {
389 if (ISRING(LEFT, R_ADDDAM))
390 dplus += cur_ring[LEFT]->o_ac;
391 else if (ISRING(LEFT, R_ADDHIT))
392 hplus += cur_ring[LEFT]->o_ac;
393 if (ISRING(RIGHT, R_ADDDAM))
394 dplus += cur_ring[RIGHT]->o_ac;
395 else if (ISRING(RIGHT, R_ADDHIT))
396 hplus += cur_ring[RIGHT]->o_ac;
397 }
398 ndice = atoi(cp);
399 if ((cp = strchr(cp, 'd')) == NULL)
400 break;
401 nsides = atoi(++cp);
402 if (def == &pstats)
403 {
404 if (cur_armor != NULL)
405 def_arm = cur_armor->o_ac;
406 else
407 def_arm = def->s_arm;
408 if (ISRING(LEFT, R_PROTECT))
409 def_arm -= cur_ring[LEFT]->o_ac;
410 else if (ISRING(RIGHT, R_PROTECT))
411 def_arm -= cur_ring[RIGHT]->o_ac;
412 }
413 else
414 def_arm = def->s_arm;
415 if (swing(att->s_lvl, def_arm, hplus+str_plus(&att->s_str)))
416 {
417 register int proll;
418
419 proll = roll(ndice, nsides);
420 if (ndice + nsides > 0 && proll < 1)
421 debug("Damage for %dd%d came out %d.", ndice, nsides, proll);
422 damage = dplus + proll + add_dam(&att->s_str);
423 def->s_hpt -= max(0, damage);
424 did_hit = TRUE;
425 }
426 if ((cp = strchr(cp, '/')) == NULL)
427 break;
428 cp++;
429 }
430 return did_hit;
431 }
432
433 /*
434 * prname:
435 * The print name of a combatant
436 */
437
438 char *
439 prname(who, upper)
440 register char *who;
441 bool upper;
442 {
443 static char tbuf[80];
444
445 *tbuf = '\0';
446 if (who == 0)
447 strcpy(tbuf, "you");
448 else if (on(player, ISBLIND))
449 strcpy(tbuf, "it");
450 else
451 {
452 strcpy(tbuf, "the ");
453 strcat(tbuf, who);
454 }
455 if (upper)
456 *tbuf = toupper(*tbuf);
457 return tbuf;
458 }
459
460 /*
461 * hit:
462 * Print a message to indicate a succesful hit
463 */
464
465 hit(er, ee)
466 register char *er, *ee;
467 {
468 register char *s;
469
470 addmsg(prname(er, TRUE));
471 if (terse)
472 s = " hit.";
473 else
474 switch (rnd(4))
475 {
476 case 0: s = " scored an excellent hit on ";
477 when 1: s = " hit ";
478 when 2: s = (er == 0 ? " have injured " : " has injured ");
479 when 3: s = (er == 0 ? " swing and hit " : " swings and hits ");
480 }
481 addmsg(s);
482 if (!terse)
483 addmsg(prname(ee, FALSE));
484 endmsg();
485 }
486
487 /*
488 * miss:
489 * Print a message to indicate a poor swing
490 */
491
492 miss(er, ee)
493 register char *er, *ee;
494 {
495 register char *s;
496
497 addmsg(prname(er, TRUE));
498 switch (terse ? 0 : rnd(4))
499 {
500 case 0: s = (er == 0 ? " miss" : " misses");
501 when 1: s = (er == 0 ? " swing and miss" : " swings and misses");
502 when 2: s = (er == 0 ? " barely miss" : " barely misses");
503 when 3: s = (er == 0 ? " don't hit" : " doesn't hit");
504 }
505 addmsg(s);
506 if (!terse)
507 addmsg(" %s", prname(ee, FALSE));
508 endmsg();
509 }
510
511 /*
512 * save_throw:
513 * See if a creature save against something
514 */
515 save_throw(which, tp)
516 int which;
517 struct thing *tp;
518 {
519 register int need;
520
521 need = 14 + which - tp->t_stats.s_lvl / 2;
522 return (roll(1, 20) >= need);
523 }
524
525 /*
526 * save:
527 * See if he saves against various nasty things
528 */
529
530 save(which)
531 int which;
532 {
533 return save_throw(which, &player);
534 }
535
536 /*
537 * str_plus:
538 * compute bonus/penalties for strength on the "to hit" roll
539 */
540
541 str_plus(str)
542 register str_t *str;
543 {
544 if (str->st_str == 18)
545 {
546 if (str->st_add == 100)
547 return 3;
548 if (str->st_add > 50)
549 return 2;
550 }
551 if (str->st_str >= 17)
552 return 1;
553 if (str->st_str > 6)
554 return 0;
555 return str->st_str - 7;
556 }
557
558 /*
559 * add_dam:
560 * compute additional damage done for exceptionally high or low strength
561 */
562
563 add_dam(str)
564 register str_t *str;
565 {
566 if (str->st_str == 18)
567 {
568 if (str->st_add == 100)
569 return 6;
570 if (str->st_add > 90)
571 return 5;
572 if (str->st_add > 75)
573 return 4;
574 if (str->st_add != 0)
575 return 3;
576 return 2;
577 }
578 if (str->st_str > 15)
579 return 1;
580 if (str->st_str > 6)
581 return 0;
582 return str->st_str - 7;
583 }
584
585 /*
586 * raise_level:
587 * The guy just magically went up a level.
588 */
589
590 raise_level()
591 {
592 pstats.s_exp = e_levels[pstats.s_lvl-1] + 1L;
593 check_level();
594 }
595
596 /*
597 * thunk:
598 * A missile hits a monster
599 */
600
601 thunk(weap, mname)
602 register struct object *weap;
603 register char *mname;
604 {
605 if (weap->o_type == WEAPON)
606 msg("The %s hits the %s", w_names[weap->o_which], mname);
607 else
608 msg("You hit the %s.", mname);
609 }
610
611 /*
612 * bounce:
613 * A missile misses a monster
614 */
615
616 bounce(weap, mname)
617 register struct object *weap;
618 register char *mname;
619 {
620 if (weap->o_type == WEAPON)
621 msg("The %s misses the %s", w_names[weap->o_which], mname);
622 else
623 msg("You missed the %s.", mname);
624 }
625
626 /*
627 * remove a monster from the screen
628 */
629 remove_monster(mp, item)
630 register coord *mp;
631 register struct linked_list *item;
632 {
633 mvwaddch(mw, mp->y, mp->x, ' ');
634 mvwaddch(cw, mp->y, mp->x, ((struct thing *) ldata(item))->t_oldch);
635 detach(mlist, item);
636 discard(item);
637 }
638
639 /*
640 * is_magic:
641 * Returns true if an object radiates magic
642 */
643
644 is_magic(obj)
645 register struct object *obj;
646 {
647 switch (obj->o_type)
648 {
649 case ARMOR:
650 return obj->o_ac != a_class[obj->o_which];
651 when WEAPON:
652 return obj->o_hplus != 0 || obj->o_dplus != 0;
653 when POTION:
654 case SCROLL:
655 case STICK:
656 case RING:
657 case AMULET:
658 return TRUE;
659 }
660 return FALSE;
661 }
662
663 /*
664 * killed:
665 * Called to put a monster to death
666 */
667
668 killed(item, pr)
669 register struct linked_list *item;
670 bool pr;
671 {
672 register struct thing *tp;
673 register struct linked_list *pitem, *nexti;
674
675 tp = (struct thing *) ldata(item);
676 if (pr)
677 {
678 addmsg(terse ? "Defeated " : "You have defeated ");
679 if (on(player, ISBLIND))
680 msg("it.");
681 else
682 {
683 if (!terse)
684 addmsg("the ");
685 msg("%s.", monsters[tp->t_type-'A'].m_name);
686 }
687 }
688 pstats.s_exp += tp->t_stats.s_exp;
689 /*
690 * Do adjustments if he went up a level
691 */
692 check_level();
693 /*
694 * If the monster was a violet fungi, un-hold him
695 */
696 switch (tp->t_type)
697 {
698 case 'F':
699 player.t_flags &= ~ISHELD;
700 fung_hit = 0;
701 strcpy(monsters['F'-'A'].m_stats.s_dmg, "000d0");
702 when 'L':
703 {
704 register struct room *rp;
705
706 if ((rp = roomin(&tp->t_pos)) == NULL)
707 break;
708 if (rp->r_goldval != 0 || fallpos(&tp->t_pos,&rp->r_gold,FALSE))
709 {
710 rp->r_goldval += GOLDCALC;
711 if (save(VS_MAGIC))
712 rp->r_goldval += GOLDCALC + GOLDCALC
713 + GOLDCALC + GOLDCALC;
714 mvwaddch(stdscr, rp->r_gold.y, rp->r_gold.x, GOLD);
715 if (!(rp->r_flags & ISDARK))
716 {
717 light(&hero);
718 mvwaddch(cw, hero.y, hero.x, PLAYER);
719 }
720 }
721 }
722 }
723 /*
724 * Empty the monsters pack
725 */
726 pitem = tp->t_pack;
727 while (pitem != NULL)
728 {
729 register struct object *obj;
730
731 nexti = next(tp->t_pack);
732 obj = (struct object *) ldata(pitem);
733 obj->o_pos = tp->t_pos;
734 detach(tp->t_pack, pitem);
735 fall(pitem, FALSE);
736 pitem = nexti;
737 }
738 /*
739 * Get rid of the monster.
740 */
741 remove_monster(&tp->t_pos, item);
742 }