OSDN Git Service

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