OSDN Git Service

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