Skip to content

Commit

Permalink
Merge pull request #50772 from hexagonrecursion/points
Browse files Browse the repository at this point in the history
Refactor: stop duplicating information in the character creation
  • Loading branch information
kevingranade committed Aug 21, 2021
2 parents bf3398f + 3a1763e commit 5343061
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 344 deletions.
95 changes: 1 addition & 94 deletions src/avatar.cpp
Expand Up @@ -1556,118 +1556,25 @@ std::unique_ptr<talker> get_talker_for( avatar *me )
return std::make_unique<talker_avatar>( me );
}

points_left::points_left()
{
limit = MULTI_POOL;
init_from_options();
}

void points_left::init_from_options()
{
stat_points = get_option<int>( "INITIAL_STAT_POINTS" );
trait_points = get_option<int>( "INITIAL_TRAIT_POINTS" );
skill_points = get_option<int>( "INITIAL_SKILL_POINTS" );
}

// Highest amount of points to spend on stats without points going invalid
int points_left::stat_points_left() const
{
switch( limit ) {
case FREEFORM:
case ONE_POOL:
return stat_points + trait_points + skill_points;
case MULTI_POOL:
return std::min( trait_points_left(),
stat_points + std::min( 0, trait_points + skill_points ) );
case TRANSFER:
return 0;
}

return 0;
}

int points_left::trait_points_left() const
{
switch( limit ) {
case FREEFORM:
case ONE_POOL:
return stat_points + trait_points + skill_points;
case MULTI_POOL:
return stat_points + trait_points + std::min( 0, skill_points );
case TRANSFER:
return 0;
}

return 0;
}

int points_left::skill_points_left() const
{
return stat_points + trait_points + skill_points;
}

bool points_left::is_freeform()
{
return limit == FREEFORM;
}

bool points_left::is_valid()
{
return is_freeform() ||
( stat_points_left() >= 0 && trait_points_left() >= 0 &&
skill_points_left() >= 0 );
}

bool points_left::has_spare()
{
return !is_freeform() && is_valid() && skill_points_left() > 0;
}

std::string points_left::to_string()
{
if( limit == MULTI_POOL ) {
return string_format(
_( "Points left: <color_%s>%d</color>%c<color_%s>%d</color>%c<color_%s>%d</color>=<color_%s>%d</color>" ),
stat_points_left() >= 0 ? "light_gray" : "red", stat_points,
trait_points >= 0 ? '+' : '-',
trait_points_left() >= 0 ? "light_gray" : "red", std::abs( trait_points ),
skill_points >= 0 ? '+' : '-',
skill_points_left() >= 0 ? "light_gray" : "red", std::abs( skill_points ),
is_valid() ? "light_gray" : "red", stat_points + trait_points + skill_points );
} else if( limit == ONE_POOL ) {
return string_format( _( "Points left: %4d" ), skill_points_left() );
} else if( limit == TRANSFER ) {
return _( "Character Transfer: No changes can be made." );
} else {
return _( "Freeform" );
}
}

int avatar::randomize_hobbies()
void avatar::randomize_hobbies()
{
hobbies.clear();
std::vector<profession_id> choices = profession::get_all_hobbies();

int random = rng( 0, 5 );
int points = 0;

if( random >= 1 ) {
const profession_id hobby = random_entry_removed( choices );
points += hobby->point_cost();
hobbies.insert( &*hobby );
}
if( random >= 3 ) {
const profession_id hobby = random_entry_removed( choices );
points += hobby->point_cost();
hobbies.insert( &*hobby );
}
if( random >= 5 ) {
const profession_id hobby = random_entry_removed( choices );
points += hobby->point_cost();
hobbies.insert( &*hobby );
}

return points;
}

void avatar::reassign_item( item &it, int invlet )
Expand Down
35 changes: 6 additions & 29 deletions src/avatar.h
Expand Up @@ -49,7 +49,7 @@ namespace debug_menu
class mission_debug;
} // namespace debug_menu
struct mtype;
struct points_left;
enum class pool_type;

// Monster visible in different directions (safe mode & compass)
struct monster_visible_info {
Expand Down Expand Up @@ -89,9 +89,10 @@ class avatar : public player
// newcharacter.cpp
bool create( character_type type, const std::string &tempname = "" );
void add_profession_items();
void randomize( bool random_scenario, points_left &points, bool play_now = false );
bool load_template( const std::string &template_name, points_left &points );
void save_template( const std::string &name, const points_left &points );
void randomize( bool random_scenario, bool play_now = false );
bool load_template( const std::string &template_name, pool_type & );
void save_template( const std::string &name, pool_type );
void character_to_template( const std::string &name );

bool is_avatar() const override {
return true;
Expand Down Expand Up @@ -308,7 +309,7 @@ class avatar : public player
void log_activity_level( float level ) override;
std::string total_daily_calories_string() const;
//set 0-3 random hobbies, with 1 and 2 being twice as likely as 0 and 3
int randomize_hobbies();
void randomize_hobbies();

int movecounter = 0;

Expand Down Expand Up @@ -365,28 +366,4 @@ avatar &get_avatar();
std::unique_ptr<talker> get_talker_for( avatar &me );
std::unique_ptr<talker> get_talker_for( avatar *me );

struct points_left {
int stat_points;
int trait_points;
int skill_points;

enum point_limit : int {
FREEFORM = 0,
ONE_POOL,
MULTI_POOL,
TRANSFER,
} limit;

points_left();
void init_from_options();
// Highest amount of points to spend on stats without points going invalid
int stat_points_left() const;
int trait_points_left() const;
int skill_points_left() const;
bool is_freeform();
bool is_valid();
bool has_spare();
std::string to_string();
};

#endif // CATA_SRC_AVATAR_H
1 change: 0 additions & 1 deletion src/character.h
Expand Up @@ -2195,7 +2195,6 @@ class Character : public Creature, public visitable
* And if you do already have them, refunds the points for the trait
*/
void add_traits();
void add_traits( points_left &points );
/** Returns true if the player has crossed a mutation threshold
* Player can only cross one mutation threshold.
*/
Expand Down
8 changes: 1 addition & 7 deletions src/main_menu.cpp
Expand Up @@ -1231,15 +1231,9 @@ void main_menu::world_tab()
ui_manager::redraw();
if( layer == 4 ) { //Character to Template
if( load_character_tab( true ) ) {
points_left points;
points.stat_points = 0;
points.trait_points = 0;
points.skill_points = 0;
points.limit = points_left::TRANSFER;

player_character.setID( character_id(), true );
player_character.reset_all_missions();
player_character.save_template( player_character.name, points );
player_character.character_to_template( player_character.name );

player_character = avatar();
MAPBUFFER.clear();
Expand Down

0 comments on commit 5343061

Please sign in to comment.