From 2b246a2f1016744c425f9cc9009cfd6b780619b3 Mon Sep 17 00:00:00 2001 From: haranp Date: Thu, 25 Oct 2007 14:49:06 +0000 Subject: Type safety and code cleanup. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@2569 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/debug.cc | 7 +- crawl-ref/source/describe.cc | 11 +- crawl-ref/source/hiscores.cc | 9 +- crawl-ref/source/newgame.cc | 13 ++- crawl-ref/source/player.cc | 260 +++++++++++-------------------------------- crawl-ref/source/player.h | 10 +- crawl-ref/source/skills2.cc | 2 +- 7 files changed, 95 insertions(+), 217 deletions(-) diff --git a/crawl-ref/source/debug.cc b/crawl-ref/source/debug.cc index 22e8303bd6..7b2c61cd97 100644 --- a/crawl-ref/source/debug.cc +++ b/crawl-ref/source/debug.cc @@ -248,8 +248,9 @@ void debug_change_species( void ) for (i = SP_HUMAN; i < NUM_SPECIES; i++) { + const species_type si = static_cast(i); const std::string sp_name = - lowercase_string(species_name(i, you.experience_level)); + lowercase_string(species_name(si, you.experience_level)); std::string::size_type pos = sp_name.find(specs); if (pos != std::string::npos) @@ -257,11 +258,11 @@ void debug_change_species( void ) if (pos == 0 && *specs) { // we prefer prefixes over partial matches - sp = static_cast(i); + sp = si; break; } else - sp = static_cast(i); + sp = si; } } diff --git a/crawl-ref/source/describe.cc b/crawl-ref/source/describe.cc index d5bc3b0efa..cab0fb3c99 100644 --- a/crawl-ref/source/describe.cc +++ b/crawl-ref/source/describe.cc @@ -3835,13 +3835,16 @@ std::string ghost_description(const monsters &mons, bool concise) const ghost_demon &ghost = *(mons.ghost); + const species_type gspecies = + static_cast(ghost.values[GVAL_SPECIES]); + // We're fudging stats so that unarmed combat gets based off // of the ghost's species, not the player's stats... exact // stats aren't required anyways, all that matters is whether // dex >= str. -- bwr const int dex = 10; int str; - switch (ghost.values[GVAL_SPECIES]) + switch (gspecies) { case SP_MOUNTAIN_DWARF: case SP_TROLL: @@ -3870,7 +3873,7 @@ std::string ghost_description(const monsters &mons, bool concise) gstr << ghost.name << " the " << skill_title( ghost.values[GVAL_BEST_SKILL], ghost.values[GVAL_SKILL_LEVEL], - ghost.values[GVAL_SPECIES], + gspecies, str, dex, GOD_NO_GOD ) << ", a" << ((ghost.values[GVAL_EXP_LEVEL] < 4) ? " weakling" : @@ -3883,10 +3886,10 @@ std::string ghost_description(const monsters &mons, bool concise) : " legendary") << " "; if ( concise ) - gstr << get_species_abbrev(ghost.values[GVAL_SPECIES]) + gstr << get_species_abbrev(gspecies) << get_class_abbrev(ghost.values[GVAL_CLASS]); else - gstr << species_name(ghost.values[GVAL_SPECIES], + gstr << species_name(gspecies, ghost.values[GVAL_EXP_LEVEL]) << " " << get_class_name(ghost.values[GVAL_CLASS]); diff --git a/crawl-ref/source/hiscores.cc b/crawl-ref/source/hiscores.cc index c75b22b62d..ee9cc0a3e6 100644 --- a/crawl-ref/source/hiscores.cc +++ b/crawl-ref/source/hiscores.cc @@ -659,7 +659,9 @@ void scorefile_entry::set_base_xlog_fields() const fields->add_field("lv", SCORE_VERSION); fields->add_field("name", "%s", name.c_str()); fields->add_field("uid", "%d", uid); - fields->add_field("race", "%s", species_name(race, lvl).c_str()); + fields->add_field("race", "%s", + species_name(static_cast(race), + lvl).c_str()); fields->add_field("cls", "%s", get_class_name(cls)); fields->add_field("char", "%s%s", get_species_abbrev(race), @@ -1224,7 +1226,8 @@ scorefile_entry::character_description(death_desc_verbosity verbosity) const else { snprintf( buf, HIGHSCORE_SIZE, "%8ld %s the %s %s (level %d", - points, name.c_str(), species_name(race, lvl).c_str(), + points, name.c_str(), + species_name(static_cast(race), lvl).c_str(), get_class_name(cls), lvl ); desc = buf; } @@ -1248,7 +1251,7 @@ scorefile_entry::character_description(death_desc_verbosity verbosity) const if (verbose) { - std::string srace = species_name( race, lvl ); + std::string srace = species_name(static_cast(race), lvl); snprintf( scratch, INFO_SIZE, "Began as a%s %s %s", is_vowel(srace[0]) ? "n" : "", srace.c_str(), diff --git a/crawl-ref/source/newgame.cc b/crawl-ref/source/newgame.cc index 8ab7aabc7f..ac2f204805 100644 --- a/crawl-ref/source/newgame.cc +++ b/crawl-ref/source/newgame.cc @@ -191,8 +191,8 @@ static bool prev_startup_options_set(void) static std::string get_opt_race_name(char race) { - int prace = letter_to_species(race); - return prace && prace != SP_UNKNOWN? species_name(prace, 1) : "Random"; + species_type prace = letter_to_species(race); + return prace != SP_UNKNOWN? species_name(prace, 1) : "Random"; } static std::string get_opt_class_name(char oclass) @@ -3069,18 +3069,19 @@ spec_query: *linebuf = 0; for (int i = SP_HUMAN; i < NUM_SPECIES; ++i) { - if (!is_species_valid_choice(static_cast(i))) + const species_type si = static_cast(i); + if (!is_species_valid_choice(si)) continue; if (you.char_class != JOB_UNKNOWN && - !class_allowed(static_cast(i), you.char_class)) + !class_allowed(si, you.char_class)) continue; char buf[100]; - char sletter = species_to_letter(i); + char sletter = species_to_letter(si); snprintf(buf, sizeof buf, "%c - %-26s", sletter, - species_name(i, 1).c_str()); + species_name(si, 1).c_str()); if (sletter == Options.prev_race) prevraceok = true; strncat(linebuf, buf, sizeof linebuf); diff --git a/crawl-ref/source/player.cc b/crawl-ref/source/player.cc index 5078a446a0..9078ed275c 100644 --- a/crawl-ref/source/player.cc +++ b/crawl-ref/source/player.cc @@ -92,63 +92,10 @@ std::string pronoun_you(description_level_type desc) } } -////////////////////////////////////////////////////////////////////////// -/* - you.duration []: //jmf: obsolete, see enum.h instead - //[ds] Well, can we lose it yet? - 0 - liquid flames - 1 - icy armour - 2 - repel missiles - 3 - prayer - 4 - regeneration - 5 - vorpal blade - 6 - fire brand - 7 - ice brand - 8 - lethal infusion - 9 - swiftness - 10 - insulation - 11 - stonemail - 12 - controlled flight - 13 - teleport - 14 - control teleport - 15 - poison weapon - 16 - resist poison - 17 - breathe something - 18 - transformation (duration) - 19 - death channel - 20 - deflect missiles - */ - -/* attributes - 0 - resist lightning - 1 - spec_air - 2 - spec_earth - 3 - control teleport - 4 - walk slowly (eg naga) - 5 - transformation (form) - 6 - Nemelex card gift countdown - 7 - Nemelex has given you a card table - 8 - How many demonic powers a dspawn has - */ - -/* armour list - 0 - wielded - 1 - cloak - 2 - helmet - 3 - gloves - 4 - boots - 5 - shield - 6 - body armour - 7 - ring 0 - 8 - ring 1 - 9 - amulet - */ - /* Contains functions which return various player state vars, and other stuff related to the player. */ -int species_exp_mod(char species); -void ability_increase(void); +static void ability_increase(); // Use this function whenever the player enters (or lands and thus re-enters) // a grid. @@ -420,7 +367,7 @@ bool player_under_penance(void) return (false); } -bool player_genus(unsigned char which_genus, unsigned char species) +bool player_genus(genus_type which_genus, species_type species) { if (species == SP_UNKNOWN) species = you.species; @@ -3338,7 +3285,7 @@ int check_stealth(void) return (stealth); } // end check_stealth() -void ability_increase(void) +static void ability_increase() { mpr("Your experience leads to an increase in your attributes!", MSGCH_INTRINSIC_GAIN); @@ -3688,72 +3635,51 @@ int str_to_species(const std::string &species) for (int i = SP_HUMAN; i < NUM_SPECIES; ++i) { - if (species == species_name(i, 10)) + if (species == species_name(static_cast(i), 10)) return (i); } for (int i = SP_HUMAN; i < NUM_SPECIES; ++i) { - if (species == species_name(i, 1)) + if (species == species_name(static_cast(i), 1)) return (i); } return (SP_HUMAN); } -// Note that this function only has the one static buffer, so if you -// want to use the results, you'll want to make a copy. -std::string species_name( int speci, int level, bool genus, - bool adj, bool cap ) -// defaults: false false true +std::string species_name(species_type speci, int level, bool genus, bool adj) +// defaults: false false { - static char species_buff[80]; + std::string res; if (player_genus( GENPC_DRACONIAN, speci )) { if (adj || genus) // adj doesn't care about exact species - strcpy( species_buff, "Draconian" ); + res = "Draconian"; else { - // No longer have problems with ghosts here -- Sharp Aug2002 if (level < 7) - strcpy( species_buff, "Draconian" ); + res = "Draconian"; else { switch (speci) { - case SP_RED_DRACONIAN: - strcpy( species_buff, "Red Draconian" ); - break; - case SP_WHITE_DRACONIAN: - strcpy( species_buff, "White Draconian" ); - break; - case SP_GREEN_DRACONIAN: - strcpy( species_buff, "Green Draconian" ); - break; - case SP_GOLDEN_DRACONIAN: - strcpy( species_buff, "Yellow Draconian" ); - break; - case SP_GREY_DRACONIAN: - strcpy( species_buff, "Grey Draconian" ); - break; - case SP_BLACK_DRACONIAN: - strcpy( species_buff, "Black Draconian" ); - break; - case SP_PURPLE_DRACONIAN: - strcpy( species_buff, "Purple Draconian" ); - break; - case SP_MOTTLED_DRACONIAN: - strcpy( species_buff, "Mottled Draconian" ); - break; - case SP_PALE_DRACONIAN: - strcpy( species_buff, "Pale Draconian" ); - break; + case SP_RED_DRACONIAN: res = "Red Draconian"; break; + case SP_WHITE_DRACONIAN: res = "White Draconian"; break; + case SP_GREEN_DRACONIAN: res = "Green Draconian"; break; + case SP_GOLDEN_DRACONIAN: res = "Yellow Draconian"; break; + case SP_GREY_DRACONIAN: res = "Grey Draconian"; break; + case SP_BLACK_DRACONIAN: res = "Black Draconian"; break; + case SP_PURPLE_DRACONIAN: res = "Purple Draconian"; break; + case SP_MOTTLED_DRACONIAN: res = "Mottled Draconian"; break; + case SP_PALE_DRACONIAN: res = "Pale Draconian"; break; + case SP_UNK0_DRACONIAN: case SP_UNK1_DRACONIAN: case SP_BASE_DRACONIAN: default: - strcpy( species_buff, "Draconian" ); + res = "Draconian"; break; } } @@ -3762,47 +3688,33 @@ std::string species_name( int speci, int level, bool genus, else if (player_genus( GENPC_ELVEN, speci )) { if (adj) // doesn't care about species/genus - strcpy( species_buff, "Elven" ); + res = "Elven"; else if (genus) - strcpy( species_buff, "Elf" ); + res = "Elf"; else { switch (speci) { - default: - strcpy( species_buff, "Elf" ); - break; - case SP_HIGH_ELF: - strcpy( species_buff, "High Elf" ); - break; - case SP_GREY_ELF: - strcpy( species_buff, "Grey Elf" ); - break; - case SP_DEEP_ELF: - strcpy( species_buff, "Deep Elf" ); - break; - case SP_SLUDGE_ELF: - strcpy( species_buff, "Sludge Elf" ); - break; + case SP_HIGH_ELF: res = "High Elf"; break; + case SP_GREY_ELF: res = "Grey Elf"; break; + case SP_DEEP_ELF: res = "Deep Elf"; break; + case SP_SLUDGE_ELF: res = "Sludge Elf"; break; + default: res = "Elf"; break; } } } else if (player_genus( GENPC_DWARVEN, speci )) { if (adj) // doesn't care about species/genus - strcpy( species_buff, "Dwarven" ); + res = "Dwarven"; else if (genus) - strcpy( species_buff, "Dwarf" ); + res = "Dwarf"; else { switch (speci) { - case SP_MOUNTAIN_DWARF: - strcpy( species_buff, "Mountain Dwarf" ); - break; - default: - strcpy( species_buff, "Dwarf" ); - break; + case SP_MOUNTAIN_DWARF: res = "Mountain Dwarf"; break; + default: res = "Dwarf"; break; } } } @@ -3810,78 +3722,37 @@ std::string species_name( int speci, int level, bool genus, { switch (speci) { - case SP_HUMAN: - strcpy( species_buff, "Human" ); - break; - case SP_HALFLING: - strcpy( species_buff, "Halfling" ); - break; + case SP_HUMAN: res = "Human"; break; + case SP_HALFLING: res = "Halfling"; break; + case SP_KOBOLD: res = "Kobold"; break; + case SP_MUMMY: res = "Mummy"; break; + case SP_NAGA: res = "Naga"; break; + // We've previously declared that these are radically + // different from Ogres... so we're not going to + // refer to them as Ogres. -- bwr + case SP_OGRE_MAGE: res = "Ogre-Mage"; break; + case SP_CENTAUR: res = "Centaur"; break; + case SP_SPRIGGAN: res = "Spriggan"; break; + case SP_MINOTAUR: res = "Minotaur"; break; + case SP_KENKU: res = "Kenku"; break; + case SP_VAMPIRE: res = "Vampire"; break; + case SP_HILL_ORC: - strcpy( species_buff, (adj) ? "Orcish" : (genus) ? "Orc" - : "Hill Orc" ); - break; - case SP_KOBOLD: - strcpy( species_buff, "Kobold" ); - break; - case SP_MUMMY: - strcpy( species_buff, "Mummy" ); - break; - case SP_NAGA: - strcpy( species_buff, "Naga" ); - break; - case SP_GNOME: - strcpy( species_buff, (adj) ? "Gnomish" : "Gnome" ); - break; - case SP_OGRE: - strcpy( species_buff, (adj) ? "Ogreish" : "Ogre" ); - break; - case SP_TROLL: - strcpy( species_buff, (adj) ? "Trollish" : "Troll" ); - break; - case SP_OGRE_MAGE: - // We've previously declared that these are radically - // different from Ogres... so we're not going to - // refer to them as Ogres. -- bwr - strcpy( species_buff, "Ogre-Mage" ); - break; - case SP_CENTAUR: - strcpy( species_buff, "Centaur" ); - break; - case SP_DEMIGOD: - strcpy( species_buff, (adj) ? "Divine" : "Demigod" ); - break; - case SP_SPRIGGAN: - strcpy( species_buff, "Spriggan" ); - break; - case SP_MINOTAUR: - strcpy( species_buff, "Minotaur" ); - break; - case SP_DEMONSPAWN: - strcpy( species_buff, (adj) ? "Demonic" : "Demonspawn" ); - break; - case SP_GHOUL: - strcpy( species_buff, (adj) ? "Ghoulish" : "Ghoul" ); - break; - case SP_KENKU: - strcpy( species_buff, "Kenku" ); - break; - case SP_MERFOLK: - strcpy( species_buff, (adj) ? "Merfolkian" : "Merfolk" ); - break; - case SP_VAMPIRE: - strcpy( species_buff, "Vampire" ); - break; - default: - strcpy( species_buff, (adj) ? "Yakish" : "Yak" ); + res = (adj ? "Orcish" : genus ? "Orc" : "Hill Orc"); break; + + case SP_GNOME: res = (adj ? "Gnomish" : "Gnome"); break; + case SP_OGRE: res = (adj ? "Ogreish" : "Ogre"); break; + case SP_TROLL: res = (adj ? "Trollish" : "Troll"); break; + case SP_DEMIGOD: res = (adj ? "Divine" : "Demigod"); break; + case SP_DEMONSPAWN: res = (adj ? "Demonic" : "Demonspawn" ); break; + case SP_GHOUL: res = (adj ? "Ghoulish" : "Ghoul"); break; + case SP_MERFOLK: res = (adj ? "Merfolkian" : "Merfolk"); break; + default: res = (adj ? "Yakish" : "Yak"); break; } } - - if (!cap) - strlwr( species_buff ); - - return (species_buff); -} // end species_name() + return res; +} bool player_res_corrosion(bool calc_unid) { @@ -3945,12 +3816,11 @@ bool player_has_spell( int spell ) return you.has_spell(spell); } -int species_exp_mod(char species) +static int species_exp_mod(species_type species) { - - if (player_genus(GENPC_DRACONIAN)) + if (player_genus(GENPC_DRACONIAN, species)) return 14; - else if (player_genus(GENPC_DWARVEN)) + else if (player_genus(GENPC_DWARVEN, species)) return 13; { switch (species) @@ -4273,7 +4143,7 @@ void modify_stat(stat_type which_stat, char amount, bool suppress_msg, } // end modify_stat() void modify_stat(stat_type which_stat, char amount, bool suppress_msg, - const std::string cause, bool see_source) + const std::string& cause, bool see_source) { modify_stat(which_stat, amount, suppress_msg, cause.c_str(), see_source); } @@ -4635,8 +4505,8 @@ int get_species_index_by_name( const char *name ) for (i = SP_HUMAN; i < NUM_SPECIES; i++) { - std::string lowered_species = - lowercase_string( species_name( i, 0, false, false, false ) ); + const std::string lowered_species = + lowercase_string(species_name(static_cast(i),0)); pos = lowered_species.find( lowered_buff ); if (pos != std::string::npos) { diff --git a/crawl-ref/source/player.h b/crawl-ref/source/player.h index dd12489769..51fbb3da85 100644 --- a/crawl-ref/source/player.h +++ b/crawl-ref/source/player.h @@ -79,8 +79,8 @@ bool wearing_amulet(char which_am, bool calc_unid = true); /* *********************************************************************** * called from: acr - chardump - describe - newgame - view * *********************************************************************** */ -std::string species_name( int speci, int level, bool genus = false, - bool adj = false, bool cap = true ); +std::string species_name( species_type speci, int level, + bool genus = false, bool adj = false); int str_to_species(const std::string &species); /* *********************************************************************** @@ -298,7 +298,7 @@ void gain_exp(unsigned int exp_gained, unsigned int* actual_gain = NULL, * misc - stuff * *********************************************************************** */ void modify_stat(stat_type which_stat, char amount, bool suppress_msg, - const std::string cause, bool see_source = true); + const std::string& cause, bool see_source = true); void modify_stat(stat_type which_stat, char amount, bool suppress_msg, const char* cause, bool see_source = true); void modify_stat(stat_type which_stat, char amount, bool suppress_msg, @@ -324,8 +324,8 @@ void redraw_skill(const std::string &your_name, const std::string &class_name); * called from: ability - fight - item_use - mutation - newgame - spells0 - * transfor * *********************************************************************** */ -bool player_genus( unsigned char which_genus, - unsigned char species = SP_UNKNOWN ); +bool player_genus( genus_type which_genus, + species_type species = SP_UNKNOWN ); bool you_can_wear( int eq, bool special_armour = false ); bool you_tran_can_wear( int eq, bool check_mutation = false ); diff --git a/crawl-ref/source/skills2.cc b/crawl-ref/source/skills2.cc index 9e968a45d7..bb3ea4b4d2 100644 --- a/crawl-ref/source/skills2.cc +++ b/crawl-ref/source/skills2.cc @@ -2066,7 +2066,7 @@ std::string skill_title( unsigned char best_skill, unsigned char skill_lev, { // need species name snprintf( title_buff, sizeof(title_buff), tempstr, - species_name(species, 0, true, + species_name(static_cast(species), 0, true, (ptr == tempstr && best_skill != SK_NECROMANCY)).c_str() ); // The above code only capitalises start-of-string racenames -- cgit v1.2.3-54-g00ecf