Skip to content

Commit

Permalink
Make monster::heal actually return amount healed (#51183)
Browse files Browse the repository at this point in the history
* Make monster::heal actually return amount healed

It is assumed in multiple places, and stated in monster.h, that the
monster::heal function returns the amount healed. Now it actually does.
This came about via #17452, which was to prevent heal from returning
a negative number; I see no way that it now can, but just in case I
have put in a std::max( 0, hp - old_hp ).

* Make recovering speed in on_load more sensible.

As suggested by @eltank (not sure how to get github to properly credit
them, and would like to know), make the healing/recovering of speed in
monster::on_load() proportional to the monster's current hp, so that
a fully healed monster also fully heals speed. Admittedly, it is
rather weird that healing speed doesn't seem to take place elsewhere,
as in while in the reality bubble.

* Follow @eltank's advice re not being paranoid

There really shouldn't be any way that it'll return negative now, and
it would probably be better to have some chance of realizing this
(via the negative return) if it did.

* Round up proportional healing of speed; typo fix

This probably doesn't matter, but make sure there's at least a bit of
healing of speed each time. Also fix one typo (`type-> speed`).

* Apply suggestions from code review

Float/int conversions strike again!

Co-authored-by: eltank <8000047+eltank@users.noreply.github.com>

* Fix error (likely from Github).

Applying a change suggested by @eltank on Github does not appear to
have gone correctly. Fixing.

Co-authored-by: actual-nh <actual-nh@users.noreply.github.com>
Co-authored-by: eltank <8000047+eltank@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 29, 2021
1 parent 83b4e06 commit 60cfb0a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,7 @@ int monster::heal( const int delta_hp, bool overheal )
if( hp > maxhp && !overheal ) {
hp = maxhp;
}
return maxhp - old_hp;
return hp - old_hp;
}

void monster::set_hp( const int hp )
Expand Down Expand Up @@ -3118,9 +3118,14 @@ void monster::on_load()
const int heal_amount = roll_remainder( regen * to_turns<int>( dt ) );
const int healed = heal( heal_amount );
int healed_speed = 0;
if( healed < heal_amount && get_speed_base() < type->speed ) {
int old_speed = get_speed_base();
set_speed_base( std::min( get_speed_base() + heal_amount - healed, type->speed ) );
if( get_speed_base() < type->speed ) {
const int old_speed = get_speed_base();
if( hp >= type->hp ) {
set_speed_base( type->speed );
} else {
const int speed_delta = std::max( healed * type->speed / type->hp, 1 );
set_speed_base( std::min( old_speed + speed_delta, type->speed ) );
}
healed_speed = get_speed_base() - old_speed;
}

Expand Down

0 comments on commit 60cfb0a

Please sign in to comment.