Skip to content

Commit

Permalink
fix github issue #752 - characteristics init
Browse files Browse the repository at this point in the history
Issue #752 by vultur-cadens:  initialization of characteristics had
off by one errors when reducing over-allocation and when increasing
under-allocation, biasing Str over Cha.

This simplifies the code very slightly but it still seems somewhat
confusing to me.

A couple of reformatting bits are included.

Closes #752
  • Loading branch information
PatR committed May 7, 2022
1 parent 6ccb566 commit 7d140c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
6 changes: 5 additions & 1 deletion doc/fixes3-7-0.txt
@@ -1,4 +1,4 @@
HDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.906 $ $NHDT-Date: 1651886993 2022/05/07 01:29:53 $
HDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.907 $ $NHDT-Date: 1651908301 2022/05/07 07:25:01 $

General Fixes and Modified Features
-----------------------------------
Expand Down Expand Up @@ -903,6 +903,10 @@ using wizard mode ^V in endgame to return to previously visited Plane of Water
likewise for clouds on Plane of Air
on tty at least, "version incompatibility for save/123xyzzy" was invisible:
a blank message of appropriate length followed by --More--
fix a pair of off-by-one bugs when doling out initial characteristics points,
resulting in an unintentional bias toward Str and away from Cha;
negligible effect on individual games but had a minor cumulative
effect across a large set of games


Fixes to 3.7.0-x Problems that Were Exposed Via git Repository
Expand Down
62 changes: 26 additions & 36 deletions src/attrib.c
@@ -1,4 +1,4 @@
/* NetHack 3.7 attrib.c $NHDT-Date: 1626312521 2021/07/15 01:28:41 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.79 $ */
/* NetHack 3.7 attrib.c $NHDT-Date: 1651908297 2022/05/07 07:24:57 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.86 $ */
/* Copyright 1988, 1989, 1990, 1992, M. Stephenson */
/* NetHack may be freely redistributed. See license for details. */

Expand Down Expand Up @@ -459,14 +459,11 @@ exerper(void)
{
if (!(g.moves % 10)) {
/* Hunger Checks */

int hs = (u.uhunger > 1000) ? SATIATED : (u.uhunger > 150)
? NOT_HUNGRY
: (u.uhunger > 50)
? HUNGRY
: (u.uhunger > 0)
? WEAK
: FAINTING;
int hs = (u.uhunger > 1000) ? SATIATED
: (u.uhunger > 150) ? NOT_HUNGRY
: (u.uhunger > 50) ? HUNGRY
: (u.uhunger > 0) ? WEAK
: FAINTING;

debugpline0("exerper: Hunger checks");
switch (hs) {
Expand Down Expand Up @@ -578,19 +575,13 @@ exerchk(void)
goto nextattrib;

debugpline2("exerchk: testing %s (%d).",
(i == A_STR)
? "Str"
: (i == A_INT)
? "Int?"
: (i == A_WIS)
? "Wis"
: (i == A_DEX)
? "Dex"
: (i == A_CON)
? "Con"
: (i == A_CHA)
? "Cha?"
: "???",
(i == A_STR) ? "Str"
: (i == A_INT) ? "Int?"
: (i == A_WIS) ? "Wis"
: (i == A_DEX) ? "Dex"
: (i == A_CON) ? "Con"
: (i == A_CHA) ? "Cha?"
: "???",
ax);
/*
* Law of diminishing returns (Part III):
Expand All @@ -617,10 +608,12 @@ exerchk(void)
AEXE(i) = (abs(ax) / 2) * mod_val;
}
g.context.next_attrib_check += rn1(200, 800);
debugpline1("exerchk: next check at %ld.", g.context.next_attrib_check);
debugpline1("exerchk: next check at %ld.",
g.context.next_attrib_check);
}
}

/* allocate hero's initial characteristics */
void
init_attr(int np)
{
Expand All @@ -632,15 +625,15 @@ init_attr(int np)
np -= g.urole.attrbase[i];
}

/* 3.7: the x -= ... calculation used to have an off by 1 error that
resulted in the values being biased toward Str and away from Cha */
tryct = 0;
while (np > 0 && tryct < 100) {
x = rn2(100);
for (i = 0; (i < A_MAX) && ((x -= g.urole.attrdist[i]) > 0); i++)
;
if (i >= A_MAX)
continue; /* impossible */

if (ABASE(i) >= ATTRMAX(i)) {
for (i = 0; i < A_MAX; ++i)
if ((x -= g.urole.attrdist[i]) < 0)
break;
if (i >= A_MAX || ABASE(i) >= ATTRMAX(i)) {
tryct++;
continue;
}
Expand All @@ -652,14 +645,11 @@ init_attr(int np)

tryct = 0;
while (np < 0 && tryct < 100) { /* for redistribution */

x = rn2(100);
for (i = 0; (i < A_MAX) && ((x -= g.urole.attrdist[i]) > 0); i++)
;
if (i >= A_MAX)
continue; /* impossible */

if (ABASE(i) <= ATTRMIN(i)) {
for (i = 0; i < A_MAX; ++i)
if ((x -= g.urole.attrdist[i]) < 0)
break;
if (i >= A_MAX || ABASE(i) <= ATTRMIN(i)) {
tryct++;
continue;
}
Expand Down

0 comments on commit 7d140c6

Please sign in to comment.