From 6a67b1833630499bb33619076623188347abe776 Mon Sep 17 00:00:00 2001 From: anothersimulacrum Date: Mon, 1 Mar 2021 17:07:51 -0800 Subject: [PATCH] Clean up character mutation gain a bit Reduce duplicated code. I presume there's a reason that set_mutations didn't just call set_mutation, and the only difference is that it doesn't do the cache updates till the end, so preserve that behaviour. --- src/character.h | 7 +++++++ src/mutation.cpp | 40 +++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/character.h b/src/character.h index 218f7e670cb49..d8e009c79a5d5 100644 --- a/src/character.h +++ b/src/character.h @@ -987,6 +987,13 @@ class Character : public Creature, public visitable /** Add or removes a mutation on the player, but does not trigger mutation loss/gain effects. */ void set_mutations( const std::vector &traits ); void set_mutation( const trait_id & ); + protected: + // Set a mutation, but don't do any of the necessary updates + // Only call this from one of the above two functions + void set_mutation_unsafe( const trait_id & ); + public: + // Do the mutation updates necessary when adding a mutation (nonspecific cache updates) + void do_mutation_updates(); void unset_mutation( const trait_id & ); /**Unset switched mutation and set target mutation instead*/ void switch_mutations( const trait_id &switched, const trait_id &target, bool start_powered ); diff --git a/src/mutation.cpp b/src/mutation.cpp index ea289fdce0c25..c8229f05af92d 100644 --- a/src/mutation.cpp +++ b/src/mutation.cpp @@ -158,27 +158,7 @@ void Character::toggle_trait( const trait_id &trait_ ) } } -void Character::set_mutations( const std::vector &traits ) -{ - for( const trait_id &trait : traits ) { - const auto iter = my_mutations.find( trait ); - if( iter != my_mutations.end() ) { - continue; - } - my_mutations.emplace( trait, trait_data{} ); - cached_mutations.push_back( &trait.obj() ); - mutation_effect( trait, false ); - } - recalc_sight_limits(); - calc_encumbrance(); - - // If the stamina is higher than the max (Languorous), set it back to max - if( get_stamina() > get_stamina_max() ) { - set_stamina( get_stamina_max() ); - } -} - -void Character::set_mutation( const trait_id &trait ) +void Character::set_mutation_unsafe( const trait_id &trait ) { const auto iter = my_mutations.find( trait ); if( iter != my_mutations.end() ) { @@ -187,6 +167,10 @@ void Character::set_mutation( const trait_id &trait ) my_mutations.emplace( trait, trait_data{} ); cached_mutations.push_back( &trait.obj() ); mutation_effect( trait, false ); +} + +void Character::do_mutation_updates() +{ recalc_sight_limits(); calc_encumbrance(); @@ -196,6 +180,20 @@ void Character::set_mutation( const trait_id &trait ) } } +void Character::set_mutations( const std::vector &traits ) +{ + for( const trait_id &trait : traits ) { + set_mutation_unsafe( trait ); + } + do_mutation_updates(); +} + +void Character::set_mutation( const trait_id &trait ) +{ + set_mutation_unsafe( trait ); + do_mutation_updates(); +} + void Character::unset_mutation( const trait_id &trait_ ) { // Take copy of argument because it might be a reference into a container