OSDN Git Service

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