OSDN Git Service

[Refactor] format 関数の戻り値を std::string にする
[hengbandforosx/hengbandosx.git] / src / io-dump / dump-util.cpp
1 #include "io-dump/dump-util.h"
2 #include "floor/geometry.h"
3 #include "game-option/keymap-directory-getter.h"
4 #include "game-option/special-options.h"
5 #include "system/terrain-type-definition.h"
6 #include "term/screen-processor.h"
7 #include "term/term-color-types.h"
8 #include "util/angband-files.h"
9 #include "util/int-char-converter.h"
10 #include "view/display-messages.h"
11
12 TERM_COLOR attr_idx = 0;
13 char char_idx = 0;
14
15 TERM_COLOR attr_idx_feat[F_LIT_MAX];
16 char char_idx_feat[F_LIT_MAX];
17
18 /*!
19  * @brief シンボル変更処理 / Do visual mode command -- Change symbols
20  * @param ch
21  * @param visual_list_ptr
22  * @param height
23  * @param width
24  * @param attr_ptr_ptr
25  * @param char_left_ptr
26  * @param cur_attr_ptr
27  * @param cur_char_ptr
28  * @param need_redraw
29  * @return 何かコマンドを入れたらTRUE
30  */
31 bool visual_mode_command(char ch, bool *visual_list_ptr,
32     int height, int width,
33     TERM_COLOR *attr_top_ptr, byte *char_left_ptr,
34     TERM_COLOR *cur_attr_ptr, char *cur_char_ptr, bool *need_redraw)
35 {
36     static TERM_COLOR attr_old = 0;
37     static char char_old = 0;
38
39     switch (ch) {
40     case ESCAPE: {
41         if (!*visual_list_ptr) {
42             return false;
43         }
44
45         *cur_attr_ptr = attr_old;
46         *cur_char_ptr = char_old;
47         *visual_list_ptr = false;
48         return true;
49     }
50     case '\n':
51     case '\r': {
52         if (!*visual_list_ptr) {
53             return false;
54         }
55
56         *visual_list_ptr = false;
57         *need_redraw = true;
58         return true;
59     }
60     case 'V':
61     case 'v': {
62         if (*visual_list_ptr) {
63             return false;
64         }
65
66         *visual_list_ptr = true;
67         *attr_top_ptr = std::max<byte>(0, (*cur_attr_ptr & 0x7f) - 5);
68         *char_left_ptr = std::max<byte>(0, *cur_char_ptr - 10);
69         attr_old = *cur_attr_ptr;
70         char_old = *cur_char_ptr;
71         return true;
72     }
73     case 'C':
74     case 'c': {
75         attr_idx = *cur_attr_ptr;
76         char_idx = *cur_char_ptr;
77         for (int i = 0; i < F_LIT_MAX; i++) {
78             attr_idx_feat[i] = 0;
79             char_idx_feat[i] = 0;
80         }
81
82         return true;
83     }
84     case 'P':
85     case 'p': {
86         if (attr_idx || (!(char_idx & 0x80) && char_idx)) {
87             *cur_attr_ptr = attr_idx;
88             *attr_top_ptr = std::max<byte>(0, (*cur_attr_ptr & 0x7f) - 5);
89             if (!*visual_list_ptr) {
90                 *need_redraw = true;
91             }
92         }
93
94         if (char_idx) {
95             /* Set the char */
96             *cur_char_ptr = char_idx;
97             *char_left_ptr = std::max<byte>(0, *cur_char_ptr - 10);
98             if (!*visual_list_ptr) {
99                 *need_redraw = true;
100             }
101         }
102
103         return true;
104     }
105     default: {
106         if (!*visual_list_ptr) {
107             return false;
108         }
109
110         int eff_width;
111         int d = get_keymap_dir(ch);
112         TERM_COLOR a = (*cur_attr_ptr & 0x7f);
113         auto c = *cur_char_ptr;
114
115         if (use_bigtile) {
116             eff_width = width / 2;
117         } else {
118             eff_width = width;
119         }
120
121         if ((a == 0) && (ddy[d] < 0)) {
122             d = 0;
123         }
124         if ((c == 0) && (ddx[d] < 0)) {
125             d = 0;
126         }
127         if ((a == 0x7f) && (ddy[d] > 0)) {
128             d = 0;
129         }
130         if (((byte)c == 0xff) && (ddx[d] > 0)) {
131             d = 0;
132         }
133
134         a += (TERM_COLOR)ddy[d];
135         c += static_cast<char>(ddx[d]);
136         if (c & 0x80) {
137             a |= 0x80;
138         }
139
140         *cur_attr_ptr = a;
141         *cur_char_ptr = c;
142         if ((ddx[d] < 0) && *char_left_ptr > std::max(0, (unsigned char)c - 10)) {
143             (*char_left_ptr)--;
144         }
145         if ((ddx[d] > 0) && *char_left_ptr + eff_width < std::min(0xff, (unsigned char)c + 10)) {
146             (*char_left_ptr)++;
147         }
148         if ((ddy[d] < 0) && *attr_top_ptr > std::max(0, (int)(a & 0x7f) - 4)) {
149             (*attr_top_ptr)--;
150         }
151         if ((ddy[d] > 0) && *attr_top_ptr + height < std::min(0x7f, (a & 0x7f) + 4)) {
152             (*attr_top_ptr)++;
153         }
154
155         return true;
156     }
157     }
158 }
159
160 /*!
161  * @brief ダンプ用の一時ファイルを開く
162  * @param fff 一時ファイルへの参照ポインタ
163  * @param file_name ファイル名
164  * @return ファイルを開けたらTRUE、開けなかったらFALSE
165  * @details
166  */
167 bool open_temporary_file(FILE **fff, char *file_name)
168 {
169     *fff = angband_fopen_temp(file_name, FILE_NAME_SIZE);
170     if (*fff != nullptr) {
171         return true;
172     }
173
174     msg_format(_("一時ファイル %s を作成できませんでした。", "Failed to create temporary file %s."), file_name);
175     msg_print(nullptr);
176     return false;
177 }
178
179 /*!
180  * @brief モンスター情報リスト中のグループを表示する /
181  * Display the object groups.
182  * @param col 開始行
183  * @param row 開始列
184  * @param wid 表示文字数幅
185  * @param per_page リストの表示行
186  * @param grp_idx グループのID配列
187  * @param group_text グループ名の文字列配列
188  * @param grp_cur 現在の選択ID
189  * @param grp_top 現在の選択リスト最上部ID
190  */
191 void display_group_list(int col, int row, int wid, int per_page, IDX grp_idx[], concptr group_text[], int grp_cur, int grp_top)
192 {
193     for (int i = 0; i < per_page && (grp_idx[i] >= 0); i++) {
194         int grp = grp_idx[grp_top + i];
195         TERM_COLOR attr = (grp_top + i == grp_cur) ? TERM_L_BLUE : TERM_WHITE;
196         term_erase(col, row + i, wid);
197         c_put_str(attr, group_text[grp], row + i, col);
198     }
199 }
200
201 /*!
202  * @brief Display visuals.
203  */
204 void display_visual_list(int col, int row, int height, int width, TERM_COLOR attr_top, byte char_left)
205 {
206     for (int i = 0; i < height; i++) {
207         term_erase(col, row + i, width);
208     }
209
210     if (use_bigtile) {
211         width /= 2;
212     }
213
214     for (int i = 0; i < height; i++) {
215         for (int j = 0; j < width; j++) {
216             TERM_LEN x = col + j;
217             TERM_LEN y = row + i;
218             if (use_bigtile) {
219                 x += j;
220             }
221
222             int ia = attr_top + i;
223             int ic = char_left + j;
224             if (ia > 0x7f || ic > 0xff || ic < ' ' ||
225                 (!use_graphics && ic > 0x7f)) {
226                 continue;
227             }
228
229             TERM_COLOR a = (TERM_COLOR)ia;
230             auto c = static_cast<char>(ic);
231             if (c & 0x80) {
232                 a |= 0x80;
233             }
234
235             term_queue_bigchar(x, y, a, c, 0, 0);
236         }
237     }
238 }
239
240 /*!
241  * @brief Place the cursor at the collect position for visual mode
242  */
243 void place_visual_list_cursor(TERM_LEN col, TERM_LEN row, TERM_COLOR a, byte c, TERM_COLOR attr_top, byte char_left)
244 {
245     int i = (a & 0x7f) - attr_top;
246     int j = c - char_left;
247
248     TERM_LEN x = col + j;
249     TERM_LEN y = row + i;
250     if (use_bigtile) {
251         x += j;
252     }
253
254     term_gotoxy(x, y);
255 }
256
257 /*!
258  * @brief Move the cursor in a browser window
259  */
260 void browser_cursor(char ch, int *column, IDX *grp_cur, int grp_cnt, IDX *list_cur, int list_cnt)
261 {
262     int d;
263     int col = *column;
264     IDX grp = *grp_cur;
265     IDX list = *list_cur;
266     if (ch == ' ') {
267         d = 3;
268     } else if (ch == '-') {
269         d = 9;
270     } else {
271         d = get_keymap_dir(ch);
272     }
273
274     if (!d) {
275         return;
276     }
277
278     if ((ddx[d] > 0) && ddy[d]) {
279         int browser_rows;
280         int wid, hgt;
281         term_get_size(&wid, &hgt);
282         browser_rows = hgt - 8;
283         if (!col) {
284             int old_grp = grp;
285             grp += ddy[d] * (browser_rows - 1);
286             if (grp >= grp_cnt) {
287                 grp = grp_cnt - 1;
288             }
289             if (grp < 0) {
290                 grp = 0;
291             }
292             if (grp != old_grp) {
293                 list = 0;
294             }
295         } else {
296             list += ddy[d] * browser_rows;
297             if (list >= list_cnt) {
298                 list = list_cnt - 1;
299             }
300             if (list < 0) {
301                 list = 0;
302             }
303         }
304
305         (*grp_cur) = grp;
306         (*list_cur) = list;
307         return;
308     }
309
310     if (ddx[d]) {
311         col += ddx[d];
312         if (col < 0) {
313             col = 0;
314         }
315         if (col > 1) {
316             col = 1;
317         }
318
319         (*column) = col;
320         return;
321     }
322
323     if (!col) {
324         int old_grp = grp;
325         grp += (IDX)ddy[d];
326         if (grp >= grp_cnt) {
327             grp = grp_cnt - 1;
328         }
329         if (grp < 0) {
330             grp = 0;
331         }
332         if (grp != old_grp) {
333             list = 0;
334         }
335     } else {
336         list += (IDX)ddy[d];
337         if (list >= list_cnt) {
338             list = list_cnt - 1;
339         }
340         if (list < 0) {
341             list = 0;
342         }
343     }
344
345     (*grp_cur) = grp;
346     (*list_cur) = list;
347 }