OSDN Git Service

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