OSDN Git Service

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