OSDN Git Service

[Refactor] #37353 TERM_* 定義を term.h へ移動.
[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                         /* Recall */
616                         if (recall)
617                         {
618                                 screen_save();
619
620                                 /* Recall on screen */
621                                 screen_roff(m_ptr->ap_r_idx, 0);
622
623                                 /* Hack -- Complete the prompt (again) */
624                                 Term_addstr(-1, TERM_WHITE, format(_("  [r思 %s%s]", "  [r,%s%s]"), x_info, info));
625
626                                 query = inkey();
627
628                                 screen_load();
629
630                                 /* Normal commands */
631                                 if (query != 'r') break;
632
633                                 /* Toggle recall */
634                                 recall = FALSE;
635
636                                 /* Cleare recall text and repeat */
637                                 continue;
638                         }
639
640                         /*** Normal ***/
641
642                         /* Describe, and prompt for recall */
643                         evaluate_monster_exp(acount, m_ptr);
644
645 #ifdef JP
646                         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);
647 #else
648                         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);
649 #endif
650
651                         prt(out_val, 0, 0);
652
653                         /* Place cursor */
654                         move_cursor_relative(y, x);
655
656                         query = inkey();
657
658                         /* Normal commands */
659                         if (query != 'r') break;
660
661                         /* Toggle recall */
662                         recall = TRUE;
663                 }
664
665                 /* Always stop at "normal" keys */
666                 if ((query != '\r') && (query != '\n') && (query != ' ') && (query != 'x')) return query;
667
668                 /* Sometimes stop at "space" key */
669                 if ((query == ' ') && !(mode & (TARGET_LOOK))) return query;
670
671                 /* Change the intro */
672                 s1 = _("それは", "It is ");
673
674                 /* Hack -- take account of gender */
675                 if (ap_r_ptr->flags1 & (RF1_FEMALE)) s1 = _("彼女は", "She is ");
676                 else if (ap_r_ptr->flags1 & (RF1_MALE)) s1 = _("彼は", "He is ");
677
678                 /* Use a preposition */
679 #ifdef JP
680                 s2 = "を";
681                 s3 = "持っている";
682 #else
683                 s2 = "carrying ";
684 #endif
685
686
687                 /* Scan all objects being carried */
688                 for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
689                 {
690                         GAME_TEXT o_name[MAX_NLEN];
691
692                         object_type *o_ptr;
693                         o_ptr = &current_floor_ptr->o_list[this_o_idx];
694                         next_o_idx = o_ptr->next_o_idx;
695
696                         object_desc(o_name, o_ptr, 0);
697
698 #ifdef JP
699                         sprintf(out_val, "%s%s%s%s[%s]", s1, o_name, s2, s3, info);
700 #else
701                         sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, o_name, info);
702 #endif
703
704                         prt(out_val, 0, 0);
705                         move_cursor_relative(y, x);
706                         query = inkey();
707
708                         /* Always stop at "normal" keys */
709                         if ((query != '\r') && (query != '\n') && (query != ' ') && (query != 'x')) return query;
710
711                         /* Sometimes stop at "space" key */
712                         if ((query == ' ') && !(mode & (TARGET_LOOK))) return query;
713
714                         /* Change the intro */
715                         s2 = _("をまた", "also carrying ");
716                 }
717
718                 /* Use a preposition */
719 #ifdef JP
720                 s2 = "の上";
721                 s3 = "にいる";
722 #else
723                 s2 = "on ";
724 #endif
725         }
726
727         if (floor_num)
728         {
729                 int min_width = 0;
730
731                 while (1)
732                 {
733                         if (floor_num == 1)
734                         {
735                                 GAME_TEXT o_name[MAX_NLEN];
736
737                                 object_type *o_ptr;
738                                 o_ptr = &current_floor_ptr->o_list[floor_list[0]];
739
740                                 object_desc(o_name, o_ptr, 0);
741
742 #ifdef JP
743                                 sprintf(out_val, "%s%s%s%s[%s]", s1, o_name, s2, s3, info);
744 #else
745                                 sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, o_name, info);
746 #endif
747
748                                 prt(out_val, 0, 0);
749                                 move_cursor_relative(y, x);
750
751                                 query = inkey();
752
753                                 /* End this grid */
754                                 return query;
755                         }
756
757                         /* Provide one cushion before item listing  */
758                         if (boring)
759                         {
760                                 /* Display rough information about items */
761 #ifdef JP
762                                 sprintf(out_val, "%s %d個のアイテム%s%s ['x'で一覧, %s]", s1, (int)floor_num, s2, s3, info);
763 #else
764                                 sprintf(out_val, "%s%s%sa pile of %d items [x,%s]", s1, s2, s3, (int)floor_num, info);
765 #endif
766
767                                 prt(out_val, 0, 0);
768                                 move_cursor_relative(y, x);
769
770                                 query = inkey();
771
772                                 /* No request for listing */
773                                 if (query != 'x' && query != ' ') return query;
774                         }
775
776
777                         /** Display list of items **/
778
779                         /* Continue scrolling list if requested */
780                         while (1)
781                         {
782                                 int i;
783                                 OBJECT_IDX o_idx;
784                                 screen_save();
785
786                                 /* Display */
787                                 show_gold_on_floor = TRUE;
788                                 (void)show_floor(0, y, x, &min_width);
789                                 show_gold_on_floor = FALSE;
790
791                                 /* Prompt */
792 #ifdef JP
793                                 sprintf(out_val, "%s %d個のアイテム%s%s [Enterで次へ, %s]", s1, (int)floor_num, s2, s3, info);
794 #else
795                                 sprintf(out_val, "%s%s%sa pile of %d items [Enter,%s]", s1, s2, s3, (int)floor_num, info);
796 #endif
797                                 prt(out_val, 0, 0);
798
799                                 query = inkey();
800                                 screen_load();
801
802                                 /* Exit unless 'Enter' */
803                                 if (query != '\n' && query != '\r')
804                                 {
805                                         return query;
806                                 }
807
808                                 /* Get the object being moved. */
809                                 o_idx = g_ptr->o_idx;
810  
811                                 /* Only rotate a pile of two or more objects. */
812                                 if (!(o_idx && current_floor_ptr->o_list[o_idx].next_o_idx)) continue;
813
814                                 /* Remove the first object from the list. */
815                                 excise_object_idx(o_idx);
816
817                                 /* Find end of the list. */
818                                 i = g_ptr->o_idx;
819                                 while (current_floor_ptr->o_list[i].next_o_idx)
820                                         i = current_floor_ptr->o_list[i].next_o_idx;
821
822                                 /* Add after the last object. */
823                                 current_floor_ptr->o_list[i].next_o_idx = o_idx;
824
825                                 /* Loop and re-display the list */
826                         }
827                 }
828
829                 /* NOTREACHED */
830         }
831
832         /* Scan all objects in the grid */
833         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
834         {
835                 object_type *o_ptr;
836                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
837                 next_o_idx = o_ptr->next_o_idx;
838
839                 if (o_ptr->marked & OM_FOUND)
840                 {
841                         GAME_TEXT o_name[MAX_NLEN];
842
843                         /* Not boring */
844                         boring = FALSE;
845
846                         object_desc(o_name, o_ptr, 0);
847
848 #ifdef JP
849                         sprintf(out_val, "%s%s%s%s[%s]", s1, o_name, s2, s3, info);
850 #else
851                         sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, o_name, info);
852 #endif
853
854                         prt(out_val, 0, 0);
855                         move_cursor_relative(y, x);
856                         query = inkey();
857
858                         /* Always stop at "normal" keys */
859                         if ((query != '\r') && (query != '\n') && (query != ' ') && (query != 'x')) return query;
860
861                         /* Sometimes stop at "space" key */
862                         if ((query == ' ') && !(mode & TARGET_LOOK)) return query;
863
864                         /* Change the intro */
865                         s1 = _("それは", "It is ");
866
867                         /* Plurals */
868                         if (o_ptr->number != 1) s1 = _("それらは", "They are ");
869
870                         /* Preposition */
871 #ifdef JP
872                         s2 = "の上";
873                         s3 = "に見える";
874 #else
875                         s2 = "on ";
876 #endif
877
878                 }
879         }
880
881
882         /* Feature code (applying "mimic" field) */
883         feat = get_feat_mimic(g_ptr);
884
885         /* Require knowledge about grid, or ability to see grid */
886         if (!(g_ptr->info & CAVE_MARK) && !player_can_see_bold(y, x))
887         {
888                 /* Forget feature */
889                 feat = feat_none;
890         }
891
892         f_ptr = &f_info[feat];
893
894         /* Terrain feature if needed */
895         if (boring || have_flag(f_ptr->flags, FF_REMEMBER))
896         {
897                 concptr name;
898
899                 /* Hack -- special handling for quest entrances */
900                 if (have_flag(f_ptr->flags, FF_QUEST_ENTER))
901                 {
902                         /* Set the quest number temporary */
903                         IDX old_quest = p_ptr->inside_quest;
904                         int j;
905
906                         /* Clear the text */
907                         for (j = 0; j < 10; j++) quest_text[j][0] = '\0';
908                         quest_text_line = 0;
909
910                         p_ptr->inside_quest = g_ptr->special;
911
912                         /* Get the quest text */
913                         init_flags = INIT_NAME_ONLY;
914
915                         process_dungeon_file("q_info.txt", 0, 0, 0, 0);
916
917                         name = format(_("クエスト「%s」(%d階相当)", "the entrance to the quest '%s'(level %d)"), 
918                                                 quest[g_ptr->special].name, quest[g_ptr->special].level);
919
920                         /* Reset the old quest number */
921                         p_ptr->inside_quest = old_quest;
922                 }
923
924                 /* Hack -- special handling for building doors */
925                 else if (have_flag(f_ptr->flags, FF_BLDG) && !p_ptr->inside_arena)
926                 {
927                         name = building[f_ptr->subtype].name;
928                 }
929                 else if (have_flag(f_ptr->flags, FF_ENTRANCE))
930                 {
931                         name = format(_("%s(%d階相当)", "%s(level %d)"), d_text + d_info[g_ptr->special].text, d_info[g_ptr->special].mindepth);
932                 }
933                 else if (have_flag(f_ptr->flags, FF_TOWN))
934                 {
935                         name = town_info[g_ptr->special].name;
936                 }
937                 else if (p_ptr->wild_mode && (feat == feat_floor))
938                 {
939                         name = _("道", "road");
940                 }
941                 else
942                 {
943                         name = f_name + f_ptr->name;
944                 }
945
946
947                 /* Pick a prefix */
948                 if (*s2 &&
949                     ((!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) ||
950                      (!have_flag(f_ptr->flags, FF_LOS) && !have_flag(f_ptr->flags, FF_TREE)) ||
951                      have_flag(f_ptr->flags, FF_TOWN)))
952                 {
953                         s2 = _("の中", "in ");
954                 }
955
956                 /* Hack -- special introduction for store & building doors -KMW- */
957                 if (have_flag(f_ptr->flags, FF_STORE) ||
958                     have_flag(f_ptr->flags, FF_QUEST_ENTER) ||
959                     (have_flag(f_ptr->flags, FF_BLDG) && !p_ptr->inside_arena) ||
960                     have_flag(f_ptr->flags, FF_ENTRANCE))
961                 {
962                         s2 = _("の入口", "");
963                 }
964 #ifndef JP
965                 else if (have_flag(f_ptr->flags, FF_FLOOR) ||
966                          have_flag(f_ptr->flags, FF_TOWN) ||
967                          have_flag(f_ptr->flags, FF_SHALLOW) ||
968                          have_flag(f_ptr->flags, FF_DEEP))
969                 {
970                         s3 ="";
971                 }
972                 else
973                 {
974                         /* Pick proper indefinite article */
975                         s3 = (is_a_vowel(name[0])) ? "an " : "a ";
976                 }
977 #endif
978
979                 /* Display a message */
980                 if (p_ptr->wizard)
981                 {
982                         char f_idx_str[32];
983                         if (g_ptr->mimic) sprintf(f_idx_str, "%d/%d", g_ptr->feat, g_ptr->mimic);
984                         else sprintf(f_idx_str, "%d", g_ptr->feat);
985 #ifdef JP
986                         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]);
987 #else
988                         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);
989 #endif
990                 }
991                 else
992 #ifdef JP
993                         sprintf(out_val, "%s%s%s%s[%s]", s1, name, s2, s3, info);
994 #else
995                         sprintf(out_val, "%s%s%s%s [%s]", s1, s2, s3, name, info);
996 #endif
997
998                 prt(out_val, 0, 0);
999                 move_cursor_relative(y, x);
1000                 query = inkey();
1001
1002                 /* Always stop at "normal" keys */
1003                 if ((query != '\r') && (query != '\n') && (query != ' ')) return query;
1004         }
1005
1006         /* Stop on everything but "return" */
1007         if ((query != '\r') && (query != '\n')) return query;
1008
1009         /* Repeat forever */
1010         return 0;
1011 }
1012
1013
1014 /*
1015  * Handle "target" and "look".
1016  *
1017  * Note that this code can be called from "get_aim_dir()".
1018  *
1019  * All locations must be on the current panel.  Consider the use of
1020  * "panel_bounds()" to allow "off-panel" targets, perhaps by using
1021  * some form of "scrolling" the map around the cursor.  
1022  * That is, consider the possibility of "auto-scrolling" the screen
1023  * while the cursor moves around.  This may require changes in the
1024  * "update_monster()" code to allow "visibility" even if off panel, and
1025  * may require dynamic recalculation of the "temp" grid set.
1026  *
1027  * Hack -- targeting/observing an "outer border grid" may induce
1028  * problems, so this is not currently allowed.
1029  *
1030  * The player can use the direction keys to move among "interesting"
1031  * grids in a heuristic manner, or the "space", "+", and "-" keys to
1032  * move through the "interesting" grids in a sequential manner, or
1033  * can enter "location" mode, and use the direction keys to move one
1034  * grid at a time in any direction.  The "t" (set target) command will
1035  * only target a monster (as opposed to a location) if the monster is
1036  * target_able and the "interesting" mode is being used.
1037  *
1038  * The current grid is described using the "look" method above, and
1039  * a new command may be entered at any time, but note that if the
1040  * "TARGET_LOOK" bit flag is set (or if we are in "location" mode,
1041  * where "space" has no obvious meaning) then "space" will scan
1042  * through the description of the current grid until done, instead
1043  * of immediately jumping to the next "interesting" grid.  This
1044  * allows the "target" command to retain its old semantics.
1045  *
1046  * The "*", "+", and "-" keys may always be used to jump immediately
1047  * to the next (or previous) interesting grid, in the proper mode.
1048  *
1049  * The "return" key may always be used to scan through a complete
1050  * grid description (forever).
1051  *
1052  * This command will cancel any old target, even if used from
1053  * inside the "look" command.
1054  */
1055 bool target_set(BIT_FLAGS mode)
1056 {
1057         int i, d, m, t, bd;
1058         POSITION y = p_ptr->y;
1059         POSITION x = p_ptr->x;
1060
1061         bool done = FALSE;
1062         bool flag = TRUE;
1063         char query;
1064         char info[80];
1065         char same_key;
1066         grid_type *g_ptr;
1067         TERM_LEN wid, hgt;
1068         
1069         get_screen_size(&wid, &hgt);
1070
1071         /* Cancel target */
1072         target_who = 0;
1073
1074         if (rogue_like_commands)
1075         {
1076                 same_key = 'x';
1077         }
1078         else
1079         {
1080                 same_key = 'l';
1081         }
1082
1083         /* Prepare the "temp" array */
1084         target_set_prepare(mode);
1085
1086         /* Start near the player */
1087         m = 0;
1088
1089         /* Interact */
1090         while (!done)
1091         {
1092                 /* Interesting grids */
1093                 if (flag && tmp_pos.n)
1094                 {
1095                         y = tmp_pos.y[m];
1096                         x = tmp_pos.x[m];
1097
1098                         /* Set forcus */
1099                         change_panel_xy(y, x);
1100
1101                         if (!(mode & TARGET_LOOK)) prt_path(y, x);
1102
1103                         /* Access */
1104                         g_ptr = &current_floor_ptr->grid_array[y][x];
1105
1106                         /* Allow target */
1107                         if (target_able(g_ptr->m_idx))
1108                         {
1109                                 strcpy(info, _("q止 t決 p自 o現 +次 -前", "q,t,p,o,+,-,<dir>"));
1110                         }
1111
1112                         /* Dis-allow target */
1113                         else
1114                         {
1115                                 strcpy(info, _("q止 p自 o現 +次 -前", "q,p,o,+,-,<dir>"));
1116                         }
1117
1118                         if (cheat_sight)
1119                         {
1120                                 char cheatinfo[30];
1121                                 sprintf(cheatinfo, " LOS:%d, PROJECTABLE:%d",
1122                                         los(p_ptr->y, p_ptr->x, y, x), projectable(p_ptr->y, p_ptr->x, y, x));
1123                                 strcat(info, cheatinfo);
1124                         }
1125                         
1126                         /* Describe and Prompt */
1127                         while (TRUE){
1128                                 query = target_set_aux(y, x, mode, info);
1129                                 if(query)break;
1130                         }
1131
1132                         /* Assume no "direction" */
1133                         d = 0;
1134
1135                         if (use_menu)
1136                         {
1137                                 if (query == '\r') query = 't';
1138                         }  
1139
1140                         /* Analyze */
1141                         switch (query)
1142                         {
1143                                 case ESCAPE:
1144                                 case 'q':
1145                                 {
1146                                         done = TRUE;
1147                                         break;
1148                                 }
1149
1150                                 case 't':
1151                                 case '.':
1152                                 case '5':
1153                                 case '0':
1154                                 {
1155                                         if (target_able(g_ptr->m_idx))
1156                                         {
1157                                                 health_track(g_ptr->m_idx);
1158                                                 target_who = g_ptr->m_idx;
1159                                                 target_row = y;
1160                                                 target_col = x;
1161                                                 done = TRUE;
1162                                         }
1163                                         else
1164                                         {
1165                                                 bell();
1166                                         }
1167                                         break;
1168                                 }
1169
1170                                 case ' ':
1171                                 case '*':
1172                                 case '+':
1173                                 {
1174                                         if (++m == tmp_pos.n)
1175                                         {
1176                                                 m = 0;
1177                                                 if (!expand_list) done = TRUE;
1178                                         }
1179                                         break;
1180                                 }
1181
1182                                 case '-':
1183                                 {
1184                                         if (m-- == 0)
1185                                         {
1186                                                 m = tmp_pos.n - 1;
1187                                                 if (!expand_list) done = TRUE;
1188                                         }
1189                                         break;
1190                                 }
1191
1192                                 case 'p':
1193                                 {
1194                                         /* Recenter the map around the player */
1195                                         verify_panel();
1196                                         p_ptr->update |= (PU_MONSTERS);
1197                                         p_ptr->redraw |= (PR_MAP);
1198                                         p_ptr->window |= (PW_OVERHEAD);
1199                                         handle_stuff();
1200
1201                                         /* Recalculate interesting grids */
1202                                         target_set_prepare(mode);
1203
1204                                         y = p_ptr->y;
1205                                         x = p_ptr->x;
1206                                 }
1207
1208                                 case 'o':
1209                                 {
1210                                         flag = FALSE;
1211                                         break;
1212                                 }
1213
1214                                 case 'm':
1215                                 {
1216                                         break;
1217                                 }
1218
1219                                 default:
1220                                 {
1221                                         if(query == same_key)
1222                                         {
1223                                                 if (++m == tmp_pos.n)
1224                                                 {
1225                                                         m = 0;
1226                                                         if (!expand_list) done = TRUE;
1227                                                 }
1228                                         }
1229                                         else
1230                                         {
1231                                                 /* Extract the action (if any) */
1232                                                 d = get_keymap_dir(query);
1233
1234                                                 if (!d) bell();
1235                                                 break;
1236                                         }
1237                                 }
1238                         }
1239                         /* Hack -- move around */
1240                         if (d)
1241                         {
1242                                 /* Modified to scroll to monster */
1243                                 POSITION y2 = panel_row_min;
1244                                 POSITION x2 = panel_col_min;
1245
1246                                 /* Find a new monster */
1247                                 i = target_pick(tmp_pos.y[m], tmp_pos.x[m], ddy[d], ddx[d]);
1248
1249                                 /* Request to target past last interesting grid */
1250                                 while (flag && (i < 0))
1251                                 {
1252                                         /* Note the change */
1253                                         if (change_panel(ddy[d], ddx[d]))
1254                                         {
1255                                                 int v = tmp_pos.y[m];
1256                                                 int u = tmp_pos.x[m];
1257
1258                                                 /* Recalculate interesting grids */
1259                                                 target_set_prepare(mode);
1260
1261                                                 /* Look at interesting grids */
1262                                                 flag = TRUE;
1263
1264                                                 /* Find a new monster */
1265                                                 i = target_pick(v, u, ddy[d], ddx[d]);
1266
1267                                                 /* Use that grid */
1268                                                 if (i >= 0) m = i;
1269                                         }
1270
1271                                         /* Nothing interesting */
1272                                         else
1273                                         {
1274                                                 POSITION dx = ddx[d];
1275                                                 POSITION dy = ddy[d];
1276
1277                                                 /* Restore previous position */
1278                                                 panel_row_min = y2;
1279                                                 panel_col_min = x2;
1280                                                 panel_bounds_center();
1281
1282                                                 p_ptr->update |= (PU_MONSTERS);
1283                                                 p_ptr->redraw |= (PR_MAP);
1284                                                 p_ptr->window |= (PW_OVERHEAD);
1285                                                 handle_stuff();
1286
1287                                                 /* Recalculate interesting grids */
1288                                                 target_set_prepare(mode);
1289
1290                                                 /* Look at boring grids */
1291                                                 flag = FALSE;
1292
1293                                                 /* Move */
1294                                                 x += dx;
1295                                                 y += dy;
1296
1297                                                 /* Do not move horizontally if unnecessary */
1298                                                 if (((x < panel_col_min + wid / 2) && (dx > 0)) ||
1299                                                          ((x > panel_col_min + wid / 2) && (dx < 0)))
1300                                                 {
1301                                                         dx = 0;
1302                                                 }
1303
1304                                                 /* Do not move vertically if unnecessary */
1305                                                 if (((y < panel_row_min + hgt / 2) && (dy > 0)) ||
1306                                                          ((y > panel_row_min + hgt / 2) && (dy < 0)))
1307                                                 {
1308                                                         dy = 0;
1309                                                 }
1310
1311                                                 /* Apply the motion */
1312                                                 if ((y >= panel_row_min+hgt) || (y < panel_row_min) ||
1313                                                     (x >= panel_col_min+wid) || (x < panel_col_min))
1314                                                 {
1315                                                         if (change_panel(dy, dx)) target_set_prepare(mode);
1316                                                 }
1317
1318                                                 /* Slide into legality */
1319                                                 if (x >= current_floor_ptr->width-1) x = current_floor_ptr->width - 2;
1320                                                 else if (x <= 0) x = 1;
1321
1322                                                 /* Slide into legality */
1323                                                 if (y >= current_floor_ptr->height-1) y = current_floor_ptr->height- 2;
1324                                                 else if (y <= 0) y = 1;
1325                                         }
1326                                 }
1327
1328                                 /* Use that grid */
1329                                 m = i;
1330                         }
1331                 }
1332
1333                 /* Arbitrary grids */
1334                 else
1335                 {
1336                         bool move_fast = FALSE;
1337
1338                         if (!(mode & TARGET_LOOK)) prt_path(y, x);
1339
1340                         /* Access */
1341                         g_ptr = &current_floor_ptr->grid_array[y][x];
1342
1343                         /* Default prompt */
1344                         strcpy(info, _("q止 t決 p自 m近 +次 -前", "q,t,p,m,+,-,<dir>"));
1345
1346                         if (cheat_sight)
1347                         {
1348                                 char cheatinfo[30];
1349                                 sprintf(cheatinfo, " LOS:%d, PROJECTABLE:%d",
1350                                         los(p_ptr->y, p_ptr->x, y, x),
1351                                         projectable(p_ptr->y, p_ptr->x, y, x));
1352                                 strcat(info, cheatinfo);
1353                         }
1354
1355                         /* Describe and Prompt (enable "TARGET_LOOK") */
1356                         while ((query = target_set_aux(y, x, mode | TARGET_LOOK, info)) == 0);
1357
1358                         /* Assume no direction */
1359                         d = 0;
1360
1361                         if (use_menu)
1362                         {
1363                                 if (query == '\r') query = 't';
1364                         }  
1365
1366                         /* Analyze the keypress */
1367                         switch (query)
1368                         {
1369                                 case ESCAPE:
1370                                 case 'q':
1371                                 {
1372                                         done = TRUE;
1373                                         break;
1374                                 }
1375
1376                                 case 't':
1377                                 case '.':
1378                                 case '5':
1379                                 case '0':
1380                                 {
1381                                         target_who = -1;
1382                                         target_row = y;
1383                                         target_col = x;
1384                                         done = TRUE;
1385                                         break;
1386                                 }
1387
1388                                 case 'p':
1389                                 {
1390                                         /* Recenter the map around the player */
1391                                         verify_panel();
1392                                         p_ptr->update |= (PU_MONSTERS);
1393                                         p_ptr->redraw |= (PR_MAP);
1394                                         p_ptr->window |= (PW_OVERHEAD);
1395                                         handle_stuff();
1396
1397                                         /* Recalculate interesting grids */
1398                                         target_set_prepare(mode);
1399
1400                                         y = p_ptr->y;
1401                                         x = p_ptr->x;
1402                                 }
1403
1404                                 case 'o':
1405                                 {
1406                                         break;
1407                                 }
1408
1409                                 case ' ':
1410                                 case '*':
1411                                 case '+':
1412                                 case '-':
1413                                 case 'm':
1414                                 {
1415                                         flag = TRUE;
1416
1417                                         m = 0;
1418                                         bd = 999;
1419
1420                                         /* Pick a nearby monster */
1421                                         for (i = 0; i < tmp_pos.n; i++)
1422                                         {
1423                                                 t = distance(y, x, tmp_pos.y[i], tmp_pos.x[i]);
1424
1425                                                 /* Pick closest */
1426                                                 if (t < bd)
1427                                                 {
1428                                                         m = i;
1429                                                         bd = t;
1430                                                 }
1431                                         }
1432
1433                                         /* Nothing interesting */
1434                                         if (bd == 999) flag = FALSE;
1435
1436                                         break;
1437                                 }
1438
1439                                 default:
1440                                 {
1441                                         /* Extract the action (if any) */
1442                                         d = get_keymap_dir(query);
1443
1444                                         /* XTRA HACK MOVEFAST */
1445                                         if (isupper(query)) move_fast = TRUE;
1446
1447                                         if (!d) bell();
1448                                         break;
1449                                 }
1450                         }
1451
1452                         /* Handle "direction" */
1453                         if (d)
1454                         {
1455                                 POSITION dx = ddx[d];
1456                                 POSITION dy = ddy[d];
1457
1458                                 /* XTRA HACK MOVEFAST */
1459                                 if (move_fast)
1460                                 {
1461                                         int mag = MIN(wid / 2, hgt / 2);
1462                                         x += dx * mag;
1463                                         y += dy * mag;
1464                                 }
1465                                 else
1466                                 {
1467                                         x += dx;
1468                                         y += dy;
1469                                 }
1470
1471                                 /* Do not move horizontally if unnecessary */
1472                                 if (((x < panel_col_min + wid / 2) && (dx > 0)) ||
1473                                          ((x > panel_col_min + wid / 2) && (dx < 0)))
1474                                 {
1475                                         dx = 0;
1476                                 }
1477
1478                                 /* Do not move vertically if unnecessary */
1479                                 if (((y < panel_row_min + hgt / 2) && (dy > 0)) ||
1480                                          ((y > panel_row_min + hgt / 2) && (dy < 0)))
1481                                 {
1482                                         dy = 0;
1483                                 }
1484
1485                                 /* Apply the motion */
1486                                 if ((y >= panel_row_min + hgt) || (y < panel_row_min) ||
1487                                          (x >= panel_col_min + wid) || (x < panel_col_min))
1488                                 {
1489                                         if (change_panel(dy, dx)) target_set_prepare(mode);
1490                                 }
1491
1492                                 /* Slide into legality */
1493                                 if (x >= current_floor_ptr->width-1) x = current_floor_ptr->width - 2;
1494                                 else if (x <= 0) x = 1;
1495
1496                                 /* Slide into legality */
1497                                 if (y >= current_floor_ptr->height-1) y = current_floor_ptr->height- 2;
1498                                 else if (y <= 0) y = 1;
1499                         }
1500                 }
1501         }
1502
1503         /* Forget */
1504         tmp_pos.n = 0;
1505
1506         /* Clear the top line */
1507         prt("", 0, 0);
1508
1509         /* Recenter the map around the player */
1510         verify_panel();
1511         p_ptr->update |= (PU_MONSTERS);
1512         p_ptr->redraw |= (PR_MAP);
1513         p_ptr->window |= (PW_OVERHEAD);
1514         handle_stuff();
1515
1516         /* Failure to set target */
1517         if (!target_who) return (FALSE);
1518
1519         /* Success */
1520         return (TRUE);
1521 }
1522
1523
1524 /*
1525  * Get an "aiming direction" from the user.
1526  *
1527  * The "dir" is loaded with 1,2,3,4,6,7,8,9 for "actual direction", and
1528  * "0" for "current target", and "-1" for "entry aborted".
1529  *
1530  * Note that "Force Target", if set, will pre-empt user interaction,
1531  * if there is a usable target already set.
1532  *
1533  * Note that confusion over-rides any (explicit?) user choice.
1534  */
1535 bool get_aim_dir(DIRECTION *dp)
1536 {
1537         DIRECTION dir;
1538         char    command;
1539         concptr p;
1540         COMMAND_CODE code;
1541
1542         (*dp) = 0;
1543
1544         /* Global direction */
1545         dir = command_dir;
1546
1547         /* Hack -- auto-target if requested */
1548         if (use_old_target && target_okay()) dir = 5;
1549
1550         if (repeat_pull(&code))
1551         {
1552                 /* Confusion? */
1553
1554                 /* Verify */
1555                 if (!(code == 5 && !target_okay()))
1556                 {
1557 /*                      return (TRUE); */
1558                         dir = (DIRECTION)code;
1559                 }
1560         }
1561         *dp = (DIRECTION)code;
1562
1563         /* Ask until satisfied */
1564         while (!dir)
1565         {
1566                 /* Choose a prompt */
1567                 if (!target_okay())
1568                 {
1569                         p = _("方向 ('*'でターゲット選択, ESCで中断)? ", "Direction ('*' to choose a target, Escape to cancel)? ");
1570                 }
1571                 else
1572                 {
1573                         p = _("方向 ('5'でターゲットへ, '*'でターゲット再選択, ESCで中断)? ", "Direction ('5' for target, '*' to re-target, Escape to cancel)? ");
1574                 }
1575
1576                 /* Get a command (or Cancel) */
1577                 if (!get_com(p, &command, TRUE)) break;
1578
1579                 if (use_menu)
1580                 {
1581                         if (command == '\r') command = 't';
1582                 }  
1583
1584                 /* Convert various keys to "standard" keys */
1585                 switch (command)
1586                 {
1587                         /* Use current target */
1588                         case 'T':
1589                         case 't':
1590                         case '.':
1591                         case '5':
1592                         case '0':
1593                         {
1594                                 dir = 5;
1595                                 break;
1596                         }
1597
1598                         /* Set new target */
1599                         case '*':
1600                         case ' ':
1601                         case '\r':
1602                         {
1603                                 if (target_set(TARGET_KILL)) dir = 5;
1604                                 break;
1605                         }
1606
1607                         default:
1608                         {
1609                                 /* Extract the action (if any) */
1610                                 dir = get_keymap_dir(command);
1611
1612                                 break;
1613                         }
1614                 }
1615
1616                 /* Verify requested targets */
1617                 if ((dir == 5) && !target_okay()) dir = 0;
1618
1619                 /* Error */
1620                 if (!dir) bell();
1621         }
1622
1623         /* No direction */
1624         if (!dir)
1625         {
1626                 project_length = 0; /* reset to default */
1627                 return (FALSE);
1628         }
1629
1630         /* Save the direction */
1631         command_dir = dir;
1632
1633         /* Check for confusion */
1634         if (p_ptr->confused)
1635         {
1636                 /* Random direction */
1637                 dir = ddd[randint0(8)];
1638         }
1639
1640         /* Notice confusion */
1641         if (command_dir != dir)
1642         {
1643                 /* Warn the user */
1644                 msg_print(_("あなたは混乱している。", "You are confused."));
1645         }
1646
1647         /* Save direction */
1648         (*dp) = dir;
1649
1650 /*      repeat_push(dir); */
1651         repeat_push((COMMAND_CODE)command_dir);
1652
1653         /* A "valid" direction was entered */
1654         return (TRUE);
1655 }
1656
1657
1658 bool get_direction(DIRECTION *dp, bool allow_under, bool with_steed)
1659 {
1660         DIRECTION dir;
1661         concptr prompt;
1662         COMMAND_CODE code;
1663
1664         (*dp) = 0;
1665
1666         /* Global direction */
1667         dir = command_dir;
1668
1669         if (repeat_pull(&code))
1670         {
1671                 dir = (DIRECTION)code;
1672                 /*              return (TRUE); */
1673         }
1674         *dp = (DIRECTION)code;
1675
1676         if (allow_under)
1677         {
1678                 prompt = _("方向 ('.'足元, ESCで中断)? ", "Direction ('.' at feet, Escape to cancel)? ");
1679         }
1680         else
1681         {
1682                 prompt = _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
1683         }
1684
1685         /* Get a direction */
1686         while (!dir)
1687         {
1688                 char ch;
1689
1690                 /* Get a command (or Cancel) */
1691                 if (!get_com(prompt, &ch, TRUE)) break;
1692
1693                 /* Look down */
1694                 if ((allow_under) && ((ch == '5') || (ch == '-') || (ch == '.')))
1695                 {
1696                         dir = 5;
1697                 }
1698                 else
1699                 {
1700                         /* Look up the direction */
1701                         dir = get_keymap_dir(ch);
1702
1703                         if (!dir) bell();
1704                 }
1705         }
1706
1707         /* Prevent weirdness */
1708         if ((dir == 5) && (!allow_under)) dir = 0;
1709
1710         /* Aborted */
1711         if (!dir) return (FALSE);
1712
1713         /* Save desired direction */
1714         command_dir = dir;
1715
1716         /* Apply "confusion" */
1717         if (p_ptr->confused)
1718         {
1719                 /* Standard confusion */
1720                 if (randint0(100) < 75)
1721                 {
1722                         /* Random direction */
1723                         dir = ddd[randint0(8)];
1724                 }
1725         }
1726         else if (p_ptr->riding && with_steed)
1727         {
1728                 monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1729                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1730
1731                 if (MON_CONFUSED(m_ptr))
1732                 {
1733                         /* Standard confusion */
1734                         if (randint0(100) < 75)
1735                         {
1736                                 /* Random direction */
1737                                 dir = ddd[randint0(8)];
1738                         }
1739                 }
1740                 else if ((r_ptr->flags1 & RF1_RAND_50) && (r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 50))
1741                 {
1742                         /* Random direction */
1743                         dir = ddd[randint0(8)];
1744                 }
1745                 else if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 25))
1746                 {
1747                         /* Random direction */
1748                         dir = ddd[randint0(8)];
1749                 }
1750         }
1751
1752         /* Notice confusion */
1753         if (command_dir != dir)
1754         {
1755                 if (p_ptr->confused)
1756                 {
1757                         /* Warn the user */
1758                         msg_print(_("あなたは混乱している。", "You are confused."));
1759                 }
1760                 else
1761                 {
1762                         GAME_TEXT m_name[MAX_NLEN];
1763                         monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1764
1765                         monster_desc(m_name, m_ptr, 0);
1766                         if (MON_CONFUSED(m_ptr))
1767                         {
1768                                 msg_format(_("%sは混乱している。", "%^s is confusing."), m_name);
1769                         }
1770                         else
1771                         {
1772                                 msg_format(_("%sは思い通りに動いてくれない。", "You cannot control %s."), m_name);
1773                         }
1774                 }
1775         }
1776
1777         /* Save direction */
1778         (*dp) = dir;
1779
1780         /*      repeat_push(dir); */
1781         repeat_push((COMMAND_CODE)command_dir);
1782
1783         /* Success */
1784         return (TRUE);
1785 }
1786
1787 /*
1788  * @brief 進行方向を指定する(騎乗対象の混乱の影響を受ける) / Request a "movement" direction (1,2,3,4,6,7,8,9) from the user,
1789  * and place it into "command_dir", unless we already have one.
1790  *
1791  * This function should be used for all "repeatable" commands, such as
1792  * run, walk, open, close, bash, disarm, spike, tunnel, etc, as well
1793  * as all commands which must reference a grid adjacent to the player,
1794  * and which may not reference the grid under the player.  Note that,
1795  * for example, it is no longer possible to "disarm" or "open" chests
1796  * in the same grid as the player.
1797  *
1798  * Direction "5" is illegal and will (cleanly) abort the command.
1799  *
1800  * This function tracks and uses the "global direction", and uses
1801  * that as the "desired direction", to which "confusion" is applied.
1802  */
1803 bool get_rep_dir(DIRECTION *dp, bool under)
1804 {
1805         DIRECTION dir;
1806         concptr prompt;
1807         COMMAND_CODE code;
1808
1809         (*dp) = 0;
1810
1811         /* Global direction */
1812         dir = command_dir;
1813
1814         if (repeat_pull(&code))
1815         {
1816                 dir = (DIRECTION)code;
1817 /*              return (TRUE); */
1818         }
1819         *dp = (DIRECTION)code;
1820
1821         if (under)
1822         {
1823                 prompt = _("方向 ('.'足元, ESCで中断)? ", "Direction ('.' at feet, Escape to cancel)? ");
1824         }
1825         else
1826         {
1827                 prompt = _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
1828         }
1829         
1830         /* Get a direction */
1831         while (!dir)
1832         {
1833                 char ch;
1834
1835                 /* Get a command (or Cancel) */
1836                 if (!get_com(prompt, &ch, TRUE)) break;
1837
1838                 /* Look down */
1839                 if ((under) && ((ch == '5') || (ch == '-') || (ch == '.')))
1840                 {
1841                         dir = 5;
1842                 }
1843                 else
1844                 {
1845                         /* Look up the direction */
1846                         dir = get_keymap_dir(ch);
1847
1848                         if (!dir) bell();
1849                 }
1850         }
1851
1852         /* Prevent weirdness */
1853         if ((dir == 5) && (!under)) dir = 0;
1854
1855         /* Aborted */
1856         if (!dir) return (FALSE);
1857
1858         /* Save desired direction */
1859         command_dir = dir;
1860
1861         /* Apply "confusion" */
1862         if (p_ptr->confused)
1863         {
1864                 /* Standard confusion */
1865                 if (randint0(100) < 75)
1866                 {
1867                         /* Random direction */
1868                         dir = ddd[randint0(8)];
1869                 }
1870         }
1871         else if (p_ptr->riding)
1872         {
1873                 monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1874                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1875
1876                 if (MON_CONFUSED(m_ptr))
1877                 {
1878                         /* Standard confusion */
1879                         if (randint0(100) < 75)
1880                         {
1881                                 /* Random direction */
1882                                 dir = ddd[randint0(8)];
1883                         }
1884                 }
1885                 else if ((r_ptr->flags1 & RF1_RAND_50) && (r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 50))
1886                 {
1887                         /* Random direction */
1888                         dir = ddd[randint0(8)];
1889                 }
1890                 else if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 25))
1891                 {
1892                         /* Random direction */
1893                         dir = ddd[randint0(8)];
1894                 }
1895         }
1896
1897         /* Notice confusion */
1898         if (command_dir != dir)
1899         {
1900                 if (p_ptr->confused)
1901                 {
1902                         /* Warn the user */
1903                         msg_print(_("あなたは混乱している。", "You are confused."));
1904                 }
1905                 else
1906                 {
1907                         GAME_TEXT m_name[MAX_NLEN];
1908                         monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
1909
1910                         monster_desc(m_name, m_ptr, 0);
1911                         if (MON_CONFUSED(m_ptr))
1912                         {
1913                                 msg_format(_("%sは混乱している。", "%^s is confusing."), m_name);
1914                         }
1915                         else
1916                         {
1917                                 msg_format(_("%sは思い通りに動いてくれない。", "You cannot control %s."), m_name);
1918                         }
1919                 }
1920         }
1921
1922         /* Save direction */
1923         (*dp) = dir;
1924
1925 /*      repeat_push(dir); */
1926         repeat_push((COMMAND_CODE)command_dir);
1927
1928         /* Success */
1929         return (TRUE);
1930 }
1931
1932
1933 /*
1934  * XAngband: determine if a given location is "interesting"
1935  * based on target_set_accept function.
1936  */
1937 static bool tgt_pt_accept(POSITION y, POSITION x)
1938 {
1939         grid_type *g_ptr;
1940
1941         if (!(in_bounds(y, x))) return (FALSE);
1942
1943         /* Player grid is always interesting */
1944         if ((y == p_ptr->y) && (x == p_ptr->x)) return (TRUE);
1945
1946         if (p_ptr->image) return (FALSE);
1947
1948         g_ptr = &current_floor_ptr->grid_array[y][x];
1949
1950         /* Interesting memorized features */
1951         if (g_ptr->info & (CAVE_MARK))
1952         {
1953                 /* Notice stairs */
1954                 if (cave_have_flag_grid(g_ptr, FF_LESS)) return (TRUE);
1955                 if (cave_have_flag_grid(g_ptr, FF_MORE)) return (TRUE);
1956
1957                 /* Notice quest features */
1958                 if (cave_have_flag_grid(g_ptr, FF_QUEST_ENTER)) return (TRUE);
1959                 if (cave_have_flag_grid(g_ptr, FF_QUEST_EXIT)) return (TRUE);
1960         }
1961
1962         return (FALSE);
1963 }
1964
1965
1966 /*
1967  * XAngband: Prepare the "temp" array for "tget_pt"
1968  * based on target_set_prepare funciton.
1969  */
1970 static void tgt_pt_prepare(void)
1971 {
1972         POSITION y, x;
1973
1974         /* Reset "temp" array */
1975         tmp_pos.n = 0;
1976
1977         if (!expand_list) return;
1978
1979         /* Scan the current panel */
1980         for (y = 1; y < current_floor_ptr->height; y++)
1981         {
1982                 for (x = 1; x < current_floor_ptr->width; x++)
1983                 {
1984                         /* Require "interesting" contents */
1985                         if (!tgt_pt_accept(y, x)) continue;
1986
1987                         /* Save the location */
1988                         tmp_pos.x[tmp_pos.n] = x;
1989                         tmp_pos.y[tmp_pos.n] = y;
1990                         tmp_pos.n++;
1991                 }
1992         }
1993
1994         /* Sort the positions */
1995         ang_sort(tmp_pos.x, tmp_pos.y, tmp_pos.n, ang_sort_comp_distance, ang_sort_swap_distance);
1996 }
1997
1998 /*
1999  * old -- from PsiAngband.
2000  */
2001 bool tgt_pt(POSITION *x_ptr, POSITION *y_ptr)
2002 {
2003         char ch = 0;
2004         int d, n = 0;
2005         POSITION x, y;
2006         bool success = FALSE;
2007
2008         TERM_LEN wid, hgt;
2009
2010         get_screen_size(&wid, &hgt);
2011
2012         x = p_ptr->x;
2013         y = p_ptr->y;
2014
2015         if (expand_list) 
2016         {
2017                 tgt_pt_prepare();
2018         }
2019
2020         msg_print(_("場所を選んでスペースキーを押して下さい。", "Select a point and press space."));
2021         msg_flag = FALSE; /* prevents "-more-" message. */
2022
2023         while ((ch != ESCAPE) && !success)
2024         {
2025                 bool move_fast = FALSE;
2026
2027                 move_cursor_relative(y, x);
2028                 ch = inkey();
2029                 switch (ch)
2030                 {
2031                 case ESCAPE:
2032                         break;
2033                 case ' ':
2034                 case 't':
2035                 case '.':
2036                 case '5':
2037                 case '0':
2038                         /* illegal place */
2039                         if (player_bold(y, x)) ch = 0;
2040
2041                         /* okay place */
2042                         else success = TRUE;
2043
2044                         break;
2045
2046                 /* XAngband: Move cursor to stairs */
2047                 case '>':
2048                 case '<':
2049                         if (expand_list && tmp_pos.n)
2050                         {
2051                                 int dx, dy;
2052                                 int cx = (panel_col_min + panel_col_max) / 2;
2053                                 int cy = (panel_row_min + panel_row_max) / 2;
2054
2055                                 n++;
2056
2057                                 /* Skip stairs which have defferent distance */
2058                                 for (; n < tmp_pos.n; ++ n)
2059                                 {
2060                                         grid_type *g_ptr = &current_floor_ptr->grid_array[tmp_pos.y[n]][tmp_pos.x[n]];
2061
2062                                         if (cave_have_flag_grid(g_ptr, FF_STAIRS) &&
2063                                             cave_have_flag_grid(g_ptr, ch == '>' ? FF_MORE : FF_LESS))
2064                                         {
2065                                                 /* Found */
2066                                                 break;
2067                                         }
2068                                 }
2069
2070                                 if (n == tmp_pos.n)     /* Loop out taget list */
2071                                 {
2072                                         n = 0;
2073                                         y = p_ptr->y;
2074                                         x = p_ptr->x;
2075                                         verify_panel(); /* Move cursor to player */
2076
2077                                         p_ptr->update |= (PU_MONSTERS);
2078
2079                                         p_ptr->redraw |= (PR_MAP);
2080
2081                                         p_ptr->window |= (PW_OVERHEAD);
2082                                         handle_stuff();
2083                                 }
2084                                 else    /* move cursor to next stair and change panel */
2085                                 {
2086                                         y = tmp_pos.y[n];
2087                                         x = tmp_pos.x[n];
2088
2089                                         dy = 2 * (y - cy) / hgt;
2090                                         dx = 2 * (x - cx) / wid;
2091                                         if (dy || dx) change_panel(dy, dx);
2092                                 }
2093                         }
2094                         break;
2095
2096                 default:
2097                         /* Look up the direction */
2098                         d = get_keymap_dir(ch);
2099
2100                         /* XTRA HACK MOVEFAST */
2101                         if (isupper(ch)) move_fast = TRUE;
2102
2103                         /* Handle "direction" */
2104                         if (d)
2105                         {
2106                                 int dx = ddx[d];
2107                                 int dy = ddy[d];
2108
2109                                 /* XTRA HACK MOVEFAST */
2110                                 if (move_fast)
2111                                 {
2112                                         int mag = MIN(wid / 2, hgt / 2);
2113                                         x += dx * mag;
2114                                         y += dy * mag;
2115                                 }
2116                                 else
2117                                 {
2118                                         x += dx;
2119                                         y += dy;
2120                                 }
2121
2122                                 /* Do not move horizontally if unnecessary */
2123                                 if (((x < panel_col_min + wid / 2) && (dx > 0)) ||
2124                                          ((x > panel_col_min + wid / 2) && (dx < 0)))
2125                                 {
2126                                         dx = 0;
2127                                 }
2128
2129                                 /* Do not move vertically if unnecessary */
2130                                 if (((y < panel_row_min + hgt / 2) && (dy > 0)) ||
2131                                          ((y > panel_row_min + hgt / 2) && (dy < 0)))
2132                                 {
2133                                         dy = 0;
2134                                 }
2135
2136                                 /* Apply the motion */
2137                                 if ((y >= panel_row_min + hgt) || (y < panel_row_min) ||
2138                                          (x >= panel_col_min + wid) || (x < panel_col_min))
2139                                 {
2140                                         /* if (change_panel(dy, dx)) target_set_prepare(mode); */
2141                                         change_panel(dy, dx);
2142                                 }
2143
2144                                 /* Slide into legality */
2145                                 if (x >= current_floor_ptr->width-1) x = current_floor_ptr->width - 2;
2146                                 else if (x <= 0) x = 1;
2147
2148                                 /* Slide into legality */
2149                                 if (y >= current_floor_ptr->height-1) y = current_floor_ptr->height- 2;
2150                                 else if (y <= 0) y = 1;
2151
2152                         }
2153                         break;
2154                 }
2155         }
2156
2157         /* Clear the top line */
2158         prt("", 0, 0);
2159
2160         /* Recenter the map around the player */
2161         verify_panel();
2162
2163         p_ptr->update |= (PU_MONSTERS);
2164
2165         p_ptr->redraw |= (PR_MAP);
2166
2167         p_ptr->window |= (PW_OVERHEAD);
2168         handle_stuff();
2169
2170         *x_ptr = x;
2171         *y_ptr = y;
2172         return success;
2173 }
2174
2175
2176 bool get_hack_dir(DIRECTION *dp)
2177 {
2178         DIRECTION dir;
2179         concptr    p;
2180         char    command;
2181
2182         (*dp) = 0;
2183
2184         /* Global direction */
2185         dir = 0;
2186
2187         /* (No auto-targeting) */
2188
2189         /* Ask until satisfied */
2190         while (!dir)
2191         {
2192                 /* Choose a prompt */
2193                 if (!target_okay())
2194                 {
2195                         p = _("方向 ('*'でターゲット選択, ESCで中断)? ", "Direction ('*' to choose a target, Escape to cancel)? ");
2196                 }
2197                 else
2198                 {
2199                         p = _("方向 ('5'でターゲットへ, '*'でターゲット再選択, ESCで中断)? ", "Direction ('5' for target, '*' to re-target, Escape to cancel)? ");
2200                 }
2201
2202                 /* Get a command (or Cancel) */
2203                 if (!get_com(p, &command, TRUE)) break;
2204
2205                 if (use_menu)
2206                 {
2207                         if (command == '\r') command = 't';
2208                 }  
2209
2210                 /* Convert various keys to "standard" keys */
2211                 switch (command)
2212                 {
2213                         /* Use current target */
2214                         case 'T':
2215                         case 't':
2216                         case '.':
2217                         case '5':
2218                         case '0':
2219                         {
2220                                 dir = 5;
2221                                 break;
2222                         }
2223
2224                         /* Set new target */
2225                         case '*':
2226                         case ' ':
2227                         case '\r':
2228                         {
2229                                 if (target_set(TARGET_KILL)) dir = 5;
2230                                 break;
2231                         }
2232
2233                         default:
2234                         {
2235                                 /* Look up the direction */
2236                                 dir = get_keymap_dir(command);
2237
2238                                 break;
2239                         }
2240                 }
2241
2242                 /* Verify requested targets */
2243                 if ((dir == 5) && !target_okay()) dir = 0;
2244
2245                 /* Error */
2246                 if (!dir) bell();
2247         }
2248
2249         /* No direction */
2250         if (!dir) return (FALSE);
2251
2252         /* Save the direction */
2253         command_dir = dir;
2254
2255         /* Check for confusion */
2256         if (p_ptr->confused)
2257         {
2258                 /* Random direction */
2259                 dir = ddd[randint0(8)];
2260         }
2261
2262         /* Notice confusion */
2263         if (command_dir != dir)
2264         {
2265                 /* Warn the user */
2266                 msg_print(_("あなたは混乱している。", "You are confused."));
2267         }
2268
2269         /* Save direction */
2270         (*dp) = dir;
2271
2272         /* A "valid" direction was entered */
2273         return (TRUE);
2274 }