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