OSDN Git Service

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