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