OSDN Git Service

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