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