OSDN Git Service

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