From 4c378295ed83f92f9b7fcbd71f1d2c1a0ad48744 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Fri, 23 Oct 2009 19:17:05 +0200 Subject: Use species_type/job_type instead of int. --- crawl-ref/source/debug.cc | 7 +++---- crawl-ref/source/hiscores.cc | 11 +++++----- crawl-ref/source/hiscores.h | 4 ++-- crawl-ref/source/jobs.cc | 48 ++++++++++++++++++++++---------------------- crawl-ref/source/jobs.h | 4 ++-- crawl-ref/source/newgame.cc | 4 ++-- crawl-ref/source/species.cc | 36 ++++++++++++++++++--------------- crawl-ref/source/species.h | 10 ++++----- 8 files changed, 63 insertions(+), 61 deletions(-) diff --git a/crawl-ref/source/debug.cc b/crawl-ref/source/debug.cc index d77c62f2e4..f2bf672f85 100644 --- a/crawl-ref/source/debug.cc +++ b/crawl-ref/source/debug.cc @@ -596,11 +596,10 @@ void wizard_create_spec_monster_name() mpr("Make player ghost which species? (case-sensitive) ", MSGCH_PROMPT); get_input_line( input_str, sizeof( input_str ) ); - int sp_id = get_species_by_abbrev(input_str); - if (sp_id == -1) + species_type sp_id = get_species_by_abbrev(input_str); + if (sp_id == SP_UNKNOWN) sp_id = str_to_species(input_str); - - if (sp_id == -1) + if (sp_id == SP_UNKNOWN) { mpr("No such species, making it Human."); sp_id = SP_HUMAN; diff --git a/crawl-ref/source/hiscores.cc b/crawl-ref/source/hiscores.cc index 5835416612..88bfe3b760 100644 --- a/crawl-ref/source/hiscores.cc +++ b/crawl-ref/source/hiscores.cc @@ -639,8 +639,7 @@ void scorefile_entry::set_base_xlog_fields() const fields->add_field("name", "%s", name.c_str()); fields->add_field("uid", "%d", uid); fields->add_field("race", "%s", - species_name(static_cast(race), - lvl).c_str()); + species_name(race, lvl).c_str()); fields->add_field("cls", "%s", get_class_name(cls)); fields->add_field("char", "%s%s", get_species_abbrev(race), get_class_abbrev(cls)); @@ -648,8 +647,8 @@ void scorefile_entry::set_base_xlog_fields() const fields->add_field("sk", "%s", skill_name(best_skill)); fields->add_field("sklev", "%d", best_skill_lvl); fields->add_field("title", "%s", - skill_title( best_skill, best_skill_lvl, - race, str, dex, god ).c_str() ); + skill_title(best_skill, best_skill_lvl, + race, str, dex, god).c_str()); // "place" is a human readable place name, and it is write-only, // so we can write place names like "Bazaar" that Crawl cannot @@ -861,8 +860,8 @@ void scorefile_entry::reset() points = -1; name.clear(); uid = 0; - race = 0; - cls = 0; + race = SP_UNKNOWN; + cls = JOB_UNKNOWN; lvl = 0; race_class_name.clear(); best_skill = 0; diff --git a/crawl-ref/source/hiscores.h b/crawl-ref/source/hiscores.h index b68bc85d57..2d5f6b8f53 100644 --- a/crawl-ref/source/hiscores.h +++ b/crawl-ref/source/hiscores.h @@ -84,8 +84,8 @@ public: long points; std::string name; long uid; // for multiuser systems - char race; - char cls; + species_type race; + job_type cls; std::string race_class_name; // overrides race & cls if non-empty. char lvl; // player level. char best_skill; // best skill # diff --git a/crawl-ref/source/jobs.cc b/crawl-ref/source/jobs.cc index 14e88c55bc..07e1ae139e 100644 --- a/crawl-ref/source/jobs.cc +++ b/crawl-ref/source/jobs.cc @@ -49,8 +49,8 @@ static job_type new_jobs_order[] = { job_type get_class(const int index) { - if (index < 0 || (unsigned int) index >= ARRAYSZ(old_jobs_order)) - return (JOB_UNKNOWN); + if (index < 0 || index >= ng_num_classes()) + return (JOB_UNKNOWN); return (Options.use_old_selection_order? old_jobs_order[index] : new_jobs_order[index]); @@ -69,7 +69,7 @@ static const char * Class_Name_List[ NUM_JOBS ] = "Chaos Knight", "Transmuter", "Healer", "Reaver", "Stalker", "Monk", "Warper", "Wanderer", "Artificer" }; -int get_class_index_by_abbrev( const char *abbrev ) +int get_class_index_by_abbrev(const char *abbrev) { COMPILE_CHECK(ARRAYSZ(Class_Abbrev_List) == NUM_JOBS, c1); @@ -89,39 +89,39 @@ int get_class_index_by_abbrev( const char *abbrev ) return (-1); } -const char *get_class_abbrev( int which_job ) +const char *get_class_abbrev(int which_job) { - ASSERT( which_job < NUM_JOBS ); + ASSERT(which_job >= 0 && which_job < NUM_JOBS); - return (Class_Abbrev_List[ which_job ]); + return (Class_Abbrev_List[which_job]); } -int get_class_by_abbrev( const char *abbrev ) +job_type get_class_by_abbrev(const char *abbrev) { int i; for (i = 0; i < NUM_JOBS; i++) { - if (tolower( abbrev[0] ) == tolower( Class_Abbrev_List[i][0] ) - && tolower( abbrev[1] ) == tolower( Class_Abbrev_List[i][1] )) + if (tolower(abbrev[0]) == tolower(Class_Abbrev_List[i][0]) + && tolower(abbrev[1]) == tolower(Class_Abbrev_List[i][1])) { break; } } - return ((i < NUM_JOBS) ? i : -1); + return ((i < NUM_JOBS) ? static_cast(i) : JOB_UNKNOWN); } -int get_class_index_by_name( const char *name ) +int get_class_index_by_name(const char *name) { - COMPILE_CHECK(ARRAYSZ(Class_Name_List) == NUM_JOBS, c1); + COMPILE_CHECK(ARRAYSZ(Class_Name_List) == NUM_JOBS, c1); char *ptr; char lowered_buff[80]; char lowered_class[80]; - strncpy( lowered_buff, name, sizeof( lowered_buff ) ); - strlwr( lowered_buff ); + strncpy(lowered_buff, name, sizeof(lowered_buff)); + strlwr(lowered_buff); int cl = -1; unsigned int job; @@ -147,32 +147,32 @@ int get_class_index_by_name( const char *name ) const char *get_class_name( int which_job ) { - ASSERT( which_job < NUM_JOBS ); + ASSERT(which_job >= 0 && which_job < NUM_JOBS); - return (Class_Name_List[ which_job ]); + return (Class_Name_List[which_job]); } -int get_class_by_name( const char *name ) +job_type get_class_by_name(const char *name) { int i; - int cl = -1; + job_type cl = JOB_UNKNOWN; char *ptr; char lowered_buff[80]; char lowered_class[80]; - strncpy( lowered_buff, name, sizeof( lowered_buff ) ); - strlwr( lowered_buff ); + strncpy(lowered_buff, name, sizeof(lowered_buff)); + strlwr(lowered_buff); for (i = 0; i < NUM_JOBS; i++) { - strncpy( lowered_class, Class_Name_List[i], sizeof( lowered_class ) ); - strlwr( lowered_class ); + strncpy(lowered_class, Class_Name_List[i], sizeof(lowered_class)); + strlwr(lowered_class); - ptr = strstr( lowered_class, lowered_buff ); + ptr = strstr(lowered_class, lowered_buff); if (ptr != NULL) { - cl = i; + cl = static_cast(i); if (ptr == lowered_class) // prefix takes preference break; } diff --git a/crawl-ref/source/jobs.h b/crawl-ref/source/jobs.h index f885418f65..70d87ab67b 100644 --- a/crawl-ref/source/jobs.h +++ b/crawl-ref/source/jobs.h @@ -5,9 +5,9 @@ int ng_num_classes(); job_type get_class(const int index); int get_class_index_by_abbrev(const char *abbrev); const char *get_class_abbrev(int which_job); -int get_class_by_abbrev(const char *abbrev); +job_type get_class_by_abbrev(const char *abbrev); int get_class_index_by_name(const char *name); const char *get_class_name(int which_job); -int get_class_by_name(const char *name); +job_type get_class_by_name(const char *name); #endif diff --git a/crawl-ref/source/newgame.cc b/crawl-ref/source/newgame.cc index ffcfb01e4f..464fb3e58e 100644 --- a/crawl-ref/source/newgame.cc +++ b/crawl-ref/source/newgame.cc @@ -149,13 +149,13 @@ static bool _prev_startup_options_set(void) static std::string _get_opt_race_name(char race) { species_type prace = get_species(letter_to_index(race)); - return (prace == SP_UNKNOWN? "Random" : species_name(prace, 1)); + return (prace == SP_UNKNOWN ? "Random" : species_name(prace, 1)); } static std::string _get_opt_class_name(char oclass) { job_type pclass = get_class(letter_to_index(oclass)); - return (pclass == JOB_UNKNOWN? "Random" : get_class_name(pclass)); + return (pclass == JOB_UNKNOWN ? "Random" : get_class_name(pclass)); } static std::string _prev_startup_description(void) diff --git a/crawl-ref/source/species.cc b/crawl-ref/source/species.cc index ab0556945d..f5ceb28751 100644 --- a/crawl-ref/source/species.cc +++ b/crawl-ref/source/species.cc @@ -61,7 +61,8 @@ species_type random_draconian_player_species() species_type get_species(const int index) { - ASSERT(index >= 0 && index < ARRAYSZ(old_species_order)); + if(index < 0 || index >= ng_num_species()) + return (SP_UNKNOWN); return (Options.use_old_selection_order ? old_species_order[index] : new_species_order[index]); @@ -126,28 +127,28 @@ int get_species_index_by_name( const char *name ) return (sp); } -const char *get_species_abbrev(int which_species) +const char *get_species_abbrev(species_type which_species) { - ASSERT( which_species > 0 && which_species < NUM_SPECIES ); + ASSERT(which_species > 0 && which_species < NUM_SPECIES); - return (Species_Abbrev_List[ which_species ]); + return (Species_Abbrev_List[which_species]); } // Needed for debug.cc and hiscores.cc. -int get_species_by_abbrev(const char *abbrev) +species_type get_species_by_abbrev(const char *abbrev) { int i; COMPILE_CHECK(ARRAYSZ(Species_Abbrev_List) == NUM_SPECIES, c1); for (i = SP_HUMAN; i < NUM_SPECIES; i++) { - if (tolower( abbrev[0] ) == tolower( Species_Abbrev_List[i][0] ) - && tolower( abbrev[1] ) == tolower( Species_Abbrev_List[i][1] )) + if (tolower(abbrev[0]) == tolower(Species_Abbrev_List[i][0]) + && tolower(abbrev[1]) == tolower(Species_Abbrev_List[i][1])) { break; } } - return ((i < NUM_SPECIES) ? i : -1); + return ((i < NUM_SPECIES) ? static_cast(i) : SP_UNKNOWN); } int ng_num_species() @@ -160,26 +161,29 @@ int ng_num_species() } // Does a case-sensitive lookup of the species name supplied. -int str_to_species(const std::string &species) +species_type str_to_species(const std::string &species) { + species_type sp; if (species.empty()) - return SP_HUMAN; + return SP_UNKNOWN; // first look for full name (e.g. Green Draconian) for (int i = SP_HUMAN; i < NUM_SPECIES; ++i) { - if (species == species_name(static_cast(i), 10)) - return (i); + sp = static_cast(i); + if (species == species_name(sp, 10)) + return (sp); } // nothing found, try again with plain name for (int i = SP_HUMAN; i < NUM_SPECIES; ++i) { - if (species == species_name(static_cast(i), 1)) - return (i); + sp = static_cast(i); + if (species == species_name(sp, 1)) + return (sp); } - return (SP_HUMAN); + return (SP_UNKNOWN); } std::string species_name(species_type speci, int level, bool genus, bool adj) @@ -187,7 +191,7 @@ std::string species_name(species_type speci, int level, bool genus, bool adj) { std::string res; - if (player_genus( GENPC_DRACONIAN, speci )) + if (player_genus(GENPC_DRACONIAN, speci)) { if (adj || genus) // adj doesn't care about exact species res = "Draconian"; diff --git a/crawl-ref/source/species.h b/crawl-ref/source/species.h index 03709311fa..d84219847c 100644 --- a/crawl-ref/source/species.h +++ b/crawl-ref/source/species.h @@ -5,15 +5,15 @@ species_type get_species(const int index); species_type random_draconian_player_species(); int ng_num_species(); -int get_species_by_abbrev(const char *abbrev); -const char *get_species_abbrev(int which_species); +species_type get_species_by_abbrev(const char *abbrev); +const char *get_species_abbrev(species_type which_species); int get_species_index_by_abbrev(const char *abbrev); int get_species_index_by_name(const char *name); // from player.cc -std::string species_name( species_type speci, int level, bool genus = false, - bool adj = false ); -int str_to_species(const std::string &species); +std::string species_name(species_type speci, int level, bool genus = false, + bool adj = false); +species_type str_to_species(const std::string &species); #endif -- cgit v1.2.3-54-g00ecf