schlosrat / token-health

FoundryVTT module
5 stars 10 forks source link

Any amount of damage erases all health in AGE System (unofficial) #27

Closed schlosrat closed 3 years ago

schlosrat commented 3 years ago

In the AGE System (unofficial) healing works as expected but application of any amount of damage will cause that token's health to be blank (not 0, just blank). this effect is visible on the token's health bar which goes to empty and in the character sheet where the box with their health score is empty after applying even 1 point of damage.

Healing applied (negative damage entered) has the expected effect and will raise the heath by the amount of healing entered.

Example: Token starts with 10 health and a full health bar. Max health is 10, min is 0. Apply 1 point of damage using Token Health dialog Character sheet show blank in health and token health bar is empty Apply 1 point of healing (-1 damage) using Token health dialog Character sheet shows 1 in health and token health bar is in the red, but not empty Apply 3 points of healing (-3 damage) using Token health dialog Character sheet shows 4 in health and token health bar is orange and about 1/3 full Apply 4 points of healing (-4 damage) using Token health dialog Character sheet shows 8 in health and token health bar is yellow and about 3/4 full Apply 5 points of healing (-5 damage) using Token health dialog Character sheet shows 10 in health and token health bar is green and full

So, healing works fine but damage doesn't work.

schlosrat commented 3 years ago

I've sorted out this bug. It's actually caused by not having a value (or having an incorrect value) in the module settings for Temporary Hit Points Source, which in turn causes a NaN to propagate through the code as soon as you start working with the attribute on this line of token-health.js. Basically, the advertised behavior is to leave that entry blank if your game system doesn't use temporary HP, but there's no check to see if it was blank and consequently you enter getNewHP with tempHP === undefined, which then gives rise to cascading NaNs resulting in the affected tokens HP being NaN.

const temp = getProperty(data, CONFIG.TEMP_HITPOINTS_ATTRIBUTE);

There are lots of ways t fix this, for example in getNewHP.js you could do this:

  if (tempHP === undefined) {
    // Get new HP value after applying damage
    let tmpHP = currentHP - value;

    // Make sure to return a negative number if allowed
    if (!options.allowNegative) tmpHP = Math.max(tmpHP, 0);

    // Make sure the hp value is less than max
    const hp = Math.min(tmpHP, maxHP);

    return [hp, tempHP];

  } else {
    // Store the temp HP value
    const tmp = Number(tempHP) ?? 0;

    // Calculate value to apply on temp only
    const dt = value > 0 ? Math.min(tmp, value) : 0;

    // Apply value to temp
    const temp = tmp - dt;

    // Get new HP value after applying some of it to temp
    let tmpHP = currentHP - (value - dt);

    // Make sure to return a negative number if allowed
    if (!options.allowNegative) tmpHP = Math.max(tmpHP, 0);

    // Make sure the hp value is less than max
    const hp = Math.min(tmpHP, maxHP);
    return [hp, temp];
  }
schlosrat commented 3 years ago

Bug fixed