OSDN Git Service

[Refactor] #37353 feat_* を feature.h へ移動。
[hengband/hengband.git] / src / xtra2.c
1 /*!
2  * @file xtra2.c
3  * @brief 雑多なその他の処理2 / effects of various "objects"
4  * @date 2014/02/06
5  * @author
6  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research, and\n
8  * not for profit purposes provided that this copyright and statement are\n
9  * included in all such copies.\n
10  * 2014 Deskull rearranged comment for Doxygen.
11  */
12
13
14 #include "angband.h"
15 #include "util.h"
16
17 #include "cmd-pet.h"
18 #include "dungeon-file.h"
19 #include "object-curse.h"
20 #include "object-flavor.h"
21 #include "monster.h"
22 #include "monsterrace-hook.h"
23 #include "objectkind-hook.h"
24 #include "sort.h"
25 #include "spells-summon.h"
26 #include "grid.h"
27 #include "floor.h"
28 #include "floor-events.h"
29 #include "player-move.h"
30 #include "player-status.h"
31 #include "monster-status.h"
32 #include "view-mainwindow.h"
33 #include "feature.h"
34
35 /*!
36  * @brief コンソール上におけるマップ表示の左上位置を返す /
37  * Calculates current boundaries Called below and from "do_cmd_locate()".
38  * @return なし
39  */
40 void panel_bounds_center(void)
41 {
42         TERM_LEN wid, hgt;
43
44         get_screen_size(&wid, &hgt);
45
46         panel_row_max = panel_row_min + hgt - 1;
47         panel_row_prt = panel_row_min - 1;
48         panel_col_max = panel_col_min + wid - 1;
49         panel_col_prt = panel_col_min - 13;
50 }
51
52 /*!
53  * @brief フォーカスを当てるべきマップ描画の基準座標を指定する
54  * @param y 変更先のフロアY座標
55  * @param x 変更先のフロアX座標
56  * @details
57  * Handle a request to change the current panel
58  * Return TRUE if the panel was changed.
59  * Also used in do_cmd_locate
60  * @return 実際に再描画が必要だった場合TRUEを返す
61  */
62 static bool change_panel_xy(POSITION y, POSITION x)
63 {
64         POSITION dy = 0, dx = 0;
65         TERM_LEN wid, hgt;
66
67         get_screen_size(&wid, &hgt);
68
69         if (y < panel_row_min) dy = -1;
70         if (y > panel_row_max) dy = 1;
71         if (x < panel_col_min) dx = -1;
72         if (x > panel_col_max) dx = 1;
73
74         if (!dy && !dx) return (FALSE);
75
76         return change_panel(dy, dx);
77 }
78
79
80 /*!
81  * @brief マップ描画のフォーカスを当てるべき座標を更新する
82  * @details
83  * Given an row (y) and col (x), this routine detects when a move
84  * off the screen has occurred and figures new borders. -RAK-
85  * "Update" forces a "full update" to take place.
86  * The map is reprinted if necessary, and "TRUE" is returned.
87  * @return 実際に再描画が必要だった場合TRUEを返す
88  */
89 void verify_panel(void)
90 {
91         POSITION y = p_ptr->y;
92         POSITION x = p_ptr->x;
93         TERM_LEN wid, hgt;
94
95         int prow_min;
96         int pcol_min;
97         int max_prow_min;
98         int max_pcol_min;
99
100         get_screen_size(&wid, &hgt);
101
102         max_prow_min = current_floor_ptr->height - hgt;
103         max_pcol_min = current_floor_ptr->width - wid;
104
105         /* Bounds checking */
106         if (max_prow_min < 0) max_prow_min = 0;
107         if (max_pcol_min < 0) max_pcol_min = 0;
108
109                 /* Center on player */
110         if (center_player && (center_running || !running))
111         {
112                 /* Center vertically */
113                 prow_min = y - hgt / 2;
114                 if (prow_min < 0) prow_min = 0;
115                 else if (prow_min > max_prow_min) prow_min = max_prow_min;
116
117                 /* Center horizontally */
118                 pcol_min = x - wid / 2;
119                 if (pcol_min < 0) pcol_min = 0;
120                 else if (pcol_min > max_pcol_min) pcol_min = max_pcol_min;
121         }
122         else
123         {
124                 prow_min = panel_row_min;
125                 pcol_min = panel_col_min;
126
127                 /* Scroll screen when 2 grids from top/bottom edge */
128                 if (y > panel_row_max - 2)
129                 {
130                         while (y > prow_min + hgt-1 - 2)
131                         {
132                                 prow_min += (hgt / 2);
133                         }
134                 }
135
136                 if (y < panel_row_min + 2)
137                 {
138                         while (y < prow_min + 2)
139                         {
140                                 prow_min -= (hgt / 2);
141                         }
142                 }
143
144                 if (prow_min > max_prow_min) prow_min = max_prow_min;
145                 if (prow_min < 0) prow_min = 0;
146
147                 /* Scroll screen when 4 grids from left/right edge */
148                 if (x > panel_col_max - 4)
149                 {
150                         while (x > pcol_min + wid-1 - 4)
151                         {
152                                 pcol_min += (wid / 2);
153                         }
154                 }
155                 
156                 if (x < panel_col_min + 4)
157                 {
158                         while (x < pcol_min + 4)
159                         {
160                                 pcol_min -= (wid / 2);
161                         }
162                 }
163
164                 if (pcol_min > max_pcol_min) pcol_min = max_pcol_min;
165                 if (pcol_min < 0) pcol_min = 0;
166         }
167
168         /* Check for "no change" */
169         if ((prow_min == panel_row_min) && (pcol_min == panel_col_min)) return;
170
171         /* Save the new panel info */
172         panel_row_min = prow_min;
173         panel_col_min = pcol_min;
174
175         /* Hack -- optional disturb on "panel change" */
176         if (disturb_panel && !center_player) disturb(FALSE, FALSE);
177
178         panel_bounds_center();
179
180         p_ptr->update |= (PU_MONSTERS);
181         p_ptr->redraw |= (PR_MAP);
182         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
183 }
184
185
186 /*** Targeting Code ***/
187
188 /*
189  * Determine is a monster makes a reasonable target
190  *
191  * The concept of "targeting" was stolen from "Morgul" (?)
192  *
193  * The player can target any location, or any "target-able" monster.
194  *
195  * Currently, a monster is "target_able" if it is visible, and if
196  * the player can hit it with a projection, and the player is not
197  * hallucinating.  This allows use of "use closest target" macros.
198  *
199  * Future versions may restrict the ability to target "trappers"
200  * and "mimics", but the semantics is a little bit weird.
201  */
202 bool target_able(MONSTER_IDX m_idx)
203 {
204         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
205
206         /* Monster must be alive */
207         if (!monster_is_valid(m_ptr)) return (FALSE);
208
209         /* Hack -- no targeting hallucinations */
210         if (p_ptr->image) return (FALSE);
211
212         /* Monster must be visible */
213         if (!m_ptr->ml) return (FALSE);
214
215         if (p_ptr->riding && (p_ptr->riding == m_idx)) return (TRUE);
216
217         /* Monster must be projectable */
218         if (!projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) return (FALSE);
219
220         /* Hack -- Never target trappers */
221         /* if (CLEAR_ATTR && (CLEAR_CHAR)) return (FALSE); */
222
223         /* Assume okay */
224         return (TRUE);
225 }
226
227
228
229
230 /*
231  * Update (if necessary) and verify (if possible) the target.
232  *
233  * We return TRUE if the target is "okay" and FALSE otherwise.
234  */
235 bool target_okay(void)
236 {
237         /* Accept stationary targets */
238         if (target_who < 0) return (TRUE);
239
240         /* Check moving targets */
241         if (target_who > 0)
242         {
243                 /* Accept reasonable targets */
244                 if (target_able(target_who))
245                 {
246                         monster_type *m_ptr = &current_floor_ptr->m_list[target_who];
247
248                         /* Acquire monster location */
249                         target_row = m_ptr->fy;
250                         target_col = m_ptr->fx;
251
252                         /* Good target */
253                         return (TRUE);
254                 }
255         }
256
257         /* Assume no target */
258         return (FALSE);
259 }
260
261
262 /*
263  * Hack -- help "select" a location (see below)
264  */
265 static POSITION_IDX target_pick(POSITION y1, POSITION x1, POSITION dy, POSITION dx)
266 {
267         POSITION_IDX i, v;
268         POSITION x2, y2, x3, y3, x4, y4;
269         POSITION_IDX b_i = -1, b_v = 9999;
270
271
272         /* Scan the locations */
273         for (i = 0; i < tmp_pos.n; i++)
274         {
275                 /* Point 2 */
276                 x2 = tmp_pos.x[i];
277                 y2 = tmp_pos.y[i];
278
279                 /* Directed distance */
280                 x3 = (x2 - x1);
281                 y3 = (y2 - y1);
282
283                 /* Verify quadrant */
284                 if (dx && (x3 * dx <= 0)) continue;
285                 if (dy && (y3 * dy <= 0)) continue;
286
287                 x4 = ABS(x3);
288                 y4 = ABS(y3);
289
290                 /* Verify quadrant */
291                 if (dy && !dx && (x4 > y4)) continue;
292                 if (dx && !dy && (y4 > x4)) continue;
293
294                 /* Approximate Double Distance */
295                 v = ((x4 > y4) ? (x4 + x4 + y4) : (y4 + y4 + x4));
296
297                 /* Penalize location */
298                 if ((b_i >= 0) && (v >= b_v)) continue;
299                 b_i = i; b_v = v;
300         }
301         return (b_i);
302 }
303
304
305 /*
306  * Hack -- determine if a given location is "interesting"
307  */
308 static bool target_set_accept(POSITION y, POSITION x)
309 {
310         grid_type *g_ptr;
311         OBJECT_IDX this_o_idx, next_o_idx = 0;
312
313         if (!(in_bounds(y, x))) return (FALSE);
314
315         /* Player grid is always interesting */
316         if (player_bold(y, x)) return (TRUE);
317
318         if (p_ptr->image) return (FALSE);
319
320         g_ptr = &current_floor_ptr->grid_array[y][x];
321
322         /* Visible monsters */
323         if (g_ptr->m_idx)
324         {
325                 monster_type *m_ptr = &current_floor_ptr->m_list[g_ptr->m_idx];
326
327                 /* Visible monsters */
328                 if (m_ptr->ml) return (TRUE);
329         }
330
331         /* Scan all objects in the grid */
332         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
333         {
334                 object_type *o_ptr;
335                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
336                 next_o_idx = o_ptr->next_o_idx;
337
338                 /* Memorized object */
339                 if (o_ptr->marked & OM_FOUND) return (TRUE);
340         }
341
342         /* Interesting memorized features */
343         if (g_ptr->info & (CAVE_MARK))
344         {
345                 /* Notice object features */
346                 if (g_ptr->info & CAVE_OBJECT) return (TRUE);
347
348                 /* Feature code (applying "mimic" field) */
349                 if (have_flag(f_info[get_feat_mimic(g_ptr)].flags, FF_NOTICE)) return TRUE;
350         }
351
352         return (FALSE);
353 }
354
355
356 /*
357  * Prepare the "temp" array for "target_set"
358  *
359  * Return the number of target_able monsters in the set.
360  */
361 static void target_set_prepare(BIT_FLAGS mode)
362 {
363         POSITION y, x;
364         POSITION min_hgt, max_hgt, min_wid, max_wid;
365
366         if (mode & TARGET_KILL)
367         {
368                 /* Inner range */
369                 min_hgt = MAX((p_ptr->y - MAX_RANGE), 0);
370                 max_hgt = MIN((p_ptr->y + MAX_RANGE), current_floor_ptr->height - 1);
371                 min_wid = MAX((p_ptr->x - MAX_RANGE), 0);
372                 max_wid = MIN((p_ptr->x + MAX_RANGE), current_floor_ptr->width - 1);
373         }
374         else /* not targetting */
375         {
376                 /* Inner panel */
377                 min_hgt = panel_row_min;
378                 max_hgt = panel_row_max;
379                 min_wid = panel_col_min;
380                 max_wid = panel_col_max;
381         }
382
383         /* Reset "temp" array */
384         tmp_pos.n = 0;
385
386         /* Scan the current panel */
387         for (y = min_hgt; y <= max_hgt; y++)
388         {
389                 for (x = min_wid; x <= max_wid; x++)
390                 {
391                         grid_type *g_ptr;
392
393                         /* Require "interesting" contents */
394                         if (!target_set_accept(y, x)) continue;
395
396                         g_ptr = &current_floor_ptr->grid_array[y][x];
397
398                         /* Require target_able monsters for "TARGET_KILL" */
399                         if ((mode & (TARGET_KILL)) && !target_able(g_ptr->m_idx)) continue;
400
401                         if ((mode & (TARGET_KILL)) && !target_pet && is_pet(&current_floor_ptr->m_list[g_ptr->m_idx])) continue;
402
403                         /* Save the location */
404                         tmp_pos.x[tmp_pos.n] = x;
405                         tmp_pos.y[tmp_pos.n] = y;
406                         tmp_pos.n++;
407                 }
408         }
409
410         /* Set the sort hooks */
411         if (mode & (TARGET_KILL))
412         {
413                 ang_sort(tmp_pos.x, tmp_pos.y, tmp_pos.n, ang_sort_comp_distance, ang_sort_swap_distance);
414         }
415         else
416         {
417                 ang_sort(tmp_pos.x, tmp_pos.y, tmp_pos.n, ang_sort_comp_importance, ang_sort_swap_distance);
418         }
419
420         if (p_ptr->riding && target_pet && (tmp_pos.n > 1) && (mode & (TARGET_KILL)))
421         {
422                 POSITION tmp;
423
424                 tmp = tmp_pos.y[0];
425                 tmp_pos.y[0] = tmp_pos.y[1];
426                 tmp_pos.y[1] = tmp;
427                 tmp = tmp_pos.x[0];
428                 tmp_pos.x[0] = tmp_pos.x[1];
429                 tmp_pos.x[1] = tmp;
430         }
431 }
432
433 void target_set_prepare_look(void){
434         target_set_prepare(TARGET_LOOK);
435 }
436
437
438 /*
439  * Evaluate number of kill needed to gain level
440  */
441 static void evaluate_monster_exp(char *buf, monster_type *m_ptr)
442 {
443         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
444         u32b num;
445         s32b exp_mon, exp_adv;
446         u32b exp_mon_frac, exp_adv_frac;
447
448         if ((p_ptr->lev >= PY_MAX_LEVEL) || (p_ptr->prace == RACE_ANDROID))
449         {
450                 sprintf(buf,"**");
451                 return;
452         }
453         else if (!ap_r_ptr->r_tkills || (m_ptr->mflag2 & MFLAG2_KAGE))
454         {
455                 if (!p_ptr->wizard)
456                 {
457                         sprintf(buf,"??");
458                         return;
459                 }
460         }
461
462
463         /* The monster's experience point (assuming average monster speed) */
464         exp_mon = ap_r_ptr->mexp * ap_r_ptr->level;
465         exp_mon_frac = 0;
466         s64b_div(&exp_mon, &exp_mon_frac, 0, (p_ptr->max_plv + 2));
467
468
469         /* Total experience value for next level */
470         exp_adv = player_exp[p_ptr->lev -1] * p_ptr->expfact;
471         exp_adv_frac = 0;
472         s64b_div(&exp_adv, &exp_adv_frac, 0, 100);
473
474         /* Experience value need to get */
475         s64b_sub(&exp_adv, &exp_adv_frac, p_ptr->exp, p_ptr->exp_frac);
476
477
478         /* You need to kill at least one monster to get any experience */
479         s64b_add(&exp_adv, &exp_adv_frac, exp_mon, exp_mon_frac);
480         s64b_sub(&exp_adv, &exp_adv_frac, 0, 1);
481
482         /* Extract number of monsters needed */
483         s64b_div(&exp_adv, &exp_adv_frac, exp_mon, exp_mon_frac);
484
485         /* If 999 or more monsters needed, only display "999". */
486         num = MIN(999, exp_adv_frac);
487
488         /* Display the number */
489         sprintf(buf,"%03ld", (long int)num);
490 }
491
492
493 bool show_gold_on_floor = FALSE;
494
495 /*
496  * Examine a grid, return a keypress.
497  *
498  * The "mode" argument contains the "TARGET_LOOK" bit flag, which
499  * indicates that the "space" key should scan through the contents
500  * of the grid, instead of simply returning immediately.  This lets
501  * the "look" command get complete information, without making the
502  * "target" command annoying.
503  *
504  * The "info" argument contains the "commands" which should be shown
505  * inside the "[xxx]" text.  This string must never be empty, or grids
506  * containing monsters will be displayed with an extra comma.
507  *
508  * Note that if a monster is in the grid, we update both the monster
509  * recall info and the health bar info to track that monster.
510  *
511  * Eventually, we may allow multiple objects per grid, or objects
512  * and terrain features in the same grid. 
513  *
514  * This function must handle blindness/hallucination.
515  */
516 static char target_set_aux(POSITION y, POSITION x, BIT_FLAGS mode, concptr info)
517 {
518         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
519         OBJECT_IDX this_o_idx, next_o_idx = 0;
520         concptr s1 = "", s2 = "", s3 = "", x_info = "";
521         bool boring = TRUE;
522         FEAT_IDX feat;
523         feature_type *f_ptr;
524         char query = '\001';
525         char out_val[MAX_NLEN+80];
526         OBJECT_IDX floor_list[23];
527         ITEM_NUMBER floor_num = 0;
528
529         /* Scan all objects in the grid */
530         if (easy_floor)
531         {
532                 floor_num = scan_floor(floor_list, y, x, 0x02);
533
534                 if (floor_num)
535                 {
536                         x_info = _("x物 ", "x,");
537                 }
538         }
539
540         /* Hack -- under the player */
541         if (player_bold(y, x))
542         {
543 #ifdef JP
544                 s1 = "あなたは";
545                 s2 = "の上";
546                 s3 = "にいる";
547 #else
548                 s1 = "You are ";
549                 s2 = "on ";
550 #endif
551         }
552         else
553         {
554                 s1 = _("ターゲット:", "Target:");
555         }
556
557         /* Hack -- hallucination */
558         if (p_ptr->image)
559         {
560                 concptr name = _("何か奇妙な物", "something strange");
561
562                 /* Display a message */
563 #ifdef JP
564                 sprintf(out_val, "%s%s%s%s [%s]", s1, name, s2, s3, info);
565 #else
566                 sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, name, info);
567 #endif
568
569                 prt(out_val, 0, 0);
570                 move_cursor_relative(y, x);
571                 query = inkey();
572
573                 /* Stop on everything but "return" */
574                 if ((query != '\r') && (query != '\n')) return query;
575
576                 /* Repeat forever */
577                 return 0;
578         }
579
580
581         /* Actual monsters */
582         if (g_ptr->m_idx && current_floor_ptr->m_list[g_ptr->m_idx].ml)
583         {
584                 monster_type *m_ptr = &current_floor_ptr->m_list[g_ptr->m_idx];
585                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
586                 GAME_TEXT m_name[MAX_NLEN];
587                 bool recall = FALSE;
588
589                 /* Not boring */
590                 boring = FALSE;
591
592                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
593                 monster_race_track(m_ptr->ap_r_idx);
594                 health_track(g_ptr->m_idx);
595                 handle_stuff();
596
597                 /* Interact */
598                 while (1)
599                 {
600                         char acount[10];
601
602                         /* Recall */
603                         if (recall)
604                         {
605                                 screen_save();
606
607                                 /* Recall on screen */
608                                 screen_roff(m_ptr->ap_r_idx, 0);
609
610                                 /* Hack -- Complete the prompt (again) */
611                                 Term_addstr(-1, TERM_WHITE, format(_("  [r思 %s%s]", "  [r,%s%s]"), x_info, info));
612
613                                 /* Command */
614                                 query = inkey();
615
616                                 screen_load();
617
618                                 /* Normal commands */
619                                 if (query != 'r') break;
620
621                                 /* Toggle recall */
622                                 recall = FALSE;
623
624                                 /* Cleare recall text and repeat */
625                                 continue;
626                         }
627
628                         /*** Normal ***/
629
630                         /* Describe, and prompt for recall */
631                         evaluate_monster_exp(acount, m_ptr);
632
633 #ifdef JP
634                         sprintf(out_val, "[%s]%s%s(%s)%s%s [r思 %s%s]", acount, s1, m_name, look_mon_desc(m_ptr, 0x01), s2, s3, x_info, info);
635 #else
636                         sprintf(out_val, "[%s]%s%s%s%s(%s) [r, %s%s]", acount, s1, s2, s3, m_name, look_mon_desc(m_ptr, 0x01), x_info, info);
637 #endif
638
639                         prt(out_val, 0, 0);
640
641                         /* Place cursor */
642                         move_cursor_relative(y, x);
643
644                         /* Command */
645                         query = inkey();
646
647                         /* Normal commands */
648                         if (query != 'r') break;
649
650                         /* Toggle recall */
651                         recall = TRUE;
652                 }
653
654                 /* Always stop at "normal" keys */
655                 if ((query != '\r') && (query != '\n') && (query != ' ') && (query != 'x')) return query;
656
657                 /* Sometimes stop at "space" key */
658                 if ((query == ' ') && !(mode & (TARGET_LOOK))) return query;
659
660                 /* Change the intro */
661                 s1 = _("それは", "It is ");
662
663                 /* Hack -- take account of gender */
664                 if (ap_r_ptr->flags1 & (RF1_FEMALE)) s1 = _("彼女は", "She is ");
665                 else if (ap_r_ptr->flags1 & (RF1_MALE)) s1 = _("彼は", "He is ");
666
667                 /* Use a preposition */
668 #ifdef JP
669                 s2 = "を";
670                 s3 = "持っている";
671 #else
672                 s2 = "carrying ";
673 #endif
674
675
676                 /* Scan all objects being carried */
677                 for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
678                 {
679                         GAME_TEXT o_name[MAX_NLEN];
680
681                         object_type *o_ptr;
682                         o_ptr = &current_floor_ptr->o_list[this_o_idx];
683                         next_o_idx = o_ptr->next_o_idx;
684
685                         /* Obtain an object description */
686                         object_desc(o_name, o_ptr, 0);
687
688 #ifdef JP
689                         sprintf(out_val, "%s%s%s%s[%s]", s1, o_name, s2, s3, info);
690 #else
691                         sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, o_name, info);
692 #endif
693
694                         prt(out_val, 0, 0);
695                         move_cursor_relative(y, x);
696                         query = inkey();
697
698                         /* Always stop at "normal" keys */
699                         if ((query != '\r') && (query != '\n') && (query != ' ') && (query != 'x')) return query;
700
701                         /* Sometimes stop at "space" key */
702                         if ((query == ' ') && !(mode & (TARGET_LOOK))) return query;
703
704                         /* Change the intro */
705                         s2 = _("をまた", "also carrying ");
706                 }
707
708                 /* Use a preposition */
709 #ifdef JP
710                 s2 = "の上";
711                 s3 = "にいる";
712 #else
713                 s2 = "on ";
714 #endif
715         }
716
717         if (floor_num)
718         {
719                 int min_width = 0;
720
721                 while (1)
722                 {
723                         if (floor_num == 1)
724                         {
725                                 GAME_TEXT o_name[MAX_NLEN];
726
727                                 object_type *o_ptr;
728                                 o_ptr = &current_floor_ptr->o_list[floor_list[0]];
729
730                                 object_desc(o_name, o_ptr, 0);
731
732 #ifdef JP
733                                 sprintf(out_val, "%s%s%s%s[%s]", s1, o_name, s2, s3, info);
734 #else
735                                 sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, o_name, info);
736 #endif
737
738                                 prt(out_val, 0, 0);
739                                 move_cursor_relative(y, x);
740
741                                 /* Command */
742                                 query = inkey();
743
744                                 /* End this grid */
745                                 return query;
746                         }
747
748                         /* Provide one cushion before item listing  */
749                         if (boring)
750                         {
751                                 /* Display rough information about items */
752 #ifdef JP
753                                 sprintf(out_val, "%s %d個のアイテム%s%s ['x'で一覧, %s]", s1, (int)floor_num, s2, s3, info);
754 #else
755                                 sprintf(out_val, "%s%s%sa pile of %d items [x,%s]", s1, s2, s3, (int)floor_num, info);
756 #endif
757
758                                 prt(out_val, 0, 0);
759                                 move_cursor_relative(y, x);
760
761                                 /* Command */
762                                 query = inkey();
763
764                                 /* No request for listing */
765                                 if (query != 'x' && query != ' ') return query;
766                         }
767
768
769                         /** Display list of items **/
770
771                         /* Continue scrolling list if requested */
772                         while (1)
773                         {
774                                 int i;
775                                 OBJECT_IDX o_idx;
776                                 screen_save();
777
778                                 /* Display */
779                                 show_gold_on_floor = TRUE;
780                                 (void)show_floor(0, y, x, &min_width);
781                                 show_gold_on_floor = FALSE;
782
783                                 /* Prompt */
784 #ifdef JP
785                                 sprintf(out_val, "%s %d個のアイテム%s%s [Enterで次へ, %s]", s1, (int)floor_num, s2, s3, info);
786 #else
787                                 sprintf(out_val, "%s%s%sa pile of %d items [Enter,%s]", s1, s2, s3, (int)floor_num, info);
788 #endif
789                                 prt(out_val, 0, 0);
790
791                                 query = inkey();
792                                 screen_load();
793
794                                 /* Exit unless 'Enter' */
795                                 if (query != '\n' && query != '\r')
796                                 {
797                                         return query;
798                                 }
799
800                                 /* Get the object being moved. */
801                                 o_idx = g_ptr->o_idx;
802  
803                                 /* Only rotate a pile of two or more objects. */
804                                 if (!(o_idx && current_floor_ptr->o_list[o_idx].next_o_idx)) continue;
805
806                                 /* Remove the first object from the list. */
807                                 excise_object_idx(o_idx);
808
809                                 /* Find end of the list. */
810                                 i = g_ptr->o_idx;
811                                 while (current_floor_ptr->o_list[i].next_o_idx)
812                                         i = current_floor_ptr->o_list[i].next_o_idx;
813
814                                 /* Add after the last object. */
815                                 current_floor_ptr->o_list[i].next_o_idx = o_idx;
816
817                                 /* Loop and re-display the list */
818                         }
819                 }
820
821                 /* NOTREACHED */
822         }
823
824         /* Scan all objects in the grid */
825         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
826         {
827                 object_type *o_ptr;
828                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
829                 next_o_idx = o_ptr->next_o_idx;
830
831                 if (o_ptr->marked & OM_FOUND)
832                 {
833                         GAME_TEXT o_name[MAX_NLEN];
834
835                         /* Not boring */
836                         boring = FALSE;
837
838                         /* Obtain an object description */
839                         object_desc(o_name, o_ptr, 0);
840
841 #ifdef JP
842                         sprintf(out_val, "%s%s%s%s[%s]", s1, o_name, s2, s3, info);
843 #else
844                         sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, o_name, info);
845 #endif
846
847                         prt(out_val, 0, 0);
848                         move_cursor_relative(y, x);
849                         query = inkey();
850
851                         /* Always stop at "normal" keys */
852                         if ((query != '\r') && (query != '\n') && (query != ' ') && (query != 'x')) return query;
853
854                         /* Sometimes stop at "space" key */
855                         if ((query == ' ') && !(mode & TARGET_LOOK)) return query;
856
857                         /* Change the intro */
858                         s1 = _("それは", "It is ");
859
860                         /* Plurals */
861                         if (o_ptr->number != 1) s1 = _("それらは", "They are ");
862
863                         /* Preposition */
864 #ifdef JP
865                         s2 = "の上";
866                         s3 = "に見える";
867 #else
868                         s2 = "on ";
869 #endif
870
871                 }
872         }
873
874
875         /* Feature code (applying "mimic" field) */
876         feat = get_feat_mimic(g_ptr);
877
878         /* Require knowledge about grid, or ability to see grid */
879         if (!(g_ptr->info & CAVE_MARK) && !player_can_see_bold(y, x))
880         {
881                 /* Forget feature */
882                 feat = feat_none;
883         }
884
885         f_ptr = &f_info[feat];
886
887         /* Terrain feature if needed */
888         if (boring || have_flag(f_ptr->flags, FF_REMEMBER))
889         {
890                 concptr name;
891
892                 /* Hack -- special handling for quest entrances */
893                 if (have_flag(f_ptr->flags, FF_QUEST_ENTER))
894                 {
895                         /* Set the quest number temporary */
896                         IDX old_quest = p_ptr->inside_quest;
897                         int j;
898
899                         /* Clear the text */
900                         for (j = 0; j < 10; j++) quest_text[j][0] = '\0';
901                         quest_text_line = 0;
902
903                         p_ptr->inside_quest = g_ptr->special;
904
905                         /* Get the quest text */
906                         init_flags = INIT_NAME_ONLY;
907
908                         process_dungeon_file("q_info.txt", 0, 0, 0, 0);
909
910                         name = format(_("クエスト「%s」(%d階相当)", "the entrance to the quest '%s'(level %d)"), 
911                                                 quest[g_ptr->special].name, quest[g_ptr->special].level);
912
913                         /* Reset the old quest number */
914                         p_ptr->inside_quest = old_quest;
915                 }
916
917                 /* Hack -- special handling for building doors */
918                 else if (have_flag(f_ptr->flags, FF_BLDG) && !p_ptr->inside_arena)
919                 {
920                         name = building[f_ptr->subtype].name;
921                 }
922                 else if (have_flag(f_ptr->flags, FF_ENTRANCE))
923                 {
924                         name = format(_("%s(%d階相当)", "%s(level %d)"), d_text + d_info[g_ptr->special].text, d_info[g_ptr->special].mindepth);
925                 }
926                 else if (have_flag(f_ptr->flags, FF_TOWN))
927                 {
928                         name = town_info[g_ptr->special].name;
929                 }
930                 else if (p_ptr->wild_mode && (feat == feat_floor))
931                 {
932                         name = _("道", "road");
933                 }
934                 else
935                 {
936                         name = f_name + f_ptr->name;
937                 }
938
939
940                 /* Pick a prefix */
941                 if (*s2 &&
942                     ((!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) ||
943                      (!have_flag(f_ptr->flags, FF_LOS) && !have_flag(f_ptr->flags, FF_TREE)) ||
944                      have_flag(f_ptr->flags, FF_TOWN)))
945                 {
946                         s2 = _("の中", "in ");
947                 }
948
949                 /* Hack -- special introduction for store & building doors -KMW- */
950                 if (have_flag(f_ptr->flags, FF_STORE) ||
951                     have_flag(f_ptr->flags, FF_QUEST_ENTER) ||
952                     (have_flag(f_ptr->flags, FF_BLDG) && !p_ptr->inside_arena) ||
953                     have_flag(f_ptr->flags, FF_ENTRANCE))
954                 {
955                         s2 = _("の入口", "");
956                 }
957 #ifndef JP
958                 else if (have_flag(f_ptr->flags, FF_FLOOR) ||
959                          have_flag(f_ptr->flags, FF_TOWN) ||
960                          have_flag(f_ptr->flags, FF_SHALLOW) ||
961                          have_flag(f_ptr->flags, FF_DEEP))
962                 {
963                         s3 ="";
964                 }
965                 else
966                 {
967                         /* Pick proper indefinite article */
968                         s3 = (is_a_vowel(name[0])) ? "an " : "a ";
969                 }
970 #endif
971
972                 /* Display a message */
973                 if (p_ptr->wizard)
974                 {
975                         char f_idx_str[32];
976                         if (g_ptr->mimic) sprintf(f_idx_str, "%d/%d", g_ptr->feat, g_ptr->mimic);
977                         else sprintf(f_idx_str, "%d", g_ptr->feat);
978 #ifdef JP
979                         sprintf(out_val, "%s%s%s%s[%s] %x %s %d %d %d (%d,%d) %d", s1, name, s2, s3, info, (unsigned int)g_ptr->info, f_idx_str, g_ptr->dist, g_ptr->cost, g_ptr->when, (int)y, (int)x, travel.cost[y][x]);
980 #else
981                         sprintf(out_val, "%s%s%s%s [%s] %x %s %d %d %d (%d,%d)", s1, s2, s3, name, info, g_ptr->info, f_idx_str, g_ptr->dist, g_ptr->cost, g_ptr->when, (int)y, (int)x);
982 #endif
983                 }
984                 else
985 #ifdef JP
986                         sprintf(out_val, "%s%s%s%s[%s]", s1, name, s2, s3, info);
987 #else
988                         sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, name, info);
989 #endif
990
991                 prt(out_val, 0, 0);
992                 move_cursor_relative(y, x);
993                 query = inkey();
994
995                 /* Always stop at "normal" keys */
996                 if ((query != '\r') && (query != '\n') && (query != ' ')) return query;
997         }
998
999         /* Stop on everything but "return" */
1000         if ((query != '\r') && (query != '\n')) return query;
1001
1002         /* Repeat forever */
1003         return 0;
1004 }
1005
1006
1007 /*
1008  * Handle "target" and "look".
1009  *
1010  * Note that this code can be called from "get_aim_dir()".
1011  *
1012  * All locations must be on the current panel.  Consider the use of
1013  * "panel_bounds()" to allow "off-panel" targets, perhaps by using
1014  * some form of "scrolling" the map around the cursor.  
1015  * That is, consider the possibility of "auto-scrolling" the screen
1016  * while the cursor moves around.  This may require changes in the
1017  * "update_monster()" code to allow "visibility" even if off panel, and
1018  * may require dynamic recalculation of the "temp" grid set.
1019  *
1020  * Hack -- targeting/observing an "outer border grid" may induce
1021  * problems, so this is not currently allowed.
1022  *
1023  * The player can use the direction keys to move among "interesting"
1024  * grids in a heuristic manner, or the "space", "+", and "-" keys to
1025  * move through the "interesting" grids in a sequential manner, or
1026  * can enter "location" mode, and use the direction keys to move one
1027  * grid at a time in any direction.  The "t" (set target) command will
1028  * only target a monster (as opposed to a location) if the monster is
1029  * target_able and the "interesting" mode is being used.
1030  *
1031  * The current grid is described using the "look" method above, and
1032  * a new command may be entered at any time, but note that if the
1033  * "TARGET_LOOK" bit flag is set (or if we are in "location" mode,
1034  * where "space" has no obvious meaning) then "space" will scan
1035  * through the description of the current grid until done, instead
1036  * of immediately jumping to the next "interesting" grid.  This
1037  * allows the "target" command to retain its old semantics.
1038  *
1039  * The "*", "+", and "-" keys may always be used to jump immediately
1040  * to the next (or previous) interesting grid, in the proper mode.
1041  *
1042  * The "return" key may always be used to scan through a complete
1043  * grid description (forever).
1044  *
1045  * This command will cancel any old target, even if used from
1046  * inside the "look" command.
1047  */
1048 bool target_set(BIT_FLAGS mode)
1049 {
1050         int i, d, m, t, bd;
1051         POSITION y = p_ptr->y;
1052         POSITION x = p_ptr->x;
1053
1054         bool done = FALSE;
1055         bool flag = TRUE;
1056         char query;
1057         char info[80];
1058         char same_key;
1059         grid_type *g_ptr;
1060         TERM_LEN wid, hgt;
1061         
1062         get_screen_size(&wid, &hgt);
1063
1064         /* Cancel target */
1065         target_who = 0;
1066
1067         if (rogue_like_commands)
1068         {
1069                 same_key = 'x';
1070         }
1071         else
1072         {
1073                 same_key = 'l';
1074         }
1075
1076         /* Prepare the "temp" array */
1077         target_set_prepare(mode);
1078
1079         /* Start near the player */
1080         m = 0;
1081
1082         /* Interact */
1083         while (!done)
1084         {
1085                 /* Interesting grids */
1086                 if (flag && tmp_pos.n)
1087                 {
1088                         y = tmp_pos.y[m];
1089                         x = tmp_pos.x[m];
1090
1091                         /* Set forcus */
1092                         change_panel_xy(y, x);
1093
1094                         if (!(mode & TARGET_LOOK)) prt_path(y, x);
1095
1096                         /* Access */
1097                         g_ptr = &current_floor_ptr->grid_array[y][x];
1098
1099                         /* Allow target */
1100                         if (target_able(g_ptr->m_idx))
1101                         {
1102                                 strcpy(info, _("q止 t決 p自 o現 +次 -前", "q,t,p,o,+,-,<dir>"));
1103                         }
1104
1105                         /* Dis-allow target */
1106                         else
1107                         {
1108                                 strcpy(info, _("q止 p自 o現 +次 -前", "q,p,o,+,-,<dir>"));
1109                         }
1110
1111                         if (cheat_sight)
1112                         {
1113                                 char cheatinfo[30];
1114                                 sprintf(cheatinfo, " LOS:%d, PROJECTABLE:%d",
1115                                         los(p_ptr->y, p_ptr->x, y, x), projectable(p_ptr->y, p_ptr->x, y, x));
1116                                 strcat(info, cheatinfo);
1117                         }
1118                         
1119                         /* Describe and Prompt */
1120                         while (TRUE){
1121                                 query = target_set_aux(y, x, mode, info);
1122                                 if(query)break;
1123                         }
1124
1125                         /* Assume no "direction" */
1126                         d = 0;
1127
1128                         if (use_menu)
1129                         {
1130                                 if (query == '\r') query = 't';
1131                         }  
1132
1133                         /* Analyze */
1134                         switch (query)
1135                         {
1136                                 case ESCAPE:
1137                                 case 'q':
1138                                 {
1139                                         done = TRUE;
1140                                         break;
1141                                 }
1142
1143                                 case 't':
1144                                 case '.':
1145                                 case '5':
1146                                 case '0':
1147                                 {
1148                                         if (target_able(g_ptr->m_idx))
1149                                         {
1150                                                 health_track(g_ptr->m_idx);
1151                                                 target_who = g_ptr->m_idx;
1152                                                 target_row = y;
1153                                                 target_col = x;
1154                                                 done = TRUE;
1155                                         }
1156                                         else
1157                                         {
1158                                                 bell();
1159                                         }
1160                                         break;
1161                                 }
1162
1163                                 case ' ':
1164                                 case '*':
1165                                 case '+':
1166                                 {
1167                                         if (++m == tmp_pos.n)
1168                                         {
1169                                                 m = 0;
1170                                                 if (!expand_list) done = TRUE;
1171                                         }
1172                                         break;
1173                                 }
1174
1175                                 case '-':
1176                                 {
1177                                         if (m-- == 0)
1178                                         {
1179                                                 m = tmp_pos.n - 1;
1180                                                 if (!expand_list) done = TRUE;
1181                                         }
1182                                         break;
1183                                 }
1184
1185                                 case 'p':
1186                                 {
1187                                         /* Recenter the map around the player */
1188                                         verify_panel();
1189                                         p_ptr->update |= (PU_MONSTERS);
1190                                         p_ptr->redraw |= (PR_MAP);
1191                                         p_ptr->window |= (PW_OVERHEAD);
1192                                         handle_stuff();
1193
1194                                         /* Recalculate interesting grids */
1195                                         target_set_prepare(mode);
1196
1197                                         y = p_ptr->y;
1198                                         x = p_ptr->x;
1199                                 }
1200
1201                                 case 'o':
1202                                 {
1203                                         flag = FALSE;
1204                                         break;
1205                                 }
1206
1207                                 case 'm':
1208                                 {
1209                                         break;
1210                                 }
1211
1212                                 default:
1213                                 {
1214                                         if(query == same_key)
1215                                         {
1216                                                 if (++m == tmp_pos.n)
1217                                                 {
1218                                                         m = 0;
1219                                                         if (!expand_list) done = TRUE;
1220                                                 }
1221                                         }
1222                                         else
1223                                         {
1224                                                 /* Extract the action (if any) */
1225                                                 d = get_keymap_dir(query);
1226
1227                                                 if (!d) bell();
1228                                                 break;
1229                                         }
1230                                 }
1231                         }
1232                         /* Hack -- move around */
1233                         if (d)
1234                         {
1235                                 /* Modified to scroll to monster */
1236                                 POSITION y2 = panel_row_min;
1237                                 POSITION x2 = panel_col_min;
1238
1239                                 /* Find a new monster */
1240                                 i = target_pick(tmp_pos.y[m], tmp_pos.x[m], ddy[d], ddx[d]);
1241
1242                                 /* Request to target past last interesting grid */
1243                                 while (flag && (i < 0))
1244                                 {
1245                                         /* Note the change */
1246                                         if (change_panel(ddy[d], ddx[d]))
1247                                         {
1248                                                 int v = tmp_pos.y[m];
1249                                                 int u = tmp_pos.x[m];
1250
1251                                                 /* Recalculate interesting grids */
1252                                                 target_set_prepare(mode);
1253
1254                                                 /* Look at interesting grids */
1255                                                 flag = TRUE;
1256
1257                                                 /* Find a new monster */
1258                                                 i = target_pick(v, u, ddy[d], ddx[d]);
1259
1260                                                 /* Use that grid */
1261                                                 if (i >= 0) m = i;
1262                                         }
1263
1264                                         /* Nothing interesting */
1265                                         else
1266                                         {
1267                                                 POSITION dx = ddx[d];
1268                                                 POSITION dy = ddy[d];
1269
1270                                                 /* Restore previous position */
1271                                                 panel_row_min = y2;
1272                                                 panel_col_min = x2;
1273                                                 panel_bounds_center();
1274
1275                                                 p_ptr->update |= (PU_MONSTERS);
1276                                                 p_ptr->redraw |= (PR_MAP);
1277                                                 p_ptr->window |= (PW_OVERHEAD);
1278                                                 handle_stuff();
1279
1280                                                 /* Recalculate interesting grids */
1281                                                 target_set_prepare(mode);
1282
1283                                                 /* Look at boring grids */
1284                                                 flag = FALSE;
1285
1286                                                 /* Move */
1287                                                 x += dx;
1288                                                 y += dy;
1289
1290                                                 /* Do not move horizontally if unnecessary */
1291                                                 if (((x < panel_col_min + wid / 2) && (dx > 0)) ||
1292                                                          ((x > panel_col_min + wid / 2) && (dx < 0)))
1293                                                 {
1294                                                         dx = 0;
1295                                                 }
1296
1297                                                 /* Do not move vertically if unnecessary */
1298                                                 if (((y < panel_row_min + hgt / 2) && (dy > 0)) ||
1299                                                          ((y > panel_row_min + hgt / 2) && (dy < 0)))
1300                                                 {
1301                                                         dy = 0;
1302                                                 }
1303
1304                                                 /* Apply the motion */
1305                                                 if ((y >= panel_row_min+hgt) || (y < panel_row_min) ||
1306                                                     (x >= panel_col_min+wid) || (x < panel_col_min))
1307                                                 {
1308                                                         if (change_panel(dy, dx)) target_set_prepare(mode);
1309                                                 }
1310
1311                                                 /* Slide into legality */
1312                                                 if (x >= current_floor_ptr->width-1) x = current_floor_ptr->width - 2;
1313                                                 else if (x <= 0) x = 1;
1314
1315                                                 /* Slide into legality */
1316                                                 if (y >= current_floor_ptr->height-1) y = current_floor_ptr->height- 2;
1317                                                 else if (y <= 0) y = 1;
1318                                         }
1319                                 }
1320
1321                                 /* Use that grid */
1322                                 m = i;
1323                         }
1324                 }
1325
1326                 /* Arbitrary grids */
1327                 else
1328                 {
1329                         bool move_fast = FALSE;
1330
1331                         if (!(mode & TARGET_LOOK)) prt_path(y, x);
1332
1333                         /* Access */
1334                         g_ptr = &current_floor_ptr->grid_array[y][x];
1335
1336                         /* Default prompt */
1337                         strcpy(info, _("q止 t決 p自 m近 +次 -前", "q,t,p,m,+,-,<dir>"));
1338
1339                         if (cheat_sight)
1340                         {
1341                                 char cheatinfo[30];
1342                                 sprintf(cheatinfo, " LOS:%d, PROJECTABLE:%d",
1343                                         los(p_ptr->y, p_ptr->x, y, x),
1344                                         projectable(p_ptr->y, p_ptr->x, y, x));
1345                                 strcat(info, cheatinfo);
1346                         }
1347
1348                         /* Describe and Prompt (enable "TARGET_LOOK") */
1349                         while ((query = target_set_aux(y, x, mode | TARGET_LOOK, info)) == 0);
1350
1351                         /* Assume no direction */
1352                         d = 0;
1353
1354                         if (use_menu)
1355                         {
1356                                 if (query == '\r') query = 't';
1357                         }  
1358
1359                         /* Analyze the keypress */
1360                         switch (query)
1361                         {
1362                                 case ESCAPE:
1363                                 case 'q':
1364                                 {
1365                                         done = TRUE;
1366                                         break;
1367                                 }
1368
1369                                 case 't':
1370                                 case '.':
1371                                 case '5':
1372                                 case '0':
1373                                 {
1374                                         target_who = -1;
1375                                         target_row = y;
1376                                         target_col = x;
1377                                         done = TRUE;
1378                                         break;
1379                                 }
1380
1381                                 case 'p':
1382                                 {
1383                                         /* Recenter the map around the player */
1384                                         verify_panel();
1385                                         p_ptr->update |= (PU_MONSTERS);
1386                                         p_ptr->redraw |= (PR_MAP);
1387                                         p_ptr->window |= (PW_OVERHEAD);
1388                                         handle_stuff();
1389
1390                                         /* Recalculate interesting grids */
1391                                         target_set_prepare(mode);
1392
1393                                         y = p_ptr->y;
1394                                         x = p_ptr->x;
1395                                 }
1396
1397                                 case 'o':
1398                                 {
1399                                         break;
1400                                 }
1401
1402                                 case ' ':
1403                                 case '*':
1404                                 case '+':
1405                                 case '-':
1406                                 case 'm':
1407                                 {
1408                                         flag = TRUE;
1409
1410                                         m = 0;
1411                                         bd = 999;
1412
1413                                         /* Pick a nearby monster */
1414                                         for (i = 0; i < tmp_pos.n; i++)
1415                                         {
1416                                                 t = distance(y, x, tmp_pos.y[i], tmp_pos.x[i]);
1417
1418                                                 /* Pick closest */
1419                                                 if (t < bd)
1420                                                 {
1421                                                         m = i;
1422                                                         bd = t;
1423                                                 }
1424                                         }
1425
1426                                         /* Nothing interesting */
1427                                         if (bd == 999) flag = FALSE;
1428
1429                                         break;
1430                                 }
1431
1432                                 default:
1433                                 {
1434                                         /* Extract the action (if any) */
1435                                         d = get_keymap_dir(query);
1436
1437                                         /* XTRA HACK MOVEFAST */
1438                                         if (isupper(query)) move_fast = TRUE;
1439
1440                                         if (!d) bell();
1441                                         break;
1442                                 }
1443                         }
1444
1445                         /* Handle "direction" */
1446                         if (d)
1447                         {
1448                                 POSITION dx = ddx[d];
1449                                 POSITION dy = ddy[d];
1450
1451                                 /* XTRA HACK MOVEFAST */
1452                                 if (move_fast)
1453                                 {
1454                                         int mag = MIN(wid / 2, hgt / 2);
1455                                         x += dx * mag;
1456                                         y += dy * mag;
1457                                 }
1458                                 else
1459                                 {
1460                                         x += dx;
1461                                         y += dy;
1462                                 }
1463
1464                                 /* Do not move horizontally if unnecessary */
1465                                 if (((x < panel_col_min + wid / 2) && (dx > 0)) ||
1466                                          ((x > panel_col_min + wid / 2) && (dx < 0)))
1467                                 {
1468                                         dx = 0;
1469                                 }
1470
1471                                 /* Do not move vertically if unnecessary */
1472                                 if (((y < panel_row_min + hgt / 2) && (dy > 0)) ||
1473                                          ((y > panel_row_min + hgt / 2) && (dy < 0)))
1474                                 {
1475                                         dy = 0;
1476                                 }
1477
1478                                 /* Apply the motion */
1479                                 if ((y >= panel_row_min + hgt) || (y < panel_row_min) ||
1480                                          (x >= panel_col_min + wid) || (x < panel_col_min))
1481                                 {
1482                                         if (change_panel(dy, dx)) target_set_prepare(mode);
1483                                 }
1484
1485                                 /* Slide into legality */
1486                                 if (x >= current_floor_ptr->width-1) x = current_floor_ptr->width - 2;
1487                                 else if (x <= 0) x = 1;
1488
1489                                 /* Slide into legality */
1490                                 if (y >= current_floor_ptr->height-1) y = current_floor_ptr->height- 2;
1491                                 else if (y <= 0) y = 1;
1492                         }
1493                 }
1494         }
1495
1496         /* Forget */
1497         tmp_pos.n = 0;
1498
1499         /* Clear the top line */
1500         prt("", 0, 0);
1501
1502         /* Recenter the map around the player */
1503         verify_panel();
1504         p_ptr->update |= (PU_MONSTERS);
1505         p_ptr->redraw |= (PR_MAP);
1506         p_ptr->window |= (PW_OVERHEAD);
1507         handle_stuff();
1508
1509         /* Failure to set target */
1510         if (!target_who) return (FALSE);
1511
1512         /* Success */
1513         return (TRUE);
1514 }
1515
1516
1517 /*
1518  * Get an "aiming direction" from the user.
1519  *
1520  * The "dir" is loaded with 1,2,3,4,6,7,8,9 for "actual direction", and
1521  * "0" for "current target", and "-1" for "entry aborted".
1522  *
1523  * Note that "Force Target", if set, will pre-empt user interaction,
1524  * if there is a usable target already set.
1525  *
1526  * Note that confusion over-rides any (explicit?) user choice.
1527  */
1528 bool get_aim_dir(DIRECTION *dp)
1529 {
1530         DIRECTION dir;
1531         char    command;
1532         concptr p;
1533         COMMAND_CODE code;
1534
1535         (*dp) = 0;
1536
1537         /* Global direction */
1538         dir = command_dir;
1539
1540         /* Hack -- auto-target if requested */
1541         if (use_old_target && target_okay()) dir = 5;
1542
1543         if (repeat_pull(&code))
1544         {
1545                 /* Confusion? */
1546
1547                 /* Verify */
1548                 if (!(code == 5 && !target_okay()))
1549                 {
1550 /*                      return (TRUE); */
1551                         dir = (DIRECTION)code;
1552                 }
1553         }
1554         *dp = (DIRECTION)code;
1555
1556         /* Ask until satisfied */
1557         while (!dir)
1558         {
1559                 /* Choose a prompt */
1560                 if (!target_okay())
1561                 {
1562                         p = _("方向 ('*'でターゲット選択, ESCで中断)? ", "Direction ('*' to choose a target, Escape to cancel)? ");
1563                 }
1564                 else
1565                 {
1566                         p = _("方向 ('5'でターゲットへ, '*'でターゲット再選択, ESCで中断)? ", "Direction ('5' for target, '*' to re-target, Escape to cancel)? ");
1567                 }
1568
1569                 /* Get a command (or Cancel) */
1570                 if (!get_com(p, &command, TRUE)) break;
1571
1572                 if (use_menu)
1573                 {
1574                         if (command == '\r') command = 't';
1575                 }  
1576
1577                 /* Convert various keys to "standard" keys */
1578                 switch (command)
1579                 {
1580                         /* Use current target */
1581                         case 'T':
1582                         case 't':
1583                         case '.':
1584                         case '5':
1585                         case '0':
1586                         {
1587                                 dir = 5;
1588                                 break;
1589                         }
1590
1591                         /* Set new target */
1592                         case '*':
1593                         case ' ':
1594                         case '\r':
1595                         {
1596                                 if (target_set(TARGET_KILL)) dir = 5;
1597                                 break;
1598                         }
1599
1600                         default:
1601                         {
1602                                 /* Extract the action (if any) */
1603                                 dir = get_keymap_dir(command);
1604
1605                                 break;
1606                         }
1607                 }
1608
1609                 /* Verify requested targets */
1610                 if ((dir == 5) && !target_okay()) dir = 0;
1611
1612                 /* Error */
1613                 if (!dir) bell();
1614         }
1615
1616         /* No direction */
1617         if (!dir)
1618         {
1619                 project_length = 0; /* reset to default */
1620                 return (FALSE);
1621         }
1622
1623         /* Save the direction */
1624         command_dir = dir;
1625
1626         /* Check for confusion */
1627         if (p_ptr->confused)
1628         {
1629                 /* Random direction */
1630                 dir = ddd[randint0(8)];
1631         }
1632
1633         /* Notice confusion */
1634         if (command_dir != dir)
1635         {
1636                 /* Warn the user */
1637                 msg_print(_("あなたは混乱している。", "You are confused."));
1638         }
1639
1640         /* Save direction */
1641         (*dp) = dir;
1642
1643 /*      repeat_push(dir); */
1644         repeat_push((COMMAND_CODE)command_dir);
1645
1646         /* A "valid" direction was entered */
1647         return (TRUE);
1648 }
1649
1650
1651 bool get_direction(DIRECTION *dp, bool allow_under, bool with_steed)
1652 {
1653         DIRECTION dir;
1654         concptr prompt;
1655         COMMAND_CODE code;
1656
1657         (*dp) = 0;
1658
1659         /* Global direction */
1660         dir = command_dir;
1661
1662         if (repeat_pull(&code))
1663         {
1664                 dir = (DIRECTION)code;
1665                 /*              return (TRUE); */
1666         }
1667         *dp = (DIRECTION)code;
1668
1669         if (allow_under)
1670         {
1671                 prompt = _("方向 ('.'足元, ESCで中断)? ", "Direction ('.' at feet, Escape to cancel)? ");
1672         }
1673         else
1674         {
1675                 prompt = _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
1676         }
1677
1678         /* Get a direction */
1679         while (!dir)
1680         {
1681                 char ch;
1682
1683                 /* Get a command (or Cancel) */
1684                 if (!get_com(prompt, &ch, TRUE)) break;
1685
1686                 /* Look down */
1687                 if ((allow_under) && ((ch == '5') || (ch == '-') || (ch == '.')))
1688                 {
1689                         dir = 5;
1690                 }
1691                 else
1692                 {
1693                         /* Look up the direction */
1694                         dir = get_keymap_dir(ch);
1695
1696                         if (!dir) bell();
1697                 }
1698         }
1699
1700         /* Prevent weirdness */
1701         if ((dir == 5) && (!allow_under)) dir = 0;
1702
1703         /* Aborted */
1704         if (!dir) return (FALSE);
1705
1706         /* Save desired direction */
1707         command_dir = dir;
1708
1709         /* Apply "confusion" */
1710         if (p_ptr->confused)
1711         {
1712                 /* Standard confusion */
1713                 if (randint0(100) < 75)
1714                 {
1715                         /* Random direction */
1716                         dir = ddd[randint0(8)];
1717                 }
1718         }
1719         else if (p_ptr->riding && with_steed)
1720         {
1721                 monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1722                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1723
1724                 if (MON_CONFUSED(m_ptr))
1725                 {
1726                         /* Standard confusion */
1727                         if (randint0(100) < 75)
1728                         {
1729                                 /* Random direction */
1730                                 dir = ddd[randint0(8)];
1731                         }
1732                 }
1733                 else if ((r_ptr->flags1 & RF1_RAND_50) && (r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 50))
1734                 {
1735                         /* Random direction */
1736                         dir = ddd[randint0(8)];
1737                 }
1738                 else if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 25))
1739                 {
1740                         /* Random direction */
1741                         dir = ddd[randint0(8)];
1742                 }
1743         }
1744
1745         /* Notice confusion */
1746         if (command_dir != dir)
1747         {
1748                 if (p_ptr->confused)
1749                 {
1750                         /* Warn the user */
1751                         msg_print(_("あなたは混乱している。", "You are confused."));
1752                 }
1753                 else
1754                 {
1755                         GAME_TEXT m_name[MAX_NLEN];
1756                         monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1757
1758                         monster_desc(m_name, m_ptr, 0);
1759                         if (MON_CONFUSED(m_ptr))
1760                         {
1761                                 msg_format(_("%sは混乱している。", "%^s is confusing."), m_name);
1762                         }
1763                         else
1764                         {
1765                                 msg_format(_("%sは思い通りに動いてくれない。", "You cannot control %s."), m_name);
1766                         }
1767                 }
1768         }
1769
1770         /* Save direction */
1771         (*dp) = dir;
1772
1773         /*      repeat_push(dir); */
1774         repeat_push((COMMAND_CODE)command_dir);
1775
1776         /* Success */
1777         return (TRUE);
1778 }
1779
1780 /*
1781  * @brief 進行方向を指定する(騎乗対象の混乱の影響を受ける) / Request a "movement" direction (1,2,3,4,6,7,8,9) from the user,
1782  * and place it into "command_dir", unless we already have one.
1783  *
1784  * This function should be used for all "repeatable" commands, such as
1785  * run, walk, open, close, bash, disarm, spike, tunnel, etc, as well
1786  * as all commands which must reference a grid adjacent to the player,
1787  * and which may not reference the grid under the player.  Note that,
1788  * for example, it is no longer possible to "disarm" or "open" chests
1789  * in the same grid as the player.
1790  *
1791  * Direction "5" is illegal and will (cleanly) abort the command.
1792  *
1793  * This function tracks and uses the "global direction", and uses
1794  * that as the "desired direction", to which "confusion" is applied.
1795  */
1796 bool get_rep_dir(DIRECTION *dp, bool under)
1797 {
1798         DIRECTION dir;
1799         concptr prompt;
1800         COMMAND_CODE code;
1801
1802         (*dp) = 0;
1803
1804         /* Global direction */
1805         dir = command_dir;
1806
1807         if (repeat_pull(&code))
1808         {
1809                 dir = (DIRECTION)code;
1810 /*              return (TRUE); */
1811         }
1812         *dp = (DIRECTION)code;
1813
1814         if (under)
1815         {
1816                 prompt = _("方向 ('.'足元, ESCで中断)? ", "Direction ('.' at feet, Escape to cancel)? ");
1817         }
1818         else
1819         {
1820                 prompt = _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
1821         }
1822         
1823         /* Get a direction */
1824         while (!dir)
1825         {
1826                 char ch;
1827
1828                 /* Get a command (or Cancel) */
1829                 if (!get_com(prompt, &ch, TRUE)) break;
1830
1831                 /* Look down */
1832                 if ((under) && ((ch == '5') || (ch == '-') || (ch == '.')))
1833                 {
1834                         dir = 5;
1835                 }
1836                 else
1837                 {
1838                         /* Look up the direction */
1839                         dir = get_keymap_dir(ch);
1840
1841                         if (!dir) bell();
1842                 }
1843         }
1844
1845         /* Prevent weirdness */
1846         if ((dir == 5) && (!under)) dir = 0;
1847
1848         /* Aborted */
1849         if (!dir) return (FALSE);
1850
1851         /* Save desired direction */
1852         command_dir = dir;
1853
1854         /* Apply "confusion" */
1855         if (p_ptr->confused)
1856         {
1857                 /* Standard confusion */
1858                 if (randint0(100) < 75)
1859                 {
1860                         /* Random direction */
1861                         dir = ddd[randint0(8)];
1862                 }
1863         }
1864         else if (p_ptr->riding)
1865         {
1866                 monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1867                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1868
1869                 if (MON_CONFUSED(m_ptr))
1870                 {
1871                         /* Standard confusion */
1872                         if (randint0(100) < 75)
1873                         {
1874                                 /* Random direction */
1875                                 dir = ddd[randint0(8)];
1876                         }
1877                 }
1878                 else if ((r_ptr->flags1 & RF1_RAND_50) && (r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 50))
1879                 {
1880                         /* Random direction */
1881                         dir = ddd[randint0(8)];
1882                 }
1883                 else if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 25))
1884                 {
1885                         /* Random direction */
1886                         dir = ddd[randint0(8)];
1887                 }
1888         }
1889
1890         /* Notice confusion */
1891         if (command_dir != dir)
1892         {
1893                 if (p_ptr->confused)
1894                 {
1895                         /* Warn the user */
1896                         msg_print(_("あなたは混乱している。", "You are confused."));
1897                 }
1898                 else
1899                 {
1900                         GAME_TEXT m_name[MAX_NLEN];
1901                         monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1902
1903                         monster_desc(m_name, m_ptr, 0);
1904                         if (MON_CONFUSED(m_ptr))
1905                         {
1906                                 msg_format(_("%sは混乱している。", "%^s is confusing."), m_name);
1907                         }
1908                         else
1909                         {
1910                                 msg_format(_("%sは思い通りに動いてくれない。", "You cannot control %s."), m_name);
1911                         }
1912                 }
1913         }
1914
1915         /* Save direction */
1916         (*dp) = dir;
1917
1918 /*      repeat_push(dir); */
1919         repeat_push((COMMAND_CODE)command_dir);
1920
1921         /* Success */
1922         return (TRUE);
1923 }
1924
1925
1926 /*
1927  * XAngband: determine if a given location is "interesting"
1928  * based on target_set_accept function.
1929  */
1930 static bool tgt_pt_accept(POSITION y, POSITION x)
1931 {
1932         grid_type *g_ptr;
1933
1934         if (!(in_bounds(y, x))) return (FALSE);
1935
1936         /* Player grid is always interesting */
1937         if ((y == p_ptr->y) && (x == p_ptr->x)) return (TRUE);
1938
1939         if (p_ptr->image) return (FALSE);
1940
1941         g_ptr = &current_floor_ptr->grid_array[y][x];
1942
1943         /* Interesting memorized features */
1944         if (g_ptr->info & (CAVE_MARK))
1945         {
1946                 /* Notice stairs */
1947                 if (cave_have_flag_grid(g_ptr, FF_LESS)) return (TRUE);
1948                 if (cave_have_flag_grid(g_ptr, FF_MORE)) return (TRUE);
1949
1950                 /* Notice quest features */
1951                 if (cave_have_flag_grid(g_ptr, FF_QUEST_ENTER)) return (TRUE);
1952                 if (cave_have_flag_grid(g_ptr, FF_QUEST_EXIT)) return (TRUE);
1953         }
1954
1955         return (FALSE);
1956 }
1957
1958
1959 /*
1960  * XAngband: Prepare the "temp" array for "tget_pt"
1961  * based on target_set_prepare funciton.
1962  */
1963 static void tgt_pt_prepare(void)
1964 {
1965         POSITION y, x;
1966
1967         /* Reset "temp" array */
1968         tmp_pos.n = 0;
1969
1970         if (!expand_list) return;
1971
1972         /* Scan the current panel */
1973         for (y = 1; y < current_floor_ptr->height; y++)
1974         {
1975                 for (x = 1; x < current_floor_ptr->width; x++)
1976                 {
1977                         /* Require "interesting" contents */
1978                         if (!tgt_pt_accept(y, x)) continue;
1979
1980                         /* Save the location */
1981                         tmp_pos.x[tmp_pos.n] = x;
1982                         tmp_pos.y[tmp_pos.n] = y;
1983                         tmp_pos.n++;
1984                 }
1985         }
1986
1987         /* Sort the positions */
1988         ang_sort(tmp_pos.x, tmp_pos.y, tmp_pos.n, ang_sort_comp_distance, ang_sort_swap_distance);
1989 }
1990
1991 /*
1992  * old -- from PsiAngband.
1993  */
1994 bool tgt_pt(POSITION *x_ptr, POSITION *y_ptr)
1995 {
1996         char ch = 0;
1997         int d, n = 0;
1998         POSITION x, y;
1999         bool success = FALSE;
2000
2001         TERM_LEN wid, hgt;
2002
2003         get_screen_size(&wid, &hgt);
2004
2005         x = p_ptr->x;
2006         y = p_ptr->y;
2007
2008         if (expand_list) 
2009         {
2010                 tgt_pt_prepare();
2011         }
2012
2013         msg_print(_("場所を選んでスペースキーを押して下さい。", "Select a point and press space."));
2014         msg_flag = FALSE; /* prevents "-more-" message. */
2015
2016         while ((ch != ESCAPE) && !success)
2017         {
2018                 bool move_fast = FALSE;
2019
2020                 move_cursor_relative(y, x);
2021                 ch = inkey();
2022                 switch (ch)
2023                 {
2024                 case ESCAPE:
2025                         break;
2026                 case ' ':
2027                 case 't':
2028                 case '.':
2029                 case '5':
2030                 case '0':
2031                         /* illegal place */
2032                         if (player_bold(y, x)) ch = 0;
2033
2034                         /* okay place */
2035                         else success = TRUE;
2036
2037                         break;
2038
2039                 /* XAngband: Move cursor to stairs */
2040                 case '>':
2041                 case '<':
2042                         if (expand_list && tmp_pos.n)
2043                         {
2044                                 int dx, dy;
2045                                 int cx = (panel_col_min + panel_col_max) / 2;
2046                                 int cy = (panel_row_min + panel_row_max) / 2;
2047
2048                                 n++;
2049
2050                                 /* Skip stairs which have defferent distance */
2051                                 for (; n < tmp_pos.n; ++ n)
2052                                 {
2053                                         grid_type *g_ptr = &current_floor_ptr->grid_array[tmp_pos.y[n]][tmp_pos.x[n]];
2054
2055                                         if (cave_have_flag_grid(g_ptr, FF_STAIRS) &&
2056                                             cave_have_flag_grid(g_ptr, ch == '>' ? FF_MORE : FF_LESS))
2057                                         {
2058                                                 /* Found */
2059                                                 break;
2060                                         }
2061                                 }
2062
2063                                 if (n == tmp_pos.n)     /* Loop out taget list */
2064                                 {
2065                                         n = 0;
2066                                         y = p_ptr->y;
2067                                         x = p_ptr->x;
2068                                         verify_panel(); /* Move cursor to player */
2069
2070                                         p_ptr->update |= (PU_MONSTERS);
2071
2072                                         p_ptr->redraw |= (PR_MAP);
2073
2074                                         p_ptr->window |= (PW_OVERHEAD);
2075                                         handle_stuff();
2076                                 }
2077                                 else    /* move cursor to next stair and change panel */
2078                                 {
2079                                         y = tmp_pos.y[n];
2080                                         x = tmp_pos.x[n];
2081
2082                                         dy = 2 * (y - cy) / hgt;
2083                                         dx = 2 * (x - cx) / wid;
2084                                         if (dy || dx) change_panel(dy, dx);
2085                                 }
2086                         }
2087                         break;
2088
2089                 default:
2090                         /* Look up the direction */
2091                         d = get_keymap_dir(ch);
2092
2093                         /* XTRA HACK MOVEFAST */
2094                         if (isupper(ch)) move_fast = TRUE;
2095
2096                         /* Handle "direction" */
2097                         if (d)
2098                         {
2099                                 int dx = ddx[d];
2100                                 int dy = ddy[d];
2101
2102                                 /* XTRA HACK MOVEFAST */
2103                                 if (move_fast)
2104                                 {
2105                                         int mag = MIN(wid / 2, hgt / 2);
2106                                         x += dx * mag;
2107                                         y += dy * mag;
2108                                 }
2109                                 else
2110                                 {
2111                                         x += dx;
2112                                         y += dy;
2113                                 }
2114
2115                                 /* Do not move horizontally if unnecessary */
2116                                 if (((x < panel_col_min + wid / 2) && (dx > 0)) ||
2117                                          ((x > panel_col_min + wid / 2) && (dx < 0)))
2118                                 {
2119                                         dx = 0;
2120                                 }
2121
2122                                 /* Do not move vertically if unnecessary */
2123                                 if (((y < panel_row_min + hgt / 2) && (dy > 0)) ||
2124                                          ((y > panel_row_min + hgt / 2) && (dy < 0)))
2125                                 {
2126                                         dy = 0;
2127                                 }
2128
2129                                 /* Apply the motion */
2130                                 if ((y >= panel_row_min + hgt) || (y < panel_row_min) ||
2131                                          (x >= panel_col_min + wid) || (x < panel_col_min))
2132                                 {
2133                                         /* if (change_panel(dy, dx)) target_set_prepare(mode); */
2134                                         change_panel(dy, dx);
2135                                 }
2136
2137                                 /* Slide into legality */
2138                                 if (x >= current_floor_ptr->width-1) x = current_floor_ptr->width - 2;
2139                                 else if (x <= 0) x = 1;
2140
2141                                 /* Slide into legality */
2142                                 if (y >= current_floor_ptr->height-1) y = current_floor_ptr->height- 2;
2143                                 else if (y <= 0) y = 1;
2144
2145                         }
2146                         break;
2147                 }
2148         }
2149
2150         /* Clear the top line */
2151         prt("", 0, 0);
2152
2153         /* Recenter the map around the player */
2154         verify_panel();
2155
2156         p_ptr->update |= (PU_MONSTERS);
2157
2158         p_ptr->redraw |= (PR_MAP);
2159
2160         p_ptr->window |= (PW_OVERHEAD);
2161         handle_stuff();
2162
2163         *x_ptr = x;
2164         *y_ptr = y;
2165         return success;
2166 }
2167
2168
2169 bool get_hack_dir(DIRECTION *dp)
2170 {
2171         DIRECTION dir;
2172         concptr    p;
2173         char    command;
2174
2175         (*dp) = 0;
2176
2177         /* Global direction */
2178         dir = 0;
2179
2180         /* (No auto-targeting) */
2181
2182         /* Ask until satisfied */
2183         while (!dir)
2184         {
2185                 /* Choose a prompt */
2186                 if (!target_okay())
2187                 {
2188                         p = _("方向 ('*'でターゲット選択, ESCで中断)? ", "Direction ('*' to choose a target, Escape to cancel)? ");
2189                 }
2190                 else
2191                 {
2192                         p = _("方向 ('5'でターゲットへ, '*'でターゲット再選択, ESCで中断)? ", "Direction ('5' for target, '*' to re-target, Escape to cancel)? ");
2193                 }
2194
2195                 /* Get a command (or Cancel) */
2196                 if (!get_com(p, &command, TRUE)) break;
2197
2198                 if (use_menu)
2199                 {
2200                         if (command == '\r') command = 't';
2201                 }  
2202
2203                 /* Convert various keys to "standard" keys */
2204                 switch (command)
2205                 {
2206                         /* Use current target */
2207                         case 'T':
2208                         case 't':
2209                         case '.':
2210                         case '5':
2211                         case '0':
2212                         {
2213                                 dir = 5;
2214                                 break;
2215                         }
2216
2217                         /* Set new target */
2218                         case '*':
2219                         case ' ':
2220                         case '\r':
2221                         {
2222                                 if (target_set(TARGET_KILL)) dir = 5;
2223                                 break;
2224                         }
2225
2226                         default:
2227                         {
2228                                 /* Look up the direction */
2229                                 dir = get_keymap_dir(command);
2230
2231                                 break;
2232                         }
2233                 }
2234
2235                 /* Verify requested targets */
2236                 if ((dir == 5) && !target_okay()) dir = 0;
2237
2238                 /* Error */
2239                 if (!dir) bell();
2240         }
2241
2242         /* No direction */
2243         if (!dir) return (FALSE);
2244
2245         /* Save the direction */
2246         command_dir = dir;
2247
2248         /* Check for confusion */
2249         if (p_ptr->confused)
2250         {
2251                 /* Random direction */
2252                 dir = ddd[randint0(8)];
2253         }
2254
2255         /* Notice confusion */
2256         if (command_dir != dir)
2257         {
2258                 /* Warn the user */
2259                 msg_print(_("あなたは混乱している。", "You are confused."));
2260         }
2261
2262         /* Save direction */
2263         (*dp) = dir;
2264
2265         /* A "valid" direction was entered */
2266         return (TRUE);
2267 }