OSDN Git Service

[Feature] *_infoのname、text、tagをstd::stringにする
[hengbandforosx/hengbandosx.git] / src / main / angband-initializer.cpp
1 /*!
2  * @brief ゲームデータ初期化
3  * @date 2014/01/28
4  * @author
5  * <pre>
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7  * This software may be copied and distributed for educational, research,
8  * and not for profit purposes provided that this copyright and statement
9  * are included in all such copies.  Other copyrights may also apply.
10  * 2014 Deskull rearranged comment for Doxygen.\n
11  * </pre>
12  */
13
14 #include "main/angband-initializer.h"
15 #include "dungeon/dungeon.h"
16 #include "floor/wild.h"
17 #include "info-reader/feature-reader.h"
18 #include "io/files-util.h"
19 #include "io/read-pref-file.h"
20 #include "io/uid-checker.h"
21 #include "main/game-data-initializer.h"
22 #include "main/info-initializer.h"
23 #include "market/building-initializer.h"
24 #include "monster-race/monster-race.h"
25 #include "monster-race/race-flags7.h"
26 #include "system/angband-version.h"
27 #include "system/system-variables.h"
28 #include "term/screen-processor.h"
29 #include "term/term-color-types.h"
30 #include "time.h"
31 #include "util/angband-files.h"
32 #include "world/world.h"
33 #ifndef WINDOWS
34 #include <dirent.h>
35 #include "util/string-processor.h"
36 #endif
37
38 char *file_read__buf;
39 char *file_read__swp;
40 char *file_read__tmp;
41
42 /*!
43  * @brief 各データファイルを読み取るためのパスを取得する
44  * Find the default paths to all of our important sub-directories.
45  * @param path パス保管先の文字列
46  * @return なし
47  */
48 void init_file_paths(char *libpath, char *varpath)
49 {
50     char *libtail, *vartail;
51     char buf[1024];
52
53     string_free(ANGBAND_DIR);
54     string_free(ANGBAND_DIR_APEX);
55     string_free(ANGBAND_DIR_BONE);
56     string_free(ANGBAND_DIR_DATA);
57     string_free(ANGBAND_DIR_EDIT);
58     string_free(ANGBAND_DIR_SCRIPT);
59     string_free(ANGBAND_DIR_FILE);
60     string_free(ANGBAND_DIR_HELP);
61     string_free(ANGBAND_DIR_INFO);
62     string_free(ANGBAND_DIR_SAVE);
63     string_free(ANGBAND_DIR_DEBUG_SAVE);
64     string_free(ANGBAND_DIR_USER);
65     string_free(ANGBAND_DIR_XTRA);
66
67     ANGBAND_DIR = string_make(libpath);
68     libtail = libpath + strlen(libpath);
69     vartail = varpath + strlen(varpath);
70     strcpy(vartail, "apex");
71     ANGBAND_DIR_APEX = string_make(varpath);
72     strcpy(vartail, "bone");
73     ANGBAND_DIR_BONE = string_make(varpath);
74     strcpy(vartail, "data");
75     ANGBAND_DIR_DATA = string_make(varpath);
76     strcpy(libtail, "edit");
77     ANGBAND_DIR_EDIT = string_make(libpath);
78     strcpy(libtail, "script");
79     ANGBAND_DIR_SCRIPT = string_make(libpath);
80     strcpy(libtail, "file");
81     ANGBAND_DIR_FILE = string_make(libpath);
82     strcpy(libtail, "help");
83     ANGBAND_DIR_HELP = string_make(libpath);
84     strcpy(libtail, "info");
85     ANGBAND_DIR_INFO = string_make(libpath);
86     strcpy(libtail, "pref");
87     ANGBAND_DIR_PREF = string_make(libpath);
88     strcpy(vartail, "save");
89     ANGBAND_DIR_SAVE = string_make(varpath);
90     path_build(buf, sizeof(buf), ANGBAND_DIR_SAVE, "log");
91     ANGBAND_DIR_DEBUG_SAVE = string_make(buf);
92 #ifdef PRIVATE_USER_PATH
93     path_build(buf, sizeof(buf), PRIVATE_USER_PATH, VERSION_NAME);
94     ANGBAND_DIR_USER = string_make(buf);
95 #else
96     strcpy(vartail, "user");
97     ANGBAND_DIR_USER = string_make(varpath);
98 #endif
99     strcpy(libtail, "xtra");
100     ANGBAND_DIR_XTRA = string_make(libpath);
101
102     time_t now = time(NULL);
103     struct tm *t = localtime(&now);
104     char tmp[128];
105     strftime(tmp, sizeof(tmp), "%Y-%m-%d-%H-%M-%S", t);
106     path_build(debug_savefile, sizeof(debug_savefile), ANGBAND_DIR_DEBUG_SAVE, tmp);
107
108 #ifdef WINDOWS
109     struct _finddata_t c_file;
110     intptr_t hFile;
111     char log_file_expr[1024];
112     path_build(log_file_expr, sizeof(log_file_expr), ANGBAND_DIR_DEBUG_SAVE, "*-*");
113
114     if ((hFile = _findfirst(log_file_expr, &c_file)) != -1L) {
115         do {
116             if (((t->tm_yday + 365 - localtime(&c_file.time_write)->tm_yday) % 365) > 7) {
117                 char c_file_fullpath[1024];
118                 path_build(c_file_fullpath, sizeof(c_file_fullpath), ANGBAND_DIR_DEBUG_SAVE, c_file.name);
119                 remove(c_file_fullpath);
120             }
121         } while (_findnext(hFile, &c_file) == 0);
122         _findclose(hFile);
123     }
124 #else
125     {
126         DIR *saves_dir = opendir(ANGBAND_DIR_DEBUG_SAVE);
127
128         if (saves_dir) {
129             struct dirent *next_entry;
130
131             while ((next_entry = readdir(saves_dir))) {
132                 if (angband_strchr(next_entry->d_name, '-')) {
133                     char path[1024];
134                     struct stat next_stat;
135
136                     path_build(path, sizeof(path), ANGBAND_DIR_DEBUG_SAVE, next_entry->d_name);
137                     /*
138                      * Remove if modified more than a week ago,
139                      * 7*24*60*60 seconds.
140                      */
141                     if (stat(path, &next_stat) == 0 &&
142                             difftime(now, next_stat.st_mtime) > 604800) {
143                         remove(path);
144                     }
145                 }
146             }
147             closedir(saves_dir);
148         }
149     }
150 #endif
151 }
152
153 /*!
154  * @brief 画面左下にシステムメッセージを表示する / Take notes on line 23
155  * @param str 初期化中のコンテンツ文字列
156  * @return なし
157  */
158 static void init_note_term(concptr str)
159 {
160     term_erase(0, 23, 255);
161     term_putstr(20, 23, -1, TERM_WHITE, str);
162     term_fresh();
163 }
164
165 /*!
166  * @brief ゲーム画面無しの時の初期化メッセージ出力
167  * @param str 初期化中のコンテンツ文字列
168  * @return なし
169  */
170 static void init_note_no_term(concptr str)
171 {
172     /* Don't show initialization message when there is no game terminal. */
173     (void)str;
174 }
175
176 /*!
177  * @brief 全ゲームデータ読み込みのサブルーチン / Explain a broken "lib" folder and quit (see below).
178  * @param なし
179  * @return なし
180  * @note
181  * <pre>
182  * This function is "messy" because various things
183  * may or may not be initialized, but the "plog()" and "quit()"
184  * functions are "supposed" to work under any conditions.
185  * </pre>
186  */
187 static void init_angband_aux(concptr why)
188 {
189     plog(why);
190     plog(_("'lib'ディレクトリが存在しないか壊れているようです。", "The 'lib' directory is probably missing or broken."));
191     plog(_("ひょっとするとアーカイブが正しく解凍されていないのかもしれません。", "The 'lib' directory is probably missing or broken."));
192     plog(_("該当する'README'ファイルを読んで確認してみて下さい。", "See the 'README' file for more information."));
193     quit(_("致命的なエラー。", "Fatal Error."));
194 }
195
196 /*!
197  * @brief タイトル記述
198  * @param なし
199  * @return なし
200  */
201 static void put_title(void)
202 {
203     char title[120];
204     put_version(title);
205
206     int col = (80 - strlen(title)) / 2;
207     col = col < 0 ? 0 : col;
208     const int VER_INFO_ROW = 3; //!< タイトル表記(行)
209     prt(title, VER_INFO_ROW, col);
210 }
211
212 /*!
213  * @brief 全ゲームデータ読み込みのメインルーチン /
214  * @param player_ptr プレーヤーへの参照ポインタ
215  * @param process_autopick_file_command 自動拾いファイル読み込み関数への関数ポインタ
216  * @param no_term TRUEならゲーム画面無しの状態で初期化を行う。
217  *                コマンドラインからスポイラーの出力のみを行う時の使用を想定する。
218  * @return なし
219  */
220 void init_angband(player_type *player_ptr, process_autopick_file_command_pf process_autopick_file_command, bool no_term)
221 {
222     C_MAKE(file_read__buf, FILE_READ_BUFF_SIZE, char);
223     C_MAKE(file_read__swp, FILE_READ_BUFF_SIZE, char);
224     C_MAKE(file_read__tmp, FILE_READ_BUFF_SIZE, char);
225     char buf[1024];
226     path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
227     int fd = fd_open(buf, O_RDONLY);
228     if (fd < 0) {
229         char why[sizeof(buf) + 128];
230         sprintf(why, _("'%s'ファイルにアクセスできません!", "Cannot access the '%s' file!"), buf);
231         init_angband_aux(why);
232     }
233
234     (void)fd_close(fd);
235
236     if (!no_term) {
237         term_clear();
238
239         path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
240         FILE *fp;
241         fp = angband_fopen(buf, "r");
242         if (fp) {
243             int i = 0;
244             while (0 == angband_fgets(fp, buf, sizeof(buf)))
245                 term_putstr(0, i++, -1, TERM_WHITE, buf);
246
247             angband_fclose(fp);
248         }
249
250         term_flush();
251     }
252
253     path_build(buf, sizeof(buf), ANGBAND_DIR_APEX, "scores.raw");
254     fd = fd_open(buf, O_RDONLY);
255     BIT_FLAGS file_permission = 0664;
256     if (fd < 0) {
257         safe_setuid_grab(player_ptr);
258         fd = fd_make(buf, file_permission);
259         safe_setuid_drop();
260         if (fd < 0) {
261             char why[sizeof(buf) + 128];
262             sprintf(why, _("'%s'ファイルを作成できません!", "Cannot create the '%s' file!"), buf);
263             init_angband_aux(why);
264         }
265     }
266
267     (void)fd_close(fd);
268     if (!no_term) {
269         put_title();
270     }
271
272     void (*init_note)(concptr) = (no_term ? init_note_no_term : init_note_term);
273
274     init_note(_("[変数を初期化しています...(その他)", "[Initializing values... (misc)]"));
275     if (init_misc(player_ptr))
276         quit(_("その他の変数を初期化できません", "Cannot initialize misc. values"));
277
278     init_note(_("[データの初期化中... (地形)]", "[Initializing arrays... (features)]"));
279     if (init_f_info())
280         quit(_("地形初期化不能", "Cannot initialize features"));
281
282     if (init_feat_variables())
283         quit(_("地形初期化不能", "Cannot initialize features"));
284
285     init_note(_("[データの初期化中... (アイテム)]", "[Initializing arrays... (objects)]"));
286     if (init_k_info())
287         quit(_("アイテム初期化不能", "Cannot initialize objects"));
288
289     init_note(_("[データの初期化中... (伝説のアイテム)]", "[Initializing arrays... (artifacts)]"));
290     if (init_a_info())
291         quit(_("伝説のアイテム初期化不能", "Cannot initialize artifacts"));
292
293     init_note(_("[データの初期化中... (名のあるアイテム)]", "[Initializing arrays... (ego-items)]"));
294     if (init_e_info())
295         quit(_("名のあるアイテム初期化不能", "Cannot initialize ego-items"));
296
297     init_note(_("[データの初期化中... (モンスター)]", "[Initializing arrays... (monsters)]"));
298     if (init_r_info())
299         quit(_("モンスター初期化不能", "Cannot initialize monsters"));
300
301     init_note(_("[データの初期化中... (ダンジョン)]", "[Initializing arrays... (dungeon)]"));
302     if (init_d_info())
303         quit(_("ダンジョン初期化不能", "Cannot initialize dungeon"));
304
305     for (int i = 1; i < current_world_ptr->max_d_idx; i++)
306         if (d_info[i].final_guardian)
307             r_info[d_info[i].final_guardian].flags7 |= RF7_GUARDIAN;
308
309     init_note(_("[データの初期化中... (魔法)]", "[Initializing arrays... (magic)]"));
310     if (init_m_info())
311         quit(_("魔法初期化不能", "Cannot initialize magic"));
312
313     init_note(_("[データの初期化中... (熟練度)]", "[Initializing arrays... (skill)]"));
314     if (init_s_info())
315         quit(_("熟練度初期化不能", "Cannot initialize skill"));
316
317     init_note(_("[配列を初期化しています... (荒野)]", "[Initializing arrays... (wilderness)]"));
318     if (init_wilderness())
319         quit(_("荒野を初期化できません", "Cannot initialize wilderness"));
320
321     init_note(_("[配列を初期化しています... (街)]", "[Initializing arrays... (towns)]"));
322     if (init_towns())
323         quit(_("街を初期化できません", "Cannot initialize towns"));
324
325     init_note(_("[配列を初期化しています... (建物)]", "[Initializing arrays... (buildings)]"));
326     if (init_buildings())
327         quit(_("建物を初期化できません", "Cannot initialize buildings"));
328
329     init_note(_("[配列を初期化しています... (クエスト)]", "[Initializing arrays... (quests)]"));
330     if (init_quests())
331         quit(_("クエストを初期化できません", "Cannot initialize quests"));
332
333     if (init_v_info())
334         quit(_("vault 初期化不能", "Cannot initialize vaults"));
335
336     init_note(_("[データの初期化中... (その他)]", "[Initializing arrays... (other)]"));
337     if (init_other(player_ptr))
338         quit(_("その他のデータ初期化不能", "Cannot initialize other stuff"));
339
340     init_note(_("[データの初期化中... (アロケーション)]", "[Initializing arrays... (alloc)]"));
341     if (init_alloc())
342         quit(_("アロケーション・スタッフ初期化不能", "Cannot initialize alloc stuff"));
343
344     init_note(_("[ユーザー設定ファイルを初期化しています...]", "[Initializing user pref files...]"));
345     strcpy(buf, "pref.prf");
346     process_pref_file(player_ptr, buf, process_autopick_file_command);
347     sprintf(buf, "pref-%s.prf", ANGBAND_SYS);
348     process_pref_file(player_ptr, buf, process_autopick_file_command);
349     init_note(_("[初期化終了]", "[Initialization complete]"));
350 }