OSDN Git Service

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