OSDN Git Service

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