OSDN Git Service

Fix Doxygen warning in object2.c.
[hengband/hengband.git] / src / object2.c
1 /*!
2  * @file object2.c
3  * @brief ¥ª¥Ö¥¸¥§¥¯¥È¤Î¼ÂÁõ / Object code, part 2
4  * @date 2014/01/11
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  *\n
8  * This software may be copied and distributed for educational, research,\n
9  * and not for profit purposes provided that this copyright and statement\n
10  * are included in all such copies.  Other copyrights may also apply.\n
11  * 2014 Deskull rearranged comment for Doxygen.\n
12  */
13
14 #include "angband.h"
15
16 #include "kajitips.h"
17
18 /*!
19  * @brief ¾²¾å¡¢¥â¥ó¥¹¥¿¡¼½ê»ý¤Ç¥¹¥¿¥Ã¥¯¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤òºï½ü¤·¥¹¥¿¥Ã¥¯¤òÊä´°¤¹¤ë / Excise a dungeon object from any stacks
20  * @param o_idx ºï½üÂоݤΥª¥Ö¥¸¥§¥¯¥È¹½Â¤ÂΥݥ¤¥ó¥¿
21  * @return ¤Ê¤·
22  */
23 void excise_object_idx(int o_idx)
24 {
25         object_type *j_ptr;
26
27         s16b this_o_idx, next_o_idx = 0;
28
29         s16b prev_o_idx = 0;
30
31
32         /* Object */
33         j_ptr = &o_list[o_idx];
34
35         /* Monster */
36         if (j_ptr->held_m_idx)
37         {
38                 monster_type *m_ptr;
39
40                 /* Monster */
41                 m_ptr = &m_list[j_ptr->held_m_idx];
42
43                 /* Scan all objects in the grid */
44                 for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
45                 {
46                         object_type *o_ptr;
47
48                         /* Acquire object */
49                         o_ptr = &o_list[this_o_idx];
50
51                         /* Acquire next object */
52                         next_o_idx = o_ptr->next_o_idx;
53
54                         /* Done */
55                         if (this_o_idx == o_idx)
56                         {
57                                 /* No previous */
58                                 if (prev_o_idx == 0)
59                                 {
60                                         /* Remove from list */
61                                         m_ptr->hold_o_idx = next_o_idx;
62                                 }
63
64                                 /* Real previous */
65                                 else
66                                 {
67                                         object_type *k_ptr;
68
69                                         /* Previous object */
70                                         k_ptr = &o_list[prev_o_idx];
71
72                                         /* Remove from list */
73                                         k_ptr->next_o_idx = next_o_idx;
74                                 }
75
76                                 /* Forget next pointer */
77                                 o_ptr->next_o_idx = 0;
78
79                                 /* Done */
80                                 break;
81                         }
82
83                         /* Save prev_o_idx */
84                         prev_o_idx = this_o_idx;
85                 }
86         }
87
88         /* Dungeon */
89         else
90         {
91                 cave_type *c_ptr;
92
93                 int y = j_ptr->iy;
94                 int x = j_ptr->ix;
95
96                 /* Grid */
97                 c_ptr = &cave[y][x];
98
99                 /* Scan all objects in the grid */
100                 for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
101                 {
102                         object_type *o_ptr;
103
104                         /* Acquire object */
105                         o_ptr = &o_list[this_o_idx];
106
107                         /* Acquire next object */
108                         next_o_idx = o_ptr->next_o_idx;
109
110                         /* Done */
111                         if (this_o_idx == o_idx)
112                         {
113                                 /* No previous */
114                                 if (prev_o_idx == 0)
115                                 {
116                                         /* Remove from list */
117                                         c_ptr->o_idx = next_o_idx;
118                                 }
119
120                                 /* Real previous */
121                                 else
122                                 {
123                                         object_type *k_ptr;
124
125                                         /* Previous object */
126                                         k_ptr = &o_list[prev_o_idx];
127
128                                         /* Remove from list */
129                                         k_ptr->next_o_idx = next_o_idx;
130                                 }
131
132                                 /* Forget next pointer */
133                                 o_ptr->next_o_idx = 0;
134
135                                 /* Done */
136                                 break;
137                         }
138
139                         /* Save prev_o_idx */
140                         prev_o_idx = this_o_idx;
141                 }
142         }
143 }
144
145 /*!
146  * @brief ¥ª¥Ö¥¸¥§¥¯¥È¤òºï½ü¤¹¤ë /
147  * Delete a dungeon object
148  * @param o_idx ºï½üÂоݤΥª¥Ö¥¸¥§¥¯¥È¹½Â¤ÂΥݥ¤¥ó¥¿
149  * @return ¤Ê¤·
150  * @details
151  * Handle "stacks" of objects correctly.
152  */
153 void delete_object_idx(int o_idx)
154 {
155         object_type *j_ptr;
156
157         /* Excise */
158         excise_object_idx(o_idx);
159
160         /* Object */
161         j_ptr = &o_list[o_idx];
162
163         /* Dungeon floor */
164         if (!(j_ptr->held_m_idx))
165         {
166                 int y, x;
167
168                 /* Location */
169                 y = j_ptr->iy;
170                 x = j_ptr->ix;
171
172                 /* Visual update */
173                 lite_spot(y, x);
174         }
175
176         /* Wipe the object */
177         object_wipe(j_ptr);
178
179         /* Count objects */
180         o_cnt--;
181 }
182
183
184 /*!
185  * @brief ¥Õ¥í¥¢¤Ë¥Þ¥¹¤ËÍî¤Á¤Æ¤¤¤ë¥ª¥Ö¥¸¥§¥¯¥È¤òÁ´¤Æºï½ü¤¹¤ë / Deletes all objects at given location
186  * Delete a dungeon object
187  * @param y ºï½ü¤·¤¿¥Õ¥í¥¢¥Þ¥¹¤ÎYºÂɸ
188  * @param x ºï½ü¤·¤¿¥Õ¥í¥¢¥Þ¥¹¤ÎXºÂɸ
189  * @return ¤Ê¤·
190  */
191 void delete_object(int y, int x)
192 {
193         cave_type *c_ptr;
194
195         s16b this_o_idx, next_o_idx = 0;
196
197
198         /* Refuse "illegal" locations */
199         if (!in_bounds(y, x)) return;
200
201
202         /* Grid */
203         c_ptr = &cave[y][x];
204
205         /* Scan all objects in the grid */
206         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
207         {
208                 object_type *o_ptr;
209
210                 /* Acquire object */
211                 o_ptr = &o_list[this_o_idx];
212
213                 /* Acquire next object */
214                 next_o_idx = o_ptr->next_o_idx;
215
216                 /* Wipe the object */
217                 object_wipe(o_ptr);
218
219                 /* Count objects */
220                 o_cnt--;
221         }
222
223         /* Objects are gone */
224         c_ptr->o_idx = 0;
225
226         /* Visual update */
227         lite_spot(y, x);
228 }
229
230
231 /*!
232  * @brief ¥°¥í¡¼¥Ð¥ë¥ª¥Ö¥¸¥§¥¯¥ÈÇÛÎó¤ËÂФ·»ØÄêÈϰϤΥª¥Ö¥¸¥§¥¯¥È¤òÀ°Íý¤·¤ÆID¤Î¼ã¤¤½ç¤Ë´ó¤»¤ë /
233  * Move an object from index i1 to index i2 in the object list
234  * @param i1 À°Íý¤·¤¿¤¤ÇÛÎó¤Î»ÏÅÀ
235  * @param i2 À°Íý¤·¤¿¤¤ÇÛÎó¤Î½ªÅÀ
236  * @return ¤Ê¤·
237  */
238 static void compact_objects_aux(int i1, int i2)
239 {
240         int i;
241
242         cave_type *c_ptr;
243
244         object_type *o_ptr;
245
246
247         /* Do nothing */
248         if (i1 == i2) return;
249
250
251         /* Repair objects */
252         for (i = 1; i < o_max; i++)
253         {
254                 /* Acquire object */
255                 o_ptr = &o_list[i];
256
257                 /* Skip "dead" objects */
258                 if (!o_ptr->k_idx) continue;
259
260                 /* Repair "next" pointers */
261                 if (o_ptr->next_o_idx == i1)
262                 {
263                         /* Repair */
264                         o_ptr->next_o_idx = i2;
265                 }
266         }
267
268
269         /* Acquire object */
270         o_ptr = &o_list[i1];
271
272
273         /* Monster */
274         if (o_ptr->held_m_idx)
275         {
276                 monster_type *m_ptr;
277
278                 /* Acquire monster */
279                 m_ptr = &m_list[o_ptr->held_m_idx];
280
281                 /* Repair monster */
282                 if (m_ptr->hold_o_idx == i1)
283                 {
284                         /* Repair */
285                         m_ptr->hold_o_idx = i2;
286                 }
287         }
288
289         /* Dungeon */
290         else
291         {
292                 int y, x;
293
294                 /* Acquire location */
295                 y = o_ptr->iy;
296                 x = o_ptr->ix;
297
298                 /* Acquire grid */
299                 c_ptr = &cave[y][x];
300
301                 /* Repair grid */
302                 if (c_ptr->o_idx == i1)
303                 {
304                         /* Repair */
305                         c_ptr->o_idx = i2;
306                 }
307         }
308
309
310         /* Structure copy */
311         o_list[i2] = o_list[i1];
312
313         /* Wipe the hole */
314         object_wipe(o_ptr);
315 }
316
317
318 /*!
319  * @brief ¥°¥í¡¼¥Ð¥ë¥ª¥Ö¥¸¥§¥¯¥ÈÇÛÎ󤫤éÍ¥ÀèÅÙ¤ÎÄ㤤¤â¤Î¤òºï½ü¤·¡¢¥Ç¡¼¥¿¤ò°µ½Ì¤¹¤ë¡£ /
320  * Compact and Reorder the object list.
321  * @size ºÇÄã¤Ç¤â¸º¤é¤·¤¿¤¤¥ª¥Ö¥¸¥§¥¯¥È¿ô¤Î¿å½à
322  * @return ¤Ê¤·
323  * @details
324  * ¡Ê´í¸±¤Ê¤Î¤Ç»ÈÍѤˤÏÃí°Õ¤¹¤ë¤³¤È¡Ë
325  * This function can be very dangerous, use with caution!\n
326  *\n
327  * When actually "compacting" objects, we base the saving throw on a\n
328  * combination of object level, distance from player, and current\n
329  * "desperation".\n
330  *\n
331  * After "compacting" (if needed), we "reorder" the objects into a more\n
332  * compact order, and we reset the allocation info, and the "live" array.\n
333  */
334 void compact_objects(int size)
335 {
336         int i, y, x, num, cnt;
337         int cur_lev, cur_dis, chance;
338         object_type *o_ptr;
339
340
341         /* Compact */
342         if (size)
343         {
344                 /* Message */
345 #ifdef JP
346                 msg_print("¥¢¥¤¥Æ¥à¾ðÊó¤ò°µ½Ì¤·¤Æ¤¤¤Þ¤¹...");
347 #else
348                 msg_print("Compacting objects...");
349 #endif
350
351
352                 /* Redraw map */
353                 p_ptr->redraw |= (PR_MAP);
354
355                 /* Window stuff */
356                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
357         }
358
359
360         /* Compact at least 'size' objects */
361         for (num = 0, cnt = 1; num < size; cnt++)
362         {
363                 /* Get more vicious each iteration */
364                 cur_lev = 5 * cnt;
365
366                 /* Get closer each iteration */
367                 cur_dis = 5 * (20 - cnt);
368
369                 /* Examine the objects */
370                 for (i = 1; i < o_max; i++)
371                 {
372                         o_ptr = &o_list[i];
373
374                         /* Skip dead objects */
375                         if (!o_ptr->k_idx) continue;
376
377                         /* Hack -- High level objects start out "immune" */
378                         if (k_info[o_ptr->k_idx].level > cur_lev) continue;
379
380                         /* Monster */
381                         if (o_ptr->held_m_idx)
382                         {
383                                 monster_type *m_ptr;
384
385                                 /* Acquire monster */
386                                 m_ptr = &m_list[o_ptr->held_m_idx];
387
388                                 /* Get the location */
389                                 y = m_ptr->fy;
390                                 x = m_ptr->fx;
391
392                                 /* Monsters protect their objects */
393                                 if (randint0(100) < 90) continue;
394                         }
395
396                         /* Dungeon */
397                         else
398                         {
399                                 /* Get the location */
400                                 y = o_ptr->iy;
401                                 x = o_ptr->ix;
402                         }
403
404                         /* Nearby objects start out "immune" */
405                         if ((cur_dis > 0) && (distance(py, px, y, x) < cur_dis)) continue;
406
407                         /* Saving throw */
408                         chance = 90;
409
410                         /* Hack -- only compact artifacts in emergencies */
411                         if ((object_is_fixed_artifact(o_ptr) || o_ptr->art_name) &&
412                             (cnt < 1000)) chance = 100;
413
414                         /* Apply the saving throw */
415                         if (randint0(100) < chance) continue;
416
417                         /* Delete the object */
418                         delete_object_idx(i);
419
420                         /* Count it */
421                         num++;
422                 }
423         }
424
425
426         /* Excise dead objects (backwards!) */
427         for (i = o_max - 1; i >= 1; i--)
428         {
429                 o_ptr = &o_list[i];
430
431                 /* Skip real objects */
432                 if (o_ptr->k_idx) continue;
433
434                 /* Move last object into open hole */
435                 compact_objects_aux(o_max - 1, i);
436
437                 /* Compress "o_max" */
438                 o_max--;
439         }
440 }
441
442
443 /*!
444  * @brief ¥°¥í¡¼¥Ð¥ë¥ª¥Ö¥¸¥§¥¯¥ÈÇÛÎó¤ò½é´ü²½¤¹¤ë /
445  * Delete all the items when player leaves the level
446  * @note we do NOT visually reflect these (irrelevant) changes
447  * @details
448  * Hack -- we clear the "c_ptr->o_idx" field for every grid,
449  * and the "m_ptr->next_o_idx" field for every monster, since
450  * we know we are clearing every object.  Technically, we only
451  * clear those fields for grids/monsters containing objects,
452  * and we clear it once for every such object.
453  * @return ¤Ê¤·
454  */
455 void wipe_o_list(void)
456 {
457         int i;
458
459         /* Delete the existing objects */
460         for (i = 1; i < o_max; i++)
461         {
462                 object_type *o_ptr = &o_list[i];
463
464                 /* Skip dead objects */
465                 if (!o_ptr->k_idx) continue;
466
467                 /* Mega-Hack -- preserve artifacts */
468                 if (!character_dungeon || preserve_mode)
469                 {
470                         /* Hack -- Preserve unknown artifacts */
471                         if (object_is_fixed_artifact(o_ptr) && !object_is_known(o_ptr))
472                         {
473                                 /* Mega-Hack -- Preserve the artifact */
474                                 a_info[o_ptr->name1].cur_num = 0;
475                         }
476                 }
477
478                 /* Monster */
479                 if (o_ptr->held_m_idx)
480                 {
481                         monster_type *m_ptr;
482
483                         /* Monster */
484                         m_ptr = &m_list[o_ptr->held_m_idx];
485
486                         /* Hack -- see above */
487                         m_ptr->hold_o_idx = 0;
488                 }
489
490                 /* Dungeon */
491                 else
492                 {
493                         cave_type *c_ptr;
494
495                         /* Access location */
496                         int y = o_ptr->iy;
497                         int x = o_ptr->ix;
498
499                         /* Access grid */
500                         c_ptr = &cave[y][x];
501
502                         /* Hack -- see above */
503                         c_ptr->o_idx = 0;
504                 }
505
506                 /* Wipe the object */
507                 object_wipe(o_ptr);
508         }
509
510         /* Reset "o_max" */
511         o_max = 1;
512
513         /* Reset "o_cnt" */
514         o_cnt = 0;
515 }
516
517
518 /*!
519  * @brief ¥°¥í¡¼¥Ð¥ë¥ª¥Ö¥¸¥§¥¯¥ÈÇÛÎ󤫤é¶õ¤­¤ò¼èÆÀ¤¹¤ë /
520  * Acquires and returns the index of a "free" object.
521  * @return ³«¤¤¤Æ¤¤¤ë¥ª¥Ö¥¸¥§¥¯¥ÈÍ×ÁǤÎID
522  * @details
523  * This routine should almost never fail, but in case it does,
524  * we must be sure to handle "failure" of this routine.
525  */
526 s16b o_pop(void)
527 {
528         int i;
529
530
531         /* Initial allocation */
532         if (o_max < max_o_idx)
533         {
534                 /* Get next space */
535                 i = o_max;
536
537                 /* Expand object array */
538                 o_max++;
539
540                 /* Count objects */
541                 o_cnt++;
542
543                 /* Use this object */
544                 return (i);
545         }
546
547
548         /* Recycle dead objects */
549         for (i = 1; i < o_max; i++)
550         {
551                 object_type *o_ptr;
552
553                 /* Acquire object */
554                 o_ptr = &o_list[i];
555
556                 /* Skip live objects */
557                 if (o_ptr->k_idx) continue;
558
559                 /* Count objects */
560                 o_cnt++;
561
562                 /* Use this object */
563                 return (i);
564         }
565
566
567         /* Warn the player (except during dungeon creation) */
568 #ifdef JP
569         if (character_dungeon) msg_print("¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
570 #else
571         if (character_dungeon) msg_print("Too many objects!");
572 #endif
573
574
575         /* Oops */
576         return (0);
577 }
578
579
580 /*!
581  * @brief ¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¥Æ¡¼¥Ö¥ë¤ËÀ¸À®À©Ìó¤ò²Ã¤¨¤ë /
582  * Apply a "object restriction function" to the "object allocation table"
583  * @return ¾ï¤Ë0¤òÊÖ¤¹¡£
584  * @details À¸À®¤ÎÀ©Ìó¤Ï¥°¥í¡¼¥Ð¥ë¤Îget_obj_num_hook´Ø¿ô¥Ý¥¤¥ó¥¿¤Ç²Ã¤¨¤ë
585  */
586 static errr get_obj_num_prep(void)
587 {
588         int i;
589
590         /* Get the entry */
591         alloc_entry *table = alloc_kind_table;
592
593         /* Scan the allocation table */
594         for (i = 0; i < alloc_kind_size; i++)
595         {
596                 /* Accept objects which pass the restriction, if any */
597                 if (!get_obj_num_hook || (*get_obj_num_hook)(table[i].index))
598                 {
599                         /* Accept this object */
600                         table[i].prob2 = table[i].prob1;
601                 }
602
603                 /* Do not use this object */
604                 else
605                 {
606                         /* Decline this object */
607                         table[i].prob2 = 0;
608                 }
609         }
610
611         /* Success */
612         return (0);
613 }
614
615
616 /*!
617  * @brief ¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¥Æ¡¼¥Ö¥ë¤«¤é¥¢¥¤¥Æ¥à¤ò¼èÆÀ¤¹¤ë /
618  * Choose an object kind that seems "appropriate" to the given level
619  * @param À¸À®³¬
620  * @return Áª¤Ð¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¥Ù¡¼¥¹ID
621  * @details
622  * This function uses the "prob2" field of the "object allocation table",\n
623  * and various local information, to calculate the "prob3" field of the\n
624  * same table, which is then used to choose an "appropriate" object, in\n
625  * a relatively efficient manner.\n
626  *\n
627  * It is (slightly) more likely to acquire an object of the given level\n
628  * than one of a lower level.  This is done by choosing several objects\n
629  * appropriate to the given level and keeping the "hardest" one.\n
630  *\n
631  * Note that if no objects are "appropriate", then this function will\n
632  * fail, and return zero, but this should *almost* never happen.\n
633  */
634 s16b get_obj_num(int level)
635 {
636         int             i, j, p;
637         int             k_idx;
638         long            value, total;
639         object_kind     *k_ptr;
640         alloc_entry     *table = alloc_kind_table;
641
642         if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
643
644         /* Boost level */
645         if ((level > 0) && !(d_info[dungeon_type].flags1 & DF1_BEGINNER))
646         {
647                 /* Occasional "boost" */
648                 if (one_in_(GREAT_OBJ))
649                 {
650                         /* What a bizarre calculation */
651                         level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
652                 }
653         }
654
655         /* Reset total */
656         total = 0L;
657
658         /* Process probabilities */
659         for (i = 0; i < alloc_kind_size; i++)
660         {
661                 /* Objects are sorted by depth */
662                 if (table[i].level > level) break;
663
664                 /* Default */
665                 table[i].prob3 = 0;
666
667                 /* Access the index */
668                 k_idx = table[i].index;
669
670                 /* Access the actual kind */
671                 k_ptr = &k_info[k_idx];
672
673                 /* Hack -- prevent embedded chests */
674                 if (opening_chest && (k_ptr->tval == TV_CHEST)) continue;
675
676                 /* Accept */
677                 table[i].prob3 = table[i].prob2;
678
679                 /* Total */
680                 total += table[i].prob3;
681         }
682
683         /* No legal objects */
684         if (total <= 0) return (0);
685
686
687         /* Pick an object */
688         value = randint0(total);
689
690         /* Find the object */
691         for (i = 0; i < alloc_kind_size; i++)
692         {
693                 /* Found the entry */
694                 if (value < table[i].prob3) break;
695
696                 /* Decrement */
697                 value = value - table[i].prob3;
698         }
699
700
701         /* Power boost */
702         p = randint0(100);
703
704         /* Try for a "better" object once (50%) or twice (10%) */
705         if (p < 60)
706         {
707                 /* Save old */
708                 j = i;
709
710                 /* Pick a object */
711                 value = randint0(total);
712
713                 /* Find the object */
714                 for (i = 0; i < alloc_kind_size; i++)
715                 {
716                         /* Found the entry */
717                         if (value < table[i].prob3) break;
718
719                         /* Decrement */
720                         value = value - table[i].prob3;
721                 }
722
723                 /* Keep the "best" one */
724                 if (table[i].level < table[j].level) i = j;
725         }
726
727         /* Try for a "better" object twice (10%) */
728         if (p < 10)
729         {
730                 /* Save old */
731                 j = i;
732
733                 /* Pick a object */
734                 value = randint0(total);
735
736                 /* Find the object */
737                 for (i = 0; i < alloc_kind_size; i++)
738                 {
739                         /* Found the entry */
740                         if (value < table[i].prob3) break;
741
742                         /* Decrement */
743                         value = value - table[i].prob3;
744                 }
745
746                 /* Keep the "best" one */
747                 if (table[i].level < table[j].level) i = j;
748         }
749
750
751         /* Result */
752         return (table[i].index);
753 }
754
755
756 /*!
757  * @brief ¥ª¥Ö¥¸¥§¥¯¥È¤ò´ÕÄêºÑ¤Ë¤¹¤ë /
758  * Known is true when the "attributes" of an object are "known".
759  * @param o_ptr ´ÕÄêºÑ¤Ë¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
760  * @return ¤Ê¤·
761  * These include tohit, todam, toac, cost, and pval (charges).\n
762  *\n
763  * Note that "knowing" an object gives you everything that an "awareness"\n
764  * gives you, and much more.  In fact, the player is always "aware" of any\n
765  * item of which he has full "knowledge".\n
766  *\n
767  * But having full knowledge of, say, one "wand of wonder", does not, by\n
768  * itself, give you knowledge, or even awareness, of other "wands of wonder".\n
769  * It happens that most "identify" routines (including "buying from a shop")\n
770  * will make the player "aware" of the object as well as fully "know" it.\n
771  *\n
772  * This routine also removes any inscriptions generated by "feelings".\n
773  */
774 void object_known(object_type *o_ptr)
775 {
776         /* Remove "default inscriptions" */
777         o_ptr->feeling = FEEL_NONE;
778
779         /* Clear the "Felt" info */
780         o_ptr->ident &= ~(IDENT_SENSE);
781
782         /* Clear the "Empty" info */
783         o_ptr->ident &= ~(IDENT_EMPTY);
784
785         /* Now we know about the item */
786         o_ptr->ident |= (IDENT_KNOWN);
787 }
788
789 /*!
790  * @brief ¥ª¥Ö¥¸¥§¥¯¥È¤ò¡ö´ÕÄê¡öºÑ¤Ë¤¹¤ë /
791  * The player is now aware of the effects of the given object.
792  * @param o_ptr ¡ö´ÕÄê¡öºÑ¤Ë¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
793  * @return ¤Ê¤·
794  */
795 void object_aware(object_type *o_ptr)
796 {
797         bool mihanmei = !object_is_aware(o_ptr);
798
799         /* Fully aware of the effects */
800         k_info[o_ptr->k_idx].aware = TRUE;
801
802         if(mihanmei && !(k_info[o_ptr->k_idx].gen_flags & TRG_INSTA_ART) && record_ident &&
803            !p_ptr->is_dead && ((o_ptr->tval >= TV_AMULET && o_ptr->tval <= TV_POTION) || (o_ptr->tval == TV_FOOD)))
804         {
805                 object_type forge;
806                 object_type *q_ptr;
807                 char o_name[MAX_NLEN];
808
809                 q_ptr = &forge;
810                 object_copy(q_ptr, o_ptr);
811
812                 q_ptr->number = 1;
813                 object_desc(o_name, q_ptr, OD_NAME_ONLY);
814                 
815                 do_cmd_write_nikki(NIKKI_HANMEI, 0, o_name);
816         }
817 }
818
819
820 /*!
821  * @brief ¥ª¥Ö¥¸¥§¥¯¥È¤ò»î¹ÔºÑ¤Ë¤¹¤ë /
822  * Something has been "sampled"
823  * @param o_ptr »î¹ÔºÑ¤Ë¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
824  * @return ¤Ê¤·
825  */
826 void object_tried(object_type *o_ptr)
827 {
828         /* Mark it as tried (even if "aware") */
829         k_info[o_ptr->k_idx].tried = TRUE;
830 }
831
832
833 /*!
834  * @brief Ì¤´ÕÄê¤Ê¥Ù¡¼¥¹¥¢¥¤¥Æ¥à¤Î´ðËܲÁ³Ê¤òÊÖ¤¹ /
835  * Return the "value" of an "unknown" item Make a guess at the value of non-aware items
836  * @param o_ptr Ì¤´ÕÄê²Á³Ê¤ò³Îǧ¤·¤¿¤¤¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
837  * @return ¥ª¥Ö¥¸¥§¥¯¥È¤Î̤´ÕÄê²Á³Ê
838  */
839 static s32b object_value_base(object_type *o_ptr)
840 {
841         /* Aware item -- use template cost */
842         if (object_is_aware(o_ptr)) return (k_info[o_ptr->k_idx].cost);
843
844         /* Analyze the type */
845         switch (o_ptr->tval)
846         {
847
848                 /* Un-aware Food */
849                 case TV_FOOD: return (5L);
850
851                 /* Un-aware Potions */
852                 case TV_POTION: return (20L);
853
854                 /* Un-aware Scrolls */
855                 case TV_SCROLL: return (20L);
856
857                 /* Un-aware Staffs */
858                 case TV_STAFF: return (70L);
859
860                 /* Un-aware Wands */
861                 case TV_WAND: return (50L);
862
863                 /* Un-aware Rods */
864                 case TV_ROD: return (90L);
865
866                 /* Un-aware Rings */
867                 case TV_RING: return (45L);
868
869                 /* Un-aware Amulets */
870                 case TV_AMULET: return (45L);
871
872                 /* Figurines, relative to monster level */
873                 case TV_FIGURINE:
874                 {
875                         int level = r_info[o_ptr->pval].level;
876                         if (level < 20) return level*50L;
877                         else if (level < 30) return 1000+(level-20)*150L;
878                         else if (level < 40) return 2500+(level-30)*350L;
879                         else if (level < 50) return 6000+(level-40)*800L;
880                         else return 14000+(level-50)*2000L;
881                 }
882
883                 case TV_CAPTURE:
884                         if (!o_ptr->pval) return 1000L;
885                         else return ((r_info[o_ptr->pval].level) * 50L + 1000);
886         }
887
888         /* Paranoia -- Oops */
889         return (0L);
890 }
891
892
893 /*!
894  * @brief ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥Õ¥é¥°Îफ¤é²Á³Ê¤ò»»½Ð¤¹¤ë /
895  * Return the value of the flags the object has...
896  * @param o_ptr ¥Õ¥é¥°²Á³Ê¤ò³Îǧ¤·¤¿¤¤¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
897  * @param plusses ¥Õ¥é¥°¤ËÍ¿¤¨¤ë²Á³Ê¤Î´ðËܽŤß
898  * @return ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥Õ¥é¥°²Á³Ê
899  */
900 s32b flag_cost(object_type *o_ptr, int plusses)
901 {
902         s32b total = 0;
903         u32b flgs[TR_FLAG_SIZE];
904         s32b tmp_cost;
905         int count;
906         int i;
907         object_kind *k_ptr = &k_info[o_ptr->k_idx];
908
909         object_flags(o_ptr, flgs);
910
911         /*
912          * Exclude fixed flags of the base item.
913          * pval bonuses of base item will be treated later.
914          */
915         for (i = 0; i < TR_FLAG_SIZE; i++)
916                 flgs[i] &= ~(k_ptr->flags[i]);
917
918         /* Exclude fixed flags of the fixed artifact. */
919         if (object_is_fixed_artifact(o_ptr))
920         {
921                 artifact_type *a_ptr = &a_info[o_ptr->name1];
922
923                 for (i = 0; i < TR_FLAG_SIZE; i++)
924                         flgs[i] &= ~(a_ptr->flags[i]);
925         }
926
927         /* Exclude fixed flags of the ego-item. */
928         else if (object_is_ego(o_ptr))
929         {
930                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
931
932                 for (i = 0; i < TR_FLAG_SIZE; i++)
933                         flgs[i] &= ~(e_ptr->flags[i]);
934         }
935
936
937         /*
938          * Calucurate values of remaining flags
939          */
940         if (have_flag(flgs, TR_STR)) total += (1500 * plusses);
941         if (have_flag(flgs, TR_INT)) total += (1500 * plusses);
942         if (have_flag(flgs, TR_WIS)) total += (1500 * plusses);
943         if (have_flag(flgs, TR_DEX)) total += (1500 * plusses);
944         if (have_flag(flgs, TR_CON)) total += (1500 * plusses);
945         if (have_flag(flgs, TR_CHR)) total += (750 * plusses);
946         if (have_flag(flgs, TR_MAGIC_MASTERY)) total += (600 * plusses);
947         if (have_flag(flgs, TR_STEALTH)) total += (250 * plusses);
948         if (have_flag(flgs, TR_SEARCH)) total += (100 * plusses);
949         if (have_flag(flgs, TR_INFRA)) total += (150 * plusses);
950         if (have_flag(flgs, TR_TUNNEL)) total += (175 * plusses);
951         if ((have_flag(flgs, TR_SPEED)) && (plusses > 0))
952                 total += (10000 + (2500 * plusses));
953         if ((have_flag(flgs, TR_BLOWS)) && (plusses > 0))
954                 total += (10000 + (2500 * plusses));
955
956         tmp_cost = 0;
957         count = 0;
958         if (have_flag(flgs, TR_CHAOTIC)) {total += 5000;count++;}
959         if (have_flag(flgs, TR_VAMPIRIC)) {total += 6500;count++;}
960         if (have_flag(flgs, TR_FORCE_WEAPON)) {tmp_cost += 2500;count++;}
961         if (have_flag(flgs, TR_KILL_ANIMAL)) {tmp_cost += 2800;count++;}
962         else if (have_flag(flgs, TR_SLAY_ANIMAL)) {tmp_cost += 1800;count++;}
963         if (have_flag(flgs, TR_KILL_EVIL)) {tmp_cost += 3300;count++;}
964         else if (have_flag(flgs, TR_SLAY_EVIL)) {tmp_cost += 2300;count++;}
965         if (have_flag(flgs, TR_KILL_HUMAN)) {tmp_cost += 2800;count++;}
966         else if (have_flag(flgs, TR_SLAY_HUMAN)) {tmp_cost += 1800;count++;}
967         if (have_flag(flgs, TR_KILL_UNDEAD)) {tmp_cost += 2800;count++;}
968         else if (have_flag(flgs, TR_SLAY_UNDEAD)) {tmp_cost += 1800;count++;}
969         if (have_flag(flgs, TR_KILL_DEMON)) {tmp_cost += 2800;count++;}
970         else if (have_flag(flgs, TR_SLAY_DEMON)) {tmp_cost += 1800;count++;}
971         if (have_flag(flgs, TR_KILL_ORC)) {tmp_cost += 2500;count++;}
972         else if (have_flag(flgs, TR_SLAY_ORC)) {tmp_cost += 1500;count++;}
973         if (have_flag(flgs, TR_KILL_TROLL)) {tmp_cost += 2800;count++;}
974         else if (have_flag(flgs, TR_SLAY_TROLL)) {tmp_cost += 1800;count++;}
975         if (have_flag(flgs, TR_KILL_GIANT)) {tmp_cost += 2800;count++;}
976         else if (have_flag(flgs, TR_SLAY_GIANT)) {tmp_cost += 1800;count++;}
977         if (have_flag(flgs, TR_KILL_DRAGON)) {tmp_cost += 2800;count++;}
978         else if (have_flag(flgs, TR_SLAY_DRAGON)) {tmp_cost += 1800;count++;}
979
980         if (have_flag(flgs, TR_VORPAL)) {tmp_cost += 2500;count++;}
981         if (have_flag(flgs, TR_IMPACT)) {tmp_cost += 2500;count++;}
982         if (have_flag(flgs, TR_BRAND_POIS)) {tmp_cost += 3800;count++;}
983         if (have_flag(flgs, TR_BRAND_ACID)) {tmp_cost += 3800;count++;}
984         if (have_flag(flgs, TR_BRAND_ELEC)) {tmp_cost += 3800;count++;}
985         if (have_flag(flgs, TR_BRAND_FIRE)) {tmp_cost += 2500;count++;}
986         if (have_flag(flgs, TR_BRAND_COLD)) {tmp_cost += 2500;count++;}
987         total += (tmp_cost * count);
988
989         if (have_flag(flgs, TR_SUST_STR)) total += 850;
990         if (have_flag(flgs, TR_SUST_INT)) total += 850;
991         if (have_flag(flgs, TR_SUST_WIS)) total += 850;
992         if (have_flag(flgs, TR_SUST_DEX)) total += 850;
993         if (have_flag(flgs, TR_SUST_CON)) total += 850;
994         if (have_flag(flgs, TR_SUST_CHR)) total += 250;
995         if (have_flag(flgs, TR_RIDING)) total += 0;
996         if (have_flag(flgs, TR_EASY_SPELL)) total += 1500;
997         if (have_flag(flgs, TR_THROW)) total += 5000;
998         if (have_flag(flgs, TR_FREE_ACT)) total += 4500;
999         if (have_flag(flgs, TR_HOLD_LIFE)) total += 8500;
1000
1001         tmp_cost = 0;
1002         count = 0;
1003         if (have_flag(flgs, TR_IM_ACID)) {tmp_cost += 15000;count += 2;}
1004         if (have_flag(flgs, TR_IM_ELEC)) {tmp_cost += 15000;count += 2;}
1005         if (have_flag(flgs, TR_IM_FIRE)) {tmp_cost += 15000;count += 2;}
1006         if (have_flag(flgs, TR_IM_COLD)) {tmp_cost += 15000;count += 2;}
1007         if (have_flag(flgs, TR_REFLECT)) {tmp_cost += 5000;count += 2;}
1008         if (have_flag(flgs, TR_RES_ACID)) {tmp_cost += 500;count++;}
1009         if (have_flag(flgs, TR_RES_ELEC)) {tmp_cost += 500;count++;}
1010         if (have_flag(flgs, TR_RES_FIRE)) {tmp_cost += 500;count++;}
1011         if (have_flag(flgs, TR_RES_COLD)) {tmp_cost += 500;count++;}
1012         if (have_flag(flgs, TR_RES_POIS)) {tmp_cost += 1000;count += 2;}
1013         if (have_flag(flgs, TR_RES_FEAR)) {tmp_cost += 1000;count += 2;}
1014         if (have_flag(flgs, TR_RES_LITE)) {tmp_cost += 800;count += 2;}
1015         if (have_flag(flgs, TR_RES_DARK)) {tmp_cost += 800;count += 2;}
1016         if (have_flag(flgs, TR_RES_BLIND)) {tmp_cost += 900;count += 2;}
1017         if (have_flag(flgs, TR_RES_CONF)) {tmp_cost += 900;count += 2;}
1018         if (have_flag(flgs, TR_RES_SOUND)) {tmp_cost += 900;count += 2;}
1019         if (have_flag(flgs, TR_RES_SHARDS)) {tmp_cost += 900;count += 2;}
1020         if (have_flag(flgs, TR_RES_NETHER)) {tmp_cost += 900;count += 2;}
1021         if (have_flag(flgs, TR_RES_NEXUS)) {tmp_cost += 900;count += 2;}
1022         if (have_flag(flgs, TR_RES_CHAOS)) {tmp_cost += 1000;count += 2;}
1023         if (have_flag(flgs, TR_RES_DISEN)) {tmp_cost += 2000;count += 2;}
1024         total += (tmp_cost * count);
1025
1026         if (have_flag(flgs, TR_SH_FIRE)) total += 5000;
1027         if (have_flag(flgs, TR_SH_ELEC)) total += 5000;
1028         if (have_flag(flgs, TR_SH_COLD)) total += 5000;
1029         if (have_flag(flgs, TR_NO_TELE)) total -= 10000;
1030         if (have_flag(flgs, TR_NO_MAGIC)) total += 2500;
1031         if (have_flag(flgs, TR_TY_CURSE)) total -= 15000;
1032         if (have_flag(flgs, TR_HIDE_TYPE)) total += 0;
1033         if (have_flag(flgs, TR_SHOW_MODS)) total += 0;
1034         if (have_flag(flgs, TR_LEVITATION)) total += 1250;
1035         if (have_flag(flgs, TR_LITE_1)) total += 1500;
1036         if (have_flag(flgs, TR_LITE_2)) total += 2500;
1037         if (have_flag(flgs, TR_LITE_3)) total += 4000;
1038         if (have_flag(flgs, TR_LITE_M1)) total -= 1500;
1039         if (have_flag(flgs, TR_LITE_M2)) total -= 2500;
1040         if (have_flag(flgs, TR_LITE_M3)) total -= 4000;
1041         if (have_flag(flgs, TR_SEE_INVIS)) total += 2000;
1042         if (have_flag(flgs, TR_TELEPATHY)) total += 20000;
1043         if (have_flag(flgs, TR_ESP_ANIMAL)) total += 1000;
1044         if (have_flag(flgs, TR_ESP_UNDEAD)) total += 1000;
1045         if (have_flag(flgs, TR_ESP_DEMON)) total += 1000;
1046         if (have_flag(flgs, TR_ESP_ORC)) total += 1000;
1047         if (have_flag(flgs, TR_ESP_TROLL)) total += 1000;
1048         if (have_flag(flgs, TR_ESP_GIANT)) total += 1000;
1049         if (have_flag(flgs, TR_ESP_DRAGON)) total += 1000;
1050         if (have_flag(flgs, TR_ESP_HUMAN)) total += 1000;
1051         if (have_flag(flgs, TR_ESP_EVIL)) total += 15000;
1052         if (have_flag(flgs, TR_ESP_GOOD)) total += 2000;
1053         if (have_flag(flgs, TR_ESP_NONLIVING)) total += 2000;
1054         if (have_flag(flgs, TR_ESP_UNIQUE)) total += 10000;
1055         if (have_flag(flgs, TR_SLOW_DIGEST)) total += 750;
1056         if (have_flag(flgs, TR_REGEN)) total += 2500;
1057         if (have_flag(flgs, TR_WARNING)) total += 2000;
1058         if (have_flag(flgs, TR_DEC_MANA)) total += 10000;
1059         if (have_flag(flgs, TR_XTRA_MIGHT)) total += 2250;
1060         if (have_flag(flgs, TR_XTRA_SHOTS)) total += 10000;
1061         if (have_flag(flgs, TR_IGNORE_ACID)) total += 100;
1062         if (have_flag(flgs, TR_IGNORE_ELEC)) total += 100;
1063         if (have_flag(flgs, TR_IGNORE_FIRE)) total += 100;
1064         if (have_flag(flgs, TR_IGNORE_COLD)) total += 100;
1065         if (have_flag(flgs, TR_ACTIVATE)) total += 100;
1066         if (have_flag(flgs, TR_DRAIN_EXP)) total -= 12500;
1067         if (have_flag(flgs, TR_DRAIN_HP)) total -= 12500;
1068         if (have_flag(flgs, TR_DRAIN_MANA)) total -= 12500;
1069         if (have_flag(flgs, TR_CALL_ANIMAL)) total -= 12500;
1070         if (have_flag(flgs, TR_CALL_DEMON)) total -= 10000;
1071         if (have_flag(flgs, TR_CALL_DRAGON)) total -= 10000;
1072         if (have_flag(flgs, TR_CALL_UNDEAD)) total -= 10000;
1073         if (have_flag(flgs, TR_COWARDICE)) total -= 5000;
1074         if (have_flag(flgs, TR_LOW_MELEE)) total -= 5000;
1075         if (have_flag(flgs, TR_LOW_AC)) total -= 5000;
1076         if (have_flag(flgs, TR_LOW_MAGIC)) total -= 15000;
1077         if (have_flag(flgs, TR_FAST_DIGEST)) total -= 10000;
1078         if (have_flag(flgs, TR_SLOW_REGEN)) total -= 10000;
1079         if (have_flag(flgs, TR_TELEPORT))
1080         {
1081                 if (object_is_cursed(o_ptr))
1082                         total -= 7500;
1083                 else
1084                         total += 250;
1085         }
1086         if (have_flag(flgs, TR_AGGRAVATE)) total -= 10000;
1087         if (have_flag(flgs, TR_BLESSED)) total += 750;
1088         if (o_ptr->curse_flags & TR_ADD_L_CURSE) total -= 5000;
1089         if (o_ptr->curse_flags & TR_ADD_H_CURSE) total -= 12500;
1090         if (o_ptr->curse_flags & TRC_CURSED) total -= 5000;
1091         if (o_ptr->curse_flags & TRC_HEAVY_CURSE) total -= 12500;
1092         if (o_ptr->curse_flags & TRC_PERMA_CURSE) total -= 15000;
1093
1094         /* Also, give some extra for activatable powers... */
1095         if (o_ptr->art_name && (have_flag(o_ptr->art_flags, TR_ACTIVATE)))
1096         {
1097                 const activation_type* const act_ptr = find_activation_info(o_ptr);
1098                 if (act_ptr) {
1099                         total += act_ptr->value;
1100                 }
1101         }
1102
1103         return total;
1104 }
1105
1106
1107 /*!
1108  * @brief ¥ª¥Ö¥¸¥§¥¯¥È¤Î¿¿¤Î²Á³Ê¤ò»»½Ð¤¹¤ë /
1109  * Return the value of the flags the object has...
1110  * @param o_ptr ËܲÁ³Ê¤ò³Îǧ¤·¤¿¤¤¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
1111  * @return ¥ª¥Ö¥¸¥§¥¯¥È¤ÎËܲÁ³Ê
1112  * @details
1113  * Return the "real" price of a "known" item, not including discounts\n
1114  *\n
1115  * Wand and staffs get cost for each charge\n
1116  *\n
1117  * Armor is worth an extra 100 gold per bonus point to armor class.\n
1118  *\n
1119  * Weapons are worth an extra 100 gold per bonus point (AC,TH,TD).\n
1120  *\n
1121  * Missiles are only worth 5 gold per bonus point, since they\n
1122  * usually appear in groups of 20, and we want the player to get\n
1123  * the same amount of cash for any "equivalent" item.  Note that\n
1124  * missiles never have any of the "pval" flags, and in fact, they\n
1125  * only have a few of the available flags, primarily of the "slay"\n
1126  * and "brand" and "ignore" variety.\n
1127  *\n
1128  * Armor with a negative armor bonus is worthless.\n
1129  * Weapons with negative hit+damage bonuses are worthless.\n
1130  *\n
1131  * Every wearable item with a "pval" bonus is worth extra (see below).\n
1132  */
1133 s32b object_value_real(object_type *o_ptr)
1134 {
1135         s32b value;
1136
1137         u32b flgs[TR_FLAG_SIZE];
1138
1139         object_kind *k_ptr = &k_info[o_ptr->k_idx];
1140
1141
1142         /* Hack -- "worthless" items */
1143         if (!k_info[o_ptr->k_idx].cost) return (0L);
1144
1145         /* Base cost */
1146         value = k_info[o_ptr->k_idx].cost;
1147
1148         /* Extract some flags */
1149         object_flags(o_ptr, flgs);
1150
1151         /* Artifact */
1152         if (object_is_fixed_artifact(o_ptr))
1153         {
1154                 artifact_type *a_ptr = &a_info[o_ptr->name1];
1155
1156                 /* Hack -- "worthless" artifacts */
1157                 if (!a_ptr->cost) return (0L);
1158
1159                 /* Hack -- Use the artifact cost instead */
1160                 value = a_ptr->cost;
1161                 value += flag_cost(o_ptr, o_ptr->pval);
1162
1163                 /* Don't add pval bonuses etc. */
1164                 return (value);
1165         }
1166
1167         /* Ego-Item */
1168         else if (object_is_ego(o_ptr))
1169         {
1170                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
1171
1172                 /* Hack -- "worthless" ego-items */
1173                 if (!e_ptr->cost) return (0L);
1174
1175                 /* Hack -- Reward the ego-item with a bonus */
1176                 value += e_ptr->cost;
1177                 value += flag_cost(o_ptr, o_ptr->pval);
1178         }
1179
1180         else
1181         {
1182                 int i;
1183                 bool flag = FALSE;
1184
1185                 for (i = 0; i < TR_FLAG_SIZE; i++) 
1186                         if (o_ptr->art_flags[i]) flag = TRUE;
1187
1188                 if (flag) value += flag_cost(o_ptr, o_ptr->pval);
1189         }
1190
1191         /* Analyze pval bonus for normal object */
1192         switch (o_ptr->tval)
1193         {
1194         case TV_SHOT:
1195         case TV_ARROW:
1196         case TV_BOLT:
1197         case TV_BOW:
1198         case TV_DIGGING:
1199         case TV_HAFTED:
1200         case TV_POLEARM:
1201         case TV_SWORD:
1202         case TV_BOOTS:
1203         case TV_GLOVES:
1204         case TV_HELM:
1205         case TV_CROWN:
1206         case TV_SHIELD:
1207         case TV_CLOAK:
1208         case TV_SOFT_ARMOR:
1209         case TV_HARD_ARMOR:
1210         case TV_DRAG_ARMOR:
1211         case TV_LITE:
1212         case TV_AMULET:
1213         case TV_RING:
1214                 /* No pval */
1215                 if (!o_ptr->pval) break;
1216
1217                 /* Hack -- Negative "pval" is always bad */
1218                 if (o_ptr->pval < 0) return (0L);
1219
1220                 /* Give credit for stat bonuses */
1221                 if (have_flag(flgs, TR_STR)) value += (o_ptr->pval * 200L);
1222                 if (have_flag(flgs, TR_INT)) value += (o_ptr->pval * 200L);
1223                 if (have_flag(flgs, TR_WIS)) value += (o_ptr->pval * 200L);
1224                 if (have_flag(flgs, TR_DEX)) value += (o_ptr->pval * 200L);
1225                 if (have_flag(flgs, TR_CON)) value += (o_ptr->pval * 200L);
1226                 if (have_flag(flgs, TR_CHR)) value += (o_ptr->pval * 200L);
1227
1228                 /* Give credit for stealth and searching */
1229                 if (have_flag(flgs, TR_MAGIC_MASTERY)) value += (o_ptr->pval * 100);
1230                 if (have_flag(flgs, TR_STEALTH)) value += (o_ptr->pval * 100L);
1231                 if (have_flag(flgs, TR_SEARCH)) value += (o_ptr->pval * 100L);
1232
1233                 /* Give credit for infra-vision and tunneling */
1234                 if (have_flag(flgs, TR_INFRA)) value += (o_ptr->pval * 50L);
1235                 if (have_flag(flgs, TR_TUNNEL)) value += (o_ptr->pval * 50L);
1236
1237                 /* Give credit for extra attacks */
1238                 if (have_flag(flgs, TR_BLOWS)) value += (o_ptr->pval * 5000L);
1239
1240                 /* Give credit for speed bonus */
1241                 if (have_flag(flgs, TR_SPEED)) value += (o_ptr->pval * 10000L);
1242
1243                 break;
1244         }
1245
1246
1247         /* Analyze the item */
1248         switch (o_ptr->tval)
1249         {
1250                 /* Wands/Staffs */
1251                 case TV_WAND:
1252                 {
1253                         /* Pay extra for charges, depending on standard number of
1254                          * charges.  Handle new-style wands correctly. -LM-
1255                          */
1256                         value += (value * o_ptr->pval / o_ptr->number / (k_ptr->pval * 2));
1257
1258                         /* Done */
1259                         break;
1260                 }
1261                 case TV_STAFF:
1262                 {
1263                         /* Pay extra for charges, depending on standard number of
1264                          * charges.  -LM-
1265                          */
1266                         value += (value * o_ptr->pval / (k_ptr->pval * 2));
1267
1268                         /* Done */
1269                         break;
1270                 }
1271
1272                 /* Rings/Amulets */
1273                 case TV_RING:
1274                 case TV_AMULET:
1275                 {
1276                         /* Hack -- negative bonuses are bad */
1277                         if (o_ptr->to_h + o_ptr->to_d + o_ptr->to_a < 0) return (0L);
1278
1279                         /* Give credit for bonuses */
1280                         value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 200L);
1281
1282                         /* Done */
1283                         break;
1284                 }
1285
1286                 /* Armor */
1287                 case TV_BOOTS:
1288                 case TV_GLOVES:
1289                 case TV_CLOAK:
1290                 case TV_CROWN:
1291                 case TV_HELM:
1292                 case TV_SHIELD:
1293                 case TV_SOFT_ARMOR:
1294                 case TV_HARD_ARMOR:
1295                 case TV_DRAG_ARMOR:
1296                 {
1297                         /* Hack -- negative armor bonus */
1298                         if (o_ptr->to_a < 0) return (0L);
1299
1300                         /* Give credit for bonuses */
1301                         value += (((o_ptr->to_h - k_ptr->to_h) + (o_ptr->to_d - k_ptr->to_d)) * 200L + (o_ptr->to_a) * 100L);
1302
1303                         /* Done */
1304                         break;
1305                 }
1306
1307                 /* Bows/Weapons */
1308                 case TV_BOW:
1309                 case TV_DIGGING:
1310                 case TV_HAFTED:
1311                 case TV_SWORD:
1312                 case TV_POLEARM:
1313                 {
1314                         /* Hack -- negative hit/damage bonuses */
1315                         if (o_ptr->to_h + o_ptr->to_d < 0) return (0L);
1316
1317                         /* Factor in the bonuses */
1318                         value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 100L);
1319
1320                         /* Hack -- Factor in extra damage dice and sides */
1321                         value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 250L;
1322                         value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 250L;
1323
1324                         /* Done */
1325                         break;
1326                 }
1327
1328                 /* Ammo */
1329                 case TV_SHOT:
1330                 case TV_ARROW:
1331                 case TV_BOLT:
1332                 {
1333                         /* Hack -- negative hit/damage bonuses */
1334                         if (o_ptr->to_h + o_ptr->to_d < 0) return (0L);
1335
1336                         /* Factor in the bonuses */
1337                         value += ((o_ptr->to_h + o_ptr->to_d) * 5L);
1338
1339                         /* Hack -- Factor in extra damage dice and sides */
1340                         value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 5L;
1341                         value += (o_ptr->ds - k_ptr->ds) * o_ptr->dd * 5L;
1342
1343                         /* Done */
1344                         break;
1345                 }
1346
1347                 /* Figurines, relative to monster level */
1348                 case TV_FIGURINE:
1349                 {
1350                         int level = r_info[o_ptr->pval].level;
1351                         if (level < 20) value = level*50L;
1352                         else if (level < 30) value = 1000+(level-20)*150L;
1353                         else if (level < 40) value = 2500+(level-30)*350L;
1354                         else if (level < 50) value = 6000+(level-40)*800L;
1355                         else value = 14000+(level-50)*2000L;
1356                         break;
1357                 }
1358
1359                 case TV_CAPTURE:
1360                 {
1361                         if (!o_ptr->pval) value = 1000L;
1362                         else value = ((r_info[o_ptr->pval].level) * 50L + 1000);
1363                         break;
1364                 }
1365
1366                 case TV_CHEST:
1367                 {
1368                         if (!o_ptr->pval) value = 0L;
1369                         break;
1370                 }
1371         }
1372
1373         /* Worthless object */
1374         if (value < 0) return 0L;
1375
1376         /* Return the value */
1377         return (value);
1378 }
1379
1380
1381 /*!
1382  * @brief ¥ª¥Ö¥¸¥§¥¯¥È²Á³Ê»»½Ð¤Î¥á¥¤¥ó¥ë¡¼¥Á¥ó /
1383  * Return the price of an item including plusses (and charges)
1384  * @param o_ptr È½ÌÀ¤·¤Æ¤¤¤ë¸½²Á³Ê¤ò³Îǧ¤·¤¿¤¤¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
1385  * @return ¥ª¥Ö¥¸¥§¥¯¥È¤ÎȽÌÀ¤·¤Æ¤¤¤ë¸½²Á³Ê
1386  * @details
1387  * This function returns the "value" of the given item (qty one)\n
1388  *\n
1389  * Never notice "unknown" bonuses or properties, including "curses",\n
1390  * since that would give the player information he did not have.\n
1391  *\n
1392  * Note that discounted items stay discounted forever, even if\n
1393  * the discount is "forgotten" by the player via memory loss.\n
1394  */
1395 s32b object_value(object_type *o_ptr)
1396 {
1397         s32b value;
1398
1399
1400         /* Unknown items -- acquire a base value */
1401         if (object_is_known(o_ptr))
1402         {
1403                 /* Broken items -- worthless */
1404                 if (object_is_broken(o_ptr)) return (0L);
1405
1406                 /* Cursed items -- worthless */
1407                 if (object_is_cursed(o_ptr)) return (0L);
1408
1409                 /* Real value (see above) */
1410                 value = object_value_real(o_ptr);
1411         }
1412
1413         /* Known items -- acquire the actual value */
1414         else
1415         {
1416                 /* Hack -- Felt broken items */
1417                 if ((o_ptr->ident & (IDENT_SENSE)) && object_is_broken(o_ptr)) return (0L);
1418
1419                 /* Hack -- Felt cursed items */
1420                 if ((o_ptr->ident & (IDENT_SENSE)) && object_is_cursed(o_ptr)) return (0L);
1421
1422                 /* Base value (see above) */
1423                 value = object_value_base(o_ptr);
1424         }
1425
1426
1427         /* Apply discount (if any) */
1428         if (o_ptr->discount) value -= (value * o_ptr->discount / 100L);
1429
1430
1431         /* Return the final value */
1432         return (value);
1433 }
1434
1435
1436
1437 /*!
1438  * @brief Ç˲õ²Äǽ¤Ê¥¢¥¤¥Æ¥à¤«¤òÊÖ¤¹ /
1439  * Determines whether an object can be destroyed, and makes fake inscription.
1440  * @param o_ptr Ç˲õ²Äǽ¤«¤ò³Îǧ¤·¤¿¤¤¥ª¥Ö¥¸¥§¥¯¥È¤Î¹½Â¤Âλ²¾È¥Ý¥¤¥ó¥¿
1441  * @return ¥ª¥Ö¥¸¥§¥¯¥È¤¬Ç˲õ²Äǽ¤Ê¤é¤ÐTRUE¤òÊÖ¤¹
1442  */
1443 bool can_player_destroy_object(object_type *o_ptr)
1444 {
1445         /* Artifacts cannot be destroyed */
1446         if (!object_is_artifact(o_ptr)) return TRUE;
1447
1448         /* If object is unidentified, makes fake inscription */
1449         if (!object_is_known(o_ptr))
1450         {
1451                 byte feel = FEEL_SPECIAL;
1452
1453                 /* Hack -- Handle icky artifacts */
1454                 if (object_is_cursed(o_ptr) || object_is_broken(o_ptr)) feel = FEEL_TERRIBLE;
1455
1456                 /* Hack -- inscribe the artifact */
1457                 o_ptr->feeling = feel;
1458
1459                 /* We have "felt" it (again) */
1460                 o_ptr->ident |= (IDENT_SENSE);
1461
1462                 /* Combine the pack */
1463                 p_ptr->notice |= (PN_COMBINE);
1464
1465                 /* Window stuff */
1466                 p_ptr->window |= (PW_INVEN | PW_EQUIP);
1467
1468                 /* Done */
1469                 return FALSE;
1470         }
1471
1472         /* Identified artifact -- Nothing to do */
1473         return FALSE;
1474 }
1475
1476
1477 /*
1478  * Distribute charges of rods or wands.
1479  *
1480  * o_ptr = source item
1481  * q_ptr = target item, must be of the same type as o_ptr
1482  * amt   = number of items that are transfered
1483  */
1484 void distribute_charges(object_type *o_ptr, object_type *q_ptr, int amt)
1485 {
1486         /*
1487          * Hack -- If rods or wands are dropped, the total maximum timeout or
1488          * charges need to be allocated between the two stacks.  If all the items
1489          * are being dropped, it makes for a neater message to leave the original
1490          * stack's pval alone. -LM-
1491          */
1492         if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_ROD))
1493         {
1494                 q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
1495                 if (amt < o_ptr->number) o_ptr->pval -= q_ptr->pval;
1496
1497                 /* Hack -- Rods also need to have their timeouts distributed.  The
1498                  * dropped stack will accept all time remaining to charge up to its
1499                  * maximum.
1500                  */
1501                 if ((o_ptr->tval == TV_ROD) && (o_ptr->timeout))
1502                 {
1503                         if (q_ptr->pval > o_ptr->timeout)
1504                                 q_ptr->timeout = o_ptr->timeout;
1505                         else
1506                                 q_ptr->timeout = q_ptr->pval;
1507
1508                         if (amt < o_ptr->number) o_ptr->timeout -= q_ptr->timeout;
1509                 }
1510         }
1511 }
1512
1513 void reduce_charges(object_type *o_ptr, int amt)
1514 {
1515         /*
1516          * Hack -- If rods or wand are destroyed, the total maximum timeout or
1517          * charges of the stack needs to be reduced, unless all the items are
1518          * being destroyed. -LM-
1519          */
1520         if (((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_ROD)) &&
1521                 (amt < o_ptr->number))
1522         {
1523                 o_ptr->pval -= o_ptr->pval * amt / o_ptr->number;
1524         }
1525 }
1526
1527
1528 /*
1529  * Determine if an item can "absorb" a second item
1530  *
1531  * See "object_absorb()" for the actual "absorption" code.
1532  *
1533  * If permitted, we allow staffs (if they are known to have equal charges
1534  * and both are either known or confirmed empty) and wands (if both are
1535  * either known or confirmed empty) and rods (in all cases) to combine.
1536  * Staffs will unstack (if necessary) when they are used, but wands and
1537  * rods will only unstack if one is dropped. -LM-
1538  *
1539  * If permitted, we allow weapons/armor to stack, if fully "known".
1540  *
1541  * Missiles will combine if both stacks have the same "known" status.
1542  * This is done to make unidentified stacks of missiles useful.
1543  *
1544  * Food, potions, scrolls, and "easy know" items always stack.
1545  *
1546  * Chests, and activatable items, never stack (for various reasons).
1547  */
1548
1549 /*
1550  * A "stack" of items is limited to less than or equal to 99 items (hard-coded).
1551  */
1552 #define MAX_STACK_SIZE 99
1553
1554
1555 /*
1556  *  Determine if an item can partly absorb a second item.
1557  *  Return maximum number of stack.
1558  */
1559 int object_similar_part(object_type *o_ptr, object_type *j_ptr)
1560 {
1561         int i;
1562
1563         /* Default maximum number of stack */
1564         int max_num = MAX_STACK_SIZE;
1565
1566         /* Require identical object types */
1567         if (o_ptr->k_idx != j_ptr->k_idx) return 0;
1568
1569
1570         /* Analyze the items */
1571         switch (o_ptr->tval)
1572         {
1573                 /* Chests and Statues*/
1574                 case TV_CHEST:
1575                 case TV_CARD:
1576                 case TV_CAPTURE:
1577                 {
1578                         /* Never okay */
1579                         return 0;
1580                 }
1581
1582                 case TV_STATUE:
1583                 {
1584                         if ((o_ptr->sval != SV_PHOTO) || (j_ptr->sval != SV_PHOTO)) return 0;
1585                         if (o_ptr->pval != j_ptr->pval) return 0;
1586                         break;
1587                 }
1588
1589                 /* Figurines and Corpses*/
1590                 case TV_FIGURINE:
1591                 case TV_CORPSE:
1592                 {
1593                         /* Same monster */
1594                         if (o_ptr->pval != j_ptr->pval) return 0;
1595
1596                         /* Assume okay */
1597                         break;
1598                 }
1599
1600                 /* Food and Potions and Scrolls */
1601                 case TV_FOOD:
1602                 case TV_POTION:
1603                 case TV_SCROLL:
1604                 {
1605                         /* Assume okay */
1606                         break;
1607                 }
1608
1609                 /* Staffs */
1610                 case TV_STAFF:
1611                 {
1612                         /* Require either knowledge or known empty for both staffs. */
1613                         if ((!(o_ptr->ident & (IDENT_EMPTY)) &&
1614                                 !object_is_known(o_ptr)) ||
1615                                 (!(j_ptr->ident & (IDENT_EMPTY)) &&
1616                                 !object_is_known(j_ptr))) return 0;
1617
1618                         /* Require identical charges, since staffs are bulky. */
1619                         if (o_ptr->pval != j_ptr->pval) return 0;
1620
1621                         /* Assume okay */
1622                         break;
1623                 }
1624
1625                 /* Wands */
1626                 case TV_WAND:
1627                 {
1628                         /* Require either knowledge or known empty for both wands. */
1629                         if ((!(o_ptr->ident & (IDENT_EMPTY)) &&
1630                                 !object_is_known(o_ptr)) ||
1631                                 (!(j_ptr->ident & (IDENT_EMPTY)) &&
1632                                 !object_is_known(j_ptr))) return 0;
1633
1634                         /* Wand charges combine in O&ZAngband.  */
1635
1636                         /* Assume okay */
1637                         break;
1638                 }
1639
1640                 /* Staffs and Wands and Rods */
1641                 case TV_ROD:
1642                 {
1643                         /* Prevent overflaw of timeout */
1644                         max_num = MIN(max_num, MAX_SHORT / k_info[o_ptr->k_idx].pval);
1645
1646                         /* Assume okay */
1647                         break;
1648                 }
1649
1650                 /* Weapons and Armor */
1651                 case TV_BOW:
1652                 case TV_DIGGING:
1653                 case TV_HAFTED:
1654                 case TV_POLEARM:
1655                 case TV_SWORD:
1656                 case TV_BOOTS:
1657                 case TV_GLOVES:
1658                 case TV_HELM:
1659                 case TV_CROWN:
1660                 case TV_SHIELD:
1661                 case TV_CLOAK:
1662                 case TV_SOFT_ARMOR:
1663                 case TV_HARD_ARMOR:
1664                 case TV_DRAG_ARMOR:
1665
1666                 /* Rings, Amulets, Lites */
1667                 case TV_RING:
1668                 case TV_AMULET:
1669                 case TV_LITE:
1670                 case TV_WHISTLE:
1671                 {
1672                         /* Require full knowledge of both items */
1673                         if (!object_is_known(o_ptr) || !object_is_known(j_ptr)) return 0;
1674
1675                         /* Fall through */
1676                 }
1677
1678                 /* Missiles */
1679                 case TV_BOLT:
1680                 case TV_ARROW:
1681                 case TV_SHOT:
1682                 {
1683                         /* Require identical knowledge of both items */
1684                         if (object_is_known(o_ptr) != object_is_known(j_ptr)) return 0;
1685                         if (o_ptr->feeling != j_ptr->feeling) return 0;
1686
1687                         /* Require identical "bonuses" */
1688                         if (o_ptr->to_h != j_ptr->to_h) return 0;
1689                         if (o_ptr->to_d != j_ptr->to_d) return 0;
1690                         if (o_ptr->to_a != j_ptr->to_a) return 0;
1691
1692                         /* Require identical "pval" code */
1693                         if (o_ptr->pval != j_ptr->pval) return 0;
1694
1695                         /* Artifacts never stack */
1696                         if (object_is_artifact(o_ptr) || object_is_artifact(j_ptr)) return 0;
1697
1698                         /* Require identical "ego-item" names */
1699                         if (o_ptr->name2 != j_ptr->name2) return 0;
1700
1701                         /* Require identical added essence  */
1702                         if (o_ptr->xtra3 != j_ptr->xtra3) return 0;
1703                         if (o_ptr->xtra4 != j_ptr->xtra4) return 0;
1704
1705                         /* Hack -- Never stack "powerful" items */
1706                         if (o_ptr->xtra1 || j_ptr->xtra1) return 0;
1707
1708                         /* Hack -- Never stack recharging items */
1709                         if (o_ptr->timeout || j_ptr->timeout) return 0;
1710
1711                         /* Require identical "values" */
1712                         if (o_ptr->ac != j_ptr->ac) return 0;
1713                         if (o_ptr->dd != j_ptr->dd) return 0;
1714                         if (o_ptr->ds != j_ptr->ds) return 0;
1715
1716                         /* Probably okay */
1717                         break;
1718                 }
1719
1720                 /* Various */
1721                 default:
1722                 {
1723                         /* Require knowledge */
1724                         if (!object_is_known(o_ptr) || !object_is_known(j_ptr)) return 0;
1725
1726                         /* Probably okay */
1727                         break;
1728                 }
1729         }
1730
1731
1732         /* Hack -- Identical art_flags! */
1733         for (i = 0; i < TR_FLAG_SIZE; i++)
1734                 if (o_ptr->art_flags[i] != j_ptr->art_flags[i]) return 0;
1735
1736         /* Hack -- Require identical "cursed" status */
1737         if (o_ptr->curse_flags != j_ptr->curse_flags) return 0;
1738
1739         /* Hack -- Require identical "broken" status */
1740         if ((o_ptr->ident & (IDENT_BROKEN)) != (j_ptr->ident & (IDENT_BROKEN))) return 0;
1741
1742
1743         /* Hack -- require semi-matching "inscriptions" */
1744         if (o_ptr->inscription && j_ptr->inscription &&
1745             (o_ptr->inscription != j_ptr->inscription))
1746                 return 0;
1747
1748         /* Hack -- normally require matching "inscriptions" */
1749         if (!stack_force_notes && (o_ptr->inscription != j_ptr->inscription)) return 0;
1750
1751         /* Hack -- normally require matching "discounts" */
1752         if (!stack_force_costs && (o_ptr->discount != j_ptr->discount)) return 0;
1753
1754
1755         /* They match, so they must be similar */
1756         return max_num;
1757 }
1758
1759 /*
1760  *  Determine if an item can absorb a second item.
1761  */
1762 bool object_similar(object_type *o_ptr, object_type *j_ptr)
1763 {
1764         int total = o_ptr->number + j_ptr->number;
1765         int max_num;
1766
1767         /* Are these objects similar? */
1768         max_num = object_similar_part(o_ptr, j_ptr);
1769
1770         /* Return if not similar */
1771         if (!max_num) return FALSE;
1772
1773         /* Maximal "stacking" limit */
1774         if (total > max_num) return (0);
1775
1776
1777         /* They match, so they must be similar */
1778         return (TRUE);
1779 }
1780
1781
1782
1783 /*
1784  * Allow one item to "absorb" another, assuming they are similar
1785  */
1786 void object_absorb(object_type *o_ptr, object_type *j_ptr)
1787 {
1788         int max_num = object_similar_part(o_ptr, j_ptr);
1789         int total = o_ptr->number + j_ptr->number;
1790         int diff = (total > max_num) ? total - max_num : 0;
1791
1792         /* Combine quantity, lose excess items */
1793         o_ptr->number = (total > max_num) ? max_num : total;
1794
1795         /* Hack -- blend "known" status */
1796         if (object_is_known(j_ptr)) object_known(o_ptr);
1797
1798         /* Hack -- clear "storebought" if only one has it */
1799         if (((o_ptr->ident & IDENT_STORE) || (j_ptr->ident & IDENT_STORE)) &&
1800             (!((o_ptr->ident & IDENT_STORE) && (j_ptr->ident & IDENT_STORE))))
1801         {
1802                 if (j_ptr->ident & IDENT_STORE) j_ptr->ident &= 0xEF;
1803                 if (o_ptr->ident & IDENT_STORE) o_ptr->ident &= 0xEF;
1804         }
1805
1806         /* Hack -- blend "mental" status */
1807         if (j_ptr->ident & (IDENT_MENTAL)) o_ptr->ident |= (IDENT_MENTAL);
1808
1809         /* Hack -- blend "inscriptions" */
1810         if (j_ptr->inscription) o_ptr->inscription = j_ptr->inscription;
1811
1812         /* Hack -- blend "feelings" */
1813         if (j_ptr->feeling) o_ptr->feeling = j_ptr->feeling;
1814
1815         /* Hack -- could average discounts XXX XXX XXX */
1816         /* Hack -- save largest discount XXX XXX XXX */
1817         if (o_ptr->discount < j_ptr->discount) o_ptr->discount = j_ptr->discount;
1818
1819         /* Hack -- if rods are stacking, add the pvals (maximum timeouts) and current timeouts together. -LM- */
1820         if (o_ptr->tval == TV_ROD)
1821         {
1822                 o_ptr->pval += j_ptr->pval * (j_ptr->number - diff) / j_ptr->number;
1823                 o_ptr->timeout += j_ptr->timeout * (j_ptr->number - diff) / j_ptr->number;
1824         }
1825
1826         /* Hack -- if wands are stacking, combine the charges. -LM- */
1827         if (o_ptr->tval == TV_WAND)
1828         {
1829                 o_ptr->pval += j_ptr->pval * (j_ptr->number - diff) / j_ptr->number;
1830         }
1831 }
1832
1833
1834 /*
1835  * Find the index of the object_kind with the given tval and sval
1836  */
1837 s16b lookup_kind(int tval, int sval)
1838 {
1839         int k;
1840         int num = 0;
1841         int bk = 0;
1842
1843         /* Look for it */
1844         for (k = 1; k < max_k_idx; k++)
1845         {
1846                 object_kind *k_ptr = &k_info[k];
1847
1848                 /* Require correct tval */
1849                 if (k_ptr->tval != tval) continue;
1850
1851                 /* Found a match */
1852                 if (k_ptr->sval == sval) return (k);
1853
1854                 /* Ignore illegal items */
1855                 if (sval != SV_ANY) continue;
1856
1857                 /* Apply the randomizer */
1858                 if (!one_in_(++num)) continue;
1859
1860                 /* Use this value */
1861                 bk = k;
1862         }
1863
1864         /* Return this choice */
1865         if (sval == SV_ANY)
1866         {
1867                 return bk;
1868         }
1869
1870 #if 0
1871         /* Oops */
1872 #ifdef JP
1873         msg_format("¥¢¥¤¥Æ¥à¤¬¤Ê¤¤ (%d,%d)", tval, sval);
1874 #else
1875         msg_format("No object (%d,%d)", tval, sval);
1876 #endif
1877 #endif
1878
1879
1880         /* Oops */
1881         return (0);
1882 }
1883
1884
1885 /*
1886  * Wipe an object clean.
1887  */
1888 void object_wipe(object_type *o_ptr)
1889 {
1890         /* Wipe the structure */
1891         (void)WIPE(o_ptr, object_type);
1892 }
1893
1894
1895 /*
1896  * Prepare an object based on an existing object
1897  */
1898 void object_copy(object_type *o_ptr, object_type *j_ptr)
1899 {
1900         /* Copy the structure */
1901         (void)COPY(o_ptr, j_ptr, object_type);
1902 }
1903
1904
1905 /*
1906  * Prepare an object based on an object kind.
1907  */
1908 void object_prep(object_type *o_ptr, int k_idx)
1909 {
1910         object_kind *k_ptr = &k_info[k_idx];
1911
1912         /* Clear the record */
1913         object_wipe(o_ptr);
1914
1915         /* Save the kind index */
1916         o_ptr->k_idx = k_idx;
1917
1918         /* Efficiency -- tval/sval */
1919         o_ptr->tval = k_ptr->tval;
1920         o_ptr->sval = k_ptr->sval;
1921
1922         /* Default "pval" */
1923         o_ptr->pval = k_ptr->pval;
1924
1925         /* Default number */
1926         o_ptr->number = 1;
1927
1928         /* Default weight */
1929         o_ptr->weight = k_ptr->weight;
1930
1931         /* Default magic */
1932         o_ptr->to_h = k_ptr->to_h;
1933         o_ptr->to_d = k_ptr->to_d;
1934         o_ptr->to_a = k_ptr->to_a;
1935
1936         /* Default power */
1937         o_ptr->ac = k_ptr->ac;
1938         o_ptr->dd = k_ptr->dd;
1939         o_ptr->ds = k_ptr->ds;
1940
1941         /* Default activation */
1942         if (k_ptr->act_idx > 0) o_ptr->xtra2 = k_ptr->act_idx;
1943
1944         /* Hack -- worthless items are always "broken" */
1945         if (k_info[o_ptr->k_idx].cost <= 0) o_ptr->ident |= (IDENT_BROKEN);
1946
1947         /* Hack -- cursed items are always "cursed" */
1948         if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
1949         if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
1950         if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
1951         if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
1952         if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
1953         if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
1954 }
1955
1956
1957 /*
1958  * Help determine an "enchantment bonus" for an object.
1959  *
1960  * To avoid floating point but still provide a smooth distribution of bonuses,
1961  * we simply round the results of division in such a way as to "average" the
1962  * correct floating point value.
1963  *
1964  * This function has been changed.  It uses "randnor()" to choose values from
1965  * a normal distribution, whose mean moves from zero towards the max as the
1966  * level increases, and whose standard deviation is equal to 1/4 of the max,
1967  * and whose values are forced to lie between zero and the max, inclusive.
1968  *
1969  * Since the "level" rarely passes 100 before Morgoth is dead, it is very
1970  * rare to get the "full" enchantment on an object, even a deep levels.
1971  *
1972  * It is always possible (albeit unlikely) to get the "full" enchantment.
1973  *
1974  * A sample distribution of values from "m_bonus(10, N)" is shown below:
1975  *
1976  *   N       0     1     2     3     4     5     6     7     8     9    10
1977  * ---    ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----
1978  *   0   66.37 13.01  9.73  5.47  2.89  1.31  0.72  0.26  0.12  0.09  0.03
1979  *   8   46.85 24.66 12.13  8.13  4.20  2.30  1.05  0.36  0.19  0.08  0.05
1980  *  16   30.12 27.62 18.52 10.52  6.34  3.52  1.95  0.90  0.31  0.15  0.05
1981  *  24   22.44 15.62 30.14 12.92  8.55  5.30  2.39  1.63  0.62  0.28  0.11
1982  *  32   16.23 11.43 23.01 22.31 11.19  7.18  4.46  2.13  1.20  0.45  0.41
1983  *  40   10.76  8.91 12.80 29.51 16.00  9.69  5.90  3.43  1.47  0.88  0.65
1984  *  48    7.28  6.81 10.51 18.27 27.57 11.76  7.85  4.99  2.80  1.22  0.94
1985  *  56    4.41  4.73  8.52 11.96 24.94 19.78 11.06  7.18  3.68  1.96  1.78
1986  *  64    2.81  3.07  5.65  9.17 13.01 31.57 13.70  9.30  6.04  3.04  2.64
1987  *  72    1.87  1.99  3.68  7.15 10.56 20.24 25.78 12.17  7.52  4.42  4.62
1988  *  80    1.02  1.23  2.78  4.75  8.37 12.04 27.61 18.07 10.28  6.52  7.33
1989  *  88    0.70  0.57  1.56  3.12  6.34 10.06 15.76 30.46 12.58  8.47 10.38
1990  *  96    0.27  0.60  1.25  2.28  4.30  7.60 10.77 22.52 22.51 11.37 16.53
1991  * 104    0.22  0.42  0.77  1.36  2.62  5.33  8.93 13.05 29.54 15.23 22.53
1992  * 112    0.15  0.20  0.56  0.87  2.00  3.83  6.86 10.06 17.89 27.31 30.27
1993  * 120    0.03  0.11  0.31  0.46  1.31  2.48  4.60  7.78 11.67 25.53 45.72
1994  * 128    0.02  0.01  0.13  0.33  0.83  1.41  3.24  6.17  9.57 14.22 64.07
1995  */
1996 s16b m_bonus(int max, int level)
1997 {
1998         int bonus, stand, extra, value;
1999
2000
2001         /* Paranoia -- enforce maximal "level" */
2002         if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
2003
2004
2005         /* The "bonus" moves towards the max */
2006         bonus = ((max * level) / MAX_DEPTH);
2007
2008         /* Hack -- determine fraction of error */
2009         extra = ((max * level) % MAX_DEPTH);
2010
2011         /* Hack -- simulate floating point computations */
2012         if (randint0(MAX_DEPTH) < extra) bonus++;
2013
2014
2015         /* The "stand" is equal to one quarter of the max */
2016         stand = (max / 4);
2017
2018         /* Hack -- determine fraction of error */
2019         extra = (max % 4);
2020
2021         /* Hack -- simulate floating point computations */
2022         if (randint0(4) < extra) stand++;
2023
2024
2025         /* Choose an "interesting" value */
2026         value = randnor(bonus, stand);
2027
2028         /* Enforce the minimum value */
2029         if (value < 0) return (0);
2030
2031         /* Enforce the maximum value */
2032         if (value > max) return (max);
2033
2034         /* Result */
2035         return (value);
2036 }
2037
2038
2039 /*
2040  * Cheat -- describe a created object for the user
2041  */
2042 static void object_mention(object_type *o_ptr)
2043 {
2044         char o_name[MAX_NLEN];
2045
2046         /* Describe */
2047         object_desc(o_name, o_ptr, (OD_NAME_ONLY | OD_STORE));
2048
2049         /* Artifact */
2050         if (object_is_fixed_artifact(o_ptr))
2051         {
2052                 /* Silly message */
2053 #ifdef JP
2054                 msg_format("ÅÁÀâ¤Î¥¢¥¤¥Æ¥à (%s)", o_name);
2055 #else
2056                 msg_format("Artifact (%s)", o_name);
2057 #endif
2058
2059         }
2060
2061         /* Random Artifact */
2062         else if (o_ptr->art_name)
2063         {
2064 #ifdef JP
2065                 msg_print("¥é¥ó¥À¥à¡¦¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È");
2066 #else
2067                 msg_print("Random artifact");
2068 #endif
2069
2070         }
2071
2072         /* Ego-item */
2073         else if (object_is_ego(o_ptr))
2074         {
2075                 /* Silly message */
2076 #ifdef JP
2077                 msg_format("̾¤Î¤¢¤ë¥¢¥¤¥Æ¥à (%s)", o_name);
2078 #else
2079                 msg_format("Ego-item (%s)", o_name);
2080 #endif
2081
2082         }
2083
2084         /* Normal item */
2085         else
2086         {
2087                 /* Silly message */
2088 #ifdef JP
2089                 msg_format("¥¢¥¤¥Æ¥à (%s)", o_name);
2090 #else
2091                 msg_format("Object (%s)", o_name);
2092 #endif
2093
2094         }
2095 }
2096
2097
2098 /*
2099  * Mega-Hack -- Attempt to create one of the "Special Objects"
2100  *
2101  * We are only called from "make_object()", and we assume that
2102  * "apply_magic()" is called immediately after we return.
2103  *
2104  * Note -- see "make_artifact()" and "apply_magic()"
2105  */
2106 static bool make_artifact_special(object_type *o_ptr)
2107 {
2108         int i;
2109         int k_idx = 0;
2110
2111
2112         /* No artifacts in the town */
2113         if (!dun_level) return (FALSE);
2114
2115         /* Themed object */
2116         if (get_obj_num_hook) return (FALSE);
2117
2118         /* Check the artifact list (just the "specials") */
2119         for (i = 0; i < max_a_idx; i++)
2120         {
2121                 artifact_type *a_ptr = &a_info[i];
2122
2123                 /* Skip "empty" artifacts */
2124                 if (!a_ptr->name) continue;
2125
2126                 /* Cannot make an artifact twice */
2127                 if (a_ptr->cur_num) continue;
2128
2129                 if (a_ptr->gen_flags & TRG_QUESTITEM) continue;
2130                 if (!(a_ptr->gen_flags & TRG_INSTA_ART)) continue;
2131
2132                 /* XXX XXX Enforce minimum "depth" (loosely) */
2133                 if (a_ptr->level > dun_level)
2134                 {
2135                         /* Acquire the "out-of-depth factor" */
2136                         int d = (a_ptr->level - dun_level) * 2;
2137
2138                         /* Roll for out-of-depth creation */
2139                         if (!one_in_(d)) continue;
2140                 }
2141
2142                 /* Artifact "rarity roll" */
2143                 if (!one_in_(a_ptr->rarity)) continue;
2144
2145                 /* Find the base object */
2146                 k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
2147
2148                 /* XXX XXX Enforce minimum "object" level (loosely) */
2149                 if (k_info[k_idx].level > object_level)
2150                 {
2151                         /* Acquire the "out-of-depth factor" */
2152                         int d = (k_info[k_idx].level - object_level) * 5;
2153
2154                         /* Roll for out-of-depth creation */
2155                         if (!one_in_(d)) continue;
2156                 }
2157
2158                 /* Assign the template */
2159                 object_prep(o_ptr, k_idx);
2160
2161                 /* Mega-Hack -- mark the item as an artifact */
2162                 o_ptr->name1 = i;
2163
2164                 /* Hack: Some artifacts get random extra powers */
2165                 random_artifact_resistance(o_ptr, a_ptr);
2166
2167                 /* Success */
2168                 return (TRUE);
2169         }
2170
2171         /* Failure */
2172         return (FALSE);
2173 }
2174
2175
2176 /*
2177  * Attempt to change an object into an artifact
2178  *
2179  * This routine should only be called by "apply_magic()"
2180  *
2181  * Note -- see "make_artifact_special()" and "apply_magic()"
2182  */
2183 static bool make_artifact(object_type *o_ptr)
2184 {
2185         int i;
2186
2187
2188         /* No artifacts in the town */
2189         if (!dun_level) return (FALSE);
2190
2191         /* Paranoia -- no "plural" artifacts */
2192         if (o_ptr->number != 1) return (FALSE);
2193
2194         /* Check the artifact list (skip the "specials") */
2195         for (i = 0; i < max_a_idx; i++)
2196         {
2197                 artifact_type *a_ptr = &a_info[i];
2198
2199                 /* Skip "empty" items */
2200                 if (!a_ptr->name) continue;
2201
2202                 /* Cannot make an artifact twice */
2203                 if (a_ptr->cur_num) continue;
2204
2205                 if (a_ptr->gen_flags & TRG_QUESTITEM) continue;
2206
2207                 if (a_ptr->gen_flags & TRG_INSTA_ART) continue;
2208
2209                 /* Must have the correct fields */
2210                 if (a_ptr->tval != o_ptr->tval) continue;
2211                 if (a_ptr->sval != o_ptr->sval) continue;
2212
2213                 /* XXX XXX Enforce minimum "depth" (loosely) */
2214                 if (a_ptr->level > dun_level)
2215                 {
2216                         /* Acquire the "out-of-depth factor" */
2217                         int d = (a_ptr->level - dun_level) * 2;
2218
2219                         /* Roll for out-of-depth creation */
2220                         if (!one_in_(d)) continue;
2221                 }
2222
2223                 /* We must make the "rarity roll" */
2224                 if (!one_in_(a_ptr->rarity)) continue;
2225
2226                 /* Hack -- mark the item as an artifact */
2227                 o_ptr->name1 = i;
2228
2229                 /* Hack: Some artifacts get random extra powers */
2230                 random_artifact_resistance(o_ptr, a_ptr);
2231
2232                 /* Success */
2233                 return (TRUE);
2234         }
2235
2236         /* Failure */
2237         return (FALSE);
2238 }
2239
2240
2241 /*
2242  *  Choose random ego type
2243  */
2244 static byte get_random_ego(byte slot, bool good)
2245 {
2246         int i, value;
2247         ego_item_type *e_ptr;
2248
2249         long total = 0L;
2250         
2251         for (i = 1; i < max_e_idx; i++)
2252         {
2253                 e_ptr = &e_info[i];
2254                 
2255                 if (e_ptr->slot == slot
2256                     && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
2257                 {
2258                         if (e_ptr->rarity)
2259                                 total += (255 / e_ptr->rarity);
2260                 }
2261         }
2262
2263         value = randint1(total);
2264
2265         for (i = 1; i < max_e_idx; i++)
2266         {
2267                 e_ptr = &e_info[i];
2268                 
2269                 if (e_ptr->slot == slot
2270                     && ((good && e_ptr->rating) || (!good && !e_ptr->rating)) )
2271                 {
2272                         if (e_ptr->rarity)
2273                                 value -= (255 / e_ptr->rarity);
2274                         if (value <= 0L) break;
2275                 }
2276         }
2277         return (byte)i;
2278 }
2279
2280
2281 /*
2282  * Apply magic to an item known to be a "weapon"
2283  *
2284  * Hack -- note special base damage dice boosting
2285  * Hack -- note special processing for weapon/digger
2286  */
2287 static void a_m_aux_1(object_type *o_ptr, int level, int power)
2288 {
2289         int tohit1 = randint1(5) + m_bonus(5, level);
2290         int todam1 = randint1(5) + m_bonus(5, level);
2291
2292         int tohit2 = m_bonus(10, level);
2293         int todam2 = m_bonus(10, level);
2294
2295         if ((o_ptr->tval == TV_BOLT) || (o_ptr->tval == TV_ARROW) || (o_ptr->tval == TV_SHOT))
2296         {
2297                 tohit2 = (tohit2+1)/2;
2298                 todam2 = (todam2+1)/2;
2299         }
2300
2301         /* Good */
2302         if (power > 0)
2303         {
2304                 /* Enchant */
2305                 o_ptr->to_h += tohit1;
2306                 o_ptr->to_d += todam1;
2307
2308                 /* Very good */
2309                 if (power > 1)
2310                 {
2311                         /* Enchant again */
2312                         o_ptr->to_h += tohit2;
2313                         o_ptr->to_d += todam2;
2314                 }
2315         }
2316
2317         /* Cursed */
2318         else if (power < 0)
2319         {
2320                 /* Penalize */
2321                 o_ptr->to_h -= tohit1;
2322                 o_ptr->to_d -= todam1;
2323
2324                 /* Very cursed */
2325                 if (power < -1)
2326                 {
2327                         /* Penalize again */
2328                         o_ptr->to_h -= tohit2;
2329                         o_ptr->to_d -= todam2;
2330                 }
2331
2332                 /* Cursed (if "bad") */
2333                 if (o_ptr->to_h + o_ptr->to_d < 0) o_ptr->curse_flags |= TRC_CURSED;
2334         }
2335
2336         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_DIAMOND_EDGE)) return;
2337
2338         /* Analyze type */
2339         switch (o_ptr->tval)
2340         {
2341                 case TV_DIGGING:
2342                 {
2343                         /* Very good */
2344                         if (power > 1)
2345                         {
2346                                 if (one_in_(30) || (power > 2)) /* power > 2 is debug only */
2347                                         create_artifact(o_ptr, FALSE);
2348                                 else
2349                                         /* Special Ego-item */
2350                                         o_ptr->name2 = EGO_DIGGING;
2351                         }
2352
2353                         /* Very bad */
2354                         else if (power < -1)
2355                         {
2356                                 /* Hack -- Horrible digging bonus */
2357                                 o_ptr->pval = 0 - (5 + randint1(5));
2358                         }
2359
2360                         /* Bad */
2361                         else if (power < 0)
2362                         {
2363                                 /* Hack -- Reverse digging bonus */
2364                                 o_ptr->pval = 0 - (o_ptr->pval);
2365                         }
2366
2367                         break;
2368                 }
2369
2370
2371                 case TV_HAFTED:
2372                 case TV_POLEARM:
2373                 case TV_SWORD:
2374                 {
2375                         /* Very Good */
2376                         if (power > 1)
2377                         {
2378                                 if (one_in_(40) || (power > 2)) /* power > 2 is debug only */
2379                                 {
2380                                         create_artifact(o_ptr, FALSE);
2381                                         break;
2382                                 }
2383                                 while (1)
2384                                 {
2385                                         /* Roll for an ego-item */
2386                                         o_ptr->name2 = get_random_ego(INVEN_RARM, TRUE);
2387                                         if (o_ptr->name2 == EGO_SHARPNESS && o_ptr->tval != TV_SWORD)
2388                                                 continue;
2389                                         if (o_ptr->name2 == EGO_EARTHQUAKES && o_ptr->tval != TV_HAFTED)
2390                                                 continue;
2391                                         if (o_ptr->name2 == EGO_WEIRD && o_ptr->tval != TV_SWORD)
2392                                                 continue;
2393                                         break;
2394                                 }
2395
2396                                 switch (o_ptr->name2)
2397                                 {
2398                                 case EGO_HA:
2399                                         if (one_in_(4) && (level > 40))
2400                                                 add_flag(o_ptr->art_flags, TR_BLOWS);
2401                                         break;
2402                                 case EGO_DF:
2403                                         if (one_in_(3))
2404                                                 add_flag(o_ptr->art_flags, TR_RES_POIS);
2405                                         if (one_in_(3))
2406                                                 add_flag(o_ptr->art_flags, TR_WARNING);
2407                                         break;
2408                                 case EGO_KILL_DRAGON:
2409                                         if (one_in_(3))
2410                                                 add_flag(o_ptr->art_flags, TR_RES_POIS);
2411                                         break;
2412                                 case EGO_WEST:
2413                                         if (one_in_(3))
2414                                                 add_flag(o_ptr->art_flags, TR_RES_FEAR);
2415                                         break;
2416                                 case EGO_SLAYING_WEAPON:
2417                                         if (one_in_(3)) /* double damage */
2418                                                 o_ptr->dd *= 2;
2419                                         else
2420                                         {
2421                                                 do
2422                                                 {
2423                                                         o_ptr->dd++;
2424                                                 }
2425                                                 while (one_in_(o_ptr->dd));
2426                                                 
2427                                                 do
2428                                                 {
2429                                                         o_ptr->ds++;
2430                                                 }
2431                                                 while (one_in_(o_ptr->ds));
2432                                         }
2433                                         
2434                                         if (one_in_(5))
2435                                         {
2436                                                 add_flag(o_ptr->art_flags, TR_BRAND_POIS);
2437                                         }
2438                                         if (o_ptr->tval == TV_SWORD && one_in_(3))
2439                                         {
2440                                                 add_flag(o_ptr->art_flags, TR_VORPAL);
2441                                         }
2442                                         break;
2443                                 case EGO_TRUMP:
2444                                         if (one_in_(5))
2445                                                 add_flag(o_ptr->art_flags, TR_SLAY_DEMON);
2446                                         if (one_in_(7))
2447                                                 one_ability(o_ptr);
2448                                         break;
2449                                 case EGO_PATTERN:
2450                                         if (one_in_(3))
2451                                                 add_flag(o_ptr->art_flags, TR_HOLD_LIFE);
2452                                         if (one_in_(3))
2453                                                 add_flag(o_ptr->art_flags, TR_DEX);
2454                                         if (one_in_(5))
2455                                                 add_flag(o_ptr->art_flags, TR_RES_FEAR);
2456                                         break;
2457                                 case EGO_SHARPNESS:
2458                                         o_ptr->pval = m_bonus(5, level) + 1;
2459                                         break;
2460                                 case EGO_EARTHQUAKES:
2461                                         if (one_in_(3) && (level > 60))
2462                                                 add_flag(o_ptr->art_flags, TR_BLOWS);
2463                                         else
2464                                                 o_ptr->pval = m_bonus(3, level);
2465                                         break;
2466                                 case EGO_VAMPIRIC:
2467                                         if (one_in_(5))
2468                                                 add_flag(o_ptr->art_flags, TR_SLAY_HUMAN);
2469                                         break;
2470                                 case EGO_DEMON:
2471                                         
2472                                         if(one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2473                                         one_in_(3) ? 
2474                                                 add_flag(o_ptr->art_flags, TR_DRAIN_EXP) :
2475                                                 one_in_(2) ?
2476                                                         add_flag(o_ptr->art_flags, TR_DRAIN_HP) :
2477                                                         add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2478                                                 
2479                                         
2480                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_CHAOTIC);
2481                                         if (one_in_(4)) add_flag(o_ptr->art_flags, TR_BLOWS);
2482                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2483                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_CALL_DEMON);
2484                                         break;
2485                                 }
2486
2487                                 if (!o_ptr->art_name)
2488                                 {
2489                                         /* Hack -- Super-charge the damage dice */
2490                                         while (one_in_(10L * o_ptr->dd * o_ptr->ds)) o_ptr->dd++;
2491
2492                                         /* Hack -- Lower the damage dice */
2493                                         if (o_ptr->dd > 9) o_ptr->dd = 9;
2494                                 }
2495                         }
2496
2497                         /* Very cursed */
2498                         else if (power < -1)
2499                         {
2500                                 /* Roll for ego-item */
2501                                 if (randint0(MAX_DEPTH) < level)
2502                                 {
2503                                         while(1)
2504                                         {
2505                                                 o_ptr->name2 = get_random_ego(INVEN_RARM, FALSE);
2506                                                 if (o_ptr->name2 == EGO_WEIRD && o_ptr->tval != TV_SWORD)
2507                                                 {
2508                                                         continue;
2509                                                 }
2510                                                 break;
2511                                         }
2512                                         switch (o_ptr->name2)
2513                                         {
2514                                         case EGO_MORGUL:
2515                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2516                                                 if (one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2517                                         case EGO_WEIRD:
2518                                                 if (one_in_(4)) add_flag(o_ptr->art_flags, TR_BRAND_POIS);
2519                                                 if (one_in_(4)) add_flag(o_ptr->art_flags, TR_RES_NETHER);
2520                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_NO_MAGIC);
2521                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_NO_TELE);
2522                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2523                                                 if (one_in_(6)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2524                                         }
2525                                 }
2526                         }
2527
2528                         break;
2529                 }
2530
2531
2532                 case TV_BOW:
2533                 {
2534                         /* Very good */
2535                         if (power > 1)
2536                         {
2537                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2538                                 {
2539                                         create_artifact(o_ptr, FALSE);
2540                                         break;
2541                                 }
2542                                 o_ptr->name2 = get_random_ego(INVEN_BOW, TRUE);
2543                         }
2544
2545                         break;
2546                 }
2547
2548
2549                 case TV_BOLT:
2550                 case TV_ARROW:
2551                 case TV_SHOT:
2552                 {
2553                         /* Very good */
2554                         if (power > 1)
2555                         {
2556                                 if (power > 2) /* power > 2 is debug only */
2557                                 {
2558                                         create_artifact(o_ptr, FALSE);
2559                                         break;
2560                                 }
2561
2562                                 o_ptr->name2 = get_random_ego(INVEN_AMMO, TRUE);
2563
2564                                 switch (o_ptr->name2)
2565                                 {
2566                                 case EGO_SLAYING_BOLT:
2567                                         o_ptr->dd++;
2568                                         break;
2569                                 }
2570
2571                                 /* Hack -- super-charge the damage dice */
2572                                 while (one_in_(10L * o_ptr->dd * o_ptr->ds)) o_ptr->dd++;
2573
2574                                 /* Hack -- restrict the damage dice */
2575                                 if (o_ptr->dd > 9) o_ptr->dd = 9;
2576                         }
2577
2578                         /* Very cursed */
2579                         else if (power < -1)
2580                         {
2581                                 /* Roll for ego-item */
2582                                 if (randint0(MAX_DEPTH) < level)
2583                                 {
2584                                         o_ptr->name2 = get_random_ego(INVEN_AMMO, FALSE);
2585                                 }
2586                         }
2587
2588                         break;
2589                 }
2590         }
2591 }
2592
2593
2594 static void dragon_resist(object_type * o_ptr)
2595 {
2596         do
2597         {
2598                 if (one_in_(4))
2599                         one_dragon_ele_resistance(o_ptr);
2600                 else
2601                         one_high_resistance(o_ptr);
2602         }
2603         while (one_in_(2));
2604 }
2605
2606
2607 static bool add_esp_strong(object_type *o_ptr)
2608 {
2609         bool nonliv = FALSE;
2610
2611         switch (randint1(3))
2612         {
2613         case 1: add_flag(o_ptr->art_flags, TR_ESP_EVIL); break;
2614         case 2: add_flag(o_ptr->art_flags, TR_TELEPATHY); break;
2615         case 3: add_flag(o_ptr->art_flags, TR_ESP_NONLIVING); nonliv = TRUE; break;
2616         }
2617
2618         return nonliv;
2619 }
2620
2621
2622 static void add_esp_weak(object_type *o_ptr, bool extra)
2623 {
2624         int i;
2625         u32b weak_esp_list[] = {
2626                 TR_ESP_ANIMAL,
2627                 TR_ESP_UNDEAD,
2628                 TR_ESP_DEMON,
2629                 TR_ESP_ORC,
2630                 TR_ESP_TROLL,
2631                 TR_ESP_GIANT,
2632                 TR_ESP_DRAGON,
2633                 TR_ESP_HUMAN,
2634                 TR_ESP_GOOD,
2635                 TR_ESP_UNIQUE,
2636         };
2637         const int MAX_ESP_WEAK = sizeof(weak_esp_list) / sizeof(weak_esp_list[0]);
2638         const int add_count = MIN(MAX_ESP_WEAK, (extra) ? (3 + randint1(randint1(6))) : randint1(3));
2639
2640         /* Add unduplicated weak esp flags randomly */
2641         for (i = 0; i < add_count; ++ i)
2642         {
2643                 int choice = rand_range(i, MAX_ESP_WEAK - 1);
2644
2645                 add_flag(o_ptr->art_flags, weak_esp_list[choice]);
2646                 weak_esp_list[choice] = weak_esp_list[i];
2647         }
2648 }
2649
2650
2651 /*
2652  * Apply magic to an item known to be "armor"
2653  *
2654  * Hack -- note special processing for crown/helm
2655  * Hack -- note special processing for robe of permanence
2656  */
2657 static void a_m_aux_2(object_type *o_ptr, int level, int power)
2658 {
2659         int toac1 = randint1(5) + m_bonus(5, level);
2660
2661         int toac2 = m_bonus(10, level);
2662
2663         /* Good */
2664         if (power > 0)
2665         {
2666                 /* Enchant */
2667                 o_ptr->to_a += toac1;
2668
2669                 /* Very good */
2670                 if (power > 1)
2671                 {
2672                         /* Enchant again */
2673                         o_ptr->to_a += toac2;
2674                 }
2675         }
2676
2677         /* Cursed */
2678         else if (power < 0)
2679         {
2680                 /* Penalize */
2681                 o_ptr->to_a -= toac1;
2682
2683                 /* Very cursed */
2684                 if (power < -1)
2685                 {
2686                         /* Penalize again */
2687                         o_ptr->to_a -= toac2;
2688                 }
2689
2690                 /* Cursed (if "bad") */
2691                 if (o_ptr->to_a < 0) o_ptr->curse_flags |= TRC_CURSED;
2692         }
2693
2694
2695         /* Analyze type */
2696         switch (o_ptr->tval)
2697         {
2698                 case TV_DRAG_ARMOR:
2699                 {
2700                         if (one_in_(50) || (power > 2)) /* power > 2 is debug only */
2701                                 create_artifact(o_ptr, FALSE);
2702
2703                         /* Mention the item */
2704                         if (cheat_peek) object_mention(o_ptr);
2705
2706                         break;
2707                 }
2708
2709                 case TV_HARD_ARMOR:
2710                 case TV_SOFT_ARMOR:
2711                 {
2712                         /* Very good */
2713                         if (power > 1)
2714                         {
2715                                 /* Hack -- Try for "Robes of the Magi" */
2716                                 if ((o_ptr->tval == TV_SOFT_ARMOR) &&
2717                                     (o_ptr->sval == SV_ROBE) &&
2718                                     (randint0(100) < 15))
2719                                 {
2720                                         if (one_in_(5))
2721                                         {
2722                                                 o_ptr->name2 = EGO_YOIYAMI;
2723                                                 o_ptr->k_idx = lookup_kind(TV_SOFT_ARMOR, SV_YOIYAMI_ROBE);
2724                                                 o_ptr->sval = SV_YOIYAMI_ROBE;
2725                                                 o_ptr->ac = 0;
2726                                                 o_ptr->to_a = 0;
2727                                         }
2728                                         else
2729                                         {
2730                                                 o_ptr->name2 = EGO_PERMANENCE;
2731                                         }
2732                                         break;
2733                                 }
2734
2735                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2736                                 {
2737                                         create_artifact(o_ptr, FALSE);
2738                                         break;
2739                                 }
2740
2741                                 while (1)
2742                                 {
2743                                         bool okay_flag = TRUE;
2744
2745                                         o_ptr->name2 = get_random_ego(INVEN_BODY, TRUE);
2746
2747                                         switch (o_ptr->name2)
2748                                         {
2749                                           case EGO_DWARVEN:
2750                                                 if (o_ptr->tval != TV_HARD_ARMOR)
2751                                                 {
2752                                                         okay_flag = FALSE;
2753                                                         break;
2754                                                 }
2755                                           case EGO_DRUID:
2756                                                 if (o_ptr->tval != TV_SOFT_ARMOR)
2757                                                 {
2758                                                         okay_flag = FALSE;
2759                                                         break;
2760                                                 }
2761                                           default:
2762                                                 break;
2763                                         }
2764
2765                                         if (okay_flag)
2766                                                 break;
2767                                 }
2768                                 switch (o_ptr->name2)
2769                                 {
2770                                   case EGO_RESISTANCE:
2771                                         if (one_in_(4))
2772                                                 add_flag(o_ptr->art_flags, TR_RES_POIS);
2773                                                 break;
2774                                   case EGO_DWARVEN:
2775                                         o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
2776                                         o_ptr->ac = k_info[o_ptr->k_idx].ac + 5;
2777                                         break;
2778                                         
2779                                   case EGO_A_DEMON:
2780                                         if(one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2781                                         one_in_(3) ? 
2782                                                 add_flag(o_ptr->art_flags, TR_DRAIN_EXP) :
2783                                                 one_in_(2) ?
2784                                                         add_flag(o_ptr->art_flags, TR_DRAIN_HP) :
2785                                                         add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2786                                                 
2787                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_AGGRAVATE);
2788                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_ADD_L_CURSE);
2789                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2790                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_HP);
2791                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2792                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
2793                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2794                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_CALL_DEMON);
2795                                         break;
2796                                   case EGO_A_MORGUL:
2797                                         if (one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
2798                                         if (one_in_(9)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2799                                         if (one_in_(4)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
2800                                         if (one_in_(6)) add_flag(o_ptr->art_flags, TR_AGGRAVATE);
2801                                         if (one_in_(9)) add_flag(o_ptr->art_flags, TR_NO_MAGIC);
2802                                         if (one_in_(9)) add_flag(o_ptr->art_flags, TR_NO_TELE);
2803                                         break;
2804                                   default:
2805                                         break;
2806                                 }
2807                         }
2808
2809                         break;
2810                 }
2811
2812                 case TV_SHIELD:
2813                 {
2814
2815                         if (o_ptr->sval == SV_DRAGON_SHIELD)
2816                         {
2817                                 /* Mention the item */
2818                                 if (cheat_peek) object_mention(o_ptr);
2819                                 dragon_resist(o_ptr);
2820                                 if (!one_in_(3)) break;
2821                         }
2822
2823                         /* Very good */
2824                         if (power > 1)
2825                         {
2826                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2827                                 {
2828                                         create_artifact(o_ptr, FALSE);
2829                                         break;
2830                                 }
2831                                 
2832                                 while(1)
2833                                 {
2834                                         o_ptr->name2 = get_random_ego(INVEN_LARM, TRUE);
2835                                         if (o_ptr->sval != SV_SMALL_METAL_SHIELD && o_ptr->sval != SV_LARGE_METAL_SHIELD 
2836                                                                 && o_ptr->name2 == EGO_S_DWARVEN)
2837                                         {
2838                                                 continue;
2839                                         }
2840                                         break;
2841                                 }
2842                                 
2843                                 switch (o_ptr->name2)
2844                                 {
2845                                 case EGO_ENDURANCE:
2846                                         if (!one_in_(3)) one_high_resistance(o_ptr);
2847                                         if (one_in_(4)) add_flag(o_ptr->art_flags, TR_RES_POIS);
2848                                         break;
2849                                 case EGO_REFLECTION:
2850                                         if (o_ptr->sval == SV_MIRROR_SHIELD)
2851                                                 o_ptr->name2 = 0;
2852                                         break;
2853                                         
2854                                 case EGO_S_DWARVEN:
2855                                         o_ptr->weight = (2 * k_info[o_ptr->k_idx].weight / 3);
2856                                         o_ptr->ac = k_info[o_ptr->k_idx].ac + 3;
2857                                         break;
2858                                 }
2859                         }
2860                         break;
2861                 }
2862
2863                 case TV_GLOVES:
2864                 {
2865                         if (o_ptr->sval == SV_SET_OF_DRAGON_GLOVES)
2866                         {
2867                                 /* Mention the item */
2868                                 if (cheat_peek) object_mention(o_ptr);
2869                                 dragon_resist(o_ptr);
2870                                 if (!one_in_(3)) break;
2871                         }
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                                 o_ptr->name2 = get_random_ego(INVEN_HANDS, TRUE);
2880                         }
2881                         
2882                         /* Very cursed */
2883                         else if (power < -1)
2884                         {
2885                                 o_ptr->name2 = get_random_ego(INVEN_HANDS, FALSE);
2886                         }
2887
2888                         break;
2889                 }
2890
2891                 case TV_BOOTS:
2892                 {
2893                         if (o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE)
2894                         {
2895                                 /* Mention the item */
2896                                 if (cheat_peek) object_mention(o_ptr);
2897                                 dragon_resist(o_ptr);
2898                                 if (!one_in_(3)) break;
2899                         }
2900                         /* Very good */
2901                         if (power > 1)
2902                         {
2903                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2904                                 {
2905                                         create_artifact(o_ptr, FALSE);
2906                                         break;
2907                                 }
2908                                 o_ptr->name2 = get_random_ego(INVEN_FEET, TRUE);
2909
2910                                 switch (o_ptr->name2)
2911                                 {
2912                                 case EGO_SLOW_DESCENT:
2913                                         if (one_in_(2))
2914                                         {
2915                                                 one_high_resistance(o_ptr);
2916                                         }
2917                                         break;
2918                                 }
2919                         }
2920                         /* Very cursed */
2921                         else if (power < -1)
2922                         {
2923                                 o_ptr->name2 = get_random_ego(INVEN_FEET, FALSE);
2924                         }
2925
2926                         break;
2927                 }
2928
2929                 case TV_CROWN:
2930                 {
2931                         /* Very good */
2932                         if (power > 1)
2933                         {
2934                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
2935                                 {
2936                                         create_artifact(o_ptr, FALSE);
2937                                         break;
2938                                 }
2939                                 while (1)
2940                                 {
2941                                         bool ok_flag = TRUE;
2942                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE);
2943
2944                                         switch (o_ptr->name2)
2945                                         {
2946                                         case EGO_TELEPATHY:
2947                                                 if (add_esp_strong(o_ptr)) add_esp_weak(o_ptr, TRUE);
2948                                                 else add_esp_weak(o_ptr, FALSE);
2949                                                 break;
2950                                         case EGO_MAGI:
2951                                         case EGO_MIGHT:
2952                                         case EGO_REGENERATION:
2953                                         case EGO_LORDLINESS:
2954                                         case EGO_BASILISK:
2955                                                 break;
2956                                         case EGO_SEEING:
2957                                                 if (one_in_(3))
2958                                                 {
2959                                                         if (one_in_(2)) add_esp_strong(o_ptr);
2960                                                         else add_esp_weak(o_ptr, FALSE);
2961                                                 }
2962                                                 break;
2963                                         default:/* not existing crown (wisdom,lite, etc...) */
2964                                                 ok_flag = FALSE;
2965                                         }
2966                                         if (ok_flag)
2967                                                 break; /* while (1) */
2968                                 }
2969                                 break;
2970                         }
2971
2972                         /* Very cursed */
2973                         else if (power < -1)
2974                         {       
2975                                 while (1)
2976                                 {
2977                                         bool ok_flag = TRUE;
2978                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE);
2979
2980                                         switch (o_ptr->name2)
2981                                         {
2982                                           case EGO_ANCIENT_CURSE:
2983                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_NO_MAGIC);
2984                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_NO_TELE);
2985                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
2986                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
2987                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_HP);
2988                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
2989                                                 break;
2990                                         }
2991                                         if (ok_flag)
2992                                                 break; /* while (1) */
2993                                 }
2994                         }
2995
2996                         break;
2997                 }
2998
2999                 case TV_HELM:
3000                 {
3001                         if (o_ptr->sval == SV_DRAGON_HELM)
3002                         {
3003                                 /* Mention the item */
3004                                 if (cheat_peek) object_mention(o_ptr);
3005                                 dragon_resist(o_ptr);
3006                                 if (!one_in_(3)) break;
3007                         }
3008
3009                         /* Very good */
3010                         if (power > 1)
3011                         {
3012                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
3013                                 {
3014                                         create_artifact(o_ptr, FALSE);
3015                                         break;
3016                                 }
3017                                 while (1)
3018                                 {
3019                                         bool ok_flag = TRUE;
3020                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, TRUE);
3021
3022                                         switch (o_ptr->name2)
3023                                         {
3024                                         case EGO_BRILLIANCE:
3025                                         case EGO_DARK:
3026                                         case EGO_INFRAVISION:
3027                                         case EGO_H_PROTECTION:
3028                                                 break;
3029                                         case EGO_SEEING:
3030                                                 if (one_in_(7))
3031                                                 {
3032                                                         if (one_in_(2)) add_esp_strong(o_ptr);
3033                                                         else add_esp_weak(o_ptr, FALSE);
3034                                                 }
3035                                                 break;
3036                                         case EGO_LITE:
3037                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_LITE_1);
3038                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_LITE_2);
3039                                                 break;
3040                                         case EGO_H_DEMON:
3041                                                 if(one_in_(3)) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
3042                                                 one_in_(3) ? 
3043                                                         add_flag(o_ptr->art_flags, TR_DRAIN_EXP) :
3044                                                         one_in_(2) ?
3045                                                                 add_flag(o_ptr->art_flags, TR_DRAIN_HP) :
3046                                                                 add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
3047                                                 
3048                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_AGGRAVATE);
3049                                                 if (one_in_(3)) add_flag(o_ptr->art_flags, TR_ADD_L_CURSE);
3050                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_ADD_H_CURSE);
3051                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_HP);
3052                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_MANA);
3053                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
3054                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_TY_CURSE);
3055                                                 if (one_in_(5)) add_flag(o_ptr->art_flags, TR_CALL_DEMON);
3056                                                 break;
3057                                         default:/* not existing helm (Magi, Might, etc...)*/
3058                                                 ok_flag = FALSE;
3059                                         }
3060                                         if (ok_flag)
3061                                                 break; /* while (1) */
3062                                 }
3063                                 break;
3064                         }
3065                         /* Very cursed */
3066                         else if (power < -1)
3067                         {
3068                                 while (1)
3069                                 {
3070                                         bool ok_flag = TRUE;
3071                                         o_ptr->name2 = get_random_ego(INVEN_HEAD, FALSE);
3072
3073                                         switch (o_ptr->name2)
3074                                         {
3075                                           case EGO_ANCIENT_CURSE:
3076                                                 ok_flag = FALSE;
3077                                         }
3078                                         if (ok_flag)
3079                                                 break; /* while (1) */
3080                                 }
3081                         }
3082                         break;
3083                 }
3084
3085                 case TV_CLOAK:
3086                 {
3087                         /* Very good */
3088                         if (power > 1)
3089                         {
3090                                 if (one_in_(20) || (power > 2)) /* power > 2 is debug only */
3091                                 {
3092                                         create_artifact(o_ptr, FALSE);
3093                                         break;
3094                                 }
3095                                 o_ptr->name2 = get_random_ego(INVEN_OUTER, TRUE);
3096
3097                                 switch (o_ptr->name2)
3098                                 {
3099                                 case EGO_BAT:
3100                                         o_ptr->to_d -= 6;
3101                                         o_ptr->to_h -= 6;
3102                                         break;
3103                                   case EGO_NAZGUL:
3104                                         o_ptr->to_d -= 3;
3105                                         o_ptr->to_h -= 3;
3106                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_COWARDICE);
3107                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_CALL_UNDEAD);
3108                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_SLOW_REGEN);
3109                                         if (one_in_(3)) add_flag(o_ptr->art_flags, TR_DRAIN_EXP);
3110                                         break;
3111                                 }
3112
3113                         }
3114
3115                         /* Very cursed */
3116                         else if (power < -1)
3117                         {
3118                                 o_ptr->name2 = get_random_ego(INVEN_OUTER, FALSE);
3119                         }
3120
3121                         break;
3122                 }
3123         }
3124 }
3125
3126
3127 /*
3128  * Apply magic to an item known to be a "ring" or "amulet"
3129  *
3130  * Hack -- note special "pval boost" code for ring of speed
3131  * Hack -- note that some items must be cursed (or blessed)
3132  */
3133 static void a_m_aux_3(object_type *o_ptr, int level, int power)
3134 {
3135         /* Apply magic (good or bad) according to type */
3136         switch (o_ptr->tval)
3137         {
3138                 case TV_RING:
3139                 {
3140                         /* Analyze */
3141                         switch (o_ptr->sval)
3142                         {
3143                                 case SV_RING_ATTACKS:
3144                                 {
3145                                         /* Stat bonus */
3146                                         o_ptr->pval = m_bonus(2, level);
3147                                         if (one_in_(15)) o_ptr->pval++;
3148                                         if (o_ptr->pval < 1) o_ptr->pval = 1;
3149
3150                                         /* Cursed */
3151                                         if (power < 0)
3152                                         {
3153                                                 /* Broken */
3154                                                 o_ptr->ident |= (IDENT_BROKEN);
3155
3156                                                 /* Cursed */
3157                                                 o_ptr->curse_flags |= TRC_CURSED;
3158
3159                                                 /* Reverse pval */
3160                                                 o_ptr->pval = 0 - (o_ptr->pval);
3161                                         }
3162
3163                                         break;
3164                                 }
3165
3166                                 case SV_RING_SHOTS:
3167                                 {
3168                                         break;
3169                                 }
3170
3171                                 /* Strength, Constitution, Dexterity, Intelligence */
3172                                 case SV_RING_STR:
3173                                 case SV_RING_CON:
3174                                 case SV_RING_DEX:
3175                                 {
3176                                         /* Stat bonus */
3177                                         o_ptr->pval = 1 + m_bonus(5, level);
3178
3179                                         /* Cursed */
3180                                         if (power < 0)
3181                                         {
3182                                                 /* Broken */
3183                                                 o_ptr->ident |= (IDENT_BROKEN);
3184
3185                                                 /* Cursed */
3186                                                 o_ptr->curse_flags |= TRC_CURSED;
3187
3188                                                 /* Reverse pval */
3189                                                 o_ptr->pval = 0 - (o_ptr->pval);
3190                                         }
3191
3192                                         break;
3193                                 }
3194
3195                                 /* Ring of Speed! */
3196                                 case SV_RING_SPEED:
3197                                 {
3198                                         /* Base speed (1 to 10) */
3199                                         o_ptr->pval = randint1(5) + m_bonus(5, level);
3200
3201                                         /* Super-charge the ring */
3202                                         while (randint0(100) < 50) o_ptr->pval++;
3203
3204                                         /* Cursed Ring */
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 pval */
3214                                                 o_ptr->pval = 0 - (o_ptr->pval);
3215
3216                                                 break;
3217                                         }
3218
3219                                         /* Mention the item */
3220                                         if (cheat_peek) object_mention(o_ptr);
3221
3222                                         break;
3223                                 }
3224
3225                                 case SV_RING_LORDLY:
3226                                 {
3227                                         do
3228                                         {
3229                                                 one_lordly_high_resistance(o_ptr);
3230                                         }
3231                                         while (one_in_(4));
3232
3233                                         /* Bonus to armor class */
3234                                         o_ptr->to_a = 10 + randint1(5) + m_bonus(10, level);
3235                                 }
3236                                 break;
3237
3238                                 case SV_RING_WARNING:
3239                                 {
3240                                         if (one_in_(3)) one_low_esp(o_ptr);
3241                                         break;
3242                                 }
3243
3244                                 /* Searching */
3245                                 case SV_RING_SEARCHING:
3246                                 {
3247                                         /* Bonus to searching */
3248                                         o_ptr->pval = 1 + m_bonus(5, level);
3249
3250                                         /* Cursed */
3251                                         if (power < 0)
3252                                         {
3253                                                 /* Broken */
3254                                                 o_ptr->ident |= (IDENT_BROKEN);
3255
3256                                                 /* Cursed */
3257                                                 o_ptr->curse_flags |= TRC_CURSED;
3258
3259                                                 /* Reverse pval */
3260                                                 o_ptr->pval = 0 - (o_ptr->pval);
3261                                         }
3262
3263                                         break;
3264                                 }
3265
3266                                 /* Flames, Acid, Ice */
3267                                 case SV_RING_FLAMES:
3268                                 case SV_RING_ACID:
3269                                 case SV_RING_ICE:
3270                                 case SV_RING_ELEC:
3271                                 {
3272                                         /* Bonus to armor class */
3273                                         o_ptr->to_a = 5 + randint1(5) + m_bonus(10, level);
3274                                         break;
3275                                 }
3276
3277                                 /* Weakness, Stupidity */
3278                                 case SV_RING_WEAKNESS:
3279                                 case SV_RING_STUPIDITY:
3280                                 {
3281                                         /* Broken */
3282                                         o_ptr->ident |= (IDENT_BROKEN);
3283
3284                                         /* Cursed */
3285                                         o_ptr->curse_flags |= TRC_CURSED;
3286
3287                                         /* Penalize */
3288                                         o_ptr->pval = 0 - (1 + m_bonus(5, level));
3289                                         if (power > 0) power = 0 - power;
3290
3291                                         break;
3292                                 }
3293
3294                                 /* WOE, Stupidity */
3295                                 case SV_RING_WOE:
3296                                 {
3297                                         /* Broken */
3298                                         o_ptr->ident |= (IDENT_BROKEN);
3299
3300                                         /* Cursed */
3301                                         o_ptr->curse_flags |= TRC_CURSED;
3302
3303                                         /* Penalize */
3304                                         o_ptr->to_a = 0 - (5 + m_bonus(10, level));
3305                                         o_ptr->pval = 0 - (1 + m_bonus(5, level));
3306                                         if (power > 0) power = 0 - power;
3307
3308                                         break;
3309                                 }
3310
3311                                 /* Ring of damage */
3312                                 case SV_RING_DAMAGE:
3313                                 {
3314                                         /* Bonus to damage */
3315                                         o_ptr->to_d = 1 + randint1(5) + m_bonus(16, level);
3316
3317                                         /* Cursed */
3318                                         if (power < 0)
3319                                         {
3320                                                 /* Broken */
3321                                                 o_ptr->ident |= (IDENT_BROKEN);
3322
3323                                                 /* Cursed */
3324                                                 o_ptr->curse_flags |= TRC_CURSED;
3325
3326                                                 /* Reverse bonus */
3327                                                 o_ptr->to_d = 0 - o_ptr->to_d;
3328                                         }
3329
3330                                         break;
3331                                 }
3332
3333                                 /* Ring of Accuracy */
3334                                 case SV_RING_ACCURACY:
3335                                 {
3336                                         /* Bonus to hit */
3337                                         o_ptr->to_h = 1 + randint1(5) + m_bonus(16, level);
3338
3339                                         /* Cursed */
3340                                         if (power < 0)
3341                                         {
3342                                                 /* Broken */
3343                                                 o_ptr->ident |= (IDENT_BROKEN);
3344
3345                                                 /* Cursed */
3346                                                 o_ptr->curse_flags |= TRC_CURSED;
3347
3348                                                 /* Reverse tohit */
3349                                                 o_ptr->to_h = 0 - o_ptr->to_h;
3350                                         }
3351
3352                                         break;
3353                                 }
3354
3355                                 /* Ring of Protection */
3356                                 case SV_RING_PROTECTION:
3357                                 {
3358                                         /* Bonus to armor class */
3359                                         o_ptr->to_a = 5 + randint1(8) + m_bonus(10, level);
3360
3361                                         /* Cursed */
3362                                         if (power < 0)
3363                                         {
3364                                                 /* Broken */
3365                                                 o_ptr->ident |= (IDENT_BROKEN);
3366
3367                                                 /* Cursed */
3368                                                 o_ptr->curse_flags |= TRC_CURSED;
3369
3370                                                 /* Reverse toac */
3371                                                 o_ptr->to_a = 0 - o_ptr->to_a;
3372                                         }
3373
3374                                         break;
3375                                 }
3376
3377                                 /* Ring of Slaying */
3378                                 case SV_RING_SLAYING:
3379                                 {
3380                                         /* Bonus to damage and to hit */
3381                                         o_ptr->to_d = randint1(5) + m_bonus(12, level);
3382                                         o_ptr->to_h = randint1(5) + m_bonus(12, level);
3383
3384                                         /* Cursed */
3385                                         if (power < 0)
3386                                         {
3387                                                 /* Broken */
3388                                                 o_ptr->ident |= (IDENT_BROKEN);
3389
3390                                                 /* Cursed */
3391                                                 o_ptr->curse_flags |= TRC_CURSED;
3392
3393                                                 /* Reverse bonuses */
3394                                                 o_ptr->to_h = 0 - o_ptr->to_h;
3395                                                 o_ptr->to_d = 0 - o_ptr->to_d;
3396                                         }
3397
3398                                         break;
3399                                 }
3400
3401                                 case SV_RING_MUSCLE:
3402                                 {
3403                                         o_ptr->pval = 1 + m_bonus(3, level);
3404                                         if (one_in_(4)) o_ptr->pval++;
3405
3406                                         /* Cursed */
3407                                         if (power < 0)
3408                                         {
3409                                                 /* Broken */
3410                                                 o_ptr->ident |= (IDENT_BROKEN);
3411
3412                                                 /* Cursed */
3413                                                 o_ptr->curse_flags |= TRC_CURSED;
3414
3415                                                 /* Reverse bonuses */
3416                                                 o_ptr->pval = 0 - o_ptr->pval;
3417                                         }
3418
3419                                         break;
3420                                 }
3421                                 case SV_RING_AGGRAVATION:
3422                                 {
3423                                         /* Broken */
3424                                         o_ptr->ident |= (IDENT_BROKEN);
3425
3426                                         /* Cursed */
3427                                         o_ptr->curse_flags |= TRC_CURSED;
3428
3429                                         if (power > 0) power = 0 - power;
3430                                         break;
3431                                 }
3432                         }
3433                         if ((one_in_(400) && (power > 0) && !object_is_cursed(o_ptr) && (level > 79))
3434                             || (power > 2)) /* power > 2 is debug only */
3435                         {
3436                                 o_ptr->pval = MIN(o_ptr->pval, 4);
3437                                 /* Randart amulet */
3438                                 create_artifact(o_ptr, FALSE);
3439                         }
3440                         else if ((power == 2) && one_in_(2))
3441                         {
3442                                 while(!o_ptr->name2)
3443                                 {
3444                                         int tmp = m_bonus(10, level);
3445                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3446                                         switch(randint1(28))
3447                                         {
3448                                         case 1: case 2:
3449                                                 o_ptr->name2 = EGO_RING_THROW;
3450                                                 break;
3451                                         case 3: case 4:
3452                                                 if (have_flag(k_ptr->flags, TR_REGEN)) break;
3453                                                 o_ptr->name2 = EGO_RING_REGEN;
3454                                                 break;
3455                                         case 5: case 6:
3456                                                 if (have_flag(k_ptr->flags, TR_LITE_1)) break;
3457                                                 o_ptr->name2 = EGO_RING_LITE;
3458                                                 break;
3459                                         case 7: case 8:
3460                                                 if (have_flag(k_ptr->flags, TR_TELEPORT)) break;
3461                                                 o_ptr->name2 = EGO_RING_TELEPORT;
3462                                                 break;
3463                                         case 9: case 10:
3464                                                 if (o_ptr->to_h) break;
3465                                                 o_ptr->name2 = EGO_RING_TO_H;
3466                                                 break;
3467                                         case 11: case 12:
3468                                                 if (o_ptr->to_d) break;
3469                                                 o_ptr->name2 = EGO_RING_TO_D;
3470                                                 break;
3471                                         case 13:
3472                                                 if ((o_ptr->to_h) || (o_ptr->to_d)) break;
3473                                                 o_ptr->name2 = EGO_RING_SLAY;
3474                                                 break;
3475                                         case 14:
3476                                                 if ((have_flag(k_ptr->flags, TR_STR)) || o_ptr->to_h || o_ptr->to_d) break;
3477                                                 o_ptr->name2 = EGO_RING_WIZARD;
3478                                                 break;
3479                                         case 15:
3480                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3481                                                 o_ptr->name2 = EGO_RING_HERO;
3482                                                 break;
3483                                         case 16:
3484                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3485                                                 if (tmp > 8) o_ptr->name2 = EGO_RING_MANA_BALL;
3486                                                 else if (tmp > 4) o_ptr->name2 = EGO_RING_MANA_BOLT;
3487                                                 else o_ptr->name2 = EGO_RING_MAGIC_MIS;
3488                                                 break;
3489                                         case 17:
3490                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3491                                                 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;
3492                                                 if (tmp > 7) o_ptr->name2 = EGO_RING_DRAGON_F;
3493                                                 else if (tmp > 3) o_ptr->name2 = EGO_RING_FIRE_BALL;
3494                                                 else o_ptr->name2 = EGO_RING_FIRE_BOLT;
3495                                                 break;
3496                                         case 18:
3497                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3498                                                 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;
3499                                                 if (tmp > 7) o_ptr->name2 = EGO_RING_DRAGON_C;
3500                                                 else if (tmp > 3) o_ptr->name2 = EGO_RING_COLD_BALL;
3501                                                 else o_ptr->name2 = EGO_RING_COLD_BOLT;
3502                                                 break;
3503                                         case 19:
3504                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3505                                                 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;
3506                                                 if (tmp > 4) o_ptr->name2 = EGO_RING_ELEC_BALL;
3507                                                 else o_ptr->name2 = EGO_RING_ELEC_BOLT;
3508                                                 break;
3509                                         case 20:
3510                                                 if (have_flag(k_ptr->flags, TR_ACTIVATE)) break;
3511                                                 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;
3512                                                 if (tmp > 4) o_ptr->name2 = EGO_RING_ACID_BALL;
3513                                                 else o_ptr->name2 = EGO_RING_ACID_BOLT;
3514                                                 break;
3515                                         case 21: case 22: case 23: case 24: case 25: case 26:
3516                                                 switch (o_ptr->sval)
3517                                                 {
3518                                                 case SV_RING_SPEED:
3519                                                         if (!one_in_(3)) break;
3520                                                         o_ptr->name2 = EGO_RING_D_SPEED;
3521                                                         break;
3522                                                 case SV_RING_DAMAGE:
3523                                                 case SV_RING_ACCURACY:
3524                                                 case SV_RING_SLAYING:
3525                                                         if (one_in_(2)) break;
3526                                                         if (one_in_(2)) o_ptr->name2 = EGO_RING_HERO;
3527                                                         else
3528                                                         {
3529                                                                 o_ptr->name2 = EGO_RING_BERSERKER;
3530                                                                 o_ptr->to_h -= 2+randint1(4);
3531                                                                 o_ptr->to_d += 2+randint1(4);
3532                                                         }
3533                                                         break;
3534                                                 case SV_RING_PROTECTION:
3535                                                         o_ptr->name2 = EGO_RING_SUPER_AC;
3536                                                         o_ptr->to_a += 7 + m_bonus(5, level);
3537                                                         break;
3538                                                 case SV_RING_RES_FEAR:
3539                                                         o_ptr->name2 = EGO_RING_HERO;
3540                                                         break;
3541                                                 case SV_RING_SHOTS:
3542                                                         if (one_in_(2)) break;
3543                                                         o_ptr->name2 = EGO_RING_HUNTER;
3544                                                         break;
3545                                                 case SV_RING_SEARCHING:
3546                                                         o_ptr->name2 = EGO_RING_STEALTH;
3547                                                         break;
3548                                                 case SV_RING_TELEPORTATION:
3549                                                         o_ptr->name2 = EGO_RING_TELE_AWAY;
3550                                                         break;
3551                                                 case SV_RING_RES_BLINDNESS:
3552                                                         if (one_in_(2))
3553                                                                 o_ptr->name2 = EGO_RING_RES_LITE;
3554                                                         else
3555                                                                 o_ptr->name2 = EGO_RING_RES_DARK;
3556                                                         break;
3557                                                 case SV_RING_LORDLY:
3558                                                         if (!one_in_(20)) break;
3559                                                         one_lordly_high_resistance(o_ptr);
3560                                                         one_lordly_high_resistance(o_ptr);
3561                                                         o_ptr->name2 = EGO_RING_TRUE;
3562                                                         break;
3563                                                 case SV_RING_SUSTAIN:
3564                                                         if (!one_in_(4)) break;
3565                                                         o_ptr->name2 = EGO_RING_RES_TIME;
3566                                                         break;
3567                                                 case SV_RING_FLAMES:
3568                                                         if (!one_in_(2)) break;
3569                                                         o_ptr->name2 = EGO_RING_DRAGON_F;
3570                                                         break;
3571                                                 case SV_RING_ICE:
3572                                                         if (!one_in_(2)) break;
3573                                                         o_ptr->name2 = EGO_RING_DRAGON_C;
3574                                                         break;
3575                                                 case SV_RING_WARNING:
3576                                                         if (!one_in_(2)) break;
3577                                                         o_ptr->name2 = EGO_RING_M_DETECT;
3578                                                         break;
3579                                                 default:
3580                                                         break;
3581                                                 }
3582                                                 break;
3583                                         }
3584                                 }
3585                                 /* Uncurse it */
3586                                 o_ptr->curse_flags = 0L;
3587                         }
3588                         else if ((power == -2) && one_in_(2))
3589                         {
3590                                 if (o_ptr->to_h > 0) o_ptr->to_h = 0-o_ptr->to_h;
3591                                 if (o_ptr->to_d > 0) o_ptr->to_d = 0-o_ptr->to_d;
3592                                 if (o_ptr->to_a > 0) o_ptr->to_a = 0-o_ptr->to_a;
3593                                 if (o_ptr->pval > 0) o_ptr->pval = 0-o_ptr->pval;
3594                                 o_ptr->art_flags[0] = 0;
3595                                 o_ptr->art_flags[1] = 0;
3596                                 while(!o_ptr->name2)
3597                                 {
3598                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3599                                         switch(randint1(5))
3600                                         {
3601                                         case 1:
3602                                                 if (have_flag(k_ptr->flags, TR_DRAIN_EXP)) break;
3603                                                 o_ptr->name2 = EGO_RING_DRAIN_EXP;
3604                                                 break;
3605                                         case 2:
3606                                                 o_ptr->name2 = EGO_RING_NO_MELEE;
3607                                                 break;
3608                                         case 3:
3609                                                 if (have_flag(k_ptr->flags, TR_AGGRAVATE)) break;
3610                                                 o_ptr->name2 = EGO_RING_AGGRAVATE;
3611                                                 break;
3612                                         case 4:
3613                                                 if (have_flag(k_ptr->flags, TR_TY_CURSE)) break;
3614                                                 o_ptr->name2 = EGO_RING_TY_CURSE;
3615                                                 break;
3616                                         case 5:
3617                                                 o_ptr->name2 = EGO_RING_ALBINO;
3618                                                 break;
3619                                         }
3620                                 }
3621                                 /* Broken */
3622                                 o_ptr->ident |= (IDENT_BROKEN);
3623
3624                                 /* Cursed */
3625                                 o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
3626                         }
3627                         break;
3628                 }
3629
3630                 case TV_AMULET:
3631                 {
3632                         /* Analyze */
3633                         switch (o_ptr->sval)
3634                         {
3635                                 /* Amulet of wisdom/charisma */
3636                                 case SV_AMULET_INTELLIGENCE:
3637                                 case SV_AMULET_WISDOM:
3638                                 case SV_AMULET_CHARISMA:
3639                                 {
3640                                         o_ptr->pval = 1 + m_bonus(5, level);
3641
3642                                         /* Cursed */
3643                                         if (power < 0)
3644                                         {
3645                                                 /* Broken */
3646                                                 o_ptr->ident |= (IDENT_BROKEN);
3647
3648                                                 /* Cursed */
3649                                                 o_ptr->curse_flags |= (TRC_CURSED);
3650
3651                                                 /* Reverse bonuses */
3652                                                 o_ptr->pval = 0 - o_ptr->pval;
3653                                         }
3654
3655                                         break;
3656                                 }
3657
3658                                 /* Amulet of brilliance */
3659                                 case SV_AMULET_BRILLIANCE:
3660                                 {
3661                                         o_ptr->pval = 1 + m_bonus(3, level);
3662                                         if (one_in_(4)) o_ptr->pval++;
3663
3664                                         /* Cursed */
3665                                         if (power < 0)
3666                                         {
3667                                                 /* Broken */
3668                                                 o_ptr->ident |= (IDENT_BROKEN);
3669
3670                                                 /* Cursed */
3671                                                 o_ptr->curse_flags |= (TRC_CURSED);
3672
3673                                                 /* Reverse bonuses */
3674                                                 o_ptr->pval = 0 - o_ptr->pval;
3675                                         }
3676
3677                                         break;
3678                                 }
3679
3680                                 case SV_AMULET_NO_MAGIC: case SV_AMULET_NO_TELE:
3681                                 {
3682                                         if (power < 0)
3683                                         {
3684                                                 o_ptr->curse_flags |= (TRC_CURSED);
3685                                         }
3686                                         break;
3687                                 }
3688
3689                                 case SV_AMULET_RESISTANCE:
3690                                 {
3691                                         if (one_in_(5)) one_high_resistance(o_ptr);
3692                                         if (one_in_(5)) add_flag(o_ptr->art_flags, TR_RES_POIS);
3693                                 }
3694                                 break;
3695
3696                                 /* Amulet of searching */
3697                                 case SV_AMULET_SEARCHING:
3698                                 {
3699                                         o_ptr->pval = randint1(2) + m_bonus(4, level);
3700
3701                                         /* Cursed */
3702                                         if (power < 0)
3703                                         {
3704                                                 /* Broken */
3705                                                 o_ptr->ident |= (IDENT_BROKEN);
3706
3707                                                 /* Cursed */
3708                                                 o_ptr->curse_flags |= (TRC_CURSED);
3709
3710                                                 /* Reverse bonuses */
3711                                                 o_ptr->pval = 0 - (o_ptr->pval);
3712                                         }
3713
3714                                         break;
3715                                 }
3716
3717                                 /* Amulet of the Magi -- never cursed */
3718                                 case SV_AMULET_THE_MAGI:
3719                                 {
3720                                         o_ptr->pval = randint1(5) + m_bonus(5, level);
3721                                         o_ptr->to_a = randint1(5) + m_bonus(5, level);
3722
3723                                         /* gain one low ESP */
3724                                         add_esp_weak(o_ptr, FALSE);
3725
3726                                         /* Mention the item */
3727                                         if (cheat_peek) object_mention(o_ptr);
3728
3729                                         break;
3730                                 }
3731
3732                                 /* Amulet of Doom -- always cursed */
3733                                 case SV_AMULET_DOOM:
3734                                 {
3735                                         /* Broken */
3736                                         o_ptr->ident |= (IDENT_BROKEN);
3737
3738                                         /* Cursed */
3739                                         o_ptr->curse_flags |= (TRC_CURSED);
3740
3741                                         /* Penalize */
3742                                         o_ptr->pval = 0 - (randint1(5) + m_bonus(5, level));
3743                                         o_ptr->to_a = 0 - (randint1(5) + m_bonus(5, level));
3744                                         if (power > 0) power = 0 - power;
3745
3746                                         break;
3747                                 }
3748
3749                                 case SV_AMULET_MAGIC_MASTERY:
3750                                 {
3751                                         o_ptr->pval = 1 + m_bonus(4, level);
3752
3753                                         /* Cursed */
3754                                         if (power < 0)
3755                                         {
3756                                                 /* Broken */
3757                                                 o_ptr->ident |= (IDENT_BROKEN);
3758
3759                                                 /* Cursed */
3760                                                 o_ptr->curse_flags |= (TRC_CURSED);
3761
3762                                                 /* Reverse bonuses */
3763                                                 o_ptr->pval = 0 - o_ptr->pval;
3764                                         }
3765
3766                                         break;
3767                                 }
3768                         }
3769                         if ((one_in_(150) && (power > 0) && !object_is_cursed(o_ptr) && (level > 79))
3770                             || (power > 2)) /* power > 2 is debug only */
3771                         {
3772                                 o_ptr->pval = MIN(o_ptr->pval, 4);
3773                                 /* Randart amulet */
3774                                 create_artifact(o_ptr, FALSE);
3775                         }
3776                         else if ((power == 2) && one_in_(2))
3777                         {
3778                                 while(!o_ptr->name2)
3779                                 {
3780                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3781                                         switch(randint1(21))
3782                                         {
3783                                         case 1: case 2:
3784                                                 if (have_flag(k_ptr->flags, TR_SLOW_DIGEST)) break;
3785                                                 o_ptr->name2 = EGO_AMU_SLOW_D;
3786                                                 break;
3787                                         case 3: case 4:
3788                                                 if (o_ptr->pval) break;
3789                                                 o_ptr->name2 = EGO_AMU_INFRA;
3790                                                 break;
3791                                         case 5: case 6:
3792                                                 if (have_flag(k_ptr->flags, TR_SEE_INVIS)) break;
3793                                                 o_ptr->name2 = EGO_AMU_SEE_INVIS;
3794                                                 break;
3795                                         case 7: case 8:
3796                                                 if (have_flag(k_ptr->flags, TR_HOLD_LIFE)) break;
3797                                                 o_ptr->name2 = EGO_AMU_HOLD_LIFE;
3798                                                 break;
3799                                         case 9:
3800                                                 if (have_flag(k_ptr->flags, TR_LEVITATION)) break;
3801                                                 o_ptr->name2 = EGO_AMU_LEVITATION;
3802                                                 break;
3803                                         case 10: case 11: case 21:
3804                                                 o_ptr->name2 = EGO_AMU_AC;
3805                                                 break;
3806                                         case 12:
3807                                                 if (have_flag(k_ptr->flags, TR_RES_FIRE)) break;
3808                                                 if (m_bonus(10, level) > 8)
3809                                                         o_ptr->name2 = EGO_AMU_RES_FIRE_;
3810                                                 else
3811                                                         o_ptr->name2 = EGO_AMU_RES_FIRE;
3812                                                 break;
3813                                         case 13:
3814                                                 if (have_flag(k_ptr->flags, TR_RES_COLD)) break;
3815                                                 if (m_bonus(10, level) > 8)
3816                                                         o_ptr->name2 = EGO_AMU_RES_COLD_;
3817                                                 else
3818                                                         o_ptr->name2 = EGO_AMU_RES_COLD;
3819                                                 break;
3820                                         case 14:
3821                                                 if (have_flag(k_ptr->flags, TR_RES_ELEC)) break;
3822                                                 if (m_bonus(10, level) > 8)
3823                                                         o_ptr->name2 = EGO_AMU_RES_ELEC_;
3824                                                 else
3825                                                         o_ptr->name2 = EGO_AMU_RES_ELEC;
3826                                                 break;
3827                                         case 15:
3828                                                 if (have_flag(k_ptr->flags, TR_RES_ACID)) break;
3829                                                 if (m_bonus(10, level) > 8)
3830                                                         o_ptr->name2 = EGO_AMU_RES_ACID_;
3831                                                 else
3832                                                         o_ptr->name2 = EGO_AMU_RES_ACID;
3833                                                 break;
3834                                         case 16: case 17: case 18: case 19: case 20:
3835                                                 switch (o_ptr->sval)
3836                                                 {
3837                                                 case SV_AMULET_TELEPORT:
3838                                                         if (m_bonus(10, level) > 9) o_ptr->name2 = EGO_AMU_D_DOOR;
3839                                                         else if (one_in_(2)) o_ptr->name2 = EGO_AMU_JUMP;
3840                                                         else o_ptr->name2 = EGO_AMU_TELEPORT;
3841                                                         break;
3842                                                 case SV_AMULET_RESIST_ACID:
3843                                                         if ((m_bonus(10, level) > 6) && one_in_(2)) o_ptr->name2 = EGO_AMU_RES_ACID_;
3844                                                         break;
3845                                                 case SV_AMULET_SEARCHING:
3846                                                         o_ptr->name2 = EGO_AMU_STEALTH;
3847                                                         break;
3848                                                 case SV_AMULET_BRILLIANCE:
3849                                                         if (!one_in_(3)) break;
3850                                                         o_ptr->name2 = EGO_AMU_IDENT;
3851                                                         break;
3852                                                 case SV_AMULET_CHARISMA:
3853                                                         if (!one_in_(3)) break;
3854                                                         o_ptr->name2 = EGO_AMU_CHARM;
3855                                                         break;
3856                                                 case SV_AMULET_THE_MAGI:
3857                                                         if (one_in_(2)) break;
3858                                                         o_ptr->name2 = EGO_AMU_GREAT;
3859                                                         break;
3860                                                 case SV_AMULET_RESISTANCE:
3861                                                         if (!one_in_(5)) break;
3862                                                         o_ptr->name2 = EGO_AMU_DEFENDER;
3863                                                         break;
3864                                                 case SV_AMULET_TELEPATHY:
3865                                                         if (!one_in_(3)) break;
3866                                                         o_ptr->name2 = EGO_AMU_DETECTION;
3867                                                         break;
3868                                                 }
3869                                         }
3870                                 }
3871                                 /* Uncurse it */
3872                                 o_ptr->curse_flags = 0L;
3873                         }
3874                         else if ((power == -2) && one_in_(2))
3875                         {
3876                                 if (o_ptr->to_h > 0) o_ptr->to_h = 0-o_ptr->to_h;
3877                                 if (o_ptr->to_d > 0) o_ptr->to_d = 0-o_ptr->to_d;
3878                                 if (o_ptr->to_a > 0) o_ptr->to_a = 0-o_ptr->to_a;
3879                                 if (o_ptr->pval > 0) o_ptr->pval = 0-o_ptr->pval;
3880                                 o_ptr->art_flags[0] = 0;
3881                                 o_ptr->art_flags[1] = 0;
3882                                 while(!o_ptr->name2)
3883                                 {
3884                                         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3885                                         switch(randint1(5))
3886                                         {
3887                                         case 1:
3888                                                 if (have_flag(k_ptr->flags, TR_DRAIN_EXP)) break;
3889                                                 o_ptr->name2 = EGO_AMU_DRAIN_EXP;
3890                                                 break;
3891                                         case 2:
3892                                                 o_ptr->name2 = EGO_AMU_FOOL;
3893                                                 break;
3894                                         case 3:
3895                                                 if (have_flag(k_ptr->flags, TR_AGGRAVATE)) break;
3896                                                 o_ptr->name2 = EGO_AMU_AGGRAVATE;
3897                                                 break;
3898                                         case 4:
3899                                                 if (have_flag(k_ptr->flags, TR_TY_CURSE)) break;
3900                                                 o_ptr->name2 = EGO_AMU_TY_CURSE;
3901                                                 break;
3902                                         case 5:
3903                                                 o_ptr->name2 = EGO_AMU_NAIVETY;
3904                                                 break;
3905                                         }
3906                                 }
3907                                 /* Broken */
3908                                 o_ptr->ident |= (IDENT_BROKEN);
3909
3910                                 /* Cursed */
3911                                 o_ptr->curse_flags |= (TRC_CURSED | TRC_HEAVY_CURSE);
3912                         }
3913                         break;
3914                 }
3915         }
3916 }
3917
3918
3919 /*
3920  * Hack -- help pick an item type
3921  */
3922 static bool item_monster_okay(int r_idx)
3923 {
3924         monster_race *r_ptr = &r_info[r_idx];
3925
3926         /* No uniques */
3927         if (r_ptr->flags1 & RF1_UNIQUE) return (FALSE);
3928         if (r_ptr->flags7 & RF7_KAGE) return (FALSE);
3929         if (r_ptr->flagsr & RFR_RES_ALL) return (FALSE);
3930         if (r_ptr->flags7 & RF7_NAZGUL) return (FALSE);
3931         if (r_ptr->flags1 & RF1_FORCE_DEPTH) return (FALSE);
3932         if (r_ptr->flags7 & RF7_UNIQUE2) return (FALSE);
3933
3934         /* Okay */
3935         return (TRUE);
3936 }
3937
3938
3939 /*
3940  * Apply magic to an item known to be "boring"
3941  *
3942  * Hack -- note the special code for various items
3943  */
3944 static void a_m_aux_4(object_type *o_ptr, int level, int power)
3945 {
3946         object_kind *k_ptr = &k_info[o_ptr->k_idx];
3947
3948         /* Unused */
3949         (void)level;
3950
3951         /* Apply magic (good or bad) according to type */
3952         switch (o_ptr->tval)
3953         {
3954                 case TV_WHISTLE:
3955                 {
3956 #if 0
3957                         /* Cursed */
3958                         if (power < 0)
3959                         {
3960                                 /* Broken */
3961                                 o_ptr->ident |= (IDENT_BROKEN);
3962
3963                                 /* Cursed */
3964                                 o_ptr->curse_flags |= (TRC_CURSED);
3965                         }
3966 #endif
3967                         break;
3968                 }
3969                 case TV_FLASK:
3970                 {
3971                         o_ptr->xtra4 = o_ptr->pval;
3972                         o_ptr->pval = 0;
3973                         break;
3974                 }
3975                 case TV_LITE:
3976                 {
3977                         /* Hack -- Torches -- random fuel */
3978                         if (o_ptr->sval == SV_LITE_TORCH)
3979                         {
3980                                 if (o_ptr->pval > 0) o_ptr->xtra4 = randint1(o_ptr->pval);
3981                                 o_ptr->pval = 0;
3982                         }
3983
3984                         /* Hack -- Lanterns -- random fuel */
3985                         if (o_ptr->sval == SV_LITE_LANTERN)
3986                         {
3987                                 if (o_ptr->pval > 0) o_ptr->xtra4 = randint1(o_ptr->pval);
3988                                 o_ptr->pval = 0;
3989                         }
3990
3991                         if (power > 2) /* power > 2 is debug only */
3992                         {
3993                                 create_artifact(o_ptr, FALSE);
3994                         }
3995                         else if ((power == 2) || ((power == 1) && one_in_(3)))
3996                         {
3997                                 while (!o_ptr->name2)
3998                                 {
3999                                         while (1)
4000                                         {
4001                                                 bool okay_flag = TRUE;
4002
4003                                                 o_ptr->name2 = get_random_ego(INVEN_LITE, TRUE);
4004
4005                                                 switch (o_ptr->name2)
4006                                                 {
4007                                                 case EGO_LITE_LONG:
4008                                                         if (o_ptr->sval == SV_LITE_FEANOR)
4009                                                                 okay_flag = FALSE;
4010                                                 }
4011                                                 if (okay_flag)
4012                                                         break;
4013                                         }
4014                                 }
4015                         }
4016                         else if (power == -2)
4017                         {
4018                                 o_ptr->name2 = get_random_ego(INVEN_LITE, FALSE);
4019
4020                                 switch (o_ptr->name2)
4021                                 {
4022                                 case EGO_LITE_DARKNESS:
4023                                         o_ptr->xtra4 = 0;
4024                                         
4025                                         if (o_ptr->sval == SV_LITE_TORCH)
4026                                         {
4027                                                 add_flag(o_ptr->art_flags, TR_LITE_M1);
4028                                         }
4029                                         else if (o_ptr->sval == SV_LITE_LANTERN)
4030                                         {
4031                                                 add_flag(o_ptr->art_flags, TR_LITE_M2);
4032                                         }
4033                                         else if (o_ptr->sval == SV_LITE_FEANOR)
4034                                         {
4035                                                 add_flag(o_ptr->art_flags, TR_LITE_M3);
4036                                         }
4037                                         break;
4038                                 }
4039                         }
4040
4041                         break;
4042                 }
4043
4044                 case TV_WAND:
4045                 case TV_STAFF:
4046                 {
4047                         /* The wand or staff gets a number of initial charges equal
4048                          * to between 1/2 (+1) and the full object kind's pval. -LM-
4049                          */
4050                         o_ptr->pval = k_ptr->pval / 2 + randint1((k_ptr->pval + 1) / 2);
4051                         break;
4052                 }
4053
4054                 case TV_ROD:
4055                 {
4056                         /* Transfer the pval. -LM- */
4057                         o_ptr->pval = k_ptr->pval;
4058                         break;
4059                 }
4060
4061                 case TV_CAPTURE:
4062                 {
4063                         o_ptr->pval = 0;
4064                         object_aware(o_ptr);
4065                         object_known(o_ptr);
4066                         break;
4067                 }
4068
4069                 case TV_FIGURINE:
4070                 {
4071                         int i = 1;
4072                         int check;
4073
4074                         monster_race *r_ptr;
4075
4076                         /* Pick a random non-unique monster race */
4077                         while (1)
4078                         {
4079                                 i = randint1(max_r_idx - 1);
4080
4081                                 if (!item_monster_okay(i)) continue;
4082                                 if (i == MON_TSUCHINOKO) continue;
4083
4084                                 r_ptr = &r_info[i];
4085
4086                                 check = (dun_level < r_ptr->level) ? (r_ptr->level - dun_level) : 0;
4087
4088                                 /* Ignore dead monsters */
4089                                 if (!r_ptr->rarity) continue;
4090
4091                                 /* Ignore uncommon monsters */
4092                                 if (r_ptr->rarity > 100) continue;
4093
4094                                 /* Prefer less out-of-depth monsters */
4095                                 if (randint0(check)) continue;
4096
4097                                 break;
4098                         }
4099
4100                         o_ptr->pval = i;
4101
4102                         /* Some figurines are cursed */
4103                         if (one_in_(6)) o_ptr->curse_flags |= TRC_CURSED;
4104
4105                         if (cheat_peek)
4106                         {
4107 #ifdef JP
4108                                 msg_format("%s¤Î¿Í·Á, ¿¼¤µ +%d%s",
4109 #else
4110                                 msg_format("Figurine of %s, depth +%d%s",
4111 #endif
4112
4113                                                           r_name + r_ptr->name, check - 1,
4114                                                           !object_is_cursed(o_ptr) ? "" : " {cursed}");
4115                         }
4116
4117                         break;
4118                 }
4119
4120                 case TV_CORPSE:
4121                 {
4122                         int i = 1;
4123                         int check;
4124
4125                         u32b match = 0;
4126
4127                         monster_race *r_ptr;
4128
4129                         if (o_ptr->sval == SV_SKELETON)
4130                         {
4131                                 match = RF9_DROP_SKELETON;
4132                         }
4133                         else if (o_ptr->sval == SV_CORPSE)
4134                         {
4135                                 match = RF9_DROP_CORPSE;
4136                         }
4137
4138                         /* Hack -- Remove the monster restriction */
4139                         get_mon_num_prep(item_monster_okay, NULL);
4140
4141                         /* Pick a random non-unique monster race */
4142                         while (1)
4143                         {
4144                                 i = get_mon_num(dun_level);
4145
4146                                 r_ptr = &r_info[i];
4147
4148                                 check = (dun_level < r_ptr->level) ? (r_ptr->level - dun_level) : 0;
4149
4150                                 /* Ignore dead monsters */
4151                                 if (!r_ptr->rarity) continue;
4152
4153                                 /* Ignore corpseless monsters */
4154                                 if (!(r_ptr->flags9 & match)) continue;
4155
4156                                 /* Prefer less out-of-depth monsters */
4157                                 if (randint0(check)) continue;
4158
4159                                 break;
4160                         }
4161
4162                         o_ptr->pval = i;
4163
4164                         if (cheat_peek)
4165                         {
4166 #ifdef JP
4167                                 msg_format("%s¤Î»àÂÎ, ¿¼¤µ +%d",
4168 #else
4169                                 msg_format("Corpse of %s, depth +%d",
4170 #endif
4171
4172                                                           r_name + r_ptr->name, check - 1);
4173                         }
4174
4175                         object_aware(o_ptr);
4176                         object_known(o_ptr);
4177                         break;
4178                 }
4179
4180                 case TV_STATUE:
4181                 {
4182                         int i = 1;
4183
4184                         monster_race *r_ptr;
4185
4186                         /* Pick a random monster race */
4187                         while (1)
4188                         {
4189                                 i = randint1(max_r_idx - 1);
4190
4191                                 r_ptr = &r_info[i];
4192
4193                                 /* Ignore dead monsters */
4194                                 if (!r_ptr->rarity) continue;
4195
4196                                 break;
4197                         }
4198
4199                         o_ptr->pval = i;
4200
4201                         if (cheat_peek)
4202                         {
4203 #ifdef JP
4204                                 msg_format("%s¤ÎÁü", r_name + r_ptr->name);
4205 #else
4206                                 msg_format("Statue of %s", r_name + r_ptr->name);
4207 #endif
4208
4209                         }
4210                         object_aware(o_ptr);
4211                         object_known(o_ptr);
4212
4213                         break;
4214                 }
4215
4216                 case TV_CHEST:
4217                 {
4218                         byte obj_level = k_info[o_ptr->k_idx].level;
4219
4220                         /* Hack -- skip ruined chests */
4221                         if (obj_level <= 0) break;
4222
4223                         /* Hack -- pick a "difficulty" */
4224                         o_ptr->pval = randint1(obj_level);
4225                         if (o_ptr->sval == SV_CHEST_KANDUME) o_ptr->pval = 6;
4226
4227                         o_ptr->xtra3 = dun_level + 5;
4228
4229                         /* Never exceed "difficulty" of 55 to 59 */
4230                         if (o_ptr->pval > 55) o_ptr->pval = 55 + (byte)randint0(5);
4231
4232                         break;
4233                 }
4234         }
4235 }
4236
4237
4238 /*
4239  * Complete the "creation" of an object by applying "magic" to the item
4240  *
4241  * This includes not only rolling for random bonuses, but also putting the
4242  * finishing touches on ego-items and artifacts, giving charges to wands and
4243  * staffs, giving fuel to lites, and placing traps on chests.
4244  *
4245  * In particular, note that "Instant Artifacts", if "created" by an external
4246  * routine, must pass through this function to complete the actual creation.
4247  *
4248  * The base "chance" of the item being "good" increases with the "level"
4249  * parameter, which is usually derived from the dungeon level, being equal
4250  * to the level plus 10, up to a maximum of 75.  If "good" is true, then
4251  * the object is guaranteed to be "good".  If an object is "good", then
4252  * the chance that the object will be "great" (ego-item or artifact), also
4253  * increases with the "level", being equal to half the level, plus 5, up to
4254  * a maximum of 20.  If "great" is true, then the object is guaranteed to be
4255  * "great".  At dungeon level 65 and below, 15/100 objects are "great".
4256  *
4257  * If the object is not "good", there is a chance it will be "cursed", and
4258  * if it is "cursed", there is a chance it will be "broken".  These chances
4259  * are related to the "good" / "great" chances above.
4260  *
4261  * Otherwise "normal" rings and amulets will be "good" half the time and
4262  * "cursed" half the time, unless the ring/amulet is always good or cursed.
4263  *
4264  * If "okay" is true, and the object is going to be "great", then there is
4265  * a chance that an artifact will be created.  This is true even if both the
4266  * "good" and "great" arguments are false.  As a total hack, if "great" is
4267  * true, then the item gets 3 extra "attempts" to become an artifact.
4268  */
4269 void apply_magic(object_type *o_ptr, int lev, u32b mode)
4270 {
4271         int i, rolls, f1, f2, power;
4272
4273         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN) lev += randint0(p_ptr->lev/2+10);
4274
4275         /* Maximum "level" for various things */
4276         if (lev > MAX_DEPTH - 1) lev = MAX_DEPTH - 1;
4277
4278         /* Base chance of being "good" */
4279         f1 = lev + 10;
4280
4281         /* Maximal chance of being "good" */
4282         if (f1 > d_info[dungeon_type].obj_good) f1 = d_info[dungeon_type].obj_good;
4283
4284         /* Base chance of being "great" */
4285         f2 = f1 * 2 / 3;
4286
4287         /* Maximal chance of being "great" */
4288         if ((p_ptr->pseikaku != SEIKAKU_MUNCHKIN) && (f2 > d_info[dungeon_type].obj_great))
4289                 f2 = d_info[dungeon_type].obj_great;
4290
4291         if (p_ptr->muta3 & MUT3_GOOD_LUCK)
4292         {
4293                 f1 += 5;
4294                 f2 += 2;
4295         }
4296         else if(p_ptr->muta3 & MUT3_BAD_LUCK)
4297         {
4298                 f1 -= 5;
4299                 f2 -= 2;
4300         }
4301
4302         /* Assume normal */
4303         power = 0;
4304
4305         /* Roll for "good" */
4306         if ((mode & AM_GOOD) || magik(f1))
4307         {
4308                 /* Assume "good" */
4309                 power = 1;
4310
4311                 /* Roll for "great" */
4312                 if ((mode & AM_GREAT) || magik(f2))
4313                 {
4314                         power = 2;
4315
4316                         /* Roll for "special" */
4317                         if (mode & AM_SPECIAL) power = 3;
4318                 }
4319         }
4320
4321         /* Roll for "cursed" */
4322         else if (magik(f1))
4323         {
4324                 /* Assume "cursed" */
4325                 power = -1;
4326
4327                 /* Roll for "broken" */
4328                 if (magik(f2)) power = -2;
4329         }
4330
4331         /* Apply curse */
4332         if (mode & AM_CURSED)
4333         {
4334                 /* Assume 'cursed' */
4335                 if (power > 0)
4336                 {
4337                         power = 0 - power;
4338                 }
4339                 /* Everything else gets more badly cursed */
4340                 else
4341                 {
4342                         power--;
4343                 }
4344         }
4345
4346         /* Assume no rolls */
4347         rolls = 0;
4348
4349         /* Get one roll if excellent */
4350         if (power >= 2) rolls = 1;
4351
4352         /* Hack -- Get four rolls if forced great or special */
4353         if (mode & (AM_GREAT | AM_SPECIAL)) rolls = 4;
4354
4355         /* Hack -- Get no rolls if not allowed */
4356         if ((mode & AM_NO_FIXED_ART) || o_ptr->name1) rolls = 0;
4357
4358         /* Roll for artifacts if allowed */
4359         for (i = 0; i < rolls; i++)
4360         {
4361                 /* Roll for an artifact */
4362                 if (make_artifact(o_ptr)) break;
4363                 if ((p_ptr->muta3 & MUT3_GOOD_LUCK) && one_in_(77))
4364                 {
4365                         if (make_artifact(o_ptr)) break;
4366                 }
4367         }
4368
4369
4370         /* Hack -- analyze artifacts */
4371         if (object_is_fixed_artifact(o_ptr))
4372         {
4373                 artifact_type *a_ptr = &a_info[o_ptr->name1];
4374
4375                 /* Hack -- Mark the artifact as "created" */
4376                 a_ptr->cur_num = 1;
4377
4378                 /* Hack -- Memorize location of artifact in saved floors */
4379                 if (character_dungeon)
4380                         a_ptr->floor_id = p_ptr->floor_id;
4381
4382                 /* Extract the other fields */
4383                 o_ptr->pval = a_ptr->pval;
4384                 o_ptr->ac = a_ptr->ac;
4385                 o_ptr->dd = a_ptr->dd;
4386                 o_ptr->ds = a_ptr->ds;
4387                 o_ptr->to_a = a_ptr->to_a;
4388                 o_ptr->to_h = a_ptr->to_h;
4389                 o_ptr->to_d = a_ptr->to_d;
4390                 o_ptr->weight = a_ptr->weight;
4391                 o_ptr->xtra2 = a_ptr->act_idx;
4392
4393                 if (o_ptr->name1 == ART_MILIM)
4394                 {
4395                     if(p_ptr->pseikaku == SEIKAKU_SEXY)
4396                     {
4397                         o_ptr->pval = 3;
4398                     }
4399                 }
4400
4401                 /* Hack -- extract the "broken" flag */
4402                 if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
4403
4404                 /* Hack -- extract the "cursed" flag */
4405                 if (a_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
4406                 if (a_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
4407                 if (a_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
4408                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4409                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4410                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4411
4412
4413                 /* Cheat -- peek at the item */
4414                 if (cheat_peek) object_mention(o_ptr);
4415
4416                 /* Done */
4417                 return;
4418         }
4419
4420
4421         /* Apply magic */
4422         switch (o_ptr->tval)
4423         {
4424                 case TV_DIGGING:
4425                 case TV_HAFTED:
4426                 case TV_BOW:
4427                 case TV_SHOT:
4428                 case TV_ARROW:
4429                 case TV_BOLT:
4430                 {
4431                         if (power) a_m_aux_1(o_ptr, lev, power);
4432                         break;
4433                 }
4434
4435                 case TV_POLEARM:
4436                 {
4437                         if (power && !(o_ptr->sval == SV_DEATH_SCYTHE)) a_m_aux_1(o_ptr, lev, power);
4438                         break;
4439                 }
4440
4441                 case TV_SWORD:
4442                 {
4443                         if (power && !(o_ptr->sval == SV_DOKUBARI)) a_m_aux_1(o_ptr, lev, power);
4444                         break;
4445                 }
4446
4447                 case TV_DRAG_ARMOR:
4448                 case TV_HARD_ARMOR:
4449                 case TV_SOFT_ARMOR:
4450                 case TV_SHIELD:
4451                 case TV_HELM:
4452                 case TV_CROWN:
4453                 case TV_CLOAK:
4454                 case TV_GLOVES:
4455                 case TV_BOOTS:
4456                 {
4457                         /* Elven Cloak and Black Clothes ... */
4458                         if (((o_ptr->tval == TV_CLOAK) && (o_ptr->sval == SV_ELVEN_CLOAK)) ||
4459                             ((o_ptr->tval == TV_SOFT_ARMOR) && (o_ptr->sval == SV_KUROSHOUZOKU)))
4460                                 o_ptr->pval = randint1(4);
4461
4462 #if 1
4463                         if (power ||
4464                              ((o_ptr->tval == TV_HELM) && (o_ptr->sval == SV_DRAGON_HELM)) ||
4465                              ((o_ptr->tval == TV_SHIELD) && (o_ptr->sval == SV_DRAGON_SHIELD)) ||
4466                              ((o_ptr->tval == TV_GLOVES) && (o_ptr->sval == SV_SET_OF_DRAGON_GLOVES)) ||
4467                              ((o_ptr->tval == TV_BOOTS) && (o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE)))
4468                                 a_m_aux_2(o_ptr, lev, power);
4469 #else
4470                         if (power) a_m_aux_2(o_ptr, lev, power);
4471 #endif
4472                         break;
4473                 }
4474
4475                 case TV_RING:
4476                 case TV_AMULET:
4477                 {
4478                         if (!power && (randint0(100) < 50)) power = -1;
4479                         a_m_aux_3(o_ptr, lev, power);
4480                         break;
4481                 }
4482
4483                 default:
4484                 {
4485                         a_m_aux_4(o_ptr, lev, power);
4486                         break;
4487                 }
4488         }
4489
4490         if ((o_ptr->tval == TV_SOFT_ARMOR) &&
4491             (o_ptr->sval == SV_ABUNAI_MIZUGI) &&
4492             (p_ptr->pseikaku == SEIKAKU_SEXY))
4493         {
4494                 o_ptr->pval = 3;
4495                 add_flag(o_ptr->art_flags, TR_STR);
4496                 add_flag(o_ptr->art_flags, TR_INT);
4497                 add_flag(o_ptr->art_flags, TR_WIS);
4498                 add_flag(o_ptr->art_flags, TR_DEX);
4499                 add_flag(o_ptr->art_flags, TR_CON);
4500                 add_flag(o_ptr->art_flags, TR_CHR);
4501         }
4502
4503         /* Hack -- analyze ego-items */
4504         if (object_is_ego(o_ptr))
4505         {
4506                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
4507
4508                 /* Hack -- acquire "broken" flag */
4509                 if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
4510
4511                 /* Hack -- acquire "cursed" flag */
4512                 if (e_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
4513                 if (e_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
4514                 if (e_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
4515                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4516                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4517                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4518
4519                 if (e_ptr->gen_flags & (TRG_ONE_SUSTAIN)) one_sustain(o_ptr);
4520                 if (e_ptr->gen_flags & (TRG_XTRA_POWER)) one_ability(o_ptr);
4521                 if (e_ptr->gen_flags & (TRG_XTRA_H_RES)) one_high_resistance(o_ptr);
4522                 if (e_ptr->gen_flags & (TRG_XTRA_E_RES)) one_ele_resistance(o_ptr);
4523                 if (e_ptr->gen_flags & (TRG_XTRA_D_RES)) one_dragon_ele_resistance(o_ptr);
4524                 if (e_ptr->gen_flags & (TRG_XTRA_L_RES)) one_lordly_high_resistance(o_ptr);
4525                 if (e_ptr->gen_flags & (TRG_XTRA_RES)) one_resistance(o_ptr);
4526                 if (e_ptr->gen_flags & (TRG_XTRA_DICE))
4527                 {
4528                         do
4529                         {
4530                                 o_ptr->dd++;
4531                         }
4532                         while (one_in_(o_ptr->dd));
4533
4534                         if (o_ptr->dd > 9) o_ptr->dd = 9;
4535                 }
4536
4537                 /* Hack -- apply activatin index if needed */
4538                 if (e_ptr->act_idx) o_ptr->xtra2 = e_ptr->act_idx;
4539
4540                 /* Hack -- apply extra penalties if needed */
4541                 if ((object_is_cursed(o_ptr) || object_is_broken(o_ptr)) && !(e_ptr->gen_flags & (TRG_POWERFUL)))
4542                 {
4543                         /* Hack -- obtain bonuses */
4544                         if (e_ptr->max_to_h) o_ptr->to_h -= randint1(e_ptr->max_to_h);
4545                         if (e_ptr->max_to_d) o_ptr->to_d -= randint1(e_ptr->max_to_d);
4546                         if (e_ptr->max_to_a) o_ptr->to_a -= randint1(e_ptr->max_to_a);
4547
4548                         /* Hack -- obtain pval */
4549                         if (e_ptr->max_pval) o_ptr->pval -= randint1(e_ptr->max_pval);
4550                 }
4551
4552                 /* Hack -- apply extra bonuses if needed */
4553                 else
4554                 {
4555                         /* Hack -- obtain bonuses */
4556                         if (e_ptr->max_to_h)
4557                         {
4558                                 if (e_ptr->max_to_h > 127)
4559                                         o_ptr->to_h -= randint1(256-e_ptr->max_to_h);
4560                                 else o_ptr->to_h += randint1(e_ptr->max_to_h);
4561                         }
4562                         if (e_ptr->max_to_d)
4563                         {
4564                                 if (e_ptr->max_to_d > 127)
4565                                         o_ptr->to_d -= randint1(256-e_ptr->max_to_d);
4566                                 else o_ptr->to_d += randint1(e_ptr->max_to_d);
4567                         }
4568                         if (e_ptr->max_to_a)
4569                         {
4570                                 if (e_ptr->max_to_a > 127)
4571                                         o_ptr->to_a -= randint1(256-e_ptr->max_to_a);
4572                                 else o_ptr->to_a += randint1(e_ptr->max_to_a);
4573                         }
4574                         
4575                         /* Accuracy ego must have high to_h */
4576                         if(o_ptr->name2 == EGO_ACCURACY)
4577                         {
4578                                 while(o_ptr->to_h < o_ptr->to_d + 10)
4579                                 {
4580                                         o_ptr->to_h += 5;
4581                                         o_ptr->to_d -= 5;
4582                                 }
4583                                 o_ptr->to_h = MAX(o_ptr->to_h, 15);
4584                         }
4585                         
4586                         /* Accuracy ego must have high to_h */
4587                         if(o_ptr->name2 == EGO_VELOCITY)
4588                         {
4589                                 while(o_ptr->to_d < o_ptr->to_h + 10)
4590                                 {
4591                                         o_ptr->to_d += 5;
4592                                         o_ptr->to_h -= 5;
4593                                 }
4594                                 o_ptr->to_d = MAX(o_ptr->to_d, 15);
4595                         }
4596                         
4597                         /* Protection ego must have high to_a */
4598                         if((o_ptr->name2 == EGO_PROTECTION) || (o_ptr->name2 == EGO_S_PROTECTION) || (o_ptr->name2 == EGO_H_PROTECTION))
4599                         {
4600                                 o_ptr->to_a = MAX(o_ptr->to_a, 15);
4601                         }
4602
4603                         /* Hack -- obtain pval */
4604                         if (e_ptr->max_pval)
4605                         {
4606                                 if ((o_ptr->name2 == EGO_HA) && (have_flag(o_ptr->art_flags, TR_BLOWS)))
4607                                 {
4608                                         o_ptr->pval++;
4609                                         if ((lev > 60) && one_in_(3) && ((o_ptr->dd*(o_ptr->ds+1)) < 15)) o_ptr->pval++;
4610                                 }
4611                                 else if (o_ptr->name2 == EGO_DEMON)
4612                                 {
4613                                         if(have_flag(o_ptr->art_flags, TR_BLOWS))
4614                                         {
4615                                                 o_ptr->pval += randint1(2);
4616                                         }
4617                                         else
4618                                         {
4619                                                 o_ptr->pval += randint1(e_ptr->max_pval);
4620                                         }
4621                                 }
4622                                 else if (o_ptr->name2 == EGO_ATTACKS)
4623                                 {
4624                                         o_ptr->pval = randint1(e_ptr->max_pval*lev/100+1);
4625                                         if (o_ptr->pval > 3) o_ptr->pval = 3;
4626                                         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA))
4627                                                 o_ptr->pval += randint1(2);
4628                                 }
4629                                 else if (o_ptr->name2 == EGO_BAT)
4630                                 {
4631                                         o_ptr->pval = randint1(e_ptr->max_pval);
4632                                         if (o_ptr->sval == SV_ELVEN_CLOAK) o_ptr->pval += randint1(2);
4633                                 }
4634                                 else if (o_ptr->name2 == EGO_A_DEMON || o_ptr->name2 == EGO_DRUID || o_ptr->name2 == EGO_OLOG)
4635                                 {
4636                                         o_ptr->pval = randint1(e_ptr->max_pval);
4637                                 }
4638                                 else
4639                                 {
4640                                         o_ptr->pval += randint1(e_ptr->max_pval);
4641                                 }
4642                                 
4643                                 
4644                         }
4645                         if ((o_ptr->name2 == EGO_SPEED) && (lev < 50))
4646                         {
4647                                 o_ptr->pval = randint1(o_ptr->pval);
4648                         }
4649                         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA) && (o_ptr->pval > 2) && (o_ptr->name2 != EGO_ATTACKS))
4650                                 o_ptr->pval = 2;
4651                 }
4652
4653                 /* Cheat -- describe the item */
4654                 if (cheat_peek) object_mention(o_ptr);
4655                 
4656                 /* Done */
4657                 return;
4658         }
4659
4660         /* Examine real objects */
4661         if (o_ptr->k_idx)
4662         {
4663                 object_kind *k_ptr = &k_info[o_ptr->k_idx];
4664
4665                 /* Hack -- acquire "broken" flag */
4666                 if (!k_info[o_ptr->k_idx].cost) o_ptr->ident |= (IDENT_BROKEN);
4667
4668                 /* Hack -- acquire "cursed" flag */
4669                 if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
4670                 if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
4671                 if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
4672                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4673                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4674                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4675         }
4676 }
4677
4678
4679 /*
4680  * Hack -- determine if a template is "good"
4681  */
4682 static bool kind_is_good(int k_idx)
4683 {
4684         object_kind *k_ptr = &k_info[k_idx];
4685
4686         /* Analyze the item type */
4687         switch (k_ptr->tval)
4688         {
4689                 /* Armor -- Good unless damaged */
4690                 case TV_HARD_ARMOR:
4691                 case TV_SOFT_ARMOR:
4692                 case TV_DRAG_ARMOR:
4693                 case TV_SHIELD:
4694                 case TV_CLOAK:
4695                 case TV_BOOTS:
4696                 case TV_GLOVES:
4697                 case TV_HELM:
4698                 case TV_CROWN:
4699                 {
4700                         if (k_ptr->to_a < 0) return (FALSE);
4701                         return (TRUE);
4702                 }
4703
4704                 /* Weapons -- Good unless damaged */
4705                 case TV_BOW:
4706                 case TV_SWORD:
4707                 case TV_HAFTED:
4708                 case TV_POLEARM:
4709                 case TV_DIGGING:
4710                 {
4711                         if (k_ptr->to_h < 0) return (FALSE);
4712                         if (k_ptr->to_d < 0) return (FALSE);
4713                         return (TRUE);
4714                 }
4715
4716                 /* Ammo -- Arrows/Bolts are good */
4717                 case TV_BOLT:
4718                 case TV_ARROW:
4719                 {
4720                         return (TRUE);
4721                 }
4722
4723                 /* Books -- High level books are good (except Arcane books) */
4724                 case TV_LIFE_BOOK:
4725                 case TV_SORCERY_BOOK:
4726                 case TV_NATURE_BOOK:
4727                 case TV_CHAOS_BOOK:
4728                 case TV_DEATH_BOOK:
4729                 case TV_TRUMP_BOOK:
4730                 case TV_CRAFT_BOOK:
4731                 case TV_DAEMON_BOOK:
4732                 case TV_CRUSADE_BOOK:
4733                 case TV_MUSIC_BOOK:
4734                 case TV_HISSATSU_BOOK:
4735                 case TV_HEX_BOOK:
4736                 {
4737                         if (k_ptr->sval >= SV_BOOK_MIN_GOOD) return (TRUE);
4738                         return (FALSE);
4739                 }
4740
4741                 /* Rings -- Rings of Speed are good */
4742                 case TV_RING:
4743                 {
4744                         if (k_ptr->sval == SV_RING_SPEED) return (TRUE);
4745                         if (k_ptr->sval == SV_RING_LORDLY) return (TRUE);
4746                         return (FALSE);
4747                 }
4748
4749                 /* Amulets -- Amulets of the Magi and Resistance are good */
4750                 case TV_AMULET:
4751                 {
4752                         if (k_ptr->sval == SV_AMULET_THE_MAGI) return (TRUE);
4753                         if (k_ptr->sval == SV_AMULET_RESISTANCE) return (TRUE);
4754                         return (FALSE);
4755                 }
4756         }
4757
4758         /* Assume not good */
4759         return (FALSE);
4760 }
4761
4762
4763 /*
4764  * Attempt to make an object (normal or good/great)
4765  *
4766  * This routine plays nasty games to generate the "special artifacts".
4767  *
4768  * This routine uses "object_level" for the "generation level".
4769  *
4770  * We assume that the given object has been "wiped".
4771  */
4772 bool make_object(object_type *j_ptr, u32b mode)
4773 {
4774         int prob, base;
4775         byte obj_level;
4776
4777
4778         /* Chance of "special object" */
4779         prob = ((mode & AM_GOOD) ? 10 : 1000);
4780
4781         /* Base level for the object */
4782         base = ((mode & AM_GOOD) ? (object_level + 10) : object_level);
4783
4784
4785         /* Generate a special object, or a normal object */
4786         if (!one_in_(prob) || !make_artifact_special(j_ptr))
4787         {
4788                 int k_idx;
4789
4790                 /* Good objects */
4791                 if ((mode & AM_GOOD) && !get_obj_num_hook)
4792                 {
4793                         /* Activate restriction (if already specified, use that) */
4794                         get_obj_num_hook = kind_is_good;
4795                 }
4796
4797                 /* Restricted objects - prepare allocation table */
4798                 if (get_obj_num_hook) get_obj_num_prep();
4799
4800                 /* Pick a random object */
4801                 k_idx = get_obj_num(base);
4802
4803                 /* Restricted objects */
4804                 if (get_obj_num_hook)
4805                 {
4806                         /* Clear restriction */
4807                         get_obj_num_hook = NULL;
4808
4809                         /* Reset allocation table to default */
4810                         get_obj_num_prep();
4811                 }
4812
4813                 /* Handle failure */
4814                 if (!k_idx) return (FALSE);
4815
4816                 /* Prepare the object */
4817                 object_prep(j_ptr, k_idx);
4818         }
4819
4820         /* Apply magic (allow artifacts) */
4821         apply_magic(j_ptr, object_level, mode);
4822
4823         /* Hack -- generate multiple spikes/missiles */
4824         switch (j_ptr->tval)
4825         {
4826                 case TV_SPIKE:
4827                 case TV_SHOT:
4828                 case TV_ARROW:
4829                 case TV_BOLT:
4830                 {
4831                         if (!j_ptr->name1)
4832                                 j_ptr->number = (byte)damroll(6, 7);
4833                 }
4834         }
4835
4836         obj_level = k_info[j_ptr->k_idx].level;
4837         if (object_is_fixed_artifact(j_ptr)) obj_level = a_info[j_ptr->name1].level;
4838
4839         /* Notice "okay" out-of-depth objects */
4840         if (!object_is_cursed(j_ptr) && !object_is_broken(j_ptr) &&
4841             (obj_level > dun_level))
4842         {
4843                 /* Cheat -- peek at items */
4844                 if (cheat_peek) object_mention(j_ptr);
4845         }
4846
4847         /* Success */
4848         return (TRUE);
4849 }
4850
4851
4852 /*
4853  * Attempt to place an object (normal or good/great) at the given location.
4854  *
4855  * This routine plays nasty games to generate the "special artifacts".
4856  *
4857  * This routine uses "object_level" for the "generation level".
4858  *
4859  * This routine requires a clean floor grid destination.
4860  */
4861 void place_object(int y, int x, u32b mode)
4862 {
4863         s16b o_idx;
4864
4865         /* Acquire grid */
4866         cave_type *c_ptr = &cave[y][x];
4867
4868         object_type forge;
4869         object_type *q_ptr;
4870
4871
4872         /* Paranoia -- check bounds */
4873         if (!in_bounds(y, x)) return;
4874
4875         /* Require floor space */
4876         if (!cave_drop_bold(y, x)) return;
4877
4878         /* Avoid stacking on other objects */
4879         if (c_ptr->o_idx) return;
4880
4881
4882         /* Get local object */
4883         q_ptr = &forge;
4884
4885         /* Wipe the object */
4886         object_wipe(q_ptr);
4887
4888         /* Make an object (if possible) */
4889         if (!make_object(q_ptr, mode)) return;
4890
4891
4892         /* Make an object */
4893         o_idx = o_pop();
4894
4895         /* Success */
4896         if (o_idx)
4897         {
4898                 object_type *o_ptr;
4899
4900                 /* Acquire object */
4901                 o_ptr = &o_list[o_idx];
4902
4903                 /* Structure Copy */
4904                 object_copy(o_ptr, q_ptr);
4905
4906                 /* Location */
4907                 o_ptr->iy = y;
4908                 o_ptr->ix = x;
4909
4910                 /* Build a stack */
4911                 o_ptr->next_o_idx = c_ptr->o_idx;
4912
4913                 /* Place the object */
4914                 c_ptr->o_idx = o_idx;
4915
4916                 /* Notice */
4917                 note_spot(y, x);
4918
4919                 /* Redraw */
4920                 lite_spot(y, x);
4921         }
4922         else
4923         {
4924                 /* Hack -- Preserve artifacts */
4925                 if (object_is_fixed_artifact(q_ptr))
4926                 {
4927                         a_info[q_ptr->name1].cur_num = 0;
4928                 }
4929         }
4930 }
4931
4932
4933 /*
4934  * Make a treasure object
4935  *
4936  * The location must be a legal, clean, floor grid.
4937  */
4938 bool make_gold(object_type *j_ptr)
4939 {
4940         int i;
4941
4942         s32b base;
4943
4944
4945         /* Hack -- Pick a Treasure variety */
4946         i = ((randint1(object_level + 2) + 2) / 2) - 1;
4947
4948         /* Apply "extra" magic */
4949         if (one_in_(GREAT_OBJ))
4950         {
4951                 i += randint1(object_level + 1);
4952         }
4953
4954         /* Hack -- Creeping Coins only generate "themselves" */
4955         if (coin_type) i = coin_type;
4956
4957         /* Do not create "illegal" Treasure Types */
4958         if (i >= MAX_GOLD) i = MAX_GOLD - 1;
4959
4960         /* Prepare a gold object */
4961         object_prep(j_ptr, OBJ_GOLD_LIST + i);
4962
4963         /* Hack -- Base coin cost */
4964         base = k_info[OBJ_GOLD_LIST+i].cost;
4965
4966         /* Determine how much the treasure is "worth" */
4967         j_ptr->pval = (base + (8L * randint1(base)) + randint1(8));
4968
4969         /* Success */
4970         return (TRUE);
4971 }
4972
4973
4974 /*
4975  * Places a treasure (Gold or Gems) at given location
4976  *
4977  * The location must be a legal, clean, floor grid.
4978  */
4979 void place_gold(int y, int x)
4980 {
4981         s16b o_idx;
4982
4983         /* Acquire grid */
4984         cave_type *c_ptr = &cave[y][x];
4985
4986
4987         object_type forge;
4988         object_type *q_ptr;
4989
4990
4991         /* Paranoia -- check bounds */
4992         if (!in_bounds(y, x)) return;
4993
4994         /* Require floor space */
4995         if (!cave_drop_bold(y, x)) return;
4996
4997         /* Avoid stacking on other objects */
4998         if (c_ptr->o_idx) return;
4999
5000
5001         /* Get local object */
5002         q_ptr = &forge;
5003
5004         /* Wipe the object */
5005         object_wipe(q_ptr);
5006
5007         /* Make some gold */
5008         if (!make_gold(q_ptr)) return;
5009
5010
5011         /* Make an object */
5012         o_idx = o_pop();
5013
5014         /* Success */
5015         if (o_idx)
5016         {
5017                 object_type *o_ptr;
5018
5019                 /* Acquire object */
5020                 o_ptr = &o_list[o_idx];
5021
5022                 /* Copy the object */
5023                 object_copy(o_ptr, q_ptr);
5024
5025                 /* Save location */
5026                 o_ptr->iy = y;
5027                 o_ptr->ix = x;
5028
5029                 /* Build a stack */
5030                 o_ptr->next_o_idx = c_ptr->o_idx;
5031
5032                 /* Place the object */
5033                 c_ptr->o_idx = o_idx;
5034
5035                 /* Notice */
5036                 note_spot(y, x);
5037
5038                 /* Redraw */
5039                 lite_spot(y, x);
5040         }
5041 }
5042
5043
5044 /*
5045  * Let an object fall to the ground at or near a location.
5046  *
5047  * The initial location is assumed to be "in_bounds()".
5048  *
5049  * This function takes a parameter "chance".  This is the percentage
5050  * chance that the item will "disappear" instead of drop.  If the object
5051  * has been thrown, then this is the chance of disappearance on contact.
5052  *
5053  * Hack -- this function uses "chance" to determine if it should produce
5054  * some form of "description" of the drop event (under the player).
5055  *
5056  * We check several locations to see if we can find a location at which
5057  * the object can combine, stack, or be placed.  Artifacts will try very
5058  * hard to be placed, including "teleporting" to a useful grid if needed.
5059  */
5060 s16b drop_near(object_type *j_ptr, int chance, int y, int x)
5061 {
5062         int i, k, d, s;
5063
5064         int bs, bn;
5065         int by, bx;
5066         int dy, dx;
5067         int ty, tx = 0;
5068
5069         s16b o_idx = 0;
5070
5071         s16b this_o_idx, next_o_idx = 0;
5072
5073         cave_type *c_ptr;
5074
5075         char o_name[MAX_NLEN];
5076
5077         bool flag = FALSE;
5078         bool done = FALSE;
5079
5080 #ifndef JP
5081         /* Extract plural */
5082         bool plural = (j_ptr->number != 1);
5083 #endif
5084
5085         /* Describe object */
5086         object_desc(o_name, j_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
5087
5088
5089         /* Handle normal "breakage" */
5090         if (!object_is_artifact(j_ptr) && (randint0(100) < chance))
5091         {
5092                 /* Message */
5093 #ifdef JP
5094                 msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
5095 #else
5096                 msg_format("The %s disappear%s.",
5097                            o_name, (plural ? "" : "s"));
5098 #endif
5099
5100
5101                 /* Debug */
5102 #ifdef JP
5103                 if (p_ptr->wizard) msg_print("(ÇË»)");
5104 #else
5105                 if (p_ptr->wizard) msg_print("(breakage)");
5106 #endif
5107
5108
5109                 /* Failure */
5110                 return (0);
5111         }
5112
5113
5114         /* Score */
5115         bs = -1;
5116
5117         /* Picker */
5118         bn = 0;
5119
5120         /* Default */
5121         by = y;
5122         bx = x;
5123
5124         /* Scan local grids */
5125         for (dy = -3; dy <= 3; dy++)
5126         {
5127                 /* Scan local grids */
5128                 for (dx = -3; dx <= 3; dx++)
5129                 {
5130                         bool comb = FALSE;
5131
5132                         /* Calculate actual distance */
5133                         d = (dy * dy) + (dx * dx);
5134
5135                         /* Ignore distant grids */
5136                         if (d > 10) continue;
5137
5138                         /* Location */
5139                         ty = y + dy;
5140                         tx = x + dx;
5141
5142                         /* Skip illegal grids */
5143                         if (!in_bounds(ty, tx)) continue;
5144
5145                         /* Require line of projection */
5146                         if (!projectable(y, x, ty, tx)) continue;
5147
5148                         /* Obtain grid */
5149                         c_ptr = &cave[ty][tx];
5150
5151                         /* Require floor space */
5152                         if (!cave_drop_bold(ty, tx)) continue;
5153
5154                         /* No objects */
5155                         k = 0;
5156
5157                         /* Scan objects in that grid */
5158                         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
5159                         {
5160                                 object_type *o_ptr;
5161
5162                                 /* Acquire object */
5163                                 o_ptr = &o_list[this_o_idx];
5164
5165                                 /* Acquire next object */
5166                                 next_o_idx = o_ptr->next_o_idx;
5167
5168                                 /* Check for possible combination */
5169                                 if (object_similar(o_ptr, j_ptr)) comb = TRUE;
5170
5171                                 /* Count objects */
5172                                 k++;
5173                         }
5174
5175                         /* Add new object */
5176                         if (!comb) k++;
5177
5178                         /* Paranoia */
5179                         if (k > 99) continue;
5180
5181                         /* Calculate score */
5182                         s = 1000 - (d + k * 5);
5183
5184                         /* Skip bad values */
5185                         if (s < bs) continue;
5186
5187                         /* New best value */
5188                         if (s > bs) bn = 0;
5189
5190                         /* Apply the randomizer to equivalent values */
5191                         if ((++bn >= 2) && !one_in_(bn)) continue;
5192
5193                         /* Keep score */
5194                         bs = s;
5195
5196                         /* Track it */
5197                         by = ty;
5198                         bx = tx;
5199
5200                         /* Okay */
5201                         flag = TRUE;
5202                 }
5203         }
5204
5205
5206         /* Handle lack of space */
5207         if (!flag && !object_is_artifact(j_ptr))
5208         {
5209                 /* Message */
5210 #ifdef JP
5211                 msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
5212 #else
5213                 msg_format("The %s disappear%s.",
5214                            o_name, (plural ? "" : "s"));
5215 #endif
5216
5217
5218                 /* Debug */
5219 #ifdef JP
5220                 if (p_ptr->wizard) msg_print("(¾²¥¹¥Ú¡¼¥¹¤¬¤Ê¤¤)");
5221 #else
5222                 if (p_ptr->wizard) msg_print("(no floor space)");
5223 #endif
5224
5225
5226                 /* Failure */
5227                 return (0);
5228         }
5229
5230
5231         /* Find a grid */
5232         for (i = 0; !flag && (i < 1000); i++)
5233         {
5234                 /* Bounce around */
5235                 ty = rand_spread(by, 1);
5236                 tx = rand_spread(bx, 1);
5237
5238                 /* Verify location */
5239                 if (!in_bounds(ty, tx)) continue;
5240
5241                 /* Bounce to that location */
5242                 by = ty;
5243                 bx = tx;
5244
5245                 /* Require floor space */
5246                 if (!cave_drop_bold(by, bx)) continue;
5247
5248                 /* Okay */
5249                 flag = TRUE;
5250         }
5251
5252
5253         if (!flag)
5254         {
5255                 int candidates = 0, pick;
5256
5257                 for (ty = 1; ty < cur_hgt - 1; ty++)
5258                 {
5259                         for (tx = 1; tx < cur_wid - 1; tx++)
5260                         {
5261                                 /* A valid space found */
5262                                 if (cave_drop_bold(ty, tx)) candidates++;
5263                         }
5264                 }
5265
5266                 /* No valid place! */
5267                 if (!candidates)
5268                 {
5269                         /* Message */
5270 #ifdef JP
5271                         msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
5272 #else
5273                         msg_format("The %s disappear%s.", o_name, (plural ? "" : "s"));
5274 #endif
5275
5276                         /* Debug */
5277 #ifdef JP
5278                         if (p_ptr->wizard) msg_print("(¾²¥¹¥Ú¡¼¥¹¤¬¤Ê¤¤)");
5279 #else
5280                         if (p_ptr->wizard) msg_print("(no floor space)");
5281 #endif
5282
5283                         /* Mega-Hack -- preserve artifacts */
5284                         if (preserve_mode)
5285                         {
5286                                 /* Hack -- Preserve unknown artifacts */
5287                                 if (object_is_fixed_artifact(j_ptr) && !object_is_known(j_ptr))
5288                                 {
5289                                         /* Mega-Hack -- Preserve the artifact */
5290                                         a_info[j_ptr->name1].cur_num = 0;
5291                                 }
5292                         }
5293
5294                         /* Failure */
5295                         return 0;
5296                 }
5297
5298                 /* Choose a random one */
5299                 pick = randint1(candidates);
5300
5301                 for (ty = 1; ty < cur_hgt - 1; ty++)
5302                 {
5303                         for (tx = 1; tx < cur_wid - 1; tx++)
5304                         {
5305                                 if (cave_drop_bold(ty, tx))
5306                                 {
5307                                         pick--;
5308
5309                                         /* Is this a picked one? */
5310                                         if (!pick) break;
5311                                 }
5312                         }
5313
5314                         if (!pick) break;
5315                 }
5316
5317                 by = ty;
5318                 bx = tx;
5319         }
5320
5321
5322         /* Grid */
5323         c_ptr = &cave[by][bx];
5324
5325         /* Scan objects in that grid for combination */
5326         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
5327         {
5328                 object_type *o_ptr;
5329
5330                 /* Acquire object */
5331                 o_ptr = &o_list[this_o_idx];
5332
5333                 /* Acquire next object */
5334                 next_o_idx = o_ptr->next_o_idx;
5335
5336                 /* Check for combination */
5337                 if (object_similar(o_ptr, j_ptr))
5338                 {
5339                         /* Combine the items */
5340                         object_absorb(o_ptr, j_ptr);
5341
5342                         /* Success */
5343                         done = TRUE;
5344
5345                         /* Done */
5346                         break;
5347                 }
5348         }
5349
5350         /* Get new object */
5351         if (!done) o_idx = o_pop();
5352
5353         /* Failure */
5354         if (!done && !o_idx)
5355         {
5356                 /* Message */
5357 #ifdef JP
5358                 msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
5359 #else
5360                 msg_format("The %s disappear%s.",
5361                            o_name, (plural ? "" : "s"));
5362 #endif
5363
5364
5365                 /* Debug */
5366 #ifdef JP
5367                 if (p_ptr->wizard) msg_print("(¥¢¥¤¥Æ¥à¤¬Â¿²á¤®¤ë)");
5368 #else
5369                 if (p_ptr->wizard) msg_print("(too many objects)");
5370 #endif
5371
5372
5373                 /* Hack -- Preserve artifacts */
5374                 if (object_is_fixed_artifact(j_ptr))
5375                 {
5376                         a_info[j_ptr->name1].cur_num = 0;
5377                 }
5378
5379                 /* Failure */
5380                 return (0);
5381         }
5382
5383         /* Stack */
5384         if (!done)
5385         {
5386                 /* Structure copy */
5387                 object_copy(&o_list[o_idx], j_ptr);
5388
5389                 /* Access new object */
5390                 j_ptr = &o_list[o_idx];
5391
5392                 /* Locate */
5393                 j_ptr->iy = by;
5394                 j_ptr->ix = bx;
5395
5396                 /* No monster */
5397                 j_ptr->held_m_idx = 0;
5398
5399                 /* Build a stack */
5400                 j_ptr->next_o_idx = c_ptr->o_idx;
5401
5402                 /* Place the object */
5403                 c_ptr->o_idx = o_idx;
5404
5405                 /* Success */
5406                 done = TRUE;
5407         }
5408
5409         /* Note the spot */
5410         note_spot(by, bx);
5411
5412         /* Draw the spot */
5413         lite_spot(by, bx);
5414
5415         /* Sound */
5416         sound(SOUND_DROP);
5417
5418         /* Mega-Hack -- no message if "dropped" by player */
5419         /* Message when an object falls under the player */
5420         if (chance && player_bold(by, bx))
5421         {
5422 #ifdef JP
5423                 msg_print("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
5424 #else
5425                 msg_print("You feel something roll beneath your feet.");
5426 #endif
5427
5428         }
5429
5430         /* XXX XXX XXX */
5431
5432         /* Result */
5433         return (o_idx);
5434 }
5435
5436
5437 /*
5438  * Scatter some "great" objects near the player
5439  */
5440 void acquirement(int y1, int x1, int num, bool great, bool special, bool known)
5441 {
5442         object_type *i_ptr;
5443         object_type object_type_body;
5444         u32b mode = AM_GOOD | (great || special ? AM_GREAT : 0L) | (special ? AM_SPECIAL : 0L) ;
5445
5446         /* Acquirement */
5447         while (num--)
5448         {
5449                 /* Get local object */
5450                 i_ptr = &object_type_body;
5451
5452                 /* Wipe the object */
5453                 object_wipe(i_ptr);
5454
5455                 /* Make a good (or great) object (if possible) */
5456                 if (!make_object(i_ptr, mode)) continue;
5457
5458                 if (known)
5459                 {
5460                         object_aware(i_ptr);
5461                         object_known(i_ptr);
5462                 }
5463
5464                 /* Drop the object */
5465                 (void)drop_near(i_ptr, -1, y1, x1);
5466         }
5467 }
5468
5469 /*
5470  * Scatter some "amusing" objects near the player
5471  */
5472
5473 #define AMS_NOTHING   0x00 /* No restriction */
5474 #define AMS_NO_UNIQUE 0x01 /* Don't make the amusing object of uniques */
5475 #define AMS_FIXED_ART 0x02 /* Make a fixed artifact based on the amusing object */
5476 #define AMS_MULTIPLE  0x04 /* Drop 1-3 objects for one type */
5477 #define AMS_PILE      0x08 /* Drop 1-99 pile objects for one type */
5478
5479 typedef struct
5480 {
5481         int tval;
5482         int sval;
5483         int prob;
5484         byte flag;
5485 } amuse_type;
5486
5487 amuse_type amuse_info[] =
5488 {
5489         { TV_BOTTLE, SV_ANY, 5, AMS_NOTHING },
5490         { TV_JUNK, SV_ANY, 3, AMS_MULTIPLE },
5491         { TV_SPIKE, SV_ANY, 10, AMS_PILE },
5492         { TV_STATUE, SV_ANY, 15, AMS_NOTHING },
5493         { TV_CORPSE, SV_ANY, 15, AMS_NO_UNIQUE },
5494         { TV_SKELETON, SV_ANY, 10, AMS_NO_UNIQUE },
5495         { TV_FIGURINE, SV_ANY, 10, AMS_NO_UNIQUE },
5496         { TV_PARCHMENT, SV_ANY, 1, AMS_NOTHING },
5497         { TV_POLEARM, SV_TSURIZAO, 3, AMS_NOTHING }, //Fishing Pole of Taikobo
5498         { TV_SWORD, SV_BROKEN_DAGGER, 3, AMS_FIXED_ART }, //Broken Dagger of Magician
5499         { TV_SWORD, SV_BROKEN_DAGGER, 10, AMS_NOTHING },
5500         { TV_SWORD, SV_BROKEN_SWORD, 5, AMS_NOTHING },
5501         { TV_SCROLL, SV_SCROLL_AMUSEMENT, 10, AMS_NOTHING },
5502
5503         { 0, 0, 0 }
5504 };
5505
5506 void amusement(int y1, int x1, int num, bool known)
5507 {
5508         object_type *i_ptr;
5509         object_type object_type_body;
5510         int n, t = 0;
5511
5512         for (n = 0; amuse_info[n].tval != 0; n++)
5513         {
5514                 t += amuse_info[n].prob;
5515         }
5516
5517         /* Acquirement */
5518         while (num)
5519         {
5520                 int i, k_idx, a_idx = 0;
5521                 int r = randint0(t);
5522                 bool insta_art, fixed_art;
5523
5524                 for (i = 0; ; i++)
5525                 {
5526                         r -= amuse_info[i].prob;
5527                         if (r <= 0) break;
5528                 }
5529
5530                 /* Get local object */
5531                 i_ptr = &object_type_body;
5532
5533                 /* Wipe the object */
5534                 object_wipe(i_ptr);
5535
5536                 /* Wipe the object */
5537                 k_idx = lookup_kind(amuse_info[i].tval, amuse_info[i].sval);
5538
5539                 /* Paranoia - reroll if nothing */
5540                 if (!k_idx) continue;
5541
5542                 /* Search an artifact index if need */
5543                 insta_art = (k_info[k_idx].gen_flags & TRG_INSTA_ART);
5544                 fixed_art = (amuse_info[i].flag & AMS_FIXED_ART);
5545
5546                 if (insta_art || fixed_art)
5547                 {
5548                         for (a_idx = 1; a_idx < max_a_idx; a_idx++)
5549                         {
5550                                 if (insta_art && !(a_info[a_idx].gen_flags & TRG_INSTA_ART)) continue;
5551                                 if (a_info[a_idx].tval != k_info[k_idx].tval) continue;
5552                                 if (a_info[a_idx].sval != k_info[k_idx].sval) continue;
5553                                 if (a_info[a_idx].cur_num > 0) continue;
5554                                 break;
5555                         }
5556
5557                         if (a_idx >= max_a_idx) continue;
5558                 }
5559
5560                 /* Make an object (if possible) */
5561                 object_prep(i_ptr, k_idx);
5562                 if (a_idx) i_ptr->name1 = a_idx;
5563                 apply_magic(i_ptr, 1, AM_NO_FIXED_ART);
5564
5565                 if (amuse_info[i].flag & AMS_NO_UNIQUE)
5566                 {
5567                         if (r_info[i_ptr->pval].flags1 & RF1_UNIQUE) continue;
5568                 }
5569
5570                 if (amuse_info[i].flag & AMS_MULTIPLE) i_ptr->number = randint1(3);
5571                 if (amuse_info[i].flag & AMS_PILE) i_ptr->number = randint1(99);
5572
5573                 if (known)
5574                 {
5575                         object_aware(i_ptr);
5576                         object_known(i_ptr);
5577                 }
5578
5579                 /* Paranoia - reroll if nothing */
5580                 if (!(i_ptr->k_idx)) continue;
5581
5582                 /* Drop the object */
5583                 (void)drop_near(i_ptr, -1, y1, x1);
5584
5585                 num--;
5586         }
5587 }
5588
5589
5590 #define MAX_NORMAL_TRAPS 18
5591
5592 /* See init_feat_variables() in init2.c */
5593 static s16b normal_traps[MAX_NORMAL_TRAPS];
5594
5595 /*
5596  * Initialize arrays for normal traps
5597  */
5598 void init_normal_traps(void)
5599 {
5600         int cur_trap = 0;
5601
5602         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_TRAPDOOR");
5603         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_PIT");
5604         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_SPIKED_PIT");
5605         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_POISON_PIT");
5606         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_TY_CURSE");
5607         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_TELEPORT");
5608         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_FIRE");
5609         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_ACID");
5610         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_SLOW");
5611         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_LOSE_STR");
5612         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_LOSE_DEX");
5613         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_LOSE_CON");
5614         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_BLIND");
5615         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_CONFUSE");
5616         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_POISON");
5617         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_SLEEP");
5618         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_TRAPS");
5619         normal_traps[cur_trap++] = f_tag_to_index_in_init("TRAP_ALARM");
5620 }
5621
5622 /*
5623  * Get random trap
5624  *
5625  * XXX XXX XXX This routine should be redone to reflect trap "level".
5626  * That is, it does not make sense to have spiked pits at 50 feet.
5627  * Actually, it is not this routine, but the "trap instantiation"
5628  * code, which should also check for "trap doors" on quest levels.
5629  */
5630 s16b choose_random_trap(void)
5631 {
5632         s16b feat;
5633
5634         /* Pick a trap */
5635         while (1)
5636         {
5637                 /* Hack -- pick a trap */
5638                 feat = normal_traps[randint0(MAX_NORMAL_TRAPS)];
5639
5640                 /* Accept non-trapdoors */
5641                 if (!have_flag(f_info[feat].flags, FF_MORE)) break;
5642
5643                 /* Hack -- no trap doors on special levels */
5644                 if (p_ptr->inside_arena || quest_number(dun_level)) continue;
5645
5646                 /* Hack -- no trap doors on the deepest level */
5647                 if (dun_level >= d_info[dungeon_type].maxdepth) continue;
5648
5649                 break;
5650         }
5651
5652         return feat;
5653 }
5654
5655 /*
5656  * Disclose an invisible trap
5657  */
5658 void disclose_grid(int y, int x)
5659 {
5660         cave_type *c_ptr = &cave[y][x];
5661
5662         if (cave_have_flag_grid(c_ptr, FF_SECRET))
5663         {
5664                 /* No longer hidden */
5665                 cave_alter_feat(y, x, FF_SECRET);
5666         }
5667         else if (c_ptr->mimic)
5668         {
5669                 /* No longer hidden */
5670                 c_ptr->mimic = 0;
5671
5672                 /* Notice */
5673                 note_spot(y, x);
5674
5675                 /* Redraw */
5676                 lite_spot(y, x);
5677         }
5678 }
5679
5680
5681 /*
5682  * Places a random trap at the given location.
5683  *
5684  * The location must be a legal, naked, floor grid.
5685  *
5686  * Note that all traps start out as "invisible" and "untyped", and then
5687  * when they are "discovered" (by detecting them or setting them off),
5688  * the trap is "instantiated" as a visible, "typed", trap.
5689  */
5690 void place_trap(int y, int x)
5691 {
5692         cave_type *c_ptr = &cave[y][x];
5693
5694         /* Paranoia -- verify location */
5695         if (!in_bounds(y, x)) return;
5696
5697         /* Require empty, clean, floor grid */
5698         if (!cave_clean_bold(y, x)) return;
5699
5700         /* Place an invisible trap */
5701         c_ptr->mimic = c_ptr->feat;
5702         c_ptr->feat = choose_random_trap();
5703 }
5704
5705
5706 /*
5707  * Describe the charges on an item in the inventory.
5708  */
5709 void inven_item_charges(int item)
5710 {
5711         object_type *o_ptr = &inventory[item];
5712
5713         /* Require staff/wand */
5714         if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
5715
5716         /* Require known item */
5717         if (!object_is_known(o_ptr)) return;
5718
5719 #ifdef JP
5720         if (o_ptr->pval <= 0)
5721         {
5722                 msg_print("¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
5723         }
5724         else
5725         {
5726                 msg_format("¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
5727         }
5728 #else
5729         /* Multiple charges */
5730         if (o_ptr->pval != 1)
5731         {
5732                 /* Print a message */
5733                 msg_format("You have %d charges remaining.", o_ptr->pval);
5734         }
5735
5736         /* Single charge */
5737         else
5738         {
5739                 /* Print a message */
5740                 msg_format("You have %d charge remaining.", o_ptr->pval);
5741         }
5742 #endif
5743
5744 }
5745
5746
5747 /*
5748  * Describe an item in the inventory.
5749  */
5750 void inven_item_describe(int item)
5751 {
5752         object_type *o_ptr = &inventory[item];
5753         char        o_name[MAX_NLEN];
5754
5755         /* Get a description */
5756         object_desc(o_name, o_ptr, 0);
5757
5758         /* Print a message */
5759 #ifdef JP
5760         /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤¹¤ë */
5761         if (o_ptr->number <= 0)
5762         {
5763                 /*FIRST*//*¤³¤³¤Ï¤â¤¦Ä̤é¤Ê¤¤¤«¤â */
5764                 msg_format("¤â¤¦%s¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£", o_name);
5765         }
5766         else
5767         {
5768                 /* ¥¢¥¤¥Æ¥à̾¤ò±ÑÆüÀÚ¤êÂؤ¨µ¡Ç½Âбþ */
5769                 msg_format("¤Þ¤À %s¤ò»ý¤Ã¤Æ¤¤¤ë¡£", o_name);
5770         }
5771 #else
5772         msg_format("You have %s.", o_name);
5773 #endif
5774
5775 }
5776
5777
5778 /*
5779  * Increase the "number" of an item in the inventory
5780  */
5781 void inven_item_increase(int item, int num)
5782 {
5783         object_type *o_ptr = &inventory[item];
5784
5785         /* Apply */
5786         num += o_ptr->number;
5787
5788         /* Bounds check */
5789         if (num > 255) num = 255;
5790         else if (num < 0) num = 0;
5791
5792         /* Un-apply */
5793         num -= o_ptr->number;
5794
5795         /* Change the number and weight */
5796         if (num)
5797         {
5798                 /* Add the number */
5799                 o_ptr->number += num;
5800
5801                 /* Add the weight */
5802                 p_ptr->total_weight += (num * o_ptr->weight);
5803
5804                 /* Recalculate bonuses */
5805                 p_ptr->update |= (PU_BONUS);
5806
5807                 /* Recalculate mana XXX */
5808                 p_ptr->update |= (PU_MANA);
5809
5810                 /* Combine the pack */
5811                 p_ptr->notice |= (PN_COMBINE);
5812
5813                 /* Window stuff */
5814                 p_ptr->window |= (PW_INVEN | PW_EQUIP);
5815
5816                 /* Hack -- Clear temporary elemental brands if player takes off weapons */
5817                 if (!o_ptr->number && p_ptr->ele_attack)
5818                 {
5819                         if ((item == INVEN_RARM) || (item == INVEN_LARM))
5820                         {
5821                                 if (!buki_motteruka(INVEN_RARM + INVEN_LARM - item))
5822                                 {
5823                                         /* Clear all temporary elemental brands */
5824                                         set_ele_attack(0, 0);
5825                                 }
5826                         }
5827                 }
5828         }
5829 }
5830
5831
5832 /*
5833  * Erase an inventory slot if it has no more items
5834  */
5835 void inven_item_optimize(int item)
5836 {
5837         object_type *o_ptr = &inventory[item];
5838
5839         /* Only optimize real items */
5840         if (!o_ptr->k_idx) return;
5841
5842         /* Only optimize empty items */
5843         if (o_ptr->number) return;
5844
5845         /* The item is in the pack */
5846         if (item < INVEN_RARM)
5847         {
5848                 int i;
5849
5850                 /* One less item */
5851                 inven_cnt--;
5852
5853                 /* Slide everything down */
5854                 for (i = item; i < INVEN_PACK; i++)
5855                 {
5856                         /* Structure copy */
5857                         inventory[i] = inventory[i+1];
5858                 }
5859
5860                 /* Erase the "final" slot */
5861                 object_wipe(&inventory[i]);
5862
5863                 /* Window stuff */
5864                 p_ptr->window |= (PW_INVEN);
5865         }
5866
5867         /* The item is being wielded */
5868         else
5869         {
5870                 /* One less item */
5871                 equip_cnt--;
5872
5873                 /* Erase the empty slot */
5874                 object_wipe(&inventory[item]);
5875
5876                 /* Recalculate bonuses */
5877                 p_ptr->update |= (PU_BONUS);
5878
5879                 /* Recalculate torch */
5880                 p_ptr->update |= (PU_TORCH);
5881
5882                 /* Recalculate mana XXX */
5883                 p_ptr->update |= (PU_MANA);
5884
5885                 /* Window stuff */
5886                 p_ptr->window |= (PW_EQUIP);
5887         }
5888
5889         /* Window stuff */
5890         p_ptr->window |= (PW_SPELL);
5891 }
5892
5893
5894 /*
5895  * Describe the charges on an item on the floor.
5896  */
5897 void floor_item_charges(int item)
5898 {
5899         object_type *o_ptr = &o_list[item];
5900
5901         /* Require staff/wand */
5902         if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
5903
5904         /* Require known item */
5905         if (!object_is_known(o_ptr)) return;
5906
5907 #ifdef JP
5908         if (o_ptr->pval <= 0)
5909         {
5910                 msg_print("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
5911         }
5912         else
5913         {
5914                 msg_format("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
5915         }
5916 #else
5917         /* Multiple charges */
5918         if (o_ptr->pval != 1)
5919         {
5920                 /* Print a message */
5921                 msg_format("There are %d charges remaining.", o_ptr->pval);
5922         }
5923
5924         /* Single charge */
5925         else
5926         {
5927                 /* Print a message */
5928                 msg_format("There is %d charge remaining.", o_ptr->pval);
5929         }
5930 #endif
5931
5932 }
5933
5934
5935 /*
5936  * Describe an item in the inventory.
5937  */
5938 void floor_item_describe(int item)
5939 {
5940         object_type *o_ptr = &o_list[item];
5941         char        o_name[MAX_NLEN];
5942
5943         /* Get a description */
5944         object_desc(o_name, o_ptr, 0);
5945
5946         /* Print a message */
5947 #ifdef JP
5948         /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤òʬ¤±¤ë */
5949         if (o_ptr->number <= 0)
5950         {
5951                 msg_format("¾²¾å¤Ë¤Ï¡¢¤â¤¦%s¤Ï¤Ê¤¤¡£", o_name);
5952         }
5953         else
5954         {
5955                 msg_format("¾²¾å¤Ë¤Ï¡¢¤Þ¤À %s¤¬¤¢¤ë¡£", o_name);
5956         }
5957 #else
5958         msg_format("You see %s.", o_name);
5959 #endif
5960
5961 }
5962
5963
5964 /*
5965  * Increase the "number" of an item on the floor
5966  */
5967 void floor_item_increase(int item, int num)
5968 {
5969         object_type *o_ptr = &o_list[item];
5970
5971         /* Apply */
5972         num += o_ptr->number;
5973
5974         /* Bounds check */
5975         if (num > 255) num = 255;
5976         else if (num < 0) num = 0;
5977
5978         /* Un-apply */
5979         num -= o_ptr->number;
5980
5981         /* Change the number */
5982         o_ptr->number += num;
5983 }
5984
5985
5986 /*
5987  * Optimize an item on the floor (destroy "empty" items)
5988  */
5989 void floor_item_optimize(int item)
5990 {
5991         object_type *o_ptr = &o_list[item];
5992
5993         /* Paranoia -- be sure it exists */
5994         if (!o_ptr->k_idx) return;
5995
5996         /* Only optimize empty items */
5997         if (o_ptr->number) return;
5998
5999         /* Delete the object */
6000         delete_object_idx(item);
6001 }
6002
6003
6004 /*
6005  * Check if we have space for an item in the pack without overflow
6006  */
6007 bool inven_carry_okay(object_type *o_ptr)
6008 {
6009         int j;
6010
6011         /* Empty slot? */
6012         if (inven_cnt < INVEN_PACK) return (TRUE);
6013
6014         /* Similar slot? */
6015         for (j = 0; j < INVEN_PACK; j++)
6016         {
6017                 object_type *j_ptr = &inventory[j];
6018
6019                 /* Skip non-objects */
6020                 if (!j_ptr->k_idx) continue;
6021
6022                 /* Check if the two items can be combined */
6023                 if (object_similar(j_ptr, o_ptr)) return (TRUE);
6024         }
6025
6026         /* Nope */
6027         return (FALSE);
6028 }
6029
6030
6031 bool object_sort_comp(object_type *o_ptr, s32b o_value, object_type *j_ptr)
6032 {
6033         int o_type, j_type;
6034
6035         /* Use empty slots */
6036         if (!j_ptr->k_idx) return TRUE;
6037
6038         /* Hack -- readable books always come first */
6039         if ((o_ptr->tval == REALM1_BOOK) &&
6040             (j_ptr->tval != REALM1_BOOK)) return TRUE;
6041         if ((j_ptr->tval == REALM1_BOOK) &&
6042             (o_ptr->tval != REALM1_BOOK)) return FALSE;
6043
6044         if ((o_ptr->tval == REALM2_BOOK) &&
6045             (j_ptr->tval != REALM2_BOOK)) return TRUE;
6046         if ((j_ptr->tval == REALM2_BOOK) &&
6047             (o_ptr->tval != REALM2_BOOK)) return FALSE;
6048
6049         /* Objects sort by decreasing type */
6050         if (o_ptr->tval > j_ptr->tval) return TRUE;
6051         if (o_ptr->tval < j_ptr->tval) return FALSE;
6052
6053         /* Non-aware (flavored) items always come last */
6054         /* Can happen in the home */
6055         if (!object_is_aware(o_ptr)) return FALSE;
6056         if (!object_is_aware(j_ptr)) return TRUE;
6057
6058         /* Objects sort by increasing sval */
6059         if (o_ptr->sval < j_ptr->sval) return TRUE;
6060         if (o_ptr->sval > j_ptr->sval) return FALSE;
6061
6062         /* Unidentified objects always come last */
6063         /* Objects in the home can be unknown */
6064         if (!object_is_known(o_ptr)) return FALSE;
6065         if (!object_is_known(j_ptr)) return TRUE;
6066
6067         /* Fixed artifacts, random artifacts and ego items */
6068         if (object_is_fixed_artifact(o_ptr)) o_type = 3;
6069         else if (o_ptr->art_name) o_type = 2;
6070         else if (object_is_ego(o_ptr)) o_type = 1;
6071         else o_type = 0;
6072
6073         if (object_is_fixed_artifact(j_ptr)) j_type = 3;
6074         else if (j_ptr->art_name) j_type = 2;
6075         else if (object_is_ego(j_ptr)) j_type = 1;
6076         else j_type = 0;
6077
6078         if (o_type < j_type) return TRUE;
6079         if (o_type > j_type) return FALSE;
6080
6081         switch (o_ptr->tval)
6082         {
6083         case TV_FIGURINE:
6084         case TV_STATUE:
6085         case TV_CORPSE:
6086         case TV_CAPTURE:
6087                 if (r_info[o_ptr->pval].level < r_info[j_ptr->pval].level) return TRUE;
6088                 if ((r_info[o_ptr->pval].level == r_info[j_ptr->pval].level) && (o_ptr->pval < j_ptr->pval)) return TRUE;
6089                 return FALSE;
6090
6091         case TV_SHOT:
6092         case TV_ARROW:
6093         case TV_BOLT:
6094                 /* Objects sort by increasing hit/damage bonuses */
6095                 if (o_ptr->to_h + o_ptr->to_d < j_ptr->to_h + j_ptr->to_d) return TRUE;
6096                 if (o_ptr->to_h + o_ptr->to_d > j_ptr->to_h + j_ptr->to_d) return FALSE;
6097                 break;
6098
6099         /* Hack:  otherwise identical rods sort by
6100         increasing recharge time --dsb */
6101         case TV_ROD:
6102                 if (o_ptr->pval < j_ptr->pval) return TRUE;
6103                 if (o_ptr->pval > j_ptr->pval) return FALSE;
6104                 break;
6105         }
6106
6107         /* Objects sort by decreasing value */
6108         return o_value > object_value(j_ptr);
6109 }
6110
6111
6112 /*
6113  * Add an item to the players inventory, and return the slot used.
6114  *
6115  * If the new item can combine with an existing item in the inventory,
6116  * it will do so, using "object_similar()" and "object_absorb()", else,
6117  * the item will be placed into the "proper" location in the inventory.
6118  *
6119  * This function can be used to "over-fill" the player's pack, but only
6120  * once, and such an action must trigger the "overflow" code immediately.
6121  * Note that when the pack is being "over-filled", the new item must be
6122  * placed into the "overflow" slot, and the "overflow" must take place
6123  * before the pack is reordered, but (optionally) after the pack is
6124  * combined.  This may be tricky.  See "dungeon.c" for info.
6125  *
6126  * Note that this code must remove any location/stack information
6127  * from the object once it is placed into the inventory.
6128  */
6129 s16b inven_carry(object_type *o_ptr)
6130 {
6131         int i, j, k;
6132         int n = -1;
6133
6134         object_type *j_ptr;
6135
6136
6137         /* Check for combining */
6138         for (j = 0; j < INVEN_PACK; j++)
6139         {
6140                 j_ptr = &inventory[j];
6141
6142                 /* Skip non-objects */
6143                 if (!j_ptr->k_idx) continue;
6144
6145                 /* Hack -- track last item */
6146                 n = j;
6147
6148                 /* Check if the two items can be combined */
6149                 if (object_similar(j_ptr, o_ptr))
6150                 {
6151                         /* Combine the items */
6152                         object_absorb(j_ptr, o_ptr);
6153
6154                         /* Increase the weight */
6155                         p_ptr->total_weight += (o_ptr->number * o_ptr->weight);
6156
6157                         /* Recalculate bonuses */
6158                         p_ptr->update |= (PU_BONUS);
6159
6160                         /* Window stuff */
6161                         p_ptr->window |= (PW_INVEN);
6162
6163                         /* Success */
6164                         return (j);
6165                 }
6166         }
6167
6168
6169         /* Paranoia */
6170         if (inven_cnt > INVEN_PACK) return (-1);
6171
6172         /* Find an empty slot */
6173         for (j = 0; j <= INVEN_PACK; j++)
6174         {
6175                 j_ptr = &inventory[j];
6176
6177                 /* Use it if found */
6178                 if (!j_ptr->k_idx) break;
6179         }
6180
6181         /* Use that slot */
6182         i = j;
6183
6184
6185         /* Reorder the pack */
6186         if (i < INVEN_PACK)
6187         {
6188                 /* Get the "value" of the item */
6189                 s32b o_value = object_value(o_ptr);
6190
6191                 /* Scan every occupied slot */
6192                 for (j = 0; j < INVEN_PACK; j++)
6193                 {
6194                         if (object_sort_comp(o_ptr, o_value, &inventory[j])) break;
6195                 }
6196
6197                 /* Use that slot */
6198                 i = j;
6199
6200                 /* Slide objects */
6201                 for (k = n; k >= i; k--)
6202                 {
6203                         /* Hack -- Slide the item */
6204                         object_copy(&inventory[k+1], &inventory[k]);
6205                 }
6206
6207                 /* Wipe the empty slot */
6208                 object_wipe(&inventory[i]);
6209         }
6210
6211
6212         /* Copy the item */
6213         object_copy(&inventory[i], o_ptr);
6214
6215         /* Access new object */
6216         j_ptr = &inventory[i];
6217
6218         /* Forget stack */
6219         j_ptr->next_o_idx = 0;
6220
6221         /* Forget monster */
6222         j_ptr->held_m_idx = 0;
6223
6224         /* Forget location */
6225         j_ptr->iy = j_ptr->ix = 0;
6226
6227         /* Player touches it, and no longer marked */
6228         j_ptr->marked = OM_TOUCHED;
6229
6230         /* Increase the weight */
6231         p_ptr->total_weight += (j_ptr->number * j_ptr->weight);
6232
6233         /* Count the items */
6234         inven_cnt++;
6235
6236         /* Recalculate bonuses */
6237         p_ptr->update |= (PU_BONUS);
6238
6239         /* Combine and Reorder pack */
6240         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
6241
6242         /* Window stuff */
6243         p_ptr->window |= (PW_INVEN);
6244
6245         /* Return the slot */
6246         return (i);
6247 }
6248
6249
6250 /*
6251  * Take off (some of) a non-cursed equipment item
6252  *
6253  * Note that only one item at a time can be wielded per slot.
6254  *
6255  * Note that taking off an item when "full" may cause that item
6256  * to fall to the ground.
6257  *
6258  * Return the inventory slot into which the item is placed.
6259  */
6260 s16b inven_takeoff(int item, int amt)
6261 {
6262         int slot;
6263
6264         object_type forge;
6265         object_type *q_ptr;
6266
6267         object_type *o_ptr;
6268
6269         cptr act;
6270
6271         char o_name[MAX_NLEN];
6272
6273
6274         /* Get the item to take off */
6275         o_ptr = &inventory[item];
6276
6277         /* Paranoia */
6278         if (amt <= 0) return (-1);
6279
6280         /* Verify */
6281         if (amt > o_ptr->number) amt = o_ptr->number;
6282
6283         /* Get local object */
6284         q_ptr = &forge;
6285
6286         /* Obtain a local object */
6287         object_copy(q_ptr, o_ptr);
6288
6289         /* Modify quantity */
6290         q_ptr->number = amt;
6291
6292         /* Describe the object */
6293         object_desc(o_name, q_ptr, 0);
6294
6295         /* Took off weapon */
6296         if (((item == INVEN_RARM) || (item == INVEN_LARM)) &&
6297             object_is_melee_weapon(o_ptr))
6298         {
6299 #ifdef JP
6300                 act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
6301 #else
6302                 act = "You were wielding";
6303 #endif
6304
6305         }
6306
6307         /* Took off bow */
6308         else if (item == INVEN_BOW)
6309         {
6310 #ifdef JP
6311                 act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
6312 #else
6313                 act = "You were holding";
6314 #endif
6315
6316         }
6317
6318         /* Took off light */
6319         else if (item == INVEN_LITE)
6320         {
6321 #ifdef JP
6322                 act = "¤ò¸÷¸»¤«¤é¤Ï¤º¤·¤¿";
6323 #else
6324                 act = "You were holding";
6325 #endif
6326
6327         }
6328
6329         /* Took off something */
6330         else
6331         {
6332 #ifdef JP
6333                 act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
6334 #else
6335                 act = "You were wearing";
6336 #endif
6337
6338         }
6339
6340         /* Modify, Optimize */
6341         inven_item_increase(item, -amt);
6342         inven_item_optimize(item);
6343
6344         /* Carry the object */
6345         slot = inven_carry(q_ptr);
6346
6347         /* Message */
6348 #ifdef JP
6349         msg_format("%s(%c)%s¡£", o_name, index_to_label(slot), act);
6350 #else
6351         msg_format("%s %s (%c).", act, o_name, index_to_label(slot));
6352 #endif
6353
6354
6355         /* Return slot */
6356         return (slot);
6357 }
6358
6359
6360 /*
6361  * Drop (some of) a non-cursed inventory/equipment item
6362  *
6363  * The object will be dropped "near" the current location
6364  */
6365 void inven_drop(int item, int amt)
6366 {
6367         object_type forge;
6368         object_type *q_ptr;
6369
6370         object_type *o_ptr;
6371
6372         char o_name[MAX_NLEN];
6373
6374
6375         /* Access original object */
6376         o_ptr = &inventory[item];
6377
6378         /* Error check */
6379         if (amt <= 0) return;
6380
6381         /* Not too many */
6382         if (amt > o_ptr->number) amt = o_ptr->number;
6383
6384
6385         /* Take off equipment */
6386         if (item >= INVEN_RARM)
6387         {
6388                 /* Take off first */
6389                 item = inven_takeoff(item, amt);
6390
6391                 /* Access original object */
6392                 o_ptr = &inventory[item];
6393         }
6394
6395
6396         /* Get local object */
6397         q_ptr = &forge;
6398
6399         /* Obtain local object */
6400         object_copy(q_ptr, o_ptr);
6401
6402         /* Distribute charges of wands or rods */
6403         distribute_charges(o_ptr, q_ptr, amt);
6404
6405         /* Modify quantity */
6406         q_ptr->number = amt;
6407
6408         /* Describe local object */
6409         object_desc(o_name, q_ptr, 0);
6410
6411         /* Message */
6412 #ifdef JP
6413         msg_format("%s(%c)¤òÍî¤È¤·¤¿¡£", o_name, index_to_label(item));
6414 #else
6415         msg_format("You drop %s (%c).", o_name, index_to_label(item));
6416 #endif
6417
6418
6419         /* Drop it near the player */
6420         (void)drop_near(q_ptr, 0, py, px);
6421
6422         /* Modify, Describe, Optimize */
6423         inven_item_increase(item, -amt);
6424         inven_item_describe(item);
6425         inven_item_optimize(item);
6426 }
6427
6428
6429 /*
6430  * Combine items in the pack
6431  *
6432  * Note special handling of the "overflow" slot
6433  */
6434 void combine_pack(void)
6435 {
6436         int             i, j, k;
6437         object_type     *o_ptr;
6438         object_type     *j_ptr;
6439         bool            flag = FALSE, combined;
6440
6441         do
6442         {
6443                 combined = FALSE;
6444
6445                 /* Combine the pack (backwards) */
6446                 for (i = INVEN_PACK; i > 0; i--)
6447                 {
6448                         /* Get the item */
6449                         o_ptr = &inventory[i];
6450
6451                         /* Skip empty items */
6452                         if (!o_ptr->k_idx) continue;
6453
6454                         /* Scan the items above that item */
6455                         for (j = 0; j < i; j++)
6456                         {
6457                                 int max_num;
6458
6459                                 /* Get the item */
6460                                 j_ptr = &inventory[j];
6461
6462                                 /* Skip empty items */
6463                                 if (!j_ptr->k_idx) continue;
6464
6465                                 /*
6466                                  * Get maximum number of the stack if these
6467                                  * are similar, get zero otherwise.
6468                                  */
6469                                 max_num = object_similar_part(j_ptr, o_ptr);
6470
6471                                 /* Can we (partialy) drop "o_ptr" onto "j_ptr"? */
6472                                 if (max_num && j_ptr->number < max_num)
6473                                 {
6474                                         if (o_ptr->number + j_ptr->number <= max_num)
6475                                         {
6476                                                 /* Take note */
6477                                                 flag = TRUE;
6478
6479                                                 /* Add together the item counts */
6480                                                 object_absorb(j_ptr, o_ptr);
6481
6482                                                 /* One object is gone */
6483                                                 inven_cnt--;
6484
6485                                                 /* Slide everything down */
6486                                                 for (k = i; k < INVEN_PACK; k++)
6487                                                 {
6488                                                         /* Structure copy */
6489                                                         inventory[k] = inventory[k+1];
6490                                                 }
6491
6492                                                 /* Erase the "final" slot */
6493                                                 object_wipe(&inventory[k]);
6494                                         }
6495                                         else
6496                                         {
6497                                                 int old_num = o_ptr->number;
6498                                                 int remain = j_ptr->number + o_ptr->number - max_num;
6499 #if 0
6500                                                 o_ptr->number -= remain;
6501 #endif
6502                                                 /* Add together the item counts */
6503                                                 object_absorb(j_ptr, o_ptr);
6504
6505                                                 o_ptr->number = remain;
6506
6507                                                 /* Hack -- if rods are stacking, add the pvals (maximum timeouts) and current timeouts together. -LM- */
6508                                                 if (o_ptr->tval == TV_ROD)
6509                                                 {
6510                                                         o_ptr->pval =  o_ptr->pval * remain / old_num;
6511                                                         o_ptr->timeout = o_ptr->timeout * remain / old_num;
6512                                                 }
6513
6514                                                 /* Hack -- if wands are stacking, combine the charges. -LM- */
6515                                                 if (o_ptr->tval == TV_WAND)
6516                                                 {
6517                                                         o_ptr->pval = o_ptr->pval * remain / old_num;
6518                                                 }
6519                                         }
6520
6521                                         /* Window stuff */
6522                                         p_ptr->window |= (PW_INVEN);
6523
6524                                         /* Take note */
6525                                         combined = TRUE;
6526
6527                                         /* Done */
6528                                         break;
6529                                 }
6530                         }
6531                 }
6532         }
6533         while (combined);
6534
6535         /* Message */
6536 #ifdef JP
6537         if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤ò¤Þ¤È¤áľ¤·¤¿¡£");
6538 #else
6539         if (flag) msg_print("You combine some items in your pack.");
6540 #endif
6541 }
6542
6543
6544 /*
6545  * Reorder items in the pack
6546  *
6547  * Note special handling of the "overflow" slot
6548  */
6549 void reorder_pack(void)
6550 {
6551         int             i, j, k;
6552         s32b            o_value;
6553         object_type     forge;
6554         object_type     *q_ptr;
6555         object_type     *o_ptr;
6556         bool            flag = FALSE;
6557
6558
6559         /* Re-order the pack (forwards) */
6560         for (i = 0; i < INVEN_PACK; i++)
6561         {
6562                 /* Mega-Hack -- allow "proper" over-flow */
6563                 if ((i == INVEN_PACK) && (inven_cnt == INVEN_PACK)) break;
6564
6565                 /* Get the item */
6566                 o_ptr = &inventory[i];
6567
6568                 /* Skip empty slots */
6569                 if (!o_ptr->k_idx) continue;
6570
6571                 /* Get the "value" of the item */
6572                 o_value = object_value(o_ptr);
6573
6574                 /* Scan every occupied slot */
6575                 for (j = 0; j < INVEN_PACK; j++)
6576                 {
6577                         if (object_sort_comp(o_ptr, o_value, &inventory[j])) break;
6578                 }
6579
6580                 /* Never move down */
6581                 if (j >= i) continue;
6582
6583                 /* Take note */
6584                 flag = TRUE;
6585
6586                 /* Get local object */
6587                 q_ptr = &forge;
6588
6589                 /* Save a copy of the moving item */
6590                 object_copy(q_ptr, &inventory[i]);
6591
6592                 /* Slide the objects */
6593                 for (k = i; k > j; k--)
6594                 {
6595                         /* Slide the item */
6596                         object_copy(&inventory[k], &inventory[k-1]);
6597                 }
6598
6599                 /* Insert the moving item */
6600                 object_copy(&inventory[j], q_ptr);
6601
6602                 /* Window stuff */
6603                 p_ptr->window |= (PW_INVEN);
6604         }
6605
6606         /* Message */
6607 #ifdef JP
6608         if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤òÊ¤Ùľ¤·¤¿¡£");
6609 #else
6610         if (flag) msg_print("You reorder some items in your pack.");
6611 #endif
6612
6613 }
6614
6615
6616 /*
6617  * Hack -- display an object kind in the current window
6618  *
6619  * Include list of usable spells for readible books
6620  */
6621 void display_koff(int k_idx)
6622 {
6623         int y;
6624
6625         object_type forge;
6626         object_type *q_ptr;
6627         int         sval;
6628         int         use_realm;
6629
6630         char o_name[MAX_NLEN];
6631
6632
6633         /* Erase the window */
6634         for (y = 0; y < Term->hgt; y++)
6635         {
6636                 /* Erase the line */
6637                 Term_erase(0, y, 255);
6638         }
6639
6640         /* No info */
6641         if (!k_idx) return;
6642
6643         /* Get local object */
6644         q_ptr = &forge;
6645
6646         /* Prepare the object */
6647         object_prep(q_ptr, k_idx);
6648
6649         /* Describe */
6650         object_desc(o_name, q_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY | OD_STORE));
6651
6652         /* Mention the object name */
6653         Term_putstr(0, 0, -1, TERM_WHITE, o_name);
6654
6655         /* Access the item's sval */
6656         sval = q_ptr->sval;
6657         use_realm = tval2realm(q_ptr->tval);
6658
6659         /* Warriors are illiterate */
6660         if (p_ptr->realm1 || p_ptr->realm2)
6661         {
6662                 if ((use_realm != p_ptr->realm1) && (use_realm != p_ptr->realm2)) return;
6663         }
6664         else
6665         {
6666                 if ((p_ptr->pclass != CLASS_SORCERER) && (p_ptr->pclass != CLASS_RED_MAGE)) return;
6667                 if (!is_magic(use_realm)) return;
6668                 if ((p_ptr->pclass == CLASS_RED_MAGE) && (use_realm != REALM_ARCANE) && (sval > 1)) return;
6669         }
6670
6671         /* Display spells in readible books */
6672         {
6673                 int     spell = -1;
6674                 int     num = 0;
6675                 byte    spells[64];
6676
6677                 /* Extract spells */
6678                 for (spell = 0; spell < 32; spell++)
6679                 {
6680                         /* Check for this spell */
6681                         if (fake_spell_flags[sval] & (1L << spell))
6682                         {
6683                                 /* Collect this spell */
6684                                 spells[num++] = spell;
6685                         }
6686                 }
6687
6688                 /* Print spells */
6689                 print_spells(0, spells, num, 2, 0, use_realm);
6690         }
6691 }
6692
6693 /* Choose one of items that have warning flag */
6694 object_type *choose_warning_item(void)
6695 {
6696         int i;
6697         int choices[INVEN_TOTAL - INVEN_RARM];
6698         int number = 0;
6699
6700         /* Paranoia -- Player has no warning ability */
6701         if (!p_ptr->warning) return NULL;
6702
6703         /* Search Inventory */
6704         for (i = INVEN_RARM; i < INVEN_TOTAL; i++)
6705         {
6706                 u32b flgs[TR_FLAG_SIZE];
6707                 object_type *o_ptr = &inventory[i];
6708
6709                 object_flags(o_ptr, flgs);
6710                 if (have_flag(flgs, TR_WARNING))
6711                 {
6712                         choices[number] = i;
6713                         number++;
6714                 }
6715         }
6716
6717         /* Choice one of them */
6718         return number ? &inventory[choices[randint0(number)]] : NULL;
6719 }
6720
6721 /* Calculate spell damages */
6722 static void spell_damcalc(monster_type *m_ptr, int typ, int dam, int limit, int *max)
6723 {
6724         monster_race *r_ptr = &r_info[m_ptr->r_idx];
6725         int          rlev = r_ptr->level;
6726         bool         ignore_wraith_form = FALSE;
6727
6728         if (limit) dam = (dam > limit) ? limit : dam;
6729
6730         /* Vulnerability, resistance and immunity */
6731         switch (typ)
6732         {
6733         case GF_ELEC:
6734                 if (p_ptr->immune_elec)
6735                 {
6736                         dam = 0;
6737                         ignore_wraith_form = TRUE;
6738                 }
6739                 else
6740                 {
6741                         if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
6742                         if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
6743                         if (prace_is_(RACE_ANDROID)) dam += dam / 3;
6744                         if (p_ptr->resist_elec) dam = (dam + 2) / 3;
6745                         if (IS_OPPOSE_ELEC())
6746                                 dam = (dam + 2) / 3;
6747                 }
6748                 break;
6749
6750         case GF_POIS:
6751                 if (p_ptr->resist_pois) dam = (dam + 2) / 3;
6752                 if (IS_OPPOSE_POIS()) dam = (dam + 2) / 3;
6753                 break;
6754
6755         case GF_ACID:
6756                 if (p_ptr->immune_acid)
6757                 {
6758                         dam = 0;
6759                         ignore_wraith_form = TRUE;
6760                 }
6761                 else
6762                 {
6763                         if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
6764                         if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
6765                         if (p_ptr->resist_acid) dam = (dam + 2) / 3;
6766                         if (IS_OPPOSE_ACID()) dam = (dam + 2) / 3;
6767                 }
6768                 break;
6769
6770         case GF_COLD:
6771         case GF_ICE:
6772                 if (p_ptr->immune_cold)
6773                 {
6774                         dam = 0;
6775                         ignore_wraith_form = TRUE;
6776                 }
6777                 else
6778                 {
6779                         if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
6780                         if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
6781                         if (p_ptr->resist_cold) dam = (dam + 2) / 3;
6782                         if (IS_OPPOSE_COLD()) dam = (dam + 2) / 3;
6783                 }
6784                 break;
6785
6786         case GF_FIRE:
6787                 if (p_ptr->immune_fire)
6788                 {
6789                         dam = 0;
6790                         ignore_wraith_form = TRUE;
6791                 }
6792                 else
6793                 {
6794                         if (p_ptr->muta3 & MUT3_VULN_ELEM) dam *= 2;
6795                         if (prace_is_(RACE_ENT)) dam += dam / 3;
6796                         if (p_ptr->special_defense & KATA_KOUKIJIN) dam += dam / 3;
6797                         if (p_ptr->resist_fire) dam = (dam + 2) / 3;
6798                         if (IS_OPPOSE_FIRE()) dam = (dam + 2) / 3;
6799                 }
6800                 break;
6801
6802         case GF_PSY_SPEAR:
6803                 ignore_wraith_form = TRUE;
6804                 break;
6805
6806         case GF_ARROW:
6807                 if (!p_ptr->blind &&
6808                     ((inventory[INVEN_RARM].k_idx && (inventory[INVEN_RARM].name1 == ART_ZANTETSU)) ||
6809                      (inventory[INVEN_LARM].k_idx && (inventory[INVEN_LARM].name1 == ART_ZANTETSU))))
6810                 {
6811                         dam = 0;
6812                         ignore_wraith_form = TRUE;
6813                 }
6814                 break;
6815
6816         case GF_LITE:
6817                 if (p_ptr->resist_lite) dam /= 2; /* Worst case of 4 / (d4 + 7) */
6818                 if (prace_is_(RACE_VAMPIRE) || (p_ptr->mimic_form == MIMIC_VAMPIRE)) dam *= 2;
6819                 else if (prace_is_(RACE_S_FAIRY)) dam = dam * 4 / 3;
6820
6821                 /*
6822                  * Cannot use "ignore_wraith_form" strictly (for "random one damage")
6823                  * "dam *= 2;" for later "dam /= 2"
6824                  */
6825                 if (p_ptr->wraith_form) dam *= 2;
6826                 break;
6827
6828         case GF_DARK:
6829                 if (prace_is_(RACE_VAMPIRE) || (p_ptr->mimic_form == MIMIC_VAMPIRE) || p_ptr->wraith_form)
6830                 {
6831                         dam = 0;
6832                         ignore_wraith_form = TRUE;
6833                 }
6834                 else if (p_ptr->resist_dark) dam /= 2; /* Worst case of 4 / (d4 + 7) */
6835                 break;
6836
6837         case GF_SHARDS:
6838                 if (p_ptr->resist_shard) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
6839                 break;
6840
6841         case GF_SOUND:
6842                 if (p_ptr->resist_sound) dam = dam * 5 / 8; /* Worst case of 5 / (d4 + 7) */
6843                 break;
6844
6845         case GF_CONFUSION:
6846                 if (p_ptr->resist_conf) dam = dam * 5 / 8; /* Worst case of 5 / (d4 + 7) */
6847                 break;
6848
6849         case GF_CHAOS:
6850                 if (p_ptr->resist_chaos) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
6851                 break;
6852
6853         case GF_NETHER:
6854                 if (prace_is_(RACE_SPECTRE))
6855                 {
6856                         dam = 0;
6857                         ignore_wraith_form = TRUE;
6858                 }
6859                 else if (p_ptr->resist_neth) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
6860                 break;
6861
6862         case GF_DISENCHANT:
6863                 if (p_ptr->resist_disen) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
6864                 break;
6865
6866         case GF_NEXUS:
6867                 if (p_ptr->resist_nexus) dam = dam * 3 / 4; /* Worst case of 6 / (d4 + 7) */
6868                 break;
6869
6870         case GF_TIME:
6871                 if (p_ptr->resist_time) dam /= 2; /* Worst case of 4 / (d4 + 7) */
6872                 break;
6873
6874         case GF_GRAVITY:
6875                 if (p_ptr->levitation) dam = (dam * 2) / 3;
6876                 break;
6877
6878         case GF_ROCKET:
6879                 if (p_ptr->resist_shard) dam /= 2;
6880                 break;
6881
6882         case GF_NUKE:
6883                 if (p_ptr->resist_pois) dam = (2 * dam + 2) / 5;
6884                 if (IS_OPPOSE_POIS()) dam = (2 * dam + 2) / 5;
6885                 break;
6886
6887         case GF_DEATH_RAY:
6888                 if (p_ptr->mimic_form)
6889                 {
6890                         if (mimic_info[p_ptr->mimic_form].MIMIC_FLAGS & MIMIC_IS_NONLIVING)
6891                         {
6892                                 dam = 0;
6893                                 ignore_wraith_form = TRUE;
6894                         }
6895                 }
6896                 else
6897                 {
6898                         switch (p_ptr->prace)
6899                         {
6900                         case RACE_GOLEM:
6901                         case RACE_SKELETON:
6902                         case RACE_ZOMBIE:
6903                         case RACE_VAMPIRE:
6904                         case RACE_DEMON:
6905                         case RACE_SPECTRE:
6906                                 dam = 0;
6907                                 ignore_wraith_form = TRUE;
6908                                 break;
6909                         }
6910                 }
6911                 break;
6912
6913         case GF_HOLY_FIRE:
6914                 if (p_ptr->align > 10) dam /= 2;
6915                 else if (p_ptr->align < -10) dam *= 2;
6916                 break;
6917
6918         case GF_HELL_FIRE:
6919                 if (p_ptr->align > 10) dam *= 2;
6920                 break;
6921
6922         case GF_MIND_BLAST:
6923         case GF_BRAIN_SMASH:
6924                 if (100 + rlev / 2 <= MAX(5, p_ptr->skill_sav))
6925                 {
6926                         dam = 0;
6927                         ignore_wraith_form = TRUE;
6928                 }
6929                 break;
6930
6931         case GF_CAUSE_1:
6932         case GF_CAUSE_2:
6933         case GF_CAUSE_3:
6934         case GF_HAND_DOOM:
6935                 if (100 + rlev / 2 <= p_ptr->skill_sav)
6936                 {
6937                         dam = 0;
6938                         ignore_wraith_form = TRUE;
6939                 }
6940                 break;
6941
6942         case GF_CAUSE_4:
6943                 if ((100 + rlev / 2 <= p_ptr->skill_sav) && (m_ptr->r_idx != MON_KENSHIROU))
6944                 {
6945                         dam = 0;
6946                         ignore_wraith_form = TRUE;
6947                 }
6948                 break;
6949         }
6950
6951         if (p_ptr->wraith_form && !ignore_wraith_form)
6952         {
6953                 dam /= 2;
6954                 if (!dam) dam = 1;
6955         }
6956
6957         if (dam > *max) *max = dam;
6958 }
6959
6960 /* Calculate blow damages */
6961 static int blow_damcalc(monster_type *m_ptr, monster_blow *blow_ptr)
6962 {
6963         int  dam = blow_ptr->d_dice * blow_ptr->d_side;
6964         int  dummy_max = 0;
6965         bool check_wraith_form = TRUE;
6966
6967         if (blow_ptr->method != RBM_EXPLODE)
6968         {
6969                 int ac = p_ptr->ac + p_ptr->to_a;
6970
6971                 switch (blow_ptr->effect)
6972                 {
6973                 case RBE_SUPERHURT:
6974                 {
6975                         int tmp_dam = dam - (dam * ((ac < 150) ? ac : 150) / 250);
6976                         dam = MAX(dam, tmp_dam * 2);
6977                         break;
6978                 }
6979
6980                 case RBE_HURT:
6981                 case RBE_SHATTER:
6982                         dam -= (dam * ((ac < 150) ? ac : 150) / 250);
6983                         break;
6984
6985                 case RBE_ACID:
6986                         spell_damcalc(m_ptr, GF_ACID, dam, 0, &dummy_max);
6987                         dam = dummy_max;
6988                         check_wraith_form = FALSE;
6989                         break;
6990
6991                 case RBE_ELEC:
6992                         spell_damcalc(m_ptr, GF_ELEC, dam, 0, &dummy_max);
6993                         dam = dummy_max;
6994                         check_wraith_form = FALSE;
6995                         break;
6996
6997                 case RBE_FIRE:
6998                         spell_damcalc(m_ptr, GF_FIRE, dam, 0, &dummy_max);
6999                         dam = dummy_max;
7000                         check_wraith_form = FALSE;
7001                         break;
7002
7003                 case RBE_COLD:
7004                         spell_damcalc(m_ptr, GF_COLD, dam, 0, &dummy_max);
7005                         dam = dummy_max;
7006                         check_wraith_form = FALSE;
7007                         break;
7008
7009                 case RBE_DR_MANA:
7010                         dam = 0;
7011                         check_wraith_form = FALSE;
7012                         break;
7013                 }
7014
7015                 if (check_wraith_form && p_ptr->wraith_form)
7016                 {
7017                         dam /= 2;
7018                         if (!dam) dam = 1;
7019                 }
7020         }
7021         else
7022         {
7023                 dam = (dam + 1) / 2;
7024                 spell_damcalc(m_ptr, mbe_info[blow_ptr->effect].explode_type, dam, 0, &dummy_max);
7025                 dam = dummy_max;
7026         }
7027
7028         return dam;
7029 }
7030
7031 /* Examine the grid (xx,yy) and warn the player if there are any danger */
7032 bool process_warning(int xx, int yy)
7033 {
7034         int mx, my;
7035         cave_type *c_ptr;
7036         char o_name[MAX_NLEN];
7037
7038 #define WARNING_AWARE_RANGE 12
7039         int dam_max = 0;
7040         static int old_damage = 0;
7041
7042         for (mx = xx - WARNING_AWARE_RANGE; mx < xx + WARNING_AWARE_RANGE + 1; mx++)
7043         {
7044                 for (my = yy - WARNING_AWARE_RANGE; my < yy + WARNING_AWARE_RANGE + 1; my++)
7045                 {
7046                         int dam_max0 = 0;
7047                         monster_type *m_ptr;
7048                         monster_race *r_ptr;
7049
7050                         if (!in_bounds(my, mx) || (distance(my, mx, yy, xx) > WARNING_AWARE_RANGE)) continue;
7051
7052                         c_ptr = &cave[my][mx];
7053
7054                         if (!c_ptr->m_idx) continue;
7055
7056                         m_ptr = &m_list[c_ptr->m_idx];
7057
7058                         if (MON_CSLEEP(m_ptr)) continue;
7059                         if (!is_hostile(m_ptr)) continue;
7060
7061                         r_ptr = &r_info[m_ptr->r_idx];
7062
7063                         /* Monster spells (only powerful ones)*/
7064                         if (projectable(my, mx, yy, xx))
7065                         {
7066                                 int breath_dam_div3 = m_ptr->hp / 3;
7067                                 int breath_dam_div6 = m_ptr->hp / 6;
7068                                 u32b f4 = r_ptr->flags4;
7069                                 u32b f5 = r_ptr->flags5;
7070                                 u32b f6 = r_ptr->flags6;
7071
7072                                 if (!(d_info[dungeon_type].flags1 & DF1_NO_MAGIC))
7073                                 {
7074                                         int rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
7075                                         int storm_dam = rlev * 4 + 150;
7076                                         bool powerful = (bool)(r_ptr->flags2 & RF2_POWERFUL);
7077
7078                                         if (f4 & RF4_BA_CHAO) spell_damcalc(m_ptr, GF_CHAOS, rlev * (powerful ? 3 : 2) + 100, 0, &dam_max0);
7079                                         if (f5 & RF5_BA_MANA) spell_damcalc(m_ptr, GF_MANA, storm_dam, 0, &dam_max0);
7080                                         if (f5 & RF5_BA_DARK) spell_damcalc(m_ptr, GF_DARK, storm_dam, 0, &dam_max0);
7081                                         if (f5 & RF5_BA_LITE) spell_damcalc(m_ptr, GF_LITE, storm_dam, 0, &dam_max0);
7082                                         if (f6 & RF6_HAND_DOOM) spell_damcalc(m_ptr, GF_HAND_DOOM, p_ptr->chp * 6 / 10, 0, &dam_max0);
7083                                         if (f6 & RF6_PSY_SPEAR) spell_damcalc(m_ptr, GF_PSY_SPEAR, powerful ? (rlev * 2 + 150) : (rlev * 3 / 2 + 100), 0, &dam_max0);
7084                                 }
7085                                 if (f4 & RF4_ROCKET) spell_damcalc(m_ptr, GF_ROCKET, m_ptr->hp / 4, 800, &dam_max0);
7086                                 if (f4 & RF4_BR_ACID) spell_damcalc(m_ptr, GF_ACID, breath_dam_div3, 1600, &dam_max0);
7087                                 if (f4 & RF4_BR_ELEC) spell_damcalc(m_ptr, GF_ELEC, breath_dam_div3, 1600, &dam_max0);
7088                                 if (f4 & RF4_BR_FIRE) spell_damcalc(m_ptr, GF_FIRE, breath_dam_div3, 1600, &dam_max0);
7089                                 if (f4 & RF4_BR_COLD) spell_damcalc(m_ptr, GF_COLD, breath_dam_div3, 1600, &dam_max0);
7090                                 if (f4 & RF4_BR_POIS) spell_damcalc(m_ptr, GF_POIS, breath_dam_div3, 800, &dam_max0);
7091                                 if (f4 & RF4_BR_NETH) spell_damcalc(m_ptr, GF_NETHER, breath_dam_div6, 550, &dam_max0);
7092                                 if (f4 & RF4_BR_LITE) spell_damcalc(m_ptr, GF_LITE, breath_dam_div6, 400, &dam_max0);
7093                                 if (f4 & RF4_BR_DARK) spell_damcalc(m_ptr, GF_DARK, breath_dam_div6, 400, &dam_max0);
7094                                 if (f4 & RF4_BR_CONF) spell_damcalc(m_ptr, GF_CONFUSION, breath_dam_div6, 450, &dam_max0);
7095                                 if (f4 & RF4_BR_SOUN) spell_damcalc(m_ptr, GF_SOUND, breath_dam_div6, 450, &dam_max0);
7096                                 if (f4 & RF4_BR_CHAO) spell_damcalc(m_ptr, GF_CHAOS, breath_dam_div6, 600, &dam_max0);
7097                                 if (f4 & RF4_BR_DISE) spell_damcalc(m_ptr, GF_DISENCHANT, breath_dam_div6, 500, &dam_max0);
7098                                 if (f4 & RF4_BR_NEXU) spell_damcalc(m_ptr, GF_NEXUS, breath_dam_div3, 250, &dam_max0);
7099                                 if (f4 & RF4_BR_TIME) spell_damcalc(m_ptr, GF_TIME, breath_dam_div3, 150, &dam_max0);
7100                                 if (f4 & RF4_BR_INER) spell_damcalc(m_ptr, GF_INERTIA, breath_dam_div6, 200, &dam_max0);
7101                                 if (f4 & RF4_BR_GRAV) spell_damcalc(m_ptr, GF_GRAVITY, breath_dam_div3, 200, &dam_max0);
7102                                 if (f4 & RF4_BR_SHAR) spell_damcalc(m_ptr, GF_SHARDS, breath_dam_div6, 500, &dam_max0);
7103                                 if (f4 & RF4_BR_PLAS) spell_damcalc(m_ptr, GF_PLASMA, breath_dam_div6, 150, &dam_max0);
7104                                 if (f4 & RF4_BR_WALL) spell_damcalc(m_ptr, GF_FORCE, breath_dam_div6, 200, &dam_max0);
7105                                 if (f4 & RF4_BR_MANA) spell_damcalc(m_ptr, GF_MANA, breath_dam_div3, 250, &dam_max0);
7106                                 if (f4 & RF4_BR_NUKE) spell_damcalc(m_ptr, GF_NUKE, breath_dam_div3, 800, &dam_max0);
7107                                 if (f4 & RF4_BR_DISI) spell_damcalc(m_ptr, GF_DISINTEGRATE, breath_dam_div6, 150, &dam_max0);
7108                         }
7109
7110                         /* Monster melee attacks */
7111                         if (!(r_ptr->flags1 & RF1_NEVER_BLOW) && !(d_info[dungeon_type].flags1 & DF1_NO_MELEE))
7112                         {
7113                                 if (mx <= xx + 1 && mx >= xx - 1 && my <= yy + 1 && my >= yy - 1)
7114                                 {
7115                                         int m;
7116                                         int dam_melee = 0;
7117                                         for (m = 0; m < 4; m++)
7118                                         {
7119                                                 /* Skip non-attacks */
7120                                                 if (!r_ptr->blow[m].method || (r_ptr->blow[m].method == RBM_SHOOT)) continue;
7121
7122                                                 /* Extract the attack info */
7123                                                 dam_melee += blow_damcalc(m_ptr, &r_ptr->blow[m]);
7124                                                 if (r_ptr->blow[m].method == RBM_EXPLODE) break;
7125                                         }
7126                                         if (dam_melee > dam_max0) dam_max0 = dam_melee;
7127                                 }
7128                         }
7129
7130                         /* Contribution from this monster */
7131                         dam_max += dam_max0;
7132                 }
7133         }
7134
7135         /* Prevent excessive warning */
7136         if (dam_max > old_damage)
7137         {
7138                 old_damage = dam_max * 3 / 2;
7139
7140                 if (dam_max > p_ptr->chp / 2)
7141                 {
7142                         object_type *o_ptr = choose_warning_item();
7143
7144                         if (o_ptr) object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
7145 #ifdef JP
7146                         else strcpy(o_name, "ÂÎ"); /* Warning ability without item */
7147                         msg_format("%s¤¬±Ô¤¯¿Ì¤¨¤¿¡ª", o_name);
7148 #else
7149                         else strcpy(o_name, "body"); /* Warning ability without item */
7150                         msg_format("Your %s pulsates sharply!", o_name);
7151 #endif
7152                         disturb(0, 1);
7153 #ifdef JP
7154                         return get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©");
7155 #else
7156                         return get_check("Really want to go ahead? ");
7157 #endif
7158                 }
7159         }
7160         else old_damage = old_damage / 2;
7161
7162         c_ptr = &cave[yy][xx];
7163         if (((!easy_disarm && is_trap(c_ptr->feat))
7164             || (c_ptr->mimic && is_trap(c_ptr->feat))) && !one_in_(13))
7165         {
7166                 object_type *o_ptr = choose_warning_item();
7167
7168                 if (o_ptr) object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
7169 #ifdef JP
7170                 else strcpy(o_name, "ÂÎ"); /* Warning ability without item */
7171                 msg_format("%s¤¬¿Ì¤¨¤¿¡ª", o_name);
7172 #else
7173                 else strcpy(o_name, "body"); /* Warning ability without item */
7174                 msg_format("Your %s pulsates!", o_name);
7175 #endif
7176                 disturb(0, 1);
7177 #ifdef JP
7178                 return get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©");
7179 #else
7180                 return get_check("Really want to go ahead? ");
7181 #endif
7182         }
7183
7184         return TRUE;
7185 }
7186
7187
7188 static bool item_tester_hook_melee_ammo(object_type *o_ptr)
7189 {
7190         switch (o_ptr->tval)
7191         {
7192                 case TV_HAFTED:
7193                 case TV_POLEARM:
7194                 case TV_DIGGING:
7195                 case TV_BOLT:
7196                 case TV_ARROW:
7197                 case TV_SHOT:
7198                 {
7199                         return (TRUE);
7200                 }
7201                 case TV_SWORD:
7202                 {
7203                         if (o_ptr->sval != SV_DOKUBARI) return (TRUE);
7204                 }
7205         }
7206
7207         return (FALSE);
7208 }
7209
7210
7211 /*
7212  *  A structure for smithing
7213  */
7214 typedef struct {
7215         int add;       /* TR flag number or special essence id */
7216         cptr add_name; /* Name of this ability */
7217         int type;      /* Menu number */
7218         int essence;   /* Index for carrying essences */
7219         int value;     /* Needed value to add this ability */
7220 } essence_type;
7221
7222
7223 /*
7224  *  Smithing type data for Weapon smith
7225  */
7226 #ifdef JP
7227 static essence_type essence_info[] = 
7228 {
7229         {TR_STR, "ÏÓÎÏ", 4, TR_STR, 20},
7230         {TR_INT, "ÃÎǽ", 4, TR_INT, 20},
7231         {TR_WIS, "¸­¤µ", 4, TR_WIS, 20},
7232         {TR_DEX, "´ïÍѤµ", 4, TR_DEX, 20},
7233         {TR_CON, "Âѵ×ÎÏ", 4, TR_CON, 20},
7234         {TR_CHR, "Ì¥ÎÏ", 4, TR_CHR, 20},
7235         {TR_MAGIC_MASTERY, "ËâÎÏ»ÙÇÛ", 4, TR_MAGIC_MASTERY, 20},
7236         {TR_STEALTH, "±£Ì©", 4, TR_STEALTH, 40},
7237         {TR_SEARCH, "õº÷", 4, TR_SEARCH, 15},
7238         {TR_INFRA, "ÀÖ³°Àþ»ëÎÏ", 4, TR_INFRA, 15},
7239         {TR_TUNNEL, "ºÎ·¡", 4, TR_TUNNEL, 15},
7240         {TR_SPEED, "¥¹¥Ô¡¼¥É", 4, TR_SPEED, 12},
7241         {TR_BLOWS, "Äɲù¶·â", 1, TR_BLOWS, 20},
7242         {TR_CHAOTIC, "¥«¥ª¥¹¹¶·â", 1, TR_CHAOTIC, 15},
7243         {TR_VAMPIRIC, "µÛ·ì¹¶·â", 1, TR_VAMPIRIC, 60},
7244         {TR_IMPACT, "ÃÏ¿Ìȯư", 7, TR_IMPACT, 15},
7245         {TR_BRAND_POIS, "ÆÇ»¦", 1, TR_BRAND_POIS, 20},
7246         {TR_BRAND_ACID, "Íϲò", 1, TR_BRAND_ACID, 20},
7247         {TR_BRAND_ELEC, "ÅÅ·â", 1, TR_BRAND_ELEC, 20},
7248         {TR_BRAND_FIRE, "¾Æ´þ", 1, TR_BRAND_FIRE, 20},
7249         {TR_BRAND_COLD, "Åà·ë", 1, TR_BRAND_COLD, 20},
7250         {TR_SUST_STR, "ÏÓÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
7251         {TR_SUST_INT, "ÃÎǽ°Ý»ý", 3, TR_SUST_STR, 15},
7252         {TR_SUST_WIS, "¸­¤µ°Ý»ý", 3, TR_SUST_STR, 15},
7253         {TR_SUST_DEX, "´ïÍѤµ°Ý»ý", 3, TR_SUST_STR, 15},
7254         {TR_SUST_CON, "Âѵ×ÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
7255         {TR_SUST_CHR, "Ì¥ÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
7256         {TR_IM_ACID, "»ÀÌȱÖ", 2, TR_IM_ACID, 20},
7257         {TR_IM_ELEC, "ÅÅ·âÌȱÖ", 2, TR_IM_ACID, 20},
7258         {TR_IM_FIRE, "²Ð±êÌȱÖ", 2, TR_IM_ACID, 20},
7259         {TR_IM_COLD, "Î䵤ÌȱÖ", 2, TR_IM_ACID, 20},
7260         {TR_REFLECT, "È¿¼Í", 2, TR_REFLECT, 20},
7261         {TR_FREE_ACT, "ËãáãÃΤ餺", 3, TR_FREE_ACT, 20},
7262         {TR_HOLD_LIFE, "À¸Ì¿ÎÏ°Ý»ý", 3, TR_HOLD_LIFE, 20},
7263         {TR_RES_ACID, "ÂÑ»À", 2, TR_RES_ACID, 15},
7264         {TR_RES_ELEC, "ÂÑÅÅ·â", 2, TR_RES_ELEC, 15},
7265         {TR_RES_FIRE, "ÂѲбê", 2, TR_RES_FIRE, 15},
7266         {TR_RES_COLD, "ÂÑÎ䵤", 2, TR_RES_COLD, 15},
7267         {TR_RES_POIS, "ÂÑÆÇ", 2, TR_RES_POIS, 25},
7268         {TR_RES_FEAR, "ÂѶ²ÉÝ", 2, TR_RES_FEAR, 20},
7269         {TR_RES_LITE, "ÂÑÁ®¸÷", 2, TR_RES_LITE, 20},
7270         {TR_RES_DARK, "ÂѰŹõ", 2, TR_RES_DARK, 20},
7271         {TR_RES_BLIND, "ÂÑÌÕÌÜ", 2, TR_RES_BLIND, 20},
7272         {TR_RES_CONF, "ÂѺ®Íð", 2, TR_RES_CONF, 20},
7273         {TR_RES_SOUND, "Âѹ첻", 2, TR_RES_SOUND, 20},
7274         {TR_RES_SHARDS, "ÂÑÇËÊÒ", 2, TR_RES_SHARDS, 20},
7275         {TR_RES_NETHER, "ÂÑÃϹö", 2, TR_RES_NETHER, 20},
7276         {TR_RES_NEXUS, "ÂÑ°ø²Ìº®Íð", 2, TR_RES_NEXUS, 20},
7277         {TR_RES_CHAOS, "ÂÑ¥«¥ª¥¹", 2, TR_RES_CHAOS, 20},
7278         {TR_RES_DISEN, "ÂÑÎô²½", 2, TR_RES_DISEN, 20},
7279         {TR_SH_FIRE, "", 0, -2, 0},
7280         {TR_SH_ELEC, "", 0, -2, 0},
7281         {TR_SH_COLD, "", 0, -2, 0},
7282         {TR_NO_MAGIC, "È¿ËâË¡", 3, TR_NO_MAGIC, 15},
7283         {TR_WARNING, "·Ù¹ð", 3, TR_WARNING, 20},
7284         {TR_LEVITATION, "ÉâÍ·", 3, TR_LEVITATION, 20},
7285         {TR_LITE_1, "±Êµ×¸÷¸»", 3, TR_LITE_1, 15},
7286         {TR_LITE_2, "", 0, -2, 0},
7287         {TR_LITE_3, "", 0, -2, 0},
7288         {TR_SEE_INVIS, "²Ä»ëÆ©ÌÀ", 3, TR_SEE_INVIS, 20},
7289         {TR_TELEPATHY, "¥Æ¥ì¥Ñ¥·¡¼", 6, TR_TELEPATHY, 15},
7290         {TR_SLOW_DIGEST, "Ãپò½", 3, TR_SLOW_DIGEST, 15},
7291         {TR_REGEN, "µÞ®²óÉü", 3, TR_REGEN, 20},
7292         {TR_TELEPORT, "¥Æ¥ì¥Ý¡¼¥È", 3, TR_TELEPORT, 25},
7293
7294         {TR_SLAY_EVIL, "¼Ù°­ÇÜÂÇ", 5, TR_SLAY_EVIL, 100},
7295         {TR_KILL_EVIL, "¼Ù°­ÇÜÇÜÂÇ", 0, TR_SLAY_EVIL, 60},
7296         {TR_SLAY_ANIMAL, "ưʪÇÜÂÇ", 5, TR_SLAY_ANIMAL, 20},
7297         {TR_KILL_ANIMAL, "ưʪÇÜÇÜÂÇ", 5, TR_SLAY_ANIMAL, 60},
7298         {TR_SLAY_UNDEAD, "ÉÔ»àÇÜÂÇ", 5, TR_SLAY_UNDEAD, 20},
7299         {TR_KILL_UNDEAD, "ÉÔ»àÇÜÇÜÂÇ", 5, TR_SLAY_UNDEAD, 60},
7300         {TR_SLAY_DEMON, "°­ËâÇÜÂÇ", 5, TR_SLAY_DEMON, 20},
7301         {TR_KILL_DEMON, "°­ËâÇÜÇÜÂÇ", 5, TR_SLAY_DEMON, 60},
7302         {TR_SLAY_ORC, "¥ª¡¼¥¯ÇÜÂÇ", 5, TR_SLAY_ORC, 15},
7303         {TR_KILL_ORC, "¥ª¡¼¥¯ÇÜÇÜÂÇ", 5, TR_SLAY_ORC, 60},
7304         {TR_SLAY_TROLL, "¥È¥í¥ëÇÜÂÇ", 5, TR_SLAY_TROLL, 15},
7305         {TR_KILL_TROLL, "¥È¥í¥ëÇÜÇÜÂÇ", 5, TR_SLAY_TROLL, 60},
7306         {TR_SLAY_GIANT, "µð¿ÍÇÜÂÇ", 5, TR_SLAY_GIANT, 20},
7307         {TR_KILL_GIANT, "µð¿ÍÇÜÇÜÂÇ", 5, TR_SLAY_GIANT, 60},       
7308         {TR_SLAY_DRAGON, "εÇÜÂÇ", 5, TR_SLAY_DRAGON, 20},
7309         {TR_KILL_DRAGON, "εÇÜÇÜÂÇ", 5, TR_SLAY_DRAGON, 60},
7310         {TR_SLAY_HUMAN, "¿Í´ÖÇÜÂÇ", 5, TR_SLAY_HUMAN, 20},
7311         {TR_KILL_HUMAN, "¿Í´ÖÇÜÇÜÂÇ", 5, TR_SLAY_HUMAN, 60},
7312
7313         {TR_ESP_ANIMAL, "ưʪESP", 6, TR_SLAY_ANIMAL, 40},
7314         {TR_ESP_UNDEAD, "ÉÔ»àESP", 6, TR_SLAY_UNDEAD, 40}, 
7315         {TR_ESP_DEMON, "°­ËâESP", 6, TR_SLAY_DEMON, 40},       
7316         {TR_ESP_ORC, "¥ª¡¼¥¯ESP", 6, TR_SLAY_ORC, 40},     
7317         {TR_ESP_TROLL, "¥È¥í¥ëESP", 6, TR_SLAY_TROLL, 40},   
7318         {TR_ESP_GIANT, "µð¿ÍESP", 6, TR_SLAY_GIANT, 40},       
7319         {TR_ESP_DRAGON, "εESP", 6, TR_SLAY_DRAGON, 40},
7320         {TR_ESP_HUMAN, "¿Í´ÖESP", 6, TR_SLAY_HUMAN, 40},
7321
7322         {ESSENCE_ATTACK, "¹¶·â", 10, TR_ES_ATTACK, 30},
7323         {ESSENCE_AC, "Ëɸæ", 10, TR_ES_AC, 15},
7324         {ESSENCE_TMP_RES_ACID, "»ÀÂÑÀ­È¯Æ°", 7, TR_RES_ACID, 50},
7325         {ESSENCE_TMP_RES_ELEC, "ÅÅ·âÂÑÀ­È¯Æ°", 7, TR_RES_ELEC, 50},
7326         {ESSENCE_TMP_RES_FIRE, "²Ð±êÂÑÀ­È¯Æ°", 7, TR_RES_FIRE, 50},
7327         {ESSENCE_TMP_RES_COLD, "Î䵤ÂÑÀ­È¯Æ°", 7, TR_RES_COLD, 50},
7328         {ESSENCE_SH_FIRE, "²Ð±ê¥ª¡¼¥é", 7, -1, 50},
7329         {ESSENCE_SH_ELEC, "Åŷ⥪¡¼¥é", 7, -1, 50},
7330         {ESSENCE_SH_COLD, "Î䵤¥ª¡¼¥é", 7, -1, 50},
7331         {ESSENCE_RESISTANCE, "Á´ÂÑÀ­", 2, -1, 150},
7332         {ESSENCE_SUSTAIN, "ÁõÈ÷ÊÝ»ý", 10, -1, 10},
7333         {ESSENCE_SLAY_GLOVE, "»¦Ù¤¤Î¾®¼ê", 1, TR_ES_ATTACK, 200},
7334
7335         {-1, NULL, 0, -1, 0}
7336 };
7337 #else
7338 static essence_type essence_info[] = 
7339 {
7340         {TR_STR, "strength", 4, TR_STR, 20},
7341         {TR_INT, "intelligence", 4, TR_INT, 20},
7342         {TR_WIS, "wisdom", 4, TR_WIS, 20},
7343         {TR_DEX, "dexterity", 4, TR_DEX, 20},
7344         {TR_CON, "constitution", 4, TR_CON, 20},
7345         {TR_CHR, "charisma", 4, TR_CHR, 20},
7346         {TR_MAGIC_MASTERY, "magic mastery", 4, TR_MAGIC_MASTERY, 20},
7347         {TR_STEALTH, "stealth", 4, TR_STEALTH, 40},
7348         {TR_SEARCH, "serching", 4, TR_SEARCH, 15},
7349         {TR_INFRA, "infravision", 4, TR_INFRA, 15},
7350         {TR_TUNNEL, "digging", 4, TR_TUNNEL, 15},
7351         {TR_SPEED, "speed", 4, TR_SPEED, 12},
7352         {TR_BLOWS, "extra attack", 1, TR_BLOWS, 20},
7353         {TR_CHAOTIC, "chaos brand", 1, TR_CHAOTIC, 15},
7354         {TR_VAMPIRIC, "vampiric brand", 1, TR_VAMPIRIC, 60},
7355         {TR_IMPACT, "quake activation", 7, TR_IMPACT, 15},
7356         {TR_BRAND_POIS, "poison brand", 1, TR_BRAND_POIS, 20},
7357         {TR_BRAND_ACID, "acid brand", 1, TR_BRAND_ACID, 20},
7358         {TR_BRAND_ELEC, "electric brand", 1, TR_BRAND_ELEC, 20},
7359         {TR_BRAND_FIRE, "fire brand", 1, TR_BRAND_FIRE, 20},
7360         {TR_BRAND_COLD, "cold brand", 1, TR_BRAND_COLD, 20},
7361         {TR_SUST_STR, "sustain strength", 3, TR_SUST_STR, 15},
7362         {TR_SUST_INT, "sustain intelligence", 3, TR_SUST_STR, 15},
7363         {TR_SUST_WIS, "sustain wisdom", 3, TR_SUST_STR, 15},
7364         {TR_SUST_DEX, "sustain dexterity", 3, TR_SUST_STR, 15},
7365         {TR_SUST_CON, "sustain constitution", 3, TR_SUST_STR, 15},
7366         {TR_SUST_CHR, "sustain charisma", 3, TR_SUST_STR, 15},
7367         {TR_IM_ACID, "acid immunity", 2, TR_IM_ACID, 20},
7368         {TR_IM_ELEC, "electric immunity", 2, TR_IM_ACID, 20},
7369         {TR_IM_FIRE, "fire immunity", 2, TR_IM_ACID, 20},
7370         {TR_IM_COLD, "cold immunity", 2, TR_IM_ACID, 20},
7371         {TR_REFLECT, "reflection", 2, TR_REFLECT, 20},
7372         {TR_FREE_ACT, "free action", 3, TR_FREE_ACT, 20},
7373         {TR_HOLD_LIFE, "hold life", 3, TR_HOLD_LIFE, 20},
7374         {TR_RES_ACID, "resistance to acid", 2, TR_RES_ACID, 15},
7375         {TR_RES_ELEC, "resistance to electric", 2, TR_RES_ELEC, 15},
7376         {TR_RES_FIRE, "resistance to fire", 2, TR_RES_FIRE, 15},
7377         {TR_RES_COLD, "resistance to cold", 2, TR_RES_COLD, 15},
7378         {TR_RES_POIS, "resistance to poison", 2, TR_RES_POIS, 25},
7379         {TR_RES_FEAR, "resistance to fear", 2, TR_RES_FEAR, 20},
7380         {TR_RES_LITE, "resistance to light", 2, TR_RES_LITE, 20},
7381         {TR_RES_DARK, "resistance to dark", 2, TR_RES_DARK, 20},
7382         {TR_RES_BLIND, "resistance to blind", 2, TR_RES_BLIND, 20},
7383         {TR_RES_CONF, "resistance to confusion", 2, TR_RES_CONF, 20},
7384         {TR_RES_SOUND, "resistance to sound", 2, TR_RES_SOUND, 20},
7385         {TR_RES_SHARDS, "resistance to shard", 2, TR_RES_SHARDS, 20},
7386         {TR_RES_NETHER, "resistance to nether", 2, TR_RES_NETHER, 20},
7387         {TR_RES_NEXUS, "resistance to nexus", 2, TR_RES_NEXUS, 20},
7388         {TR_RES_CHAOS, "resistance to chaos", 2, TR_RES_CHAOS, 20},
7389         {TR_RES_DISEN, "resistance to disenchantment", 2, TR_RES_DISEN, 20},
7390         {TR_SH_FIRE, "", 0, -2, 0},
7391         {TR_SH_ELEC, "", 0, -2, 0},
7392         {TR_SH_COLD, "", 0, -2, 0},
7393         {TR_NO_MAGIC, "anti magic", 3, TR_NO_MAGIC, 15},
7394         {TR_WARNING, "warning", 3, TR_WARNING, 20},
7395         {TR_LEVITATION, "levitation", 3, TR_LEVITATION, 20},
7396         {TR_LITE_1, "permanent light", 3, TR_LITE_1, 15},
7397         {TR_LITE_2, "", 0, -2, 0},
7398         {TR_LITE_3, "", 0, -2, 0},
7399         {TR_SEE_INVIS, "see invisible", 3, TR_SEE_INVIS, 20},
7400         {TR_TELEPATHY, "telepathy", 6, TR_TELEPATHY, 15},
7401         {TR_SLOW_DIGEST, "slow digestion", 3, TR_SLOW_DIGEST, 15},
7402         {TR_REGEN, "regeneration", 3, TR_REGEN, 20},
7403         {TR_TELEPORT, "teleport", 3, TR_TELEPORT, 25},
7404
7405         {TR_SLAY_EVIL, "slay evil", 5, TR_SLAY_EVIL, 100},
7406         {TR_SLAY_ANIMAL, "slay animal", 5, TR_SLAY_ANIMAL, 20},
7407         {TR_KILL_ANIMAL, "kill animal", 5, TR_SLAY_ANIMAL, 60},
7408         {TR_KILL_EVIL, "kill evil", 0, TR_SLAY_EVIL, 60},
7409         {TR_SLAY_UNDEAD, "slay undead", 5, TR_SLAY_UNDEAD, 20},
7410         {TR_KILL_UNDEAD, "kill undead", 5, TR_SLAY_UNDEAD, 60},
7411         {TR_SLAY_DEMON, "slay demon", 5, TR_SLAY_DEMON, 20},
7412         {TR_KILL_DEMON, "kill demon", 5, TR_SLAY_DEMON, 60},
7413         {TR_SLAY_ORC, "slay orc", 5, TR_SLAY_ORC, 15},
7414         {TR_KILL_ORC, "kill orc", 5, TR_SLAY_ORC, 60},
7415         {TR_SLAY_TROLL, "slay troll", 5, TR_SLAY_TROLL, 15},
7416         {TR_KILL_TROLL, "kill troll", 5, TR_SLAY_TROLL, 60},
7417         {TR_SLAY_GIANT, "slay giant", 5, TR_SLAY_GIANT, 20},
7418         {TR_KILL_GIANT, "kill giant", 5, TR_SLAY_GIANT, 60},       
7419         {TR_SLAY_DRAGON, "slay dragon", 5, TR_SLAY_DRAGON, 20},
7420         {TR_KILL_DRAGON, "kill dragon", 5, TR_SLAY_DRAGON, 60},
7421         {TR_SLAY_HUMAN, "slay human", 5, TR_SLAY_HUMAN, 20},
7422         {TR_KILL_HUMAN, "kill human", 5, TR_SLAY_HUMAN, 60},
7423
7424         {TR_ESP_ANIMAL, "sense animal", 6, TR_SLAY_ANIMAL, 40},
7425         {TR_ESP_UNDEAD, "sense undead", 6, TR_SLAY_UNDEAD, 40}, 
7426         {TR_ESP_DEMON, "sense demon", 6, TR_SLAY_DEMON, 40},       
7427         {TR_ESP_ORC, "sense orc", 6, TR_SLAY_ORC, 40},     
7428         {TR_ESP_TROLL, "sense troll", 6, TR_SLAY_TROLL, 40},   
7429         {TR_ESP_GIANT, "sense giant", 6, TR_SLAY_GIANT, 40},       
7430         {TR_ESP_DRAGON, "sense dragon", 6, TR_SLAY_DRAGON, 40},
7431         {TR_ESP_HUMAN, "sense human", 6, TR_SLAY_HUMAN, 40},
7432
7433         {ESSENCE_ATTACK, "weapon enchant", 10, TR_ES_ATTACK, 30},
7434         {ESSENCE_AC, "armor enchant", 10, TR_ES_AC, 15},
7435         {ESSENCE_TMP_RES_ACID, "resist acid activation", 7, TR_RES_ACID, 50},
7436         {ESSENCE_TMP_RES_ELEC, "resist electricity activation", 7, TR_RES_ELEC, 50},
7437         {ESSENCE_TMP_RES_FIRE, "resist fire activation", 7, TR_RES_FIRE, 50},
7438         {ESSENCE_TMP_RES_COLD, "resist cold activation", 7, TR_RES_COLD, 50},
7439         {ESSENCE_SH_FIRE, "fiery sheath", 7, -1, 50},
7440         {ESSENCE_SH_ELEC, "electric sheath", 7, -1, 50},
7441         {ESSENCE_SH_COLD, "sheath of coldness", 7, -1, 50},
7442         {ESSENCE_RESISTANCE, "resistance", 2, -1, 150},
7443         {ESSENCE_SUSTAIN, "elements proof", 10, -1, 10},
7444         {ESSENCE_SLAY_GLOVE, "gauntlets of slaying", 1, TR_ES_ATTACK, 200},
7445
7446         {-1, NULL, 0, -1, 0}
7447 };
7448 #endif
7449
7450
7451 /*
7452  *  Essense names for Weapon smith
7453  */
7454 #ifdef JP
7455 cptr essence_name[] = 
7456 {
7457         "ÏÓÎÏ",
7458         "ÃÎǽ",
7459         "¸­¤µ",
7460         "´ïÍѤµ",
7461         "Âѵ×ÎÏ",
7462         "Ì¥ÎÏ",
7463         "ËâÎÏ»ÙÇÛ",
7464         "",
7465         "±£Ì©",
7466         "õº÷",
7467         "ÀÖ³°Àþ»ëÎÏ",
7468         "ºÎ·¡",
7469         "¥¹¥Ô¡¼¥É",
7470         "Äɲù¶·â",
7471         "¥«¥ª¥¹¹¶·â",
7472         "µÛ·ì¹¶·â",
7473         "ưʪÇÜÂÇ",
7474         "¼Ù°­ÇÜÂÇ",
7475         "ÉÔ»àÇÜÂÇ",
7476         "°­ËâÇÜÂÇ",
7477         "¥ª¡¼¥¯ÇÜÂÇ",
7478         "¥È¥í¥ëÇÜÂÇ",
7479         "µð¿ÍÇÜÂÇ",
7480         "εÇÜÂÇ",
7481         "",
7482         "",
7483         "ÃÏ¿Ì",
7484         "ÆÇ»¦",
7485         "Íϲò",
7486         "ÅÅ·â",
7487         "¾Æ´þ",
7488         "Åà·ë",
7489         "ǽÎÏ°Ý»ý",
7490         "",
7491         "",
7492         "",
7493         "",
7494         "",
7495         "",
7496         "",
7497         "ÌȱÖ",
7498         "",
7499         "",
7500         "",
7501         "",
7502         "È¿¼Í",
7503         "ËãáãÃΤ餺",
7504         "À¸Ì¿ÎÏ°Ý»ý",
7505         "ÂÑ»À",
7506         "ÂÑÅÅ·â",
7507         "ÂѲбê",
7508         "ÂÑÎ䵤",
7509         "ÂÑÆÇ",
7510         "ÂѶ²ÉÝ",
7511         "ÂÑÁ®¸÷",
7512         "ÂѰŹõ",
7513         "ÂÑÌÕÌÜ",
7514         "ÂѺ®Íð",
7515         "Âѹ첻",
7516         "ÂÑÇËÊÒ",
7517         "ÂÑÃϹö",
7518         "ÂÑ°ø²Ìº®Íð",
7519         "ÂÑ¥«¥ª¥¹",
7520         "ÂÑÎô²½",
7521         "",
7522         "",
7523         "¿Í´ÖÇÜÂÇ",
7524         "",
7525         "",
7526         "È¿ËâË¡",
7527         "",
7528         "",
7529         "·Ù¹ð",
7530         "",
7531         "",
7532         "",
7533         "ÉâÍ·",
7534         "±Êµ×¸÷¸»",
7535         "²Ä»ëÆ©ÌÀ",
7536         "¥Æ¥ì¥Ñ¥·¡¼",
7537         "Ãپò½",
7538         "µÞ®²óÉü",
7539         "",
7540         "",
7541         "",
7542         "",
7543         "",
7544         "",
7545         "",
7546         "",
7547         "¥Æ¥ì¥Ý¡¼¥È",
7548         "",
7549         "",
7550         "¹¶·â",
7551         "Ëɸæ",
7552
7553         NULL
7554 };
7555
7556 #else
7557
7558 static cptr essence_name[] = 
7559 {
7560         "strength",
7561         "intelligen.",
7562         "wisdom",
7563         "dexterity",
7564         "constitut.",
7565         "charisma",
7566         "magic mast.",
7567         "",
7568         "stealth",
7569         "serching",
7570         "infravision",
7571         "digging",
7572         "speed",
7573         "extra atk",
7574         "chaos brand",
7575         "vampiric",
7576         "slay animal",
7577         "slay evil",
7578         "slay undead",
7579         "slay demon",
7580         "slay orc",
7581         "slay troll",
7582         "slay giant",
7583         "slay dragon",
7584         "",
7585         "",
7586         "quake",
7587         "pois. brand",
7588         "acid brand",
7589         "elec. brand",
7590         "fire brand",
7591         "cold brand",
7592         "sustain",
7593         "",
7594         "",
7595         "",
7596         "",
7597         "",
7598         "",
7599         "",
7600         "immunity",
7601         "",
7602         "",
7603         "",
7604         "",
7605         "reflection",
7606         "free action",
7607         "hold life",
7608         "res. acid",
7609         "res. elec.",
7610         "res. fire",
7611         "res. cold",
7612         "res. poison",
7613         "res. fear",
7614         "res. light",
7615         "res. dark",
7616         "res. blind",
7617         "res.confuse",
7618         "res. sound",
7619         "res. shard",
7620         "res. nether",
7621         "res. nexus",
7622         "res. chaos",
7623         "res. disen.",
7624         "",
7625         "",
7626         "slay human",
7627         "",
7628         "",
7629         "anti magic",
7630         "",
7631         "",
7632         "warning",
7633         "",
7634         "",
7635         "",
7636         "levitation",
7637         "perm. light",
7638         "see invis.",
7639         "telepathy",
7640         "slow dige.",
7641         "regen.",
7642         "",
7643         "",
7644         "",
7645         "",
7646         "",
7647         "",
7648         "",
7649         "",
7650         "teleport",
7651         "",
7652         "",
7653         "weapon enc.",
7654         "armor enc.",
7655
7656         NULL
7657 };
7658 #endif
7659
7660
7661 static void display_essence(void)
7662 {
7663         int i, num = 0;
7664
7665         screen_save();
7666         for (i = 1; i < 22; i++)
7667         {
7668                 prt("",i,0);
7669         }
7670 #ifdef JP
7671         prt("¥¨¥Ã¥»¥ó¥¹   ¸Ä¿ô     ¥¨¥Ã¥»¥ó¥¹   ¸Ä¿ô     ¥¨¥Ã¥»¥ó¥¹   ¸Ä¿ô", 1, 8);
7672 #else
7673         prt("Essence      Num      Essence      Num      Essence      Num ", 1, 8);
7674 #endif
7675         for (i = 0; essence_name[i]; i++)
7676         {
7677                 if (!essence_name[i][0]) continue;
7678                 prt(format("%-11s %5d", essence_name[i], p_ptr->magic_num1[i]), 2+num%21, 8+num/21*22);
7679                 num++;
7680         }
7681 #ifdef JP
7682         prt("¸½ºß½ê»ý¤·¤Æ¤¤¤ë¥¨¥Ã¥»¥ó¥¹", 0, 0);
7683 #else
7684         prt("List of all essences you have.", 0, 0);
7685 #endif
7686         (void)inkey();
7687         screen_load();
7688         return;
7689 }
7690
7691 static void drain_essence(void)
7692 {
7693         int drain_value[sizeof(p_ptr->magic_num1) / sizeof(s32b)];
7694         int i, item;
7695         int dec = 4;
7696         bool observe = FALSE;
7697         int old_ds, old_dd, old_to_h, old_to_d, old_ac, old_to_a, old_pval, old_name2, old_timeout;
7698         u32b old_flgs[TR_FLAG_SIZE], new_flgs[TR_FLAG_SIZE];
7699         object_type *o_ptr;
7700         cptr            q, s;
7701         byte iy, ix, marked, number;
7702         s16b next_o_idx, weight;
7703
7704         for (i = 0; i < sizeof(drain_value) / sizeof(int); i++)
7705                 drain_value[i] = 0;
7706
7707         item_tester_hook = object_is_weapon_armour_ammo;
7708         item_tester_no_ryoute = TRUE;
7709
7710         /* Get an item */
7711 #ifdef JP
7712         q = "¤É¤Î¥¢¥¤¥Æ¥à¤«¤éÃê½Ð¤·¤Þ¤¹¤«¡©";
7713         s = "Ãê½Ð¤Ç¤­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
7714 #else
7715         q = "Extract from which item? ";
7716         s = "You have nothing you can extract from.";
7717 #endif
7718
7719         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
7720
7721         /* Get the item (in the pack) */
7722         if (item >= 0)
7723         {
7724                 o_ptr = &inventory[item];
7725         }
7726
7727         /* Get the item (on the floor) */
7728         else
7729         {
7730                 o_ptr = &o_list[0 - item];
7731         }
7732
7733         if (object_is_known(o_ptr) && !object_is_nameless(o_ptr))
7734         {
7735                 char o_name[MAX_NLEN];
7736                 object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
7737 #ifdef JP
7738                 if (!get_check(format("ËÜÅö¤Ë%s¤«¤éÃê½Ð¤·¤Æ¤è¤í¤·¤¤¤Ç¤¹¤«¡©", o_name))) return;
7739 #else
7740                 if (!get_check(format("Really extract from %s? ", o_name))) return;
7741 #endif
7742         }
7743
7744         energy_use = 100;
7745
7746         object_flags(o_ptr, old_flgs);
7747         if (have_flag(old_flgs, TR_KILL_DRAGON)) add_flag(old_flgs, TR_SLAY_DRAGON);
7748         if (have_flag(old_flgs, TR_KILL_ANIMAL)) add_flag(old_flgs, TR_SLAY_ANIMAL);
7749         if (have_flag(old_flgs, TR_KILL_EVIL)) add_flag(old_flgs, TR_SLAY_EVIL);
7750         if (have_flag(old_flgs, TR_KILL_UNDEAD)) add_flag(old_flgs, TR_SLAY_UNDEAD);
7751         if (have_flag(old_flgs, TR_KILL_DEMON)) add_flag(old_flgs, TR_SLAY_DEMON);
7752         if (have_flag(old_flgs, TR_KILL_ORC)) add_flag(old_flgs, TR_SLAY_ORC);
7753         if (have_flag(old_flgs, TR_KILL_TROLL)) add_flag(old_flgs, TR_SLAY_TROLL);
7754         if (have_flag(old_flgs, TR_KILL_GIANT)) add_flag(old_flgs, TR_SLAY_GIANT);
7755         if (have_flag(old_flgs, TR_KILL_HUMAN)) add_flag(old_flgs, TR_SLAY_HUMAN);
7756
7757         old_to_a = o_ptr->to_a;
7758         old_ac = o_ptr->ac;
7759         old_to_h = o_ptr->to_h;
7760         old_to_d = o_ptr->to_d;
7761         old_ds = o_ptr->ds;
7762         old_dd = o_ptr->dd;
7763         old_pval = o_ptr->pval;
7764         old_name2 = o_ptr->name2;
7765         old_timeout = o_ptr->timeout;
7766         if (o_ptr->curse_flags & (TRC_CURSED | TRC_HEAVY_CURSE | TRC_PERMA_CURSE)) dec--;
7767         if (have_flag(old_flgs, TR_ADD_L_CURSE)) dec--;
7768         if (have_flag(old_flgs, TR_ADD_H_CURSE)) dec--;
7769         if (have_flag(old_flgs, TR_AGGRAVATE)) dec--;
7770         if (have_flag(old_flgs, TR_NO_TELE)) dec--;
7771         if (have_flag(old_flgs, TR_DRAIN_EXP)) dec--;
7772         if (have_flag(old_flgs, TR_DRAIN_HP)) dec--;
7773         if (have_flag(old_flgs, TR_DRAIN_MANA)) dec--;
7774         if (have_flag(old_flgs, TR_CALL_ANIMAL)) dec--;
7775         if (have_flag(old_flgs, TR_CALL_DEMON)) dec--;
7776         if (have_flag(old_flgs, TR_CALL_DRAGON)) dec--;
7777         if (have_flag(old_flgs, TR_CALL_UNDEAD)) dec--;
7778         if (have_flag(old_flgs, TR_COWARDICE)) dec--;
7779         if (have_flag(old_flgs, TR_LOW_MELEE)) dec--;
7780         if (have_flag(old_flgs, TR_LOW_AC)) dec--;
7781         if (have_flag(old_flgs, TR_LOW_MAGIC)) dec--;
7782         if (have_flag(old_flgs, TR_FAST_DIGEST)) dec--;
7783         if (have_flag(old_flgs, TR_SLOW_REGEN)) dec--;
7784         if (have_flag(old_flgs, TR_TY_CURSE)) dec--;
7785         
7786         iy = o_ptr->iy;
7787         ix = o_ptr->ix;
7788         next_o_idx = o_ptr->next_o_idx;
7789         marked = o_ptr->marked;
7790         weight = o_ptr->weight;
7791         number = o_ptr->number;
7792
7793         object_prep(o_ptr, o_ptr->k_idx);
7794
7795         o_ptr->iy=iy;
7796         o_ptr->ix=ix;
7797         o_ptr->next_o_idx=next_o_idx;
7798         o_ptr->marked=marked;
7799         o_ptr->number = number;
7800         if (o_ptr->tval == TV_DRAG_ARMOR) o_ptr->timeout = old_timeout;
7801         if (item >= 0) p_ptr->total_weight += (o_ptr->weight*o_ptr->number - weight*number);
7802         o_ptr->ident |= (IDENT_MENTAL);
7803         object_aware(o_ptr);
7804         object_known(o_ptr);
7805
7806         object_flags(o_ptr, new_flgs);
7807
7808         for (i = 0; essence_info[i].add_name; i++)
7809         {
7810                 essence_type *es_ptr = &essence_info[i];
7811                 int pval = 0;
7812
7813                 if (es_ptr->add < TR_FLAG_MAX && is_pval_flag(es_ptr->add) && old_pval)
7814                         pval = (have_flag(new_flgs, es_ptr->add)) ? old_pval - o_ptr->pval : old_pval;
7815
7816                 if (es_ptr->add < TR_FLAG_MAX &&
7817                     (!have_flag(new_flgs, es_ptr->add) || pval) &&
7818                     have_flag(old_flgs, es_ptr->add))
7819                 {
7820                         if (pval)
7821                         {
7822                                 drain_value[es_ptr->essence] += 10 * pval;
7823                         }
7824                         else if (es_ptr->essence != -2)
7825                         {
7826                                 drain_value[es_ptr->essence] += 10;
7827                         }
7828                         else if (es_ptr->add == TR_SH_FIRE)
7829                         {
7830                                 drain_value[TR_BRAND_FIRE] += 10;
7831                                 drain_value[TR_RES_FIRE] += 10;
7832                         }
7833                         else if (es_ptr->add == TR_SH_ELEC)
7834                         {
7835                                 drain_value[TR_BRAND_ELEC] += 10;
7836                                 drain_value[TR_RES_ELEC] += 10;
7837                         }
7838                         else if (es_ptr->add == TR_SH_COLD)
7839                         {
7840                                 drain_value[TR_BRAND_COLD] += 10;
7841                                 drain_value[TR_RES_COLD] += 10;
7842                         }
7843                         else if (es_ptr->add == TR_LITE_2)
7844                         {
7845                                 drain_value[TR_LITE_1] += 20;
7846                         }
7847                         else if (es_ptr->add == TR_LITE_3)
7848                         {
7849                                 drain_value[TR_LITE_1] += 30;
7850                         }
7851                 }
7852         }
7853
7854         if ((have_flag(old_flgs, TR_FORCE_WEAPON)) && !(have_flag(new_flgs, TR_FORCE_WEAPON)))
7855         {
7856                 drain_value[TR_INT] += 5;
7857                 drain_value[TR_WIS] += 5;
7858         }
7859         if ((have_flag(old_flgs, TR_VORPAL)) && !(have_flag(new_flgs, TR_VORPAL)))
7860         {
7861                 drain_value[TR_BRAND_POIS] += 5;
7862                 drain_value[TR_BRAND_ACID] += 5;
7863                 drain_value[TR_BRAND_ELEC] += 5;
7864                 drain_value[TR_BRAND_FIRE] += 5;
7865                 drain_value[TR_BRAND_COLD] += 5;
7866         }
7867         if ((have_flag(old_flgs, TR_DEC_MANA)) && !(have_flag(new_flgs, TR_DEC_MANA)))
7868         {
7869                 drain_value[TR_INT] += 10;
7870         }
7871         if ((have_flag(old_flgs, TR_XTRA_MIGHT)) && !(have_flag(new_flgs, TR_XTRA_MIGHT)))
7872         {
7873                 drain_value[TR_STR] += 10;
7874         }
7875         if ((have_flag(old_flgs, TR_XTRA_SHOTS)) && !(have_flag(new_flgs, TR_XTRA_SHOTS)))
7876         {
7877                 drain_value[TR_DEX] += 10;
7878         }
7879         if (old_name2 == EGO_2WEAPON)
7880         {
7881                 drain_value[TR_DEX] += 20;
7882         }
7883         if (object_is_weapon_ammo(o_ptr))
7884         {
7885                 if (old_ds > o_ptr->ds) drain_value[TR_ES_ATTACK] += (old_ds-o_ptr->ds)*10;
7886
7887                 if (old_dd > o_ptr->dd) drain_value[TR_ES_ATTACK] += (old_dd-o_ptr->dd)*10;
7888         }
7889         if (old_to_h > o_ptr->to_h) drain_value[TR_ES_ATTACK] += (old_to_h-o_ptr->to_h)*10;
7890         if (old_to_d > o_ptr->to_d) drain_value[TR_ES_ATTACK] += (old_to_d-o_ptr->to_d)*10;
7891         if (old_ac > o_ptr->ac) drain_value[TR_ES_AC] += (old_ac-o_ptr->ac)*10;
7892         if (old_to_a > o_ptr->to_a) drain_value[TR_ES_AC] += (old_to_a-o_ptr->to_a)*10;
7893
7894         for (i = 0; i < sizeof(drain_value) / sizeof(int); i++)
7895         {
7896                 drain_value[i] *= number;
7897                 drain_value[i] = drain_value[i] * dec / 4;
7898                 drain_value[i] = MAX(drain_value[i], 0);
7899                 if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_BOLT)) drain_value[i] /= 10;
7900                 if (drain_value[i])
7901                 {
7902                         observe = TRUE;
7903                 }
7904         }
7905         if (!observe)
7906         {
7907                 msg_print(_("¥¨¥Ã¥»¥ó¥¹¤ÏÃê½Ð¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", "You were not able to extract any essence."));
7908         }
7909         else
7910         {
7911                 msg_print(_("Ãê½Ð¤·¤¿¥¨¥Ã¥»¥ó¥¹:", "Extracted essences:"));
7912
7913                 for (i = 0; essence_name[i]; i++)
7914                 {
7915                         if (!essence_name[i][0]) continue;
7916                         if (!drain_value[i]) continue;
7917
7918                         p_ptr->magic_num1[i] += drain_value[i];
7919                         p_ptr->magic_num1[i] = MIN(20000, p_ptr->magic_num1[i]);
7920                         msg_print(NULL);
7921                         msg_format("%s...%d%s", essence_name[i], drain_value[i], _("¡£", ". "));
7922                 }
7923         }
7924
7925         /* Apply autodestroy/inscription to the drained item */
7926         autopick_alter_item(item, TRUE);
7927
7928         /* Combine the pack */
7929         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
7930
7931         /* Window stuff */
7932         p_ptr->window |= (PW_INVEN);
7933 }
7934
7935
7936
7937 static int choose_essence(void)
7938 {
7939         int mode = 0;
7940         char choice;
7941         int menu_line = (use_menu ? 1 : 0);
7942
7943 #ifdef JP
7944         cptr menu_name[] = {
7945                 "Éð´ï°À­", 
7946                 "ÂÑÀ­",
7947                 "ǽÎÏ",
7948                 "¿ôÃÍ",
7949                 "¥¹¥ì¥¤",
7950                 "ESP",
7951                 "¤½¤Î¾"
7952         };
7953 #else
7954         cptr menu_name[] = {
7955                 "Brand weapon",
7956                 "Resistance",
7957                 "Ability",
7958                 "Magic number", 
7959                 "Slay",
7960                 "ESP",
7961                 "Others"
7962         };
7963 #endif
7964         const int mode_max = 7;
7965
7966 #ifdef ALLOW_REPEAT
7967         if (repeat_pull(&mode) && 1 <= mode && mode <= mode_max)
7968                 return mode;
7969         mode = 0;
7970 #endif /* ALLOW_REPEAT */
7971
7972         if (use_menu)
7973         {
7974                 screen_save();
7975
7976                 while(!mode)
7977                 {
7978                         int i;
7979                         for (i = 0; i < mode_max; i++)
7980 #ifdef JP
7981                                 prt(format(" %s %s", (menu_line == 1+i) ? "¡Õ" : "  ", menu_name[i]), 2 + i, 14);
7982                         prt("¤É¤Î¼ïÎà¤Î¥¨¥Ã¥»¥ó¥¹Éղäò¹Ô¤¤¤Þ¤¹¤«¡©", 0, 0);
7983 #else
7984                                 prt(format(" %s %s", (menu_line == 1+i) ? "> " : "  ", menu_name[i]), 2 + i, 14);
7985                         prt("Choose from menu.", 0, 0);
7986 #endif
7987
7988                         choice = inkey();
7989                         switch(choice)
7990                         {
7991                         case ESCAPE:
7992                         case 'z':
7993                         case 'Z':
7994                                 screen_load();
7995                                 return 0;
7996                         case '2':
7997                         case 'j':
7998                         case 'J':
7999                                 menu_line++;
8000                                 break;
8001                         case '8':
8002                         case 'k':
8003                         case 'K':
8004                                 menu_line += mode_max - 1;
8005                                 break;
8006                         case '\r':
8007                         case '\n':
8008                         case 'x':
8009                         case 'X':
8010                                 mode = menu_line;
8011                                 break;
8012                         }
8013                         if (menu_line > mode_max) menu_line -= mode_max;
8014                 }
8015                 screen_load();
8016         }
8017         else
8018         {
8019                 screen_save();
8020                 while (!mode)
8021                 {
8022                         int i;
8023
8024                         for (i = 0; i < mode_max; i++)
8025                                 prt(format("  %c) %s", 'a' + i, menu_name[i]), 2 + i, 14);
8026
8027 #ifdef JP
8028                         if (!get_com("²¿¤òÉղä·¤Þ¤¹¤«:", &choice, TRUE))
8029 #else
8030                         if (!get_com("Command :", &choice, TRUE))
8031 #endif
8032                         {
8033                                 screen_load();
8034                                 return 0;
8035                         }
8036
8037                         if (isupper(choice)) choice = tolower(choice);
8038
8039                         if ('a' <= choice && choice <= 'a' + (char)mode_max - 1)
8040                                 mode = (int)choice - 'a' + 1;
8041                 }
8042                 screen_load();
8043         }
8044
8045 #ifdef ALLOW_REPEAT
8046         repeat_push(mode);
8047 #endif /* ALLOW_REPEAT */
8048         return mode;
8049 }
8050
8051 static void add_essence(int mode)
8052 {
8053         int item, max_num = 0;
8054         int i;
8055         bool flag,redraw;
8056         char choice;
8057         cptr            q, s;
8058         object_type *o_ptr;
8059         int ask = TRUE;
8060         char out_val[160];
8061         int num[22];
8062         char o_name[MAX_NLEN];
8063         int use_essence;
8064         essence_type *es_ptr;
8065
8066         int menu_line = (use_menu ? 1 : 0);
8067
8068         for (i = 0; essence_info[i].add_name; i++)
8069         {
8070                 es_ptr = &essence_info[i];
8071
8072                 if (es_ptr->type != mode) continue;
8073                 num[max_num++] = i;
8074         }
8075
8076 #ifdef ALLOW_REPEAT
8077         if (!repeat_pull(&i) || i<0 || i>=max_num)
8078         {
8079 #endif /* ALLOW_REPEAT */
8080
8081
8082         /* Nothing chosen yet */
8083         flag = FALSE;
8084
8085         /* No redraw yet */
8086         redraw = FALSE;
8087
8088         /* Build a prompt */
8089 #ifdef JP
8090         (void) strnfmt(out_val, 78, "('*'¤Ç°ìÍ÷, ESC¤ÇÃæÃÇ) ¤É¤ÎǽÎϤòÉղä·¤Þ¤¹¤«¡©");
8091 #else
8092         (void)strnfmt(out_val, 78, "(*=List, ESC=exit) Add which ability? ");
8093 #endif
8094         if (use_menu) screen_save();
8095
8096         /* Get a spell from the user */
8097
8098         choice = (always_show_list || use_menu) ? ESCAPE:1;
8099         while (!flag)
8100         {
8101                 bool able[22];
8102                 if( choice==ESCAPE ) choice = ' '; 
8103                 else if( !get_com(out_val, &choice, FALSE) )break; 
8104
8105                 if (use_menu && choice != ' ')
8106                 {
8107                         switch(choice)
8108                         {
8109                                 case '0':
8110                                 {
8111                                         screen_load();
8112                                         return;
8113                                 }
8114
8115                                 case '8':
8116                                 case 'k':
8117                                 case 'K':
8118                                 {
8119                                         menu_line += (max_num-1);
8120                                         break;
8121                                 }
8122
8123                                 case '2':
8124                                 case 'j':
8125                                 case 'J':
8126                                 {
8127                                         menu_line++;
8128                                         break;
8129                                 }
8130
8131                                 case '4':
8132                                 case 'h':
8133                                 case 'H':
8134                                 {
8135                                         menu_line = 1;
8136                                         break;
8137                                 }
8138                                 case '6':
8139                                 case 'l':
8140                                 case 'L':
8141                                 {
8142                                         menu_line = max_num;
8143                                         break;
8144                                 }
8145
8146                                 case 'x':
8147                                 case 'X':
8148                                 case '\r':
8149                                 case '\n':
8150                                 {
8151                                         i = menu_line - 1;
8152                                         ask = FALSE;
8153                                         break;
8154                                 }
8155                         }
8156                         if (menu_line > max_num) menu_line -= max_num;
8157                 }
8158                 /* Request redraw */
8159                 if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && ask))
8160                 {
8161                         /* Show the list */
8162                         if (!redraw || use_menu)
8163                         {
8164                                 byte y, x = 10;
8165                                 int ctr;
8166                                 char dummy[80], dummy2[80];
8167                                 byte col;
8168
8169                                 strcpy(dummy, "");
8170
8171                                 /* Show list */
8172                                 redraw = TRUE;
8173
8174                                 /* Save the screen */
8175                                 if (!use_menu) screen_save();
8176
8177                                 for (y = 1; y < 24; y++)
8178                                         prt("", y, x);
8179
8180                                 /* Print header(s) */
8181 #ifdef JP
8182                                 prt(format("   %-43s %6s/%s", "ǽÎÏ(ɬÍ×¥¨¥Ã¥»¥ó¥¹)", "ɬÍ׿ô", "½ê»ý¿ô"), 1, x);
8183
8184 #else
8185                                 prt(format("   %-43s %6s/%s", "Ability (needed essence)", "Needs", "Possess"), 1, x);
8186 #endif
8187                                 /* Print list */
8188                                 for (ctr = 0; ctr < max_num; ctr++)
8189                                 {
8190                                         es_ptr = &essence_info[num[ctr]];
8191
8192                                         if (use_menu)
8193                                         {
8194                                                 if (ctr == (menu_line-1))
8195 #ifdef JP
8196                                                         strcpy(dummy, "¡Õ ");
8197 #else
8198                                                         strcpy(dummy, ">  ");
8199 #endif
8200                                                 else strcpy(dummy, "   ");
8201                                                 
8202                                         }
8203                                         /* letter/number for power selection */
8204                                         else
8205                                         {
8206                                                 sprintf(dummy, "%c) ",I2A(ctr));
8207                                         }
8208
8209                                         strcat(dummy, es_ptr->add_name);
8210
8211                                         col = TERM_WHITE;
8212                                         able[ctr] = TRUE;
8213
8214                                         if (es_ptr->essence != -1)
8215                                         {
8216                                                 strcat(dummy, format("(%s)", essence_name[es_ptr->essence]));
8217                                                 if (p_ptr->magic_num1[es_ptr->essence] < es_ptr->value) able[ctr] = FALSE;
8218                                         }
8219                                         else
8220                                         {
8221                                                 switch(es_ptr->add)
8222                                                 {
8223                                                 case ESSENCE_SH_FIRE:
8224 #ifdef JP
8225                                                         strcat(dummy, "(¾Æ´þ+ÂѲбê)");
8226 #else
8227                                                         strcat(dummy, "(brand fire + res.fire)");
8228 #endif
8229                                                         if (p_ptr->magic_num1[TR_BRAND_FIRE] < es_ptr->value) able[ctr] = FALSE;
8230                                                         if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
8231                                                         break;
8232                                                 case ESSENCE_SH_ELEC:
8233 #ifdef JP
8234                                                         strcat(dummy, "(ÅÅ·â+ÂÑÅÅ·â)");
8235 #else
8236                                                         strcat(dummy, "(brand elec. + res. elec.)");
8237 #endif
8238                                                         if (p_ptr->magic_num1[TR_BRAND_ELEC] < es_ptr->value) able[ctr] = FALSE;
8239                                                         if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
8240                                                         break;
8241                                                 case ESSENCE_SH_COLD:
8242 #ifdef JP
8243                                                         strcat(dummy, "(Åà·ë+ÂÑÎ䵤)");
8244 #else
8245                                                         strcat(dummy, "(brand cold + res. cold)");
8246 #endif
8247                                                         if (p_ptr->magic_num1[TR_BRAND_COLD] < es_ptr->value) able[ctr] = FALSE;
8248                                                         if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
8249                                                         break;
8250                                                 case ESSENCE_RESISTANCE:
8251 #ifdef JP
8252                                                         strcat(dummy, "(ÂѲбê+ÂÑÎ䵤+ÂÑÅÅ·â+ÂÑ»À)");
8253 #else
8254                                                         strcat(dummy, "(r.fire+r.cold+r.elec+r.acid)");
8255 #endif
8256                                                         if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
8257                                                         if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
8258                                                         if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
8259                                                         if (p_ptr->magic_num1[TR_RES_ACID] < es_ptr->value) able[ctr] = FALSE;
8260                                                         break;
8261                                                 case ESSENCE_SUSTAIN:
8262 #ifdef JP
8263                                                         strcat(dummy, "(ÂѲбê+ÂÑÎ䵤+ÂÑÅÅ·â+ÂÑ»À)");
8264 #else
8265                                                         strcat(dummy, "(r.fire+r.cold+r.elec+r.acid)");
8266 #endif
8267                                                         if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
8268                                                         if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
8269                                                         if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
8270                                                         if (p_ptr->magic_num1[TR_RES_ACID] < es_ptr->value) able[ctr] = FALSE;
8271                                                         break;
8272                                                 }
8273                                         }
8274
8275                                         if (!able[ctr]) col = TERM_RED;
8276
8277                                         if (es_ptr->essence != -1)
8278                                         {
8279                                                 sprintf(dummy2, "%-49s %3d/%d", dummy, es_ptr->value, (int)p_ptr->magic_num1[es_ptr->essence]);
8280                                         }
8281                                         else
8282                                         {
8283                                                 sprintf(dummy2, "%-49s %3d/(\?\?)", dummy, es_ptr->value);
8284                                         }
8285
8286                                         c_prt(col, dummy2, ctr+2, x);
8287                                 }
8288                         }
8289
8290                         /* Hide the list */
8291                         else
8292                         {
8293                                 /* Hide list */
8294                                 redraw = FALSE;
8295
8296                                 /* Restore the screen */
8297                                 screen_load();
8298                         }
8299
8300                         /* Redo asking */
8301                         continue;
8302                 }
8303
8304                 if (!use_menu)
8305                 {
8306                         /* Note verify */
8307                         ask = (isupper(choice));
8308
8309                         /* Lowercase */
8310                         if (ask) choice = tolower(choice);
8311
8312                         /* Extract request */
8313                         i = (islower(choice) ? A2I(choice) : -1);
8314                 }
8315
8316                 /* Totally Illegal */
8317                 if ((i < 0) || (i >= max_num) || !able[i])
8318                 {
8319                         bell();
8320                         continue;
8321                 }
8322
8323                 /* Verify it */
8324                 if (ask)
8325                 {
8326                         char tmp_val[160];
8327
8328                         /* Prompt */
8329 #ifdef JP
8330                         (void) strnfmt(tmp_val, 78, "%s¤òÉղä·¤Þ¤¹¤«¡© ", essence_info[num[i]].add_name);
8331 #else
8332                         (void) strnfmt(tmp_val, 78, "Add the abilitiy of %s? ", essence_info[num[i]].add_name);
8333 #endif
8334
8335                         /* Belay that order */
8336                         if (!get_check(tmp_val)) continue;
8337                 }
8338
8339                 /* Stop the loop */
8340                 flag = TRUE;
8341         }
8342
8343         /* Restore the screen */
8344         if (redraw) screen_load();
8345
8346         if (!flag) return;
8347
8348 #ifdef ALLOW_REPEAT
8349         repeat_push(i);
8350         }
8351 #endif /* ALLOW_REPEAT */
8352
8353         es_ptr = &essence_info[num[i]];
8354
8355         if (es_ptr->add == ESSENCE_SLAY_GLOVE)
8356                 item_tester_tval = TV_GLOVES;
8357         else if (mode == 1 || mode == 5)
8358                 item_tester_hook = item_tester_hook_melee_ammo;
8359         else if (es_ptr->add == ESSENCE_ATTACK)
8360                 item_tester_hook = object_allow_enchant_weapon;
8361         else if (es_ptr->add == ESSENCE_AC)
8362                 item_tester_hook = object_is_armour;
8363         else
8364                 item_tester_hook = object_is_weapon_armour_ammo;
8365         item_tester_no_ryoute = TRUE;
8366
8367         /* Get an item */
8368 #ifdef JP
8369         q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò²þÎɤ·¤Þ¤¹¤«¡©";
8370         s = "²þÎɤǤ­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
8371 #else
8372         q = "Improve which item? ";
8373         s = "You have nothing to improve.";
8374 #endif
8375
8376         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
8377
8378         /* Get the item (in the pack) */
8379         if (item >= 0)
8380         {
8381                 o_ptr = &inventory[item];
8382         }
8383
8384         /* Get the item (on the floor) */
8385         else
8386         {
8387                 o_ptr = &o_list[0 - item];
8388         }
8389
8390         if ((mode != 10) && (object_is_artifact(o_ptr) || object_is_smith(o_ptr)))
8391         {
8392 #ifdef JP
8393                 msg_print("¤½¤Î¥¢¥¤¥Æ¥à¤Ï¤³¤ì°Ê¾å²þÎɤǤ­¤Ê¤¤¡£");
8394 #else
8395                 msg_print("This item is no more able to be improved.");
8396 #endif
8397                 return;
8398         }
8399
8400         object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
8401
8402         use_essence = es_ptr->value;
8403         if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_BOLT)) use_essence = (use_essence+9)/10;
8404         if (o_ptr->number > 1)
8405         {
8406                 use_essence *= o_ptr->number;
8407 #ifdef JP
8408                 msg_format("%d¸Ä¤¢¤ë¤Î¤Ç¥¨¥Ã¥»¥ó¥¹¤Ï%dɬÍפǤ¹¡£", o_ptr->number, use_essence);
8409 #else
8410                 msg_format("It will take %d essences.",use_essence);
8411 #endif
8412
8413         }
8414
8415         if (es_ptr->essence != -1)
8416         {
8417                 if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
8418                 {
8419 #ifdef JP
8420                         msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
8421 #else
8422                         msg_print("You don't have enough essences.");
8423 #endif
8424                         return;
8425                 }
8426                 if (is_pval_flag(es_ptr->add))
8427                 {
8428                         if (o_ptr->pval < 0)
8429                         {
8430 #ifdef JP
8431                                 msg_print("¤³¤Î¥¢¥¤¥Æ¥à¤ÎǽÎϽ¤Àµ¤ò¶¯²½¤¹¤ë¤³¤È¤Ï¤Ç¤­¤Ê¤¤¡£");
8432 #else
8433                                 msg_print("You cannot increase magic number of this item.");
8434 #endif
8435                                 return;
8436                         }
8437                         else if (es_ptr->add == TR_BLOWS)
8438                         {
8439                                 if (o_ptr->pval > 1)
8440                                 {
8441 #ifdef JP
8442                                         if (!get_check("½¤ÀµÃͤÏ1¤Ë¤Ê¤ê¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡©")) return;
8443 #else
8444                                         if (!get_check("The magic number of this weapon will become 1. Are you sure? ")) return;
8445 #endif
8446                                 }
8447
8448                                 o_ptr->pval = 1;
8449 #ifdef JP
8450                                 msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
8451 #else
8452                                 msg_format("It will take %d essences.", use_essence);
8453 #endif
8454                         }
8455                         else if (o_ptr->pval > 0)
8456                         {
8457                                 use_essence *= o_ptr->pval;
8458 #ifdef JP
8459                                 msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
8460 #else
8461                                 msg_format("It will take %d essences.", use_essence);
8462 #endif
8463                         }
8464                         else
8465                         {
8466                                 char tmp[80];
8467                                 char tmp_val[160];
8468                                 int pval;
8469                                 int limit = MIN(5, p_ptr->magic_num1[es_ptr->essence]/es_ptr->value);
8470
8471 #ifdef JP
8472                                 sprintf(tmp, "¤¤¤¯¤ÄÉղä·¤Þ¤¹¤«¡© (1-%d): ", limit);
8473 #else
8474                                 sprintf(tmp, "Enchant how many? (1-%d): ", limit);
8475 #endif
8476                                 strcpy(tmp_val, "1");
8477
8478                                 if (!get_string(tmp, tmp_val, 1)) return;
8479                                 pval = atoi(tmp_val);
8480                                 if (pval > limit) pval = limit;
8481                                 else if (pval < 1) pval = 1;
8482                                 o_ptr->pval += pval;
8483                                 use_essence *= pval;
8484 #ifdef JP
8485                                 msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
8486 #else
8487                                 msg_format("It will take %d essences.", use_essence);
8488 #endif
8489                         }
8490
8491                         if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
8492                         {
8493 #ifdef JP
8494                                 msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
8495 #else
8496                                 msg_print("You don't have enough essences.");
8497 #endif
8498                                 return;
8499                         }
8500                 }
8501                 else if (es_ptr->add == ESSENCE_SLAY_GLOVE)
8502                 {
8503                         char tmp_val[160];
8504                         int val;
8505                         int get_to_h, get_to_d;
8506
8507                         strcpy(tmp_val, "1");
8508 #ifdef JP
8509                         if (!get_string(format("¤¤¤¯¤ÄÉղä·¤Þ¤¹¤«¡© (1-%d):", p_ptr->lev/7+3), tmp_val, 2)) return;
8510 #else
8511                         if (!get_string(format("Enchant how many? (1-%d):", p_ptr->lev/7+3), tmp_val, 2)) return;
8512 #endif
8513                         val = atoi(tmp_val);
8514                         if (val > p_ptr->lev/7+3) val = p_ptr->lev/7+3;
8515                         else if (val < 1) val = 1;
8516                         use_essence *= val;
8517 #ifdef JP
8518                         msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£", use_essence);
8519 #else
8520                         msg_format("It will take %d essences.", use_essence);
8521 #endif
8522                         if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
8523                         {
8524 #ifdef JP
8525                                 msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
8526 #else
8527                                 msg_print("You don't have enough essences.");
8528 #endif
8529                                 return;
8530                         }
8531                         get_to_h = ((val+1)/2+randint0(val/2+1));
8532                         get_to_d = ((val+1)/2+randint0(val/2+1));
8533                         o_ptr->xtra4 = (get_to_h<<8)+get_to_d;
8534                         o_ptr->to_h += get_to_h;
8535                         o_ptr->to_d += get_to_d;
8536                 }
8537                 p_ptr->magic_num1[es_ptr->essence] -= use_essence;
8538                 if (es_ptr->add == ESSENCE_ATTACK)
8539                 {
8540                         if ((o_ptr->to_h >= p_ptr->lev/5+5) && (o_ptr->to_d >= p_ptr->lev/5+5))
8541                         {
8542 #ifdef JP
8543                                 msg_print("²þÎɤ˼ºÇÔ¤·¤¿¡£");
8544 #else
8545                                 msg_print("You failed to enchant.");
8546 #endif
8547                                 energy_use = 100;
8548                                 return;
8549                         }
8550                         else
8551                         {
8552                                 if (o_ptr->to_h < p_ptr->lev/5+5) o_ptr->to_h++;
8553                                 if (o_ptr->to_d < p_ptr->lev/5+5) o_ptr->to_d++;
8554                         }
8555                 }
8556                 else if (es_ptr->add == ESSENCE_AC)
8557                 {
8558                         if (o_ptr->to_a >= p_ptr->lev/5+5)
8559                         {
8560 #ifdef JP
8561                                 msg_print("²þÎɤ˼ºÇÔ¤·¤¿¡£");
8562 #else
8563                                 msg_print("You failed to enchant.");
8564 #endif
8565                                 energy_use = 100;
8566                                 return;
8567                         }
8568                         else
8569                         {
8570                                 if (o_ptr->to_a < p_ptr->lev/5+5) o_ptr->to_a++;
8571                         }
8572                 }
8573                 else
8574                 {
8575                         o_ptr->xtra3 = es_ptr->add + 1;
8576                 }
8577         }
8578         else
8579         {
8580                 bool success = TRUE;
8581
8582                 switch(es_ptr->add)
8583                 {
8584                 case ESSENCE_SH_FIRE:
8585                         if ((p_ptr->magic_num1[TR_BRAND_FIRE] < use_essence) || (p_ptr->magic_num1[TR_RES_FIRE] < use_essence))
8586                         {
8587                                 success = FALSE;
8588                                 break;
8589                         }
8590                         p_ptr->magic_num1[TR_BRAND_FIRE] -= use_essence;
8591                         p_ptr->magic_num1[TR_RES_FIRE] -= use_essence;
8592                         break;
8593                 case ESSENCE_SH_ELEC:
8594                         if ((p_ptr->magic_num1[TR_BRAND_ELEC] < use_essence) || (p_ptr->magic_num1[TR_RES_ELEC] < use_essence))
8595                         {
8596                                 success = FALSE;
8597                                 break;
8598                         }
8599                         p_ptr->magic_num1[TR_BRAND_ELEC] -= use_essence;
8600                         p_ptr->magic_num1[TR_RES_ELEC] -= use_essence;
8601                         break;
8602                 case ESSENCE_SH_COLD:
8603                         if ((p_ptr->magic_num1[TR_BRAND_COLD] < use_essence) || (p_ptr->magic_num1[TR_RES_COLD] < use_essence))
8604                         {
8605                                 success = FALSE;
8606                                 break;
8607                         }
8608                         p_ptr->magic_num1[TR_BRAND_COLD] -= use_essence;
8609                         p_ptr->magic_num1[TR_RES_COLD] -= use_essence;
8610                         break;
8611                 case ESSENCE_RESISTANCE:
8612                 case ESSENCE_SUSTAIN:
8613                         if ((p_ptr->magic_num1[TR_RES_ACID] < use_essence) || (p_ptr->magic_num1[TR_RES_ELEC] < use_essence) || (p_ptr->magic_num1[TR_RES_FIRE] < use_essence) || (p_ptr->magic_num1[TR_RES_COLD] < use_essence))
8614                         {
8615                                 success = FALSE;
8616                                 break;
8617                         }
8618                         p_ptr->magic_num1[TR_RES_ACID] -= use_essence;
8619                         p_ptr->magic_num1[TR_RES_ELEC] -= use_essence;
8620                         p_ptr->magic_num1[TR_RES_FIRE] -= use_essence;
8621                         p_ptr->magic_num1[TR_RES_COLD] -= use_essence;
8622                         break;
8623                 }
8624                 if (!success)
8625                 {
8626 #ifdef JP
8627                         msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
8628 #else
8629                         msg_print("You don't have enough essences.");
8630 #endif
8631                         return;
8632                 }
8633                 if (es_ptr->add == ESSENCE_SUSTAIN)
8634                 {
8635                         add_flag(o_ptr->art_flags, TR_IGNORE_ACID);
8636                         add_flag(o_ptr->art_flags, TR_IGNORE_ELEC);
8637                         add_flag(o_ptr->art_flags, TR_IGNORE_FIRE);
8638                         add_flag(o_ptr->art_flags, TR_IGNORE_COLD);
8639                 }
8640                 else
8641                 {
8642                         o_ptr->xtra3 = es_ptr->add + 1;
8643                 }
8644         }
8645
8646         energy_use = 100;
8647
8648 #ifdef JP
8649         msg_format("%s¤Ë%s¤ÎǽÎϤòÉղä·¤Þ¤·¤¿¡£", o_name, es_ptr->add_name);
8650 #else
8651         msg_format("You have added ability of %s to %s.", es_ptr->add_name, o_name);
8652 #endif
8653
8654         /* Combine the pack */
8655         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
8656
8657         /* Window stuff */
8658         p_ptr->window |= (PW_INVEN);
8659 }
8660
8661
8662 static void erase_essence(void)
8663 {
8664         int item;
8665         cptr q, s;
8666         object_type *o_ptr;
8667         char o_name[MAX_NLEN];
8668         u32b flgs[TR_FLAG_SIZE];
8669
8670         item_tester_hook = object_is_smith;
8671
8672         /* Get an item */
8673 #ifdef JP
8674         q = "¤É¤Î¥¢¥¤¥Æ¥à¤Î¥¨¥Ã¥»¥ó¥¹¤ò¾Ãµî¤·¤Þ¤¹¤«¡©";
8675         s = "¥¨¥Ã¥»¥ó¥¹¤òÉղä·¤¿¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
8676 #else
8677         q = "Remove from which item? ";
8678         s = "You have nothing to remove essence.";
8679 #endif
8680
8681         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
8682
8683         /* Get the item (in the pack) */
8684         if (item >= 0)
8685         {
8686                 o_ptr = &inventory[item];
8687         }
8688
8689         /* Get the item (on the floor) */
8690         else
8691         {
8692                 o_ptr = &o_list[0 - item];
8693         }
8694
8695         object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
8696 #ifdef JP
8697         if (!get_check(format("¤è¤í¤·¤¤¤Ç¤¹¤«¡© [%s]", o_name))) return;
8698 #else
8699         if (!get_check(format("Are you sure? [%s]", o_name))) return;
8700 #endif
8701
8702         energy_use = 100;
8703
8704         if (o_ptr->xtra3 == 1+ESSENCE_SLAY_GLOVE)
8705         {
8706                 o_ptr->to_h -= (o_ptr->xtra4>>8);
8707                 o_ptr->to_d -= (o_ptr->xtra4 & 0x000f);
8708                 o_ptr->xtra4 = 0;
8709                 if (o_ptr->to_h < 0) o_ptr->to_h = 0;
8710                 if (o_ptr->to_d < 0) o_ptr->to_d = 0;
8711         }
8712         o_ptr->xtra3 = 0;
8713         object_flags(o_ptr, flgs);
8714         if (!(have_pval_flags(flgs))) o_ptr->pval = 0;
8715 #ifdef JP
8716         msg_print("¥¨¥Ã¥»¥ó¥¹¤ò¼è¤êµî¤Ã¤¿¡£");
8717 #else
8718         msg_print("You removed all essence you have added.");
8719 #endif
8720
8721         /* Combine the pack */
8722         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
8723
8724         /* Window stuff */
8725         p_ptr->window |= (PW_INVEN);
8726 }
8727
8728 void do_cmd_kaji(bool only_browse)
8729 {
8730         int mode = 0;
8731         char choice;
8732
8733         int menu_line = (use_menu ? 1 : 0);
8734
8735         if (!only_browse)
8736         {
8737                 if (p_ptr->confused)
8738                 {
8739 #ifdef JP
8740                         msg_print("º®Í𤷤Ƥ¤¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
8741 #else
8742                         msg_print("You are too confused!");
8743 #endif
8744
8745                         return;
8746                 }
8747                 if (p_ptr->blind)
8748                 {
8749 #ifdef JP
8750                         msg_print("Ìܤ¬¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
8751 #else
8752                         msg_print("You are blind!");
8753 #endif
8754
8755                         return;
8756                 }
8757                 if (p_ptr->image)
8758                 {
8759 #ifdef JP
8760                         msg_print("¤¦¤Þ¤¯¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
8761 #else
8762                         msg_print("You are hallucinating!");
8763 #endif
8764
8765                         return;
8766                 }
8767         }
8768
8769 #ifdef ALLOW_REPEAT
8770         if (!(repeat_pull(&mode) && 1 <= mode && mode <= 5))
8771         {
8772 #endif /* ALLOW_REPEAT */
8773
8774         if (only_browse) screen_save();
8775         do {
8776         if (!only_browse) screen_save();
8777         if (use_menu)
8778         {
8779                 while(!mode)
8780                 {
8781 #ifdef JP
8782                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹°ìÍ÷", (menu_line == 1) ? "¡Õ" : "  "), 2, 14);
8783                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹Ãê½Ð", (menu_line == 2) ? "¡Õ" : "  "), 3, 14);
8784                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹¾Ãµî", (menu_line == 3) ? "¡Õ" : "  "), 4, 14);
8785                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹ÉÕ²Ã", (menu_line == 4) ? "¡Õ" : "  "), 5, 14);
8786                         prt(format(" %s Éð´ï/Ëɶñ¶¯²½", (menu_line == 5) ? "¡Õ" : "  "), 6, 14);
8787                         prt(format("¤É¤Î¼ïÎà¤Îµ»½Ñ¤ò%s¤Þ¤¹¤«¡©", only_browse ? "Ä´¤Ù" : "»È¤¤"), 0, 0);
8788 #else
8789                         prt(format(" %s List essences", (menu_line == 1) ? "> " : "  "), 2, 14);
8790                         prt(format(" %s Extract essence", (menu_line == 2) ? "> " : "  "), 3, 14);
8791                         prt(format(" %s Remove essence", (menu_line == 3) ? "> " : "  "), 4, 14);
8792                         prt(format(" %s Add essence", (menu_line == 4) ? "> " : "  "), 5, 14);
8793                         prt(format(" %s Enchant weapon/armor", (menu_line == 5) ? "> " : "  "), 6, 14);
8794                         prt(format("Choose command from menu."), 0, 0);
8795 #endif
8796                         choice = inkey();
8797                         switch(choice)
8798                         {
8799                         case ESCAPE:
8800                         case 'z':
8801                         case 'Z':
8802                                 screen_load();
8803                                 return;
8804                         case '2':
8805                         case 'j':
8806                         case 'J':
8807                                 menu_line++;
8808                                 break;
8809                         case '8':
8810                         case 'k':
8811                         case 'K':
8812                                 menu_line+= 4;
8813                                 break;
8814                         case '\r':
8815                         case '\n':
8816                         case 'x':
8817                         case 'X':
8818                                 mode = menu_line;
8819                                 break;
8820                         }
8821                         if (menu_line > 5) menu_line -= 5;
8822                 }
8823         }
8824
8825         else
8826         {
8827                 while (!mode)
8828                 {
8829 #ifdef JP
8830                         prt("  a) ¥¨¥Ã¥»¥ó¥¹°ìÍ÷", 2, 14);
8831                         prt("  b) ¥¨¥Ã¥»¥ó¥¹Ãê½Ð", 3, 14);
8832                         prt("  c) ¥¨¥Ã¥»¥ó¥¹¾Ãµî", 4, 14);
8833                         prt("  d) ¥¨¥Ã¥»¥ó¥¹ÉÕ²Ã", 5, 14);
8834                         prt("  e) Éð´ï/Ëɶñ¶¯²½", 6, 14);
8835                         if (!get_com(format("¤É¤ÎǽÎϤò%s¤Þ¤¹¤«:", only_browse ? "Ä´¤Ù" : "»È¤¤"), &choice, TRUE))
8836 #else
8837                         prt("  a) List essences", 2, 14);
8838                         prt("  b) Extract essence", 3, 14);
8839                         prt("  c) Remove essence", 4, 14);
8840                         prt("  d) Add essence", 5, 14);
8841                         prt("  e) Enchant weapon/armor", 6, 14);
8842                         if (!get_com("Command :", &choice, TRUE))
8843 #endif
8844                         {
8845                                 screen_load();
8846                                 return;
8847                         }
8848                         switch (choice)
8849                         {
8850                         case 'A':
8851                         case 'a':
8852                                 mode = 1;
8853                                 break;
8854                         case 'B':
8855                         case 'b':
8856                                 mode = 2;
8857                                 break;
8858                         case 'C':
8859                         case 'c':
8860                                 mode = 3;
8861                                 break;
8862                         case 'D':
8863                         case 'd':
8864                                 mode = 4;
8865                                 break;
8866                         case 'E':
8867                         case 'e':
8868                                 mode = 5;
8869                                 break;
8870                         }
8871                 }
8872         }
8873
8874         if (only_browse)
8875         {
8876                 char temp[62*5];
8877                 int line, j;
8878
8879                 /* Clear lines, position cursor  (really should use strlen here) */
8880                 Term_erase(14, 21, 255);
8881                 Term_erase(14, 20, 255);
8882                 Term_erase(14, 19, 255);
8883                 Term_erase(14, 18, 255);
8884                 Term_erase(14, 17, 255);
8885                 Term_erase(14, 16, 255);
8886
8887                 roff_to_buf(kaji_tips[mode-1], 62, temp, sizeof(temp));
8888                 for(j=0, line = 17;temp[j];j+=(1+strlen(&temp[j])))
8889                 {
8890                         prt(&temp[j], line, 15);
8891                         line++;
8892                 }
8893                 mode = 0;
8894         }
8895         if (!only_browse) screen_load();
8896         } while (only_browse);
8897 #ifdef ALLOW_REPEAT
8898         repeat_push(mode);
8899         }
8900 #endif /* ALLOW_REPEAT */
8901
8902         switch(mode)
8903         {
8904                 case 1: display_essence();break;
8905                 case 2: drain_essence();break;
8906                 case 3: erase_essence();break;
8907                 case 4:
8908                         mode = choose_essence();
8909                         if (mode == 0)
8910                                 break;
8911                         add_essence(mode);
8912                         break;
8913                 case 5: add_essence(10);break;
8914         }
8915 }
8916
8917
8918 /*
8919  * Torches have special abilities when they are flaming.
8920  */
8921 void torch_flags(object_type *o_ptr, u32b *flgs)
8922 {
8923         if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_TORCH))
8924         {
8925                 if (o_ptr->xtra4 > 0)
8926                 {
8927                         add_flag(flgs, TR_BRAND_FIRE);
8928                         add_flag(flgs, TR_KILL_UNDEAD);
8929                         add_flag(flgs, TR_THROW);
8930                 }
8931         }
8932 }
8933
8934 void torch_dice(object_type *o_ptr, int *dd, int *ds)
8935 {
8936         if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_TORCH))
8937         {
8938                 if (o_ptr->xtra4 > 0)
8939                 {
8940                         (*dd) = 1;
8941                         (*ds) = 6;
8942                 }
8943         }
8944 }
8945
8946 void torch_lost_fuel(object_type *o_ptr)
8947 {
8948         if ((o_ptr->tval == TV_LITE) && (o_ptr->sval == SV_LITE_TORCH))
8949         {
8950                 o_ptr->xtra4 -= (FUEL_TORCH / 25);
8951                 if (o_ptr->xtra4 < 0) o_ptr->xtra4 = 0;
8952         }
8953 }