OSDN Git Service

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