OSDN Git Service

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