OSDN Git Service

[Refactor] #37353 add_esp_strong() and add_esp_weak() to object-boost.c.
[hengband/hengband.git] / src / object2.c
1 /*!
2  * @file object2.c
3  * @brief オブジェクトの実装 / Object code, part 2
4  * @date 2014/01/11
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  *\n
8  * This software may be copied and distributed for educational, research,\n
9  * and not for profit purposes provided that this copyright and statement\n
10  * are included in all such copies.  Other copyrights may also apply.\n
11  * 2014 Deskull rearranged comment for Doxygen.\n
12  */
13
14 #include "angband.h"
15 #include "object-boost.h"
16 #include "object-hook.h"
17 #include "object-curse.h"
18 #include "artifact.h"
19 #include "player-status.h"
20 #include "feature.h"
21 #include "player-move.h"
22
23 /*!
24  * @brief 床上、モンスター所持でスタックされたアイテムを削除しスタックを補完する / Excise a dungeon object from any stacks
25  * @param o_idx 削除対象のオブジェクト構造体ポインタ
26  * @return なし
27  */
28 void excise_object_idx(OBJECT_IDX o_idx)
29 {
30         object_type *j_ptr;
31
32         OBJECT_IDX this_o_idx, next_o_idx = 0;
33         OBJECT_IDX prev_o_idx = 0;
34
35         /* Object */
36         j_ptr = &current_floor_ptr->o_list[o_idx];
37
38         if (j_ptr->held_m_idx)
39         {
40                 monster_type *m_ptr;
41                 m_ptr = &current_floor_ptr->m_list[j_ptr->held_m_idx];
42
43                 /* Scan all objects in the grid */
44                 for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
45                 {
46                         object_type *o_ptr;
47                         o_ptr = &current_floor_ptr->o_list[this_o_idx];
48                         next_o_idx = o_ptr->next_o_idx;
49
50                         if (this_o_idx == o_idx)
51                         {
52                                 /* No previous */
53                                 if (prev_o_idx == 0)
54                                 {
55                                         /* Remove from list */
56                                         m_ptr->hold_o_idx = next_o_idx;
57                                 }
58
59                                 /* Real previous */
60                                 else
61                                 {
62                                         object_type *k_ptr;
63
64                                         /* Previous object */
65                                         k_ptr = &current_floor_ptr->o_list[prev_o_idx];
66
67                                         /* Remove from list */
68                                         k_ptr->next_o_idx = next_o_idx;
69                                 }
70
71                                 /* Forget next pointer */
72                                 o_ptr->next_o_idx = 0;
73
74                                 break;
75                         }
76
77                         /* Save prev_o_idx */
78                         prev_o_idx = this_o_idx;
79                 }
80         }
81
82         /* Dungeon */
83         else
84         {
85                 grid_type *g_ptr;
86
87                 POSITION y = j_ptr->iy;
88                 POSITION x = j_ptr->ix;
89
90                 g_ptr = &current_floor_ptr->grid_array[y][x];
91
92                 /* Scan all objects in the grid */
93                 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
94                 {
95                         object_type *o_ptr;
96                         o_ptr = &current_floor_ptr->o_list[this_o_idx];
97                         next_o_idx = o_ptr->next_o_idx;
98
99                         if (this_o_idx == o_idx)
100                         {
101                                 /* No previous */
102                                 if (prev_o_idx == 0)
103                                 {
104                                         /* Remove from list */
105                                         g_ptr->o_idx = next_o_idx;
106                                 }
107
108                                 /* Real previous */
109                                 else
110                                 {
111                                         object_type *k_ptr;
112
113                                         /* Previous object */
114                                         k_ptr = &current_floor_ptr->o_list[prev_o_idx];
115
116                                         /* Remove from list */
117                                         k_ptr->next_o_idx = next_o_idx;
118                                 }
119
120                                 /* Forget next pointer */
121                                 o_ptr->next_o_idx = 0;
122
123                                 break;
124                         }
125
126                         /* Save prev_o_idx */
127                         prev_o_idx = this_o_idx;
128                 }
129         }
130 }
131
132 /*!
133  * @brief オブジェクトを削除する /
134  * Delete a dungeon object
135  * @param o_idx 削除対象のオブジェクト構造体ポインタ
136  * @return なし
137  * @details
138  * Handle "stacks" of objects correctly.
139  */
140 void delete_object_idx(OBJECT_IDX o_idx)
141 {
142         object_type *j_ptr;
143
144         /* Excise */
145         excise_object_idx(o_idx);
146
147         /* Object */
148         j_ptr = &current_floor_ptr->o_list[o_idx];
149
150         /* Dungeon floor */
151         if (!(j_ptr->held_m_idx))
152         {
153                 POSITION y, x;
154
155                 y = j_ptr->iy;
156                 x = j_ptr->ix;
157
158                 /* Visual update */
159                 lite_spot(y, x);
160         }
161         object_wipe(j_ptr);
162
163         /* Count objects */
164         o_cnt--;
165 }
166
167
168 /*!
169  * @brief フロアにマスに落ちているオブジェクトを全て削除する / Deletes all objects at given location
170  * Delete a dungeon object
171  * @param y 削除したフロアマスのY座標
172  * @param x 削除したフロアマスのX座標
173  * @return なし
174  */
175 void delete_object(POSITION y, POSITION x)
176 {
177         grid_type *g_ptr;
178         OBJECT_IDX this_o_idx, next_o_idx = 0;
179
180         /* Refuse "illegal" locations */
181         if (!in_bounds(y, x)) return;
182
183         g_ptr = &current_floor_ptr->grid_array[y][x];
184
185         /* Scan all objects in the grid */
186         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
187         {
188                 object_type *o_ptr;
189                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
190                 next_o_idx = o_ptr->next_o_idx;
191                 object_wipe(o_ptr);
192
193                 /* Count objects */
194                 o_cnt--;
195         }
196
197         /* Objects are gone */
198         g_ptr->o_idx = 0;
199
200         /* Visual update */
201         lite_spot(y, x);
202 }
203
204
205 /*!
206  * @brief グローバルオブジェクト配列に対し指定範囲のオブジェクトを整理してIDの若い順に寄せる /
207  * Move an object from index i1 to index i2 in the object list
208  * @param i1 整理したい配列の始点
209  * @param i2 整理したい配列の終点
210  * @return なし
211  */
212 static void compact_objects_aux(OBJECT_IDX i1, OBJECT_IDX i2)
213 {
214         OBJECT_IDX i;
215         grid_type *g_ptr;
216         object_type *o_ptr;
217
218         /* Do nothing */
219         if (i1 == i2) return;
220
221         /* Repair objects */
222         for (i = 1; i < o_max; i++)
223         {
224                 o_ptr = &current_floor_ptr->o_list[i];
225
226                 /* Skip "dead" objects */
227                 if (!o_ptr->k_idx) continue;
228
229                 /* Repair "next" pointers */
230                 if (o_ptr->next_o_idx == i1)
231                 {
232                         /* Repair */
233                         o_ptr->next_o_idx = i2;
234                 }
235         }
236         o_ptr = &current_floor_ptr->o_list[i1];
237
238         if (o_ptr->held_m_idx)
239         {
240                 monster_type *m_ptr;
241
242                 /* Acquire monster */
243                 m_ptr = &current_floor_ptr->m_list[o_ptr->held_m_idx];
244
245                 /* Repair monster */
246                 if (m_ptr->hold_o_idx == i1)
247                 {
248                         /* Repair */
249                         m_ptr->hold_o_idx = i2;
250                 }
251         }
252
253         /* Dungeon */
254         else
255         {
256                 POSITION y, x;
257
258                 /* Acquire location */
259                 y = o_ptr->iy;
260                 x = o_ptr->ix;
261
262                 /* Acquire grid */
263                 g_ptr = &current_floor_ptr->grid_array[y][x];
264
265                 /* Repair grid */
266                 if (g_ptr->o_idx == i1)
267                 {
268                         /* Repair */
269                         g_ptr->o_idx = i2;
270                 }
271         }
272
273         /* Structure copy */
274         current_floor_ptr->o_list[i2] = current_floor_ptr->o_list[i1];
275
276         /* Wipe the hole */
277         object_wipe(o_ptr);
278 }
279
280
281 /*!
282  * @brief グローバルオブジェクト配列から優先度の低いものを削除し、データを圧縮する。 /
283  * Compact and Reorder the object list.
284  * @param size 最低でも減らしたいオブジェクト数の水準
285  * @return なし
286  * @details
287  * (危険なので使用には注意すること)
288  * This function can be very dangerous, use with caution!\n
289  *\n
290  * When actually "compacting" objects, we base the saving throw on a\n
291  * combination of object level, distance from player, and current\n
292  * "desperation".\n
293  *\n
294  * After "compacting" (if needed), we "reorder" the objects into a more\n
295  * compact order, and we reset the allocation info, and the "live" array.\n
296  */
297 void compact_objects(int size)
298 {
299         OBJECT_IDX i;
300         POSITION y, x;
301         int num, cnt;
302         int cur_lev, cur_dis, chance;
303         object_type *o_ptr;
304
305         /* Compact */
306         if (size)
307         {
308                 msg_print(_("アイテム情報を圧縮しています...", "Compacting objects..."));
309                 p_ptr->redraw |= (PR_MAP);
310                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
311         }
312
313
314         /* Compact at least 'size' objects */
315         for (num = 0, cnt = 1; num < size; cnt++)
316         {
317                 /* Get more vicious each iteration */
318                 cur_lev = 5 * cnt;
319
320                 /* Get closer each iteration */
321                 cur_dis = 5 * (20 - cnt);
322
323                 /* Examine the objects */
324                 for (i = 1; i < o_max; i++)
325                 {
326                         o_ptr = &current_floor_ptr->o_list[i];
327
328                         /* Skip dead objects */
329                         if (!o_ptr->k_idx) continue;
330
331                         /* Hack -- High level objects start out "immune" */
332                         if (k_info[o_ptr->k_idx].level > cur_lev) continue;
333
334                         if (o_ptr->held_m_idx)
335                         {
336                                 monster_type *m_ptr;
337
338                                 /* Acquire monster */
339                                 m_ptr = &current_floor_ptr->m_list[o_ptr->held_m_idx];
340
341                                 y = m_ptr->fy;
342                                 x = m_ptr->fx;
343
344                                 /* Monsters protect their objects */
345                                 if (randint0(100) < 90) continue;
346                         }
347
348                         /* Dungeon */
349                         else
350                         {
351                                 y = o_ptr->iy;
352                                 x = o_ptr->ix;
353                         }
354
355                         /* Nearby objects start out "immune" */
356                         if ((cur_dis > 0) && (distance(p_ptr->y, p_ptr->x, y, x) < cur_dis)) continue;
357
358                         /* Saving throw */
359                         chance = 90;
360
361                         /* Hack -- only compact artifacts in emergencies */
362                         if ((object_is_fixed_artifact(o_ptr) || o_ptr->art_name) &&
363                             (cnt < 1000)) chance = 100;
364
365                         /* Apply the saving throw */
366                         if (randint0(100) < chance) continue;
367
368                         delete_object_idx(i);
369
370                         /* Count it */
371                         num++;
372                 }
373         }
374
375
376         /* Excise dead objects (backwards!) */
377         for (i = o_max - 1; i >= 1; i--)
378         {
379                 o_ptr = &current_floor_ptr->o_list[i];
380
381                 /* Skip real objects */
382                 if (o_ptr->k_idx) continue;
383
384                 /* Move last object into open hole */
385                 compact_objects_aux(o_max - 1, i);
386
387                 /* Compress "o_max" */
388                 o_max--;
389         }
390 }
391
392
393 /*!
394  * @brief グローバルオブジェクト配列を初期化する /
395  * Delete all the items when player leaves the level
396  * @note we do NOT visually reflect these (irrelevant) changes
397  * @details
398  * Hack -- we clear the "g_ptr->o_idx" field for every grid,
399  * and the "m_ptr->next_o_idx" field for every monster, since
400  * we know we are clearing every object.  Technically, we only
401  * clear those fields for grids/monsters containing objects,
402  * and we clear it once for every such object.
403  * @return なし
404  */
405 void wipe_o_list(void)
406 {
407         int i;
408
409         /* Delete the existing objects */
410         for (i = 1; i < o_max; i++)
411         {
412                 object_type *o_ptr = &current_floor_ptr->o_list[i];
413
414                 /* Skip dead objects */
415                 if (!o_ptr->k_idx) continue;
416
417                 /* Mega-Hack -- preserve artifacts */
418                 if (!character_dungeon || preserve_mode)
419                 {
420                         /* Hack -- Preserve unknown artifacts */
421                         if (object_is_fixed_artifact(o_ptr) && !object_is_known(o_ptr))
422                         {
423                                 /* Mega-Hack -- Preserve the artifact */
424                                 a_info[o_ptr->name1].cur_num = 0;
425                         }
426                 }
427
428                 if (o_ptr->held_m_idx)
429                 {
430                         monster_type *m_ptr;
431                         m_ptr = &current_floor_ptr->m_list[o_ptr->held_m_idx];
432
433                         /* Hack -- see above */
434                         m_ptr->hold_o_idx = 0;
435                 }
436
437                 /* Dungeon */
438                 else
439                 {
440                         grid_type *g_ptr;
441
442                         /* Access location */
443                         POSITION y = o_ptr->iy;
444                         POSITION x = o_ptr->ix;
445
446                         /* Access grid */
447                         g_ptr = &current_floor_ptr->grid_array[y][x];
448
449                         /* Hack -- see above */
450                         g_ptr->o_idx = 0;
451                 }
452                 object_wipe(o_ptr);
453         }
454
455         /* Reset "o_max" */
456         o_max = 1;
457
458         /* Reset "o_cnt" */
459         o_cnt = 0;
460 }
461
462
463 /*!
464  * @brief グローバルオブジェクト配列から空きを取得する /
465  * Acquires and returns the index of a "free" object.
466  * @return 開いているオブジェクト要素のID
467  * @details
468  * This routine should almost never fail, but in case it does,
469  * we must be sure to handle "failure" of this routine.
470  */
471 OBJECT_IDX o_pop(void)
472 {
473         OBJECT_IDX i;
474
475         /* Initial allocation */
476         if (o_max < current_floor_ptr->max_o_idx)
477         {
478                 /* Get next space */
479                 i = o_max;
480
481                 /* Expand object array */
482                 o_max++;
483
484                 /* Count objects */
485                 o_cnt++;
486
487                 /* Use this object */
488                 return (i);
489         }
490
491
492         /* Recycle dead objects */
493         for (i = 1; i < o_max; i++)
494         {
495                 object_type *o_ptr;
496                 o_ptr = &current_floor_ptr->o_list[i];
497
498                 /* Skip live objects */
499                 if (o_ptr->k_idx) continue;
500
501                 /* Count objects */
502                 o_cnt++;
503
504                 /* Use this object */
505                 return (i);
506         }
507
508
509         /* Warn the player (except during dungeon creation) */
510         if (character_dungeon) msg_print(_("アイテムが多すぎる!", "Too many objects!"));
511
512         return (0);
513 }
514
515
516 /*!
517  * @brief オブジェクト生成テーブルに生成制約を加える /
518  * Apply a "object restriction function" to the "object allocation table"
519  * @return 常に0を返す。
520  * @details 生成の制約はグローバルのget_obj_num_hook関数ポインタで加える
521  */
522 static errr get_obj_num_prep(void)
523 {
524         int i;
525
526         /* Get the entry */
527         alloc_entry *table = alloc_kind_table;
528
529         /* Scan the allocation table */
530         for (i = 0; i < alloc_kind_size; i++)
531         {
532                 /* Accept objects which pass the restriction, if any */
533                 if (!get_obj_num_hook || (*get_obj_num_hook)(table[i].index))
534                 {
535                         /* Accept this object */
536                         table[i].prob2 = table[i].prob1;
537                 }
538
539                 /* Do not use this object */
540                 else
541                 {
542                         /* Decline this object */
543                         table[i].prob2 = 0;
544                 }
545         }
546
547         /* Success */
548         return (0);
549 }
550
551
552 /*!
553  * @brief オブジェクト生成テーブルからアイテムを取得する /
554  * Choose an object kind that seems "appropriate" to the given level
555  * @param level 生成階
556  * @return 選ばれたオブジェクトベースID
557  * @details
558  * This function uses the "prob2" field of the "object allocation table",\n
559  * and various local information, to calculate the "prob3" field of the\n
560  * same table, which is then used to choose an "appropriate" object, in\n
561  * a relatively efficient manner.\n
562  *\n
563  * It is (slightly) more likely to acquire an object of the given level\n
564  * than one of a lower level.  This is done by choosing several objects\n
565  * appropriate to the given level and keeping the "hardest" one.\n
566  *\n
567  * Note that if no objects are "appropriate", then this function will\n
568  * fail, and return zero, but this should *almost* never happen.\n
569  */
570 OBJECT_IDX get_obj_num(DEPTH level)
571 {
572         int i, j, p;
573         KIND_OBJECT_IDX k_idx;
574         long value, total;
575         object_kind     *k_ptr;
576         alloc_entry     *table = alloc_kind_table;
577
578         if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
579
580         /* Boost level */
581         if ((level > 0) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_BEGINNER))
582         {
583                 /* Occasional "boost" */
584                 if (one_in_(GREAT_OBJ))
585                 {
586                         /* What a bizarre calculation */
587                         level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
588                 }
589         }
590
591         /* Reset total */
592         total = 0L;
593
594         /* Process probabilities */
595         for (i = 0; i < alloc_kind_size; i++)
596         {
597                 /* Objects are sorted by depth */
598                 if (table[i].level > level) break;
599
600                 /* Default */
601                 table[i].prob3 = 0;
602
603                 /* Access the index */
604                 k_idx = table[i].index;
605
606                 /* Access the actual kind */
607                 k_ptr = &k_info[k_idx];
608
609                 /* Hack -- prevent embedded chests */
610                 if (opening_chest && (k_ptr->tval == TV_CHEST)) continue;
611
612                 /* Accept */
613                 table[i].prob3 = table[i].prob2;
614
615                 /* Total */
616                 total += table[i].prob3;
617         }
618
619         /* No legal objects */
620         if (total <= 0) return (0);
621
622
623         /* Pick an object */
624         value = randint0(total);
625
626         /* Find the object */
627         for (i = 0; i < alloc_kind_size; i++)
628         {
629                 /* Found the entry */
630                 if (value < table[i].prob3) break;
631
632                 /* Decrement */
633                 value = value - table[i].prob3;
634         }
635
636
637         /* Power boost */
638         p = randint0(100);
639
640         /* Try for a "better" object once (50%) or twice (10%) */
641         if (p < 60)
642         {
643                 /* Save old */
644                 j = i;
645
646                 /* Pick a object */
647                 value = randint0(total);
648
649                 /* Find the object */
650                 for (i = 0; i < alloc_kind_size; i++)
651                 {
652                         /* Found the entry */
653                         if (value < table[i].prob3) break;
654
655                         /* Decrement */
656                         value = value - table[i].prob3;
657                 }
658
659                 /* Keep the "best" one */
660                 if (table[i].level < table[j].level) i = j;
661         }
662
663         /* Try for a "better" object twice (10%) */
664         if (p < 10)
665         {
666                 /* Save old */
667                 j = i;
668
669                 /* Pick a object */
670                 value = randint0(total);
671
672                 /* Find the object */
673                 for (i = 0; i < alloc_kind_size; i++)
674                 {
675                         /* Found the entry */
676                         if (value < table[i].prob3) break;
677
678                         /* Decrement */
679                         value = value - table[i].prob3;
680                 }
681
682                 /* Keep the "best" one */
683                 if (table[i].level < table[j].level) i = j;
684         }
685
686         return (table[i].index);
687 }
688
689
690 /*!
691  * @brief オブジェクトを鑑定済にする /
692  * Known is true when the "attributes" of an object are "known".
693  * @param o_ptr 鑑定済にするオブジェクトの構造体参照ポインタ
694  * @return なし
695  * These include tohit, todam, toac, cost, and pval (charges).\n
696  *\n
697  * Note that "knowing" an object gives you everything that an "awareness"\n
698  * gives you, and much more.  In fact, the player is always "aware" of any\n
699  * item of which he has full "knowledge".\n
700  *\n
701  * But having full knowledge of, say, one "wand of wonder", does not, by\n
702  * itself, give you knowledge, or even awareness, of other "wands of wonder".\n
703  * It happens that most "identify" routines (including "buying from a shop")\n
704  * will make the player "aware" of the object as well as fully "know" it.\n
705  *\n
706  * This routine also removes any inscriptions generated by "feelings".\n
707  */
708 void object_known(object_type *o_ptr)
709 {
710         /* Remove "default inscriptions" */
711         o_ptr->feeling = FEEL_NONE;
712
713         /* Clear the "Felt" info */
714         o_ptr->ident &= ~(IDENT_SENSE);
715
716         /* Clear the "Empty" info */
717         o_ptr->ident &= ~(IDENT_EMPTY);
718
719         /* Now we know about the item */
720         o_ptr->ident |= (IDENT_KNOWN);
721 }
722
723 /*!
724  * @brief オブジェクトを*鑑定*済にする /
725  * The player is now aware of the effects of the given object.
726  * @param o_ptr *鑑定*済にするオブジェクトの構造体参照ポインタ
727  * @return なし
728  */
729 void object_aware(object_type *o_ptr)
730 {
731         bool mihanmei = !object_is_aware(o_ptr);
732
733         /* Fully aware of the effects */
734         k_info[o_ptr->k_idx].aware = TRUE;
735
736         if(mihanmei && !(k_info[o_ptr->k_idx].gen_flags & TRG_INSTA_ART) && record_ident &&
737            !p_ptr->is_dead && ((o_ptr->tval >= TV_AMULET && o_ptr->tval <= TV_POTION) || (o_ptr->tval == TV_FOOD)))
738         {
739                 object_type forge;
740                 object_type *q_ptr;
741                 GAME_TEXT o_name[MAX_NLEN];
742
743                 q_ptr = &forge;
744                 object_copy(q_ptr, o_ptr);
745
746                 q_ptr->number = 1;
747                 object_desc(o_name, q_ptr, OD_NAME_ONLY);
748                 
749                 do_cmd_write_nikki(NIKKI_HANMEI, 0, o_name);
750         }
751 }
752
753 /*!
754  * @brief オブジェクトを試行済にする /
755  * Something has been "sampled"
756  * @param o_ptr 試行済にするオブジェクトの構造体参照ポインタ
757  * @return なし
758  */
759 void object_tried(object_type *o_ptr)
760 {
761         /* Mark it as tried (even if "aware") */
762         k_info[o_ptr->k_idx].tried = TRUE;
763 }
764
765 /*!
766 * @brief 重度擬似鑑定の判断処理 / Return a "feeling" (or NULL) about an item.  Method 1 (Heavy).
767 * @param o_ptr 擬似鑑定を行うオブジェクトの参照ポインタ。
768 * @return 擬似鑑定結果のIDを返す。
769 */
770 byte value_check_aux1(object_type *o_ptr)
771 {
772         /* Artifacts */
773         if (object_is_artifact(o_ptr))
774         {
775                 /* Cursed/Broken */
776                 if (object_is_cursed(o_ptr) || object_is_broken(o_ptr)) return FEEL_TERRIBLE;
777
778                 /* Normal */
779                 return FEEL_SPECIAL;
780         }
781
782         /* Ego-Items */
783         if (object_is_ego(o_ptr))
784         {
785                 /* Cursed/Broken */
786                 if (object_is_cursed(o_ptr) || object_is_broken(o_ptr)) return FEEL_WORTHLESS;
787
788                 /* Normal */
789                 return FEEL_EXCELLENT;
790         }
791
792         /* Cursed items */
793         if (object_is_cursed(o_ptr)) return FEEL_CURSED;
794
795         /* Broken items */
796         if (object_is_broken(o_ptr)) return FEEL_BROKEN;
797
798         if ((o_ptr->tval == TV_RING) || (o_ptr->tval == TV_AMULET)) return FEEL_AVERAGE;
799
800         /* Good "armor" bonus */
801         if (o_ptr->to_a > 0) return FEEL_GOOD;
802
803         /* Good "weapon" bonus */
804         if (o_ptr->to_h + o_ptr->to_d > 0) return FEEL_GOOD;
805
806         /* Default to "average" */
807         return FEEL_AVERAGE;
808 }
809
810 /*!
811 * @brief 軽度擬似鑑定の判断処理 / Return a "feeling" (or NULL) about an item.  Method 2 (Light).
812 * @param o_ptr 擬似鑑定を行うオブジェクトの参照ポインタ。
813 * @return 擬似鑑定結果のIDを返す。
814 */
815 byte value_check_aux2(object_type *o_ptr)
816 {
817         /* Cursed items (all of them) */
818         if (object_is_cursed(o_ptr)) return FEEL_CURSED;
819
820         /* Broken items (all of them) */
821         if (object_is_broken(o_ptr)) return FEEL_BROKEN;
822
823         /* Artifacts -- except cursed/broken ones */
824         if (object_is_artifact(o_ptr)) return FEEL_UNCURSED;
825
826         /* Ego-Items -- except cursed/broken ones */
827         if (object_is_ego(o_ptr)) return FEEL_UNCURSED;
828
829         /* Good armor bonus */
830         if (o_ptr->to_a > 0) return FEEL_UNCURSED;
831
832         /* Good weapon bonuses */
833         if (o_ptr->to_h + o_ptr->to_d > 0) return FEEL_UNCURSED;
834
835         /* No feeling */
836         return FEEL_NONE;
837 }
838
839 /*!
840  * @brief 未鑑定なベースアイテムの基本価格を返す /
841  * Return the "value" of an "unknown" item Make a guess at the value of non-aware items
842  * @param o_ptr 未鑑定価格を確認したいオブジェクトの構造体参照ポインタ
843  * @return オブジェクトの未鑑定価格
844  */
845 static PRICE object_value_base(object_type *o_ptr)
846 {
847         /* Aware item -- use template cost */
848         if (object_is_aware(o_ptr)) return (k_info[o_ptr->k_idx].cost);
849
850         /* Analyze the type */
851         switch (o_ptr->tval)
852         {
853
854                 /* Un-aware Food */
855                 case TV_FOOD: return (5L);
856
857                 /* Un-aware Potions */
858                 case TV_POTION: return (20L);
859
860                 /* Un-aware Scrolls */
861                 case TV_SCROLL: return (20L);
862
863                 /* Un-aware Staffs */
864                 case TV_STAFF: return (70L);
865
866                 /* Un-aware Wands */
867                 case TV_WAND: return (50L);
868
869                 /* Un-aware Rods */
870                 case TV_ROD: return (90L);
871
872                 /* Un-aware Rings */
873                 case TV_RING: return (45L);
874
875                 /* Un-aware Amulets */
876                 case TV_AMULET: return (45L);
877
878                 /* Figurines, relative to monster level */
879                 case TV_FIGURINE:
880                 {
881                         DEPTH level = r_info[o_ptr->pval].level;
882                         if (level < 20) return level*50L;
883                         else if (level < 30) return 1000+(level-20)*150L;
884                         else if (level < 40) return 2500+(level-30)*350L;
885                         else if (level < 50) return 6000+(level-40)*800L;
886                         else return 14000+(level-50)*2000L;
887                 }
888
889                 case TV_CAPTURE:
890                         if (!o_ptr->pval) return 1000L;
891                         else return ((r_info[o_ptr->pval].level) * 50L + 1000);
892         }
893
894         /* Paranoia -- Oops */
895         return (0L);
896 }
897
898
899 /*!
900  * @brief オブジェクトのフラグ類から価格を算出する /
901  * Return the value of the flags the object has...
902  * @param o_ptr フラグ価格を確認したいオブジェクトの構造体参照ポインタ
903  * @param plusses フラグに与える価格の基本重み
904  * @return オブジェクトのフラグ価格
905  */
906 PRICE flag_cost(object_type *o_ptr, int plusses)
907 {
908         PRICE total = 0;
909         BIT_FLAGS flgs[TR_FLAG_SIZE];
910         PRICE tmp_cost;
911         int count;
912         int i;
913         object_kind *k_ptr = &k_info[o_ptr->k_idx];
914
915         object_flags(o_ptr, flgs);
916
917         /*
918          * Exclude fixed flags of the base item.
919          * pval bonuses of base item will be treated later.
920          */
921         for (i = 0; i < TR_FLAG_SIZE; i++)
922                 flgs[i] &= ~(k_ptr->flags[i]);
923
924         /* Exclude fixed flags of the fixed artifact. */
925         if (object_is_fixed_artifact(o_ptr))
926         {
927                 artifact_type *a_ptr = &a_info[o_ptr->name1];
928
929                 for (i = 0; i < TR_FLAG_SIZE; i++)
930                         flgs[i] &= ~(a_ptr->flags[i]);
931         }
932
933         /* Exclude fixed flags of the ego-item. */
934         else if (object_is_ego(o_ptr))
935         {
936                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
937
938                 for (i = 0; i < TR_FLAG_SIZE; i++)
939                         flgs[i] &= ~(e_ptr->flags[i]);
940         }
941
942
943         /*
944          * Calucurate values of remaining flags
945          */
946         if (have_flag(flgs, TR_STR)) total += (1500 * plusses);
947         if (have_flag(flgs, TR_INT)) total += (1500 * plusses);
948         if (have_flag(flgs, TR_WIS)) total += (1500 * plusses);
949         if (have_flag(flgs, TR_DEX)) total += (1500 * plusses);
950         if (have_flag(flgs, TR_CON)) total += (1500 * plusses);
951         if (have_flag(flgs, TR_CHR)) total += (750 * plusses);
952         if (have_flag(flgs, TR_MAGIC_MASTERY)) total += (600 * plusses);
953         if (have_flag(flgs, TR_STEALTH)) total += (250 * plusses);
954         if (have_flag(flgs, TR_SEARCH)) total += (100 * plusses);
955         if (have_flag(flgs, TR_INFRA)) total += (150 * plusses);
956         if (have_flag(flgs, TR_TUNNEL)) total += (175 * plusses);
957         if ((have_flag(flgs, TR_SPEED)) && (plusses > 0))
958                 total += (10000 + (2500 * plusses));
959         if ((have_flag(flgs, TR_BLOWS)) && (plusses > 0))
960                 total += (10000 + (2500 * plusses));
961
962         tmp_cost = 0;
963         count = 0;
964         if (have_flag(flgs, TR_CHAOTIC)) {total += 5000;count++;}
965         if (have_flag(flgs, TR_VAMPIRIC)) {total += 6500;count++;}
966         if (have_flag(flgs, TR_FORCE_WEAPON)) {tmp_cost += 2500;count++;}
967         if (have_flag(flgs, TR_KILL_ANIMAL)) {tmp_cost += 2800;count++;}
968         else if (have_flag(flgs, TR_SLAY_ANIMAL)) {tmp_cost += 1800;count++;}
969         if (have_flag(flgs, TR_KILL_EVIL)) {tmp_cost += 3300;count++;}
970         else if (have_flag(flgs, TR_SLAY_EVIL)) {tmp_cost += 2300;count++;}
971         if (have_flag(flgs, TR_KILL_HUMAN)) {tmp_cost += 2800;count++;}
972         else if (have_flag(flgs, TR_SLAY_HUMAN)) {tmp_cost += 1800;count++;}
973         if (have_flag(flgs, TR_KILL_UNDEAD)) {tmp_cost += 2800;count++;}
974         else if (have_flag(flgs, TR_SLAY_UNDEAD)) {tmp_cost += 1800;count++;}
975         if (have_flag(flgs, TR_KILL_DEMON)) {tmp_cost += 2800;count++;}
976         else if (have_flag(flgs, TR_SLAY_DEMON)) {tmp_cost += 1800;count++;}
977         if (have_flag(flgs, TR_KILL_ORC)) {tmp_cost += 2500;count++;}
978         else if (have_flag(flgs, TR_SLAY_ORC)) {tmp_cost += 1500;count++;}
979         if (have_flag(flgs, TR_KILL_TROLL)) {tmp_cost += 2800;count++;}
980         else if (have_flag(flgs, TR_SLAY_TROLL)) {tmp_cost += 1800;count++;}
981         if (have_flag(flgs, TR_KILL_GIANT)) {tmp_cost += 2800;count++;}
982         else if (have_flag(flgs, TR_SLAY_GIANT)) {tmp_cost += 1800;count++;}
983         if (have_flag(flgs, TR_KILL_DRAGON)) {tmp_cost += 2800;count++;}
984         else if (have_flag(flgs, TR_SLAY_DRAGON)) {tmp_cost += 1800;count++;}
985
986         if (have_flag(flgs, TR_VORPAL)) {tmp_cost += 2500;count++;}
987         if (have_flag(flgs, TR_IMPACT)) {tmp_cost += 2500;count++;}
988         if (have_flag(flgs, TR_BRAND_POIS)) {tmp_cost += 3800;count++;}
989         if (have_flag(flgs, TR_BRAND_ACID)) {tmp_cost += 3800;count++;}
990         if (have_flag(flgs, TR_BRAND_ELEC)) {tmp_cost += 3800;count++;}
991         if (have_flag(flgs, TR_BRAND_FIRE)) {tmp_cost += 2500;count++;}
992         if (have_flag(flgs, TR_BRAND_COLD)) {tmp_cost += 2500;count++;}
993         total += (tmp_cost * count);
994
995         if (have_flag(flgs, TR_SUST_STR)) total += 850;
996         if (have_flag(flgs, TR_SUST_INT)) total += 850;
997         if (have_flag(flgs, TR_SUST_WIS)) total += 850;
998         if (have_flag(flgs, TR_SUST_DEX)) total += 850;
999         if (have_flag(flgs, TR_SUST_CON)) total += 850;
1000         if (have_flag(flgs, TR_SUST_CHR)) total += 250;
1001         if (have_flag(flgs, TR_RIDING)) total += 0;
1002         if (have_flag(flgs, TR_EASY_SPELL)) total += 1500;
1003         if (have_flag(flgs, TR_THROW)) total += 5000;
1004         if (have_flag(flgs, TR_FREE_ACT)) total += 4500;
1005         if (have_flag(flgs, TR_HOLD_EXP)) total += 8500;
1006
1007         tmp_cost = 0;
1008         count = 0;
1009         if (have_flag(flgs, TR_IM_ACID)) {tmp_cost += 15000;count += 2;}
1010         if (have_flag(flgs, TR_IM_ELEC)) {tmp_cost += 15000;count += 2;}
1011         if (have_flag(flgs, TR_IM_FIRE)) {tmp_cost += 15000;count += 2;}
1012         if (have_flag(flgs, TR_IM_COLD)) {tmp_cost += 15000;count += 2;}
1013         if (have_flag(flgs, TR_REFLECT)) {tmp_cost += 5000;count += 2;}
1014         if (have_flag(flgs, TR_RES_ACID)) {tmp_cost += 500;count++;}
1015         if (have_flag(flgs, TR_RES_ELEC)) {tmp_cost += 500;count++;}
1016         if (have_flag(flgs, TR_RES_FIRE)) {tmp_cost += 500;count++;}
1017         if (have_flag(flgs, TR_RES_COLD)) {tmp_cost += 500;count++;}
1018         if (have_flag(flgs, TR_RES_POIS)) {tmp_cost += 1000;count += 2;}
1019         if (have_flag(flgs, TR_RES_FEAR)) {tmp_cost += 1000;count += 2;}
1020         if (have_flag(flgs, TR_RES_LITE)) {tmp_cost += 800;count += 2;}
1021         if (have_flag(flgs, TR_RES_DARK)) {tmp_cost += 800;count += 2;}
1022         if (have_flag(flgs, TR_RES_BLIND)) {tmp_cost += 900;count += 2;}
1023         if (have_flag(flgs, TR_RES_CONF)) {tmp_cost += 900;count += 2;}
1024         if (have_flag(flgs, TR_RES_SOUND)) {tmp_cost += 900;count += 2;}
1025         if (have_flag(flgs, TR_RES_SHARDS)) {tmp_cost += 900;count += 2;}
1026         if (have_flag(flgs, TR_RES_NETHER)) {tmp_cost += 900;count += 2;}
1027         if (have_flag(flgs, TR_RES_NEXUS)) {tmp_cost += 900;count += 2;}
1028         if (have_flag(flgs, TR_RES_CHAOS)) {tmp_cost += 1000;count += 2;}
1029         if (have_flag(flgs, TR_RES_DISEN)) {tmp_cost += 2000;count += 2;}
1030         total += (tmp_cost * count);
1031
1032         if (have_flag(flgs, TR_SH_FIRE)) total += 5000;
1033         if (have_flag(flgs, TR_SH_ELEC)) total += 5000;
1034         if (have_flag(flgs, TR_SH_COLD)) total += 5000;
1035         if (have_flag(flgs, TR_NO_TELE)) total -= 10000;
1036         if (have_flag(flgs, TR_NO_MAGIC)) total += 2500;
1037         if (have_flag(flgs, TR_TY_CURSE)) total -= 15000;
1038         if (have_flag(flgs, TR_HIDE_TYPE)) total += 0;
1039         if (have_flag(flgs, TR_SHOW_MODS)) total += 0;
1040         if (have_flag(flgs, TR_LEVITATION)) total += 1250;
1041         if (have_flag(flgs, TR_LITE_1)) total += 1500;
1042         if (have_flag(flgs, TR_LITE_2)) total += 2500;
1043         if (have_flag(flgs, TR_LITE_3)) total += 4000;
1044         if (have_flag(flgs, TR_LITE_M1)) total -= 1500;
1045         if (have_flag(flgs, TR_LITE_M2)) total -= 2500;
1046         if (have_flag(flgs, TR_LITE_M3)) total -= 4000;
1047         if (have_flag(flgs, TR_SEE_INVIS)) total += 2000;
1048         if (have_flag(flgs, TR_TELEPATHY)) total += 20000;
1049         if (have_flag(flgs, TR_ESP_ANIMAL)) total += 1000;
1050         if (have_flag(flgs, TR_ESP_UNDEAD)) total += 1000;
1051         if (have_flag(flgs, TR_ESP_DEMON)) total += 1000;
1052         if (have_flag(flgs, TR_ESP_ORC)) total += 1000;
1053         if (have_flag(flgs, TR_ESP_TROLL)) total += 1000;
1054         if (have_flag(flgs, TR_ESP_GIANT)) total += 1000;
1055         if (have_flag(flgs, TR_ESP_DRAGON)) total += 1000;
1056         if (have_flag(flgs, TR_ESP_HUMAN)) total += 1000;
1057         if (have_flag(flgs, TR_ESP_EVIL)) total += 15000;
1058         if (have_flag(flgs, TR_ESP_GOOD)) total += 2000;
1059         if (have_flag(flgs, TR_ESP_NONLIVING)) total += 2000;
1060         if (have_flag(flgs, TR_ESP_UNIQUE)) total += 10000;
1061         if (have_flag(flgs, TR_SLOW_DIGEST)) total += 750;
1062         if (have_flag(flgs, TR_REGEN)) total += 2500;
1063         if (have_flag(flgs, TR_WARNING)) total += 2000;
1064         if (have_flag(flgs, TR_DEC_MANA)) total += 10000;
1065         if (have_flag(flgs, TR_XTRA_MIGHT)) total += 2250;
1066         if (have_flag(flgs, TR_XTRA_SHOTS)) total += 10000;
1067         if (have_flag(flgs, TR_IGNORE_ACID)) total += 100;
1068         if (have_flag(flgs, TR_IGNORE_ELEC)) total += 100;
1069         if (have_flag(flgs, TR_IGNORE_FIRE)) total += 100;
1070         if (have_flag(flgs, TR_IGNORE_COLD)) total += 100;
1071         if (have_flag(flgs, TR_ACTIVATE)) total += 100;
1072         if (have_flag(flgs, TR_DRAIN_EXP)) total -= 12500;
1073         if (have_flag(flgs, TR_DRAIN_HP)) total -= 12500;
1074         if (have_flag(flgs, TR_DRAIN_MANA)) total -= 12500;
1075         if (have_flag(flgs, TR_CALL_ANIMAL)) total -= 12500;
1076         if (have_flag(flgs, TR_CALL_DEMON)) total -= 10000;
1077         if (have_flag(flgs, TR_CALL_DRAGON)) total -= 10000;
1078         if (have_flag(flgs, TR_CALL_UNDEAD)) total -= 10000;
1079         if (have_flag(flgs, TR_COWARDICE)) total -= 5000;
1080         if (have_flag(flgs, TR_LOW_MELEE)) total -= 5000;
1081         if (have_flag(flgs, TR_LOW_AC)) total -= 5000;
1082         if (have_flag(flgs, TR_LOW_MAGIC)) total -= 15000;
1083         if (have_flag(flgs, TR_FAST_DIGEST)) total -= 10000;
1084         if (have_flag(flgs, TR_SLOW_REGEN)) total -= 10000;
1085         if (have_flag(flgs, TR_TELEPORT))
1086         {
1087                 if (object_is_cursed(o_ptr))
1088                         total -= 7500;
1089                 else
1090                         total += 250;
1091         }
1092         if (have_flag(flgs, TR_AGGRAVATE)) total -= 10000;
1093         if (have_flag(flgs, TR_BLESSED)) total += 750;
1094         if (o_ptr->curse_flags & TR_ADD_L_CURSE) total -= 5000;
1095         if (o_ptr->curse_flags & TR_ADD_H_CURSE) total -= 12500;
1096         if (o_ptr->curse_flags & TRC_CURSED) total -= 5000;
1097         if (o_ptr->curse_flags & TRC_HEAVY_CURSE) total -= 12500;
1098         if (o_ptr->curse_flags & TRC_PERMA_CURSE) total -= 15000;
1099
1100         /* Also, give some extra for activatable powers... */
1101         if (o_ptr->art_name && (have_flag(o_ptr->art_flags, TR_ACTIVATE)))
1102         {
1103                 const activation_type* const act_ptr = find_activation_info(o_ptr);
1104                 if (act_ptr) {
1105                         total += act_ptr->value;
1106                 }
1107         }
1108
1109         return total;
1110 }
1111
1112
1113 /*!
1114  * @brief オブジェクトの真の価格を算出する /
1115  * Return the value of the flags the object has...
1116  * @param o_ptr 本価格を確認したいオブジェクトの構造体参照ポインタ
1117  * @return オブジェクトの本価格
1118  * @details
1119  * Return the "real" price of a "known" item, not including discounts\n
1120  *\n
1121  * Wand and staffs get cost for each charge\n
1122  *\n
1123  * Armor is worth an extra 100 gold per bonus point to armor class.\n
1124  *\n
1125  * Weapons are worth an extra 100 gold per bonus point (AC,TH,TD).\n
1126  *\n
1127  * Missiles are only worth 5 gold per bonus point, since they\n
1128  * usually appear in groups of 20, and we want the player to get\n
1129  * the same amount of cash for any "equivalent" item.  Note that\n
1130  * missiles never have any of the "pval" flags, and in fact, they\n
1131  * only have a few of the available flags, primarily of the "slay"\n
1132  * and "brand" and "ignore" variety.\n
1133  *\n
1134  * Armor with a negative armor bonus is worthless.\n
1135  * Weapons with negative hit+damage bonuses are worthless.\n
1136  *\n
1137  * Every wearable item with a "pval" bonus is worth extra (see below).\n
1138  */
1139 PRICE object_value_real(object_type *o_ptr)
1140 {
1141         PRICE value;
1142         BIT_FLAGS flgs[TR_FLAG_SIZE];
1143         object_kind *k_ptr = &k_info[o_ptr->k_idx];
1144
1145
1146         /* Hack -- "worthless" items */
1147         if (!k_info[o_ptr->k_idx].cost) return (0L);
1148
1149         /* Base cost */
1150         value = k_info[o_ptr->k_idx].cost;
1151
1152         /* Extract some flags */
1153         object_flags(o_ptr, flgs);
1154
1155         /* Artifact */
1156         if (object_is_fixed_artifact(o_ptr))
1157         {
1158                 artifact_type *a_ptr = &a_info[o_ptr->name1];
1159
1160                 /* Hack -- "worthless" artifacts */
1161                 if (!a_ptr->cost) return (0L);
1162
1163                 /* Hack -- Use the artifact cost instead */
1164                 value = a_ptr->cost;
1165                 value += flag_cost(o_ptr, o_ptr->pval);
1166
1167                 /* Don't add pval bonuses etc. */
1168                 return (value);
1169         }
1170
1171         /* Ego-Item */
1172         else if (object_is_ego(o_ptr))
1173         {
1174                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
1175
1176                 /* Hack -- "worthless" ego-items */
1177                 if (!e_ptr->cost) return (0L);
1178
1179                 /* Hack -- Reward the ego-item with a bonus */
1180                 value += e_ptr->cost;
1181                 value += flag_cost(o_ptr, o_ptr->pval);
1182         }
1183
1184         else
1185         {
1186                 int i;
1187                 bool flag = FALSE;
1188
1189                 for (i = 0; i < TR_FLAG_SIZE; i++) 
1190                         if (o_ptr->art_flags[i]) flag = TRUE;
1191
1192                 if (flag) value += flag_cost(o_ptr, o_ptr->pval);
1193         }
1194
1195         /* Analyze pval bonus for normal object */
1196         switch (o_ptr->tval)
1197         {
1198         case TV_SHOT:
1199         case TV_ARROW:
1200         case TV_BOLT:
1201         case TV_BOW:
1202         case TV_DIGGING:
1203         case TV_HAFTED:
1204         case TV_POLEARM:
1205         case TV_SWORD:
1206         case TV_BOOTS:
1207         case TV_GLOVES:
1208         case TV_HELM:
1209         case TV_CROWN:
1210         case TV_SHIELD:
1211         case TV_CLOAK:
1212         case TV_SOFT_ARMOR:
1213         case TV_HARD_ARMOR:
1214         case TV_DRAG_ARMOR:
1215         case TV_LITE:
1216         case TV_AMULET:
1217         case TV_RING:
1218                 /* No pval */
1219                 if (!o_ptr->pval) break;
1220
1221                 /* Hack -- Negative "pval" is always bad */
1222                 if (o_ptr->pval < 0) return (0L);
1223
1224                 /* Give credit for stat bonuses */
1225                 if (have_flag(flgs, TR_STR)) value += (o_ptr->pval * 200L);
1226                 if (have_flag(flgs, TR_INT)) value += (o_ptr->pval * 200L);
1227                 if (have_flag(flgs, TR_WIS)) value += (o_ptr->pval * 200L);
1228                 if (have_flag(flgs, TR_DEX)) value += (o_ptr->pval * 200L);
1229                 if (have_flag(flgs, TR_CON)) value += (o_ptr->pval * 200L);
1230                 if (have_flag(flgs, TR_CHR)) value += (o_ptr->pval * 200L);
1231
1232                 /* Give credit for stealth and searching */
1233                 if (have_flag(flgs, TR_MAGIC_MASTERY)) value += (o_ptr->pval * 100);
1234                 if (have_flag(flgs, TR_STEALTH)) value += (o_ptr->pval * 100L);
1235                 if (have_flag(flgs, TR_SEARCH)) value += (o_ptr->pval * 100L);
1236
1237                 /* Give credit for infra-vision and tunneling */
1238                 if (have_flag(flgs, TR_INFRA)) value += (o_ptr->pval * 50L);
1239                 if (have_flag(flgs, TR_TUNNEL)) value += (o_ptr->pval * 50L);
1240
1241                 /* Give credit for extra attacks */
1242                 if (have_flag(flgs, TR_BLOWS)) value += (o_ptr->pval * 5000L);
1243
1244                 /* Give credit for speed bonus */
1245                 if (have_flag(flgs, TR_SPEED)) value += (o_ptr->pval * 10000L);
1246
1247                 break;
1248         }
1249
1250
1251         /* Analyze the item */
1252         switch (o_ptr->tval)
1253         {
1254                 /* Wands/Staffs */
1255                 case TV_WAND:
1256                 {
1257                         /* Pay extra for charges, depending on standard number of
1258                          * charges.  Handle new-style wands correctly. -LM-
1259                          */
1260                         value += (value * o_ptr->pval / o_ptr->number / (k_ptr->pval * 2));
1261
1262                         break;
1263                 }
1264                 case TV_STAFF:
1265                 {
1266                         /* Pay extra for charges, depending on standard number of
1267                          * charges.  -LM-
1268                          */
1269                         value += (value * o_ptr->pval / (k_ptr->pval * 2));
1270
1271                         break;
1272                 }
1273
1274                 /* Rings/Amulets */
1275                 case TV_RING:
1276                 case TV_AMULET:
1277                 {
1278                         /* Hack -- negative bonuses are bad */
1279                         if (o_ptr->to_h + o_ptr->to_d + o_ptr->to_a < 0) return (0L);
1280
1281                         /* Give credit for bonuses */
1282                         value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 200L);
1283
1284                         break;
1285                 }
1286
1287                 /* Armor */
1288                 case TV_BOOTS:
1289                 case TV_GLOVES:
1290                 case TV_CLOAK:
1291                 case TV_CROWN:
1292                 case TV_HELM:
1293                 case TV_SHIELD:
1294                 case TV_SOFT_ARMOR:
1295                 case TV_HARD_ARMOR:
1296                 case TV_DRAG_ARMOR:
1297                 {
1298                         /* Hack -- negative armor bonus */
1299                         if (o_ptr->to_a < 0) return (0L);
1300
1301                         /* Give credit for bonuses */
1302                         value += (((o_ptr->to_h - k_ptr->to_h) + (o_ptr->to_d - k_ptr->to_d)) * 200L + (o_ptr->to_a) * 100L);
1303
1304                         break;
1305                 }
1306
1307                 /* Bows/Weapons */
1308                 case TV_BOW:
1309                 case TV_DIGGING:
1310                 case TV_HAFTED:
1311                 case TV_SWORD:
1312                 case TV_POLEARM:
1313                 {
1314                         /* Hack -- negative hit/damage bonuses */
1315                         if (o_ptr->to_h + o_ptr->to_d < 0) return (0L);
1316
1317                         /* Factor in the bonuses */
1318                         value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 100L);
1319
1320                         /* Hack -- Factor in extra damage dice and sides */
1321                         value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 250L;
1322                         value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 250L;
1323
1324                         break;
1325                 }
1326
1327                 /* Ammo */
1328                 case TV_SHOT:
1329                 case TV_ARROW:
1330                 case TV_BOLT:
1331                 {
1332                         /* Hack -- negative hit/damage bonuses */
1333                         if (o_ptr->to_h + o_ptr->to_d < 0) return (0L);
1334
1335                         /* Factor in the bonuses */
1336                         value += ((o_ptr->to_h + o_ptr->to_d) * 5L);
1337
1338                         /* Hack -- Factor in extra damage dice and sides */
1339                         value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 5L;
1340                         value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 5L;
1341
1342                         break;
1343                 }
1344
1345                 /* Figurines, relative to monster level */
1346                 case TV_FIGURINE:
1347                 {
1348                         DEPTH level = r_info[o_ptr->pval].level;
1349                         if (level < 20) value = level*50L;
1350                         else if (level < 30) value = 1000+(level-20)*150L;
1351                         else if (level < 40) value = 2500+(level-30)*350L;
1352                         else if (level < 50) value = 6000+(level-40)*800L;
1353                         else value = 14000+(level-50)*2000L;
1354                         break;
1355                 }
1356
1357                 case TV_CAPTURE:
1358                 {
1359                         if (!o_ptr->pval) value = 1000L;
1360                         else value = ((r_info[o_ptr->pval].level) * 50L + 1000);
1361                         break;
1362                 }
1363
1364                 case TV_CHEST:
1365                 {
1366                         if (!o_ptr->pval) value = 0L;
1367                         break;
1368                 }
1369         }
1370
1371         /* Worthless object */
1372         if (value < 0) return 0L;
1373
1374         /* Return the value */
1375         return (value);
1376 }
1377
1378
1379 /*!
1380  * @brief オブジェクト価格算出のメインルーチン /
1381  * Return the price of an item including plusses (and charges)
1382  * @param o_ptr 判明している現価格を確認したいオブジェクトの構造体参照ポインタ
1383  * @return オブジェクトの判明している現価格
1384  * @details
1385  * This function returns the "value" of the given item (qty one)\n
1386  *\n
1387  * Never notice "unknown" bonuses or properties, including "curses",\n
1388  * since that would give the player information he did not have.\n
1389  *\n
1390  * Note that discounted items stay discounted forever, even if\n
1391  * the discount is "forgotten" by the player via memory loss.\n
1392  */
1393 PRICE object_value(object_type *o_ptr)
1394 {
1395         PRICE value;
1396
1397         /* Unknown items -- acquire a base value */
1398         if (object_is_known(o_ptr))
1399         {
1400                 /* Broken items -- worthless */
1401                 if (object_is_broken(o_ptr)) return (0L);
1402
1403                 /* Cursed items -- worthless */
1404                 if (object_is_cursed(o_ptr)) return (0L);
1405
1406                 /* Real value (see above) */
1407                 value = object_value_real(o_ptr);
1408         }
1409
1410         /* Known items -- acquire the actual value */
1411         else
1412         {
1413                 /* Hack -- Felt broken items */
1414                 if ((o_ptr->ident & (IDENT_SENSE)) && object_is_broken(o_ptr)) return (0L);
1415
1416                 /* Hack -- Felt cursed items */
1417                 if ((o_ptr->ident & (IDENT_SENSE)) && object_is_cursed(o_ptr)) return (0L);
1418
1419                 /* Base value (see above) */
1420                 value = object_value_base(o_ptr);
1421         }
1422
1423         /* Apply discount (if any) */
1424         if (o_ptr->discount) value -= (value * o_ptr->discount / 100L);
1425
1426         /* Return the final value */
1427         return (value);
1428 }
1429
1430
1431
1432
1433 /*!
1434  * @brief 魔法棒やロッドのスロット分割時に使用回数を分配する /
1435  * Distribute charges of rods or wands.
1436  * @param o_ptr 分割元オブジェクトの構造体参照ポインタ source item
1437  * @param q_ptr 分割先オブジェクトの構造体参照ポインタ target item, must be of the same type as o_ptr
1438  * @param amt 分割したい回数量 number of items that are transfered
1439  * @return なし
1440  * @details
1441  * Hack -- If rods or wands are dropped, the total maximum timeout or\n
1442  * charges need to be allocated between the two stacks.  If all the items\n
1443  * are being dropped, it makes for a neater message to leave the original\n
1444  * stack's pval alone. -LM-\n
1445  */
1446 void distribute_charges(object_type *o_ptr, object_type *q_ptr, int amt)
1447 {
1448         if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_ROD))
1449         {
1450                 q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
1451                 if (amt < o_ptr->number) o_ptr->pval -= q_ptr->pval;
1452
1453                 /* Hack -- Rods also need to have their timeouts distributed.  The
1454                  * dropped stack will accept all time remaining to charge up to its
1455                  * maximum.
1456                  */
1457                 if ((o_ptr->tval == TV_ROD) && (o_ptr->timeout))
1458                 {
1459                         if (q_ptr->pval > o_ptr->timeout)
1460                                 q_ptr->timeout = o_ptr->timeout;
1461                         else
1462                                 q_ptr->timeout = q_ptr->pval;
1463
1464                         if (amt < o_ptr->number) o_ptr->timeout -= q_ptr->timeout;
1465                 }
1466         }
1467 }
1468
1469 /*!
1470  * @brief 魔法棒やロッドの使用回数を減らす /
1471  * @param o_ptr オブジェクトの構造体参照ポインタ source item
1472  * @param amt 減らしたい回数量 number of items that are transfered
1473  * @return なし
1474  * @details
1475  * Hack -- If rods or wand are destroyed, the total maximum timeout or\n
1476  * charges of the stack needs to be reduced, unless all the items are\n
1477  * being destroyed. -LM-\n
1478  */
1479 void reduce_charges(object_type *o_ptr, int amt)
1480 {
1481         if (((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_ROD)) &&
1482                 (amt < o_ptr->number))
1483         {
1484                 o_ptr->pval -= o_ptr->pval * amt / o_ptr->number;
1485         }
1486 }
1487
1488 /*
1489  * Determine if an item can "absorb" a second item
1490  *
1491  * See "object_absorb()" for the actual "absorption" code.
1492  *
1493  * If permitted, we allow staffs (if they are known to have equal charges
1494  * and both are either known or confirmed empty) and wands (if both are
1495  * either known or confirmed empty) and rods (in all cases) to combine.
1496  * Staffs will unstack (if necessary) when they are used, but wands and
1497  * rods will only unstack if one is dropped. -LM-
1498  *
1499  * If permitted, we allow weapons/armor to stack, if fully "known".
1500  *
1501  * Missiles will combine if both stacks have the same "known" status.
1502  * This is done to make unidentified stacks of missiles useful.
1503  *
1504  * Food, potions, scrolls, and "easy know" items always stack.
1505  *
1506  * Chests, and activatable items, never stack (for various reasons).
1507  */
1508
1509 /*
1510  * A "stack" of items is limited to less than or equal to 99 items (hard-coded).
1511  */
1512 #define MAX_STACK_SIZE 99
1513
1514
1515 /*!
1516  * @brief 両オブジェクトをスロットに重ね合わせ可能な最大数を返す。
1517  * Determine if an item can partly absorb a second item. Return maximum number of stack.
1518  * @param o_ptr 検証したいオブジェクトの構造体参照ポインタ1
1519  * @param j_ptr 検証したいオブジェクトの構造体参照ポインタ2
1520  * @return 重ね合わせ可能なアイテム数
1521  */
1522 int object_similar_part(object_type *o_ptr, object_type *j_ptr)
1523 {
1524         int i;
1525
1526         /* Default maximum number of stack */
1527         int max_num = MAX_STACK_SIZE;
1528
1529         /* Require identical object types */
1530         if (o_ptr->k_idx != j_ptr->k_idx) return 0;
1531
1532
1533         /* Analyze the items */
1534         switch (o_ptr->tval)
1535         {
1536                 /* Chests and Statues*/
1537                 case TV_CHEST:
1538                 case TV_CARD:
1539                 case TV_CAPTURE:
1540                 {
1541                         /* Never okay */
1542                         return 0;
1543                 }
1544
1545                 case TV_STATUE:
1546                 {
1547                         if ((o_ptr->sval != SV_PHOTO) || (j_ptr->sval != SV_PHOTO)) return 0;
1548                         if (o_ptr->pval != j_ptr->pval) return 0;
1549                         break;
1550                 }
1551
1552                 /* Figurines and Corpses*/
1553                 case TV_FIGURINE:
1554                 case TV_CORPSE:
1555                 {
1556                         /* Same monster */
1557                         if (o_ptr->pval != j_ptr->pval) return 0;
1558
1559                         /* Assume okay */
1560                         break;
1561                 }
1562
1563                 /* Food and Potions and Scrolls */
1564                 case TV_FOOD:
1565                 case TV_POTION:
1566                 case TV_SCROLL:
1567                 {
1568                         /* Assume okay */
1569                         break;
1570                 }
1571
1572                 /* Staffs */
1573                 case TV_STAFF:
1574                 {
1575                         /* Require either knowledge or known empty for both staffs. */
1576                         if ((!(o_ptr->ident & (IDENT_EMPTY)) &&
1577                                 !object_is_known(o_ptr)) ||
1578                                 (!(j_ptr->ident & (IDENT_EMPTY)) &&
1579                                 !object_is_known(j_ptr))) return 0;
1580
1581                         /* Require identical charges, since staffs are bulky. */
1582                         if (o_ptr->pval != j_ptr->pval) return 0;
1583
1584                         /* Assume okay */
1585                         break;
1586                 }
1587
1588                 /* Wands */
1589                 case TV_WAND:
1590                 {
1591                         /* Require either knowledge or known empty for both wands. */
1592                         if ((!(o_ptr->ident & (IDENT_EMPTY)) &&
1593                                 !object_is_known(o_ptr)) ||
1594                                 (!(j_ptr->ident & (IDENT_EMPTY)) &&
1595                                 !object_is_known(j_ptr))) return 0;
1596
1597                         /* Wand charges combine in O&ZAngband.  */
1598
1599                         /* Assume okay */
1600                         break;
1601                 }
1602
1603                 /* Staffs and Wands and Rods */
1604                 case TV_ROD:
1605                 {
1606                         /* Prevent overflaw of timeout */
1607                         max_num = MIN(max_num, MAX_SHORT / k_info[o_ptr->k_idx].pval);
1608
1609                         /* Assume okay */
1610                         break;
1611                 }
1612
1613                 /* Weapons and Armor */
1614                 case TV_BOW:
1615                 case TV_DIGGING:
1616                 case TV_HAFTED:
1617                 case TV_POLEARM:
1618                 case TV_SWORD:
1619                 case TV_BOOTS:
1620                 case TV_GLOVES:
1621                 case TV_HELM:
1622                 case TV_CROWN:
1623                 case TV_SHIELD:
1624                 case TV_CLOAK:
1625                 case TV_SOFT_ARMOR:
1626                 case TV_HARD_ARMOR:
1627                 case TV_DRAG_ARMOR:
1628
1629                 /* Rings, Amulets, Lites */
1630                 case TV_RING:
1631                 case TV_AMULET:
1632                 case TV_LITE:
1633                 case TV_WHISTLE:
1634                 {
1635                         /* Require full knowledge of both items */
1636                         if (!object_is_known(o_ptr) || !object_is_known(j_ptr)) return 0;
1637
1638                         /* Fall through */
1639                 }
1640
1641                 /* Missiles */
1642                 case TV_BOLT:
1643                 case TV_ARROW:
1644                 case TV_SHOT:
1645                 {
1646                         /* Require identical knowledge of both items */
1647                         if (object_is_known(o_ptr) != object_is_known(j_ptr)) return 0;
1648                         if (o_ptr->feeling != j_ptr->feeling) return 0;
1649
1650                         /* Require identical "bonuses" */
1651                         if (o_ptr->to_h != j_ptr->to_h) return 0;
1652                         if (o_ptr->to_d != j_ptr->to_d) return 0;
1653                         if (o_ptr->to_a != j_ptr->to_a) return 0;
1654
1655                         /* Require identical "pval" code */
1656                         if (o_ptr->pval != j_ptr->pval) return 0;
1657
1658                         /* Artifacts never stack */
1659                         if (object_is_artifact(o_ptr) || object_is_artifact(j_ptr)) return 0;
1660
1661                         /* Require identical "ego-item" names */
1662                         if (o_ptr->name2 != j_ptr->name2) return 0;
1663
1664                         /* Require identical added essence  */
1665                         if (o_ptr->xtra3 != j_ptr->xtra3) return 0;
1666                         if (o_ptr->xtra4 != j_ptr->xtra4) return 0;
1667
1668                         /* Hack -- Never stack "powerful" items */
1669                         if (o_ptr->xtra1 || j_ptr->xtra1) return 0;
1670
1671                         /* Hack -- Never stack recharging items */
1672                         if (o_ptr->timeout || j_ptr->timeout) return 0;
1673
1674                         /* Require identical "values" */
1675                         if (o_ptr->ac != j_ptr->ac) return 0;
1676                         if (o_ptr->dd != j_ptr->dd) return 0;
1677                         if (o_ptr->ds != j_ptr->ds) return 0;
1678
1679                         /* Probably okay */
1680                         break;
1681                 }
1682
1683                 /* Various */
1684                 default:
1685                 {
1686                         /* Require knowledge */
1687                         if (!object_is_known(o_ptr) || !object_is_known(j_ptr)) return 0;
1688
1689                         /* Probably okay */
1690                         break;
1691                 }
1692         }
1693
1694
1695         /* Hack -- Identical art_flags! */
1696         for (i = 0; i < TR_FLAG_SIZE; i++)
1697                 if (o_ptr->art_flags[i] != j_ptr->art_flags[i]) return 0;
1698
1699         /* Hack -- Require identical "cursed" status */
1700         if (o_ptr->curse_flags != j_ptr->curse_flags) return 0;
1701
1702         /* Hack -- Require identical "broken" status */
1703         if ((o_ptr->ident & (IDENT_BROKEN)) != (j_ptr->ident & (IDENT_BROKEN))) return 0;
1704
1705
1706         /* Hack -- require semi-matching "inscriptions" */
1707         if (o_ptr->inscription && j_ptr->inscription &&
1708             (o_ptr->inscription != j_ptr->inscription))
1709                 return 0;
1710
1711         /* Hack -- normally require matching "inscriptions" */
1712         if (!stack_force_notes && (o_ptr->inscription != j_ptr->inscription)) return 0;
1713
1714         /* Hack -- normally require matching "discounts" */
1715         if (!stack_force_costs && (o_ptr->discount != j_ptr->discount)) return 0;
1716
1717
1718         /* They match, so they must be similar */
1719         return max_num;
1720 }
1721
1722 /*!
1723  * @brief 両オブジェクトをスロットに重ねることができるかどうかを返す。
1724  * Determine if an item can absorb a second item.
1725  * @param o_ptr 検証したいオブジェクトの構造体参照ポインタ1
1726  * @param j_ptr 検証したいオブジェクトの構造体参照ポインタ2
1727  * @return 重ね合わせ可能ならばTRUEを返す。
1728  */
1729 bool object_similar(object_type *o_ptr, object_type *j_ptr)
1730 {
1731         int total = o_ptr->number + j_ptr->number;
1732         int max_num;
1733
1734         /* Are these objects similar? */
1735         max_num = object_similar_part(o_ptr, j_ptr);
1736
1737         /* Return if not similar */
1738         if (!max_num) return FALSE;
1739
1740         /* Maximal "stacking" limit */
1741         if (total > max_num) return (0);
1742
1743
1744         /* They match, so they must be similar */
1745         return (TRUE);
1746 }
1747
1748
1749 /*!
1750  * @brief 両オブジェクトをスロットに重ね合わせる。
1751  * Allow one item to "absorb" another, assuming they are similar
1752  * @param o_ptr 重ね合わせ先のオブジェクトの構造体参照ポインタ
1753  * @param j_ptr 重ね合わせ元のオブジェクトの構造体参照ポインタ
1754  * @return なし
1755  */
1756 void object_absorb(object_type *o_ptr, object_type *j_ptr)
1757 {
1758         int max_num = object_similar_part(o_ptr, j_ptr);
1759         int total = o_ptr->number + j_ptr->number;
1760         int diff = (total > max_num) ? total - max_num : 0;
1761
1762         /* Combine quantity, lose excess items */
1763         o_ptr->number = (total > max_num) ? max_num : total;
1764
1765         /* Hack -- blend "known" status */
1766         if (object_is_known(j_ptr)) object_known(o_ptr);
1767
1768         /* Hack -- clear "storebought" if only one has it */
1769         if (((o_ptr->ident & IDENT_STORE) || (j_ptr->ident & IDENT_STORE)) &&
1770             (!((o_ptr->ident & IDENT_STORE) && (j_ptr->ident & IDENT_STORE))))
1771         {
1772                 if (j_ptr->ident & IDENT_STORE) j_ptr->ident &= 0xEF;
1773                 if (o_ptr->ident & IDENT_STORE) o_ptr->ident &= 0xEF;
1774         }
1775
1776         /* Hack -- blend "mental" status */
1777         if (j_ptr->ident & (IDENT_MENTAL)) o_ptr->ident |= (IDENT_MENTAL);
1778
1779         /* Hack -- blend "inscriptions" */
1780         if (j_ptr->inscription) o_ptr->inscription = j_ptr->inscription;
1781
1782         /* Hack -- blend "feelings" */
1783         if (j_ptr->feeling) o_ptr->feeling = j_ptr->feeling;
1784
1785         /* Hack -- could average discounts */
1786         /* Hack -- save largest discount */
1787         if (o_ptr->discount < j_ptr->discount) o_ptr->discount = j_ptr->discount;
1788
1789         /* Hack -- if rods are stacking, add the pvals (maximum timeouts) and current timeouts together. -LM- */
1790         if (o_ptr->tval == TV_ROD)
1791         {
1792                 o_ptr->pval += j_ptr->pval * (j_ptr->number - diff) / j_ptr->number;
1793                 o_ptr->timeout += j_ptr->timeout * (j_ptr->number - diff) / j_ptr->number;
1794         }
1795
1796         /* Hack -- if wands are stacking, combine the charges. -LM- */
1797         if (o_ptr->tval == TV_WAND)
1798         {
1799                 o_ptr->pval += j_ptr->pval * (j_ptr->number - diff) / j_ptr->number;
1800         }
1801 }
1802
1803
1804 /*!
1805  * @brief tvalとsvalに対応するベースアイテムのIDを返す。
1806  * Find the index of the object_kind with the given tval and sval
1807  * @param tval 検索したいベースアイテムのtval
1808  * @param sval 検索したいベースアイテムのsval
1809  * @return なし
1810  */
1811 KIND_OBJECT_IDX lookup_kind(OBJECT_TYPE_VALUE tval, OBJECT_SUBTYPE_VALUE sval)
1812 {
1813         KIND_OBJECT_IDX k;
1814         int num = 0;
1815         KIND_OBJECT_IDX bk = 0;
1816
1817         /* Look for it */
1818         for (k = 1; k < max_k_idx; k++)
1819         {
1820                 object_kind *k_ptr = &k_info[k];
1821
1822                 /* Require correct tval */
1823                 if (k_ptr->tval != tval) continue;
1824
1825                 /* Found a match */
1826                 if (k_ptr->sval == sval) return (k);
1827
1828                 /* Ignore illegal items */
1829                 if (sval != SV_ANY) continue;
1830
1831                 /* Apply the randomizer */
1832                 if (!one_in_(++num)) continue;
1833
1834                 /* Use this value */
1835                 bk = k;
1836         }
1837
1838         /* Return this choice */
1839         if (sval == SV_ANY)
1840         {
1841                 return bk;
1842         }
1843
1844 #if 0
1845         msg_format(_("アイテムがない (%d,%d)", "No object (%d,%d)"), tval, sval);
1846 #endif
1847
1848
1849         return (0);
1850 }
1851
1852
1853 /*!
1854  * @brief オブジェクトを初期化する
1855  * Wipe an object clean.
1856  * @param o_ptr 初期化したいオブジェクトの構造体参照ポインタ
1857  * @return なし
1858  */
1859 void object_wipe(object_type *o_ptr)
1860 {
1861         /* Wipe the structure */
1862         (void)WIPE(o_ptr, object_type);
1863 }
1864
1865
1866 /*!
1867  * @brief オブジェクトを複製する
1868  * Wipe an object clean.
1869  * @param o_ptr 複製元のオブジェクトの構造体参照ポインタ
1870  * @param j_ptr 複製先のオブジェクトの構造体参照ポインタ
1871  * @return なし
1872  */
1873 void object_copy(object_type *o_ptr, object_type *j_ptr)
1874 {
1875         /* Copy the structure */
1876         (void)COPY(o_ptr, j_ptr, object_type);
1877 }
1878
1879
1880 /*!
1881  * @brief オブジェクト構造体にベースアイテムを作成する
1882  * Prepare an object based on an object kind.
1883  * @param o_ptr 代入したいオブジェクトの構造体参照ポインタ
1884  * @param k_idx 新たに作成したいベースアイテム情報のID
1885  * @return なし
1886  */
1887 void object_prep(object_type *o_ptr, KIND_OBJECT_IDX k_idx)
1888 {
1889         object_kind *k_ptr = &k_info[k_idx];
1890
1891         /* Clear the record */
1892         object_wipe(o_ptr);
1893
1894         /* Save the kind index */
1895         o_ptr->k_idx = k_idx;
1896
1897         /* Efficiency -- tval/sval */
1898         o_ptr->tval = k_ptr->tval;
1899         o_ptr->sval = k_ptr->sval;
1900
1901         /* Default "pval" */
1902         o_ptr->pval = k_ptr->pval;
1903
1904         /* Default number */
1905         o_ptr->number = 1;
1906
1907         /* Default weight */
1908         o_ptr->weight = k_ptr->weight;
1909
1910         /* Default magic */
1911         o_ptr->to_h = k_ptr->to_h;
1912         o_ptr->to_d = k_ptr->to_d;
1913         o_ptr->to_a = k_ptr->to_a;
1914
1915         /* Default power */
1916         o_ptr->ac = k_ptr->ac;
1917         o_ptr->dd = k_ptr->dd;
1918         o_ptr->ds = k_ptr->ds;
1919
1920         /* Default activation */
1921         if (k_ptr->act_idx > 0) o_ptr->xtra2 = (XTRA8)k_ptr->act_idx;
1922
1923         /* Hack -- worthless items are always "broken" */
1924         if (k_info[o_ptr->k_idx].cost <= 0) o_ptr->ident |= (IDENT_BROKEN);
1925
1926         /* Hack -- cursed items are always "cursed" */
1927         if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
1928         if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
1929         if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
1930         if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
1931         if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
1932         if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
1933 }
1934
1935
1936 /*!
1937  * @brief デバッグ時にアイテム生成情報をメッセージに出力する / Cheat -- describe a created object for the user
1938  * @param o_ptr デバッグ出力するオブジェクトの構造体参照ポインタ
1939  * @return なし
1940  */
1941 static void object_mention(object_type *o_ptr)
1942 {
1943         GAME_TEXT o_name[MAX_NLEN];
1944
1945         object_aware(o_ptr);
1946         object_known(o_ptr);
1947
1948         /* Mark the item as fully known */
1949         o_ptr->ident |= (IDENT_MENTAL);
1950         object_desc(o_name, o_ptr, 0);
1951         msg_format_wizard(CHEAT_OBJECT, _("%sを生成しました。", "%s was generated."), o_name);
1952 }
1953
1954
1955 /*!
1956  * @brief アイテムのエゴをレア度の重みに合わせてランダムに選択する
1957  * Choose random ego type
1958  * @param slot 取得したいエゴの装備部位
1959  * @param good TRUEならば通常のエゴ、FALSEならば呪いのエゴが選択対象となる。
1960  * @return 選択されたエゴ情報のID、万一選択できなかった場合はmax_e_idxが返る。
1961  */
1962 static byte get_random_ego(byte slot, bool good)
1963 {
1964         int i, value;
1965         ego_item_type *e_ptr;
1966
1967         long total = 0L;
1968         
1969         for (i = 1; i < max_e_idx; i++)
1970         {
1971                 e_ptr = &e_info[i];
1972                 
1973                 if (e_ptr->slot == slot
1974                     && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
1975                 {
1976                         if (e_ptr->rarity)
1977                                 total += (255 / e_ptr->rarity);
1978                 }
1979         }
1980
1981         value = randint1(total);
1982
1983         for (i = 1; i < max_e_idx; i++)
1984         {
1985                 e_ptr = &e_info[i];
1986                 
1987                 if (e_ptr->slot == slot
1988                     && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
1989                 {
1990                         if (e_ptr->rarity)
1991                                 value -= (255 / e_ptr->rarity);
1992                         if (value <= 0L) break;
1993                 }
1994         }
1995         return (byte)i;
1996 }
1997
1998
1999 /*!
2000  * @brief 武器系オブジェクトに生成ランクごとの強化を与えるサブルーチン
2001  * Apply magic to an item known to be a "weapon"
2002  * @param o_ptr 強化を与えたいオブジェクトの構造体参照ポインタ
2003  * @param level 生成基準階
2004  * @param power 生成ランク
2005  * @return なし
2006  * @details
2007  * Hack -- note special base damage dice boosting\n
2008  * Hack -- note special processing for weapon/digger\n
2009  */
2010 void apply_magic_weapon(object_type *o_ptr, DEPTH level, int power)
2011 {
2012         HIT_PROB tohit1 = randint1(5) + (HIT_PROB)m_bonus(5, level);
2013         HIT_POINT todam1 = randint1(5) + (HIT_POINT)m_bonus(5, level);
2014
2015         HIT_PROB tohit2 = (HIT_PROB)m_bonus(10, level);
2016         HIT_POINT todam2 = (HIT_POINT)m_bonus(10, level);
2017
2018         if ((o_ptr->tval == TV_BOLT) || (o_ptr->tval == TV_ARROW) || (o_ptr->tval == TV_SHOT))
2019         {
2020                 tohit2 = (tohit2 + 1) / 2;
2021                 todam2 = (todam2 + 1) / 2;
2022         }
2023
2024         /* Good */
2025         if (power > 0)
2026         {
2027                 /* Enchant */
2028                 o_ptr->to_h += tohit1;
2029                 o_ptr->to_d += todam1;
2030
2031                 /* Very good */
2032                 if (power > 1)
2033                 {
2034                         /* Enchant again */
2035                         o_ptr->to_h += tohit2;
2036                         o_ptr->to_d += todam2;
2037                 }
2038         }
2039
2040         /* Cursed */
2041         else if (power < 0)
2042         {
2043                 /* Penalize */
2044                 o_ptr->to_h -= tohit1;
2045                 o_ptr->to_d -= todam1;
2046
2047                 /* Very cursed */
2048                 if (power < -1)
2049                 {
2050                         /* Penalize again */
2051                         o_ptr->to_h -= tohit2;
2052                         o_ptr->to_d -= todam2;
2053                 }
2054
2055                 /* Cursed (if "bad") */
2056                 if (o_ptr->to_h + o_ptr->to_d < 0) o_ptr->curse_flags |= TRC_CURSED;
2057         }
2058
2059         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_DIAMOND_EDGE)) return;
2060
2061         switch (o_ptr->tval)
2062         {
2063                 case TV_DIGGING:
2064                 {
2065                         /* Very good */
2066                         if (power > 1)
2067                         {
2068                                 if (one_in_(30) || (power > 2)) /* power > 2 is debug only */
2069                                         create_artifact(o_ptr, FALSE);
2070                                 else
2071                                         /* Special Ego-item */
2072                                         o_ptr->name2 = EGO_DIGGING;
2073                         }
2074
2075                         /* Very bad */
2076                         else if (power < -1)
2077                         {
2078                                 /* Hack -- Horrible digging bonus */
2079                                 o_ptr->pval = 0 - (5 + randint1(5));
2080                         }
2081
2082                         /* Bad */
2083                         else if (power < 0)
2084                         {
2085                                 /* Hack -- Reverse digging bonus */
2086                                 o_ptr->pval = 0 - (o_ptr->pval);
2087                         }
2088
2089                         break;
2090                 }
2091
2092                 case TV_HAFTED:
2093                 case TV_POLEARM:
2094                 case TV_SWORD:
2095                 {
2096                         /* Very Good */
2097                         if (power > 1)
2098                         {
2099                                 if (one_in_(40) || (power > 2)) /* power > 2 is debug only */
2100                                 {
2101                                         create_artifact(o_ptr, FALSE);
2102                                         break;
2103                                 }
2104                                 while (1)
2105                                 {
2106                                         /* Roll for an ego-item */
2107                                         o_ptr->name2 = get_random_ego(INVEN_RARM, TRUE);
2108                                         if (o_ptr->name2 == EGO_SHARPNESS && o_ptr->tval != TV_SWORD)
2109                                                 continue;
2110                                         if (o_ptr->name2 == EGO_EARTHQUAKES && o_ptr->tval != TV_HAFTED)
2111                                                 continue;
2112                                         if (o_ptr->name2 == EGO_WEIRD && o_ptr->tval != TV_SWORD)
2113                                                 continue;
2114                                         break;
2115                                 }
2116
2117                                 switch (o_ptr->name2)
2118                                 {
2119                                 case EGO_HA:
2120                                         if (one_in_(4) && (level > 40))
2121                                                 add_flag(o_ptr->art_flags, TR_BLOWS);
2122                                         break;
2123                                 case EGO_DF:
2124                                         if (one_in_(3))
2125                                                 add_flag(o_ptr->art_flags, TR_RES_POIS);
2126                                         if (one_in_(3))
2127                                                 add_flag(o_ptr->art_flags, TR_WARNING);
2128                                         break;
2129                                 case EGO_KILL_DRAGON:
2130                                         if (one_in_(3))
2131                                                 add_flag(o_ptr->art_flags, TR_RES_POIS);
2132                                         break;
2133                                 case EGO_WEST:
2134                                         if (one_in_(3))
2135                                                 add_flag(o_ptr->art_flags, TR_RES_FEAR);
2136                                         break;
2137                                 case EGO_SLAYING_WEAPON:
2138                                         if (one_in_(3)) /* double damage */
2139                                                 o_ptr->dd *= 2;
2140                                         else
2141                                         {
2142                                                 do
2143                                                 {
2144                                                         o_ptr->dd++;
2145                                                 } while (one_in_(o_ptr->dd));
2146
2147                                                 do
2148                                                 {
2149                                                         o_ptr->ds++;
2150                                                 } while (one_in_(o_ptr->ds));
2151                                         }
2152
2153                                         if (one_in_(5))
2154                                         {
2155                                                 add_flag(o_ptr->art_flags, TR_BRAND_POIS);
2156                                         }
2157                                         if (o_ptr->tval == TV_SWORD && one_in_(3))
2158                                         {
2159                                                 add_flag(o_ptr->art_flags, TR_VORPAL);
2160                                         }
2161                                         break;
2162                                 case EGO_TRUMP:
2163                                         if (one_in_(5))
2164                                                 add_flag(o_ptr->art_flags, TR_SLAY_DEMON);
2165                                         if (one_in_(7))
2166                                                 one_ability(o_ptr);
2167                                         break;
2168                                 case EGO_PATTERN:
2169                                         if (one_in_(3))
2170                                                 add_flag(o_ptr->art_flags, TR_HOLD_EXP);
2171                                         if (one_in_(3))
2172                                                 add_flag(o_ptr->art_flags, TR_DEX);
2173                                         if (one_in_(5))
2174                                                 add_flag(o_ptr->art_flags, TR_RES_FEAR);
2175                                         break;
2176                                 case EGO_SHARPNESS:
2177                                         o_ptr->pval = (PARAMETER_VALUE)m_bonus(5, level) + 1;
2178                                         break;
2179                                 case EGO_EARTHQUAKES:
2180                                         if (one_in_(3) && (level > 60))
2181                                                 add_flag(o_ptr->art_flags, TR_BLOWS);
2182                                         else
2183                                                 o_ptr->pval = (PARAMETER_VALUE)m_bonus(3, level);
2184                                         break;
2185                                 case EGO_VAMPIRIC:
2186                                         if (one_in_(5))
2187                                                 add_flag(o_ptr->art_flags, TR_SLAY_HUMAN);
2188                                         break;
2189                                 case EGO_DEMON:
2190
2191                                         if (one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2192                                         one_in_(3) ?
2193                                                 add_flag(o_ptr->art_flags, TR_DRAIN_EXP) :
2194                                                 one_in_(2) ?
2195                                                 add_flag(o_ptr->art_flags, TR_DRAIN_HP) :
2196                                                 add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2197
2198
2199                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_CHAOTIC);
2200                                         if (one_in_(4)) add_flag(o_ptr->art_flags, TR_BLOWS);
2201                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2202                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_CALL_DEMON);
2203                                         break;
2204                                 }
2205
2206                                 if (!o_ptr->art_name)
2207                                 {
2208                                         /* Hack -- Super-charge the damage dice */
2209                                         while (one_in_(10L * o_ptr->dd * o_ptr->ds)) o_ptr->dd++;
2210
2211                                         /* Hack -- Lower the damage dice */
2212                                         if (o_ptr->dd > 9) o_ptr->dd = 9;
2213                                 }
2214                         }
2215
2216                         /* Very cursed */
2217                         else if (power < -1)
2218                         {
2219                                 /* Roll for ego-item */
2220                                 if (randint0(MAX_DEPTH) < level)
2221                                 {
2222                                         while (1)
2223                                         {
2224                                                 o_ptr->name2 = get_random_ego(INVEN_RARM, FALSE);
2225                                                 if (o_ptr->name2 == EGO_WEIRD && o_ptr->tval != TV_SWORD)
2226                                                 {
2227                                                         continue;
2228                                                 }
2229                                                 break;
2230                                         }
2231                                         switch (o_ptr->name2)
2232                                         {
2233                                         case EGO_MORGUL:
2234                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2235                                                 if (one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2236                                         case EGO_WEIRD:
2237                                                 if (one_in_(4)) add_flag(o_ptr->art_flags, TR_BRAND_POIS);
2238                                                 if (one_in_(4)) add_flag(o_ptr->art_flags, TR_RES_NETHER);
2239                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_NO_MAGIC);
2240                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_NO_TELE);
2241                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2242                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2243                                         }
2244                                 }
2245                         }
2246
2247                         break;
2248                 }
2249
2250
2251                 case TV_BOW:
2252                 {
2253                         /* Very good */
2254                         if (power > 1)
2255                         {
2256                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2257                                 {
2258                                         create_artifact(o_ptr, FALSE);
2259                                         break;
2260                                 }
2261                                 o_ptr->name2 = get_random_ego(INVEN_BOW, TRUE);
2262                         }
2263
2264                         break;
2265                 }
2266
2267
2268                 case TV_BOLT:
2269                 case TV_ARROW:
2270                 case TV_SHOT:
2271                 {
2272                         /* Very good */
2273                         if (power > 1)
2274                         {
2275                                 if (power > 2) /* power > 2 is debug only */
2276                                 {
2277                                         create_artifact(o_ptr, FALSE);
2278                                         break;
2279                                 }
2280
2281                                 o_ptr->name2 = get_random_ego(INVEN_AMMO, TRUE);
2282
2283                                 switch (o_ptr->name2)
2284                                 {
2285                                 case EGO_SLAYING_BOLT:
2286                                         o_ptr->dd++;
2287                                         break;
2288                                 }
2289
2290                                 /* Hack -- super-charge the damage dice */
2291                                 while (one_in_(10L * o_ptr->dd * o_ptr->ds)) o_ptr->dd++;
2292
2293                                 /* Hack -- restrict the damage dice */
2294                                 if (o_ptr->dd > 9) o_ptr->dd = 9;
2295                         }
2296
2297                         /* Very cursed */
2298                         else if (power < -1)
2299                         {
2300                                 /* Roll for ego-item */
2301                                 if (randint0(MAX_DEPTH) < level)
2302                                 {
2303                                         o_ptr->name2 = get_random_ego(INVEN_AMMO, FALSE);
2304                                 }
2305                         }
2306
2307                         break;
2308                 }
2309         }
2310 }
2311
2312
2313 /*!
2314  * @brief ドラゴン装備にランダムな耐性を与える
2315  * @param o_ptr 強化を与えたいオブジェクトの構造体参照ポインタ
2316  * @return なし
2317  */
2318 static void dragon_resist(object_type * o_ptr)
2319 {
2320         do
2321         {
2322                 if (one_in_(4))
2323                         one_dragon_ele_resistance(o_ptr);
2324                 else
2325                         one_high_resistance(o_ptr);
2326         }
2327         while (one_in_(2));
2328 }
2329
2330
2331 /*!
2332  * @brief 防具系オブジェクトに生成ランクごとの強化を与えるサブルーチン
2333  * Apply magic to an item known to be "armor"
2334  * @param o_ptr 強化を与えたいオブジェクトの構造体参照ポインタ
2335  * @param level 生成基準階
2336  * @param power 生成ランク
2337  * @return なし
2338  * @details
2339  * Hack -- note special processing for crown/helm\n
2340  * Hack -- note special processing for robe of permanence\n
2341  */
2342 static void a_m_aux_2(object_type *o_ptr, DEPTH level, int power)
2343 {
2344         ARMOUR_CLASS toac1 = (ARMOUR_CLASS)randint1(5) + m_bonus(5, level);
2345         ARMOUR_CLASS toac2 = (ARMOUR_CLASS)m_bonus(10, level);
2346
2347         /* Good */
2348         if (power > 0)
2349         {
2350                 /* Enchant */
2351                 o_ptr->to_a += toac1;
2352
2353                 /* Very good */
2354                 if (power > 1)
2355                 {
2356                         /* Enchant again */
2357                         o_ptr->to_a += toac2;
2358                 }
2359         }
2360
2361         /* Cursed */
2362         else if (power < 0)
2363         {
2364                 /* Penalize */
2365                 o_ptr->to_a -= toac1;
2366
2367                 /* Very cursed */
2368                 if (power < -1)
2369                 {
2370                         /* Penalize again */
2371                         o_ptr->to_a -= toac2;
2372                 }
2373
2374                 /* Cursed (if "bad") */
2375                 if (o_ptr->to_a < 0) o_ptr->curse_flags |= TRC_CURSED;
2376         }
2377
2378         switch (o_ptr->tval)
2379         {
2380                 case TV_DRAG_ARMOR:
2381                 {
2382                         if (one_in_(50) || (power > 2)) /* power > 2 is debug only */
2383                                 create_artifact(o_ptr, FALSE);
2384                         break;
2385                 }
2386
2387                 case TV_HARD_ARMOR:
2388                 case TV_SOFT_ARMOR:
2389                 {
2390                         /* Very good */
2391                         if (power > 1)
2392                         {
2393                                 /* Hack -- Try for "Robes of the Magi" */
2394                                 if ((o_ptr->tval == TV_SOFT_ARMOR) &&
2395                                     (o_ptr->sval == SV_ROBE) &&
2396                                     (randint0(100) < 15))
2397                                 {
2398                                         if (one_in_(5))
2399                                         {
2400                                                 o_ptr->name2 = EGO_YOIYAMI;
2401                                                 o_ptr->k_idx = lookup_kind(TV_SOFT_ARMOR, SV_YOIYAMI_ROBE);
2402                                                 o_ptr->sval = SV_YOIYAMI_ROBE;
2403                                                 o_ptr->ac = 0;
2404                                                 o_ptr->to_a = 0;
2405                                         }
2406                                         else
2407                                         {
2408                                                 o_ptr->name2 = EGO_PERMANENCE;
2409                                         }
2410                                         break;
2411                                 }
2412
2413                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2414                                 {
2415                                         create_artifact(o_ptr, FALSE);
2416                                         break;
2417                                 }
2418
2419                                 while (1)
2420                                 {
2421                                         bool okay_flag = TRUE;
2422
2423                                         o_ptr->name2 = get_random_ego(INVEN_BODY, TRUE);
2424
2425                                         switch (o_ptr->name2)
2426                                         {
2427                                                 case EGO_DWARVEN:
2428                                                         if (o_ptr->tval != TV_HARD_ARMOR)
2429                                                         {
2430                                                                 okay_flag = FALSE;
2431                                                         }
2432                                                 break;
2433                                                 case EGO_DRUID:
2434                                                         if (o_ptr->tval != TV_SOFT_ARMOR)
2435                                                         {
2436                                                                 okay_flag = FALSE;
2437                                                         }
2438                                                 break;
2439                                                 default:
2440                                                 break;
2441                                         }
2442
2443                                         if (okay_flag) break;
2444                                 }
2445                                 switch (o_ptr->name2)
2446                                 {
2447                                   case EGO_RESISTANCE:
2448                                         if (one_in_(4))
2449                                                 add_flag(o_ptr->art_flags, TR_RES_POIS);
2450                                                 break;
2451                                   case EGO_DWARVEN:
2452                                         o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
2453                                         o_ptr->ac = k_info[o_ptr->k_idx].ac + 5;
2454                                         break;
2455                                         
2456                                   case EGO_A_DEMON:
2457                                         if(one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2458                                         one_in_(3) ? 
2459                                                 add_flag(o_ptr->art_flags, TR_DRAIN_EXP) :
2460                                                 one_in_(2) ?
2461                                                         add_flag(o_ptr->art_flags, TR_DRAIN_HP) :
2462                                                         add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2463                                                 
2464                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_AGGRAVATE);
2465                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_ADD_L_CURSE);
2466                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2467                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_HP);
2468                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2469                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
2470                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2471                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_CALL_DEMON);
2472                                         break;
2473                                   case EGO_A_MORGUL:
2474                                         if (one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2475                                         if (one_in_(9)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2476                                         if (one_in_(4)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2477                                         if (one_in_(6)) add_flag(o_ptr->art_flags, TR_AGGRAVATE);
2478                                         if (one_in_(9)) add_flag(o_ptr->art_flags, TR_NO_MAGIC);
2479                                         if (one_in_(9)) add_flag(o_ptr->art_flags, TR_NO_TELE);
2480                                         break;
2481                                   default:
2482                                         break;
2483                                 }
2484                         }
2485
2486                         break;
2487                 }
2488
2489                 case TV_SHIELD:
2490                 {
2491
2492                         if (o_ptr->sval == SV_DRAGON_SHIELD)
2493                         {
2494                                 dragon_resist(o_ptr);
2495                                 if (!one_in_(3)) break;
2496                         }
2497
2498                         /* Very good */
2499                         if (power > 1)
2500                         {
2501                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2502                                 {
2503                                         create_artifact(o_ptr, FALSE);
2504                                         break;
2505                                 }
2506                                 
2507                                 while(1)
2508                                 {
2509                                         o_ptr->name2 = get_random_ego(INVEN_LARM, TRUE);
2510                                         if (o_ptr->sval != SV_SMALL_METAL_SHIELD && o_ptr->sval != SV_LARGE_METAL_SHIELD 
2511                                                                 && o_ptr->name2 == EGO_S_DWARVEN)
2512                                         {
2513                                                 continue;
2514                                         }
2515                                         break;
2516                                 }
2517                                 
2518                                 switch (o_ptr->name2)
2519                                 {
2520                                 case EGO_ENDURANCE:
2521                                         if (!one_in_(3)) one_high_resistance(o_ptr);
2522                                         if (one_in_(4)) add_flag(o_ptr->art_flags, TR_RES_POIS);
2523                                         break;
2524                                 case EGO_REFLECTION:
2525                                         if (o_ptr->sval == SV_MIRROR_SHIELD)
2526                                                 o_ptr->name2 = 0;
2527                                         break;
2528                                         
2529                                 case EGO_S_DWARVEN:
2530                                         o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
2531                                         o_ptr->ac = k_info[o_ptr->k_idx].ac + 3;
2532                                         break;
2533                                 }
2534                         }
2535                         break;
2536                 }
2537
2538                 case TV_GLOVES:
2539                 {
2540                         if (o_ptr->sval == SV_SET_OF_DRAGON_GLOVES)
2541                         {
2542                                 dragon_resist(o_ptr);
2543                                 if (!one_in_(3)) break;
2544                         }
2545                         if (power > 1)
2546                         {
2547                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2548                                 {
2549                                         create_artifact(o_ptr, FALSE);
2550                                         break;
2551                                 }
2552                                 o_ptr->name2 = get_random_ego(INVEN_HANDS, TRUE);
2553                         }
2554                         
2555                         /* Very cursed */
2556                         else if (power < -1)
2557                         {
2558                                 o_ptr->name2 = get_random_ego(INVEN_HANDS, FALSE);
2559                         }
2560
2561                         break;
2562                 }
2563
2564                 case TV_BOOTS:
2565                 {
2566                         if (o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE)
2567                         {
2568                                 dragon_resist(o_ptr);
2569                                 if (!one_in_(3)) break;
2570                         }
2571                         /* Very good */
2572                         if (power > 1)
2573                         {
2574                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2575                                 {
2576                                         create_artifact(o_ptr, FALSE);
2577                                         break;
2578                                 }
2579                                 o_ptr->name2 = get_random_ego(INVEN_FEET, TRUE);
2580
2581                                 switch (o_ptr->name2)
2582                                 {
2583                                 case EGO_SLOW_DESCENT:
2584                                         if (one_in_(2))
2585                                         {
2586                                                 one_high_resistance(o_ptr);
2587                                         }
2588                                         break;
2589                                 }
2590                         }
2591                         /* Very cursed */
2592                         else if (power < -1)
2593                         {
2594                                 o_ptr->name2 = get_random_ego(INVEN_FEET, FALSE);
2595                         }
2596
2597                         break;
2598                 }
2599
2600                 case TV_CROWN:
2601                 {
2602                         /* Very good */
2603                         if (power > 1)
2604                         {
2605                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2606                                 {
2607                                         create_artifact(o_ptr, FALSE);
2608                                         break;
2609                                 }
2610                                 while (1)
2611                                 {
2612                                         bool ok_flag = TRUE;
2613                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE);
2614
2615                                         switch (o_ptr->name2)
2616                                         {
2617                                         case EGO_TELEPATHY:
2618                                                 if (add_esp_strong(o_ptr)) add_esp_weak(o_ptr, TRUE);
2619                                                 else add_esp_weak(o_ptr, FALSE);
2620                                                 break;
2621                                         case EGO_MAGI:
2622                                         case EGO_MIGHT:
2623                                         case EGO_REGENERATION:
2624                                         case EGO_LORDLINESS:
2625                                         case EGO_BASILISK:
2626                                                 break;
2627                                         case EGO_SEEING:
2628                                                 if (one_in_(3))
2629                                                 {
2630                                                         if (one_in_(2)) add_esp_strong(o_ptr);
2631                                                         else add_esp_weak(o_ptr, FALSE);
2632                                                 }
2633                                                 break;
2634                                         default:/* not existing crown (wisdom,lite, etc...) */
2635                                                 ok_flag = FALSE;
2636                                         }
2637                                         if (ok_flag)
2638                                                 break; /* while (1) */
2639                                 }
2640                                 break;
2641                         }
2642
2643                         /* Very cursed */
2644                         else if (power < -1)
2645                         {       
2646                                 while (1)
2647                                 {
2648                                         bool ok_flag = TRUE;
2649                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE);
2650
2651                                         switch (o_ptr->name2)
2652                                         {
2653                                           case EGO_ANCIENT_CURSE:
2654                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_NO_MAGIC);
2655                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_NO_TELE);
2656                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2657                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
2658                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_HP);
2659                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2660                                                 break;
2661                                         }
2662                                         if (ok_flag)
2663                                                 break; /* while (1) */
2664                                 }
2665                         }
2666
2667                         break;
2668                 }
2669
2670                 case TV_HELM:
2671                 {
2672                         if (o_ptr->sval == SV_DRAGON_HELM)
2673                         {
2674                                 dragon_resist(o_ptr);
2675                                 if (!one_in_(3)) break;
2676                         }
2677
2678                         /* Very good */
2679                         if (power > 1)
2680                         {
2681                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2682                                 {
2683                                         create_artifact(o_ptr, FALSE);
2684                                         break;
2685                                 }
2686                                 while (1)
2687                                 {
2688                                         bool ok_flag = TRUE;
2689                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE);
2690
2691                                         switch (o_ptr->name2)
2692                                         {
2693                                         case EGO_BRILLIANCE:
2694                                         case EGO_DARK:
2695                                         case EGO_INFRAVISION:
2696                                         case EGO_H_PROTECTION:
2697                                                 break;
2698                                         case EGO_SEEING:
2699                                                 if (one_in_(7))
2700                                                 {
2701                                                         if (one_in_(2)) add_esp_strong(o_ptr);
2702                                                         else add_esp_weak(o_ptr, FALSE);
2703                                                 }
2704                                                 break;
2705                                         case EGO_LITE:
2706                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_LITE_1);
2707                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_LITE_2);
2708                                                 break;
2709                                         case EGO_H_DEMON:
2710                                                 if(one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2711                                                 one_in_(3) ? 
2712                                                         add_flag(o_ptr->art_flags, TR_DRAIN_EXP) :
2713                                                         one_in_(2) ?
2714                                                                 add_flag(o_ptr->art_flags, TR_DRAIN_HP) :
2715                                                                 add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2716                                                 
2717                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_AGGRAVATE);
2718                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_ADD_L_CURSE);
2719                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2720                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_HP);
2721                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2722                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
2723                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2724                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_CALL_DEMON);
2725                                                 break;
2726                                         default:/* not existing helm (Magi, Might, etc...)*/
2727                                                 ok_flag = FALSE;
2728                                         }
2729                                         if (ok_flag)
2730                                                 break; /* while (1) */
2731                                 }
2732                                 break;
2733                         }
2734                         /* Very cursed */
2735                         else if (power < -1)
2736                         {
2737                                 while (1)
2738                                 {
2739                                         bool ok_flag = TRUE;
2740                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE);
2741
2742                                         switch (o_ptr->name2)
2743                                         {
2744                                           case EGO_ANCIENT_CURSE:
2745                                                 ok_flag = FALSE;
2746                                         }
2747                                         if (ok_flag)
2748                                                 break; /* while (1) */
2749                                 }
2750                         }
2751                         break;
2752                 }
2753
2754                 case TV_CLOAK:
2755                 {
2756                         /* Very good */
2757                         if (power > 1)
2758                         {
2759                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2760                                 {
2761                                         create_artifact(o_ptr, FALSE);
2762                                         break;
2763                                 }
2764                                 o_ptr->name2 = get_random_ego(INVEN_OUTER, TRUE);
2765
2766                                 switch (o_ptr->name2)
2767                                 {
2768                                 case EGO_BAT:
2769                                         o_ptr->to_d -= 6;
2770                                         o_ptr->to_h -= 6;
2771                                         break;
2772                                 case EGO_NAZGUL:
2773                                         o_ptr->to_d -= 3;
2774                                         o_ptr->to_h -= 3;
2775                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_COWARDICE);
2776                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_CALL_UNDEAD);
2777                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_SLOW_REGEN);
2778                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
2779                                         break;
2780                                 }
2781
2782                         }
2783
2784                         /* Very cursed */
2785                         else if (power < -1)
2786                         {
2787                                 o_ptr->name2 = get_random_ego(INVEN_OUTER, FALSE);
2788                         }
2789
2790                         break;
2791                 }
2792         }
2793
2794 }
2795
2796
2797 /*!
2798  * @brief 装飾品系オブジェクトに生成ランクごとの強化を与えるサブルーチン
2799  * Apply magic to an item known to be a "ring" or "amulet"
2800  * @param o_ptr 強化を与えたいオブジェクトの構造体参照ポインタ
2801  * @param level 生成基準階
2802  * @param power 生成ランク
2803  * @return なし
2804  * @details
2805  * Hack -- note special "pval boost" code for ring of speed\n
2806  * Hack -- note that some items must be cursed (or blessed)\n
2807  */
2808 static void a_m_aux_3(object_type *o_ptr, DEPTH level, int power)
2809 {
2810         /* Apply magic (good or bad) according to type */
2811         switch (o_ptr->tval)
2812         {
2813                 case TV_RING:
2814                 {
2815                         /* Analyze */
2816                         switch (o_ptr->sval)
2817                         {
2818                                 case SV_RING_ATTACKS:
2819                                 {
2820                                         /* Stat bonus */
2821                                         o_ptr->pval = (PARAMETER_VALUE)m_bonus(2, level);
2822                                         if (one_in_(15)) o_ptr->pval++;
2823                                         if (o_ptr->pval < 1) o_ptr->pval = 1;
2824
2825                                         /* Cursed */
2826                                         if (power < 0)
2827                                         {
2828                                                 /* Broken */
2829                                                 o_ptr->ident |= (IDENT_BROKEN);
2830
2831                                                 /* Cursed */
2832                                                 o_ptr->curse_flags |= TRC_CURSED;
2833
2834                                                 /* Reverse pval */
2835                                                 o_ptr->pval = 0 - (o_ptr->pval);
2836                                         }
2837
2838                                         break;
2839                                 }
2840
2841                                 case SV_RING_SHOTS:
2842                                 {
2843                                         break;
2844                                 }
2845
2846                                 /* Strength, Constitution, Dexterity, Intelligence */
2847                                 case SV_RING_STR:
2848                                 case SV_RING_CON:
2849                                 case SV_RING_DEX:
2850                                 {
2851                                         /* Stat bonus */
2852                                         o_ptr->pval = 1 + (PARAMETER_VALUE)m_bonus(5, level);
2853
2854                                         /* Cursed */
2855                                         if (power < 0)
2856                                         {
2857                                                 /* Broken */
2858                                                 o_ptr->ident |= (IDENT_BROKEN);
2859
2860                                                 /* Cursed */
2861                                                 o_ptr->curse_flags |= TRC_CURSED;
2862
2863                                                 /* Reverse pval */
2864                                                 o_ptr->pval = 0 - (o_ptr->pval);
2865                                         }
2866
2867                                         break;
2868                                 }
2869
2870                                 /* Ring of Speed! */
2871                                 case SV_RING_SPEED:
2872                                 {
2873                                         /* Base speed (1 to 10) */
2874                                         o_ptr->pval = randint1(5) + (PARAMETER_VALUE)m_bonus(5, level);
2875
2876                                         /* Super-charge the ring */
2877                                         while (randint0(100) < 50) o_ptr->pval++;
2878
2879                                         /* Cursed Ring */
2880                                         if (power < 0)
2881                                         {
2882                                                 /* Broken */
2883                                                 o_ptr->ident |= (IDENT_BROKEN);
2884
2885                                                 /* Cursed */
2886                                                 o_ptr->curse_flags |= TRC_CURSED;
2887
2888                                                 /* Reverse pval */
2889                                                 o_ptr->pval = 0 - (o_ptr->pval);
2890
2891                                                 break;
2892                                         }
2893
2894                                         break;
2895                                 }
2896
2897                                 case SV_RING_LORDLY:
2898                                 {
2899                                         do
2900                                         {
2901                                                 one_lordly_high_resistance(o_ptr);
2902                                         }
2903                                         while (one_in_(4));
2904
2905                                         /* Bonus to armor class */
2906                                         o_ptr->to_a = 10 + randint1(5) + (ARMOUR_CLASS)m_bonus(10, level);
2907                                 }
2908                                 break;
2909
2910                                 case SV_RING_WARNING:
2911                                 {
2912                                         if (one_in_(3)) one_low_esp(o_ptr);
2913                                         break;
2914                                 }
2915
2916                                 /* Searching */
2917                                 case SV_RING_SEARCHING:
2918                                 {
2919                                         /* Bonus to searching */
2920                                         o_ptr->pval = 1 + (PARAMETER_VALUE)m_bonus(5, level);
2921
2922                                         /* Cursed */
2923                                         if (power < 0)
2924                                         {
2925                                                 /* Broken */
2926                                                 o_ptr->ident |= (IDENT_BROKEN);
2927
2928                                                 /* Cursed */
2929                                                 o_ptr->curse_flags |= TRC_CURSED;
2930
2931                                                 /* Reverse pval */
2932                                                 o_ptr->pval = 0 - (o_ptr->pval);
2933                                         }
2934
2935                                         break;
2936                                 }
2937
2938                                 /* Flames, Acid, Ice */
2939                                 case SV_RING_FLAMES:
2940                                 case SV_RING_ACID:
2941                                 case SV_RING_ICE:
2942                                 case SV_RING_ELEC:
2943                                 {
2944                                         /* Bonus to armor class */
2945                                         o_ptr->to_a = 5 + randint1(5) + (ARMOUR_CLASS)m_bonus(10, level);
2946                                         break;
2947                                 }
2948
2949                                 /* Weakness, Stupidity */
2950                                 case SV_RING_WEAKNESS:
2951                                 case SV_RING_STUPIDITY:
2952                                 {
2953                                         /* Broken */
2954                                         o_ptr->ident |= (IDENT_BROKEN);
2955
2956                                         /* Cursed */
2957                                         o_ptr->curse_flags |= TRC_CURSED;
2958
2959                                         /* Penalize */
2960                                         o_ptr->pval = 0 - (1 + (PARAMETER_VALUE)m_bonus(5, level));
2961                                         if (power > 0) power = 0 - power;
2962
2963                                         break;
2964                                 }
2965
2966                                 /* WOE, Stupidity */
2967                                 case SV_RING_WOE:
2968                                 {
2969                                         /* Broken */
2970                                         o_ptr->ident |= (IDENT_BROKEN);
2971
2972                                         /* Cursed */
2973                                         o_ptr->curse_flags |= TRC_CURSED;
2974
2975                                         /* Penalize */
2976                                         o_ptr->to_a = 0 - (5 + (ARMOUR_CLASS)m_bonus(10, level));
2977                                         o_ptr->pval = 0 - (1 + (PARAMETER_VALUE)m_bonus(5, level));
2978                                         if (power > 0) power = 0 - power;
2979
2980                                         break;
2981                                 }
2982
2983                                 /* Ring of damage */
2984                                 case SV_RING_DAMAGE:
2985                                 {
2986                                         /* Bonus to damage */
2987                                         o_ptr->to_d = 1 + randint1(5) + (HIT_POINT)m_bonus(16, level);
2988
2989                                         /* Cursed */
2990                                         if (power < 0)
2991                                         {
2992                                                 /* Broken */
2993                                                 o_ptr->ident |= (IDENT_BROKEN);
2994
2995                                                 /* Cursed */
2996                                                 o_ptr->curse_flags |= TRC_CURSED;
2997
2998                                                 /* Reverse bonus */
2999                                                 o_ptr->to_d = 0 - o_ptr->to_d;
3000                                         }
3001
3002                                         break;
3003                                 }
3004
3005                                 /* Ring of Accuracy */
3006                                 case SV_RING_ACCURACY:
3007                                 {
3008                                         /* Bonus to hit */
3009                                         o_ptr->to_h = 1 + randint1(5) + (HIT_PROB)m_bonus(16, level);
3010
3011                                         /* Cursed */
3012                                         if (power < 0)
3013                                         {
3014                                                 /* Broken */
3015                                                 o_ptr->ident |= (IDENT_BROKEN);
3016
3017                                                 /* Cursed */
3018                                                 o_ptr->curse_flags |= TRC_CURSED;
3019
3020                                                 /* Reverse tohit */
3021                                                 o_ptr->to_h = 0 - o_ptr->to_h;
3022                                         }
3023
3024                                         break;
3025                                 }
3026
3027                                 /* Ring of Protection */
3028                                 case SV_RING_PROTECTION:
3029                                 {
3030                                         /* Bonus to armor class */
3031                                         o_ptr->to_a = 5 + randint1(8) + (ARMOUR_CLASS)m_bonus(10, level);
3032
3033                                         /* Cursed */
3034                                         if (power < 0)
3035                                         {
3036                                                 /* Broken */
3037                                                 o_ptr->ident |= (IDENT_BROKEN);
3038
3039                                                 /* Cursed */
3040                                                 o_ptr->curse_flags |= TRC_CURSED;
3041
3042                                                 /* Reverse toac */
3043                                                 o_ptr->to_a = 0 - o_ptr->to_a;
3044                                         }
3045
3046                                         break;
3047                                 }
3048
3049                                 /* Ring of Slaying */
3050                                 case SV_RING_SLAYING:
3051                                 {
3052                                         /* Bonus to damage and to hit */
3053                                         o_ptr->to_d = randint1(5) + (HIT_POINT)m_bonus(12, level);
3054                                         o_ptr->to_h = randint1(5) + (HIT_PROB)m_bonus(12, level);
3055
3056                                         /* Cursed */
3057                                         if (power < 0)
3058                                         {
3059                                                 /* Broken */
3060                                                 o_ptr->ident |= (IDENT_BROKEN);
3061
3062                                                 /* Cursed */
3063                                                 o_ptr->curse_flags |= TRC_CURSED;
3064
3065                                                 /* Reverse bonuses */
3066                                                 o_ptr->to_h = 0 - o_ptr->to_h;
3067                                                 o_ptr->to_d = 0 - o_ptr->to_d;
3068                                         }
3069
3070                                         break;
3071                                 }
3072
3073                                 case SV_RING_MUSCLE:
3074                                 {
3075                                         o_ptr->pval = 1 + (PARAMETER_VALUE)m_bonus(3, level);
3076                                         if (one_in_(4)) o_ptr->pval++;
3077
3078                                         /* Cursed */
3079                                         if (power < 0)
3080                                         {
3081                                                 /* Broken */
3082                                                 o_ptr->ident |= (IDENT_BROKEN);
3083
3084                                                 /* Cursed */
3085                                                 o_ptr->curse_flags |= TRC_CURSED;
3086
3087                                                 /* Reverse bonuses */
3088                                                 o_ptr->pval = 0 - o_ptr->pval;
3089                                         }
3090
3091                                         break;
3092                                 }
3093                                 case SV_RING_AGGRAVATION:
3094                                 {
3095                                         /* Broken */
3096                                         o_ptr->ident |= (IDENT_BROKEN);
3097
3098                                         /* Cursed */
3099                                         o_ptr->curse_flags |= TRC_CURSED;
3100
3101                                         if (power > 0) power = 0 - power;
3102                                         break;
3103                                 }
3104                         }
3105                         if ((one_in_(400) && (power > 0) && !object_is_cursed(o_ptr) && (level > 79))
3106                             || (power > 2)) /* power > 2 is debug only */
3107                         {
3108                                 o_ptr->pval = MIN(o_ptr->pval, 4);
3109                                 /* Randart amulet */
3110                                 create_artifact(o_ptr, FALSE);
3111                         }
3112                         else if ((power == 2) && one_in_(2))
3113                         {
3114                                 while(!o_ptr->name2)
3115                                 {
3116                                         int tmp = m_bonus(10, level);
3117                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3118                                         switch(randint1(28))
3119                                         {
3120                                         case 1: case 2:
3121                                                 o_ptr->name2 = EGO_RING_THROW;
3122                                                 break;
3123                                         case 3: case 4:
3124                                                 if (have_flag(k_ptr->flags, TR_REGEN)) break;
3125                                                 o_ptr->name2 = EGO_RING_REGEN;
3126                                                 break;
3127                                         case 5: case 6:
3128                                                 if (have_flag(k_ptr->flags, TR_LITE_1)) break;
3129                                                 o_ptr->name2 = EGO_RING_LITE;
3130                                                 break;
3131                                         case 7: case 8:
3132                                                 if (have_flag(k_ptr->flags, TR_TELEPORT)) break;
3133                                                 o_ptr->name2 = EGO_RING_TELEPORT;
3134                                                 break;
3135                                         case 9: case 10:
3136                                                 if (o_ptr->to_h) break;
3137                                                 o_ptr->name2 = EGO_RING_TO_H;
3138                                                 break;
3139                                         case 11: case 12:
3140                                                 if (o_ptr->to_d) break;
3141                                                 o_ptr->name2 = EGO_RING_TO_D;
3142                                                 break;
3143                                         case 13:
3144                                                 if ((o_ptr->to_h) || (o_ptr->to_d)) break;
3145                                                 o_ptr->name2 = EGO_RING_SLAY;
3146                                                 break;
3147                                         case 14:
3148                                                 if ((have_flag(k_ptr->flags, TR_STR)) || o_ptr->to_h || o_ptr->to_d) break;
3149                                                 o_ptr->name2 = EGO_RING_WIZARD;
3150                                                 break;
3151                                         case 15:
3152                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3153                                                 o_ptr->name2 = EGO_RING_HERO;
3154                                                 break;
3155                                         case 16:
3156                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3157                                                 if (tmp > 8) o_ptr->name2 = EGO_RING_MANA_BALL;
3158                                                 else if (tmp > 4) o_ptr->name2 = EGO_RING_MANA_BOLT;
3159                                                 else o_ptr->name2 = EGO_RING_MAGIC_MIS;
3160                                                 break;
3161                                         case 17:
3162                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3163                                                 if (!(have_flag(k_ptr->flags, TR_RES_FIRE)) && (have_flag(k_ptr->flags, TR_RES_COLD) || have_flag(k_ptr->flags, TR_RES_ELEC) || have_flag(k_ptr->flags, TR_RES_ACID))) break;
3164                                                 if (tmp > 7) o_ptr->name2 = EGO_RING_DRAGON_F;
3165                                                 else if (tmp > 3) o_ptr->name2 = EGO_RING_FIRE_BALL;
3166                                                 else o_ptr->name2 = EGO_RING_FIRE_BOLT;
3167                                                 break;
3168                                         case 18:
3169                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3170                                                 if (!(have_flag(k_ptr->flags, TR_RES_COLD)) && (have_flag(k_ptr->flags, TR_RES_FIRE) || have_flag(k_ptr->flags, TR_RES_ELEC) || have_flag(k_ptr->flags, TR_RES_ACID))) break;
3171                                                 if (tmp > 7) o_ptr->name2 = EGO_RING_DRAGON_C;
3172                                                 else if (tmp > 3) o_ptr->name2 = EGO_RING_COLD_BALL;
3173                                                 else o_ptr->name2 = EGO_RING_COLD_BOLT;
3174                                                 break;
3175                                         case 19:
3176                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3177                                                 if (!(have_flag(k_ptr->flags, TR_RES_ELEC)) && (have_flag(k_ptr->flags, TR_RES_COLD) || have_flag(k_ptr->flags, TR_RES_FIRE) || have_flag(k_ptr->flags, TR_RES_ACID))) break;
3178                                                 if (tmp > 4) o_ptr->name2 = EGO_RING_ELEC_BALL;
3179                                                 else o_ptr->name2 = EGO_RING_ELEC_BOLT;
3180                                                 break;
3181                                         case 20:
3182                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3183                                                 if (!(have_flag(k_ptr->flags, TR_RES_ACID)) && (have_flag(k_ptr->flags, TR_RES_COLD) || have_flag(k_ptr->flags, TR_RES_ELEC) || have_flag(k_ptr->flags, TR_RES_FIRE))) break;
3184                                                 if (tmp > 4) o_ptr->name2 = EGO_RING_ACID_BALL;
3185                                                 else o_ptr->name2 = EGO_RING_ACID_BOLT;
3186                                                 break;
3187                                         case 21: case 22: case 23: case 24: case 25: case 26:
3188                                                 switch (o_ptr->sval)
3189                                                 {
3190                                                 case SV_RING_SPEED:
3191                                                         if (!one_in_(3)) break;
3192                                                         o_ptr->name2 = EGO_RING_D_SPEED;
3193                                                         break;
3194                                                 case SV_RING_DAMAGE:
3195                                                 case SV_RING_ACCURACY:
3196                                                 case SV_RING_SLAYING:
3197                                                         if (one_in_(2)) break;
3198                                                         if (one_in_(2)) o_ptr->name2 = EGO_RING_HERO;
3199                                                         else
3200                                                         {
3201                                                                 o_ptr->name2 = EGO_RING_BERSERKER;
3202                                                                 o_ptr->to_h -= 2+randint1(4);
3203                                                                 o_ptr->to_d += 2+randint1(4);
3204                                                         }
3205                                                         break;
3206                                                 case SV_RING_PROTECTION:
3207                                                         o_ptr->name2 = EGO_RING_SUPER_AC;
3208                                                         o_ptr->to_a += 7 + m_bonus(5, level);
3209                                                         break;
3210                                                 case SV_RING_RES_FEAR:
3211                                                         o_ptr->name2 = EGO_RING_HERO;
3212                                                         break;
3213                                                 case SV_RING_SHOTS:
3214                                                         if (one_in_(2)) break;
3215                                                         o_ptr->name2 = EGO_RING_HUNTER;
3216                                                         break;
3217                                                 case SV_RING_SEARCHING:
3218                                                         o_ptr->name2 = EGO_RING_STEALTH;
3219                                                         break;
3220                                                 case SV_RING_TELEPORTATION:
3221                                                         o_ptr->name2 = EGO_RING_TELE_AWAY;
3222                                                         break;
3223                                                 case SV_RING_RES_BLINDNESS:
3224                                                         if (one_in_(2))
3225                                                                 o_ptr->name2 = EGO_RING_RES_LITE;
3226                                                         else
3227                                                                 o_ptr->name2 = EGO_RING_RES_DARK;
3228                                                         break;
3229                                                 case SV_RING_LORDLY:
3230                                                         if (!one_in_(20)) break;
3231                                                         one_lordly_high_resistance(o_ptr);
3232                                                         one_lordly_high_resistance(o_ptr);
3233                                                         o_ptr->name2 = EGO_RING_TRUE;
3234                                                         break;
3235                                                 case SV_RING_SUSTAIN:
3236                                                         if (!one_in_(4)) break;
3237                                                         o_ptr->name2 = EGO_RING_RES_TIME;
3238                                                         break;
3239                                                 case SV_RING_FLAMES:
3240                                                         if (!one_in_(2)) break;
3241                                                         o_ptr->name2 = EGO_RING_DRAGON_F;
3242                                                         break;
3243                                                 case SV_RING_ICE:
3244                                                         if (!one_in_(2)) break;
3245                                                         o_ptr->name2 = EGO_RING_DRAGON_C;
3246                                                         break;
3247                                                 case SV_RING_WARNING:
3248                                                         if (!one_in_(2)) break;
3249                                                         o_ptr->name2 = EGO_RING_M_DETECT;
3250                                                         break;
3251                                                 default:
3252                                                         break;
3253                                                 }
3254                                                 break;
3255                                         }
3256                                 }
3257                                 o_ptr->curse_flags = 0L;
3258                         }
3259                         else if ((power == -2) && one_in_(2))
3260                         {
3261                                 if (o_ptr->to_h > 0) o_ptr->to_h = 0-o_ptr->to_h;
3262                                 if (o_ptr->to_d > 0) o_ptr->to_d = 0-o_ptr->to_d;
3263                                 if (o_ptr->to_a > 0) o_ptr->to_a = 0-o_ptr->to_a;
3264                                 if (o_ptr->pval > 0) o_ptr->pval = 0-o_ptr->pval;
3265                                 o_ptr->art_flags[0] = 0;
3266                                 o_ptr->art_flags[1] = 0;
3267                                 while(!o_ptr->name2)
3268                                 {
3269                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3270                                         switch(randint1(5))
3271                                         {
3272                                         case 1:
3273                                                 if (have_flag(k_ptr->flags, TR_DRAIN_EXP)) break;
3274                                                 o_ptr->name2 = EGO_RING_DRAIN_EXP;
3275                                                 break;
3276                                         case 2:
3277                                                 o_ptr->name2 = EGO_RING_NO_MELEE;
3278                                                 break;
3279                                         case 3:
3280                                                 if (have_flag(k_ptr->flags, TR_AGGRAVATE)) break;
3281                                                 o_ptr->name2 = EGO_RING_AGGRAVATE;
3282                                                 break;
3283                                         case 4:
3284                                                 if (have_flag(k_ptr->flags, TR_TY_CURSE)) break;
3285                                                 o_ptr->name2 = EGO_RING_TY_CURSE;
3286                                                 break;
3287                                         case 5:
3288                                                 o_ptr->name2 = EGO_RING_ALBINO;
3289                                                 break;
3290                                         }
3291                                 }
3292                                 /* Broken */
3293                                 o_ptr->ident |= (IDENT_BROKEN);
3294
3295                                 /* Cursed */
3296                                 o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
3297                         }
3298                         break;
3299                 }
3300
3301                 case TV_AMULET:
3302                 {
3303                         /* Analyze */
3304                         switch (o_ptr->sval)
3305                         {
3306                                 /* Amulet of wisdom/charisma */
3307                                 case SV_AMULET_INTELLIGENCE:
3308                                 case SV_AMULET_WISDOM:
3309                                 case SV_AMULET_CHARISMA:
3310                                 {
3311                                         o_ptr->pval = 1 + (PARAMETER_VALUE)m_bonus(5, level);
3312
3313                                         /* Cursed */
3314                                         if (power < 0)
3315                                         {
3316                                                 /* Broken */
3317                                                 o_ptr->ident |= (IDENT_BROKEN);
3318
3319                                                 /* Cursed */
3320                                                 o_ptr->curse_flags |= (TRC_CURSED);
3321
3322                                                 /* Reverse bonuses */
3323                                                 o_ptr->pval = 0 - o_ptr->pval;
3324                                         }
3325
3326                                         break;
3327                                 }
3328
3329                                 /* Amulet of brilliance */
3330                                 case SV_AMULET_BRILLIANCE:
3331                                 {
3332                                         o_ptr->pval = 1 + m_bonus(3, level);
3333                                         if (one_in_(4)) o_ptr->pval++;
3334
3335                                         /* Cursed */
3336                                         if (power < 0)
3337                                         {
3338                                                 /* Broken */
3339                                                 o_ptr->ident |= (IDENT_BROKEN);
3340
3341                                                 /* Cursed */
3342                                                 o_ptr->curse_flags |= (TRC_CURSED);
3343
3344                                                 /* Reverse bonuses */
3345                                                 o_ptr->pval = 0 - o_ptr->pval;
3346                                         }
3347
3348                                         break;
3349                                 }
3350
3351                                 case SV_AMULET_NO_MAGIC: case SV_AMULET_NO_TELE:
3352                                 {
3353                                         if (power < 0)
3354                                         {
3355                                                 o_ptr->curse_flags |= (TRC_CURSED);
3356                                         }
3357                                         break;
3358                                 }
3359
3360                                 case SV_AMULET_RESISTANCE:
3361                                 {
3362                                         if (one_in_(5)) one_high_resistance(o_ptr);
3363                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_RES_POIS);
3364                                 }
3365                                 break;
3366
3367                                 /* Amulet of searching */
3368                                 case SV_AMULET_SEARCHING:
3369                                 {
3370                                         o_ptr->pval = randint1(2) + (PARAMETER_VALUE)m_bonus(4, level);
3371
3372                                         /* Cursed */
3373                                         if (power < 0)
3374                                         {
3375                                                 /* Broken */
3376                                                 o_ptr->ident |= (IDENT_BROKEN);
3377
3378                                                 /* Cursed */
3379                                                 o_ptr->curse_flags |= (TRC_CURSED);
3380
3381                                                 /* Reverse bonuses */
3382                                                 o_ptr->pval = 0 - (o_ptr->pval);
3383                                         }
3384
3385                                         break;
3386                                 }
3387
3388                                 /* Amulet of the Magi -- never cursed */
3389                                 case SV_AMULET_THE_MAGI:
3390                                 {
3391                                         o_ptr->pval = randint1(5) + (PARAMETER_VALUE)m_bonus(5, level);
3392                                         o_ptr->to_a = randint1(5) + (ARMOUR_CLASS)m_bonus(5, level);
3393
3394                                         /* gain one low ESP */
3395                                         add_esp_weak(o_ptr, FALSE);
3396
3397                                         break;
3398                                 }
3399
3400                                 /* Amulet of Doom -- always cursed */
3401                                 case SV_AMULET_DOOM:
3402                                 {
3403                                         /* Broken */
3404                                         o_ptr->ident |= (IDENT_BROKEN);
3405
3406                                         /* Cursed */
3407                                         o_ptr->curse_flags |= (TRC_CURSED);
3408
3409                                         /* Penalize */
3410                                         o_ptr->pval = 0 - (randint1(5) + (PARAMETER_VALUE)m_bonus(5, level));
3411                                         o_ptr->to_a = 0 - (randint1(5) + (ARMOUR_CLASS)m_bonus(5, level));
3412                                         if (power > 0) power = 0 - power;
3413
3414                                         break;
3415                                 }
3416
3417                                 case SV_AMULET_MAGIC_MASTERY:
3418                                 {
3419                                         o_ptr->pval = 1 + (PARAMETER_VALUE)m_bonus(4, level);
3420
3421                                         /* Cursed */
3422                                         if (power < 0)
3423                                         {
3424                                                 /* Broken */
3425                                                 o_ptr->ident |= (IDENT_BROKEN);
3426
3427                                                 /* Cursed */
3428                                                 o_ptr->curse_flags |= (TRC_CURSED);
3429
3430                                                 /* Reverse bonuses */
3431                                                 o_ptr->pval = 0 - o_ptr->pval;
3432                                         }
3433
3434                                         break;
3435                                 }
3436                         }
3437                         if ((one_in_(150) && (power > 0) && !object_is_cursed(o_ptr) && (level > 79))
3438                             || (power > 2)) /* power > 2 is debug only */
3439                         {
3440                                 o_ptr->pval = MIN(o_ptr->pval, 4);
3441                                 /* Randart amulet */
3442                                 create_artifact(o_ptr, FALSE);
3443                         }
3444                         else if ((power == 2) && one_in_(2))
3445                         {
3446                                 while(!o_ptr->name2)
3447                                 {
3448                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3449                                         switch(randint1(21))
3450                                         {
3451                                         case 1: case 2:
3452                                                 if (have_flag(k_ptr->flags, TR_SLOW_DIGEST)) break;
3453                                                 o_ptr->name2 = EGO_AMU_SLOW_D;
3454                                                 break;
3455                                         case 3: case 4:
3456                                                 if (o_ptr->pval) break;
3457                                                 o_ptr->name2 = EGO_AMU_INFRA;
3458                                                 break;
3459                                         case 5: case 6:
3460                                                 if (have_flag(k_ptr->flags, TR_SEE_INVIS)) break;
3461                                                 o_ptr->name2 = EGO_AMU_SEE_INVIS;
3462                                                 break;
3463                                         case 7: case 8:
3464                                                 if (have_flag(k_ptr->flags, TR_HOLD_EXP)) break;
3465                                                 o_ptr->name2 = EGO_AMU_HOLD_EXP;
3466                                                 break;
3467                                         case 9:
3468                                                 if (have_flag(k_ptr->flags, TR_LEVITATION)) break;
3469                                                 o_ptr->name2 = EGO_AMU_LEVITATION;
3470                                                 break;
3471                                         case 10: case 11: case 21:
3472                                                 o_ptr->name2 = EGO_AMU_AC;
3473                                                 break;
3474                                         case 12:
3475                                                 if (have_flag(k_ptr->flags, TR_RES_FIRE)) break;
3476                                                 if (m_bonus(10, level) > 8)
3477                                                         o_ptr->name2 = EGO_AMU_RES_FIRE_;
3478                                                 else
3479                                                         o_ptr->name2 = EGO_AMU_RES_FIRE;
3480                                                 break;
3481                                         case 13:
3482                                                 if (have_flag(k_ptr->flags, TR_RES_COLD)) break;
3483                                                 if (m_bonus(10, level) > 8)
3484                                                         o_ptr->name2 = EGO_AMU_RES_COLD_;
3485                                                 else
3486                                                         o_ptr->name2 = EGO_AMU_RES_COLD;
3487                                                 break;
3488                                         case 14:
3489                                                 if (have_flag(k_ptr->flags, TR_RES_ELEC)) break;
3490                                                 if (m_bonus(10, level) > 8)
3491                                                         o_ptr->name2 = EGO_AMU_RES_ELEC_;
3492                                                 else
3493                                                         o_ptr->name2 = EGO_AMU_RES_ELEC;
3494                                                 break;
3495                                         case 15:
3496                                                 if (have_flag(k_ptr->flags, TR_RES_ACID)) break;
3497                                                 if (m_bonus(10, level) > 8)
3498                                                         o_ptr->name2 = EGO_AMU_RES_ACID_;
3499                                                 else
3500                                                         o_ptr->name2 = EGO_AMU_RES_ACID;
3501                                                 break;
3502                                         case 16: case 17: case 18: case 19: case 20:
3503                                                 switch (o_ptr->sval)
3504                                                 {
3505                                                 case SV_AMULET_TELEPORT:
3506                                                         if (m_bonus(10, level) > 9) o_ptr->name2 = EGO_AMU_D_DOOR;
3507                                                         else if (one_in_(2)) o_ptr->name2 = EGO_AMU_JUMP;
3508                                                         else o_ptr->name2 = EGO_AMU_TELEPORT;
3509                                                         break;
3510                                                 case SV_AMULET_RESIST_ACID:
3511                                                         if ((m_bonus(10, level) > 6) && one_in_(2)) o_ptr->name2 = EGO_AMU_RES_ACID_;
3512                                                         break;
3513                                                 case SV_AMULET_SEARCHING:
3514                                                         o_ptr->name2 = EGO_AMU_STEALTH;
3515                                                         break;
3516                                                 case SV_AMULET_BRILLIANCE:
3517                                                         if (!one_in_(3)) break;
3518                                                         o_ptr->name2 = EGO_AMU_IDENT;
3519                                                         break;
3520                                                 case SV_AMULET_CHARISMA:
3521                                                         if (!one_in_(3)) break;
3522                                                         o_ptr->name2 = EGO_AMU_CHARM;
3523                                                         break;
3524                                                 case SV_AMULET_THE_MAGI:
3525                                                         if (one_in_(2)) break;
3526                                                         o_ptr->name2 = EGO_AMU_GREAT;
3527                                                         break;
3528                                                 case SV_AMULET_RESISTANCE:
3529                                                         if (!one_in_(5)) break;
3530                                                         o_ptr->name2 = EGO_AMU_DEFENDER;
3531                                                         break;
3532                                                 case SV_AMULET_TELEPATHY:
3533                                                         if (!one_in_(3)) break;
3534                                                         o_ptr->name2 = EGO_AMU_DETECTION;
3535                                                         break;
3536                                                 }
3537                                         }
3538                                 }
3539                                 o_ptr->curse_flags = 0L;
3540                         }
3541                         else if ((power == -2) && one_in_(2))
3542                         {
3543                                 if (o_ptr->to_h > 0) o_ptr->to_h = 0-o_ptr->to_h;
3544                                 if (o_ptr->to_d > 0) o_ptr->to_d = 0-o_ptr->to_d;
3545                                 if (o_ptr->to_a > 0) o_ptr->to_a = 0-o_ptr->to_a;
3546                                 if (o_ptr->pval > 0) o_ptr->pval = 0-o_ptr->pval;
3547                                 o_ptr->art_flags[0] = 0;
3548                                 o_ptr->art_flags[1] = 0;
3549                                 while(!o_ptr->name2)
3550                                 {
3551                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3552                                         switch(randint1(5))
3553                                         {
3554                                         case 1:
3555                                                 if (have_flag(k_ptr->flags, TR_DRAIN_EXP)) break;
3556                                                 o_ptr->name2 = EGO_AMU_DRAIN_EXP;
3557                                                 break;
3558                                         case 2:
3559                                                 o_ptr->name2 = EGO_AMU_FOOL;
3560                                                 break;
3561                                         case 3:
3562                                                 if (have_flag(k_ptr->flags, TR_AGGRAVATE)) break;
3563                                                 o_ptr->name2 = EGO_AMU_AGGRAVATE;
3564                                                 break;
3565                                         case 4:
3566                                                 if (have_flag(k_ptr->flags, TR_TY_CURSE)) break;
3567                                                 o_ptr->name2 = EGO_AMU_TY_CURSE;
3568                                                 break;
3569                                         case 5:
3570                                                 o_ptr->name2 = EGO_AMU_NAIVETY;
3571                                                 break;
3572                                         }
3573                                 }
3574                                 /* Broken */
3575                                 o_ptr->ident |= (IDENT_BROKEN);
3576
3577                                 /* Cursed */
3578                                 o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
3579                         }
3580                         break;
3581                 }
3582         }
3583 }
3584
3585 /*!
3586  * @brief モンスターが人形のベースにできるかを返す
3587  * @param r_idx チェックしたいモンスター種族のID
3588  * @return 人形にできるならTRUEを返す
3589  */
3590 static bool item_monster_okay(MONRACE_IDX r_idx)
3591 {
3592         monster_race *r_ptr = &r_info[r_idx];
3593
3594         /* No uniques */
3595         if (r_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3596         if (r_ptr->flags7 & RF7_KAGE) return (FALSE);
3597         if (r_ptr->flagsr & RFR_RES_ALL) return (FALSE);
3598         if (r_ptr->flags7 & RF7_NAZGUL) return (FALSE);
3599         if (r_ptr->flags1 & RF1_FORCE_DEPTH) return (FALSE);
3600         if (r_ptr->flags7 & RF7_UNIQUE2) return (FALSE);
3601
3602         return (TRUE);
3603 }
3604
3605
3606 /*!
3607  * @brief その他雑多のオブジェクトに生成ランクごとの強化を与えるサブルーチン
3608  * Apply magic to an item known to be "boring"
3609  * @param o_ptr 強化を与えたいオブジェクトの構造体参照ポインタ
3610  * @param level 生成基準階
3611  * @param power 生成ランク
3612  * @return なし
3613  * @details
3614  * Hack -- note the special code for various items
3615  */
3616 static void a_m_aux_4(object_type *o_ptr, DEPTH level, int power)
3617 {
3618         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3619
3620         /* Unused */
3621         (void)level;
3622
3623         /* Apply magic (good or bad) according to type */
3624         switch (o_ptr->tval)
3625         {
3626                 case TV_WHISTLE:
3627                 {
3628 #if 0
3629                         /* Cursed */
3630                         if (power < 0)
3631                         {
3632                                 /* Broken */
3633                                 o_ptr->ident |= (IDENT_BROKEN);
3634
3635                                 /* Cursed */
3636                                 o_ptr->curse_flags |= (TRC_CURSED);
3637                         }
3638 #endif
3639                         break;
3640                 }
3641                 case TV_FLASK:
3642                 {
3643                         o_ptr->xtra4 = o_ptr->pval;
3644                         o_ptr->pval = 0;
3645                         break;
3646                 }
3647                 case TV_LITE:
3648                 {
3649                         /* Hack -- Torches -- random fuel */
3650                         if (o_ptr->sval == SV_LITE_TORCH)
3651                         {
3652                                 if (o_ptr->pval > 0) o_ptr->xtra4 = randint1(o_ptr->pval);
3653                                 o_ptr->pval = 0;
3654                         }
3655
3656                         /* Hack -- Lanterns -- random fuel */
3657                         if (o_ptr->sval == SV_LITE_LANTERN)
3658                         {
3659                                 if (o_ptr->pval > 0) o_ptr->xtra4 = randint1(o_ptr->pval);
3660                                 o_ptr->pval = 0;
3661                         }
3662
3663                         if (power > 2) /* power > 2 is debug only */
3664                         {
3665                                 create_artifact(o_ptr, FALSE);
3666                         }
3667                         else if ((power == 2) || ((power == 1) && one_in_(3)))
3668                         {
3669                                 while (!o_ptr->name2)
3670                                 {
3671                                         while (1)
3672                                         {
3673                                                 bool okay_flag = TRUE;
3674
3675                                                 o_ptr->name2 = get_random_ego(INVEN_LITE, TRUE);
3676
3677                                                 switch (o_ptr->name2)
3678                                                 {
3679                                                 case EGO_LITE_LONG:
3680                                                         if (o_ptr->sval == SV_LITE_FEANOR)
3681                                                                 okay_flag = FALSE;
3682                                                 }
3683                                                 if (okay_flag)
3684                                                         break;
3685                                         }
3686                                 }
3687                         }
3688                         else if (power == -2)
3689                         {
3690                                 o_ptr->name2 = get_random_ego(INVEN_LITE, FALSE);
3691
3692                                 switch (o_ptr->name2)
3693                                 {
3694                                 case EGO_LITE_DARKNESS:
3695                                         o_ptr->xtra4 = 0;
3696                                         
3697                                         if (o_ptr->sval == SV_LITE_TORCH)
3698                                         {
3699                                                 add_flag(o_ptr->art_flags, TR_LITE_M1);
3700                                         }
3701                                         else if (o_ptr->sval == SV_LITE_LANTERN)
3702                                         {
3703                                                 add_flag(o_ptr->art_flags, TR_LITE_M2);
3704                                         }
3705                                         else if (o_ptr->sval == SV_LITE_FEANOR)
3706                                         {
3707                                                 add_flag(o_ptr->art_flags, TR_LITE_M3);
3708                                         }
3709                                         break;
3710                                 }
3711                         }
3712
3713                         break;
3714                 }
3715
3716                 case TV_WAND:
3717                 case TV_STAFF:
3718                 {
3719                         /* The wand or staff gets a number of initial charges equal
3720                          * to between 1/2 (+1) and the full object kind's pval. -LM-
3721                          */
3722                         o_ptr->pval = k_ptr->pval / 2 + randint1((k_ptr->pval + 1) / 2);
3723                         break;
3724                 }
3725
3726                 case TV_ROD:
3727                 {
3728                         /* Transfer the pval. -LM- */
3729                         o_ptr->pval = k_ptr->pval;
3730                         break;
3731                 }
3732
3733                 case TV_CAPTURE:
3734                 {
3735                         o_ptr->pval = 0;
3736                         object_aware(o_ptr);
3737                         object_known(o_ptr);
3738                         break;
3739                 }
3740
3741                 case TV_FIGURINE:
3742                 {
3743                         PARAMETER_VALUE i = 1;
3744                         int check;
3745
3746                         monster_race *r_ptr;
3747
3748                         /* Pick a random non-unique monster race */
3749                         while (1)
3750                         {
3751                                 i = randint1(max_r_idx - 1);
3752
3753                                 if (!item_monster_okay(i)) continue;
3754                                 if (i == MON_TSUCHINOKO) continue;
3755
3756                                 r_ptr = &r_info[i];
3757
3758                                 check = (current_floor_ptr->dun_level < r_ptr->level) ? (r_ptr->level - current_floor_ptr->dun_level) : 0;
3759
3760                                 /* Ignore dead monsters */
3761                                 if (!r_ptr->rarity) continue;
3762
3763                                 /* Ignore uncommon monsters */
3764                                 if (r_ptr->rarity > 100) continue;
3765
3766                                 /* Prefer less out-of-depth monsters */
3767                                 if (randint0(check)) continue;
3768
3769                                 break;
3770                         }
3771
3772                         o_ptr->pval = i;
3773
3774                         /* Some figurines are cursed */
3775                         if (one_in_(6)) o_ptr->curse_flags |= TRC_CURSED;
3776
3777                         break;
3778                 }
3779
3780                 case TV_CORPSE:
3781                 {
3782                         PARAMETER_VALUE i = 1;
3783                         int check;
3784
3785                         u32b match = 0;
3786
3787                         monster_race *r_ptr;
3788
3789                         if (o_ptr->sval == SV_SKELETON)
3790                         {
3791                                 match = RF9_DROP_SKELETON;
3792                         }
3793                         else if (o_ptr->sval == SV_CORPSE)
3794                         {
3795                                 match = RF9_DROP_CORPSE;
3796                         }
3797
3798                         /* Hack -- Remove the monster restriction */
3799                         get_mon_num_prep(item_monster_okay, NULL);
3800
3801                         /* Pick a random non-unique monster race */
3802                         while (1)
3803                         {
3804                                 i = get_mon_num(current_floor_ptr->dun_level);
3805
3806                                 r_ptr = &r_info[i];
3807
3808                                 check = (current_floor_ptr->dun_level < r_ptr->level) ? (r_ptr->level - current_floor_ptr->dun_level) : 0;
3809
3810                                 /* Ignore dead monsters */
3811                                 if (!r_ptr->rarity) continue;
3812
3813                                 /* Ignore corpseless monsters */
3814                                 if (!(r_ptr->flags9 & match)) continue;
3815
3816                                 /* Prefer less out-of-depth monsters */
3817                                 if (randint0(check)) continue;
3818
3819                                 break;
3820                         }
3821
3822                         o_ptr->pval = i;
3823
3824
3825                         object_aware(o_ptr);
3826                         object_known(o_ptr);
3827                         break;
3828                 }
3829
3830                 case TV_STATUE:
3831                 {
3832                         PARAMETER_VALUE i = 1;
3833
3834                         monster_race *r_ptr;
3835
3836                         /* Pick a random monster race */
3837                         while (1)
3838                         {
3839                                 i = randint1(max_r_idx - 1);
3840
3841                                 r_ptr = &r_info[i];
3842
3843                                 /* Ignore dead monsters */
3844                                 if (!r_ptr->rarity) continue;
3845
3846                                 break;
3847                         }
3848
3849                         o_ptr->pval = i;
3850
3851                         if (cheat_peek)
3852                         {
3853                                 msg_format(_("%sの像", "Statue of %s"), r_name + r_ptr->name);
3854                         }
3855                         object_aware(o_ptr);
3856                         object_known(o_ptr);
3857
3858                         break;
3859                 }
3860
3861                 case TV_CHEST:
3862                 {
3863                         DEPTH obj_level = k_info[o_ptr->k_idx].level;
3864
3865                         /* Hack -- skip ruined chests */
3866                         if (obj_level <= 0) break;
3867
3868                         /* Hack -- pick a "difficulty" */
3869                         o_ptr->pval = randint1(obj_level);
3870                         if (o_ptr->sval == SV_CHEST_KANDUME) o_ptr->pval = 6;
3871
3872                         o_ptr->xtra3 = current_floor_ptr->dun_level + 5;
3873
3874                         /* Never exceed "difficulty" of 55 to 59 */
3875                         if (o_ptr->pval > 55) o_ptr->pval = 55 + (byte)randint0(5);
3876
3877                         break;
3878                 }
3879         }
3880 }
3881
3882 /*!
3883  * @brief 生成されたベースアイテムに魔法的な強化を与えるメインルーチン
3884  * Complete the "creation" of an object by applying "magic" to the item
3885  * @param o_ptr 強化を与えたいオブジェクトの構造体参照ポインタ
3886  * @param lev 生成基準階
3887  * @param mode 生成オプション
3888  * @return なし
3889  * @details
3890  * This includes not only rolling for random bonuses, but also putting the\n
3891  * finishing touches on ego-items and artifacts, giving charges to wands and\n
3892  * staffs, giving fuel to lites, and placing traps on chests.\n
3893  *\n
3894  * In particular, note that "Instant Artifacts", if "created" by an external\n
3895  * routine, must pass through this function to complete the actual creation.\n
3896  *\n
3897  * The base "chance" of the item being "good" increases with the "level"\n
3898  * parameter, which is usually derived from the dungeon level, being equal\n
3899  * to the level plus 10, up to a maximum of 75.  If "good" is true, then\n
3900  * the object is guaranteed to be "good".  If an object is "good", then\n
3901  * the chance that the object will be "great" (ego-item or artifact), also\n
3902  * increases with the "level", being equal to half the level, plus 5, up to\n
3903  * a maximum of 20.  If "great" is true, then the object is guaranteed to be\n
3904  * "great".  At dungeon level 65 and below, 15/100 objects are "great".\n
3905  *\n
3906  * If the object is not "good", there is a chance it will be "cursed", and\n
3907  * if it is "cursed", there is a chance it will be "broken".  These chances\n
3908  * are related to the "good" / "great" chances above.\n
3909  *\n
3910  * Otherwise "normal" rings and amulets will be "good" half the time and\n
3911  * "cursed" half the time, unless the ring/amulet is always good or cursed.\n
3912  *\n
3913  * If "okay" is true, and the object is going to be "great", then there is\n
3914  * a chance that an artifact will be created.  This is true even if both the\n
3915  * "good" and "great" arguments are false.  As a total hack, if "great" is\n
3916  * true, then the item gets 3 extra "attempts" to become an artifact.\n
3917  */
3918 void apply_magic(object_type *o_ptr, DEPTH lev, BIT_FLAGS mode)
3919 {
3920         int i, rolls, f1, f2, power;
3921
3922         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN) lev += randint0(p_ptr->lev/2+10);
3923
3924         /* Maximum "level" for various things */
3925         if (lev > MAX_DEPTH - 1) lev = MAX_DEPTH - 1;
3926
3927         /* Base chance of being "good" */
3928         f1 = lev + 10;
3929
3930         /* Maximal chance of being "good" */
3931         if (f1 > d_info[p_ptr->dungeon_idx].obj_good) f1 = d_info[p_ptr->dungeon_idx].obj_good;
3932
3933         /* Base chance of being "great" */
3934         f2 = f1 * 2 / 3;
3935
3936         /* Maximal chance of being "great" */
3937         if ((p_ptr->pseikaku != SEIKAKU_MUNCHKIN) && (f2 > d_info[p_ptr->dungeon_idx].obj_great))
3938                 f2 = d_info[p_ptr->dungeon_idx].obj_great;
3939
3940         if (p_ptr->muta3 & MUT3_GOOD_LUCK)
3941         {
3942                 f1 += 5;
3943                 f2 += 2;
3944         }
3945         else if(p_ptr->muta3 & MUT3_BAD_LUCK)
3946         {
3947                 f1 -= 5;
3948                 f2 -= 2;
3949         }
3950
3951         /* Assume normal */
3952         power = 0;
3953
3954         /* Roll for "good" */
3955         if ((mode & AM_GOOD) || magik(f1))
3956         {
3957                 /* Assume "good" */
3958                 power = 1;
3959
3960                 /* Roll for "great" */
3961                 if ((mode & AM_GREAT) || magik(f2))
3962                 {
3963                         power = 2;
3964
3965                         /* Roll for "special" */
3966                         if (mode & AM_SPECIAL) power = 3;
3967                 }
3968         }
3969
3970         /* Roll for "cursed" */
3971         else if (magik(f1))
3972         {
3973                 /* Assume "cursed" */
3974                 power = -1;
3975
3976                 /* Roll for "broken" */
3977                 if (magik(f2)) power = -2;
3978         }
3979
3980         /* Apply curse */
3981         if (mode & AM_CURSED)
3982         {
3983                 /* Assume 'cursed' */
3984                 if (power > 0)
3985                 {
3986                         power = 0 - power;
3987                 }
3988                 /* Everything else gets more badly cursed */
3989                 else
3990                 {
3991                         power--;
3992                 }
3993         }
3994
3995         /* Assume no rolls */
3996         rolls = 0;
3997
3998         /* Get one roll if excellent */
3999         if (power >= 2) rolls = 1;
4000
4001         /* Hack -- Get four rolls if forced great or special */
4002         if (mode & (AM_GREAT | AM_SPECIAL)) rolls = 4;
4003
4004         /* Hack -- Get no rolls if not allowed */
4005         if ((mode & AM_NO_FIXED_ART) || o_ptr->name1) rolls = 0;
4006
4007         /* Roll for artifacts if allowed */
4008         for (i = 0; i < rolls; i++)
4009         {
4010                 /* Roll for an artifact */
4011                 if (make_artifact(o_ptr)) break;
4012                 if ((p_ptr->muta3 & MUT3_GOOD_LUCK) && one_in_(77))
4013                 {
4014                         if (make_artifact(o_ptr)) break;
4015                 }
4016         }
4017
4018
4019         /* Hack -- analyze artifacts */
4020         if (object_is_fixed_artifact(o_ptr))
4021         {
4022                 artifact_type *a_ptr = &a_info[o_ptr->name1];
4023
4024                 /* Hack -- Mark the artifact as "created" */
4025                 a_ptr->cur_num = 1;
4026
4027                 /* Hack -- Memorize location of artifact in saved floors */
4028                 if (character_dungeon)
4029                         a_ptr->floor_id = p_ptr->floor_id;
4030
4031                 /* Extract the other fields */
4032                 o_ptr->pval = a_ptr->pval;
4033                 o_ptr->ac = a_ptr->ac;
4034                 o_ptr->dd = a_ptr->dd;
4035                 o_ptr->ds = a_ptr->ds;
4036                 o_ptr->to_a = a_ptr->to_a;
4037                 o_ptr->to_h = a_ptr->to_h;
4038                 o_ptr->to_d = a_ptr->to_d;
4039                 o_ptr->weight = a_ptr->weight;
4040                 o_ptr->xtra2 = a_ptr->act_idx;
4041
4042                 if (o_ptr->name1 == ART_MILIM)
4043                 {
4044                     if(p_ptr->pseikaku == SEIKAKU_SEXY)
4045                     {
4046                         o_ptr->pval = 3;
4047                     }
4048                 }
4049
4050                 /* Hack -- extract the "broken" flag */
4051                 if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
4052
4053                 /* Hack -- extract the "cursed" flag */
4054                 if (a_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
4055                 if (a_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
4056                 if (a_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
4057                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4058                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4059                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4060
4061                 return;
4062         }
4063
4064         switch (o_ptr->tval)
4065         {
4066                 case TV_DIGGING:
4067                 case TV_HAFTED:
4068                 case TV_BOW:
4069                 case TV_SHOT:
4070                 case TV_ARROW:
4071                 case TV_BOLT:
4072                 {
4073                         if (power) apply_magic_weapon(o_ptr, lev, power);
4074                         break;
4075                 }
4076
4077                 case TV_POLEARM:
4078                 {
4079                         if (power && !(o_ptr->sval == SV_DEATH_SCYTHE)) apply_magic_weapon(o_ptr, lev, power);
4080                         break;
4081                 }
4082
4083                 case TV_SWORD:
4084                 {
4085                         if (power && !(o_ptr->sval == SV_DOKUBARI)) apply_magic_weapon(o_ptr, lev, power);
4086                         break;
4087                 }
4088
4089                 case TV_DRAG_ARMOR:
4090                 case TV_HARD_ARMOR:
4091                 case TV_SOFT_ARMOR:
4092                 case TV_SHIELD:
4093                 case TV_HELM:
4094                 case TV_CROWN:
4095                 case TV_CLOAK:
4096                 case TV_GLOVES:
4097                 case TV_BOOTS:
4098                 {
4099                         /* Elven Cloak and Black Clothes ... */
4100                         if (((o_ptr->tval == TV_CLOAK) && (o_ptr->sval == SV_ELVEN_CLOAK)) ||
4101                             ((o_ptr->tval == TV_SOFT_ARMOR) && (o_ptr->sval == SV_KUROSHOUZOKU)))
4102                                 o_ptr->pval = randint1(4);
4103
4104 #if 1
4105                         if (power ||
4106                              ((o_ptr->tval == TV_HELM) && (o_ptr->sval == SV_DRAGON_HELM)) ||
4107                              ((o_ptr->tval == TV_SHIELD) && (o_ptr->sval == SV_DRAGON_SHIELD)) ||
4108                              ((o_ptr->tval == TV_GLOVES) && (o_ptr->sval == SV_SET_OF_DRAGON_GLOVES)) ||
4109                              ((o_ptr->tval == TV_BOOTS) && (o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE)))
4110                                 a_m_aux_2(o_ptr, lev, power);
4111 #else
4112                         if (power) a_m_aux_2(o_ptr, lev, power);
4113 #endif
4114                         break;
4115                 }
4116
4117                 case TV_RING:
4118                 case TV_AMULET:
4119                 {
4120                         if (!power && (randint0(100) < 50)) power = -1;
4121                         a_m_aux_3(o_ptr, lev, power);
4122                         break;
4123                 }
4124
4125                 default:
4126                 {
4127                         a_m_aux_4(o_ptr, lev, power);
4128                         break;
4129                 }
4130         }
4131
4132         if ((o_ptr->tval == TV_SOFT_ARMOR) &&
4133             (o_ptr->sval == SV_ABUNAI_MIZUGI) &&
4134             (p_ptr->pseikaku == SEIKAKU_SEXY))
4135         {
4136                 o_ptr->pval = 3;
4137                 add_flag(o_ptr->art_flags, TR_STR);
4138                 add_flag(o_ptr->art_flags, TR_INT);
4139                 add_flag(o_ptr->art_flags, TR_WIS);
4140                 add_flag(o_ptr->art_flags, TR_DEX);
4141                 add_flag(o_ptr->art_flags, TR_CON);
4142                 add_flag(o_ptr->art_flags, TR_CHR);
4143         }
4144
4145         /* Hack -- analyze ego-items */
4146         if (object_is_ego(o_ptr))
4147         {
4148                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
4149
4150                 /* Hack -- acquire "broken" flag */
4151                 if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
4152
4153                 /* Hack -- acquire "cursed" flag */
4154                 if (e_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
4155                 if (e_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
4156                 if (e_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
4157                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4158                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4159                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4160
4161                 if (e_ptr->gen_flags & (TRG_ONE_SUSTAIN)) one_sustain(o_ptr);
4162                 if (e_ptr->gen_flags & (TRG_XTRA_POWER)) one_ability(o_ptr);
4163                 if (e_ptr->gen_flags & (TRG_XTRA_H_RES)) one_high_resistance(o_ptr);
4164                 if (e_ptr->gen_flags & (TRG_XTRA_E_RES)) one_ele_resistance(o_ptr);
4165                 if (e_ptr->gen_flags & (TRG_XTRA_D_RES)) one_dragon_ele_resistance(o_ptr);
4166                 if (e_ptr->gen_flags & (TRG_XTRA_L_RES)) one_lordly_high_resistance(o_ptr);
4167                 if (e_ptr->gen_flags & (TRG_XTRA_RES)) one_resistance(o_ptr);
4168                 if (e_ptr->gen_flags & (TRG_XTRA_DICE))
4169                 {
4170                         do
4171                         {
4172                                 o_ptr->dd++;
4173                         }
4174                         while (one_in_(o_ptr->dd));
4175
4176                         if (o_ptr->dd > 9) o_ptr->dd = 9;
4177                 }
4178
4179                 /* Hack -- apply activatin index if needed */
4180                 if (e_ptr->act_idx) o_ptr->xtra2 = (XTRA8)e_ptr->act_idx;
4181
4182                 /* Hack -- apply extra penalties if needed */
4183                 if ((object_is_cursed(o_ptr) || object_is_broken(o_ptr)) && !(e_ptr->gen_flags & (TRG_POWERFUL)))
4184                 {
4185                         /* Hack -- obtain bonuses */
4186                         if (e_ptr->max_to_h) o_ptr->to_h -= randint1(e_ptr->max_to_h);
4187                         if (e_ptr->max_to_d) o_ptr->to_d -= randint1(e_ptr->max_to_d);
4188                         if (e_ptr->max_to_a) o_ptr->to_a -= randint1(e_ptr->max_to_a);
4189
4190                         /* Hack -- obtain pval */
4191                         if (e_ptr->max_pval) o_ptr->pval -= randint1(e_ptr->max_pval);
4192                 }
4193
4194                 /* Hack -- apply extra bonuses if needed */
4195                 else
4196                 {
4197                         /* Hack -- obtain bonuses */
4198                         if (e_ptr->max_to_h)
4199                         {
4200                                 if (e_ptr->max_to_h > 127)
4201                                         o_ptr->to_h -= randint1(256-e_ptr->max_to_h);
4202                                 else o_ptr->to_h += randint1(e_ptr->max_to_h);
4203                         }
4204                         if (e_ptr->max_to_d)
4205                         {
4206                                 if (e_ptr->max_to_d > 127)
4207                                         o_ptr->to_d -= randint1(256-e_ptr->max_to_d);
4208                                 else o_ptr->to_d += randint1(e_ptr->max_to_d);
4209                         }
4210                         if (e_ptr->max_to_a)
4211                         {
4212                                 if (e_ptr->max_to_a > 127)
4213                                         o_ptr->to_a -= randint1(256-e_ptr->max_to_a);
4214                                 else o_ptr->to_a += randint1(e_ptr->max_to_a);
4215                         }
4216                         
4217                         /* Accuracy ego must have high to_h */
4218                         if(o_ptr->name2 == EGO_ACCURACY)
4219                         {
4220                                 while(o_ptr->to_h < o_ptr->to_d + 10)
4221                                 {
4222                                         o_ptr->to_h += 5;
4223                                         o_ptr->to_d -= 5;
4224                                 }
4225                                 o_ptr->to_h = MAX(o_ptr->to_h, 15);
4226                         }
4227                         
4228                         /* Accuracy ego must have high to_h */
4229                         if(o_ptr->name2 == EGO_VELOCITY)
4230                         {
4231                                 while(o_ptr->to_d < o_ptr->to_h + 10)
4232                                 {
4233                                         o_ptr->to_d += 5;
4234                                         o_ptr->to_h -= 5;
4235                                 }
4236                                 o_ptr->to_d = MAX(o_ptr->to_d, 15);
4237                         }
4238                         
4239                         /* Protection ego must have high to_a */
4240                         if((o_ptr->name2 == EGO_PROTECTION) || (o_ptr->name2 == EGO_S_PROTECTION) || (o_ptr->name2 == EGO_H_PROTECTION))
4241                         {
4242                                 o_ptr->to_a = MAX(o_ptr->to_a, 15);
4243                         }
4244
4245                         /* Hack -- obtain pval */
4246                         if (e_ptr->max_pval)
4247                         {
4248                                 if ((o_ptr->name2 == EGO_HA) && (have_flag(o_ptr->art_flags, TR_BLOWS)))
4249                                 {
4250                                         o_ptr->pval++;
4251                                         if ((lev > 60) && one_in_(3) && ((o_ptr->dd*(o_ptr->ds+1)) < 15)) o_ptr->pval++;
4252                                 }
4253                                 else if (o_ptr->name2 == EGO_DEMON)
4254                                 {
4255                                         if(have_flag(o_ptr->art_flags, TR_BLOWS))
4256                                         {
4257                                                 o_ptr->pval += randint1(2);
4258                                         }
4259                                         else
4260                                         {
4261                                                 o_ptr->pval += randint1(e_ptr->max_pval);
4262                                         }
4263                                 }
4264                                 else if (o_ptr->name2 == EGO_ATTACKS)
4265                                 {
4266                                         o_ptr->pval = randint1(e_ptr->max_pval*lev/100+1);
4267                                         if (o_ptr->pval > 3) o_ptr->pval = 3;
4268                                         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA))
4269                                                 o_ptr->pval += randint1(2);
4270                                 }
4271                                 else if (o_ptr->name2 == EGO_BAT)
4272                                 {
4273                                         o_ptr->pval = randint1(e_ptr->max_pval);
4274                                         if (o_ptr->sval == SV_ELVEN_CLOAK) o_ptr->pval += randint1(2);
4275                                 }
4276                                 else if (o_ptr->name2 == EGO_A_DEMON || o_ptr->name2 == EGO_DRUID || o_ptr->name2 == EGO_OLOG)
4277                                 {
4278                                         o_ptr->pval = randint1(e_ptr->max_pval);
4279                                 }
4280                                 else
4281                                 {
4282                                         o_ptr->pval += randint1(e_ptr->max_pval);
4283                                 }
4284                                 
4285                                 
4286                         }
4287                         if ((o_ptr->name2 == EGO_SPEED) && (lev < 50))
4288                         {
4289                                 o_ptr->pval = randint1(o_ptr->pval);
4290                         }
4291                         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA) && (o_ptr->pval > 2) && (o_ptr->name2 != EGO_ATTACKS))
4292                                 o_ptr->pval = 2;
4293                 }
4294                 
4295                 return;
4296         }
4297
4298         /* Examine real objects */
4299         if (o_ptr->k_idx)
4300         {
4301                 object_kind *k_ptr = &k_info[o_ptr->k_idx];
4302
4303                 /* Hack -- acquire "broken" flag */
4304                 if (!k_info[o_ptr->k_idx].cost) o_ptr->ident |= (IDENT_BROKEN);
4305
4306                 /* Hack -- acquire "cursed" flag */
4307                 if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
4308                 if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
4309                 if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
4310                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4311                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4312                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4313         }
4314
4315         
4316 }
4317
4318
4319 /*!
4320  * @brief ベースアイテムが上質として扱われるかどうかを返す。
4321  * Hack -- determine if a template is "good"
4322  * @param k_idx 判定したいベースアイテムのID
4323  * @return ベースアイテムが上質ならばTRUEを返す。
4324  */
4325 static bool kind_is_good(KIND_OBJECT_IDX k_idx)
4326 {
4327         object_kind *k_ptr = &k_info[k_idx];
4328
4329         /* Analyze the item type */
4330         switch (k_ptr->tval)
4331         {
4332                 /* Armor -- Good unless damaged */
4333                 case TV_HARD_ARMOR:
4334                 case TV_SOFT_ARMOR:
4335                 case TV_DRAG_ARMOR:
4336                 case TV_SHIELD:
4337                 case TV_CLOAK:
4338                 case TV_BOOTS:
4339                 case TV_GLOVES:
4340                 case TV_HELM:
4341                 case TV_CROWN:
4342                 {
4343                         if (k_ptr->to_a < 0) return (FALSE);
4344                         return (TRUE);
4345                 }
4346
4347                 /* Weapons -- Good unless damaged */
4348                 case TV_BOW:
4349                 case TV_SWORD:
4350                 case TV_HAFTED:
4351                 case TV_POLEARM:
4352                 case TV_DIGGING:
4353                 {
4354                         if (k_ptr->to_h < 0) return (FALSE);
4355                         if (k_ptr->to_d < 0) return (FALSE);
4356                         return (TRUE);
4357                 }
4358
4359                 /* Ammo -- Arrows/Bolts are good */
4360                 case TV_BOLT:
4361                 case TV_ARROW:
4362                 {
4363                         return (TRUE);
4364                 }
4365
4366                 /* Books -- High level books are good (except Arcane books) */
4367                 case TV_LIFE_BOOK:
4368                 case TV_SORCERY_BOOK:
4369                 case TV_NATURE_BOOK:
4370                 case TV_CHAOS_BOOK:
4371                 case TV_DEATH_BOOK:
4372                 case TV_TRUMP_BOOK:
4373                 case TV_CRAFT_BOOK:
4374                 case TV_DAEMON_BOOK:
4375                 case TV_CRUSADE_BOOK:
4376                 case TV_MUSIC_BOOK:
4377                 case TV_HISSATSU_BOOK:
4378                 case TV_HEX_BOOK:
4379                 {
4380                         if (k_ptr->sval >= SV_BOOK_MIN_GOOD) return (TRUE);
4381                         return (FALSE);
4382                 }
4383
4384                 /* Rings -- Rings of Speed are good */
4385                 case TV_RING:
4386                 {
4387                         if (k_ptr->sval == SV_RING_SPEED) return (TRUE);
4388                         if (k_ptr->sval == SV_RING_LORDLY) return (TRUE);
4389                         return (FALSE);
4390                 }
4391
4392                 /* Amulets -- Amulets of the Magi and Resistance are good */
4393                 case TV_AMULET:
4394                 {
4395                         if (k_ptr->sval == SV_AMULET_THE_MAGI) return (TRUE);
4396                         if (k_ptr->sval == SV_AMULET_RESISTANCE) return (TRUE);
4397                         return (FALSE);
4398                 }
4399         }
4400
4401         /* Assume not good */
4402         return (FALSE);
4403 }
4404
4405 /*!
4406  * @brief 生成階に応じたベースアイテムの生成を行う。
4407  * Attempt to make an object (normal or good/great)
4408  * @param j_ptr 生成結果を収めたいオブジェクト構造体の参照ポインタ
4409  * @param mode オプションフラグ
4410  * @return 生成に成功したらTRUEを返す。
4411  * @details
4412  * This routine plays nasty games to generate the "special artifacts".\n
4413  * This routine uses "current_floor_ptr->object_level" for the "generation level".\n
4414  * We assume that the given object has been "wiped".\n
4415  */
4416 bool make_object(object_type *j_ptr, BIT_FLAGS mode)
4417 {
4418         PERCENTAGE prob;
4419         DEPTH base;
4420
4421
4422         /* Chance of "special object" */
4423         prob = ((mode & AM_GOOD) ? 10 : 1000);
4424
4425         /* Base level for the object */
4426         base = ((mode & AM_GOOD) ? (current_floor_ptr->object_level + 10) : current_floor_ptr->object_level);
4427
4428
4429         /* Generate a special object, or a normal object */
4430         if (!one_in_(prob) || !make_artifact_special(j_ptr))
4431         {
4432                 KIND_OBJECT_IDX k_idx;
4433
4434                 /* Good objects */
4435                 if ((mode & AM_GOOD) && !get_obj_num_hook)
4436                 {
4437                         /* Activate restriction (if already specified, use that) */
4438                         get_obj_num_hook = kind_is_good;
4439                 }
4440
4441                 /* Restricted objects - prepare allocation table */
4442                 if (get_obj_num_hook) get_obj_num_prep();
4443
4444                 /* Pick a random object */
4445                 k_idx = get_obj_num(base);
4446
4447                 /* Restricted objects */
4448                 if (get_obj_num_hook)
4449                 {
4450                         /* Clear restriction */
4451                         get_obj_num_hook = NULL;
4452
4453                         /* Reset allocation table to default */
4454                         get_obj_num_prep();
4455                 }
4456
4457                 /* Handle failure */
4458                 if (!k_idx) return (FALSE);
4459
4460                 /* Prepare the object */
4461                 object_prep(j_ptr, k_idx);
4462         }
4463
4464         /* Apply magic (allow artifacts) */
4465         apply_magic(j_ptr, current_floor_ptr->object_level, mode);
4466
4467         /* Hack -- generate multiple spikes/missiles */
4468         switch (j_ptr->tval)
4469         {
4470                 case TV_SPIKE:
4471                 case TV_SHOT:
4472                 case TV_ARROW:
4473                 case TV_BOLT:
4474                 {
4475                         if (!j_ptr->name1)
4476                                 j_ptr->number = (byte)damroll(6, 7);
4477                 }
4478         }
4479
4480         if (cheat_peek) object_mention(j_ptr);
4481
4482         /* Success */
4483         return (TRUE);
4484 }
4485
4486
4487 /*!
4488  * @brief フロアの指定位置に生成階に応じたベースアイテムの生成を行う。
4489  * Attempt to place an object (normal or good/great) at the given location.
4490  * @param y 配置したいフロアのY座標
4491  * @param x 配置したいフロアのX座標
4492  * @param mode オプションフラグ
4493  * @return 生成に成功したらTRUEを返す。
4494  * @details
4495  * This routine plays nasty games to generate the "special artifacts".\n
4496  * This routine uses "current_floor_ptr->object_level" for the "generation level".\n
4497  * This routine requires a clean floor grid destination.\n
4498  */
4499 void place_object(POSITION y, POSITION x, BIT_FLAGS mode)
4500 {
4501         OBJECT_IDX o_idx;
4502
4503         /* Acquire grid */
4504         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
4505
4506         object_type forge;
4507         object_type *q_ptr;
4508
4509
4510         /* Paranoia -- check bounds */
4511         if (!in_bounds(y, x)) return;
4512
4513         /* Require floor space */
4514         if (!cave_drop_bold(y, x)) return;
4515
4516         /* Avoid stacking on other objects */
4517         if (g_ptr->o_idx) return;
4518
4519         q_ptr = &forge;
4520         object_wipe(q_ptr);
4521
4522         /* Make an object (if possible) */
4523         if (!make_object(q_ptr, mode)) return;
4524
4525
4526         /* Make an object */
4527         o_idx = o_pop();
4528
4529         /* Success */
4530         if (o_idx)
4531         {
4532                 object_type *o_ptr;
4533                 o_ptr = &current_floor_ptr->o_list[o_idx];
4534
4535                 /* Structure Copy */
4536                 object_copy(o_ptr, q_ptr);
4537
4538                 o_ptr->iy = y;
4539                 o_ptr->ix = x;
4540
4541                 /* Build a stack */
4542                 o_ptr->next_o_idx = g_ptr->o_idx;
4543
4544                 /* Place the object */
4545                 g_ptr->o_idx = o_idx;
4546
4547                 note_spot(y, x);
4548
4549                 lite_spot(y, x);
4550         }
4551         else
4552         {
4553                 /* Hack -- Preserve artifacts */
4554                 if (object_is_fixed_artifact(q_ptr))
4555                 {
4556                         a_info[q_ptr->name1].cur_num = 0;
4557                 }
4558         }
4559 }
4560
4561
4562 /*!
4563  * @brief 生成階に応じた財宝オブジェクトの生成を行う。
4564  * Make a treasure object
4565  * @param j_ptr 生成結果を収めたいオブジェクト構造体の参照ポインタ
4566  * @return 生成に成功したらTRUEを返す。
4567  * @details
4568  * The location must be a legal, clean, floor grid.
4569  */
4570 bool make_gold(object_type *j_ptr)
4571 {
4572         int i;
4573         s32b base;
4574
4575         /* Hack -- Pick a Treasure variety */
4576         i = ((randint1(current_floor_ptr->object_level + 2) + 2) / 2) - 1;
4577
4578         /* Apply "extra" magic */
4579         if (one_in_(GREAT_OBJ))
4580         {
4581                 i += randint1(current_floor_ptr->object_level + 1);
4582         }
4583
4584         /* Hack -- Creeping Coins only generate "themselves" */
4585         if (coin_type) i = coin_type;
4586
4587         /* Do not create "illegal" Treasure Types */
4588         if (i >= MAX_GOLD) i = MAX_GOLD - 1;
4589
4590         /* Prepare a gold object */
4591         object_prep(j_ptr, OBJ_GOLD_LIST + i);
4592
4593         /* Hack -- Base coin cost */
4594         base = k_info[OBJ_GOLD_LIST + i].cost;
4595
4596         /* Determine how much the treasure is "worth" */
4597         j_ptr->pval = (base + (8L * randint1(base)) + randint1(8));
4598
4599         /* Success */
4600         return (TRUE);
4601 }
4602
4603
4604 /*!
4605  * @brief フロアの指定位置に生成階に応じた財宝オブジェクトの生成を行う。
4606  * Places a treasure (Gold or Gems) at given location
4607  * @param y 配置したいフロアのY座標
4608  * @param x 配置したいフロアのX座標
4609  * @return 生成に成功したらTRUEを返す。
4610  * @details
4611  * The location must be a legal, clean, floor grid.
4612  */
4613 void place_gold(POSITION y, POSITION x)
4614 {
4615         OBJECT_IDX o_idx;
4616
4617         /* Acquire grid */
4618         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
4619
4620         object_type forge;
4621         object_type *q_ptr;
4622
4623
4624         /* Paranoia -- check bounds */
4625         if (!in_bounds(y, x)) return;
4626
4627         /* Require floor space */
4628         if (!cave_drop_bold(y, x)) return;
4629
4630         /* Avoid stacking on other objects */
4631         if (g_ptr->o_idx) return;
4632
4633         q_ptr = &forge;
4634         object_wipe(q_ptr);
4635
4636         /* Make some gold */
4637         if (!make_gold(q_ptr)) return;
4638
4639         /* Make an object */
4640         o_idx = o_pop();
4641
4642         /* Success */
4643         if (o_idx)
4644         {
4645                 object_type *o_ptr;
4646                 o_ptr = &current_floor_ptr->o_list[o_idx];
4647
4648                 /* Copy the object */
4649                 object_copy(o_ptr, q_ptr);
4650
4651                 /* Save location */
4652                 o_ptr->iy = y;
4653                 o_ptr->ix = x;
4654
4655                 /* Build a stack */
4656                 o_ptr->next_o_idx = g_ptr->o_idx;
4657
4658                 /* Place the object */
4659                 g_ptr->o_idx = o_idx;
4660
4661                 note_spot(y, x);
4662
4663                 lite_spot(y, x);
4664         }
4665 }
4666
4667
4668 /*!
4669  * @brief 生成済のオブジェクトをフロアの所定の位置に落とす。
4670  * Let an object fall to the ground at or near a location.
4671  * @param j_ptr 落としたいオブジェクト構造体の参照ポインタ
4672  * @param chance ドロップの成功率(%)
4673  * @param y 配置したいフロアのY座標
4674  * @param x 配置したいフロアのX座標
4675  * @return 生成に成功したらオブジェクトのIDを返す。
4676  * @details
4677  * The initial location is assumed to be "in_bounds()".\n
4678  *\n
4679  * This function takes a parameter "chance".  This is the percentage\n
4680  * chance that the item will "disappear" instead of drop.  If the object\n
4681  * has been thrown, then this is the chance of disappearance on contact.\n
4682  *\n
4683  * Hack -- this function uses "chance" to determine if it should produce\n
4684  * some form of "description" of the drop event (under the player).\n
4685  *\n
4686  * We check several locations to see if we can find a location at which\n
4687  * the object can combine, stack, or be placed.  Artifacts will try very\n
4688  * hard to be placed, including "teleporting" to a useful grid if needed.\n
4689  */
4690 OBJECT_IDX drop_near(object_type *j_ptr, PERCENTAGE chance, POSITION y, POSITION x)
4691 {
4692         int i, k, d, s;
4693
4694         int bs, bn;
4695         POSITION by, bx;
4696         POSITION dy, dx;
4697         POSITION ty, tx = 0;
4698
4699         OBJECT_IDX o_idx = 0;
4700         OBJECT_IDX this_o_idx, next_o_idx = 0;
4701
4702         grid_type *g_ptr;
4703
4704         GAME_TEXT o_name[MAX_NLEN];
4705
4706         bool flag = FALSE;
4707         bool done = FALSE;
4708
4709 #ifndef JP
4710         /* Extract plural */
4711         bool plural = (j_ptr->number != 1);
4712 #endif
4713
4714         /* Describe object */
4715         object_desc(o_name, j_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
4716
4717
4718         /* Handle normal "breakage" */
4719         if (!object_is_artifact(j_ptr) && (randint0(100) < chance))
4720         {
4721 #ifdef JP
4722                 msg_format("%sは消えた。", o_name);
4723 #else
4724                 msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
4725 #endif
4726                 if (p_ptr->wizard) msg_print(_("(破損)", "(breakage)"));
4727
4728                 /* Failure */
4729                 return (0);
4730         }
4731
4732
4733         /* Score */
4734         bs = -1;
4735
4736         /* Picker */
4737         bn = 0;
4738
4739         /* Default */
4740         by = y;
4741         bx = x;
4742
4743         /* Scan local grids */
4744         for (dy = -3; dy <= 3; dy++)
4745         {
4746                 /* Scan local grids */
4747                 for (dx = -3; dx <= 3; dx++)
4748                 {
4749                         bool comb = FALSE;
4750
4751                         /* Calculate actual distance */
4752                         d = (dy * dy) + (dx * dx);
4753
4754                         /* Ignore distant grids */
4755                         if (d > 10) continue;
4756
4757                         ty = y + dy;
4758                         tx = x + dx;
4759
4760                         /* Skip illegal grids */
4761                         if (!in_bounds(ty, tx)) continue;
4762
4763                         /* Require line of projection */
4764                         if (!projectable(y, x, ty, tx)) continue;
4765
4766                         /* Obtain grid */
4767                         g_ptr = &current_floor_ptr->grid_array[ty][tx];
4768
4769                         /* Require floor space */
4770                         if (!cave_drop_bold(ty, tx)) continue;
4771
4772                         /* No objects */
4773                         k = 0;
4774
4775                         /* Scan objects in that grid */
4776                         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
4777                         {
4778                                 object_type *o_ptr;
4779                                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
4780                                 next_o_idx = o_ptr->next_o_idx;
4781
4782                                 /* Check for possible combination */
4783                                 if (object_similar(o_ptr, j_ptr)) comb = TRUE;
4784
4785                                 /* Count objects */
4786                                 k++;
4787                         }
4788
4789                         /* Add new object */
4790                         if (!comb) k++;
4791
4792                         /* Paranoia */
4793                         if (k > 99) continue;
4794
4795                         /* Calculate score */
4796                         s = 1000 - (d + k * 5);
4797
4798                         /* Skip bad values */
4799                         if (s < bs) continue;
4800
4801                         /* New best value */
4802                         if (s > bs) bn = 0;
4803
4804                         /* Apply the randomizer to equivalent values */
4805                         if ((++bn >= 2) && !one_in_(bn)) continue;
4806
4807                         /* Keep score */
4808                         bs = s;
4809
4810                         /* Track it */
4811                         by = ty;
4812                         bx = tx;
4813
4814                         flag = TRUE;
4815                 }
4816         }
4817
4818
4819         /* Handle lack of space */
4820         if (!flag && !object_is_artifact(j_ptr))
4821         {
4822 #ifdef JP
4823                 msg_format("%sは消えた。", o_name);
4824 #else
4825                 msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
4826 #endif
4827
4828                 if (p_ptr->wizard) msg_print(_("(床スペースがない)", "(no floor space)"));
4829
4830                 /* Failure */
4831                 return (0);
4832         }
4833
4834
4835         /* Find a grid */
4836         for (i = 0; !flag && (i < 1000); i++)
4837         {
4838                 /* Bounce around */
4839                 ty = rand_spread(by, 1);
4840                 tx = rand_spread(bx, 1);
4841
4842                 /* Verify location */
4843                 if (!in_bounds(ty, tx)) continue;
4844
4845                 /* Bounce to that location */
4846                 by = ty;
4847                 bx = tx;
4848
4849                 /* Require floor space */
4850                 if (!cave_drop_bold(by, bx)) continue;
4851
4852                 flag = TRUE;
4853         }
4854
4855
4856         if (!flag)
4857         {
4858                 int candidates = 0, pick;
4859
4860                 for (ty = 1; ty < current_floor_ptr->height - 1; ty++)
4861                 {
4862                         for (tx = 1; tx < current_floor_ptr->width - 1; tx++)
4863                         {
4864                                 /* A valid space found */
4865                                 if (cave_drop_bold(ty, tx)) candidates++;
4866                         }
4867                 }
4868
4869                 /* No valid place! */
4870                 if (!candidates)
4871                 {
4872 #ifdef JP
4873                         msg_format("%sは消えた。", o_name);
4874 #else
4875                         msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
4876 #endif
4877
4878                         if (p_ptr->wizard) msg_print(_("(床スペースがない)", "(no floor space)"));
4879
4880                         /* Mega-Hack -- preserve artifacts */
4881                         if (preserve_mode)
4882                         {
4883                                 /* Hack -- Preserve unknown artifacts */
4884                                 if (object_is_fixed_artifact(j_ptr) && !object_is_known(j_ptr))
4885                                 {
4886                                         /* Mega-Hack -- Preserve the artifact */
4887                                         a_info[j_ptr->name1].cur_num = 0;
4888                                 }
4889                         }
4890
4891                         /* Failure */
4892                         return 0;
4893                 }
4894
4895                 /* Choose a random one */
4896                 pick = randint1(candidates);
4897
4898                 for (ty = 1; ty < current_floor_ptr->height - 1; ty++)
4899                 {
4900                         for (tx = 1; tx < current_floor_ptr->width - 1; tx++)
4901                         {
4902                                 if (cave_drop_bold(ty, tx))
4903                                 {
4904                                         pick--;
4905
4906                                         /* Is this a picked one? */
4907                                         if (!pick) break;
4908                                 }
4909                         }
4910
4911                         if (!pick) break;
4912                 }
4913
4914                 by = ty;
4915                 bx = tx;
4916         }
4917
4918
4919         g_ptr = &current_floor_ptr->grid_array[by][bx];
4920
4921         /* Scan objects in that grid for combination */
4922         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
4923         {
4924                 object_type *o_ptr;
4925                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
4926                 next_o_idx = o_ptr->next_o_idx;
4927
4928                 /* Check for combination */
4929                 if (object_similar(o_ptr, j_ptr))
4930                 {
4931                         object_absorb(o_ptr, j_ptr);
4932
4933                         /* Success */
4934                         done = TRUE;
4935
4936                         break;
4937                 }
4938         }
4939
4940         if (!done) o_idx = o_pop();
4941
4942         /* Failure */
4943         if (!done && !o_idx)
4944         {
4945 #ifdef JP
4946                 msg_format("%sは消えた。", o_name);
4947 #else
4948                 msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
4949 #endif
4950
4951                 if (p_ptr->wizard) msg_print(_("(アイテムが多過ぎる)", "(too many objects)"));
4952
4953                 /* Hack -- Preserve artifacts */
4954                 if (object_is_fixed_artifact(j_ptr))
4955                 {
4956                         a_info[j_ptr->name1].cur_num = 0;
4957                 }
4958
4959                 /* Failure */
4960                 return (0);
4961         }
4962
4963         /* Stack */
4964         if (!done)
4965         {
4966                 /* Structure copy */
4967                 object_copy(&current_floor_ptr->o_list[o_idx], j_ptr);
4968
4969                 /* Access new object */
4970                 j_ptr = &current_floor_ptr->o_list[o_idx];
4971
4972                 /* Locate */
4973                 j_ptr->iy = by;
4974                 j_ptr->ix = bx;
4975
4976                 /* No monster */
4977                 j_ptr->held_m_idx = 0;
4978
4979                 /* Build a stack */
4980                 j_ptr->next_o_idx = g_ptr->o_idx;
4981
4982                 /* Place the object */
4983                 g_ptr->o_idx = o_idx;
4984
4985                 /* Success */
4986                 done = TRUE;
4987         }
4988
4989         note_spot(by, bx);
4990         lite_spot(by, bx);
4991         sound(SOUND_DROP);
4992
4993         /* Mega-Hack -- no message if "dropped" by player */
4994         /* Message when an object falls under the player */
4995         if (chance && player_bold(by, bx))
4996         {
4997                 msg_print(_("何かが足下に転がってきた。", "You feel something roll beneath your feet."));
4998         }
4999
5000         return (o_idx);
5001 }
5002
5003 /*!
5004  * @brief 魔道具の使用回数の残量を示すメッセージを表示する /
5005  * Describe the charges on an item in the inventory.
5006  * @param item 残量を表示したいプレイヤーのアイテム所持スロット
5007  * @return なし
5008  */
5009 void inven_item_charges(INVENTORY_IDX item)
5010 {
5011         object_type *o_ptr = &inventory[item];
5012
5013         /* Require staff/wand */
5014         if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
5015
5016         /* Require known item */
5017         if (!object_is_known(o_ptr)) return;
5018
5019 #ifdef JP
5020         if (o_ptr->pval <= 0)
5021         {
5022                 msg_print("もう魔力が残っていない。");
5023         }
5024         else
5025         {
5026                 msg_format("あと %d 回分の魔力が残っている。", o_ptr->pval);
5027         }
5028 #else
5029         /* Multiple charges */
5030         if (o_ptr->pval != 1)
5031         {
5032                 msg_format("You have %d charges remaining.", o_ptr->pval);
5033         }
5034
5035         /* Single charge */
5036         else
5037         {
5038                 msg_format("You have %d charge remaining.", o_ptr->pval);
5039         }
5040 #endif
5041
5042 }
5043
5044 /*!
5045  * @brief アイテムの残り所持数メッセージを表示する /
5046  * Describe an item in the inventory.
5047  * @param item 残量を表示したいプレイヤーのアイテム所持スロット
5048  * @return なし
5049  */
5050 void inven_item_describe(INVENTORY_IDX item)
5051 {
5052         object_type *o_ptr = &inventory[item];
5053         GAME_TEXT o_name[MAX_NLEN];
5054
5055         object_desc(o_name, o_ptr, 0);
5056
5057 #ifdef JP
5058         /* "no more" の場合はこちらで表示する */
5059         if (o_ptr->number <= 0)
5060         {
5061                 /*FIRST*//*ここはもう通らないかも */
5062                 msg_format("もう%sを持っていない。", o_name);
5063         }
5064         else
5065         {
5066                 /* アイテム名を英日切り替え機能対応 */
5067                 msg_format("まだ %sを持っている。", o_name);
5068         }
5069 #else
5070         msg_format("You have %s.", o_name);
5071 #endif
5072
5073 }
5074
5075 /*!
5076  * @brief アイテムを増減させ残り所持数メッセージを表示する /
5077  * Increase the "number" of an item in the inventory
5078  * @param item 所持数を増やしたいプレイヤーのアイテム所持スロット
5079  * @param num 増やしたい量
5080  * @return なし
5081  */
5082 void inven_item_increase(INVENTORY_IDX item, ITEM_NUMBER num)
5083 {
5084         object_type *o_ptr = &inventory[item];
5085
5086         /* Apply */
5087         num += o_ptr->number;
5088
5089         /* Bounds check */
5090         if (num > 255) num = 255;
5091         else if (num < 0) num = 0;
5092
5093         /* Un-apply */
5094         num -= o_ptr->number;
5095
5096         /* Change the number and weight */
5097         if (num)
5098         {
5099                 /* Add the number */
5100                 o_ptr->number += num;
5101
5102                 /* Add the weight */
5103                 p_ptr->total_weight += (num * o_ptr->weight);
5104                 p_ptr->update |= (PU_BONUS);
5105                 p_ptr->update |= (PU_MANA);
5106                 p_ptr->update |= (PU_COMBINE);
5107                 p_ptr->window |= (PW_INVEN | PW_EQUIP);
5108
5109                 /* Hack -- Clear temporary elemental brands if player takes off weapons */
5110                 if (!o_ptr->number && p_ptr->ele_attack)
5111                 {
5112                         if ((item == INVEN_RARM) || (item == INVEN_LARM))
5113                         {
5114                                 if (!has_melee_weapon(INVEN_RARM + INVEN_LARM - item))
5115                                 {
5116                                         /* Clear all temporary elemental brands */
5117                                         set_ele_attack(0, 0);
5118                                 }
5119                         }
5120                 }
5121         }
5122 }
5123
5124 /*!
5125  * @brief 所持アイテムスロットから所持数のなくなったアイテムを消去する /
5126  * Erase an inventory slot if it has no more items
5127  * @param item 消去したいプレイヤーのアイテム所持スロット
5128  * @return なし
5129  */
5130 void inven_item_optimize(INVENTORY_IDX item)
5131 {
5132         object_type *o_ptr = &inventory[item];
5133
5134         /* Only optimize real items */
5135         if (!o_ptr->k_idx) return;
5136
5137         /* Only optimize empty items */
5138         if (o_ptr->number) return;
5139
5140         /* The item is in the pack */
5141         if (item < INVEN_RARM)
5142         {
5143                 int i;
5144
5145                 /* One less item */
5146                 inven_cnt--;
5147
5148                 /* Slide everything down */
5149                 for (i = item; i < INVEN_PACK; i++)
5150                 {
5151                         /* Structure copy */
5152                         inventory[i] = inventory[i+1];
5153                 }
5154
5155                 /* Erase the "final" slot */
5156                 object_wipe(&inventory[i]);
5157
5158                 p_ptr->window |= (PW_INVEN);
5159         }
5160
5161         /* The item is being wielded */
5162         else
5163         {
5164                 /* One less item */
5165                 equip_cnt--;
5166
5167                 /* Erase the empty slot */
5168                 object_wipe(&inventory[item]);
5169                 p_ptr->update |= (PU_BONUS);
5170                 p_ptr->update |= (PU_TORCH);
5171                 p_ptr->update |= (PU_MANA);
5172
5173                 p_ptr->window |= (PW_EQUIP);
5174         }
5175
5176         p_ptr->window |= (PW_SPELL);
5177 }
5178
5179 /*!
5180  * @brief 床上の魔道具の残り残量メッセージを表示する /
5181  * Describe the charges on an item on the floor.
5182  * @param item メッセージの対象にしたいアイテム所持スロット
5183  * @return なし
5184  */
5185 void floor_item_charges(INVENTORY_IDX item)
5186 {
5187         object_type *o_ptr = &current_floor_ptr->o_list[item];
5188
5189         /* Require staff/wand */
5190         if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
5191
5192         /* Require known item */
5193         if (!object_is_known(o_ptr)) return;
5194
5195 #ifdef JP
5196         if (o_ptr->pval <= 0)
5197         {
5198                 msg_print("この床上のアイテムは、もう魔力が残っていない。");
5199         }
5200         else
5201         {
5202                 msg_format("この床上のアイテムは、あと %d 回分の魔力が残っている。", o_ptr->pval);
5203         }
5204 #else
5205         /* Multiple charges */
5206         if (o_ptr->pval != 1)
5207         {
5208                 msg_format("There are %d charges remaining.", o_ptr->pval);
5209         }
5210
5211         /* Single charge */
5212         else
5213         {
5214                 msg_format("There is %d charge remaining.", o_ptr->pval);
5215         }
5216 #endif
5217
5218 }
5219
5220 /*!
5221  * @brief 床上のアイテムの残り数メッセージを表示する /
5222  * Describe the charges on an item on the floor.
5223  * @param item メッセージの対象にしたいアイテム所持スロット
5224  * @return なし
5225  */
5226 void floor_item_describe(INVENTORY_IDX item)
5227 {
5228         object_type *o_ptr = &current_floor_ptr->o_list[item];
5229         GAME_TEXT o_name[MAX_NLEN];
5230
5231         object_desc(o_name, o_ptr, 0);
5232
5233 #ifdef JP
5234         /* "no more" の場合はこちらで表示を分ける */
5235         if (o_ptr->number <= 0)
5236         {
5237                 msg_format("床上には、もう%sはない。", o_name);
5238         }
5239         else
5240         {
5241                 msg_format("床上には、まだ %sがある。", o_name);
5242         }
5243 #else
5244         msg_format("You see %s.", o_name);
5245 #endif
5246
5247 }
5248
5249
5250 /*!
5251  * @brief 床上のアイテムの数を増やす /
5252  * Increase the "number" of an item on the floor
5253  * @param item 増やしたいアイテムの所持スロット
5254  * @param num 増やしたいアイテムの数
5255  * @return なし
5256  */
5257 void floor_item_increase(INVENTORY_IDX item, ITEM_NUMBER num)
5258 {
5259         object_type *o_ptr = &current_floor_ptr->o_list[item];
5260
5261         /* Apply */
5262         num += o_ptr->number;
5263
5264         /* Bounds check */
5265         if (num > 255) num = 255;
5266         else if (num < 0) num = 0;
5267
5268         /* Un-apply */
5269         num -=  o_ptr->number;
5270
5271         /* Change the number */
5272         o_ptr->number += num;
5273 }
5274
5275
5276 /*!
5277  * @brief 床上の数の無くなったアイテムスロットを消去する /
5278  * Optimize an item on the floor (destroy "empty" items)
5279  * @param item 消去したいアイテムの所持スロット
5280  * @return なし
5281  */
5282 void floor_item_optimize(INVENTORY_IDX item)
5283 {
5284         object_type *o_ptr = &current_floor_ptr->o_list[item];
5285
5286         /* Paranoia -- be sure it exists */
5287         if (!o_ptr->k_idx) return;
5288
5289         /* Only optimize empty items */
5290         if (o_ptr->number) return;
5291
5292         delete_object_idx(item);
5293 }
5294
5295
5296 /*!
5297  * @brief アイテムを拾う際にザックから溢れずに済むかを判定する /
5298  * Check if we have space for an item in the pack without overflow
5299  * @param o_ptr 拾いたいオブジェクトの構造体参照ポインタ
5300  * @return 溢れずに済むならTRUEを返す
5301  */
5302 bool inven_carry_okay(object_type *o_ptr)
5303 {
5304         int j;
5305
5306         /* Empty slot? */
5307         if (inven_cnt < INVEN_PACK) return (TRUE);
5308
5309         /* Similar slot? */
5310         for (j = 0; j < INVEN_PACK; j++)
5311         {
5312                 object_type *j_ptr = &inventory[j];
5313
5314                 /* Skip non-objects */
5315                 if (!j_ptr->k_idx) continue;
5316
5317                 /* Check if the two items can be combined */
5318                 if (object_similar(j_ptr, o_ptr)) return (TRUE);
5319         }
5320
5321         return (FALSE);
5322 }
5323
5324 /*!
5325  * @brief オブジェクトを定義された基準に従いソートするための関数 /
5326  * Check if we have space for an item in the pack without overflow
5327  * @param o_ptr 比較対象オブジェクトの構造体参照ポインタ1
5328  * @param o_value o_ptrのアイテム価値(手動であらかじめ代入する必要がある?)
5329  * @param j_ptr 比較対象オブジェクトの構造体参照ポインタ2
5330  * @return o_ptrの方が上位ならばTRUEを返す。
5331  */
5332 bool object_sort_comp(object_type *o_ptr, s32b o_value, object_type *j_ptr)
5333 {
5334         int o_type, j_type;
5335
5336         /* Use empty slots */
5337         if (!j_ptr->k_idx) return TRUE;
5338
5339         /* Hack -- readable books always come first */
5340         if ((o_ptr->tval == REALM1_BOOK) &&
5341             (j_ptr->tval != REALM1_BOOK)) return TRUE;
5342         if ((j_ptr->tval == REALM1_BOOK) &&
5343             (o_ptr->tval != REALM1_BOOK)) return FALSE;
5344
5345         if ((o_ptr->tval == REALM2_BOOK) &&
5346             (j_ptr->tval != REALM2_BOOK)) return TRUE;
5347         if ((j_ptr->tval == REALM2_BOOK) &&
5348             (o_ptr->tval != REALM2_BOOK)) return FALSE;
5349
5350         /* Objects sort by decreasing type */
5351         if (o_ptr->tval > j_ptr->tval) return TRUE;
5352         if (o_ptr->tval < j_ptr->tval) return FALSE;
5353
5354         /* Non-aware (flavored) items always come last */
5355         /* Can happen in the home */
5356         if (!object_is_aware(o_ptr)) return FALSE;
5357         if (!object_is_aware(j_ptr)) return TRUE;
5358
5359         /* Objects sort by increasing sval */
5360         if (o_ptr->sval < j_ptr->sval) return TRUE;
5361         if (o_ptr->sval > j_ptr->sval) return FALSE;
5362
5363         /* Unidentified objects always come last */
5364         /* Objects in the home can be unknown */
5365         if (!object_is_known(o_ptr)) return FALSE;
5366         if (!object_is_known(j_ptr)) return TRUE;
5367
5368         /* Fixed artifacts, random artifacts and ego items */
5369         if (object_is_fixed_artifact(o_ptr)) o_type = 3;
5370         else if (o_ptr->art_name) o_type = 2;
5371         else if (object_is_ego(o_ptr)) o_type = 1;
5372         else o_type = 0;
5373
5374         if (object_is_fixed_artifact(j_ptr)) j_type = 3;
5375         else if (j_ptr->art_name) j_type = 2;
5376         else if (object_is_ego(j_ptr)) j_type = 1;
5377         else j_type = 0;
5378
5379         if (o_type < j_type) return TRUE;
5380         if (o_type > j_type) return FALSE;
5381
5382         switch (o_ptr->tval)
5383         {
5384         case TV_FIGURINE:
5385         case TV_STATUE:
5386         case TV_CORPSE:
5387         case TV_CAPTURE:
5388                 if (r_info[o_ptr->pval].level < r_info[j_ptr->pval].level) return TRUE;
5389                 if ((r_info[o_ptr->pval].level == r_info[j_ptr->pval].level) && (o_ptr->pval < j_ptr->pval)) return TRUE;
5390                 return FALSE;
5391
5392         case TV_SHOT:
5393         case TV_ARROW:
5394         case TV_BOLT:
5395                 /* Objects sort by increasing hit/damage bonuses */
5396                 if (o_ptr->to_h + o_ptr->to_d < j_ptr->to_h + j_ptr->to_d) return TRUE;
5397                 if (o_ptr->to_h + o_ptr->to_d > j_ptr->to_h + j_ptr->to_d) return FALSE;
5398                 break;
5399
5400         /* Hack:  otherwise identical rods sort by
5401         increasing recharge time --dsb */
5402         case TV_ROD:
5403                 if (o_ptr->pval < j_ptr->pval) return TRUE;
5404                 if (o_ptr->pval > j_ptr->pval) return FALSE;
5405                 break;
5406         }
5407
5408         /* Objects sort by decreasing value */
5409         return o_value > object_value(j_ptr);
5410 }
5411
5412
5413 /*!
5414  * @brief オブジェクトをプレイヤーが拾って所持スロットに納めるメインルーチン /
5415  * Add an item to the players inventory, and return the slot used.
5416  * @param o_ptr 拾うオブジェクトの構造体参照ポインタ
5417  * @return 収められた所持スロットのID、拾うことができなかった場合-1を返す。
5418  * @details
5419  * If the new item can combine with an existing item in the inventory,\n
5420  * it will do so, using "object_similar()" and "object_absorb()", else,\n
5421  * the item will be placed into the "proper" location in the inventory.\n
5422  *\n
5423  * This function can be used to "over-fill" the player's pack, but only\n
5424  * once, and such an action must trigger the "overflow" code immediately.\n
5425  * Note that when the pack is being "over-filled", the new item must be\n
5426  * placed into the "overflow" slot, and the "overflow" must take place\n
5427  * before the pack is reordered, but (optionally) after the pack is\n
5428  * combined.  This may be tricky.  See "dungeon.c" for info.\n
5429  *\n
5430  * Note that this code must remove any location/stack information\n
5431  * from the object once it is placed into the inventory.\n
5432  */
5433 s16b inven_carry(object_type *o_ptr)
5434 {
5435         INVENTORY_IDX i, j, k;
5436         INVENTORY_IDX n = -1;
5437
5438         object_type *j_ptr;
5439
5440
5441         /* Check for combining */
5442         for (j = 0; j < INVEN_PACK; j++)
5443         {
5444                 j_ptr = &inventory[j];
5445
5446                 /* Skip non-objects */
5447                 if (!j_ptr->k_idx) continue;
5448
5449                 /* Hack -- track last item */
5450                 n = j;
5451
5452                 /* Check if the two items can be combined */
5453                 if (object_similar(j_ptr, o_ptr))
5454                 {
5455                         object_absorb(j_ptr, o_ptr);
5456
5457                         p_ptr->total_weight += (o_ptr->number * o_ptr->weight);
5458                         p_ptr->update |= (PU_BONUS);
5459                         p_ptr->window |= (PW_INVEN);
5460
5461                         /* Success */
5462                         return (j);
5463                 }
5464         }
5465
5466
5467         /* Paranoia */
5468         if (inven_cnt > INVEN_PACK) return (-1);
5469
5470         /* Find an empty slot */
5471         for (j = 0; j <= INVEN_PACK; j++)
5472         {
5473                 j_ptr = &inventory[j];
5474
5475                 /* Use it if found */
5476                 if (!j_ptr->k_idx) break;
5477         }
5478
5479         /* Use that slot */
5480         i = j;
5481
5482
5483         /* Reorder the pack */
5484         if (i < INVEN_PACK)
5485         {
5486                 /* Get the "value" of the item */
5487                 s32b o_value = object_value(o_ptr);
5488
5489                 /* Scan every occupied slot */
5490                 for (j = 0; j < INVEN_PACK; j++)
5491                 {
5492                         if (object_sort_comp(o_ptr, o_value, &inventory[j])) break;
5493                 }
5494
5495                 /* Use that slot */
5496                 i = j;
5497
5498                 /* Slide objects */
5499                 for (k = n; k >= i; k--)
5500                 {
5501                         /* Hack -- Slide the item */
5502                         object_copy(&inventory[k+1], &inventory[k]);
5503                 }
5504
5505                 /* Wipe the empty slot */
5506                 object_wipe(&inventory[i]);
5507         }
5508
5509
5510         /* Copy the item */
5511         object_copy(&inventory[i], o_ptr);
5512
5513         /* Access new object */
5514         j_ptr = &inventory[i];
5515
5516         /* Forget stack */
5517         j_ptr->next_o_idx = 0;
5518
5519         /* Forget monster */
5520         j_ptr->held_m_idx = 0;
5521
5522         /* Forget location */
5523         j_ptr->iy = j_ptr->ix = 0;
5524
5525         /* Player touches it, and no longer marked */
5526         j_ptr->marked = OM_TOUCHED;
5527
5528         p_ptr->total_weight += (j_ptr->number * j_ptr->weight);
5529
5530         /* Count the items */
5531         inven_cnt++;
5532         p_ptr->update |= (PU_BONUS | PU_COMBINE | PU_REORDER);
5533         p_ptr->window |= (PW_INVEN);
5534
5535         /* Return the slot */
5536         return (i);
5537 }
5538
5539
5540 /*!
5541  * @brief 装備スロットからオブジェクトを外すメインルーチン /
5542  * Take off (some of) a non-cursed equipment item
5543  * @param item オブジェクトを外したい所持テーブルのID
5544  * @param amt 外したい個数
5545  * @return 収められた所持スロットのID、拾うことができなかった場合-1を返す。
5546  * @details
5547  * Note that only one item at a time can be wielded per slot.\n
5548  * Note that taking off an item when "full" may cause that item\n
5549  * to fall to the ground.\n
5550  * Return the inventory slot into which the item is placed.\n
5551  */
5552 INVENTORY_IDX inven_takeoff(INVENTORY_IDX item, ITEM_NUMBER amt)
5553 {
5554         INVENTORY_IDX slot;
5555
5556         object_type forge;
5557         object_type *q_ptr;
5558
5559         object_type *o_ptr;
5560
5561         concptr act;
5562
5563         GAME_TEXT o_name[MAX_NLEN];
5564
5565
5566         /* Get the item to take off */
5567         o_ptr = &inventory[item];
5568
5569         /* Paranoia */
5570         if (amt <= 0) return (-1);
5571
5572         /* Verify */
5573         if (amt > o_ptr->number) amt = o_ptr->number;
5574         q_ptr = &forge;
5575
5576         /* Obtain a local object */
5577         object_copy(q_ptr, o_ptr);
5578
5579         /* Modify quantity */
5580         q_ptr->number = amt;
5581
5582         object_desc(o_name, q_ptr, 0);
5583
5584         /* Took off weapon */
5585         if (((item == INVEN_RARM) || (item == INVEN_LARM)) &&
5586             object_is_melee_weapon(o_ptr))
5587         {
5588                 act = _("を装備からはずした", "You were wielding");
5589         }
5590
5591         /* Took off bow */
5592         else if (item == INVEN_BOW)
5593         {
5594                 act = _("を装備からはずした", "You were holding");
5595         }
5596
5597         /* Took off light */
5598         else if (item == INVEN_LITE)
5599         {
5600                 act = _("を光源からはずした", "You were holding");
5601         }
5602
5603         /* Took off something */
5604         else
5605         {
5606                 act = _("を装備からはずした", "You were wearing");
5607         }
5608
5609         /* Modify, Optimize */
5610         inven_item_increase(item, -amt);
5611         inven_item_optimize(item);
5612
5613         /* Carry the object */
5614         slot = inven_carry(q_ptr);
5615
5616 #ifdef JP
5617         msg_format("%s(%c)%s。", o_name, index_to_label(slot), act);
5618 #else
5619         msg_format("%s %s (%c).", act, o_name, index_to_label(slot));
5620 #endif
5621
5622
5623         /* Return slot */
5624         return (slot);
5625 }
5626
5627
5628 /*!
5629  * @brief 所持スロットから床下にオブジェクトを落とすメインルーチン /
5630  * Drop (some of) a non-cursed inventory/equipment item
5631  * @param item 所持テーブルのID
5632  * @param amt 落としたい個数
5633  * @return なし
5634  * @details
5635  * The object will be dropped "near" the current location
5636  */
5637 void inven_drop(INVENTORY_IDX item, ITEM_NUMBER amt)
5638 {
5639         object_type forge;
5640         object_type *q_ptr;
5641         object_type *o_ptr;
5642
5643         GAME_TEXT o_name[MAX_NLEN];
5644
5645         /* Access original object */
5646         o_ptr = &inventory[item];
5647
5648         /* Error check */
5649         if (amt <= 0) return;
5650
5651         /* Not too many */
5652         if (amt > o_ptr->number) amt = o_ptr->number;
5653
5654         /* Take off equipment */
5655         if (item >= INVEN_RARM)
5656         {
5657                 /* Take off first */
5658                 item = inven_takeoff(item, amt);
5659
5660                 /* Access original object */
5661                 o_ptr = &inventory[item];
5662         }
5663
5664         q_ptr = &forge;
5665
5666         /* Obtain local object */
5667         object_copy(q_ptr, o_ptr);
5668
5669         /* Distribute charges of wands or rods */
5670         distribute_charges(o_ptr, q_ptr, amt);
5671
5672         /* Modify quantity */
5673         q_ptr->number = amt;
5674
5675         /* Describe local object */
5676         object_desc(o_name, q_ptr, 0);
5677
5678         msg_format(_("%s(%c)を落とした。", "You drop %s (%c)."), o_name, index_to_label(item));
5679
5680         /* Drop it near the player */
5681         (void)drop_near(q_ptr, 0, p_ptr->y, p_ptr->x);
5682
5683         /* Modify, Describe, Optimize */
5684         inven_item_increase(item, -amt);
5685         inven_item_describe(item);
5686         inven_item_optimize(item);
5687 }
5688
5689
5690 /*!
5691  * @brief プレイヤーの所持スロットに存在するオブジェクトをまとめなおす /
5692  * Combine items in the pack
5693  * @return なし
5694  * @details
5695  * Note special handling of the "overflow" slot
5696  */
5697 void combine_pack(void)
5698 {
5699         int             i, j, k;
5700         object_type *o_ptr;
5701         object_type     *j_ptr;
5702         bool            flag = FALSE, combined;
5703
5704         do
5705         {
5706                 combined = FALSE;
5707
5708                 /* Combine the pack (backwards) */
5709                 for (i = INVEN_PACK; i > 0; i--)
5710                 {
5711                         o_ptr = &inventory[i];
5712
5713                         /* Skip empty items */
5714                         if (!o_ptr->k_idx) continue;
5715
5716                         /* Scan the items above that item */
5717                         for (j = 0; j < i; j++)
5718                         {
5719                                 int max_num;
5720
5721                                 j_ptr = &inventory[j];
5722
5723                                 /* Skip empty items */
5724                                 if (!j_ptr->k_idx) continue;
5725
5726                                 /*
5727                                  * Get maximum number of the stack if these
5728                                  * are similar, get zero otherwise.
5729                                  */
5730                                 max_num = object_similar_part(j_ptr, o_ptr);
5731
5732                                 /* Can we (partialy) drop "o_ptr" onto "j_ptr"? */
5733                                 if (max_num && j_ptr->number < max_num)
5734                                 {
5735                                         if (o_ptr->number + j_ptr->number <= max_num)
5736                                         {
5737                                                 /* Take note */
5738                                                 flag = TRUE;
5739
5740                                                 /* Add together the item counts */
5741                                                 object_absorb(j_ptr, o_ptr);
5742
5743                                                 /* One object is gone */
5744                                                 inven_cnt--;
5745
5746                                                 /* Slide everything down */
5747                                                 for (k = i; k < INVEN_PACK; k++)
5748                                                 {
5749                                                         /* Structure copy */
5750                                                         inventory[k] = inventory[k+1];
5751                                                 }
5752
5753                                                 /* Erase the "final" slot */
5754                                                 object_wipe(&inventory[k]);
5755                                         }
5756                                         else
5757                                         {
5758                                                 int old_num = o_ptr->number;
5759                                                 int remain = j_ptr->number + o_ptr->number - max_num;
5760 #if 0
5761                                                 o_ptr->number -= remain;
5762 #endif
5763                                                 /* Add together the item counts */
5764                                                 object_absorb(j_ptr, o_ptr);
5765
5766                                                 o_ptr->number = remain;
5767
5768                                                 /* Hack -- if rods are stacking, add the pvals (maximum timeouts) and current timeouts together. -LM- */
5769                                                 if (o_ptr->tval == TV_ROD)
5770                                                 {
5771                                                         o_ptr->pval =  o_ptr->pval * remain / old_num;
5772                                                         o_ptr->timeout = o_ptr->timeout * remain / old_num;
5773                                                 }
5774
5775                                                 /* Hack -- if wands are stacking, combine the charges. -LM- */
5776                                                 if (o_ptr->tval == TV_WAND)
5777                                                 {
5778                                                         o_ptr->pval = o_ptr->pval * remain / old_num;
5779                                                 }
5780                                         }
5781
5782                                         p_ptr->window |= (PW_INVEN);
5783
5784                                         /* Take note */
5785                                         combined = TRUE;
5786
5787                                         break;
5788                                 }
5789                         }
5790                 }
5791         }
5792         while (combined);
5793
5794         if (flag) msg_print(_("ザックの中のアイテムをまとめ直した。", "You combine some items in your pack."));
5795 }
5796
5797 /*!
5798  * @brief プレイヤーの所持スロットに存在するオブジェクトを並び替える /
5799  * Reorder items in the pack
5800  * @return なし
5801  * @details
5802  * Note special handling of the "overflow" slot
5803  */
5804 void reorder_pack(void)
5805 {
5806         int             i, j, k;
5807         s32b            o_value;
5808         object_type     forge;
5809         object_type     *q_ptr;
5810         object_type *o_ptr;
5811         bool            flag = FALSE;
5812
5813
5814         /* Re-order the pack (forwards) */
5815         for (i = 0; i < INVEN_PACK; i++)
5816         {
5817                 /* Mega-Hack -- allow "proper" over-flow */
5818                 if ((i == INVEN_PACK) && (inven_cnt == INVEN_PACK)) break;
5819
5820                 o_ptr = &inventory[i];
5821
5822                 /* Skip empty slots */
5823                 if (!o_ptr->k_idx) continue;
5824
5825                 /* Get the "value" of the item */
5826                 o_value = object_value(o_ptr);
5827
5828                 /* Scan every occupied slot */
5829                 for (j = 0; j < INVEN_PACK; j++)
5830                 {
5831                         if (object_sort_comp(o_ptr, o_value, &inventory[j])) break;
5832                 }
5833
5834                 /* Never move down */
5835                 if (j >= i) continue;
5836
5837                 /* Take note */
5838                 flag = TRUE;
5839                 q_ptr = &forge;
5840
5841                 /* Save a copy of the moving item */
5842                 object_copy(q_ptr, &inventory[i]);
5843
5844                 /* Slide the objects */
5845                 for (k = i; k > j; k--)
5846                 {
5847                         /* Slide the item */
5848                         object_copy(&inventory[k], &inventory[k-1]);
5849                 }
5850
5851                 /* Insert the moving item */
5852                 object_copy(&inventory[j], q_ptr);
5853
5854                 p_ptr->window |= (PW_INVEN);
5855         }
5856
5857         if (flag) msg_print(_("ザックの中のアイテムを並べ直した。", "You reorder some items in your pack."));
5858 }
5859
5860 /*!
5861  * @brief 現在アクティブになっているウィンドウにオブジェクトの詳細を表示する /
5862  * Hack -- display an object kind in the current window
5863  * @param k_idx ベースアイテムの参照ID
5864  * @return なし
5865  * @details
5866  * Include list of usable spells for readible books
5867  */
5868 void display_koff(KIND_OBJECT_IDX k_idx)
5869 {
5870         int y;
5871
5872         object_type forge;
5873         object_type *q_ptr;
5874         int         sval;
5875         REALM_IDX   use_realm;
5876
5877         GAME_TEXT o_name[MAX_NLEN];
5878
5879
5880         /* Erase the window */
5881         for (y = 0; y < Term->hgt; y++)
5882         {
5883                 /* Erase the line */
5884                 Term_erase(0, y, 255);
5885         }
5886
5887         /* No info */
5888         if (!k_idx) return;
5889         q_ptr = &forge;
5890
5891         /* Prepare the object */
5892         object_prep(q_ptr, k_idx);
5893         object_desc(o_name, q_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY | OD_STORE));
5894
5895         /* Mention the object name */
5896         Term_putstr(0, 0, -1, TERM_WHITE, o_name);
5897
5898         /* Access the item's sval */
5899         sval = q_ptr->sval;
5900         use_realm = tval2realm(q_ptr->tval);
5901
5902         /* Warriors are illiterate */
5903         if (p_ptr->realm1 || p_ptr->realm2)
5904         {
5905                 if ((use_realm != p_ptr->realm1) && (use_realm != p_ptr->realm2)) return;
5906         }
5907         else
5908         {
5909                 if ((p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE)) return;
5910                 if (!is_magic(use_realm)) return;
5911                 if ((p_ptr->pclass == CLASS_RED_MAGE) && (use_realm != REALM_ARCANE) && (sval > 1)) return;
5912         }
5913
5914         /* Display spells in readible books */
5915         {
5916                 int     spell = -1;
5917                 int     num = 0;
5918                 SPELL_IDX    spells[64];
5919
5920                 /* Extract spells */
5921                 for (spell = 0; spell < 32; spell++)
5922                 {
5923                         /* Check for this spell */
5924                         if (fake_spell_flags[sval] & (1L << spell))
5925                         {
5926                                 /* Collect this spell */
5927                                 spells[num++] = spell;
5928                         }
5929                 }
5930
5931                 /* Print spells */
5932                 print_spells(0, spells, num, 2, 0, use_realm);
5933         }
5934 }
5935
5936
5937 /*!
5938  * @brief 投擲時たいまつに投げやすい/焼棄/アンデッドスレイの特別効果を返す。
5939  * Torches have special abilities when they are flaming.
5940  * @param o_ptr 投擲するオブジェクトの構造体参照ポインタ
5941  * @param flgs 特別に追加するフラグを返す参照ポインタ
5942  * @return なし
5943  */
5944 void torch_flags(object_type *o_ptr, BIT_FLAGS *flgs)
5945 {
5946         if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_TORCH))
5947         {
5948                 if (o_ptr->xtra4 > 0)
5949                 {
5950                         add_flag(flgs, TR_BRAND_FIRE);
5951                         add_flag(flgs, TR_KILL_UNDEAD);
5952                         add_flag(flgs, TR_THROW);
5953                 }
5954         }
5955 }
5956
5957 /*!
5958  * @brief 投擲時たいまつにダイスを与える。
5959  * Torches have special abilities when they are flaming.
5960  * @param o_ptr 投擲するオブジェクトの構造体参照ポインタ
5961  * @param dd 特別なダイス数を返す参照ポインタ
5962  * @param ds 特別なダイス面数を返す参照ポインタ
5963  * @return なし
5964  */
5965 void torch_dice(object_type *o_ptr, DICE_NUMBER *dd, DICE_SID *ds)
5966 {
5967         if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_TORCH))
5968         {
5969                 if (o_ptr->xtra4 > 0)
5970                 {
5971                         (*dd) = 1;
5972                         (*ds) = 6;
5973                 }
5974         }
5975 }
5976
5977 /*!
5978  * @brief 投擲時命中したたいまつの寿命を縮める。
5979  * Torches have special abilities when they are flaming.
5980  * @param o_ptr 投擲するオブジェクトの構造体参照ポインタ
5981  * @return なし
5982  */
5983 void torch_lost_fuel(object_type *o_ptr)
5984 {
5985         if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_TORCH))
5986         {
5987                 o_ptr->xtra4 -= (FUEL_TORCH / 25);
5988                 if (o_ptr->xtra4 < 0) o_ptr->xtra4 = 0;
5989         }
5990 }