OSDN Git Service

Merge pull request #3814 from Slimebreath6078/feature/Add_Laffey_II
[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/quest.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 "system/angband-version.h"
27 #include "system/dungeon-info.h"
28 #include "system/monster-race-info.h"
29 #include "system/system-variables.h"
30 #include "term/gameterm.h"
31 #include "term/screen-processor.h"
32 #include "term/term-color-types.h"
33 #include "time.h"
34 #include "util/angband-files.h"
35 #include "world/world.h"
36 #include <chrono>
37
38 /*!
39  * @brief 古いデバッグ用セーブファイルを削除する
40  *
41  * 最終更新日時が現在時刻より7日以前のデバッグ用セーブファイルを削除する。
42  * デバッグ用セーブファイルは、ANGBAND_DIR_DEBUG_SAVEディレクトリにある、
43  * ファイル名に '-' を含むファイルであることを想定する。
44  */
45 static void remove_old_debug_savefiles()
46 {
47     namespace fs = std::filesystem;
48     constexpr auto remove_threshold_days = std::chrono::days(7);
49     const auto now = fs::file_time_type::clock::now();
50
51     // アクセスエラーが発生した場合に例外が送出されないようにするため
52     // 例外を送出せず引数でエラーコードを返すオーバーロードを使用する。
53     // アクセスエラーが発生した場合は単に無視し、エラーコードの確認は行わない。
54     std::error_code ec;
55
56     for (const auto &entry : fs::directory_iterator(ANGBAND_DIR_DEBUG_SAVE, ec)) {
57         const auto &path = entry.path();
58         if (path.filename().string().find('-') == std::string::npos) {
59             continue;
60         }
61
62         const auto savefile_timestamp = fs::last_write_time(path);
63         const auto elapsed_days = std::chrono::duration_cast<std::chrono::days>(now - savefile_timestamp);
64         if (elapsed_days >= remove_threshold_days) {
65             fs::remove(path, ec);
66         }
67     }
68 }
69
70 /*!
71  * @brief 各データファイルを読み取るためのパスを取得する.
72  * @param libpath 各PCのインストール環境における"lib/" を表す絶対パス
73  */
74 void init_file_paths(const std::filesystem::path &libpath)
75 {
76     ANGBAND_DIR = std::filesystem::path(libpath);
77     ANGBAND_DIR_APEX = std::filesystem::path(libpath).append("apex");
78     ANGBAND_DIR_BONE = std::filesystem::path(libpath).append("bone");
79     ANGBAND_DIR_DATA = std::filesystem::path(libpath).append("data");
80     ANGBAND_DIR_EDIT = std::filesystem::path(libpath).append("edit");
81     ANGBAND_DIR_SCRIPT = std::filesystem::path(libpath).append("script");
82     ANGBAND_DIR_FILE = std::filesystem::path(libpath).append("file");
83     ANGBAND_DIR_HELP = std::filesystem::path(libpath).append("help");
84     ANGBAND_DIR_INFO = std::filesystem::path(libpath).append("info");
85     ANGBAND_DIR_PREF = std::filesystem::path(libpath).append("pref");
86     ANGBAND_DIR_SAVE = std::filesystem::path(libpath).append("save");
87     ANGBAND_DIR_DEBUG_SAVE = std::filesystem::path(ANGBAND_DIR_SAVE).append("log");
88 #ifdef PRIVATE_USER_PATH
89     ANGBAND_DIR_USER = std::filesystem::path(PRIVATE_USER_PATH).append(VARIANT_NAME);
90 #else
91     ANGBAND_DIR_USER = std::filesystem::path(libpath).append("user");
92 #endif
93     ANGBAND_DIR_XTRA = std::filesystem::path(libpath).append("xtra");
94
95     time_t now = time(nullptr);
96     struct tm *t = localtime(&now);
97     char tmp[128];
98     strftime(tmp, sizeof(tmp), "%Y-%m-%d-%H-%M-%S", t);
99     debug_savefile = path_build(ANGBAND_DIR_DEBUG_SAVE, tmp);
100     remove_old_debug_savefiles();
101 }
102
103 /*!
104  * @brief 画面左下にシステムメッセージを表示する / Take notes on line 23
105  * @param str 初期化中のコンテンツ文字列
106  */
107 static void init_note_term(concptr str)
108 {
109     term_erase(0, 23);
110     term_putstr(20, 23, -1, TERM_WHITE, str);
111     term_fresh();
112 }
113
114 /*!
115  * @brief ゲーム画面無しの時の初期化メッセージ出力
116  * @param str 初期化中のコンテンツ文字列
117  */
118 static void init_note_no_term(concptr str)
119 {
120     /* Don't show initialization message when there is no game terminal. */
121     (void)str;
122 }
123
124 /*!
125  * @brief 全ゲームデータ読み込みのサブルーチン / Explain a broken "lib" folder and quit (see below).
126  * @param なし
127  * @note
128  * <pre>
129  * This function is "messy" because various things
130  * may or may not be initialized, but the "plog()" and "quit()"
131  * functions are "supposed" to work under any conditions.
132  * </pre>
133  */
134 static void init_angband_aux(const std::string &why)
135 {
136     plog(why.data());
137     plog(_("'lib'ディレクトリが存在しないか壊れているようです。", "The 'lib' directory is probably missing or broken."));
138     plog(_("ひょっとするとアーカイブが正しく解凍されていないのかもしれません。", "The 'lib' directory is probably missing or broken."));
139     plog(_("該当する'README'ファイルを読んで確認してみて下さい。", "See the 'README' file for more information."));
140     quit(_("致命的なエラー。", "Fatal Error."));
141 }
142
143 /*!
144  * @brief タイトル記述
145  * @param なし
146  */
147 static void put_title()
148 {
149     const auto title = get_version();
150
151     auto col = (title.length() <= MAIN_TERM_MIN_COLS) ? (MAIN_TERM_MIN_COLS - title.length()) / 2 : 0;
152     constexpr auto VER_INFO_ROW = 3; //!< タイトル表記(行)
153     prt(title, VER_INFO_ROW, col);
154 }
155
156 /*!
157  * @brief 全ゲームデータ読み込みのメインルーチン /
158  * @param player_ptr プレイヤーへの参照ポインタ
159  * @param no_term TRUEならゲーム画面無しの状態で初期化を行う。
160  *                コマンドラインからスポイラーの出力のみを行う時の使用を想定する。
161  */
162 void init_angband(PlayerType *player_ptr, bool no_term)
163 {
164     const auto &path_news = path_build(ANGBAND_DIR_FILE, _("news_j.txt", "news.txt"));
165     auto fd = fd_open(path_news, O_RDONLY);
166     if (fd < 0) {
167         std::string why = _("'", "Cannot access the '");
168         why.append(path_news.string());
169         why.append(_("'ファイルにアクセスできません!", "' file!"));
170         init_angband_aux(why);
171     }
172
173     (void)fd_close(fd);
174     if (!no_term) {
175         term_clear();
176         auto *fp = angband_fopen(path_news, FileOpenMode::READ);
177         if (fp) {
178             int i = 0;
179             char buf[1024]{};
180             while (0 == angband_fgets(fp, buf, sizeof(buf))) {
181                 term_putstr(0, i++, -1, TERM_WHITE, buf);
182             }
183
184             angband_fclose(fp);
185         }
186
187         term_flush();
188     }
189
190     const auto &path_score = path_build(ANGBAND_DIR_APEX, "scores.raw");
191     fd = fd_open(path_score, O_RDONLY);
192     if (fd < 0) {
193         safe_setuid_grab();
194         fd = fd_make(path_score, true);
195         safe_setuid_drop();
196         if (fd < 0) {
197             const auto &filename_score = path_score.string();
198             std::string why = _("'", "Cannot create the '");
199             why.append(filename_score);
200             why.append(_("'ファイルを作成できません!", "' file!"));
201             init_angband_aux(why);
202         }
203     }
204
205     (void)fd_close(fd);
206     if (!no_term) {
207         put_title();
208     }
209
210     void (*init_note)(concptr) = (no_term ? init_note_no_term : init_note_term);
211
212     init_note(_("[データの初期化中... (地形)]", "[Initializing arrays... (features)]"));
213     if (init_terrains_info()) {
214         quit(_("地形初期化不能", "Cannot initialize features"));
215     }
216
217     if (init_feat_variables()) {
218         quit(_("地形初期化不能", "Cannot initialize features"));
219     }
220
221     init_note(_("[データの初期化中... (アイテム)]", "[Initializing arrays... (objects)]"));
222     if (init_baseitems_info()) {
223         quit(_("アイテム初期化不能", "Cannot initialize objects"));
224     }
225
226     init_note(_("[データの初期化中... (伝説のアイテム)]", "[Initializing arrays... (artifacts)]"));
227     if (init_artifacts_info()) {
228         quit(_("伝説のアイテム初期化不能", "Cannot initialize artifacts"));
229     }
230
231     init_note(_("[データの初期化中... (名のあるアイテム)]", "[Initializing arrays... (ego-items)]"));
232     if (init_egos_info()) {
233         quit(_("名のあるアイテム初期化不能", "Cannot initialize ego-items"));
234     }
235
236     init_note(_("[データの初期化中... (モンスター)]", "[Initializing arrays... (monsters)]"));
237     if (init_monster_race_definitions()) {
238         quit(_("モンスター初期化不能", "Cannot initialize monsters"));
239     }
240
241     init_note(_("[データの初期化中... (ダンジョン)]", "[Initializing arrays... (dungeon)]"));
242     if (init_dungeons_info()) {
243         quit(_("ダンジョン初期化不能", "Cannot initialize dungeon"));
244     }
245
246     for (const auto &d_ref : dungeons_info) {
247         if (d_ref.idx > 0 && MonsterRace(d_ref.final_guardian).is_valid()) {
248             monraces_info[d_ref.final_guardian].misc_flags.set(MonsterMiscType::GUARDIAN);
249         }
250     }
251
252     init_note(_("[データの初期化中... (魔法)]", "[Initializing arrays... (magic)]"));
253     if (init_class_magics_info()) {
254         quit(_("魔法初期化不能", "Cannot initialize magic"));
255     }
256
257     init_note(_("[データの初期化中... (熟練度)]", "[Initializing arrays... (skill)]"));
258     if (init_class_skills_info()) {
259         quit(_("熟練度初期化不能", "Cannot initialize skill"));
260     }
261
262     init_note(_("[配列を初期化しています... (荒野)]", "[Initializing arrays... (wilderness)]"));
263     if (!init_wilderness()) {
264         quit(_("荒野を初期化できません", "Cannot initialize wilderness"));
265     }
266
267     init_note(_("[配列を初期化しています... (街)]", "[Initializing arrays... (towns)]"));
268     init_towns();
269
270     init_note(_("[配列を初期化しています... (建物)]", "[Initializing arrays... (buildings)]"));
271     init_buildings();
272
273     init_note(_("[配列を初期化しています... (クエスト)]", "[Initializing arrays... (quests)]"));
274     QuestList::get_instance().initialize();
275     if (init_vaults_info()) {
276         quit(_("vault 初期化不能", "Cannot initialize vaults"));
277     }
278
279     init_note(_("[データの初期化中... (その他)]", "[Initializing arrays... (other)]"));
280     init_other(player_ptr);
281     init_note(_("[データの初期化中... (モンスターアロケーション)]", "[Initializing arrays... (monsters alloc)]"));
282     init_monsters_alloc();
283     init_note(_("[データの初期化中... (アイテムアロケーション)]", "[Initializing arrays... (items alloc)]"));
284     init_items_alloc();
285     init_note(_("[ユーザー設定ファイルを初期化しています...]", "[Initializing user pref files...]"));
286     process_pref_file(player_ptr, "pref.prf");
287     process_pref_file(player_ptr, std::string("pref-").append(ANGBAND_SYS).append(".prf"));
288     init_note(_("[初期化終了]", "[Initialization complete]"));
289 }