OSDN Git Service

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