OSDN Git Service

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