OSDN Git Service

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