OSDN Git Service

2e01d7674cf00166ae183e486736fc8a569a8f97
[hengbandforosx/hengbandosx.git] / src / player / process-death.cpp
1 /*!
2  * @brief 死亡・引退・切腹時の画面表示
3  * @date 2020/02/24
4  * @author Hourier
5  * @details
6  * core、files、view-mainwindowの参照禁止。コールバックで対応すること
7  */
8
9 #include "process-death.h"
10 #include "core/asking-player.h"
11 #include "core/player-update-types.h"
12 #include "core/stuff-handler.h"
13 #include "flavor/flavor-describer.h"
14 #include "floor/floor-town.h"
15 #include "game-option/game-play-options.h"
16 #include "inventory/inventory-slot-types.h"
17 #include "io/input-key-acceptor.h"
18 #include "object/item-tester-hooker.h"
19 #include "object/item-use-flags.h"
20 #include "perception/object-perception.h"
21 #include "player/player-class.h"
22 #include "store/store-util.h"
23 #include "store/store.h"
24 #include "system/floor-type-definition.h"
25 #include "system/object-type-definition.h"
26 #include "system/player-type-definition.h"
27 #include "term/gameterm.h"
28 #include "term/screen-processor.h"
29 #include "util/buffer-shaper.h"
30 #include "util/int-char-converter.h"
31 #include "util/string-processor.h"
32 #include "view/display-inventory.h"
33 #include "view/display-messages.h"
34 #include "world/world.h"
35
36 #define GRAVE_LINE_WIDTH 31
37
38 /*!
39  * @brief 墓石の真ん中に文字列を書き込む /
40  * Centers a string within a GRAVE_LINE_WIDTH character string          -JWT-
41  * @details
42  */
43 static void center_string(char *buf, concptr str)
44 {
45     int i = strlen(str);
46     int j = GRAVE_LINE_WIDTH / 2 - i / 2;
47     (void)sprintf(buf, "%*s%s%*s", j, "", str, GRAVE_LINE_WIDTH - i - j, "");
48 }
49
50 /*!
51  * @brief 墓に基本情報を表示
52  * @param dead_ptr プレーヤーへの参照ポインタ
53  * @param buf 墓テンプレ
54  */
55 static void show_basic_params(player_type *dead_ptr, char *buf)
56 {
57     char tomb_message[160];
58     (void)sprintf(tomb_message, _("レベル: %d", "Level: %d"), (int)dead_ptr->lev);
59     center_string(buf, tomb_message);
60     put_str(buf, 11, 11);
61
62     (void)sprintf(tomb_message, _("経験値: %ld", "Exp: %ld"), (long)dead_ptr->exp);
63     center_string(buf, tomb_message);
64     put_str(buf, 12, 11);
65
66     (void)sprintf(tomb_message, _("所持金: %ld", "AU: %ld"), (long)dead_ptr->au);
67     center_string(buf, tomb_message);
68     put_str(buf, 13, 11);
69 }
70
71 #ifdef JP
72 /*!
73  * @brief プレーヤーを殺したモンスターを表示する (日本語版専用)
74  * @param dead_ptr プレーヤーへの参照ポインタ
75  * @param buf 墓テンプレ
76  * @param tomb_message 墓碑に刻む言葉
77  * @return 追加の行数
78  */
79 static int show_killing_monster(player_type *dead_ptr, char *buf, char *tomb_message, size_t tomb_message_size)
80 {
81     shape_buffer(dead_ptr->died_from, GRAVE_LINE_WIDTH + 1, tomb_message, tomb_message_size);
82     char *t;
83     t = tomb_message + strlen(tomb_message) + 1;
84     if (!*t)
85         return 0;
86
87     char killer[MAX_MONSTER_NAME];
88     strcpy(killer, t); /* 2nd line */
89     if (*(t + strlen(t) + 1)) /* Does 3rd line exist? */
90     {
91         for (t = killer + strlen(killer) - 2; iskanji(*(t - 1)); t--) /* Loop */
92             ;
93         strcpy(t, "…");
94     } else if (angband_strstr(tomb_message, "『") && suffix(killer, "』")) {
95         char killer2[MAX_MONSTER_NAME];
96         char *name_head = angband_strstr(tomb_message, "『");
97         sprintf(killer2, "%s%s", name_head, killer);
98         if (strlen(killer2) <= GRAVE_LINE_WIDTH) {
99             strcpy(killer, killer2);
100             *name_head = '\0';
101         }
102     } else if (angband_strstr(tomb_message, "「") && suffix(killer, "」")) {
103         char killer2[MAX_MONSTER_NAME];
104         char *name_head = angband_strstr(tomb_message, "「");
105         sprintf(killer2, "%s%s", name_head, killer);
106         if (strlen(killer2) <= GRAVE_LINE_WIDTH) {
107             strcpy(killer, killer2);
108             *name_head = '\0';
109         }
110     }
111
112     center_string(buf, killer);
113     put_str(buf, 15, 11);
114     return 1;
115 }
116
117 /*!
118  * @brief どこで死んだかを表示する (日本語版専用)
119  * @param dead_ptr プレーヤーへの参照ポインタ
120  * @param buf 墓テンプレ
121  * @param tomb_message 表示する文字列
122  * @param extra_line 追加の行数
123  */
124 static void show_dead_place(player_type *dead_ptr, char *buf, char *tomb_message, int extra_line)
125 {
126     if (streq(dead_ptr->died_from, "ripe") || streq(dead_ptr->died_from, "Seppuku"))
127         return;
128
129     if (dead_ptr->current_floor_ptr->dun_level == 0) {
130         concptr field_name = dead_ptr->town_num ? "街" : "荒野";
131         if (streq(dead_ptr->died_from, "途中終了")) {
132             sprintf(tomb_message, "%sで死んだ", field_name);
133         } else {
134             sprintf(tomb_message, "に%sで殺された", field_name);
135         }
136     } else {
137         if (streq(dead_ptr->died_from, "途中終了")) {
138             sprintf(tomb_message, "地下 %d 階で死んだ", (int)dead_ptr->current_floor_ptr->dun_level);
139         } else {
140             sprintf(tomb_message, "に地下 %d 階で殺された", (int)dead_ptr->current_floor_ptr->dun_level);
141         }
142     }
143
144     center_string(buf, tomb_message);
145     put_str(buf, 15 + extra_line, 11);
146 }
147
148 /*!
149  * @brief 墓に刻む言葉を細かく表示 (日本語版専用)
150  * @param dead_ptr プレーヤーへの参照ポインタ
151  * @param buf 墓テンプレ
152  */
153 static void show_tomb_detail(player_type *dead_ptr, char *buf)
154 {
155     char tomb_message[160];
156     int extra_line = 0;
157     if (streq(dead_ptr->died_from, "途中終了")) {
158         strcpy(tomb_message, "<自殺>");
159     } else if (streq(dead_ptr->died_from, "ripe")) {
160         strcpy(tomb_message, "引退後に天寿を全う");
161     } else if (streq(dead_ptr->died_from, "Seppuku")) {
162         strcpy(tomb_message, "勝利の後、切腹");
163     } else {
164         extra_line = show_killing_monster(dead_ptr, buf, tomb_message, sizeof(tomb_message));
165     }
166
167     center_string(buf, tomb_message);
168     put_str(buf, 14, 11);
169
170     show_dead_place(dead_ptr, buf, tomb_message, extra_line);
171 }
172 #else
173
174 /*!
175  * @brief Detailed display of words engraved on the tomb (English version only)
176  * @param dead_ptr reference pointer to the player
177  * @param buf template of the tomb
178  * @return nothing
179  */
180 static void show_tomb_detail(player_type *dead_ptr, char *buf)
181 {
182     char tomb_message[160];
183     (void)sprintf(tomb_message, "Killed on Level %d", dead_ptr->current_floor_ptr->dun_level);
184     center_string(buf, tomb_message);
185     put_str(buf, 14, 11);
186
187     shape_buffer(format("by %s.", dead_ptr->died_from), GRAVE_LINE_WIDTH + 1, tomb_message, sizeof(tomb_message));
188     center_string(buf, tomb_message);
189     char *t;
190     put_str(buf, 15, 11);
191     t = tomb_message + strlen(tomb_message) + 1;
192     if (!*t)
193         return;
194
195     char killer[MAX_MONSTER_NAME];
196     strcpy(killer, t); /* 2nd line */
197     if (*(t + strlen(t) + 1)) /* Does 3rd line exist? */
198     {
199         int dummy_len = strlen(killer);
200         strcpy(killer + MIN(dummy_len, GRAVE_LINE_WIDTH - 3), "...");
201     }
202
203     center_string(buf, killer);
204     put_str(buf, 16, 11);
205 }
206 #endif
207
208 /*!
209  * @brief 墓石のアスキーアート表示 /
210  * Display a "tomb-stone"
211  * @param creature_ptr プレーヤーへの参照ポインタ
212  */
213 void print_tomb(player_type *dead_ptr)
214 {
215     term_clear();
216     char buf[1024];
217     read_dead_file(buf, sizeof(buf));
218     concptr p = (current_world_ptr->total_winner || (dead_ptr->lev > PY_MAX_LEVEL)) ? _("偉大なる者", "Magnificent")
219                                                                                     : player_title[dead_ptr->pclass][(dead_ptr->lev - 1) / 5];
220
221     center_string(buf, dead_ptr->name);
222     put_str(buf, 6, 11);
223
224 #ifdef JP
225 #else
226     center_string(buf, "the");
227     put_str(buf, 7, 11);
228 #endif
229
230     center_string(buf, p);
231     put_str(buf, 8, 11);
232
233     center_string(buf, cp_ptr->title);
234     put_str(buf, 10, 11);
235
236     show_basic_params(dead_ptr, buf);
237     show_tomb_detail(dead_ptr, buf);
238
239     char current_time[80];
240     time_t ct = time((time_t *)0);
241     (void)sprintf(current_time, "%-.24s", ctime(&ct));
242     center_string(buf, current_time);
243     put_str(buf, 17, 11);
244     msg_format(_("さようなら、%s!", "Goodbye, %s!"), dead_ptr->name);
245 }
246
247 /*!
248  * @brief 死亡/引退/切腹時にインベントリ内のアイテムを*鑑定*する
249  * @param creature_ptr プレーヤーへの参照ポインタ
250  */
251 static void inventory_aware(player_type *creature_ptr)
252 {
253     object_type *o_ptr;
254     for (int i = 0; i < INVEN_TOTAL; i++) {
255         o_ptr = &creature_ptr->inventory_list[i];
256         if (!o_ptr->k_idx)
257             continue;
258
259         object_aware(creature_ptr, o_ptr);
260         object_known(o_ptr);
261     }
262 }
263
264 /*!
265  * @brief 死亡/引退/切腹時に我が家のアイテムを*鑑定*する
266  * @param creature_ptr プレーヤーへの参照ポインタ
267  */
268 static void home_aware(player_type *creature_ptr)
269 {
270     object_type *o_ptr;
271     store_type *store_ptr;
272     for (int i = 1; i < max_towns; i++) {
273         store_ptr = &town_info[i].store[STORE_HOME];
274         for (int j = 0; j < store_ptr->stock_num; j++) {
275             o_ptr = &store_ptr->stock[j];
276             if (!o_ptr->k_idx)
277                 continue;
278
279             object_aware(creature_ptr, o_ptr);
280             object_known(o_ptr);
281         }
282     }
283 }
284
285 /*!
286  * @brief プレーヤーの持ち物を表示する
287  * @param creature_ptr プレーヤーへの参照ポインタ
288  * @return Escキーでゲームを終了する時TRUE
289  */
290 static bool show_dead_player_items(player_type *creature_ptr)
291 {
292     if (creature_ptr->equip_cnt) {
293         term_clear();
294         (void)show_equipment(creature_ptr, 0, USE_FULL, AllMatchItemTester());
295         prt(_("装備していたアイテム: -続く-", "You are using: -more-"), 0, 0);
296         if (inkey() == ESCAPE)
297             return true;
298     }
299
300     if (creature_ptr->inven_cnt) {
301         term_clear();
302         (void)show_inventory(creature_ptr, 0, USE_FULL, AllMatchItemTester());
303         prt(_("持っていたアイテム: -続く-", "You are carrying: -more-"), 0, 0);
304
305         if (inkey() == ESCAPE)
306             return true;
307     }
308
309     return false;
310 }
311
312 /*!
313  * @brief 我が家にあったアイテムを表示する
314  * @param creature_ptr プレーヤーへの参照ポインタ
315  */
316 static void show_dead_home_items(player_type *creature_ptr)
317 {
318     for (int l = 1; l < max_towns; l++) {
319         store_type *store_ptr;
320         store_ptr = &town_info[l].store[STORE_HOME];
321         if (store_ptr->stock_num == 0)
322             continue;
323
324         for (int i = 0, k = 0; i < store_ptr->stock_num; k++) {
325             term_clear();
326             for (int j = 0; (j < 12) && (i < store_ptr->stock_num); j++, i++) {
327                 GAME_TEXT o_name[MAX_NLEN];
328                 char tmp_val[80];
329                 object_type *o_ptr;
330                 o_ptr = &store_ptr->stock[i];
331                 sprintf(tmp_val, "%c) ", I2A(j));
332                 prt(tmp_val, j + 2, 4);
333                 describe_flavor(creature_ptr, o_name, o_ptr, 0);
334                 c_put_str(tval_to_attr[o_ptr->tval], o_name, j + 2, 7);
335             }
336
337             prt(format(_("我が家に置いてあったアイテム ( %d ページ): -続く-", "Your home contains (page %d): -more-"), k + 1), 0, 0);
338             if (inkey() == ESCAPE)
339                 return;
340         }
341     }
342 }
343
344 /*!
345  * @brief キャラクタ情報をファイルに書き出す
346  * @param creature_ptr プレーヤーへの参照ポインタ
347  * @param file_character ステータスダンプへのコールバック
348  */
349 static void export_player_info(player_type *creature_ptr, display_player_pf display_player)
350 {
351     prt(_("キャラクターの記録をファイルに書き出すことができます。", "You may now dump a character record to one or more files."), 21, 0);
352     prt(_("リターンキーでキャラクターを見ます。ESCで中断します。", "Then, hit RETURN to see the character, or ESC to abort."), 22, 0);
353     while (true) {
354         char out_val[160];
355         put_str(_("ファイルネーム: ", "Filename: "), 23, 0);
356         strcpy(out_val, "");
357         if (!askfor(out_val, 60))
358             return;
359         if (!out_val[0])
360             break;
361
362         screen_save();
363         (void)file_character(creature_ptr, out_val, display_player);
364         screen_load();
365     }
366 }
367
368 /*!
369  * @brief 自動的にプレイヤーステータスをファイルダンプ出力する
370  */
371 static void file_character_auto(player_type *creature_ptr, display_player_pf display_player)
372 {
373     time_t now_t = time(nullptr);
374     struct tm *now_tm = localtime(&now_t);
375
376     char datetime[32];
377     char filename[128];
378
379     strftime(datetime, sizeof(datetime), "%Y-%m-%d_%H%M%S", now_tm);
380     strnfmt(filename, sizeof(filename), "%s_Autodump_%s.txt", p_ptr->name, datetime);
381
382     screen_save();
383     (void)file_character(creature_ptr, filename, display_player);
384     screen_load();
385 }
386
387 /*!
388  * @brief 死亡、引退時の簡易ステータス表示
389  * @param creature_ptr プレーヤーへの参照ポインタ
390  * @param display_player ステータス表示へのコールバック
391  */
392 void show_death_info(player_type *creature_ptr, display_player_pf display_player)
393 {
394     inventory_aware(creature_ptr);
395     home_aware(creature_ptr);
396
397     creature_ptr->update |= (PU_BONUS);
398     handle_stuff(creature_ptr);
399     flush();
400     msg_erase();
401
402     if (auto_dump)
403         file_character_auto(creature_ptr, display_player);
404
405     export_player_info(creature_ptr, display_player);
406     (*display_player)(creature_ptr, 0);
407     prt(_("何かキーを押すとさらに情報が続きます (ESCで中断): ", "Hit any key to see more information (ESC to abort): "), 23, 0);
408     if (inkey() == ESCAPE)
409         return;
410     if (show_dead_player_items(creature_ptr))
411         return;
412
413     show_dead_home_items(creature_ptr);
414 }