OSDN Git Service

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