From 40956bc52c156bacda1f94067116b9f68607b136 Mon Sep 17 00:00:00 2001 From: OzoneH3 Date: Tue, 12 Nov 2019 09:14:22 +0100 Subject: [PATCH] tidy up code --- src/avatar.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++ src/avatar.h | 88 +++++--------------------------------------- src/character.cpp | 2 +- src/character.h | 2 +- src/main_menu.cpp | 2 +- src/newcharacter.cpp | 10 ++--- 6 files changed, 105 insertions(+), 86 deletions(-) diff --git a/src/avatar.cpp b/src/avatar.cpp index b6365f31d3fc1..ed702634113e8 100644 --- a/src/avatar.cpp +++ b/src/avatar.cpp @@ -1591,3 +1591,90 @@ bool avatar::invoke_item( item *used, const std::string &method ) { return Character::invoke_item( used, method ); } + +points_left::points_left() +{ + limit = MULTI_POOL; + init_from_options(); +} + +void points_left::init_from_options() +{ + stat_points = get_option( "INITIAL_STAT_POINTS" ); + trait_points = get_option( "INITIAL_TRAIT_POINTS" ); + skill_points = get_option( "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: %d%c%d%c%d=%d" ), + stat_points_left() >= 0 ? "light_gray" : "red", stat_points, + trait_points >= 0 ? '+' : '-', + trait_points_left() >= 0 ? "light_gray" : "red", abs( trait_points ), + skill_points >= 0 ? '+' : '-', + skill_points_left() >= 0 ? "light_gray" : "red", 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" ); + } +} diff --git a/src/avatar.h b/src/avatar.h index 8110c584b112f..d67abc1ca8571 100644 --- a/src/avatar.h +++ b/src/avatar.h @@ -16,7 +16,6 @@ #include "map_memory.h" #include "pldata.h" #include "point.h" -#include "options.h" class JsonIn; class JsonObject; @@ -45,7 +44,7 @@ class avatar : public player bool create( character_type type, const std::string &tempname = "" ); void randomize( bool random_scenario, points_left &points, bool play_now = false ); bool load_template( const std::string &template_name, points_left &points ); - static void save_template( const avatar &u, const std::string &name, const points_left &points ); + void save_template( const std::string &name, const points_left &points ); bool is_avatar() const override { return true; @@ -235,83 +234,16 @@ struct points_left { TRANSFER, } limit; - points_left() { - limit = MULTI_POOL; - init_from_options(); - } - - void init_from_options() { - stat_points = get_option( "INITIAL_STAT_POINTS" ); - trait_points = get_option( "INITIAL_TRAIT_POINTS" ); - skill_points = get_option( "INITIAL_SKILL_POINTS" ); - } - + points_left(); + void init_from_options(); // Highest amount of points to spend on stats without points going invalid - int 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 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 skill_points_left() const { - return stat_points + trait_points + skill_points; - } - - bool is_freeform() { - return limit == FREEFORM; - } - - bool is_valid() { - return is_freeform() || - ( stat_points_left() >= 0 && trait_points_left() >= 0 && - skill_points_left() >= 0 ); - } - - bool has_spare() { - return !is_freeform() && is_valid() && skill_points_left() > 0; - } - - std::string to_string() { - if( limit == MULTI_POOL ) { - return string_format( - _( "Points left: %d%c%d%c%d=%d" ), - stat_points_left() >= 0 ? "light_gray" : "red", stat_points, - trait_points >= 0 ? '+' : '-', - trait_points_left() >= 0 ? "light_gray" : "red", abs( trait_points ), - skill_points >= 0 ? '+' : '-', - skill_points_left() >= 0 ? "light_gray" : "red", 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 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 diff --git a/src/character.cpp b/src/character.cpp index 2163ee4bff5a2..58bb76c0b186e 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -280,7 +280,7 @@ Character::~Character() = default; Character::Character( Character && ) = default; Character &Character::operator=( Character && ) = default; -void Character::setID( character_id i, const bool force ) +void Character::setID( character_id i, bool force ) { if( id.is_valid() && !force ) { debugmsg( "tried to set id of a npc/player, but has already a id: %d", id.get_value() ); diff --git a/src/character.h b/src/character.h index 9a45de6b4a3fd..495c5f1b24f76 100644 --- a/src/character.h +++ b/src/character.h @@ -200,7 +200,7 @@ class Character : public Creature, public visitable character_id getID() const; // sets the ID, will *only* succeed when the current id is not valid // allows forcing a -1 id which is required for templates to not throw errors - void setID( character_id i, const bool force = false ); + void setID( character_id i, bool force = false ); field_type_id bloodType() const override; field_type_id gibType() const override; diff --git a/src/main_menu.cpp b/src/main_menu.cpp index c48f3f3a87b50..fcfa46459e392 100644 --- a/src/main_menu.cpp +++ b/src/main_menu.cpp @@ -1062,7 +1062,7 @@ void main_menu::world_tab() g->u.setID( character_id(), true ); g->u.reset_all_misions(); - avatar::save_template( g->u, g->u.name, points ); + g->u.save_template( g->u.name, points ); g->u = avatar(); MAPBUFFER.reset(); diff --git a/src/newcharacter.cpp b/src/newcharacter.cpp index 4911fdc643bd1..4bd2167633845 100644 --- a/src/newcharacter.cpp +++ b/src/newcharacter.cpp @@ -468,7 +468,7 @@ bool avatar::create( character_type type, const std::string &tempname ) return true; } - save_template( *this, _( "Last Character" ), points ); + save_template( _( "Last Character" ), points ); recalc_hp(); for( int i = 0; i < num_hp_parts; i++ ) { @@ -2363,7 +2363,7 @@ tab_direction set_description( const catacurses::window &w, avatar &you, const b return tab_direction::NONE; } else if( action == "SAVE_TEMPLATE" ) { if( const auto name = query_for_template_name() ) { - avatar::save_template( you, *name, points ); + you.save_template( *name, points ); } // redraw after saving template draw_character_tabs( w, _( "DESCRIPTION" ) ); @@ -2536,7 +2536,7 @@ cata::optional query_for_template_name() } } -void avatar::save_template( const avatar &u, const std::string &name, const points_left &points ) +void avatar::save_template( const std::string &name, const points_left &points ) { std::string native = utf8_to_native( name ); #if defined(_WIN32) @@ -2561,10 +2561,10 @@ void avatar::save_template( const avatar &u, const std::string &name, const poin jsout.member( "trait_points", points.trait_points ); jsout.member( "skill_points", points.skill_points ); jsout.member( "limit", points.limit ); - jsout.member( "start_location", u.start_location ); + jsout.member( "start_location", start_location ); jsout.end_object(); - u.serialize( jsout ); + serialize( jsout ); jsout.end_array(); }, _( "player template" ) );