OSDN Git Service

Merge pull request #716 from sikabane-works/release/3.0.0Alpha16
[hengbandforosx/hengbandosx.git] / src / player / process-name.cpp
1 #include "player/process-name.h"
2 #include "autopick/autopick-reader-writer.h"
3 #include "core/asking-player.h"
4 #include "game-option/birth-options.h"
5 #include "io/files-util.h"
6 #include "player/player-personality.h"
7 #include "term/screen-processor.h"
8 #include "term/term-color-types.h"
9 #include "util/angband-files.h"
10 #include "util/string-processor.h"
11 #include "world/world.h"
12
13 /*!
14  * @brief プレイヤーの名前をチェックして修正する
15  * Process the player name.
16  * @param player_ptr プレーヤーへの参照ポインタ
17  * @param sf セーブファイル名に合わせた修正を行うならばTRUE
18  * @return なし
19  * @details
20  * Extract a clean "base name".
21  * Build the savefile name if needed.
22  */
23 void process_player_name(player_type *creature_ptr, bool sf)
24 {
25     char old_player_base[32] = "";
26     if (current_world_ptr->character_generated)
27         strcpy(old_player_base, creature_ptr->base_name);
28
29     for (int i = 0; creature_ptr->name[i]; i++) {
30 #ifdef JP
31         if (iskanji(creature_ptr->name[i])) {
32             i++;
33             continue;
34         }
35
36         if (iscntrl((unsigned char)creature_ptr->name[i]))
37 #else
38         if (iscntrl(creature_ptr->name[i]))
39 #endif
40         {
41             quit_fmt(_("'%s' という名前は不正なコントロールコードを含んでいます。", "The name '%s' contains control chars!"), creature_ptr->name);
42         }
43     }
44
45     int k = 0;
46     for (int i = 0; creature_ptr->name[i]; i++) {
47 #ifdef JP
48         unsigned char c = creature_ptr->name[i];
49 #else
50         char c = creature_ptr->name[i];
51 #endif
52
53 #ifdef JP
54         if (iskanji(c)) {
55             if (k + 2 >= (int)sizeof(creature_ptr->base_name) || !creature_ptr->name[i + 1])
56                 break;
57
58             creature_ptr->base_name[k++] = c;
59             i++;
60             creature_ptr->base_name[k++] = creature_ptr->name[i];
61         }
62 #ifdef SJIS
63         else if (iskana(c))
64             creature_ptr->base_name[k++] = c;
65 #endif
66         else
67 #endif
68             if (!strncmp(PATH_SEP, creature_ptr->name + i, strlen(PATH_SEP))) {
69             creature_ptr->base_name[k++] = '_';
70             i += strlen(PATH_SEP);
71         }
72 #if defined(WINDOWS)
73         else if (angband_strchr("\"*,/:;<>?\\|", c))
74             creature_ptr->base_name[k++] = '_';
75 #endif
76         else if (isprint(c))
77             creature_ptr->base_name[k++] = c;
78     }
79
80     creature_ptr->base_name[k] = '\0';
81     if (!creature_ptr->base_name[0])
82         strcpy(creature_ptr->base_name, "PLAYER");
83
84     if (!keep_savefile || !savefile[0]) {
85 #ifdef SAVEFILE_MUTABLE
86         sf = TRUE;
87 #endif
88         if (!savefile_base[0] && savefile[0]) {
89             concptr s = savefile;
90             while (TRUE) {
91                 concptr t;
92                 t = angband_strstr(s, PATH_SEP);
93                 if (!t)
94                     break;
95                 s = t + 1;
96             }
97
98             strcpy(savefile_base, s);
99         }
100
101         if (!savefile_base[0] || !savefile[0])
102             sf = TRUE;
103
104         if (sf) {
105             char temp[128];
106             strcpy(savefile_base, creature_ptr->base_name);
107
108 #ifdef SAVEFILE_USE_UID
109             /* Rename the savefile, using the creature_ptr->player_uid and creature_ptr->base_name */
110             (void)sprintf(temp, "%d.%s", creature_ptr->player_uid, creature_ptr->base_name);
111 #else
112             /* Rename the savefile, using the creature_ptr->base_name */
113             (void)sprintf(temp, "%s", creature_ptr->base_name);
114 #endif
115             path_build(savefile, sizeof(savefile), ANGBAND_DIR_SAVE, temp);
116         }
117     }
118
119     if (current_world_ptr->character_generated && !streq(old_player_base, creature_ptr->base_name)) {
120         autopick_load_pref(creature_ptr, FALSE);
121     }
122 }
123
124 /*!
125  * @brief プレイヤーの名前を変更するコマンドのメインルーチン
126  * Gets a name for the character, reacting to name changes.
127  * @param creature_ptr プレーヤーへの参照ポインタ
128  * @return なし
129  * @details
130  * <pre>
131  * Assumes that "display_player()" has just been called
132  * Perhaps we should NOT ask for a name (at "birth()") on
133  * Unix machines?  XXX XXX
134  * What a horrible name for a global function.
135  * </pre>
136  */
137 void get_name(player_type *creature_ptr)
138 {
139     char tmp[64];
140     strcpy(tmp, creature_ptr->name);
141
142     if (get_string(_("キャラクターの名前を入力して下さい: ", "Enter a name for your character: "), tmp, 15)) {
143         strcpy(creature_ptr->name, tmp);
144     }
145
146     if (strlen(creature_ptr->name) == 0) {
147         strcpy(creature_ptr->name, "PLAYER");
148     }
149
150     strcpy(tmp, ap_ptr->title);
151 #ifdef JP
152     if (ap_ptr->no == 1)
153         strcat(tmp, "の");
154 #else
155     strcat(tmp, " ");
156 #endif
157     strcat(tmp, creature_ptr->name);
158
159     term_erase(34, 1, 255);
160     c_put_str(TERM_L_BLUE, tmp, 1, 34);
161     clear_from(22);
162 }