Skip to content

Commit

Permalink
Clean up character mutation gain a bit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anothersimulacrum authored and ZhilkinSerg committed Mar 16, 2021
1 parent b86cf92 commit ae06a85
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<trait_id> &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 );
Expand Down
40 changes: 19 additions & 21 deletions src/mutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,7 @@ void Character::toggle_trait( const trait_id &trait_ )
}
}

void Character::set_mutations( const std::vector<trait_id> &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() ) {
Expand All @@ -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();

Expand All @@ -196,6 +180,20 @@ void Character::set_mutation( const trait_id &trait )
}
}

void Character::set_mutations( const std::vector<trait_id> &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
Expand Down

0 comments on commit ae06a85

Please sign in to comment.