OSDN Git Service

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