Skip to content

Commit

Permalink
perf: correctly cache and invalidate dead state (#3304)
Browse files Browse the repository at this point in the history
* perf: correctly cache and invalidate dead state

duplicate codes are awful, but other solutions involve editing lots of other files...

* style(autofix.ci): automated formatting

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
scarf005 and autofix-ci[bot] authored Sep 29, 2023
1 parent f4520a1 commit 25957dc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
40 changes: 35 additions & 5 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "consumption.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "creature.h"
#include "damage.h"
#include "debug.h"
#include "disease.h"
Expand Down Expand Up @@ -514,13 +515,10 @@ auto Character::is_dead_state() const -> bool
}

const auto all_bps = get_all_body_parts( true );
const bool is_dead = std::any_of( all_bps.begin(), all_bps.end(), [this]( const bodypart_id & bp ) {
cached_dead_state = std::any_of( all_bps.begin(), all_bps.end(), [this]( const bodypart_id & bp ) {
return bp->essential && get_part_hp_cur( bp ) <= 0;
} );
if( is_dead ) {
cached_dead_state = true;
}
return is_dead;
return cached_dead_state.value();
}

void Character::set_part_hp_cur( const bodypart_id &id, int set )
Expand All @@ -531,6 +529,38 @@ void Character::set_part_hp_cur( const bodypart_id &id, int set )
Creature::set_part_hp_cur( id, set );
}

void Character::set_part_hp_max( const bodypart_id &id, int set )
{
if( set <= 0 ) {
cached_dead_state.reset();
}
Creature::set_part_hp_max( id, set );
}

void Character::mod_part_hp_cur( const bodypart_id &id, int mod )
{
if( mod < 0 ) {
cached_dead_state.reset();
}
Creature::mod_part_hp_cur( id, mod );
}

void Character::mod_part_hp_max( const bodypart_id &id, int mod )
{
if( mod < 0 ) {
cached_dead_state.reset();
}
Creature::mod_part_hp_max( id, mod );
}

void Character::set_all_parts_hp_cur( int set )
{
if( set <= 0 ) {
cached_dead_state.reset();
}
Creature::set_all_parts_hp_cur( set );
}

field_type_id Character::bloodType() const
{
if( has_trait( trait_ACIDBLOOD ) ) {
Expand Down
6 changes: 6 additions & 0 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ class Character : public Creature, public visitable<Character>

public:
void set_part_hp_cur( const bodypart_id &id, int set ) override;
void set_part_hp_max( const bodypart_id &id, int set ) override;

void mod_part_hp_cur( const bodypart_id &id, int mod ) override;
void mod_part_hp_max( const bodypart_id &id, int mod ) override;

void set_all_parts_hp_cur( int set ) override;

field_type_id bloodType() const override;
field_type_id gibType() const override;
Expand Down
8 changes: 4 additions & 4 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,14 @@ class Creature
int get_part_healed_total( const bodypart_id &id ) const;

virtual void set_part_hp_cur( const bodypart_id &id, int set );
void set_part_hp_max( const bodypart_id &id, int set );
virtual void set_part_hp_max( const bodypart_id &id, int set );
void set_part_healed_total( const bodypart_id &id, int set );
void mod_part_hp_cur( const bodypart_id &id, int mod );
void mod_part_hp_max( const bodypart_id &id, int mod );
virtual void mod_part_hp_cur( const bodypart_id &id, int mod );
virtual void mod_part_hp_max( const bodypart_id &id, int mod );
void mod_part_healed_total( const bodypart_id &id, int mod );


void set_all_parts_hp_cur( int set );
virtual void set_all_parts_hp_cur( int set );
void set_all_parts_hp_to_max();

virtual int get_speed_base() const;
Expand Down

0 comments on commit 25957dc

Please sign in to comment.