OSDN Git Service

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