Also pass raw printed texts to the msghandler
[aNetHack.git] / src / attrib.c
blob5c80c5ed23f3efdbd0bf8c14ac3db0f2df92d5cd
1 /* NetHack 3.6 attrib.c $NHDT-Date: 1455357587 2016/02/13 09:59:47 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.55 $ */
2 /* Copyright 1988, 1989, 1990, 1992, M. Stephenson */
3 /* NetHack may be freely redistributed. See license for details. */
5 /* attribute modification routines. */
7 #include "hack.h"
8 #include <ctype.h>
10 /* part of the output on gain or loss of attribute */
11 static const char
12 *const plusattr[] = { "strong", "smart", "wise",
13 "agile", "tough", "charismatic" },
14 *const minusattr[] = { "weak", "stupid",
15 "foolish", "clumsy",
16 "fragile", "repulsive" };
18 static const struct innate {
19 schar ulevel;
20 long *ability;
21 const char *gainstr, *losestr;
22 } arc_abil[] = { { 1, &(HStealth), "", "" },
23 { 1, &(HFast), "", "" },
24 { 10, &(HSearching), "perceptive", "" },
25 { 0, 0, 0, 0 } },
27 bar_abil[] = { { 1, &(HPoison_resistance), "", "" },
28 { 7, &(HFast), "quick", "slow" },
29 { 15, &(HStealth), "stealthy", "" },
30 { 0, 0, 0, 0 } },
32 cav_abil[] = { { 7, &(HFast), "quick", "slow" },
33 { 15, &(HWarning), "sensitive", "" },
34 { 0, 0, 0, 0 } },
36 hea_abil[] = { { 1, &(HPoison_resistance), "", "" },
37 { 15, &(HWarning), "sensitive", "" },
38 { 0, 0, 0, 0 } },
40 kni_abil[] = { { 7, &(HFast), "quick", "slow" }, { 0, 0, 0, 0 } },
42 mon_abil[] = { { 1, &(HFast), "", "" },
43 { 1, &(HSleep_resistance), "", "" },
44 { 1, &(HSee_invisible), "", "" },
45 { 3, &(HPoison_resistance), "healthy", "" },
46 { 5, &(HStealth), "stealthy", "" },
47 { 7, &(HWarning), "sensitive", "" },
48 { 9, &(HSearching), "perceptive", "unaware" },
49 { 11, &(HFire_resistance), "cool", "warmer" },
50 { 13, &(HCold_resistance), "warm", "cooler" },
51 { 15, &(HShock_resistance), "insulated", "conductive" },
52 { 17, &(HTeleport_control), "controlled", "uncontrolled" },
53 { 0, 0, 0, 0 } },
55 pri_abil[] = { { 15, &(HWarning), "sensitive", "" },
56 { 20, &(HFire_resistance), "cool", "warmer" },
57 { 0, 0, 0, 0 } },
59 ran_abil[] = { { 1, &(HSearching), "", "" },
60 { 7, &(HStealth), "stealthy", "" },
61 { 15, &(HSee_invisible), "", "" },
62 { 0, 0, 0, 0 } },
64 rog_abil[] = { { 1, &(HStealth), "", "" },
65 { 10, &(HSearching), "perceptive", "" },
66 { 0, 0, 0, 0 } },
68 sam_abil[] = { { 1, &(HFast), "", "" },
69 { 15, &(HStealth), "stealthy", "" },
70 { 0, 0, 0, 0 } },
72 tou_abil[] = { { 10, &(HSearching), "perceptive", "" },
73 { 20, &(HPoison_resistance), "hardy", "" },
74 { 0, 0, 0, 0 } },
76 val_abil[] = { { 1, &(HCold_resistance), "", "" },
77 { 1, &(HStealth), "", "" },
78 { 7, &(HFast), "quick", "slow" },
79 { 0, 0, 0, 0 } },
81 wiz_abil[] = { { 15, &(HWarning), "sensitive", "" },
82 { 17, &(HTeleport_control), "controlled", "uncontrolled" },
83 { 0, 0, 0, 0 } },
85 /* Intrinsics conferred by race */
86 dwa_abil[] = { { 1, &HInfravision, "", "" },
87 { 0, 0, 0, 0 } },
89 elf_abil[] = { { 1, &HInfravision, "", "" },
90 { 4, &HSleep_resistance, "awake", "tired" },
91 { 0, 0, 0, 0 } },
93 gno_abil[] = { { 1, &HInfravision, "", "" },
94 { 0, 0, 0, 0 } },
96 orc_abil[] = { { 1, &HInfravision, "", "" },
97 { 1, &HPoison_resistance, "", "" },
98 { 0, 0, 0, 0 } },
100 hum_abil[] = { { 0, 0, 0, 0 } };
102 STATIC_DCL void NDECL(exerper);
103 STATIC_DCL void FDECL(postadjabil, (long *));
104 STATIC_DCL const struct innate *FDECL(role_abil, (int));
105 STATIC_DCL const struct innate *FDECL(check_innate_abil, (long *, long));
106 STATIC_DCL int FDECL(innately, (long *));
108 /* adjust an attribute; return TRUE if change is made, FALSE otherwise */
109 boolean
110 adjattrib(ndx, incr, msgflg)
111 int ndx, incr;
112 int msgflg; /* positive => no message, zero => message, and */
113 { /* negative => conditional (msg if change made) */
114 int old_acurr;
115 boolean abonflg;
116 const char *attrstr;
118 if (Fixed_abil || !incr)
119 return FALSE;
121 if ((ndx == A_INT || ndx == A_WIS) && uarmh && uarmh->otyp == DUNCE_CAP) {
122 if (msgflg == 0)
123 Your("cap constricts briefly, then relaxes again.");
124 return FALSE;
127 old_acurr = ACURR(ndx);
128 if (incr > 0) {
129 ABASE(ndx) += incr;
130 if (ABASE(ndx) > AMAX(ndx)) {
131 incr = ABASE(ndx) - AMAX(ndx);
132 AMAX(ndx) += incr;
133 if (AMAX(ndx) > ATTRMAX(ndx))
134 AMAX(ndx) = ATTRMAX(ndx);
135 ABASE(ndx) = AMAX(ndx);
137 attrstr = plusattr[ndx];
138 abonflg = (ABON(ndx) < 0);
139 } else {
140 ABASE(ndx) += incr;
141 if (ABASE(ndx) < ATTRMIN(ndx)) {
142 incr = ABASE(ndx) - ATTRMIN(ndx);
143 ABASE(ndx) = ATTRMIN(ndx);
144 AMAX(ndx) += incr;
145 if (AMAX(ndx) < ATTRMIN(ndx))
146 AMAX(ndx) = ATTRMIN(ndx);
148 attrstr = minusattr[ndx];
149 abonflg = (ABON(ndx) > 0);
151 if (ACURR(ndx) == old_acurr) {
152 if (msgflg == 0 && flags.verbose)
153 pline("You're %s as %s as you can get.",
154 abonflg ? "currently" : "already", attrstr);
155 return FALSE;
158 if (msgflg <= 0)
159 You_feel("%s%s!", (incr > 1 || incr < -1) ? "very " : "", attrstr);
160 context.botl = 1;
161 if (moves > 1 && (ndx == A_STR || ndx == A_CON))
162 (void) encumber_msg();
163 return TRUE;
166 void
167 gainstr(otmp, incr, givemsg)
168 struct obj *otmp;
169 int incr;
170 boolean givemsg;
172 int num = incr;
174 if (!num) {
175 if (ABASE(A_STR) < 18)
176 num = (rn2(4) ? 1 : rnd(6));
177 else if (ABASE(A_STR) < STR18(85))
178 num = rnd(10);
179 else
180 num = 1;
182 (void) adjattrib(A_STR, (otmp && otmp->cursed) ? -num : num,
183 givemsg ? -1 : 1);
186 /* may kill you; cause may be poison or monster like 'a' */
187 void
188 losestr(num)
189 register int num;
191 int ustr = ABASE(A_STR) - num;
193 while (ustr < 3) {
194 ++ustr;
195 --num;
196 if (Upolyd) {
197 u.mh -= 6;
198 u.mhmax -= 6;
199 } else {
200 u.uhp -= 6;
201 u.uhpmax -= 6;
204 (void) adjattrib(A_STR, -num, 1);
207 static const struct poison_effect_message {
208 void VDECL((*delivery_func), (const char *, ...));
209 const char *effect_msg;
210 } poiseff[] = {
211 { You_feel, "weaker" }, /* A_STR */
212 { Your, "brain is on fire" }, /* A_INT */
213 { Your, "judgement is impaired" }, /* A_WIS */
214 { Your, "muscles won't obey you" }, /* A_DEX */
215 { You_feel, "very sick" }, /* A_CON */
216 { You, "break out in hives" } /* A_CHA */
219 /* feedback for attribute loss due to poisoning */
220 void
221 poisontell(typ, exclaim)
222 int typ; /* which attribute */
223 boolean exclaim; /* emphasis */
225 void VDECL((*func), (const char *, ...)) = poiseff[typ].delivery_func;
227 (*func)("%s%c", poiseff[typ].effect_msg, exclaim ? '!' : '.');
230 /* called when an attack or trap has poisoned the hero (used to be in mon.c)
232 void
233 poisoned(reason, typ, pkiller, fatal, thrown_weapon)
234 const char *reason, /* controls what messages we display */
235 *pkiller; /* for score+log file if fatal */
236 int typ, fatal; /* if fatal is 0, limit damage to adjattrib */
237 boolean thrown_weapon; /* thrown weapons are less deadly */
239 int i, loss, kprefix = KILLED_BY_AN;
241 /* inform player about being poisoned unless that's already been done;
242 "blast" has given a "blast of poison gas" message; "poison arrow",
243 "poison dart", etc have implicitly given poison messages too... */
244 if (strcmp(reason, "blast") && !strstri(reason, "poison")) {
245 boolean plural = (reason[strlen(reason) - 1] == 's') ? 1 : 0;
247 /* avoid "The" Orcus's sting was poisoned... */
248 pline("%s%s %s poisoned!",
249 isupper((uchar) *reason) ? "" : "The ", reason,
250 plural ? "were" : "was");
252 if (Poison_resistance) {
253 if (!strcmp(reason, "blast"))
254 shieldeff(u.ux, u.uy);
255 pline_The("poison doesn't seem to affect you.");
256 return;
259 /* suppress killer prefix if it already has one */
260 i = name_to_mon(pkiller);
261 if (i >= LOW_PM && (mons[i].geno & G_UNIQ)) {
262 kprefix = KILLED_BY;
263 if (!type_is_pname(&mons[i]))
264 pkiller = the(pkiller);
265 } else if (!strncmpi(pkiller, "the ", 4) || !strncmpi(pkiller, "an ", 3)
266 || !strncmpi(pkiller, "a ", 2)) {
267 /*[ does this need a plural check too? ]*/
268 kprefix = KILLED_BY;
271 i = !fatal ? 1 : rn2(fatal + (thrown_weapon ? 20 : 0));
272 if (i == 0 && typ != A_CHA) {
273 /* instant kill */
274 u.uhp = -1;
275 pline_The("poison was deadly...");
276 } else if (i > 5) {
277 /* HP damage; more likely--but less severe--with missiles */
278 loss = thrown_weapon ? rnd(6) : rn1(10, 6);
279 losehp(loss, pkiller, kprefix); /* poison damage */
280 } else {
281 /* attribute loss; if typ is A_STR, reduction in current and
282 maximum HP will occur once strength has dropped down to 3 */
283 loss = (thrown_weapon || !fatal) ? 1 : d(2, 2); /* was rn1(3,3) */
284 /* check that a stat change was made */
285 if (adjattrib(typ, -loss, 1))
286 poisontell(typ, TRUE);
289 if (u.uhp < 1) {
290 killer.format = kprefix;
291 Strcpy(killer.name, pkiller);
292 /* "Poisoned by a poisoned ___" is redundant */
293 done(strstri(pkiller, "poison") ? DIED : POISONING);
295 (void) encumber_msg();
298 void
299 change_luck(n)
300 register schar n;
302 u.uluck += n;
303 if (u.uluck < 0 && u.uluck < LUCKMIN)
304 u.uluck = LUCKMIN;
305 if (u.uluck > 0 && u.uluck > LUCKMAX)
306 u.uluck = LUCKMAX;
310 stone_luck(parameter)
311 boolean parameter; /* So I can't think up of a good name. So sue me. --KAA */
313 register struct obj *otmp;
314 register long bonchance = 0;
316 for (otmp = invent; otmp; otmp = otmp->nobj)
317 if (confers_luck(otmp)) {
318 if (otmp->cursed)
319 bonchance -= otmp->quan;
320 else if (otmp->blessed)
321 bonchance += otmp->quan;
322 else if (parameter)
323 bonchance += otmp->quan;
326 return sgn((int) bonchance);
329 /* there has just been an inventory change affecting a luck-granting item */
330 void
331 set_moreluck()
333 int luckbon = stone_luck(TRUE);
335 if (!luckbon && !carrying(LUCKSTONE))
336 u.moreluck = 0;
337 else if (luckbon >= 0)
338 u.moreluck = LUCKADD;
339 else
340 u.moreluck = -LUCKADD;
343 void
344 restore_attrib()
346 int i;
348 for (i = 0; i < A_MAX; i++) { /* all temporary losses/gains */
350 if (ATEMP(i) && ATIME(i)) {
351 if (!(--(ATIME(i)))) { /* countdown for change */
352 ATEMP(i) += ATEMP(i) > 0 ? -1 : 1;
354 if (ATEMP(i)) /* reset timer */
355 ATIME(i) = 100 / ACURR(A_CON);
359 (void) encumber_msg();
362 #define AVAL 50 /* tune value for exercise gains */
364 void
365 exercise(i, inc_or_dec)
366 int i;
367 boolean inc_or_dec;
369 debugpline0("Exercise:");
370 if (i == A_INT || i == A_CHA)
371 return; /* can't exercise these */
373 /* no physical exercise while polymorphed; the body's temporary */
374 if (Upolyd && i != A_WIS)
375 return;
377 if (abs(AEXE(i)) < AVAL) {
379 * Law of diminishing returns (Part I):
381 * Gain is harder at higher attribute values.
382 * 79% at "3" --> 0% at "18"
383 * Loss is even at all levels (50%).
385 * Note: *YES* ACURR is the right one to use.
387 AEXE(i) += (inc_or_dec) ? (rn2(19) > ACURR(i)) : -rn2(2);
388 debugpline3("%s, %s AEXE = %d",
389 (i == A_STR) ? "Str" : (i == A_WIS) ? "Wis" : (i == A_DEX)
390 ? "Dex"
391 : "Con",
392 (inc_or_dec) ? "inc" : "dec", AEXE(i));
394 if (moves > 0 && (i == A_STR || i == A_CON))
395 (void) encumber_msg();
398 STATIC_OVL void
399 exerper()
401 if (!(moves % 10)) {
402 /* Hunger Checks */
404 int hs = (u.uhunger > 1000) ? SATIATED : (u.uhunger > 150)
405 ? NOT_HUNGRY
406 : (u.uhunger > 50)
407 ? HUNGRY
408 : (u.uhunger > 0)
409 ? WEAK
410 : FAINTING;
412 debugpline0("exerper: Hunger checks");
413 switch (hs) {
414 case SATIATED:
415 exercise(A_DEX, FALSE);
416 if (Role_if(PM_MONK))
417 exercise(A_WIS, FALSE);
418 break;
419 case NOT_HUNGRY:
420 exercise(A_CON, TRUE);
421 break;
422 case WEAK:
423 exercise(A_STR, FALSE);
424 if (Role_if(PM_MONK)) /* fasting */
425 exercise(A_WIS, TRUE);
426 break;
427 case FAINTING:
428 case FAINTED:
429 exercise(A_CON, FALSE);
430 break;
433 /* Encumbrance Checks */
434 debugpline0("exerper: Encumber checks");
435 switch (near_capacity()) {
436 case MOD_ENCUMBER:
437 exercise(A_STR, TRUE);
438 break;
439 case HVY_ENCUMBER:
440 exercise(A_STR, TRUE);
441 exercise(A_DEX, FALSE);
442 break;
443 case EXT_ENCUMBER:
444 exercise(A_DEX, FALSE);
445 exercise(A_CON, FALSE);
446 break;
450 /* status checks */
451 if (!(moves % 5)) {
452 debugpline0("exerper: Status checks");
453 if ((HClairvoyant & (INTRINSIC | TIMEOUT)) && !BClairvoyant)
454 exercise(A_WIS, TRUE);
455 if (HRegeneration)
456 exercise(A_STR, TRUE);
458 if (Sick || Vomiting)
459 exercise(A_CON, FALSE);
460 if (Confusion || Hallucination)
461 exercise(A_WIS, FALSE);
462 if ((Wounded_legs && !u.usteed) || Fumbling || HStun)
463 exercise(A_DEX, FALSE);
467 /* exercise/abuse text (must be in attribute order, not botl order);
468 phrased as "You must have been [][0]." or "You haven't been [][1]." */
469 static NEARDATA const char *const exertext[A_MAX][2] = {
470 { "exercising diligently", "exercising properly" }, /* Str */
471 { 0, 0 }, /* Int */
472 { "very observant", "paying attention" }, /* Wis */
473 { "working on your reflexes", "working on reflexes lately" }, /* Dex */
474 { "leading a healthy life-style", "watching your health" }, /* Con */
475 { 0, 0 }, /* Cha */
478 void
479 exerchk()
481 int i, ax, mod_val, lolim, hilim;
483 /* Check out the periodic accumulations */
484 exerper();
486 if (moves >= context.next_attrib_check) {
487 debugpline1("exerchk: ready to test. multi = %d.", multi);
489 /* Are we ready for a test? */
490 if (moves >= context.next_attrib_check && !multi) {
491 debugpline0("exerchk: testing.");
493 * Law of diminishing returns (Part II):
495 * The effects of "exercise" and "abuse" wear
496 * off over time. Even if you *don't* get an
497 * increase/decrease, you lose some of the
498 * accumulated effects.
500 for (i = 0; i < A_MAX; ++i) {
501 ax = AEXE(i);
502 /* nothing to do here if no exercise or abuse has occurred
503 (Int and Cha always fall into this category) */
504 if (!ax)
505 continue; /* ok to skip nextattrib */
507 mod_val = sgn(ax); /* +1 or -1; used below */
508 /* no further effect for exercise if at max or abuse if at min;
509 can't exceed 18 via exercise even if actual max is higher */
510 lolim = ATTRMIN(i); /* usually 3; might be higher */
511 hilim = ATTRMAX(i); /* usually 18; maybe lower or higher */
512 if (hilim > 18)
513 hilim = 18;
514 if ((ax < 0) ? (ABASE(i) <= lolim) : (ABASE(i) >= hilim))
515 goto nextattrib;
516 /* can't exercise non-Wisdom while polymorphed; previous
517 exercise/abuse gradually wears off without impact then */
518 if (Upolyd && i != A_WIS)
519 goto nextattrib;
521 debugpline2("exerchk: testing %s (%d).",
522 (i == A_STR)
523 ? "Str"
524 : (i == A_INT)
525 ? "Int?"
526 : (i == A_WIS)
527 ? "Wis"
528 : (i == A_DEX)
529 ? "Dex"
530 : (i == A_CON)
531 ? "Con"
532 : (i == A_CHA)
533 ? "Cha?"
534 : "???",
535 ax);
537 * Law of diminishing returns (Part III):
539 * You don't *always* gain by exercising.
540 * [MRS 92/10/28 - Treat Wisdom specially for balance.]
542 if (rn2(AVAL) > ((i != A_WIS) ? (abs(ax) * 2 / 3) : abs(ax)))
543 goto nextattrib;
545 debugpline1("exerchk: changing %d.", i);
546 if (adjattrib(i, mod_val, -1)) {
547 debugpline1("exerchk: changed %d.", i);
548 /* if you actually changed an attrib - zero accumulation */
549 AEXE(i) = ax = 0;
550 /* then print an explanation */
551 You("%s %s.",
552 (mod_val > 0) ? "must have been" : "haven't been",
553 exertext[i][(mod_val > 0) ? 0 : 1]);
555 nextattrib:
556 /* this used to be ``AEXE(i) /= 2'' but that would produce
557 platform-dependent rounding/truncation for negative vals */
558 AEXE(i) = (abs(ax) / 2) * mod_val;
560 context.next_attrib_check += rn1(200, 800);
561 debugpline1("exerchk: next check at %ld.", context.next_attrib_check);
565 void
566 init_attr(np)
567 register int np;
569 register int i, x, tryct;
571 for (i = 0; i < A_MAX; i++) {
572 ABASE(i) = AMAX(i) = urole.attrbase[i];
573 ATEMP(i) = ATIME(i) = 0;
574 np -= urole.attrbase[i];
577 tryct = 0;
578 while (np > 0 && tryct < 100) {
579 x = rn2(100);
580 for (i = 0; (i < A_MAX) && ((x -= urole.attrdist[i]) > 0); i++)
582 if (i >= A_MAX)
583 continue; /* impossible */
585 if (ABASE(i) >= ATTRMAX(i)) {
586 tryct++;
587 continue;
589 tryct = 0;
590 ABASE(i)++;
591 AMAX(i)++;
592 np--;
595 tryct = 0;
596 while (np < 0 && tryct < 100) { /* for redistribution */
598 x = rn2(100);
599 for (i = 0; (i < A_MAX) && ((x -= urole.attrdist[i]) > 0); i++)
601 if (i >= A_MAX)
602 continue; /* impossible */
604 if (ABASE(i) <= ATTRMIN(i)) {
605 tryct++;
606 continue;
608 tryct = 0;
609 ABASE(i)--;
610 AMAX(i)--;
611 np++;
615 void
616 redist_attr()
618 register int i, tmp;
620 for (i = 0; i < A_MAX; i++) {
621 if (i == A_INT || i == A_WIS)
622 continue;
623 /* Polymorphing doesn't change your mind */
624 tmp = AMAX(i);
625 AMAX(i) += (rn2(5) - 2);
626 if (AMAX(i) > ATTRMAX(i))
627 AMAX(i) = ATTRMAX(i);
628 if (AMAX(i) < ATTRMIN(i))
629 AMAX(i) = ATTRMIN(i);
630 ABASE(i) = ABASE(i) * AMAX(i) / tmp;
631 /* ABASE(i) > ATTRMAX(i) is impossible */
632 if (ABASE(i) < ATTRMIN(i))
633 ABASE(i) = ATTRMIN(i);
635 (void) encumber_msg();
638 STATIC_OVL
639 void
640 postadjabil(ability)
641 long *ability;
643 if (!ability)
644 return;
645 if (ability == &(HWarning) || ability == &(HSee_invisible))
646 see_monsters();
649 STATIC_OVL const struct innate *
650 role_abil(r)
651 int r;
653 const struct {
654 short role;
655 const struct innate *abil;
656 } roleabils[] = {
657 { PM_ARCHEOLOGIST, arc_abil },
658 { PM_BARBARIAN, bar_abil },
659 { PM_CAVEMAN, cav_abil },
660 { PM_HEALER, hea_abil },
661 { PM_KNIGHT, kni_abil },
662 { PM_MONK, mon_abil },
663 { PM_PRIEST, pri_abil },
664 { PM_RANGER, ran_abil },
665 { PM_ROGUE, rog_abil },
666 { PM_SAMURAI, sam_abil },
667 { PM_TOURIST, tou_abil },
668 { PM_VALKYRIE, val_abil },
669 { PM_WIZARD, wiz_abil },
670 { 0, 0 }
672 int i;
673 for (i = 0; roleabils[i].abil && roleabils[i].role != r; i++);
674 return roleabils[i].abil;
677 STATIC_OVL const struct innate *
678 check_innate_abil(ability, frommask)
679 long *ability;
680 long frommask;
682 const struct innate *abil = 0;
684 if (frommask == FROMEXPER)
685 abil = role_abil(Role_switch);
686 else if (frommask == FROMRACE)
687 switch (Race_switch) {
688 case PM_DWARF:
689 abil = dwa_abil;
690 break;
691 case PM_ELF:
692 abil = elf_abil;
693 break;
694 case PM_GNOME:
695 abil = gno_abil;
696 break;
697 case PM_ORC:
698 abil = orc_abil;
699 break;
700 case PM_HUMAN:
701 abil = hum_abil;
702 break;
703 default:
704 break;
707 while (abil && abil->ability) {
708 if ((abil->ability == ability) && (u.ulevel >= abil->ulevel))
709 return abil;
710 abil++;
712 return (struct innate *) 0;
715 /* reasons for innate ability */
716 #define FROM_NONE 0
717 #define FROM_ROLE 1 /* from experience at level 1 */
718 #define FROM_RACE 2
719 #define FROM_INTR 3 /* intrinsically (eating some corpse or prayer reward) */
720 #define FROM_EXP 4 /* from experience for some level > 1 */
721 #define FROM_FORM 5
722 #define FROM_LYCN 6
724 /* check whether particular ability has been obtained via innate attribute */
725 STATIC_OVL int
726 innately(ability)
727 long *ability;
729 const struct innate *iptr;
731 if ((iptr = check_innate_abil(ability, FROMEXPER)) != 0)
732 return (iptr->ulevel == 1) ? FROM_ROLE : FROM_EXP;
733 if ((iptr = check_innate_abil(ability, FROMRACE)) != 0)
734 return FROM_RACE;
735 if ((*ability & FROMOUTSIDE) != 0L)
736 return FROM_INTR;
737 if ((*ability & FROMFORM) != 0L)
738 return FROM_FORM;
739 return FROM_NONE;
743 is_innate(propidx)
744 int propidx;
746 int innateness;
748 /* innately() would report FROM_FORM for this; caller wants specificity */
749 if (propidx == DRAIN_RES && u.ulycn >= LOW_PM)
750 return FROM_LYCN;
751 if (propidx == FAST && Very_fast)
752 return FROM_NONE; /* can't become very fast innately */
753 if ((innateness = innately(&u.uprops[propidx].intrinsic)) != FROM_NONE)
754 return innateness;
755 if (propidx == JUMPING && Role_if(PM_KNIGHT)
756 /* knight has intrinsic jumping, but extrinsic is more versatile so
757 ignore innateness if equipment is going to claim responsibility */
758 && !u.uprops[propidx].extrinsic)
759 return FROM_ROLE;
760 if (propidx == BLINDED && !haseyes(youmonst.data))
761 return FROM_FORM;
762 return FROM_NONE;
765 char *
766 from_what(propidx)
767 int propidx; /* special cases can have negative values */
769 static char buf[BUFSZ];
771 buf[0] = '\0';
773 * Restrict the source of the attributes just to debug mode for now
775 if (wizard) {
776 static NEARDATA const char because_of[] = " because of %s";
778 if (propidx >= 0) {
779 char *p;
780 struct obj *obj = (struct obj *) 0;
781 int innateness = is_innate(propidx);
784 * Properties can be obtained from multiple sources and we
785 * try to pick the most significant one. Classification
786 * priority is not set in stone; current precedence is:
787 * "from the start" (from role or race at level 1),
788 * "from outside" (eating corpse, divine reward, blessed potion),
789 * "from experience" (from role or race at level 2+),
790 * "from current form" (while polymorphed),
791 * "from timed effect" (potion or spell),
792 * "from worn/wielded equipment" (Firebrand, elven boots, &c),
793 * "from carried equipment" (mainly quest artifacts).
794 * There are exceptions. Versatile jumping from spell or boots
795 * takes priority over knight's innate but limited jumping.
797 if (propidx == BLINDED && u.uroleplay.blind)
798 Sprintf(buf, " from birth");
799 else if (innateness == FROM_ROLE || innateness == FROM_RACE)
800 Strcpy(buf, " innately");
801 else if (innateness == FROM_INTR) /* [].intrinsic & FROMOUTSIDE */
802 Strcpy(buf, " intrinsically");
803 else if (innateness == FROM_EXP)
804 Strcpy(buf, " because of your experience");
805 else if (innateness == FROM_LYCN)
806 Strcpy(buf, " due to your lycanthropy");
807 else if (innateness == FROM_FORM)
808 Strcpy(buf, " from current creature form");
809 else if (propidx == FAST && Very_fast)
810 Sprintf(buf, because_of,
811 ((HFast & TIMEOUT) != 0L) ? "a potion or spell"
812 : ((EFast & W_ARMF) != 0L && uarmf->dknown
813 && objects[uarmf->otyp].oc_name_known)
814 ? ysimple_name(uarmf) /* speed boots */
815 : EFast ? "worn equipment"
816 : something);
817 else if (wizard
818 && (obj = what_gives(&u.uprops[propidx].extrinsic)) != 0)
819 Sprintf(buf, because_of, obj->oartifact
820 ? bare_artifactname(obj)
821 : ysimple_name(obj));
822 else if (propidx == BLINDED && Blindfolded_only)
823 Sprintf(buf, because_of, ysimple_name(ublindf));
825 /* remove some verbosity and/or redundancy */
826 if ((p = strstri(buf, " pair of ")) != 0)
827 copynchars(p + 1, p + 9, BUFSZ); /* overlapping buffers ok */
828 else if (propidx == STRANGLED
829 && (p = strstri(buf, " of strangulation")) != 0)
830 *p = '\0';
832 } else { /* negative property index */
833 /* if more blocking capabilities get implemented we'll need to
834 replace this with what_blocks() comparable to what_gives() */
835 switch (-propidx) {
836 case BLINDED:
837 if (ublindf
838 && ublindf->oartifact == ART_EYES_OF_THE_OVERWORLD)
839 Sprintf(buf, because_of, bare_artifactname(ublindf));
840 break;
841 case INVIS:
842 if (u.uprops[INVIS].blocked & W_ARMC)
843 Sprintf(buf, because_of,
844 ysimple_name(uarmc)); /* mummy wrapping */
845 break;
846 case CLAIRVOYANT:
847 if (wizard && (u.uprops[CLAIRVOYANT].blocked & W_ARMH))
848 Sprintf(buf, because_of,
849 ysimple_name(uarmh)); /* cornuthaum */
850 break;
854 } /*wizard*/
855 return buf;
858 void
859 adjabil(oldlevel, newlevel)
860 int oldlevel, newlevel;
862 register const struct innate *abil, *rabil;
863 long prevabil, mask = FROMEXPER;
865 abil = role_abil(Role_switch);
867 switch (Race_switch) {
868 case PM_ELF:
869 rabil = elf_abil;
870 break;
871 case PM_ORC:
872 rabil = orc_abil;
873 break;
874 case PM_HUMAN:
875 case PM_DWARF:
876 case PM_GNOME:
877 default:
878 rabil = 0;
879 break;
882 while (abil || rabil) {
883 /* Have we finished with the intrinsics list? */
884 if (!abil || !abil->ability) {
885 /* Try the race intrinsics */
886 if (!rabil || !rabil->ability)
887 break;
888 abil = rabil;
889 rabil = 0;
890 mask = FROMRACE;
892 prevabil = *(abil->ability);
893 if (oldlevel < abil->ulevel && newlevel >= abil->ulevel) {
894 /* Abilities gained at level 1 can never be lost
895 * via level loss, only via means that remove _any_
896 * sort of ability. A "gain" of such an ability from
897 * an outside source is devoid of meaning, so we set
898 * FROMOUTSIDE to avoid such gains.
900 if (abil->ulevel == 1)
901 *(abil->ability) |= (mask | FROMOUTSIDE);
902 else
903 *(abil->ability) |= mask;
904 if (!(*(abil->ability) & INTRINSIC & ~mask)) {
905 if (*(abil->gainstr))
906 You_feel("%s!", abil->gainstr);
908 } else if (oldlevel >= abil->ulevel && newlevel < abil->ulevel) {
909 *(abil->ability) &= ~mask;
910 if (!(*(abil->ability) & INTRINSIC)) {
911 if (*(abil->losestr))
912 You_feel("%s!", abil->losestr);
913 else if (*(abil->gainstr))
914 You_feel("less %s!", abil->gainstr);
917 if (prevabil != *(abil->ability)) /* it changed */
918 postadjabil(abil->ability);
919 abil++;
922 if (oldlevel > 0) {
923 if (newlevel > oldlevel)
924 add_weapon_skill(newlevel - oldlevel);
925 else
926 lose_weapon_skill(oldlevel - newlevel);
931 newhp()
933 int hp, conplus;
935 if (u.ulevel == 0) {
936 /* Initialize hit points */
937 hp = urole.hpadv.infix + urace.hpadv.infix;
938 if (urole.hpadv.inrnd > 0)
939 hp += rnd(urole.hpadv.inrnd);
940 if (urace.hpadv.inrnd > 0)
941 hp += rnd(urace.hpadv.inrnd);
942 if (moves <= 1L) { /* initial hero; skip for polyself to new man */
943 /* Initialize alignment stuff */
944 u.ualign.type = aligns[flags.initalign].value;
945 u.ualign.record = urole.initrecord;
947 /* no Con adjustment for initial hit points */
948 } else {
949 if (u.ulevel < urole.xlev) {
950 hp = urole.hpadv.lofix + urace.hpadv.lofix;
951 if (urole.hpadv.lornd > 0)
952 hp += rnd(urole.hpadv.lornd);
953 if (urace.hpadv.lornd > 0)
954 hp += rnd(urace.hpadv.lornd);
955 } else {
956 hp = urole.hpadv.hifix + urace.hpadv.hifix;
957 if (urole.hpadv.hirnd > 0)
958 hp += rnd(urole.hpadv.hirnd);
959 if (urace.hpadv.hirnd > 0)
960 hp += rnd(urace.hpadv.hirnd);
962 if (ACURR(A_CON) <= 3)
963 conplus = -2;
964 else if (ACURR(A_CON) <= 6)
965 conplus = -1;
966 else if (ACURR(A_CON) <= 14)
967 conplus = 0;
968 else if (ACURR(A_CON) <= 16)
969 conplus = 1;
970 else if (ACURR(A_CON) == 17)
971 conplus = 2;
972 else if (ACURR(A_CON) == 18)
973 conplus = 3;
974 else
975 conplus = 4;
976 hp += conplus;
978 if (hp <= 0)
979 hp = 1;
980 if (u.ulevel < MAXULEV)
981 u.uhpinc[u.ulevel] = (xchar) hp;
982 return hp;
985 schar
986 acurr(x)
987 int x;
989 register int tmp = (u.abon.a[x] + u.atemp.a[x] + u.acurr.a[x]);
991 if (x == A_STR) {
992 if (tmp >= 125 || (uarmg && uarmg->otyp == GAUNTLETS_OF_POWER))
993 return (schar) 125;
994 else
995 #ifdef WIN32_BUG
996 return (x = ((tmp <= 3) ? 3 : tmp));
997 #else
998 return (schar) ((tmp <= 3) ? 3 : tmp);
999 #endif
1000 } else if (x == A_CHA) {
1001 if (tmp < 18
1002 && (youmonst.data->mlet == S_NYMPH || u.umonnum == PM_SUCCUBUS
1003 || u.umonnum == PM_INCUBUS))
1004 return (schar) 18;
1005 } else if (x == A_INT || x == A_WIS) {
1006 /* yes, this may raise int/wis if player is sufficiently
1007 * stupid. there are lower levels of cognition than "dunce".
1009 if (uarmh && uarmh->otyp == DUNCE_CAP)
1010 return (schar) 6;
1012 #ifdef WIN32_BUG
1013 return (x = ((tmp >= 25) ? 25 : (tmp <= 3) ? 3 : tmp));
1014 #else
1015 return (schar) ((tmp >= 25) ? 25 : (tmp <= 3) ? 3 : tmp);
1016 #endif
1019 /* condense clumsy ACURR(A_STR) value into value that fits into game formulas
1021 schar
1022 acurrstr()
1024 register int str = ACURR(A_STR);
1026 if (str <= 18)
1027 return (schar) str;
1028 if (str <= 121)
1029 return (schar) (19 + str / 50); /* map to 19..21 */
1030 else
1031 return (schar) (min(str, 125) - 100); /* 22..25 */
1034 /* when wearing (or taking off) an unID'd item, this routine is used
1035 to distinguish between observable +0 result and no-visible-effect
1036 due to an attribute not being able to exceed maximum or minimum */
1037 boolean
1038 extremeattr(attrindx) /* does attrindx's value match its max or min? */
1039 int attrindx;
1041 /* Fixed_abil and racial MINATTR/MAXATTR aren't relevant here */
1042 int lolimit = 3, hilimit = 25, curval = ACURR(attrindx);
1044 /* upper limit for Str is 25 but its value is encoded differently */
1045 if (attrindx == A_STR) {
1046 hilimit = STR19(25); /* 125 */
1047 /* lower limit for Str can also be 25 */
1048 if (uarmg && uarmg->otyp == GAUNTLETS_OF_POWER)
1049 lolimit = hilimit;
1051 /* this exception is hypothetical; the only other worn item affecting
1052 Int or Wis is another helmet so can't be in use at the same time */
1053 if (attrindx == A_INT || attrindx == A_WIS) {
1054 if (uarmh && uarmh->otyp == DUNCE_CAP)
1055 hilimit = lolimit = 6;
1058 /* are we currently at either limit? */
1059 return (curval == lolimit || curval == hilimit) ? TRUE : FALSE;
1062 /* avoid possible problems with alignment overflow, and provide a centralized
1063 location for any future alignment limits */
1064 void
1065 adjalign(n)
1066 int n;
1068 int newalign = u.ualign.record + n;
1070 if (n < 0) {
1071 if (newalign < u.ualign.record)
1072 u.ualign.record = newalign;
1073 } else if (newalign > u.ualign.record) {
1074 u.ualign.record = newalign;
1075 if (u.ualign.record > ALIGNLIM)
1076 u.ualign.record = ALIGNLIM;
1080 /* change hero's alignment type, possibly losing use of artifacts */
1081 void
1082 uchangealign(newalign, reason)
1083 int newalign;
1084 int reason; /* 0==conversion, 1==helm-of-OA on, 2==helm-of-OA off */
1086 aligntyp oldalign = u.ualign.type;
1088 u.ublessed = 0; /* lose divine protection */
1089 context.botl = 1; /* status line needs updating */
1090 if (reason == 0) {
1091 /* conversion via altar */
1092 u.ualignbase[A_CURRENT] = (aligntyp) newalign;
1093 /* worn helm of opposite alignment might block change */
1094 if (!uarmh || uarmh->otyp != HELM_OF_OPPOSITE_ALIGNMENT)
1095 u.ualign.type = u.ualignbase[A_CURRENT];
1096 You("have a %ssense of a new direction.",
1097 (u.ualign.type != oldalign) ? "sudden " : "");
1098 } else {
1099 /* putting on or taking off a helm of opposite alignment */
1100 u.ualign.type = (aligntyp) newalign;
1101 if (reason == 1)
1102 Your("mind oscillates %s.", Hallucination ? "wildly" : "briefly");
1103 else if (reason == 2)
1104 Your("mind is %s.", Hallucination
1105 ? "much of a muchness"
1106 : "back in sync with your body");
1109 if (u.ualign.type != oldalign) {
1110 u.ualign.record = 0; /* slate is wiped clean */
1111 retouch_equipment(0);
1115 /*attrib.c*/