OSDN Git Service

Merge pull request #3415 from sikabane-works/release/3.0.0.85
[hengbandforosx/hengbandosx.git] / src / view / display-map.cpp
1 #include "view/display-map.h"
2 #include "autopick/autopick-finder.h"
3 #include "autopick/autopick-methods-table.h"
4 #include "autopick/autopick-util.h"
5 #include "floor/cave.h"
6 #include "floor/geometry.h"
7 #include "game-option/map-screen-options.h"
8 #include "game-option/special-options.h"
9 #include "grid/feature.h"
10 #include "grid/grid.h"
11 #include "monster-race/monster-race.h"
12 #include "monster-race/race-flags1.h"
13 #include "monster-race/race-flags2.h"
14 #include "object/object-info.h"
15 #include "object/object-mark-types.h"
16 #include "system/baseitem-info.h"
17 #include "system/floor-type-definition.h"
18 #include "system/grid-type-definition.h"
19 #include "system/item-entity.h"
20 #include "system/monster-entity.h"
21 #include "system/monster-race-info.h"
22 #include "system/player-type-definition.h"
23 #include "system/terrain-type-definition.h"
24 #include "term/term-color-types.h"
25 #include "timed-effect/player-blindness.h"
26 #include "timed-effect/player-hallucination.h"
27 #include "timed-effect/timed-effects.h"
28 #include "util/bit-flags-calculator.h"
29 #include "window/main-window-util.h"
30 #include "world/world.h"
31 #include <span>
32
33 byte display_autopick; /*!< 自動拾い状態の設定フラグ */
34
35 namespace {
36 /* 一般的にオブジェクトシンボルとして扱われる記号を定義する(幻覚処理向け) /  Hack -- Legal object codes */
37 const std::string image_objects = R"(?/|\"!$()_-=[]{},~)";
38
39 /* 一般的にモンスターシンボルとして扱われる記号を定義する(幻覚処理向け) / Hack -- Legal monster codes */
40 const std::string image_monsters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
41 }
42
43 /*!
44  * @brief オブジェクトの表示を幻覚状態に差し替える / Hallucinatory object
45  * @param ap 本来の色
46  * @param cp 本来のシンボル
47  */
48 static void image_object(TERM_COLOR *ap, char *cp)
49 {
50     if (use_graphics) {
51         std::span<BaseitemInfo> candidates(baseitems_info.begin() + 1, baseitems_info.end());
52         const auto &baseitem = rand_choice(candidates);
53         *cp = baseitem.x_char;
54         *ap = baseitem.x_attr;
55         return;
56     }
57
58     *cp = rand_choice(image_objects);
59     *ap = randint1(15);
60 }
61
62 /*!
63  * @brief モンスターの表示を幻覚状態に差し替える / Mega-Hack -- Hallucinatory monster
64  * @param ap 本来の色
65  * @param cp 本来のシンボル
66  */
67 static void image_monster(TERM_COLOR *ap, char *cp)
68 {
69     if (use_graphics) {
70         auto r_idx = MonsterRace::pick_one_at_random();
71         auto *r_ptr = &monraces_info[r_idx];
72         *cp = r_ptr->x_char;
73         *ap = r_ptr->x_attr;
74         return;
75     }
76
77     *cp = one_in_(25) ? rand_choice(image_objects) : rand_choice(image_monsters);
78     *ap = randint1(15);
79 }
80
81 /*!
82  * @brief オブジェクト&モンスターの表示を幻覚状態に差し替える / Hack -- Random hallucination
83  * @param ap 本来の色
84  * @param cp 本来のシンボル
85  */
86 static void image_random(TERM_COLOR *ap, char *cp)
87 {
88     if (randint0(100) < 75) {
89         image_monster(ap, cp);
90     } else {
91         image_object(ap, cp);
92     }
93 }
94
95 /*!
96  * @brief マップに表示されるべき地形(壁)かどうかを判定する
97  * @param floor_ptr 階の情報への参照ポインタ
98  * @param f_ptr 地形の情報への参照ポインタ
99  * @param y グリッドy座標
100  * @param x グリッドx座標
101  * @return 表示されるべきならtrue、そうでないならfalse
102  * @details
103  * 周り全てが壁に囲まれている壁についてはオプション状態による。
104  * 1か所でも空きがあるか、壁ではない地形、金を含む地形、永久岩は表示。
105  */
106 static bool is_revealed_wall(FloorType *floor_ptr, TerrainType *f_ptr, POSITION y, POSITION x)
107 {
108     if (view_hidden_walls) {
109         if (view_unsafe_walls) {
110             return true;
111         }
112         if (none_bits(floor_ptr->grid_array[y][x].info, CAVE_UNSAFE)) {
113             return true;
114         }
115     }
116
117     if (f_ptr->flags.has_not(TerrainCharacteristics::WALL) || f_ptr->flags.has(TerrainCharacteristics::HAS_GOLD)) {
118         return true;
119     }
120
121     if (in_bounds(floor_ptr, y, x) && f_ptr->flags.has(TerrainCharacteristics::PERMANENT)) {
122         return true;
123     }
124
125     int n = 0;
126     for (int i = 0; i < 8; i++) {
127         int dy = y + ddy_cdd[i];
128         int dx = x + ddx_cdd[i];
129         if (!in_bounds(floor_ptr, dy, dx)) {
130             n++;
131             continue;
132         }
133
134         FEAT_IDX f_idx = floor_ptr->grid_array[dy][dx].feat;
135         TerrainType *n_ptr = &terrains_info[f_idx];
136         if (n_ptr->flags.has(TerrainCharacteristics::WALL)) {
137             n++;
138         }
139     }
140
141     return n != 8;
142 }
143
144 /*!
145  * @brief 指定した座標の地形の表示属性を取得する / Extract the attr/char to display at the given (legal) map location
146  * @param player_ptr プレイヤー情報への参照ポインタ
147  * @param y 階の中のy座標
148  * @param x 階の中のy座標
149  * @param ap 文字色属性
150  * @param cp 文字種属性
151  * @param tap 文字色属性(タイル)
152  * @param tcp 文字種属性(タイル)
153  */
154 void map_info(PlayerType *player_ptr, POSITION y, POSITION x, TERM_COLOR *ap, char *cp, TERM_COLOR *tap, char *tcp)
155 {
156     auto *floor_ptr = player_ptr->current_floor_ptr;
157     auto *g_ptr = &floor_ptr->grid_array[y][x];
158     FEAT_IDX feat = g_ptr->get_feat_mimic();
159     auto *f_ptr = &terrains_info[feat];
160     TERM_COLOR a;
161     char c;
162     if (f_ptr->flags.has_not(TerrainCharacteristics::REMEMBER)) {
163         auto is_visible = any_bits(g_ptr->info, (CAVE_MARK | CAVE_LITE | CAVE_MNLT));
164         auto is_glowing = match_bits(g_ptr->info, CAVE_GLOW | CAVE_MNDK, CAVE_GLOW);
165         auto can_view = g_ptr->is_view() && (is_glowing || player_ptr->see_nocto);
166         const auto is_blind = player_ptr->effects()->blindness()->is_blind();
167         if (!is_blind && (is_visible || can_view)) {
168             a = f_ptr->x_attr[F_LIT_STANDARD];
169             c = f_ptr->x_char[F_LIT_STANDARD];
170             if (player_ptr->wild_mode) {
171                 if (view_special_lite && !is_daytime()) {
172                     a = f_ptr->x_attr[F_LIT_DARK];
173                     c = f_ptr->x_char[F_LIT_DARK];
174                 }
175             } else if (darkened_grid(player_ptr, g_ptr)) {
176                 feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
177                 f_ptr = &terrains_info[feat];
178                 a = f_ptr->x_attr[F_LIT_STANDARD];
179                 c = f_ptr->x_char[F_LIT_STANDARD];
180             } else if (view_special_lite) {
181                 if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) {
182                     if (view_yellow_lite) {
183                         a = f_ptr->x_attr[F_LIT_LITE];
184                         c = f_ptr->x_char[F_LIT_LITE];
185                     }
186                 } else if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) {
187                     a = f_ptr->x_attr[F_LIT_DARK];
188                     c = f_ptr->x_char[F_LIT_DARK];
189                 } else if (!(g_ptr->info & CAVE_VIEW)) {
190                     if (view_bright_lite) {
191                         a = f_ptr->x_attr[F_LIT_DARK];
192                         c = f_ptr->x_char[F_LIT_DARK];
193                     }
194                 }
195             }
196         } else {
197             feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
198             f_ptr = &terrains_info[feat];
199             a = f_ptr->x_attr[F_LIT_STANDARD];
200             c = f_ptr->x_char[F_LIT_STANDARD];
201         }
202     } else {
203         if (g_ptr->is_mark() && is_revealed_wall(floor_ptr, f_ptr, y, x)) {
204             a = f_ptr->x_attr[F_LIT_STANDARD];
205             c = f_ptr->x_char[F_LIT_STANDARD];
206             const auto is_blind = player_ptr->effects()->blindness()->is_blind();
207             if (player_ptr->wild_mode) {
208                 if (view_granite_lite && (is_blind || !is_daytime())) {
209                     a = f_ptr->x_attr[F_LIT_DARK];
210                     c = f_ptr->x_char[F_LIT_DARK];
211                 }
212             } else if (darkened_grid(player_ptr, g_ptr) && !is_blind) {
213                 if (f_ptr->flags.has_all_of({ TerrainCharacteristics::LOS, TerrainCharacteristics::PROJECT })) {
214                     feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
215                     f_ptr = &terrains_info[feat];
216                     a = f_ptr->x_attr[F_LIT_STANDARD];
217                     c = f_ptr->x_char[F_LIT_STANDARD];
218                 } else if (view_granite_lite && view_bright_lite) {
219                     a = f_ptr->x_attr[F_LIT_DARK];
220                     c = f_ptr->x_char[F_LIT_DARK];
221                 }
222             } else if (view_granite_lite) {
223                 if (is_blind) {
224                     a = f_ptr->x_attr[F_LIT_DARK];
225                     c = f_ptr->x_char[F_LIT_DARK];
226                 } else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) {
227                     if (view_yellow_lite) {
228                         a = f_ptr->x_attr[F_LIT_LITE];
229                         c = f_ptr->x_char[F_LIT_LITE];
230                     }
231                 } else if (view_bright_lite) {
232                     if (!(g_ptr->info & CAVE_VIEW)) {
233                         a = f_ptr->x_attr[F_LIT_DARK];
234                         c = f_ptr->x_char[F_LIT_DARK];
235                     } else if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) {
236                         a = f_ptr->x_attr[F_LIT_DARK];
237                         c = f_ptr->x_char[F_LIT_DARK];
238                     } else if (f_ptr->flags.has_not(TerrainCharacteristics::LOS) && !check_local_illumination(player_ptr, y, x)) {
239                         a = f_ptr->x_attr[F_LIT_DARK];
240                         c = f_ptr->x_char[F_LIT_DARK];
241                     }
242                 }
243             }
244         } else {
245             feat = (view_unsafe_grids && (g_ptr->info & CAVE_UNSAFE)) ? feat_undetected : feat_none;
246             f_ptr = &terrains_info[feat];
247             a = f_ptr->x_attr[F_LIT_STANDARD];
248             c = f_ptr->x_char[F_LIT_STANDARD];
249         }
250     }
251
252     if (feat_priority == -1) {
253         feat_priority = f_ptr->priority;
254     }
255
256     (*tap) = a;
257     (*tcp) = c;
258     (*ap) = a;
259     (*cp) = c;
260
261     auto is_hallucinated = player_ptr->effects()->hallucination()->is_hallucinated();
262     if (is_hallucinated && one_in_(256)) {
263         image_random(ap, cp);
264     }
265
266     for (const auto this_o_idx : g_ptr->o_idx_list) {
267         ItemEntity *o_ptr;
268         o_ptr = &floor_ptr->o_list[this_o_idx];
269         if (o_ptr->marked.has_not(OmType::FOUND)) {
270             continue;
271         }
272
273         if (display_autopick) {
274             byte act;
275
276             match_autopick = find_autopick_list(player_ptr, o_ptr);
277             if (match_autopick == -1) {
278                 continue;
279             }
280
281             act = autopick_list[match_autopick].action;
282
283             if ((act & DO_DISPLAY) && (act & display_autopick)) {
284                 autopick_obj = o_ptr;
285             } else {
286                 match_autopick = -1;
287                 continue;
288             }
289         }
290
291         *cp = o_ptr->get_symbol();
292         *ap = o_ptr->get_color();
293         feat_priority = 20;
294         if (is_hallucinated) {
295             image_object(ap, cp);
296         }
297
298         break;
299     }
300
301     if (g_ptr->m_idx && display_autopick != 0) {
302         set_term_color(player_ptr, y, x, ap, cp);
303         return;
304     }
305
306     auto *m_ptr = &floor_ptr->m_list[g_ptr->m_idx];
307     if (!m_ptr->ml) {
308         set_term_color(player_ptr, y, x, ap, cp);
309         return;
310     }
311
312     auto *r_ptr = &monraces_info[m_ptr->ap_r_idx];
313     feat_priority = 30;
314     if (is_hallucinated) {
315         if (r_ptr->visual_flags.has_all_of({ MonsterVisualType::CLEAR, MonsterVisualType::CLEAR_COLOR })) {
316             /* Do nothing */
317         } else {
318             image_monster(ap, cp);
319         }
320
321         set_term_color(player_ptr, y, x, ap, cp);
322         return;
323     }
324
325     a = r_ptr->x_attr;
326     c = r_ptr->x_char;
327     if (r_ptr->visual_flags.has_none_of({ MonsterVisualType::CLEAR, MonsterVisualType::SHAPECHANGER, MonsterVisualType::CLEAR_COLOR, MonsterVisualType::MULTI_COLOR, MonsterVisualType::RANDOM_COLOR })) {
328         *ap = a;
329         *cp = c;
330         set_term_color(player_ptr, y, x, ap, cp);
331         return;
332     }
333
334     if (r_ptr->visual_flags.has_all_of({ MonsterVisualType::CLEAR, MonsterVisualType::CLEAR_COLOR })) {
335         set_term_color(player_ptr, y, x, ap, cp);
336         return;
337     }
338
339     if (r_ptr->visual_flags.has(MonsterVisualType::CLEAR_COLOR) && (*ap != TERM_DARK) && !use_graphics) {
340         /* Do nothing */
341     } else if (r_ptr->visual_flags.has(MonsterVisualType::MULTI_COLOR) && !use_graphics) {
342         if (r_ptr->visual_flags.has(MonsterVisualType::ANY_COLOR)) {
343             *ap = randint1(15);
344         } else {
345             constexpr static auto colors = {
346                 TERM_RED,
347                 TERM_L_RED,
348                 TERM_WHITE,
349                 TERM_L_GREEN,
350                 TERM_BLUE,
351                 TERM_L_DARK,
352                 TERM_GREEN,
353             };
354
355             *ap = rand_choice(colors);
356         }
357     } else if (r_ptr->visual_flags.has(MonsterVisualType::RANDOM_COLOR) && !use_graphics) {
358         *ap = g_ptr->m_idx % 15 + 1;
359     } else {
360         *ap = a;
361     }
362
363     if (r_ptr->visual_flags.has(MonsterVisualType::CLEAR) && (*cp != ' ') && !use_graphics) {
364         set_term_color(player_ptr, y, x, ap, cp);
365         return;
366     }
367
368     if (r_ptr->visual_flags.has(MonsterVisualType::SHAPECHANGER)) {
369         if (use_graphics) {
370             auto r_idx = MonsterRace::pick_one_at_random();
371             MonsterRaceInfo *tmp_r_ptr = &monraces_info[r_idx];
372             *cp = tmp_r_ptr->x_char;
373             *ap = tmp_r_ptr->x_attr;
374         } else {
375             *cp = one_in_(25) ? rand_choice(image_objects) : rand_choice(image_monsters);
376         }
377
378         set_term_color(player_ptr, y, x, ap, cp);
379         return;
380     }
381
382     *cp = c;
383     set_term_color(player_ptr, y, x, ap, cp);
384 }