From 25957dc7718b4ca3585ff0bddb87103b38923a67 Mon Sep 17 00:00:00 2001 From: scarf Date: Fri, 29 Sep 2023 23:02:11 +0900 Subject: [PATCH] perf: correctly cache and invalidate dead state (#3304) * 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> --- src/character.cpp | 40 +++++++++++++++++++++++++++++++++++----- src/character.h | 6 ++++++ src/creature.h | 8 ++++---- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/character.cpp b/src/character.cpp index d3c3258465ed..5b73cb239753 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -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" @@ -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 ) @@ -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 ) ) { diff --git a/src/character.h b/src/character.h index 315e5b8a7275..73e39fb49169 100644 --- a/src/character.h +++ b/src/character.h @@ -249,6 +249,12 @@ class Character : public Creature, public visitable 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; diff --git a/src/creature.h b/src/creature.h index b046d80b3f10..e82711f1de52 100644 --- a/src/creature.h +++ b/src/creature.h @@ -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;