OSDN Git Service

[feature] ソースファイルをC++に対応
[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[], monster_lore_mode 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) && (mode != MONSTER_LORE_RESEARCH)) && !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         if (mode == MONSTER_LORE_DEBUG)
97             break;
98     }
99
100     mon_idx[mon_cnt] = -1;
101     int dummy_why;
102     ang_sort(creature_ptr, mon_idx, &dummy_why, mon_cnt, ang_sort_comp_monster_level, ang_sort_swap_hook);
103     return mon_cnt;
104 }
105
106 /*!
107  * @brief 現在のペットを表示するコマンドのメインルーチン /
108  * Display current pets
109  * @param creature_ptr プレーヤーへの参照ポインタ
110  * @return なし
111  */
112 void do_cmd_knowledge_pets(player_type *creature_ptr)
113 {
114     FILE *fff = NULL;
115     GAME_TEXT file_name[FILE_NAME_SIZE];
116     if (!open_temporary_file(&fff, file_name))
117         return;
118
119     monster_type *m_ptr;
120     GAME_TEXT pet_name[MAX_NLEN];
121     int t_friends = 0;
122     for (int i = creature_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
123         m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
124         if (!monster_is_valid(m_ptr) || !is_pet(m_ptr))
125             continue;
126
127         t_friends++;
128         monster_desc(creature_ptr, pet_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
129         fprintf(fff, "%s (%s)\n", pet_name, look_mon_desc(m_ptr, 0x00));
130     }
131
132     int show_upkeep = calculate_upkeep(creature_ptr);
133
134     fprintf(fff, "----------------------------------------------\n");
135 #ifdef JP
136     fprintf(fff, "    合計: %d 体のペット\n", t_friends);
137 #else
138     fprintf(fff, "   Total: %d pet%s.\n", t_friends, (t_friends == 1 ? "" : "s"));
139 #endif
140     fprintf(fff, _(" 維持コスト: %d%% MP\n", "   Upkeep: %d%% mana.\n"), show_upkeep);
141
142     angband_fclose(fff);
143     (void)show_file(creature_ptr, TRUE, file_name, _("現在のペット", "Current Pets"), 0, 0);
144     fd_kill(file_name);
145 }
146
147 /*!
148  * @brief 現在までに倒したモンスターを表示するコマンドのメインルーチン /
149  * @param creature_ptr プレーヤーへの参照ポインタ
150  * Total kill count
151  * @return なし
152  * @note the player ghosts are ignored.
153  */
154 void do_cmd_knowledge_kill_count(player_type *creature_ptr)
155 {
156     FILE *fff = NULL;
157     GAME_TEXT file_name[FILE_NAME_SIZE];
158     if (!open_temporary_file(&fff, file_name))
159         return;
160
161     MONRACE_IDX *who;
162     C_MAKE(who, max_r_idx, MONRACE_IDX);
163     s32b total = 0;
164     for (int kk = 1; kk < max_r_idx; kk++) {
165         monster_race *r_ptr = &r_info[kk];
166
167         if (any_bits(r_ptr->flags1, RF1_UNIQUE)) {
168             bool dead = (r_ptr->max_num == 0);
169
170             if (dead) {
171                 total++;
172             }
173         } else {
174             MONSTER_NUMBER this_monster = r_ptr->r_pkills;
175
176             if (this_monster > 0) {
177                 total += this_monster;
178             }
179         }
180     }
181
182     if (total < 1)
183         fprintf(fff, _("あなたはまだ敵を倒していない。\n\n", "You have defeated no enemies yet.\n\n"));
184     else
185 #ifdef JP
186         fprintf(fff, "あなたは%ld体の敵を倒している。\n\n", (long int)total);
187 #else
188         fprintf(fff, "You have defeated %ld %s.\n\n", (long int)total, (total == 1) ? "enemy" : "enemies");
189 #endif
190
191     total = 0;
192     int n = 0;
193     for (MONRACE_IDX i = 1; i < max_r_idx; i++) {
194         monster_race *r_ptr = &r_info[i];
195         if (r_ptr->name)
196             who[n++] = i;
197     }
198
199     u16b why = 2;
200     ang_sort(creature_ptr, who, &why, n, ang_sort_comp_hook, ang_sort_swap_hook);
201     for (int k = 0; k < n; k++) {
202         monster_race *r_ptr = &r_info[who[k]];
203         if (any_bits(r_ptr->flags1, RF1_UNIQUE)) {
204             bool dead = (r_ptr->max_num == 0);
205             if (dead) {
206                 fprintf(fff, "     %s\n", (r_name + r_ptr->name));
207                 total++;
208             }
209
210             continue;
211         }
212
213         MONSTER_NUMBER this_monster = r_ptr->r_pkills;
214         if (this_monster <= 0)
215             continue;
216
217 #ifdef JP
218         concptr number_of_kills = angband_strchr("pt", r_ptr->d_char) ? "人" : "体";
219         fprintf(fff, "     %3d %sの %s\n", (int)this_monster, number_of_kills, r_name + r_ptr->name);
220 #else
221         if (this_monster < 2) {
222             if (angband_strstr(r_name + r_ptr->name, "coins")) {
223                 fprintf(fff, "     1 pile of %s\n", (r_name + r_ptr->name));
224             } else {
225                 fprintf(fff, "     1 %s\n", (r_name + r_ptr->name));
226             }
227         } else {
228             char ToPlural[80];
229             strcpy(ToPlural, (r_name + r_ptr->name));
230             plural_aux(ToPlural);
231             fprintf(fff, "     %d %s\n", this_monster, ToPlural);
232         }
233 #endif
234         total += this_monster;
235     }
236
237     fprintf(fff, "----------------------------------------------\n");
238 #ifdef JP
239     fprintf(fff, "    合計: %lu 体を倒した。\n", (unsigned long int)total);
240 #else
241     fprintf(fff, "   Total: %lu creature%s killed.\n", (unsigned long int)total, (total == 1 ? "" : "s"));
242 #endif
243
244     C_KILL(who, max_r_idx, s16b);
245     angband_fclose(fff);
246     (void)show_file(creature_ptr, TRUE, file_name, _("倒した敵の数", "Kill Count"), 0, 0);
247     fd_kill(file_name);
248 }
249
250 /*
251  * Display the monsters in a group.
252  */
253 static void display_monster_list(int col, int row, int per_page, s16b mon_idx[], int mon_cur, int mon_top, bool visual_only)
254 {
255     int i;
256     for (i = 0; i < per_page && (mon_idx[mon_top + i] >= 0); i++) {
257         TERM_COLOR attr;
258         MONRACE_IDX r_idx = mon_idx[mon_top + i];
259         monster_race *r_ptr = &r_info[r_idx];
260         attr = ((i + mon_top == mon_cur) ? TERM_L_BLUE : TERM_WHITE);
261         c_prt(attr, (r_name + r_ptr->name), row + i, col);
262         if (per_page == 1)
263             c_prt(attr, format("%02x/%02x", r_ptr->x_attr, r_ptr->x_char), row + i, (current_world_ptr->wizard || visual_only) ? 56 : 61);
264
265         if (current_world_ptr->wizard || visual_only)
266             c_prt(attr, format("%d", r_idx), row + i, 62);
267
268         term_erase(69, row + i, 255);
269         term_queue_bigchar(use_bigtile ? 69 : 70, row + i, r_ptr->x_attr, r_ptr->x_char, 0, 0);
270         if (!visual_only) {
271             if (none_bits(r_ptr->flags1, RF1_UNIQUE))
272                 put_str(format("%5d", r_ptr->r_pkills), row + i, 73);
273             else
274                 c_put_str((r_ptr->max_num == 0 ? TERM_L_DARK : TERM_WHITE), (r_ptr->max_num == 0 ? _("死亡", " dead") : _("生存", "alive")), row + i, 74);
275         }
276     }
277
278     for (; i < per_page; i++) {
279         term_erase(col, row + i, 255);
280     }
281 }
282
283 /*
284  * todo 引数の詳細について加筆求む
285  * Display known monsters.
286  * @param creature_ptr プレーヤーへの参照ポインタ
287  * @param need_redraw 画面の再描画が必要な時TRUE
288  * @param visual_only ???
289  * @param direct_r_idx モンスターID
290  * @return なし
291  */
292 void do_cmd_knowledge_monsters(player_type *creature_ptr, bool *need_redraw, bool visual_only, IDX direct_r_idx)
293 {
294     TERM_LEN wid, hgt;
295     term_get_size(&wid, &hgt);
296     IDX *mon_idx;
297     C_MAKE(mon_idx, max_r_idx, MONRACE_IDX);
298
299     int max = 0;
300     IDX grp_cnt = 0;
301     IDX grp_idx[100];
302     IDX mon_cnt;
303     bool visual_list = FALSE;
304     TERM_COLOR attr_top = 0;
305     byte char_left = 0;
306     monster_lore_mode mode;
307     int browser_rows = hgt - 8;
308     if (direct_r_idx < 0) {
309         mode = visual_only ? MONSTER_LORE_DEBUG : MONSTER_LORE_NORMAL;
310         int len;
311         for (IDX i = 0; monster_group_text[i] != NULL; i++) {
312             len = strlen(monster_group_text[i]);
313             if (len > max)
314                 max = len;
315
316             if ((monster_group_char[i] == ((char *)-1L)) || collect_monsters(creature_ptr, i, mon_idx, mode)) {
317                 grp_idx[grp_cnt++] = i;
318             }
319         }
320
321         mon_cnt = 0;
322     } else {
323         mon_idx[0] = direct_r_idx;
324         mon_cnt = 1;
325         mon_idx[1] = -1;
326
327         (void)visual_mode_command('v', &visual_list, browser_rows - 1, wid - (max + 3), &attr_top, &char_left, &r_info[direct_r_idx].x_attr,
328             &r_info[direct_r_idx].x_char, need_redraw);
329     }
330
331     grp_idx[grp_cnt] = -1;
332     mode = visual_only ? MONSTER_LORE_RESEARCH : MONSTER_LORE_NONE;
333     IDX old_grp_cur = -1;
334     IDX grp_cur = 0;
335     IDX grp_top = 0;
336     IDX mon_cur = 0;
337     IDX mon_top = 0;
338     int column = 0;
339     bool flag = FALSE;
340     bool redraw = TRUE;
341     while (!flag) {
342         if (redraw) {
343             clear_from(0);
344             prt(format(_("%s - モンスター", "%s - monsters"), !visual_only ? _("知識", "Knowledge") : _("表示", "Visuals")), 2, 0);
345             if (direct_r_idx < 0)
346                 prt(_("グループ", "Group"), 4, 0);
347             prt(_("名前", "Name"), 4, max + 3);
348             if (current_world_ptr->wizard || visual_only)
349                 prt("Idx", 4, 62);
350             prt(_("文字", "Sym"), 4, 67);
351             if (!visual_only)
352                 prt(_("殺害数", "Kills"), 4, 72);
353
354             for (IDX i = 0; i < 78; i++) {
355                 term_putch(i, 5, TERM_WHITE, '=');
356             }
357
358             if (direct_r_idx < 0) {
359                 for (IDX i = 0; i < browser_rows; i++) {
360                     term_putch(max + 1, 6 + i, TERM_WHITE, '|');
361                 }
362             }
363
364             redraw = FALSE;
365         }
366
367         if (direct_r_idx < 0) {
368             if (grp_cur < grp_top)
369                 grp_top = grp_cur;
370             if (grp_cur >= grp_top + browser_rows)
371                 grp_top = grp_cur - browser_rows + 1;
372
373             display_group_list(0, 6, max, browser_rows, grp_idx, monster_group_text, grp_cur, grp_top);
374             if (old_grp_cur != grp_cur) {
375                 old_grp_cur = grp_cur;
376                 mon_cnt = collect_monsters(creature_ptr, grp_idx[grp_cur], mon_idx, mode);
377             }
378
379             while (mon_cur < mon_top)
380                 mon_top = MAX(0, mon_top - browser_rows / 2);
381             while (mon_cur >= mon_top + browser_rows)
382                 mon_top = MIN(mon_cnt - browser_rows, mon_top + browser_rows / 2);
383         }
384
385         if (!visual_list) {
386             display_monster_list(max + 3, 6, browser_rows, mon_idx, mon_cur, mon_top, visual_only);
387         } else {
388             mon_top = mon_cur;
389             display_monster_list(max + 3, 6, 1, mon_idx, mon_cur, mon_top, visual_only);
390             display_visual_list(max + 3, 7, browser_rows - 1, wid - (max + 3), attr_top, char_left);
391         }
392
393         prt(format(_("<方向>%s%s%s, ESC", "<dir>%s%s%s, ESC"), (!visual_list && !visual_only) ? _(", 'r'で思い出を見る", ", 'r' to recall") : "",
394                 visual_list ? _(", ENTERで決定", ", ENTER to accept") : _(", 'v'でシンボル変更", ", 'v' for visuals"),
395                 (attr_idx || char_idx) ? _(", 'c', 'p'でペースト", ", 'c', 'p' to paste") : _(", 'c'でコピー", ", 'c' to copy")),
396             hgt - 1, 0);
397
398         monster_race *r_ptr;
399         r_ptr = &r_info[mon_idx[mon_cur]];
400
401         if (!visual_only) {
402             if (mon_cnt)
403                 monster_race_track(creature_ptr, mon_idx[mon_cur]);
404             handle_stuff(creature_ptr);
405         }
406
407         if (visual_list) {
408             place_visual_list_cursor(max + 3, 7, r_ptr->x_attr, r_ptr->x_char, attr_top, char_left);
409         } else if (!column) {
410             term_gotoxy(0, 6 + (grp_cur - grp_top));
411         } else {
412             term_gotoxy(max + 3, 6 + (mon_cur - mon_top));
413         }
414
415         char ch = inkey();
416         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)) {
417             if (direct_r_idx >= 0) {
418                 switch (ch) {
419                 case '\n':
420                 case '\r':
421                 case ESCAPE:
422                     flag = TRUE;
423                     break;
424                 }
425             }
426
427             continue;
428         }
429
430         switch (ch) {
431         case ESCAPE: {
432             flag = TRUE;
433             break;
434         }
435
436         case 'R':
437         case 'r': {
438             if (!visual_list && !visual_only && (mon_idx[mon_cur] > 0)) {
439                 screen_roff(creature_ptr, mon_idx[mon_cur], MONSTER_LORE_NORMAL);
440
441                 (void)inkey();
442
443                 redraw = TRUE;
444             }
445
446             break;
447         }
448
449         default: {
450             browser_cursor(ch, &column, &grp_cur, grp_cnt, &mon_cur, mon_cnt);
451
452             break;
453         }
454         }
455     }
456
457     C_KILL(mon_idx, max_r_idx, MONRACE_IDX);
458 }
459
460 /*
461  * List wanted monsters
462  * @param creature_ptr プレーヤーへの参照ポインタ
463  * @return なし
464  */
465 void do_cmd_knowledge_bounty(player_type *creature_ptr)
466 {
467     FILE *fff = NULL;
468     GAME_TEXT file_name[FILE_NAME_SIZE];
469     if (!open_temporary_file(&fff, file_name))
470         return;
471
472     fprintf(fff, _("今日のターゲット : %s\n", "Today's target : %s\n"),
473         (creature_ptr->today_mon ? r_name + r_info[creature_ptr->today_mon].name : _("不明", "unknown")));
474     fprintf(fff, "\n");
475     fprintf(fff, _("賞金首リスト\n", "List of wanted monsters\n"));
476     fprintf(fff, "----------------------------------------------\n");
477
478     bool listed = FALSE;
479     for (int i = 0; i < MAX_BOUNTY; i++) {
480         if (current_world_ptr->bounty_r_idx[i] <= 10000) {
481             fprintf(fff, "%s\n", r_name + r_info[current_world_ptr->bounty_r_idx[i]].name);
482             listed = TRUE;
483         }
484     }
485
486     if (!listed)
487         fprintf(fff, "\n%s\n", _("賞金首はもう残っていません。", "There are no more wanted monster."));
488
489     angband_fclose(fff);
490     (void)show_file(creature_ptr, TRUE, file_name, _("賞金首の一覧", "Wanted monsters"), 0, 0);
491     fd_kill(file_name);
492 }