OSDN Git Service

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