OSDN Git Service

Merge pull request #2810 from Hourier/Remove-KindObjectIndex
[hengbandforosx/hengbandosx.git] / src / knowledge / knowledge-features.cpp
1 /*!
2  * @brief 地形に関する情報を表示する
3  * @date 2020/04/24
4  * @author Hourier
5  */
6
7 #include "knowledge/knowledge-features.h"
8 #include "core/show-file.h"
9 #include "game-option/special-options.h"
10 #include "grid/feature.h"
11 #include "io-dump/dump-util.h"
12 #include "io/input-key-acceptor.h"
13 #include "knowledge/lighting-level-table.h"
14 #include "monster-race/monster-race.h"
15 #include "system/dungeon-info.h"
16 #include "system/monster-race-info.h"
17 #include "system/player-type-definition.h"
18 #include "system/terrain-type-definition.h"
19 #include "term/screen-processor.h"
20 #include "term/term-color-types.h"
21 #include "util/angband-files.h"
22 #include "util/int-char-converter.h"
23 #include "world/world.h"
24
25 /*
26  * Build a list of feature indexes in the given group. Return the number
27  * of features in the group.
28  *
29  * mode & 0x01 : check for non-empty group
30  */
31 static FEAT_IDX collect_features(FEAT_IDX *feat_idx, BIT_FLAGS8 mode)
32 {
33     FEAT_IDX feat_cnt = 0;
34     for (const auto &f_ref : terrains_info) {
35         if (f_ref.name.empty()) {
36             continue;
37         }
38         if (f_ref.mimic != f_ref.idx) {
39             continue;
40         }
41
42         feat_idx[feat_cnt++] = f_ref.idx;
43         if (mode & 0x01) {
44             break;
45         }
46     }
47
48     feat_idx[feat_cnt] = -1;
49     return feat_cnt;
50 }
51
52 /*
53  * Display the features in a group.
54  */
55 static void display_feature_list(int col, int row, int per_page, FEAT_IDX *feat_idx, FEAT_IDX feat_cur, FEAT_IDX feat_top, bool visual_only, int lighting_level)
56 {
57     int lit_col[F_LIT_MAX], i;
58     int f_idx_col = use_bigtile ? 62 : 64;
59
60     lit_col[F_LIT_STANDARD] = use_bigtile ? (71 - F_LIT_MAX) : 71;
61     for (i = F_LIT_NS_BEGIN; i < F_LIT_MAX; i++) {
62         lit_col[i] = lit_col[F_LIT_STANDARD] + 2 + (i - F_LIT_NS_BEGIN) * 2 + (use_bigtile ? i : 0);
63     }
64
65     for (i = 0; i < per_page && (feat_idx[feat_top + i] >= 0); i++) {
66         TERM_COLOR attr;
67         FEAT_IDX f_idx = feat_idx[feat_top + i];
68         auto *f_ptr = &terrains_info[f_idx];
69         int row_i = row + i;
70         attr = ((i + feat_top == feat_cur) ? TERM_L_BLUE : TERM_WHITE);
71         c_prt(attr, f_ptr->name.data(), row_i, col);
72         if (per_page == 1) {
73             c_prt(attr, format("(%s)", lighting_level_str[lighting_level]), row_i, col + 1 + f_ptr->name.size());
74             c_prt(attr, format("%02x/%02x", f_ptr->x_attr[lighting_level], (unsigned char)f_ptr->x_char[lighting_level]), row_i,
75                 f_idx_col - ((w_ptr->wizard || visual_only) ? 6 : 2));
76         }
77         if (w_ptr->wizard || visual_only) {
78             c_prt(attr, format("%d", f_idx), row_i, f_idx_col);
79         }
80
81         term_queue_bigchar(lit_col[F_LIT_STANDARD], row_i, f_ptr->x_attr[F_LIT_STANDARD], f_ptr->x_char[F_LIT_STANDARD], 0, 0);
82         term_putch(lit_col[F_LIT_NS_BEGIN], row_i, TERM_SLATE, '(');
83         for (int j = F_LIT_NS_BEGIN + 1; j < F_LIT_MAX; j++) {
84             term_putch(lit_col[j], row_i, TERM_SLATE, '/');
85         }
86
87         term_putch(lit_col[F_LIT_MAX - 1] + (use_bigtile ? 3 : 2), row_i, TERM_SLATE, ')');
88         for (int j = F_LIT_NS_BEGIN; j < F_LIT_MAX; j++) {
89             term_queue_bigchar(lit_col[j] + 1, row_i, f_ptr->x_attr[j], f_ptr->x_char[j], 0, 0);
90         }
91     }
92
93     for (; i < per_page; i++) {
94         term_erase(col, row + i, 255);
95     }
96 }
97
98 /*
99  * Interact with feature visuals.
100  */
101 void do_cmd_knowledge_features(bool *need_redraw, bool visual_only, IDX direct_f_idx, IDX *lighting_level)
102 {
103     TERM_COLOR attr_old[F_LIT_MAX] = {};
104     char char_old[F_LIT_MAX] = {};
105
106     TERM_LEN wid, hgt;
107     term_get_size(&wid, &hgt);
108
109     std::vector<FEAT_IDX> feat_idx(terrains_info.size());
110
111     concptr feature_group_text[] = { "terrains", nullptr };
112     int len;
113     int max = 0;
114     int grp_cnt = 0;
115     int feat_cnt;
116     FEAT_IDX grp_idx[100];
117     TERM_COLOR attr_top = 0;
118     bool visual_list = false;
119     byte char_left = 0;
120     TERM_LEN browser_rows = hgt - 8;
121     if (direct_f_idx < 0) {
122         for (FEAT_IDX i = 0; feature_group_text[i] != nullptr; i++) {
123             len = strlen(feature_group_text[i]);
124             if (len > max) {
125                 max = len;
126             }
127
128             if (collect_features(feat_idx.data(), 0x01)) {
129                 grp_idx[grp_cnt++] = i;
130             }
131         }
132
133         feat_cnt = 0;
134     } else {
135         auto *f_ptr = &terrains_info[direct_f_idx];
136
137         feat_idx[0] = direct_f_idx;
138         feat_cnt = 1;
139         feat_idx[1] = -1;
140
141         (void)visual_mode_command('v', &visual_list, browser_rows - 1, wid - (max + 3), &attr_top, &char_left, &f_ptr->x_attr[*lighting_level],
142             &f_ptr->x_char[*lighting_level], need_redraw);
143
144         for (FEAT_IDX i = 0; i < F_LIT_MAX; i++) {
145             attr_old[i] = f_ptr->x_attr[i];
146             char_old[i] = f_ptr->x_char[i];
147         }
148     }
149
150     grp_idx[grp_cnt] = -1;
151
152     FEAT_IDX old_grp_cur = -1;
153     FEAT_IDX grp_cur = 0;
154     FEAT_IDX grp_top = 0;
155     FEAT_IDX feat_cur = 0;
156     FEAT_IDX feat_top = 0;
157     TERM_LEN column = 0;
158     bool flag = false;
159     bool redraw = true;
160     TERM_COLOR *cur_attr_ptr;
161     char *cur_char_ptr;
162     while (!flag) {
163         char ch;
164         TerrainType *f_ptr;
165
166         if (redraw) {
167             clear_from(0);
168
169             prt(_("表示 - 地形", "Visuals - features"), 2, 0);
170             if (direct_f_idx < 0) {
171                 prt(_("グループ", "Group"), 4, 0);
172             }
173             prt(_("名前", "Name"), 4, max + 3);
174             if (use_bigtile) {
175                 if (w_ptr->wizard || visual_only) {
176                     prt("Idx", 4, 62);
177                 }
178                 prt(_("文字 ( l/ d)", "Sym ( l/ d)"), 4, 66);
179             } else {
180                 if (w_ptr->wizard || visual_only) {
181                     prt("Idx", 4, 64);
182                 }
183                 prt(_("文字 (l/d)", "Sym (l/d)"), 4, 68);
184             }
185
186             for (FEAT_IDX i = 0; i < 78; i++) {
187                 term_putch(i, 5, TERM_WHITE, '=');
188             }
189
190             if (direct_f_idx < 0) {
191                 for (FEAT_IDX i = 0; i < browser_rows; i++) {
192                     term_putch(max + 1, 6 + i, TERM_WHITE, '|');
193                 }
194             }
195
196             redraw = false;
197         }
198
199         if (direct_f_idx < 0) {
200             if (grp_cur < grp_top) {
201                 grp_top = grp_cur;
202             }
203             if (grp_cur >= grp_top + browser_rows) {
204                 grp_top = grp_cur - browser_rows + 1;
205             }
206
207             display_group_list(0, 6, max, browser_rows, grp_idx, feature_group_text, grp_cur, grp_top);
208             if (old_grp_cur != grp_cur) {
209                 old_grp_cur = grp_cur;
210                 feat_cnt = collect_features(feat_idx.data(), 0x00);
211             }
212
213             while (feat_cur < feat_top) {
214                 feat_top = std::max<short>(0, feat_top - browser_rows / 2);
215             }
216             while (feat_cur >= feat_top + browser_rows) {
217                 feat_top = std::min<short>(feat_cnt - browser_rows, feat_top + browser_rows / 2);
218             }
219         }
220
221         if (!visual_list) {
222             display_feature_list(max + 3, 6, browser_rows, feat_idx.data(), feat_cur, feat_top, visual_only, F_LIT_STANDARD);
223         } else {
224             feat_top = feat_cur;
225             display_feature_list(max + 3, 6, 1, feat_idx.data(), feat_cur, feat_top, visual_only, *lighting_level);
226             display_visual_list(max + 3, 7, browser_rows - 1, wid - (max + 3), attr_top, char_left);
227         }
228
229         prt(format(_("<方向>%s, 'd'で標準光源効果%s, ESC", "<dir>%s, 'd' for default lighting%s, ESC"),
230                 visual_list ? _(", ENTERで決定, 'a'で対象明度変更", ", ENTER to accept, 'a' for lighting level")
231                             : _(", 'v'でシンボル変更", ", 'v' for visuals"),
232                 (attr_idx || char_idx) ? _(", 'c', 'p'でペースト", ", 'c', 'p' to paste") : _(", 'c'でコピー", ", 'c' to copy")),
233             hgt - 1, 0);
234
235         f_ptr = &terrains_info[feat_idx[feat_cur]];
236         cur_attr_ptr = &f_ptr->x_attr[*lighting_level];
237         cur_char_ptr = &f_ptr->x_char[*lighting_level];
238
239         if (visual_list) {
240             place_visual_list_cursor(max + 3, 7, *cur_attr_ptr, *cur_char_ptr, attr_top, char_left);
241         } else if (!column) {
242             term_gotoxy(0, 6 + (grp_cur - grp_top));
243         } else {
244             term_gotoxy(max + 3, 6 + (feat_cur - feat_top));
245         }
246
247         ch = inkey();
248         if (visual_list && ((ch == 'A') || (ch == 'a'))) {
249             int prev_lighting_level = *lighting_level;
250
251             if (ch == 'A') {
252                 if (*lighting_level <= 0) {
253                     *lighting_level = F_LIT_MAX - 1;
254                 } else {
255                     (*lighting_level)--;
256                 }
257             } else {
258                 if (*lighting_level >= F_LIT_MAX - 1) {
259                     *lighting_level = 0;
260                 } else {
261                     (*lighting_level)++;
262                 }
263             }
264
265             if (f_ptr->x_attr[prev_lighting_level] != f_ptr->x_attr[*lighting_level]) {
266                 attr_top = std::max<byte>(0, (f_ptr->x_attr[*lighting_level] & 0x7f) - 5);
267             }
268
269             if (f_ptr->x_char[prev_lighting_level] != f_ptr->x_char[*lighting_level]) {
270                 char_left = std::max<byte>(0, f_ptr->x_char[*lighting_level] - 10);
271             }
272
273             continue;
274         } else if ((ch == 'D') || (ch == 'd')) {
275             TERM_COLOR prev_x_attr = f_ptr->x_attr[*lighting_level];
276             byte prev_x_char = f_ptr->x_char[*lighting_level];
277
278             apply_default_feat_lighting(f_ptr->x_attr, f_ptr->x_char);
279
280             if (visual_list) {
281                 if (prev_x_attr != f_ptr->x_attr[*lighting_level]) {
282                     attr_top = std::max<byte>(0, (f_ptr->x_attr[*lighting_level] & 0x7f) - 5);
283                 }
284
285                 if (prev_x_char != f_ptr->x_char[*lighting_level]) {
286                     char_left = std::max<byte>(0, f_ptr->x_char[*lighting_level] - 10);
287                 }
288             } else {
289                 *need_redraw = true;
290             }
291
292             continue;
293         } else if (visual_mode_command(ch, &visual_list, browser_rows - 1, wid - (max + 3), &attr_top, &char_left, cur_attr_ptr, cur_char_ptr, need_redraw)) {
294             switch (ch) {
295             case ESCAPE:
296                 for (FEAT_IDX i = 0; i < F_LIT_MAX; i++) {
297                     f_ptr->x_attr[i] = attr_old[i];
298                     f_ptr->x_char[i] = char_old[i];
299                 }
300
301                 [[fallthrough]];
302             case '\n':
303             case '\r':
304                 if (direct_f_idx >= 0) {
305                     flag = true;
306                 } else {
307                     *lighting_level = F_LIT_STANDARD;
308                 }
309                 break;
310             case 'V':
311             case 'v':
312                 for (FEAT_IDX i = 0; i < F_LIT_MAX; i++) {
313                     attr_old[i] = f_ptr->x_attr[i];
314                     char_old[i] = f_ptr->x_char[i];
315                 }
316                 *lighting_level = F_LIT_STANDARD;
317                 break;
318
319             case 'C':
320             case 'c':
321                 if (!visual_list) {
322                     for (FEAT_IDX i = 0; i < F_LIT_MAX; i++) {
323                         attr_idx_feat[i] = f_ptr->x_attr[i];
324                         char_idx_feat[i] = f_ptr->x_char[i];
325                     }
326                 }
327                 break;
328
329             case 'P':
330             case 'p':
331                 if (!visual_list) {
332                     for (FEAT_IDX i = F_LIT_NS_BEGIN; i < F_LIT_MAX; i++) {
333                         if (attr_idx_feat[i] || (!(char_idx_feat[i] & 0x80) && char_idx_feat[i])) {
334                             f_ptr->x_attr[i] = attr_idx_feat[i];
335                         }
336                         if (char_idx_feat[i]) {
337                             f_ptr->x_char[i] = char_idx_feat[i];
338                         }
339                     }
340                 }
341                 break;
342             }
343             continue;
344         }
345
346         switch (ch) {
347         case ESCAPE: {
348             flag = true;
349             break;
350         }
351
352         default: {
353             browser_cursor(ch, &column, &grp_cur, grp_cnt, &feat_cur, feat_cnt);
354             break;
355         }
356         }
357     }
358 }
359
360 /*
361  * Dungeon
362  */
363 void do_cmd_knowledge_dungeon(PlayerType *player_ptr)
364 {
365     FILE *fff = nullptr;
366     GAME_TEXT file_name[FILE_NAME_SIZE];
367     if (!open_temporary_file(&fff, file_name)) {
368         return;
369     }
370
371     for (const auto &d_ref : dungeons_info) {
372         bool seiha = false;
373
374         if (d_ref.idx == 0 || !d_ref.maxdepth) {
375             continue;
376         }
377         if (!max_dlv[d_ref.idx]) {
378             continue;
379         }
380         if (MonsterRace(d_ref.final_guardian).is_valid()) {
381             if (!monraces_info[d_ref.final_guardian].max_num) {
382                 seiha = true;
383             }
384         } else if (max_dlv[d_ref.idx] == d_ref.maxdepth) {
385             seiha = true;
386         }
387
388         fprintf(fff, _("%c%-12s :  %3d 階\n", "%c%-16s :  level %3d\n"), seiha ? '!' : ' ', d_ref.name.data(), (int)max_dlv[d_ref.idx]);
389     }
390
391     angband_fclose(fff);
392     (void)show_file(player_ptr, true, file_name, _("今までに入ったダンジョン", "Dungeon"), 0, 0);
393     fd_kill(file_name);
394 }