OSDN Git Service

1a3a027c063cd206ef111fb568f2fa8daafa9bdf
[hengbandforosx/hengbandosx.git] / src / knowledge / knowledge-monsters.c
1 /*!
2  * todo サブルーチン分割を行うと行数が膨れ上がりそう、再分割も検討すべし
3  * @brief 既知のモンスターに関する情報を表示する
4  * @date 2020/04/24
5  * @author Hourier
6  */
7
8 #include "knowledge/knowledge-monsters.h"
9 #include "core/show-file.h"
10 #include "core/stuff-handler.h"
11 #include "game-option/cheat-options.h"
12 #include "game-option/special-options.h"
13 #include "io-dump/dump-util.h"
14 #include "io/input-key-acceptor.h"
15 #include "knowledge/monster-group-table.h"
16 #include "locale/english.h"
17 #include "monster-race/monster-race.h"
18 #include "monster-race/race-flags1.h"
19 #include "monster-race/race-flags3.h"
20 #include "monster-race/race-flags7.h"
21 #include "monster/monster-describer.h"
22 #include "monster/monster-description-types.h"
23 #include "monster/monster-info.h"
24 #include "monster/monster-status.h"
25 #include "monster/smart-learn-types.h"
26 #include "system/floor-type-definition.h"
27 #include "term/screen-processor.h"
28 #include "term/term-color-types.h"
29 #include "util/angband-files.h"
30 #include "util/bit-flags-calculator.h"
31 #include "util/int-char-converter.h"
32 #include "util/sort.h"
33 #include "util/string-processor.h"
34 #include "view/display-lore.h"
35 #include "view/display-monster-status.h"
36 #include "world/world.h"
37
38 /*
39  * todo 引数と戻り値について追記求む
40  * Build a list of monster indexes in the given group.
41  *
42  * mode & 0x01 : check for non-empty group
43  * mode & 0x02 : visual operation only
44
45  * @param creature_ptr プレーヤーへの参照ポインタ
46  * @param grp_cur ???
47  * @param mon_idx[] ???
48  * @param mode ???
49  * @return The number of monsters in the group
50  */
51 static IDX collect_monsters(player_type *creature_ptr, IDX grp_cur, IDX mon_idx[], BIT_FLAGS8 mode)
52 {
53     concptr group_char = monster_group_char[grp_cur];
54     bool grp_unique = (monster_group_char[grp_cur] == (char *)-1L);
55     bool grp_riding = (monster_group_char[grp_cur] == (char *)-2L);
56     bool grp_wanted = (monster_group_char[grp_cur] == (char *)-3L);
57     bool grp_amberite = (monster_group_char[grp_cur] == (char *)-4L);
58
59     IDX mon_cnt = 0;
60     for (IDX i = 0; i < max_r_idx; i++) {
61         monster_race *r_ptr = &r_info[i];
62         if (!r_ptr->name)
63             continue;
64         if ((mode != MONSTER_LORE_DEBUG) && !cheat_know && !r_ptr->r_sights)
65             continue;
66
67         if (grp_unique) {
68             if (none_bits(r_ptr->flags1, RF1_UNIQUE))
69                 continue;
70         } else if (grp_riding) {
71             if (none_bits(r_ptr->flags7, RF7_RIDING))
72                 continue;
73         } else if (grp_wanted) {
74             bool wanted = FALSE;
75             for (int j = 0; j < MAX_BOUNTY; j++) {
76                 if (current_world_ptr->bounty_r_idx[j] == i || current_world_ptr->bounty_r_idx[j] - 10000 == i
77                     || (creature_ptr->today_mon && creature_ptr->today_mon == i)) {
78                     wanted = TRUE;
79                     break;
80                 }
81             }
82
83             if (!wanted)
84                 continue;
85         } else if (grp_amberite) {
86             if (none_bits(r_ptr->flags3, RF3_AMBERITE))
87                 continue;
88         } else {
89             if (!angband_strchr(group_char, r_ptr->d_char))
90                 continue;
91         }
92
93         mon_idx[mon_cnt++] = i;
94         if (mode == MONSTER_LORE_NORMAL)
95             break;
96     }
97
98     mon_idx[mon_cnt] = -1;
99     int dummy_why;
100     ang_sort(creature_ptr, mon_idx, &dummy_why, mon_cnt, ang_sort_comp_monster_level, ang_sort_swap_hook);
101     return mon_cnt;
102 }
103
104 /*!
105  * @brief 現在のペットを表示するコマンドのメインルーチン /
106  * Display current pets
107  * @param creature_ptr プレーヤーへの参照ポインタ
108  * @return なし
109  */
110 void do_cmd_knowledge_pets(player_type *creature_ptr)
111 {
112     FILE *fff = NULL;
113     GAME_TEXT file_name[FILE_NAME_SIZE];
114     if (!open_temporary_file(&fff, file_name))
115         return;
116
117     monster_type *m_ptr;
118     GAME_TEXT pet_name[MAX_NLEN];
119     int t_friends = 0;
120     for (int i = creature_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
121         m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
122         if (!monster_is_valid(m_ptr) || !is_pet(m_ptr))
123             continue;
124
125         t_friends++;
126         monster_desc(creature_ptr, pet_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
127         fprintf(fff, "%s (%s)\n", pet_name, look_mon_desc(m_ptr, 0x00));
128     }
129
130     int show_upkeep = calculate_upkeep(creature_ptr);
131
132     fprintf(fff, "----------------------------------------------\n");
133 #ifdef JP
134     fprintf(fff, "    合計: %d 体のペット\n", t_friends);
135 #else
136     fprintf(fff, "   Total: %d pet%s.\n", t_friends, (t_friends == 1 ? "" : "s"));
137 #endif
138     fprintf(fff, _(" 維持コスト: %d%% MP\n", "   Upkeep: %d%% mana.\n"), show_upkeep);
139
140     angband_fclose(fff);
141     (void)show_file(creature_ptr, TRUE, file_name, _("現在のペット", "Current Pets"), 0, 0);
142     fd_kill(file_name);
143 }
144
145 /*!
146  * @brief 現在までに倒したモンスターを表示するコマンドのメインルーチン /
147  * @param creature_ptr プレーヤーへの参照ポインタ
148  * Total kill count
149  * @return なし
150  * @note the player ghosts are ignored.
151  */
152 void do_cmd_knowledge_kill_count(player_type *creature_ptr)
153 {
154     FILE *fff = NULL;
155     GAME_TEXT file_name[FILE_NAME_SIZE];
156     if (!open_temporary_file(&fff, file_name))
157         return;
158
159     MONRACE_IDX *who;
160     C_MAKE(who, max_r_idx, MONRACE_IDX);
161     s32b total = 0;
162     for (int kk = 1; kk < max_r_idx; kk++) {
163         monster_race *r_ptr = &r_info[kk];
164
165         if (any_bits(r_ptr->flags1, RF1_UNIQUE)) {
166             bool dead = (r_ptr->max_num == 0);
167
168             if (dead) {
169                 total++;
170             }
171         } else {
172             MONSTER_NUMBER this_monster = r_ptr->r_pkills;
173
174             if (this_monster > 0) {
175                 total += this_monster;
176             }
177         }
178     }
179
180     if (total < 1)
181         fprintf(fff, _("あなたはまだ敵を倒していない。\n\n", "You have defeated no enemies yet.\n\n"));
182     else
183 #ifdef JP
184         fprintf(fff, "あなたは%ld体の敵を倒している。\n\n", (long int)total);
185 #else
186         fprintf(fff, "You have defeated %ld %s.\n\n", (long int)total, (total == 1) ? "enemy" : "enemies");
187 #endif
188
189     total = 0;
190     int n = 0;
191     for (MONRACE_IDX i = 1; i < max_r_idx; i++) {
192         monster_race *r_ptr = &r_info[i];
193         if (r_ptr->name)
194             who[n++] = i;
195     }
196
197     u16b why = 2;
198     ang_sort(creature_ptr, who, &why, n, ang_sort_comp_hook, ang_sort_swap_hook);
199     for (int k = 0; k < n; k++) {
200         monster_race *r_ptr = &r_info[who[k]];
201         if (any_bits(r_ptr->flags1, RF1_UNIQUE)) {
202             bool dead = (r_ptr->max_num == 0);
203             if (dead) {
204                 fprintf(fff, "     %s\n", (r_name + r_ptr->name));
205                 total++;
206             }
207
208             continue;
209         }
210
211         MONSTER_NUMBER this_monster = r_ptr->r_pkills;
212         if (this_monster <= 0)
213             continue;
214
215 #ifdef JP
216         char *number_of_kills = angband_strchr("pt", r_ptr->d_char) ? "人" : "体";
217         fprintf(fff, "     %3d %sの %s\n", (int)this_monster, number_of_kills, r_name + r_ptr->name);
218 #else
219         if (this_monster < 2) {
220             if (angband_strstr(r_name + r_ptr->name, "coins")) {
221                 fprintf(fff, "     1 pile of %s\n", (r_name + r_ptr->name));
222             } else {
223                 fprintf(fff, "     1 %s\n", (r_name + r_ptr->name));
224             }
225         } else {
226             char ToPlural[80];
227             strcpy(ToPlural, (r_name + r_ptr->name));
228             plural_aux(ToPlural);
229             fprintf(fff, "     %d %s\n", this_monster, ToPlural);
230         }
231 #endif
232         total += this_monster;
233     }
234
235     fprintf(fff, "----------------------------------------------\n");
236 #ifdef JP
237     fprintf(fff, "    合計: %lu 体を倒した。\n", (unsigned long int)total);
238 #else
239     fprintf(fff, "   Total: %lu creature%s killed.\n", (unsigned long int)total, (total == 1 ? "" : "s"));
240 #endif
241
242     C_KILL(who, max_r_idx, s16b);
243     angband_fclose(fff);
244     (void)show_file(creature_ptr, TRUE, file_name, _("倒した敵の数", "Kill Count"), 0, 0);
245     fd_kill(file_name);
246 }
247
248 /*
249  * Display the monsters in a group.
250  */
251 static void display_monster_list(int col, int row, int per_page, s16b mon_idx[], int mon_cur, int mon_top, bool visual_only)
252 {
253     int i;
254     for (i = 0; i < per_page && (mon_idx[mon_top + i] >= 0); i++) {
255         TERM_COLOR attr;
256         MONRACE_IDX r_idx = mon_idx[mon_top + i];
257         monster_race *r_ptr = &r_info[r_idx];
258         attr = ((i + mon_top == mon_cur) ? TERM_L_BLUE : TERM_WHITE);
259         c_prt(attr, (r_name + r_ptr->name), row + i, col);
260         if (per_page == 1)
261             c_prt(attr, format("%02x/%02x", r_ptr->x_attr, r_ptr->x_char), row + i, (current_world_ptr->wizard || visual_only) ? 56 : 61);
262
263         if (current_world_ptr->wizard || visual_only)
264             c_prt(attr, format("%d", r_idx), row + i, 62);
265
266         term_erase(69, row + i, 255);
267         term_queue_bigchar(use_bigtile ? 69 : 70, row + i, r_ptr->x_attr, r_ptr->x_char, 0, 0);
268         if (!visual_only) {
269             if (none_bits(r_ptr->flags1, RF1_UNIQUE))
270                 put_str(format("%5d", r_ptr->r_pkills), row + i, 73);
271             else
272                 c_put_str((r_ptr->max_num == 0 ? TERM_L_DARK : TERM_WHITE), (r_ptr->max_num == 0 ? _("死亡", " dead") : _("生存", "alive")), row + i, 74);
273         }
274     }
275
276     for (; i < per_page; i++) {
277         term_erase(col, row + i, 255);
278     }
279 }
280
281 /*
282  * todo 引数の詳細について加筆求む
283  * Display known monsters.
284  * @param creature_ptr プレーヤーへの参照ポインタ
285  * @param need_redraw 画面の再描画が必要な時TRUE
286  * @param visual_only ???
287  * @param direct_r_idx モンスターID
288  * @return なし
289  */
290 void do_cmd_knowledge_monsters(player_type *creature_ptr, bool *need_redraw, bool visual_only, IDX direct_r_idx)
291 {
292     TERM_LEN wid, hgt;
293     term_get_size(&wid, &hgt);
294     IDX *mon_idx;
295     C_MAKE(mon_idx, max_r_idx, MONRACE_IDX);
296
297     int max = 0;
298     IDX grp_cnt = 0;
299     IDX grp_idx[100];
300     IDX mon_cnt;
301     bool visual_list = FALSE;
302     TERM_COLOR attr_top = 0;
303     byte char_left = 0;
304     BIT_FLAGS8 mode;
305     int browser_rows = hgt - 8;
306     if (direct_r_idx < 0) {
307         mode = visual_only ? 0x03 : 0x01;
308         int len;
309         for (IDX i = 0; monster_group_text[i] != NULL; i++) {
310             len = strlen(monster_group_text[i]);
311             if (len > max)
312                 max = len;
313
314             if ((monster_group_char[i] == ((char *)-1L)) || collect_monsters(creature_ptr, i, mon_idx, mode)) {
315                 grp_idx[grp_cnt++] = i;
316             }
317         }
318
319         mon_cnt = 0;
320     } else {
321         mon_idx[0] = direct_r_idx;
322         mon_cnt = 1;
323         mon_idx[1] = -1;
324
325         (void)visual_mode_command('v', &visual_list, browser_rows - 1, wid - (max + 3), &attr_top, &char_left, &r_info[direct_r_idx].x_attr,
326             &r_info[direct_r_idx].x_char, need_redraw);
327     }
328
329     grp_idx[grp_cnt] = -1;
330     mode = visual_only ? 0x02 : 0x00;
331     IDX old_grp_cur = -1;
332     IDX grp_cur = 0;
333     IDX grp_top = 0;
334     IDX mon_cur = 0;
335     IDX mon_top = 0;
336     int column = 0;
337     bool flag = FALSE;
338     bool redraw = TRUE;
339     while (!flag) {
340         if (redraw) {
341             clear_from(0);
342             prt(format(_("%s - モンスター", "%s - monsters"), !visual_only ? _("知識", "Knowledge") : _("表示", "Visuals")), 2, 0);
343             if (direct_r_idx < 0)
344                 prt(_("グループ", "Group"), 4, 0);
345             prt(_("名前", "Name"), 4, max + 3);
346             if (current_world_ptr->wizard || visual_only)
347                 prt("Idx", 4, 62);
348             prt(_("文字", "Sym"), 4, 67);
349             if (!visual_only)
350                 prt(_("殺害数", "Kills"), 4, 72);
351
352             for (IDX i = 0; i < 78; i++) {
353                 term_putch(i, 5, TERM_WHITE, '=');
354             }
355
356             if (direct_r_idx < 0) {
357                 for (IDX i = 0; i < browser_rows; i++) {
358                     term_putch(max + 1, 6 + i, TERM_WHITE, '|');
359                 }
360             }
361
362             redraw = FALSE;
363         }
364
365         if (direct_r_idx < 0) {
366             if (grp_cur < grp_top)
367                 grp_top = grp_cur;
368             if (grp_cur >= grp_top + browser_rows)
369                 grp_top = grp_cur - browser_rows + 1;
370
371             display_group_list(0, 6, max, browser_rows, grp_idx, monster_group_text, grp_cur, grp_top);
372             if (old_grp_cur != grp_cur) {
373                 old_grp_cur = grp_cur;
374                 mon_cnt = collect_monsters(creature_ptr, grp_idx[grp_cur], mon_idx, mode);
375             }
376
377             while (mon_cur < mon_top)
378                 mon_top = MAX(0, mon_top - browser_rows / 2);
379             while (mon_cur >= mon_top + browser_rows)
380                 mon_top = MIN(mon_cnt - browser_rows, mon_top + browser_rows / 2);
381         }
382
383         if (!visual_list) {
384             display_monster_list(max + 3, 6, browser_rows, mon_idx, mon_cur, mon_top, visual_only);
385         } else {
386             mon_top = mon_cur;
387             display_monster_list(max + 3, 6, 1, mon_idx, mon_cur, mon_top, visual_only);
388             display_visual_list(max + 3, 7, browser_rows - 1, wid - (max + 3), attr_top, char_left);
389         }
390
391         prt(format(_("<方向>%s%s%s, ESC", "<dir>%s%s%s, ESC"), (!visual_list && !visual_only) ? _(", 'r'で思い出を見る", ", 'r' to recall") : "",
392                 visual_list ? _(", ENTERで決定", ", ENTER to accept") : _(", 'v'でシンボル変更", ", 'v' for visuals"),
393                 (attr_idx || char_idx) ? _(", 'c', 'p'でペースト", ", 'c', 'p' to paste") : _(", 'c'でコピー", ", 'c' to copy")),
394             hgt - 1, 0);
395
396         monster_race *r_ptr;
397         r_ptr = &r_info[mon_idx[mon_cur]];
398
399         if (!visual_only) {
400             if (mon_cnt)
401                 monster_race_track(creature_ptr, mon_idx[mon_cur]);
402             handle_stuff(creature_ptr);
403         }
404
405         if (visual_list) {
406             place_visual_list_cursor(max + 3, 7, r_ptr->x_attr, r_ptr->x_char, attr_top, char_left);
407         } else if (!column) {
408             term_gotoxy(0, 6 + (grp_cur - grp_top));
409         } else {
410             term_gotoxy(max + 3, 6 + (mon_cur - mon_top));
411         }
412
413         char ch = inkey();
414         if (visual_mode_command(ch, &visual_list, browser_rows - 1, wid - (max + 3), &attr_top, &char_left, &r_ptr->x_attr, &r_ptr->x_char, need_redraw)) {
415             if (direct_r_idx >= 0) {
416                 switch (ch) {
417                 case '\n':
418                 case '\r':
419                 case ESCAPE:
420                     flag = TRUE;
421                     break;
422                 }
423             }
424
425             continue;
426         }
427
428         switch (ch) {
429         case ESCAPE: {
430             flag = TRUE;
431             break;
432         }
433
434         case 'R':
435         case 'r': {
436             if (!visual_list && !visual_only && (mon_idx[mon_cur] > 0)) {
437                 screen_roff(creature_ptr, mon_idx[mon_cur], MONSTER_LORE_NORMAL);
438
439                 (void)inkey();
440
441                 redraw = TRUE;
442             }
443
444             break;
445         }
446
447         default: {
448             browser_cursor(ch, &column, &grp_cur, grp_cnt, &mon_cur, mon_cnt);
449
450             break;
451         }
452         }
453     }
454
455     C_KILL(mon_idx, max_r_idx, MONRACE_IDX);
456 }
457
458 /*
459  * List wanted monsters
460  * @param creature_ptr プレーヤーへの参照ポインタ
461  * @return なし
462  */
463 void do_cmd_knowledge_bounty(player_type *creature_ptr)
464 {
465     FILE *fff = NULL;
466     GAME_TEXT file_name[FILE_NAME_SIZE];
467     if (!open_temporary_file(&fff, file_name))
468         return;
469
470     fprintf(fff, _("今日のターゲット : %s\n", "Today's target : %s\n"),
471         (creature_ptr->today_mon ? r_name + r_info[creature_ptr->today_mon].name : _("不明", "unknown")));
472     fprintf(fff, "\n");
473     fprintf(fff, _("賞金首リスト\n", "List of wanted monsters\n"));
474     fprintf(fff, "----------------------------------------------\n");
475
476     bool listed = FALSE;
477     for (int i = 0; i < MAX_BOUNTY; i++) {
478         if (current_world_ptr->bounty_r_idx[i] <= 10000) {
479             fprintf(fff, "%s\n", r_name + r_info[current_world_ptr->bounty_r_idx[i]].name);
480             listed = TRUE;
481         }
482     }
483
484     if (!listed)
485         fprintf(fff, "\n%s\n", _("賞金首はもう残っていません。", "There are no more wanted monster."));
486
487     angband_fclose(fff);
488     (void)show_file(creature_ptr, TRUE, file_name, _("賞金首の一覧", "Wanted monsters"), 0, 0);
489     fd_kill(file_name);
490 }