OSDN Git Service

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