From afae3fff550710edf5a1d12302b2dac0fd53fe82 Mon Sep 17 00:00:00 2001 From: Kevin Granade Date: Wed, 13 Mar 2019 17:05:17 -0700 Subject: [PATCH] Adjust recalc_hp to avoid migrating hp levels Looks like cumulative rounding errors in recalc_hp were causing characters using the new JSON-based STS to suddenly die. --- src/character.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/character.cpp b/src/character.cpp index 48c1dcd91b1f6..18dfc80b24bf8 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -510,7 +510,15 @@ void Character::recalc_hp() new_max_hp[hp_head] *= 0.8; } for( int i = 0; i < num_hp_parts; i++ ) { - hp_cur[i] *= static_cast( new_max_hp[i] ) / static_cast( hp_max[i] ); + // Only recalculate when max changes, + // otherwise we end up walking all over due to rounding errors. + if( new_max_hp[i] == hp_max[i] ) { + continue; + } + float max_hp_ratio = static_cast( new_max_hp[i] ) / + static_cast( hp_max[i] ); + hp_cur[i] = std::ceil( static_cast( hp_cur[i] ) * max_hp_ratio ); + hp_cur[i] = std::max( std::min( hp_cur[i], new_max_hp[i] ), 1 ); hp_max[i] = new_max_hp[i]; } }