OSDN Git Service

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