Skip to content

Commit

Permalink
tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
OzoneH3 committed Nov 13, 2019
1 parent aa998cb commit 40956bc
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 86 deletions.
87 changes: 87 additions & 0 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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", 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" );
}
}
88 changes: 10 additions & 78 deletions src/avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "map_memory.h"
#include "pldata.h"
#include "point.h"
#include "options.h"

class JsonIn;
class JsonObject;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<int>( "INITIAL_STAT_POINTS" );
trait_points = get_option<int>( "INITIAL_TRAIT_POINTS" );
skill_points = get_option<int>( "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: <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", 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
2 changes: 1 addition & 1 deletion src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down
2 changes: 1 addition & 1 deletion src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class Character : public Creature, public visitable<Character>
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;
Expand Down
2 changes: 1 addition & 1 deletion src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions src/newcharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++ ) {
Expand Down Expand Up @@ -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" ) );
Expand Down Expand Up @@ -2536,7 +2536,7 @@ cata::optional<std::string> 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)
Expand All @@ -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" ) );
Expand Down

0 comments on commit 40956bc

Please sign in to comment.