diff options
author | Bardur Arantsson <bardur@scientician.net> | 2017-05-02 19:20:57 +0200 |
---|---|---|
committer | Bardur Arantsson <bardur@scientician.net> | 2017-05-02 19:20:57 +0200 |
commit | a40a0d1aeddb4742e486f601cbcc7e9ddcc06e2d (patch) | |
tree | da5e8653050f47fc68f14d0062c11273774688e5 /src | |
parent | c2ff640e5540fd4c55dd054ae87dd510a11155e9 (diff) |
Move player_{name,base} to Game
Diffstat (limited to 'src')
-rw-r--r-- | src/birth.cc | 134 | ||||
-rw-r--r-- | src/cmd4.cc | 74 | ||||
-rw-r--r-- | src/dungeon.cc | 5 | ||||
-rw-r--r-- | src/files.cc | 142 | ||||
-rw-r--r-- | src/game.hpp | 10 | ||||
-rw-r--r-- | src/init1.cc | 2 | ||||
-rw-r--r-- | src/loadsave.cc | 6 | ||||
-rw-r--r-- | src/main.cc | 16 | ||||
-rw-r--r-- | src/notes.cc | 10 | ||||
-rw-r--r-- | src/notes.hpp | 2 | ||||
-rw-r--r-- | src/q_library.cc | 3 | ||||
-rw-r--r-- | src/randart.cc | 2 | ||||
-rw-r--r-- | src/squeltch.cc | 8 | ||||
-rw-r--r-- | src/util.cc | 39 | ||||
-rw-r--r-- | src/util.hpp | 5 | ||||
-rw-r--r-- | src/variable.cc | 10 | ||||
-rw-r--r-- | src/variable.hpp | 2 |
17 files changed, 235 insertions, 235 deletions
diff --git a/src/birth.cc b/src/birth.cc index 75d533ab..3b8c9fda 100644 --- a/src/birth.cc +++ b/src/birth.cc @@ -103,73 +103,48 @@ static s32b auto_round; */ static s32b last_round; -/* Human */ -static const char *human_syllable1[] = -{ - "Ab", "Ac", "Ad", "Af", "Agr", "Ast", "As", "Al", "Adw", "Adr", "Ar", - "B", "Br", "C", "Cr", "Ch", "Cad", "D", "Dr", "Dw", "Ed", "Eth", "Et", - "Er", "El", "Eow", "F", "Fr", "G", "Gr", "Gw", "Gal", "Gl", "H", "Ha", - "Ib", "Jer", "K", "Ka", "Ked", "L", "Loth", "Lar", "Leg", "M", "Mir", - "N", "Nyd", "Ol", "Oc", "On", "P", "Pr", "R", "Rh", "S", "Sev", "T", - "Tr", "Th", "V", "Y", "Z", "W", "Wic", -}; - -static const char *human_syllable2[] = -{ - "a", "ae", "au", "ao", "are", "ale", "ali", "ay", "ardo", "e", "ei", - "ea", "eri", "era", "ela", "eli", "enda", "erra", "i", "ia", "ie", - "ire", "ira", "ila", "ili", "ira", "igo", "o", "oa", "oi", "oe", - "ore", "u", "y", -}; - -static const char *human_syllable3[] = -{ - "a", "and", "b", "bwyn", "baen", "bard", "c", "ctred", "cred", "ch", - "can", "d", "dan", "don", "der", "dric", "dfrid", "dus", "f", "g", - "gord", "gan", "l", "li", "lgrin", "lin", "lith", "lath", "loth", - "ld", "ldric", "ldan", "m", "mas", "mos", "mar", "mond", "n", - "nydd", "nidd", "nnon", "nwan", "nyth", "nad", "nn", "nnor", "nd", - "p", "r", "ron", "rd", "s", "sh", "seth", "sean", "t", "th", "tha", - "tlan", "trem", "tram", "v", "vudd", "w", "wan", "win", "wyn", "wyr", - "wyr", "wyth", -}; - /* * Random Name Generator * based on a Javascript by Michael Hensley * "http://geocities.com/timessquare/castle/6274/" */ -static void create_random_name(int race, char *name) +static std::string create_random_name() { - const char *syl1, *syl2, *syl3; - - int idx; - - - /* Paranoia */ - if (!name) return; - - /* Select the monster type */ - switch (race) - { - /* Create the monster name */ - - /* Use human ones */ - default: - { - idx = rand_int(sizeof(human_syllable1) / sizeof(char *)); - syl1 = human_syllable1[idx]; - idx = rand_int(sizeof(human_syllable2) / sizeof(char *)); - syl2 = human_syllable2[idx]; - idx = rand_int(sizeof(human_syllable3) / sizeof(char *)); - syl3 = human_syllable3[idx]; - - break; - } - } - - /* Concatenate selected syllables */ - strnfmt(name, 32, "%s%s%s", syl1, syl2, syl3); + static const std::vector<std::string> human_syllable1 + { + "Ab", "Ac", "Ad", "Af", "Agr", "Ast", "As", "Al", "Adw", "Adr", "Ar", + "B", "Br", "C", "Cr", "Ch", "Cad", "D", "Dr", "Dw", "Ed", "Eth", "Et", + "Er", "El", "Eow", "F", "Fr", "G", "Gr", "Gw", "Gal", "Gl", "H", "Ha", + "Ib", "Jer", "K", "Ka", "Ked", "L", "Loth", "Lar", "Leg", "M", "Mir", + "N", "Nyd", "Ol", "Oc", "On", "P", "Pr", "R", "Rh", "S", "Sev", "T", + "Tr", "Th", "V", "Y", "Z", "W", "Wic", + }; + + static const std::vector<std::string> human_syllable2 + { + "a", "ae", "au", "ao", "are", "ale", "ali", "ay", "ardo", "e", "ei", + "ea", "eri", "era", "ela", "eli", "enda", "erra", "i", "ia", "ie", + "ire", "ira", "ila", "ili", "ira", "igo", "o", "oa", "oi", "oe", + "ore", "u", "y", + }; + + static const std::vector<std::string> human_syllable3 + { + "a", "and", "b", "bwyn", "baen", "bard", "c", "ctred", "cred", "ch", + "can", "d", "dan", "don", "der", "dric", "dfrid", "dus", "f", "g", + "gord", "gan", "l", "li", "lgrin", "lin", "lith", "lath", "loth", + "ld", "ldric", "ldan", "m", "mas", "mos", "mar", "mond", "n", + "nydd", "nidd", "nnon", "nwan", "nyth", "nad", "nn", "nnor", "nd", + "p", "r", "ron", "rd", "s", "sh", "seth", "sean", "t", "th", "tha", + "tlan", "trem", "tram", "v", "vudd", "w", "wan", "win", "wyn", "wyr", + "wyr", "wyth", + }; + + auto const &syl1 = *uniform_element(human_syllable1); + auto const &syl2 = *uniform_element(human_syllable2); + auto const &syl3 = *uniform_element(human_syllable3); + + return syl1 + syl2 + syl3; } @@ -1488,7 +1463,7 @@ static bool_ player_birth_aux_ask() /* Title everything */ put_str("Name :", NAME_ROW, 1); - c_put_str(TERM_L_BLUE, player_name, NAME_ROW, 9); + c_put_str(TERM_L_BLUE, game->player_name.c_str(), NAME_ROW, 9); put_str("Race :", RACE_ROW, 1); @@ -1631,10 +1606,13 @@ static bool_ player_birth_aux_ask() c_put_str(TERM_L_BLUE, rp_ptr->title.c_str(), RACE_ROW, 9); /* Get a random name */ - if (!do_quick_start) create_random_name(p_ptr->prace, player_name); + if (!do_quick_start) + { + game->player_name = create_random_name(); + } /* Display */ - c_put_str(TERM_L_BLUE, player_name, NAME_ROW, 9); + c_put_str(TERM_L_BLUE, game->player_name.c_str(), NAME_ROW, 9); /* Clean up */ clear_from(12); @@ -2539,7 +2517,7 @@ static bool_ player_birth_aux_auto() Term_clear(); put_str("Name :", 2, 1); - c_put_str(TERM_L_BLUE, player_name, 2, 9); + c_put_str(TERM_L_BLUE, game->player_name.c_str(), 2, 9); put_str("Race :", 3, 1); auto const player_race_name = get_player_race_name(p_ptr->prace, p_ptr->pracem); @@ -3195,7 +3173,7 @@ int load_savefile_names() FILE *fff; char buf[1024]; char tmp[50]; - char player_base_save[32]; + std::string player_base_save; int max = 0, fd; @@ -3211,7 +3189,7 @@ int load_savefile_names() /* Save the current 'player_base' */ - strncpy(player_base_save, player_base, 32); + player_base_save = game->player_base; /* @@ -3270,7 +3248,7 @@ int load_savefile_names() strcpy(savefile_desc[max], buf + i); /* Build platform-dependent savefile name */ - strncpy(player_base, savefile_names[max], 32); + game->player_base = savefile_names[max]; process_player_name(TRUE); /* Try to open the savefile */ @@ -3287,7 +3265,7 @@ int load_savefile_names() my_fclose(fff); /* Restore the values of 'player_base' and 'savefile' */ - strncpy(player_base, player_base_save, 32); + game->player_base = player_base_save; process_player_name(TRUE); return (max); @@ -3321,14 +3299,14 @@ void save_savefile_names() */ auto const player_race_name = get_player_race_name(p_ptr->prace, p_ptr->pracem); fprintf(fff, "%s@%c%s@%s, the %s %s is %s\n", game_module, - (death) ? '0' : '1', player_base, player_name, + (death) ? '0' : '1', game->player_base.c_str(), game->player_name.c_str(), player_race_name.c_str(), spp_ptr->title, (!death) ? "alive" : "dead"); for (i = 0; i < max; i++) { - if (!strcmp(savefile_names[i], player_base)) continue; + if (!strcmp(savefile_names[i], game->player_base.c_str())) continue; fprintf(fff, "%s@%c%s@%s\n", savefile_module[i], (savefile_alive[i]) ? '1' : '0', savefile_names[i], savefile_desc[i]); } @@ -3459,22 +3437,20 @@ savefile_try_again: } else if (((k == 0x7F) || (k == '\010')) && (sel >= 2)) { - char player_base_save[32]; - if (!get_check(format("Really delete '%s'?", savefile_names[savefile_idx[sel - 2]]))) continue; /* Save current 'player_base' */ - strncpy(player_base_save, player_base, 32); + std::string player_base_save = game->player_base; /* Build platform-dependent save file name */ - strncpy(player_base, savefile_names[savefile_idx[sel - 2]], 32); + game->player_base = savefile_names[savefile_idx[sel - 2]]; process_player_name(TRUE); /* Remove the savefile */ fd_kill(savefile); /* Restore 'player_base' and 'savefile' */ - strncpy(player_base, player_base_save, 32); + game->player_base = player_base_save; process_player_name(TRUE); /* Reload, gods I hate using goto .. */ @@ -3489,7 +3465,7 @@ savefile_try_again: prt("Enter the name of the savefile that will hold this character: ", 23, 0); /* Ask the user for a string */ - if (!askfor_aux(player_base, 15)) continue; + if (!askfor_aux(&game->player_base, 15)) continue; /* Process the player name */ process_player_name(TRUE); @@ -3502,7 +3478,7 @@ savefile_try_again: prt("Enter the name of a savefile: ", 23, 0); /* Ask the user for a string */ - if (!askfor_aux(player_base, 15)) continue; + if (!askfor_aux(&game->player_base, 15)) continue; /* Process the player name */ process_player_name(TRUE); @@ -3518,7 +3494,7 @@ savefile_try_again: if ((x < 2) || (x >= max)) continue; - strnfmt(player_base, 32, "%s", savefile_names[savefile_idx[x - 2]]); + game->player_base = savefile_names[savefile_idx[x - 2]]; /* Process the player name */ process_player_name(TRUE); diff --git a/src/cmd4.cc b/src/cmd4.cc index 2dcb0beb..afb84def 100644 --- a/src/cmd4.cc +++ b/src/cmd4.cc @@ -180,7 +180,7 @@ void do_cmd_change_name(void) /* File dump */ else if (c == 'f') { - strnfmt(tmp, 160, "%s.txt", player_name); + strnfmt(tmp, 160, "%s.txt", game->player_name.c_str()); if (get_string("Filename(you can post it to http://angband.oook.cz/): ", tmp, 80)) { if (tmp[0] && (tmp[0] != ' ')) @@ -1094,9 +1094,6 @@ static errr option_dump(cptr fname) */ static void do_cmd_pref_file_hack(int row) { - char ftmp[80]; - - /* Prompt */ prt("Command: Load a user pref file", row, 0); @@ -1104,21 +1101,24 @@ static void do_cmd_pref_file_hack(int row) prt("File: ", row + 2, 0); /* Default filename */ - strnfmt(ftmp, 80, "%s.prf", player_base); + std::string ftmp = fmt::format("{}.prf", game->player_base); /* Ask for a file (or cancel) */ - if (!askfor_aux(ftmp, 80)) return; + if (!askfor_aux(&ftmp, 80)) + { + return; + } /* Process the given filename */ - if (process_pref_file(ftmp)) + if (process_pref_file(ftmp.c_str())) { /* Mention failure */ - msg_format("Failed to load '%s'!", ftmp); + msg_format("Failed to load '%s'!", ftmp.c_str()); } else { /* Mention success */ - msg_format("Loaded '%s'.", ftmp); + msg_format("Loaded '%s'.", ftmp.c_str()); } } @@ -1199,8 +1199,6 @@ void do_cmd_options(void) case 'u': case 'U': { - char ftmp[80]; - /* Prompt */ prt("Command: Append options to a file", 21, 0); @@ -1208,13 +1206,13 @@ void do_cmd_options(void) prt("File: ", 21, 0); /* Default filename */ - strnfmt(ftmp, 80, "%s.prf", player_base); + auto ftmp = fmt::format("{}.prf", game->player_base); /* Ask for a file */ - if (!askfor_aux(ftmp, 80)) continue; + if (!askfor_aux(&ftmp, 80)) continue; /* Dump the options */ - if (option_dump(ftmp)) + if (option_dump(ftmp.c_str())) { /* Failure */ msg_print("Failed!"); @@ -1650,17 +1648,8 @@ static errr keymap_dump(cptr fname) */ void do_cmd_macros(void) { - int i; - - char tmp[1024]; - - char buf[1024]; - - int mode; - - /* Keymap mode */ - mode = get_keymap_mode(); + int mode = get_keymap_mode(); /* Enter "icky" mode */ @@ -1673,6 +1662,8 @@ void do_cmd_macros(void) /* Process requests until done */ while (1) { + char buf[1024]; + /* Clear screen */ Term_clear(); @@ -1706,7 +1697,7 @@ void do_cmd_macros(void) prt("Command: ", 16, 0); /* Get a command */ - i = inkey(); + int i = inkey(); /* Leave */ if (i == ESCAPE) break; @@ -1721,13 +1712,16 @@ void do_cmd_macros(void) prt("File: ", 18, 0); /* Default filename */ - strnfmt(tmp, 1024, "%s.prf", player_name); + auto tmp = fmt::format("{}.prf", game->player_name); /* Ask for a file */ - if (!askfor_aux(tmp, 80)) continue; + if (!askfor_aux(&tmp, 80)) + { + continue; + } /* Process the given filename */ - if (0 != process_pref_file(tmp)) + if (0 != process_pref_file(tmp.c_str())) { /* Prompt */ msg_print("Could not load file!"); @@ -1744,13 +1738,16 @@ void do_cmd_macros(void) prt("File: ", 18, 0); /* Default filename */ - strnfmt(tmp, 1024, "%s.prf", player_name); + auto tmp = fmt::format("{}.prf", game->player_name); /* Ask for a file */ - if (!askfor_aux(tmp, 80)) continue; + if (!askfor_aux(&tmp, 80)) + { + continue; + } /* Dump the macros */ - (void)macro_dump(tmp); + macro_dump(tmp.c_str()); /* Prompt */ msg_print("Appended macros."); @@ -1800,6 +1797,8 @@ void do_cmd_macros(void) /* Create a macro */ else if (i == '4') { + char tmp[1024]; + /* Prompt */ prt("Command: Create a macro", 16, 0); @@ -1861,13 +1860,16 @@ void do_cmd_macros(void) prt("File: ", 18, 0); /* Default filename */ - strnfmt(tmp, 1024, "%s.prf", player_name); + auto tmp = fmt::format("{}.prf", game->player_name); /* Ask for a file */ - if (!askfor_aux(tmp, 80)) continue; + if (!askfor_aux(&tmp, 80)) + { + continue; + } /* Dump the macros */ - (void)keymap_dump(tmp); + keymap_dump(tmp.c_str()); /* Prompt */ msg_print("Appended keymaps."); @@ -1917,6 +1919,8 @@ void do_cmd_macros(void) /* Create a keymap */ else if (i == '8') { + char tmp[1024]; + /* Prompt */ prt("Command: Create a keymap", 16, 0); @@ -1973,6 +1977,8 @@ void do_cmd_macros(void) /* Enter a new action */ else if (i == '0') { + char tmp[1024]; + /* Prompt */ prt("Command: Enter a new action", 16, 0); diff --git a/src/dungeon.cc b/src/dungeon.cc index c1170c93..ce42ecf4 100644 --- a/src/dungeon.cc +++ b/src/dungeon.cc @@ -4983,6 +4983,7 @@ static void load_all_pref_files(void) { char buf[1024]; + std::string const &player_name = game->player_name; /* Access the "race" pref file */ sprintf(buf, "%s.prf", rp_ptr->title.c_str()); @@ -4997,7 +4998,7 @@ static void load_all_pref_files(void) process_pref_file(buf); /* Access the "character" pref file */ - sprintf(buf, "%s.prf", player_name); + sprintf(buf, "%s.prf", player_name.c_str()); /* Process that file */ process_pref_file(buf); @@ -5008,7 +5009,7 @@ static void load_all_pref_files(void) * the providence of rules and such to avoid the same * duplication problems as caused when saving macros/keymaps. */ boost::filesystem::path userDirectory(ANGBAND_DIR_USER); - if (automatizer_load(userDirectory / (std::string(player_name) + ".atm"))) + if (automatizer_load(userDirectory / (player_name + ".atm"))) { // Done } diff --git a/src/files.cc b/src/files.cc index 3b9bc875..c460fc5f 100644 --- a/src/files.cc +++ b/src/files.cc @@ -936,7 +936,7 @@ static cptr process_pref_file_expr(char **sp, char *fp) /* Player */ else if (streq(b + 1, "PLAYER")) { - v = player_base; + v = game->player_base.c_str(); // The string SHOULD be stable enough for this } } @@ -2236,7 +2236,7 @@ void display_player(int mode) /* Name, Sex, Race, Class */ put_str("Name :", 2, 1); - c_put_str(TERM_L_BLUE, player_name, 2, 9); + c_put_str(TERM_L_BLUE, game->player_name.c_str(), 2, 9); put_str("Race :", 3, 1); auto const player_race_name = get_player_race_name(p_ptr->prace, p_ptr->pracem); @@ -3757,65 +3757,53 @@ void do_cmd_help(void) -/* - * Process the player name. - * Extract a clean "base name". - * Build the savefile name if needed. - */ void process_player_base() { - char temp[128]; - - /* Rename the savefile, using the player_base */ - (void)sprintf(temp, "%s", player_base); - - /* Build the filename */ - path_build(savefile, 1024, ANGBAND_DIR_SAVE, temp); + path_build(savefile, 1024, ANGBAND_DIR_SAVE, game->player_base.c_str()); } void process_player_name(bool_ sf) { - int i, k = 0; - char tmp[50]; - /* Cannot be too long */ - if (strlen(player_base) > 15) + if (game->player_base.size() > 15) { - /* Name too long */ - quit_fmt("The name '%s' is too long!", player_base); + quit_fmt("The name '%s' is too long!", game->player_base.c_str()); } - /* Cannot contain "icky" characters */ - for (i = 0; player_base[i]; i++) + /* Cannot contain control characters */ + for (auto c : game->player_base) { - /* No control characters */ - if (iscntrl(player_base[i])) + if (iscntrl(c)) { - /* Illegal characters */ - quit_fmt("The name '%s' contains control chars!", player_base); + quit_fmt("The name '%s' contains control chars!", game->player_base.c_str()); } } /* Extract "useful" letters */ - for (i = 0; player_base[i]; i++) + std::string buf; + for (auto c : game->player_base) { - char c = player_base[i]; - /* Accept some letters */ - if (isalpha(c) || isdigit(c)) tmp[k++] = c; + if (isalpha(c) || isdigit(c)) + { + buf += c; + } /* Convert space, dot, and underscore to underscore */ - else if (strchr("@. _", c)) tmp[k++] = '_'; + else if (strchr("@. _", c)) + { + buf += '_'; + } } - /* Terminate */ - tmp[k] = '\0'; - sprintf(player_base, "%s", tmp); + game->player_base = buf; /* Require a "base" name */ - if (!player_base[0]) strcpy(player_base, "PLAYER"); - + if (game->player_base.empty()) + { + game->player_base = "PLAYER"; + } /* Change the savefile name */ if (sf) @@ -3852,10 +3840,13 @@ void get_name(void) move_cursor(2, 9); /* Save the player name */ - strcpy(tmp, player_name); + strcpy(tmp, game->player_name.c_str()); /* Get an input, ignore "Escape" */ - if (askfor_aux(tmp, 31)) strcpy(player_name, tmp); + if (askfor_aux(tmp, 31)) + { + game->player_name = tmp; + } /* Process the player name */ process_player_name(FALSE); @@ -3865,7 +3856,7 @@ void get_name(void) } /* Pad the name (to clear junk) */ - sprintf(tmp, "%-31.31s", player_name); + sprintf(tmp, "%-31.31s", game->player_name.c_str()); /* Re-Draw the name (in light blue) */ c_put_str(TERM_L_BLUE, tmp, 2, 9); @@ -4208,7 +4199,7 @@ static void print_tomb(void) p = cp_ptr->titles[(p_ptr->lev - 1) / 5]; } - center_string(buf, player_name); + center_string(buf, game->player_name.c_str()); put_str(buf, 6, 11); center_string(buf, "the"); @@ -4596,7 +4587,7 @@ void show_highclass(int building) int i = 0, j, m = 0; int pr, pc, clev; high_score the_score; - char buf[1024], out_val[256]; + char buf[1024]; int highscore_fd; switch (building) @@ -4655,6 +4646,9 @@ void show_highclass(int building) j = 0; clev = 0; + auto const format_num = "{:>3d}) {} the {} (Level {:>2d})"; // See also race_score() + auto const format_you = "You) {} the {} (Level {:>2d})"; + while ((m < 9) || (j < MAX_HISCORES)) { if (highscore_seek(highscore_fd, j)) break; @@ -4665,9 +4659,9 @@ void show_highclass(int building) if (((pc == (building - 10)) && (building != 1)) || ((building == 1) && (clev >= PY_MAX_LEVEL))) { - sprintf(out_val, "%3d) %s the %s (Level %2d)", - (m + 1), the_score.who, race_info[pr].title.c_str(), clev); - prt(out_val, (m + 7), 0); + auto out_val = fmt::format(format_num, + (m + 1), the_score.who, race_info[pr].title, clev); + prt(out_val.c_str(), (m + 7), 0); m++; } j++; @@ -4676,17 +4670,21 @@ void show_highclass(int building) /* Now, list the active player if they qualify */ if ((building == 1) && (p_ptr->lev >= PY_MAX_LEVEL)) { - sprintf(out_val, "You) %s the %s (Level %2d)", - player_name, race_info[p_ptr->prace].title.c_str(), p_ptr->lev); - prt(out_val, (m + 8), 0); + auto out_val = fmt::format(format_you, + game->player_name, + race_info[p_ptr->prace].title, + p_ptr->lev); + prt(out_val.c_str(), (m + 8), 0); } else if ((building != 1)) { if ((p_ptr->lev > clev) && (p_ptr->pclass == (building - 10))) { - sprintf(out_val, "You) %s the %s (Level %2d)", - player_name, race_info[p_ptr->prace].title.c_str(), p_ptr->lev); - prt(out_val, (m + 8), 0); + auto out_val = fmt::format(format_you, + game->player_name, + race_info[p_ptr->prace].title, + p_ptr->lev); + prt(out_val.c_str(), (m + 8), 0); } } @@ -4710,7 +4708,7 @@ void race_score(int race_num) int i = 0, j, m = 0; int pr, clev, lastlev; high_score the_score; - char buf[1024], out_val[256], tmp_str[80]; + char buf[1024], tmp_str[80]; int highscore_fd; lastlev = 0; @@ -4742,6 +4740,9 @@ void race_score(int race_num) m = 0; j = 0; + auto const format_num = "{:>3d}) {} the {} (Level {:>2d})"; // See also show_highclass() + auto const format_you = "You) {} the {} (Level {:>2d})"; + while ((m < 10) && (j < i)) { if (highscore_seek(highscore_fd, j)) break; @@ -4750,10 +4751,12 @@ void race_score(int race_num) clev = atoi(the_score.cur_lev); if (pr == race_num) { - sprintf(out_val, "%3d) %s the %s (Level %3d)", - (m + 1), the_score.who, - race_info[pr].title.c_str(), clev); - prt(out_val, (m + 7), 0); + auto out_val = fmt::format(format_num, + (m + 1), + the_score.who, + race_info[pr].title, + clev); + prt(out_val.c_str(), (m + 7), 0); m++; lastlev = clev; } @@ -4763,9 +4766,11 @@ void race_score(int race_num) /* add player if qualified */ if ((p_ptr->prace == race_num) && (p_ptr->lev >= lastlev)) { - sprintf(out_val, "You) %s the %s (Level %3d)", - player_name, race_info[p_ptr->prace].title.c_str(), p_ptr->lev); - prt(out_val, (m + 8), 0); + auto out_val = fmt::format(format_you, + game->player_name, + race_info[p_ptr->prace].title, + p_ptr->lev); + prt(out_val.c_str(), (m + 8), 0); } fd_close(highscore_fd); @@ -4889,7 +4894,7 @@ static errr top_twenty(void) strftime(the_score.day, 9, "%m/%d/%y", localtime(&ct)); /* Save the player name (15 chars) */ - sprintf(the_score.who, "%-.15s", player_name); + sprintf(the_score.who, "%-.15s", game->player_name.c_str()); /* Save the player info XXX XXX XXX */ sprintf(the_score.p_r, "%2d", p_ptr->prace); @@ -4989,7 +4994,7 @@ static errr predict_score(void) strcpy(the_score.day, "TODAY"); /* Save the player name (15 chars) */ - sprintf(the_score.who, "%-.15s", player_name); + sprintf(the_score.who, "%-.15s", game->player_name.c_str()); /* Save the player info XXX XXX XXX */ sprintf(the_score.p_r, "%2d", p_ptr->prace); @@ -5138,7 +5143,7 @@ static void kingly(void) /* Display a message */ put_str("Veni, Vidi, Vici!", 15, 26); put_str("I came, I saw, I conquered!", 16, 21); - put_str(format("All Hail the Mighty %s!", player_name), 17, 22); + put_str(format("All Hail the Mighty %s!", game->player_name.c_str()), 17, 22); /* Flush input */ flush(); @@ -5170,10 +5175,10 @@ void wipe_saved() dungeon_type = d; if (get_dungeon_save(buf)) { - char tmp[80], name[1024]; + auto tmp = fmt::format("{}.{}", game->player_base, buf); - sprintf(tmp, "%s.%s", player_base, buf); - path_build(name, 1024, ANGBAND_DIR_SAVE, tmp); + char name[1024]; + path_build(name, 1024, ANGBAND_DIR_SAVE, tmp.c_str()); /* Remove the dungeon save file */ fd_kill(name); @@ -5234,7 +5239,6 @@ void close_game(void) /* Make a note */ { char long_day[30]; - char buf[80]; time_t ct = time((time_t*)NULL); /* Get the date */ @@ -5242,11 +5246,13 @@ void close_game(void) "%Y-%m-%d at %H:%M:%S", localtime(&ct)); /* Create string */ - sprintf(buf, "\n%s was killed by %s on %s\n", player_name, - died_from, long_day); + auto buf = fmt::format("\n{} was killed by {} on {}\n", + game->player_name, + died_from, + long_day); /* Output to the notes file */ - output_note(buf); + output_note(buf.c_str()); } /* Handle score, show Top scores */ diff --git a/src/game.hpp b/src/game.hpp index 845824b7..daae15c3 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -15,6 +15,16 @@ struct Game { /** + * Player character name + */ + std::string player_name; + + /* + * Stripped version of "player_name" + */ + std::string player_base; + + /** * Wilderness map */ grid<wilderness_map> wilderness; diff --git a/src/init1.cc b/src/init1.cc index 427e2d86..96c1b067 100644 --- a/src/init1.cc +++ b/src/init1.cc @@ -6749,7 +6749,7 @@ static cptr process_dungeon_file_expr(char **sp, char *fp) /* Player */ else if (streq(b + 1, "PLAYER")) { - v = player_base; + v = game->player_base.c_str(); // The string SHOULD be stable enough for this } /* Town */ diff --git a/src/loadsave.cc b/src/loadsave.cc index 5c6b24bb..645d479e 100644 --- a/src/loadsave.cc +++ b/src/loadsave.cc @@ -520,7 +520,7 @@ static bool_ do_extra(ls_flag_t flag) auto const &d_info = game->edit_data.d_info; auto &s_info = game->s_info; - do_string(player_name, 32, flag); + do_std_string(game->player_name, flag); do_string(died_from, 80, flag); @@ -1566,7 +1566,7 @@ void save_dungeon(void) if (!get_dungeon_save(buf) || (!dun_level)) return; /* Construct filename */ - sprintf(tmp, "%s.%s", player_base, buf); + sprintf(tmp, "%s.%s", game->player_base.c_str(), buf); path_build(name, 1024, ANGBAND_DIR_SAVE, tmp); /* Open the file */ @@ -1973,7 +1973,7 @@ bool_ load_dungeon(char *ext) s16b old_dun = dun_level; /* Construct name */ - sprintf(tmp, "%s.%s", player_base, ext); + sprintf(tmp, "%s.%s", game->player_base.c_str(), ext); path_build(name, 1024, ANGBAND_DIR_SAVE, tmp); /* Open the file */ diff --git a/src/main.cc b/src/main.cc index 9dd49aaa..3a08c6b0 100644 --- a/src/main.cc +++ b/src/main.cc @@ -76,16 +76,6 @@ static void init_save_dir(void) } } -static void init_player_name() -{ - /* Get the user id (?) */ - int player_uid = getuid(); - - /* Acquire the "user name" as a default player name */ - user_name(player_name, player_uid); -} - - /* * Initialize and verify the file paths, and the score file. * @@ -139,7 +129,7 @@ int main_real(int argc, char *argv[], char const *platform_sys, int (*init_platf init_file_paths_with_env(); /* Initialize the player name */ - init_player_name(); + game->player_name = user_name(); /* Make sure save directory exists */ init_save_dir(); @@ -179,8 +169,8 @@ int main_real(int argc, char *argv[], char const *platform_sys, int (*init_platf case 'U': { if (!argv[i][2]) goto usage; - strcpy(player_name, &argv[i][2]); - strcpy(player_base, &argv[i][2]); + game->player_name = &argv[i][2]; + game->player_base = &argv[i][2]; no_begin_screen = TRUE; break; } diff --git a/src/notes.cc b/src/notes.cc index 071b02e9..33c6bf76 100644 --- a/src/notes.cc +++ b/src/notes.cc @@ -28,7 +28,7 @@ void show_notes_file(void) char caption[10 + 13]; /* Hack -- extract first 8 characters of name and append an extension */ - (void)strnfmt(basename, sizeof(basename), "%.8s.nte", player_base); + (void)strnfmt(basename, sizeof(basename), "%.8s.nte", game->player_base.c_str()); basename[sizeof(basename) - 1] = '\0'; /* Build the path */ @@ -48,14 +48,14 @@ void show_notes_file(void) * Output a string to the notes file. * This is the only function that references that file. */ -void output_note(char *final_note) +void output_note(const char *final_note) { FILE *fff; char basename[13]; char buf[1024]; /* Hack -- extract first 8 characters of name and append an extension */ - (void)strnfmt(basename, sizeof(basename), "%.8s.nte", player_base); + (void)strnfmt(basename, sizeof(basename), "%.8s.nte", game->player_base.c_str()); basename[sizeof(basename) - 1] = '\0'; /* Build the path */ @@ -145,7 +145,7 @@ void add_note_type(int note_number) "%s %s\n" "Born on %s\n" "================================================\n", - player_name, player, true_long_day); + game->player_name.c_str(), player, true_long_day); break; } @@ -156,7 +156,7 @@ void add_note_type(int note_number) "%s slew Morgoth on %s\n" "Long live %s!\n" "================================================", - player_name, true_long_day, player_name); + game->player_name.c_str(), true_long_day, game->player_name.c_str()); break; } diff --git a/src/notes.hpp b/src/notes.hpp index e8c22bb7..dbe5c04f 100644 --- a/src/notes.hpp +++ b/src/notes.hpp @@ -1,6 +1,6 @@ #pragma once extern void show_notes_file(void); -extern void output_note(char *final_note); +extern void output_note(char const *final_note); extern void add_note(char *note, char code); extern void add_note_type(int note_number); diff --git a/src/q_library.cc b/src/q_library.cc index 8d2e8a54..c9855591 100644 --- a/src/q_library.cc +++ b/src/q_library.cc @@ -2,6 +2,7 @@ #include "cave_type.hpp" #include "dungeon_flag.hpp" +#include "game.hpp" #include "hooks.hpp" #include "lua_bind.hpp" #include "monster2.hpp" @@ -455,7 +456,7 @@ void quest_library_building(bool_ *paid, bool_ *recreate) object_type forge; object_type *q_ptr = &forge; object_prep(q_ptr, lookup_kind(TV_BOOK, 61)); - q_ptr->artifact_name = player_name; + q_ptr->artifact_name = game->player_name; q_ptr->found = OBJ_FOUND_REWARD; object_aware(q_ptr); object_known(q_ptr); diff --git a/src/randart.cc b/src/randart.cc index 488f11e1..55ffd265 100644 --- a/src/randart.cc +++ b/src/randart.cc @@ -358,7 +358,7 @@ bool_ create_artifact(object_type *o_ptr, bool_ a_scroll, bool_ get_name) } else /* Default name = of 'player name' */ - sprintf(new_name, "of '%s'", player_name); + sprintf(new_name, "of '%s'", game->player_name.c_str()); } else { diff --git a/src/squeltch.cc b/src/squeltch.cc index 747c6beb..8d2db53a 100644 --- a/src/squeltch.cc +++ b/src/squeltch.cc @@ -33,6 +33,7 @@ #include <algorithm> #include <deque> +#include <fmt/format.h> #include <list> #include <memory> #include <string> @@ -170,16 +171,15 @@ static int create_new_rule() static void automatizer_save_rules() { - char name[30] = { '\0' }; char buf[1025]; char ch; int hgt, wid; Term_get_size(&wid, &hgt); - sprintf(name, "%s.atm", player_name); + std::string name = fmt::format("{}.atm", game->player_name); - if (!input_box("Save name?", hgt / 2, wid / 2, name, sizeof(name))) + if (!input_box("Save name?", hgt / 2, wid / 2, &name, 30)) { return; } @@ -201,7 +201,7 @@ static void automatizer_save_rules() }; // Build the filename - path_build(buf, 1024, ANGBAND_DIR_USER, name); + path_build(buf, 1024, ANGBAND_DIR_USER, name.c_str()); if (file_exist(buf)) { diff --git a/src/util.cc b/src/util.cc index ccc61143..604c8793 100644 --- a/src/util.cc +++ b/src/util.cc @@ -37,23 +37,23 @@ using std::chrono::milliseconds; /* * Find a default user name from the system. */ -void user_name(char *buf, int id) +std::string user_name() { #ifdef SET_UID + /* Get the user id (?) */ + int player_uid = getuid(); + struct passwd *pw; /* Look up the user name */ - if ((pw = getpwuid(id))) + if ((pw = getpwuid(player_uid))) { - (void)strcpy(buf, pw->pw_name); - buf[16] = '\0'; - - return; + return pw->pw_name; } #endif /* SET_UID */ - /* Oops. Hack -- default to "PLAYER" */ - strcpy(buf, "PLAYER"); + /* Default to "PLAYER" if we don't have platform support */ + return "PLAYER"; } @@ -3601,7 +3601,7 @@ void display_list(int y, int x, int h, int w, cptr title, cptr *list, int max, i /* * Creates an input box */ -bool_ input_box(cptr text, int y, int x, char *buf, int max) +bool input_box(cptr text, int y, int x, char *buf, int max) { int smax = strlen(text); @@ -3616,6 +3616,27 @@ bool_ input_box(cptr text, int y, int x, char *buf, int max) } /* + * Creates an input box + */ +bool input_box(std::string const &text, int y, int x, std::string *buf, std::size_t max) +{ + std::size_t smax = text.size(); + + if (max > smax) + { + smax = max; + } + smax++; + + draw_box(y - 1, x - (smax / 2), 3, smax); + c_put_str(TERM_WHITE, text.c_str(), y, x - (text.size() / 2)); + + Term_gotoxy(x - (smax / 2) + 1, y + 1); + return askfor_aux(buf, max); +} + + +/* * Creates a msg bbox and ask a question */ char msg_box(cptr text, int y, int x) diff --git a/src/util.hpp b/src/util.hpp index c4c7b30c..3c489120 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -8,7 +8,8 @@ #define MAX_IGNORE_KEYMAPS 12 -extern bool_ input_box(cptr text, int y, int x, char *buf, int max); +extern bool input_box(cptr text, int y, int x, char *buf, int max); +extern bool input_box(std::string const &text, int y, int x, std::string *buf, std::size_t max); extern void draw_box(int y, int x, int h, int w); extern void display_list(int y, int x, int h, int w, cptr title, cptr *list, int max, int begin, int sel, byte sel_color); extern std::string get_player_race_name(int pr, int ps); @@ -78,4 +79,4 @@ extern bool in_bounds2(int y, int x); extern bool panel_contains(int y, int x); extern errr path_parse(char *buf, int max, cptr file); extern void pause_line(int row); -extern void user_name(char *buf, int id); +extern std::string user_name(); diff --git a/src/variable.cc b/src/variable.cc index f1d007ff..e0a31604 100644 --- a/src/variable.cc +++ b/src/variable.cc @@ -202,16 +202,6 @@ object_type *tracked_object; /* - * Current player's character name - */ -char player_name[32]; - -/* - * Stripped version of "player_name" - */ -char player_base[32]; - -/* * What killed the player */ char died_from[80]; diff --git a/src/variable.hpp b/src/variable.hpp index 0fba970d..74569434 100644 --- a/src/variable.hpp +++ b/src/variable.hpp @@ -145,8 +145,6 @@ extern player_race const *rp_ptr; extern player_race_mod const *rmp_ptr; extern player_class const *cp_ptr; extern player_spec const *spp_ptr; -extern char player_name[32]; -extern char player_base[32]; extern int wildc2i[256]; extern cptr DEFAULT_FEAT_TEXT; extern cptr DEFAULT_FEAT_TUNNEL; |