OSDN Git Service

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