OSDN Git Service

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