OSDN Git Service

既に忘れた何かの理由によって、長い事tabをスペース8文字にする設定で
[hengband/hengband.git] / src / object2.c
1 /* File: object2.c */
2
3 /* Purpose: Object code, part 2 */
4
5 /*
6  * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14
15 #include "kajitips.h"
16
17
18 /*
19  * Excise a dungeon object from any stacks
20  */
21 void excise_object_idx(int o_idx)
22 {
23         object_type *j_ptr;
24
25         s16b this_o_idx, next_o_idx = 0;
26
27         s16b prev_o_idx = 0;
28
29
30         /* Object */
31         j_ptr = &o_list[o_idx];
32
33         /* Monster */
34         if (j_ptr->held_m_idx)
35         {
36                 monster_type *m_ptr;
37
38                 /* Monster */
39                 m_ptr = &m_list[j_ptr->held_m_idx];
40
41                 /* Scan all objects in the grid */
42                 for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
43                 {
44                         object_type *o_ptr;
45
46                         /* Acquire object */
47                         o_ptr = &o_list[this_o_idx];
48
49                         /* Acquire next object */
50                         next_o_idx = o_ptr->next_o_idx;
51
52                         /* Done */
53                         if (this_o_idx == o_idx)
54                         {
55                                 /* No previous */
56                                 if (prev_o_idx == 0)
57                                 {
58                                         /* Remove from list */
59                                         m_ptr->hold_o_idx = next_o_idx;
60                                 }
61
62                                 /* Real previous */
63                                 else
64                                 {
65                                         object_type *k_ptr;
66
67                                         /* Previous object */
68                                         k_ptr = &o_list[prev_o_idx];
69
70                                         /* Remove from list */
71                                         k_ptr->next_o_idx = next_o_idx;
72                                 }
73
74                                 /* Forget next pointer */
75                                 o_ptr->next_o_idx = 0;
76
77                                 /* Done */
78                                 break;
79                         }
80
81                         /* Save prev_o_idx */
82                         prev_o_idx = this_o_idx;
83                 }
84         }
85
86         /* Dungeon */
87         else
88         {
89                 cave_type *c_ptr;
90
91                 int y = j_ptr->iy;
92                 int x = j_ptr->ix;
93
94                 /* Grid */
95                 c_ptr = &cave[y][x];
96
97                 /* Scan all objects in the grid */
98                 for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
99                 {
100                         object_type *o_ptr;
101
102                         /* Acquire object */
103                         o_ptr = &o_list[this_o_idx];
104
105                         /* Acquire next object */
106                         next_o_idx = o_ptr->next_o_idx;
107
108                         /* Done */
109                         if (this_o_idx == o_idx)
110                         {
111                                 /* No previous */
112                                 if (prev_o_idx == 0)
113                                 {
114                                         /* Remove from list */
115                                         c_ptr->o_idx = next_o_idx;
116                                 }
117
118                                 /* Real previous */
119                                 else
120                                 {
121                                         object_type *k_ptr;
122
123                                         /* Previous object */
124                                         k_ptr = &o_list[prev_o_idx];
125
126                                         /* Remove from list */
127                                         k_ptr->next_o_idx = next_o_idx;
128                                 }
129
130                                 /* Forget next pointer */
131                                 o_ptr->next_o_idx = 0;
132
133                                 /* Done */
134                                 break;
135                         }
136
137                         /* Save prev_o_idx */
138                         prev_o_idx = this_o_idx;
139                 }
140         }
141 }
142
143
144 /*
145  * Delete a dungeon object
146  *
147  * Handle "stacks" of objects correctly.
148  */
149 void delete_object_idx(int o_idx)
150 {
151         object_type *j_ptr;
152
153         /* Excise */
154         excise_object_idx(o_idx);
155
156         /* Object */
157         j_ptr = &o_list[o_idx];
158
159         /* Dungeon floor */
160         if (!(j_ptr->held_m_idx))
161         {
162                 int y, x;
163
164                 /* Location */
165                 y = j_ptr->iy;
166                 x = j_ptr->ix;
167
168                 /* Visual update */
169                 lite_spot(y, x);
170         }
171
172         /* Wipe the object */
173         object_wipe(j_ptr);
174
175         /* Count objects */
176         o_cnt--;
177 }
178
179
180 /*
181  * Deletes all objects at given location
182  */
183 void delete_object(int y, int x)
184 {
185         cave_type *c_ptr;
186
187         s16b this_o_idx, next_o_idx = 0;
188
189
190         /* Refuse "illegal" locations */
191         if (!in_bounds(y, x)) return;
192
193
194         /* Grid */
195         c_ptr = &cave[y][x];
196
197         /* Scan all objects in the grid */
198         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
199         {
200                 object_type *o_ptr;
201
202                 /* Acquire object */
203                 o_ptr = &o_list[this_o_idx];
204
205                 /* Acquire next object */
206                 next_o_idx = o_ptr->next_o_idx;
207
208                 /* Wipe the object */
209                 object_wipe(o_ptr);
210
211                 /* Count objects */
212                 o_cnt--;
213         }
214
215         /* Objects are gone */
216         c_ptr->o_idx = 0;
217
218         /* Visual update */
219         lite_spot(y, x);
220 }
221
222
223 /*
224  * Move an object from index i1 to index i2 in the object list
225  */
226 static void compact_objects_aux(int i1, int i2)
227 {
228         int i;
229
230         cave_type *c_ptr;
231
232         object_type *o_ptr;
233
234
235         /* Do nothing */
236         if (i1 == i2) return;
237
238
239         /* Repair objects */
240         for (i = 1; i < o_max; i++)
241         {
242                 /* Acquire object */
243                 o_ptr = &o_list[i];
244
245                 /* Skip "dead" objects */
246                 if (!o_ptr->k_idx) continue;
247
248                 /* Repair "next" pointers */
249                 if (o_ptr->next_o_idx == i1)
250                 {
251                         /* Repair */
252                         o_ptr->next_o_idx = i2;
253                 }
254         }
255
256
257         /* Acquire object */
258         o_ptr = &o_list[i1];
259
260
261         /* Monster */
262         if (o_ptr->held_m_idx)
263         {
264                 monster_type *m_ptr;
265
266                 /* Acquire monster */
267                 m_ptr = &m_list[o_ptr->held_m_idx];
268
269                 /* Repair monster */
270                 if (m_ptr->hold_o_idx == i1)
271                 {
272                         /* Repair */
273                         m_ptr->hold_o_idx = i2;
274                 }
275         }
276
277         /* Dungeon */
278         else
279         {
280                 int y, x;
281
282                 /* Acquire location */
283                 y = o_ptr->iy;
284                 x = o_ptr->ix;
285
286                 /* Acquire grid */
287                 c_ptr = &cave[y][x];
288
289                 /* Repair grid */
290                 if (c_ptr->o_idx == i1)
291                 {
292                         /* Repair */
293                         c_ptr->o_idx = i2;
294                 }
295         }
296
297
298         /* Structure copy */
299         o_list[i2] = o_list[i1];
300
301         /* Wipe the hole */
302         object_wipe(o_ptr);
303 }
304
305
306 /*
307  * Compact and Reorder the object list
308  *
309  * This function can be very dangerous, use with caution!
310  *
311  * When actually "compacting" objects, we base the saving throw on a
312  * combination of object level, distance from player, and current
313  * "desperation".
314  *
315  * After "compacting" (if needed), we "reorder" the objects into a more
316  * compact order, and we reset the allocation info, and the "live" array.
317  */
318 void compact_objects(int size)
319 {
320         int i, y, x, num, cnt;
321         int cur_lev, cur_dis, chance;
322         object_type *o_ptr;
323
324
325         /* Compact */
326         if (size)
327         {
328                 /* Message */
329 #ifdef JP
330                 msg_print("¥¢¥¤¥Æ¥à¾ðÊó¤ò°µ½Ì¤·¤Æ¤¤¤Þ¤¹...");
331 #else
332                 msg_print("Compacting objects...");
333 #endif
334
335
336                 /* Redraw map */
337                 p_ptr->redraw |= (PR_MAP);
338
339                 /* Window stuff */
340                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
341         }
342
343
344         /* Compact at least 'size' objects */
345         for (num = 0, cnt = 1; num < size; cnt++)
346         {
347                 /* Get more vicious each iteration */
348                 cur_lev = 5 * cnt;
349
350                 /* Get closer each iteration */
351                 cur_dis = 5 * (20 - cnt);
352
353                 /* Examine the objects */
354                 for (i = 1; i < o_max; i++)
355                 {
356                         o_ptr = &o_list[i];
357
358                         /* Skip dead objects */
359                         if (!o_ptr->k_idx) continue;
360
361                         /* Hack -- High level objects start out "immune" */
362                         if (get_object_level(o_ptr) > cur_lev) continue;
363
364                         /* Monster */
365                         if (o_ptr->held_m_idx)
366                         {
367                                 monster_type *m_ptr;
368
369                                 /* Acquire monster */
370                                 m_ptr = &m_list[o_ptr->held_m_idx];
371
372                                 /* Get the location */
373                                 y = m_ptr->fy;
374                                 x = m_ptr->fx;
375
376                                 /* Monsters protect their objects */
377                                 if (randint0(100) < 90) continue;
378                         }
379
380                         /* Dungeon */
381                         else
382                         {
383                                 /* Get the location */
384                                 y = o_ptr->iy;
385                                 x = o_ptr->ix;
386                         }
387
388                         /* Nearby objects start out "immune" */
389                         if ((cur_dis > 0) && (distance(py, px, y, x) < cur_dis)) continue;
390
391                         /* Saving throw */
392                         chance = 90;
393
394                         /* Hack -- only compact artifacts in emergencies */
395                         if ((artifact_p(o_ptr) || o_ptr->art_name) &&
396                             (cnt < 1000)) chance = 100;
397
398                         /* Apply the saving throw */
399                         if (randint0(100) < chance) continue;
400
401                         /* Delete the object */
402                         delete_object_idx(i);
403
404                         /* Count it */
405                         num++;
406                 }
407         }
408
409
410         /* Excise dead objects (backwards!) */
411         for (i = o_max - 1; i >= 1; i--)
412         {
413                 o_ptr = &o_list[i];
414
415                 /* Skip real objects */
416                 if (o_ptr->k_idx) continue;
417
418                 /* Move last object into open hole */
419                 compact_objects_aux(o_max - 1, i);
420
421                 /* Compress "o_max" */
422                 o_max--;
423         }
424 }
425
426
427 /*
428  * Delete all the items when player leaves the level
429  *
430  * Note -- we do NOT visually reflect these (irrelevant) changes
431  *
432  * Hack -- we clear the "c_ptr->o_idx" field for every grid,
433  * and the "m_ptr->next_o_idx" field for every monster, since
434  * we know we are clearing every object.  Technically, we only
435  * clear those fields for grids/monsters containing objects,
436  * and we clear it once for every such object.
437  */
438 void wipe_o_list(void)
439 {
440         int i;
441
442         /* Delete the existing objects */
443         for (i = 1; i < o_max; i++)
444         {
445                 object_type *o_ptr = &o_list[i];
446
447                 /* Skip dead objects */
448                 if (!o_ptr->k_idx) continue;
449
450                 /* Mega-Hack -- preserve artifacts */
451                 if (!character_dungeon || preserve_mode)
452                 {
453                         /* Hack -- Preserve unknown artifacts */
454                         if (artifact_p(o_ptr) && !object_known_p(o_ptr))
455                         {
456                                 /* Mega-Hack -- Preserve the artifact */
457                                 a_info[o_ptr->name1].cur_num = 0;
458                         }
459                 }
460
461                 /* Monster */
462                 if (o_ptr->held_m_idx)
463                 {
464                         monster_type *m_ptr;
465
466                         /* Monster */
467                         m_ptr = &m_list[o_ptr->held_m_idx];
468
469                         /* Hack -- see above */
470                         m_ptr->hold_o_idx = 0;
471                 }
472
473                 /* Dungeon */
474                 else
475                 {
476                         cave_type *c_ptr;
477
478                         /* Access location */
479                         int y = o_ptr->iy;
480                         int x = o_ptr->ix;
481
482                         /* Access grid */
483                         c_ptr = &cave[y][x];
484
485                         /* Hack -- see above */
486                         c_ptr->o_idx = 0;
487                 }
488
489                 /* Wipe the object */
490                 object_wipe(o_ptr);
491         }
492
493         /* Reset "o_max" */
494         o_max = 1;
495
496         /* Reset "o_cnt" */
497         o_cnt = 0;
498 }
499
500
501 /*
502  * Acquires and returns the index of a "free" object.
503  *
504  * This routine should almost never fail, but in case it does,
505  * we must be sure to handle "failure" of this routine.
506  */
507 s16b o_pop(void)
508 {
509         int i;
510
511
512         /* Initial allocation */
513         if (o_max < max_o_idx)
514         {
515                 /* Get next space */
516                 i = o_max;
517
518                 /* Expand object array */
519                 o_max++;
520
521                 /* Count objects */
522                 o_cnt++;
523
524                 /* Use this object */
525                 return (i);
526         }
527
528
529         /* Recycle dead objects */
530         for (i = 1; i < o_max; i++)
531         {
532                 object_type *o_ptr;
533
534                 /* Acquire object */
535                 o_ptr = &o_list[i];
536
537                 /* Skip live objects */
538                 if (o_ptr->k_idx) continue;
539
540                 /* Count objects */
541                 o_cnt++;
542
543                 /* Use this object */
544                 return (i);
545         }
546
547
548         /* Warn the player (except during dungeon creation) */
549 #ifdef JP
550         if (character_dungeon) msg_print("¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë¡ª");
551 #else
552         if (character_dungeon) msg_print("Too many objects!");
553 #endif
554
555
556         /* Oops */
557         return (0);
558 }
559
560
561 /*
562  * Apply a "object restriction function" to the "object allocation table"
563  */
564 errr get_obj_num_prep(void)
565 {
566         int i;
567
568         /* Get the entry */
569         alloc_entry *table = alloc_kind_table;
570
571         /* Scan the allocation table */
572         for (i = 0; i < alloc_kind_size; i++)
573         {
574                 /* Accept objects which pass the restriction, if any */
575                 if (!get_obj_num_hook || (*get_obj_num_hook)(table[i].index))
576                 {
577                         /* Accept this object */
578                         table[i].prob2 = table[i].prob1;
579                 }
580
581                 /* Do not use this object */
582                 else
583                 {
584                         /* Decline this object */
585                         table[i].prob2 = 0;
586                 }
587         }
588
589         /* Success */
590         return (0);
591 }
592
593
594 /*
595  * Choose an object kind that seems "appropriate" to the given level
596  *
597  * This function uses the "prob2" field of the "object allocation table",
598  * and various local information, to calculate the "prob3" field of the
599  * same table, which is then used to choose an "appropriate" object, in
600  * a relatively efficient manner.
601  *
602  * It is (slightly) more likely to acquire an object of the given level
603  * than one of a lower level.  This is done by choosing several objects
604  * appropriate to the given level and keeping the "hardest" one.
605  *
606  * Note that if no objects are "appropriate", then this function will
607  * fail, and return zero, but this should *almost* never happen.
608  */
609 s16b get_obj_num(int level)
610 {
611         int             i, j, p;
612         int             k_idx;
613         long            value, total;
614         object_kind     *k_ptr;
615         alloc_entry     *table = alloc_kind_table;
616
617         if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
618
619         /* Boost level */
620         if ((level > 0) && !(d_info[dungeon_type].flags1 & DF1_BEGINNER))
621         {
622                 /* Occasional "boost" */
623                 if (one_in_(GREAT_OBJ))
624                 {
625                         /* What a bizarre calculation */
626                         level = 1 + (level * MAX_DEPTH / randint1(MAX_DEPTH));
627                 }
628         }
629
630         /* Reset total */
631         total = 0L;
632
633         /* Process probabilities */
634         for (i = 0; i < alloc_kind_size; i++)
635         {
636                 /* Objects are sorted by depth */
637                 if (table[i].level > level) break;
638
639                 /* Default */
640                 table[i].prob3 = 0;
641
642                 /* Access the index */
643                 k_idx = table[i].index;
644
645                 /* Access the actual kind */
646                 k_ptr = &k_info[k_idx];
647
648                 /* Hack -- prevent embedded chests */
649                 if (opening_chest && (k_ptr->tval == TV_CHEST)) continue;
650
651                 /* Accept */
652                 table[i].prob3 = table[i].prob2;
653
654                 /* Total */
655                 total += table[i].prob3;
656         }
657
658         /* No legal objects */
659         if (total <= 0) return (0);
660
661
662         /* Pick an object */
663         value = randint0(total);
664
665         /* Find the object */
666         for (i = 0; i < alloc_kind_size; i++)
667         {
668                 /* Found the entry */
669                 if (value < table[i].prob3) break;
670
671                 /* Decrement */
672                 value = value - table[i].prob3;
673         }
674
675
676         /* Power boost */
677         p = randint0(100);
678
679         /* Try for a "better" object once (50%) or twice (10%) */
680         if (p < 60)
681         {
682                 /* Save old */
683                 j = i;
684
685                 /* Pick a object */
686                 value = randint0(total);
687
688                 /* Find the object */
689                 for (i = 0; i < alloc_kind_size; i++)
690                 {
691                         /* Found the entry */
692                         if (value < table[i].prob3) break;
693
694                         /* Decrement */
695                         value = value - table[i].prob3;
696                 }
697
698                 /* Keep the "best" one */
699                 if (table[i].level < table[j].level) i = j;
700         }
701
702         /* Try for a "better" object twice (10%) */
703         if (p < 10)
704         {
705                 /* Save old */
706                 j = i;
707
708                 /* Pick a object */
709                 value = randint0(total);
710
711                 /* Find the object */
712                 for (i = 0; i < alloc_kind_size; i++)
713                 {
714                         /* Found the entry */
715                         if (value < table[i].prob3) break;
716
717                         /* Decrement */
718                         value = value - table[i].prob3;
719                 }
720
721                 /* Keep the "best" one */
722                 if (table[i].level < table[j].level) i = j;
723         }
724
725
726         /* Result */
727         return (table[i].index);
728 }
729
730
731 /*
732  * Known is true when the "attributes" of an object are "known".
733  * These include tohit, todam, toac, cost, and pval (charges).
734  *
735  * Note that "knowing" an object gives you everything that an "awareness"
736  * gives you, and much more.  In fact, the player is always "aware" of any
737  * item of which he has full "knowledge".
738  *
739  * But having full knowledge of, say, one "wand of wonder", does not, by
740  * itself, give you knowledge, or even awareness, of other "wands of wonder".
741  * It happens that most "identify" routines (including "buying from a shop")
742  * will make the player "aware" of the object as well as fully "know" it.
743  *
744  * This routine also removes any inscriptions generated by "feelings".
745  */
746 void object_known(object_type *o_ptr)
747 {
748         /* Remove "default inscriptions" */
749         o_ptr->feeling = FEEL_NONE;
750
751         /* Clear the "Felt" info */
752         o_ptr->ident &= ~(IDENT_SENSE);
753
754         /* Clear the "Empty" info */
755         o_ptr->ident &= ~(IDENT_EMPTY);
756
757         /* Now we know about the item */
758         o_ptr->ident |= (IDENT_KNOWN);
759 }
760
761
762 /*
763  * The player is now aware of the effects of the given object.
764  */
765 void object_aware(object_type *o_ptr)
766 {
767         bool mihanmei = !object_aware_p(o_ptr);
768
769 #ifndef SCRIPT_OBJ_KIND
770         /* Fully aware of the effects */
771         k_info[o_ptr->k_idx].aware = TRUE;
772 #else /* SCRIPT_OBJ_KIND */
773         /* Fully aware of the effects */
774         o_ptr->aware = TRUE;
775 #endif /* SCRIPT_OBJ_KIND */
776
777         if(mihanmei && !(k_info[o_ptr->k_idx].gen_flags & TRG_INSTA_ART) && record_ident &&
778            !p_ptr->is_dead && ((o_ptr->tval >= TV_AMULET && o_ptr->tval <= TV_POTION) || (o_ptr->tval == TV_FOOD)))
779         {
780                 object_type forge;
781                 object_type *q_ptr;
782                 char o_name[MAX_NLEN];
783
784                 q_ptr = &forge;
785                 object_copy(q_ptr, o_ptr);
786
787                 q_ptr->number = 1;
788                 object_desc(o_name, q_ptr, TRUE, 0);
789                 
790                 do_cmd_write_nikki(NIKKI_HANMEI, 0, o_name);
791         }
792 }
793
794
795 /*
796  * Something has been "sampled"
797  */
798 void object_tried(object_type *o_ptr)
799 {
800 #ifndef SCRIPT_OBJ_KIND
801         /* Mark it as tried (even if "aware") */
802         k_info[o_ptr->k_idx].tried = TRUE;
803 #else /* SCRIPT_OBJ_KIND */
804         o_ptr->tried = TRUE;
805 #endif /* SCRIPT_OBJ_KIND */
806 }
807
808
809 /*
810  * Return the "value" of an "unknown" item
811  * Make a guess at the value of non-aware items
812  */
813 static s32b object_value_base(object_type *o_ptr)
814 {
815         /* Aware item -- use template cost */
816         if (object_aware_p(o_ptr)) return (get_object_cost(o_ptr));
817
818         /* Analyze the type */
819         switch (o_ptr->tval)
820         {
821
822                 /* Un-aware Food */
823                 case TV_FOOD: return (5L);
824
825                 /* Un-aware Potions */
826                 case TV_POTION: return (20L);
827
828                 /* Un-aware Scrolls */
829                 case TV_SCROLL: return (20L);
830
831                 /* Un-aware Staffs */
832                 case TV_STAFF: return (70L);
833
834                 /* Un-aware Wands */
835                 case TV_WAND: return (50L);
836
837                 /* Un-aware Rods */
838                 case TV_ROD: return (90L);
839
840                 /* Un-aware Rings */
841                 case TV_RING: return (45L);
842
843                 /* Un-aware Amulets */
844                 case TV_AMULET: return (45L);
845
846                 /* Figurines, relative to monster level */
847                 case TV_FIGURINE:
848                 {
849                         int level = r_info[o_ptr->pval].level;
850                         if (level < 20) return level*50L;
851                         else if (level < 30) return 1000+(level-20)*150L;
852                         else if (level < 40) return 2500+(level-30)*350L;
853                         else if (level < 50) return 6000+(level-40)*800L;
854                         else return 14000+(level-50)*2000L;
855                         break;
856                 }
857
858                 case TV_CAPTURE:
859                         if (!o_ptr->pval) return 1000L;
860                         else return ((r_info[o_ptr->pval].level) * 50L + 1000);
861         }
862
863         /* Paranoia -- Oops */
864         return (0L);
865 }
866
867
868 /* Return the value of the flags the object has... */
869 s32b flag_cost(object_type * o_ptr, int plusses)
870 {
871         s32b total = 0;
872         u32b flgs[TR_FLAG_SIZE];
873         s32b tmp_cost;
874         int count;
875         int i;
876
877         object_flags(o_ptr, flgs);
878
879         if (o_ptr->name1)
880         {
881                 artifact_type *a_ptr = &a_info[o_ptr->name1];
882
883                 for (i = 0; i < TR_FLAG_SIZE; i++)
884                         flgs[i] &= ~(a_ptr->flags[i]);
885         }
886         else
887         {
888                 if ((o_ptr->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                 /* Extract the other fields */
4215                 o_ptr->pval = a_ptr->pval;
4216                 o_ptr->ac = a_ptr->ac;
4217                 o_ptr->dd = a_ptr->dd;
4218                 o_ptr->ds = a_ptr->ds;
4219                 o_ptr->to_a = a_ptr->to_a;
4220                 o_ptr->to_h = a_ptr->to_h;
4221                 o_ptr->to_d = a_ptr->to_d;
4222                 o_ptr->weight = a_ptr->weight;
4223
4224                 /* Hack -- extract the "broken" flag */
4225                 if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
4226
4227                 /* Hack -- extract the "cursed" flag */
4228                 if (a_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
4229                 if (a_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
4230                 if (a_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
4231                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4232                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4233                 if (a_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4234
4235                 /* Mega-Hack -- increase the rating */
4236                 rating += 10;
4237
4238                 /* Mega-Hack -- increase the rating again */
4239                 if (a_ptr->cost > 50000L) rating += 10;
4240
4241                 /* Mega-Hack -- increase the rating again */
4242                 if (a_ptr->cost > 100000L) rating += 10;
4243
4244                 /* Set the good item flag */
4245                 good_item_flag = TRUE;
4246
4247                 /* Cheat -- peek at the item */
4248                 if (cheat_peek) object_mention(o_ptr);
4249
4250                 /* Done */
4251                 return;
4252         }
4253
4254
4255         /* Apply magic */
4256         switch (o_ptr->tval)
4257         {
4258                 case TV_DIGGING:
4259                 case TV_HAFTED:
4260                 case TV_BOW:
4261                 case TV_SHOT:
4262                 case TV_ARROW:
4263                 case TV_BOLT:
4264                 {
4265                         if (power) a_m_aux_1(o_ptr, lev, power);
4266                         break;
4267                 }
4268
4269                 case TV_POLEARM:
4270                 {
4271                         if (power && !(o_ptr->sval == SV_DEATH_SCYTHE)) a_m_aux_1(o_ptr, lev, power);
4272                         break;
4273                 }
4274
4275                 case TV_SWORD:
4276                 {
4277                         if (power && !(o_ptr->sval == SV_DOKUBARI)) a_m_aux_1(o_ptr, lev, power);
4278                         break;
4279                 }
4280
4281                 case TV_DRAG_ARMOR:
4282                 case TV_HARD_ARMOR:
4283                 case TV_SOFT_ARMOR:
4284                 case TV_SHIELD:
4285                 case TV_HELM:
4286                 case TV_CROWN:
4287                 case TV_CLOAK:
4288                 case TV_GLOVES:
4289                 case TV_BOOTS:
4290                 {
4291                         /* Elven Cloak and Black Clothes ... */
4292                         if (((o_ptr->tval == TV_CLOAK) && (o_ptr->sval == SV_ELVEN_CLOAK)) ||
4293                             ((o_ptr->tval == TV_SOFT_ARMOR) && (o_ptr->sval == SV_KUROSHOUZOKU)))
4294                                 o_ptr->pval = randint1(4);
4295
4296 #if 1
4297                         if (power ||
4298                              ((o_ptr->tval == TV_HELM) && (o_ptr->sval == SV_DRAGON_HELM)) ||
4299                              ((o_ptr->tval == TV_SHIELD) && (o_ptr->sval == SV_DRAGON_SHIELD)) ||
4300                              ((o_ptr->tval == TV_GLOVES) && (o_ptr->sval == SV_SET_OF_DRAGON_GLOVES)) ||
4301                              ((o_ptr->tval == TV_BOOTS) && (o_ptr->sval == SV_PAIR_OF_DRAGON_GREAVE)))
4302                                 a_m_aux_2(o_ptr, lev, power);
4303 #else
4304                         if (power) a_m_aux_2(o_ptr, lev, power);
4305 #endif
4306                         break;
4307                 }
4308
4309                 case TV_RING:
4310                 case TV_AMULET:
4311                 {
4312                         if (!power && (randint0(100) < 50)) power = -1;
4313                         a_m_aux_3(o_ptr, lev, power);
4314                         break;
4315                 }
4316
4317                 default:
4318                 {
4319                         a_m_aux_4(o_ptr, lev, power);
4320                         break;
4321                 }
4322         }
4323
4324         if ((o_ptr->tval == TV_SOFT_ARMOR) &&
4325             (o_ptr->sval == SV_ABUNAI_MIZUGI) &&
4326             (p_ptr->pseikaku == SEIKAKU_SEXY))
4327         {
4328                 o_ptr->pval = 3;
4329                 add_flag(o_ptr->art_flags, TR_STR);
4330                 add_flag(o_ptr->art_flags, TR_INT);
4331                 add_flag(o_ptr->art_flags, TR_WIS);
4332                 add_flag(o_ptr->art_flags, TR_DEX);
4333                 add_flag(o_ptr->art_flags, TR_CON);
4334                 add_flag(o_ptr->art_flags, TR_CHR);
4335         }
4336
4337         if (o_ptr->art_name) rating += 30;
4338
4339         /* Hack -- analyze ego-items */
4340         else if (o_ptr->name2)
4341         {
4342                 ego_item_type *e_ptr = &e_info[o_ptr->name2];
4343
4344                 /* Hack -- acquire "broken" flag */
4345                 if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
4346
4347                 /* Hack -- acquire "cursed" flag */
4348                 if (e_ptr->gen_flags & TRG_CURSED) o_ptr->curse_flags |= (TRC_CURSED);
4349                 if (e_ptr->gen_flags & TRG_HEAVY_CURSE) o_ptr->curse_flags |= (TRC_HEAVY_CURSE);
4350                 if (e_ptr->gen_flags & TRG_PERMA_CURSE) o_ptr->curse_flags |= (TRC_PERMA_CURSE);
4351                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4352                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4353                 if (e_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4354
4355                 if (e_ptr->gen_flags & (TRG_ONE_SUSTAIN)) one_sustain(o_ptr);
4356                 if (e_ptr->gen_flags & (TRG_XTRA_POWER)) one_ability(o_ptr);
4357                 if (e_ptr->gen_flags & (TRG_XTRA_H_RES)) one_high_resistance(o_ptr);
4358                 if (e_ptr->gen_flags & (TRG_XTRA_E_RES)) one_ele_resistance(o_ptr);
4359                 if (e_ptr->gen_flags & (TRG_XTRA_D_RES)) one_dragon_ele_resistance(o_ptr);
4360                 if (e_ptr->gen_flags & (TRG_XTRA_L_RES)) one_lordly_high_resistance(o_ptr);
4361                 if (e_ptr->gen_flags & (TRG_XTRA_RES)) one_resistance(o_ptr);
4362
4363                 /* Hack -- apply extra penalties if needed */
4364                 if (cursed_p(o_ptr) || broken_p(o_ptr))
4365                 {
4366                         /* Hack -- obtain bonuses */
4367                         if (e_ptr->max_to_h) o_ptr->to_h -= randint1(e_ptr->max_to_h);
4368                         if (e_ptr->max_to_d) o_ptr->to_d -= randint1(e_ptr->max_to_d);
4369                         if (e_ptr->max_to_a) o_ptr->to_a -= randint1(e_ptr->max_to_a);
4370
4371                         /* Hack -- obtain pval */
4372                         if (e_ptr->max_pval) o_ptr->pval -= randint1(e_ptr->max_pval);
4373                 }
4374
4375                 /* Hack -- apply extra bonuses if needed */
4376                 else
4377                 {
4378                         /* Hack -- obtain bonuses */
4379                         if (e_ptr->max_to_h)
4380                         {
4381                                 if (e_ptr->max_to_h > 127)
4382                                         o_ptr->to_h -= randint1(256-e_ptr->max_to_h);
4383                                 else o_ptr->to_h += randint1(e_ptr->max_to_h);
4384                         }
4385                         if (e_ptr->max_to_d)
4386                         {
4387                                 if (e_ptr->max_to_d > 127)
4388                                         o_ptr->to_d -= randint1(256-e_ptr->max_to_d);
4389                                 else o_ptr->to_d += randint1(e_ptr->max_to_d);
4390                         }
4391                         if (e_ptr->max_to_a)
4392                         {
4393                                 if (e_ptr->max_to_a > 127)
4394                                         o_ptr->to_a -= randint1(256-e_ptr->max_to_a);
4395                                 else o_ptr->to_a += randint1(e_ptr->max_to_a);
4396                         }
4397
4398                         /* Hack -- obtain pval */
4399                         if (e_ptr->max_pval)
4400                         {
4401                                 if ((o_ptr->name2 == EGO_HA) && (have_flag(o_ptr->art_flags, TR_BLOWS)))
4402                                 {
4403                                         o_ptr->pval++;
4404                                         if ((lev > 60) && one_in_(3) && ((o_ptr->dd*(o_ptr->ds+1)) < 15)) o_ptr->pval++;
4405                                 }
4406                                 else if (o_ptr->name2 == EGO_ATTACKS)
4407                                 {
4408                                         o_ptr->pval = randint1(e_ptr->max_pval*lev/100+1);
4409                                         if (o_ptr->pval > 3) o_ptr->pval = 3;
4410                                         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA))
4411                                                 o_ptr->pval += randint1(2);
4412                                 }
4413                                 else if (o_ptr->name2 == EGO_BAT)
4414                                 {
4415                                         o_ptr->pval = randint1(e_ptr->max_pval);
4416                                         if (o_ptr->sval == SV_ELVEN_CLOAK) o_ptr->pval += randint1(2);
4417                                 }
4418                                 else
4419                                 {
4420                                         o_ptr->pval += randint1(e_ptr->max_pval);
4421                                 }
4422                         }
4423                         if ((o_ptr->name2 == EGO_SPEED) && (lev < 50))
4424                         {
4425                                 o_ptr->pval = randint1(o_ptr->pval);
4426                         }
4427                         if ((o_ptr->tval == TV_SWORD) && (o_ptr->sval == SV_HAYABUSA) && (o_ptr->pval > 2) && (o_ptr->name2 != EGO_ATTACKS))
4428                                 o_ptr->pval = 2;
4429                 }
4430
4431                 /* Hack -- apply rating bonus */
4432                 rating += e_ptr->rating;
4433
4434                 /* Cheat -- describe the item */
4435                 if (cheat_peek) object_mention(o_ptr);
4436
4437                 /* Done */
4438                 return;
4439         }
4440
4441         /* Examine real objects */
4442         if (o_ptr->k_idx)
4443         {
4444                 object_kind *k_ptr = &k_info[o_ptr->k_idx];
4445
4446                 /* Hack -- acquire "broken" flag */
4447                 if (!get_object_cost(o_ptr)) o_ptr->ident |= (IDENT_BROKEN);
4448
4449                 /* Hack -- acquire "cursed" flag */
4450                 if (k_ptr->gen_flags & (TRG_CURSED)) o_ptr->curse_flags |= (TRC_CURSED);
4451                 if (k_ptr->gen_flags & (TRG_HEAVY_CURSE)) o_ptr->curse_flags |= TRC_HEAVY_CURSE;
4452                 if (k_ptr->gen_flags & (TRG_PERMA_CURSE)) o_ptr->curse_flags |= TRC_PERMA_CURSE;
4453                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE0)) o_ptr->curse_flags |= get_curse(0, o_ptr);
4454                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE1)) o_ptr->curse_flags |= get_curse(1, o_ptr);
4455                 if (k_ptr->gen_flags & (TRG_RANDOM_CURSE2)) o_ptr->curse_flags |= get_curse(2, o_ptr);
4456         }
4457 }
4458
4459
4460 /*
4461  * Hack -- determine if a template is "good"
4462  */
4463 static bool kind_is_good(int k_idx)
4464 {
4465         object_kind *k_ptr = &k_info[k_idx];
4466
4467         /* Analyze the item type */
4468         switch (k_ptr->tval)
4469         {
4470                 /* Armor -- Good unless damaged */
4471                 case TV_HARD_ARMOR:
4472                 case TV_SOFT_ARMOR:
4473                 case TV_DRAG_ARMOR:
4474                 case TV_SHIELD:
4475                 case TV_CLOAK:
4476                 case TV_BOOTS:
4477                 case TV_GLOVES:
4478                 case TV_HELM:
4479                 case TV_CROWN:
4480                 {
4481                         if (k_ptr->to_a < 0) return (FALSE);
4482                         return (TRUE);
4483                 }
4484
4485                 /* Weapons -- Good unless damaged */
4486                 case TV_BOW:
4487                 case TV_SWORD:
4488                 case TV_HAFTED:
4489                 case TV_POLEARM:
4490                 case TV_DIGGING:
4491                 {
4492                         if (k_ptr->to_h < 0) return (FALSE);
4493                         if (k_ptr->to_d < 0) return (FALSE);
4494                         return (TRUE);
4495                 }
4496
4497                 /* Ammo -- Arrows/Bolts are good */
4498                 case TV_BOLT:
4499                 case TV_ARROW:
4500                 {
4501                         return (TRUE);
4502                 }
4503
4504                 /* Books -- High level books are good (except Arcane books) */
4505                 case TV_LIFE_BOOK:
4506                 case TV_SORCERY_BOOK:
4507                 case TV_NATURE_BOOK:
4508                 case TV_CHAOS_BOOK:
4509                 case TV_DEATH_BOOK:
4510                 case TV_TRUMP_BOOK:
4511                 case TV_ENCHANT_BOOK:
4512                 case TV_DAEMON_BOOK:
4513                 case TV_CRUSADE_BOOK:
4514                 case TV_MUSIC_BOOK:
4515                 case TV_HISSATSU_BOOK:
4516                 {
4517                         if (k_ptr->sval >= SV_BOOK_MIN_GOOD) return (TRUE);
4518                         return (FALSE);
4519                 }
4520
4521                 /* Rings -- Rings of Speed are good */
4522                 case TV_RING:
4523                 {
4524                         if (k_ptr->sval == SV_RING_SPEED) return (TRUE);
4525                         if (k_ptr->sval == SV_RING_LORDLY) return (TRUE);
4526                         return (FALSE);
4527                 }
4528
4529                 /* Amulets -- Amulets of the Magi and Resistance are good */
4530                 case TV_AMULET:
4531                 {
4532                         if (k_ptr->sval == SV_AMULET_THE_MAGI) return (TRUE);
4533                         if (k_ptr->sval == SV_AMULET_RESISTANCE) return (TRUE);
4534                         return (FALSE);
4535                 }
4536         }
4537
4538         /* Assume not good */
4539         return (FALSE);
4540 }
4541
4542
4543 /*
4544  * Attempt to make an object (normal or good/great)
4545  *
4546  * This routine plays nasty games to generate the "special artifacts".
4547  *
4548  * This routine uses "object_level" for the "generation level".
4549  *
4550  * We assume that the given object has been "wiped".
4551  */
4552 bool make_object(object_type *j_ptr, bool good, bool great)
4553 {
4554         int prob, base;
4555         byte obj_level;
4556
4557
4558         /* Chance of "special object" */
4559         prob = (good ? 10 : 1000);
4560
4561         /* Base level for the object */
4562         base = (good ? (object_level + 10) : object_level);
4563
4564
4565         /* Generate a special object, or a normal object */
4566         if (!one_in_(prob) || !make_artifact_special(j_ptr))
4567         {
4568                 int k_idx;
4569
4570                 /* Good objects */
4571                 if (good)
4572                 {
4573                         /* Activate restriction */
4574                         get_obj_num_hook = kind_is_good;
4575
4576                         /* Prepare allocation table */
4577                         get_obj_num_prep();
4578                 }
4579
4580                 /* Pick a random object */
4581                 k_idx = get_obj_num(base);
4582
4583                 /* Good objects */
4584                 if (get_obj_num_hook)
4585                 {
4586                         /* Clear restriction */
4587                         get_obj_num_hook = NULL;
4588
4589                         /* Prepare allocation table */
4590                         get_obj_num_prep();
4591                 }
4592
4593                 /* Handle failure */
4594                 if (!k_idx) return (FALSE);
4595
4596                 /* Prepare the object */
4597                 object_prep(j_ptr, k_idx);
4598         }
4599
4600         /* Apply magic (allow artifacts) */
4601         apply_magic(j_ptr, object_level, TRUE, good, great, FALSE);
4602
4603         /* Hack -- generate multiple spikes/missiles */
4604         switch (j_ptr->tval)
4605         {
4606                 case TV_SPIKE:
4607                 case TV_SHOT:
4608                 case TV_ARROW:
4609                 case TV_BOLT:
4610                 {
4611                         if (!j_ptr->name1)
4612                                 j_ptr->number = (byte)damroll(6, 7);
4613                 }
4614         }
4615
4616         obj_level = get_object_level(j_ptr);
4617         if (artifact_p(j_ptr)) obj_level = a_info[j_ptr->name1].level;
4618
4619         /* Notice "okay" out-of-depth objects */
4620         if (!cursed_p(j_ptr) && !broken_p(j_ptr) &&
4621             (obj_level > dun_level))
4622         {
4623                 /* Rating increase */
4624                 rating += (obj_level - dun_level);
4625
4626                 /* Cheat -- peek at items */
4627                 if (cheat_peek) object_mention(j_ptr);
4628         }
4629
4630         /* Success */
4631         return (TRUE);
4632 }
4633
4634
4635 /*
4636  * Attempt to place an object (normal or good/great) at the given location.
4637  *
4638  * This routine plays nasty games to generate the "special artifacts".
4639  *
4640  * This routine uses "object_level" for the "generation level".
4641  *
4642  * This routine requires a clean floor grid destination.
4643  */
4644 void place_object(int y, int x, bool good, bool great)
4645 {
4646         s16b o_idx;
4647
4648         cave_type *c_ptr;
4649
4650         object_type forge;
4651         object_type *q_ptr;
4652
4653
4654         /* Paranoia -- check bounds */
4655         if (!in_bounds(y, x)) return;
4656
4657         /* Require clean floor space */
4658         if (!cave_clean_bold(y, x)) return;
4659
4660
4661         /* Get local object */
4662         q_ptr = &forge;
4663
4664         /* Wipe the object */
4665         object_wipe(q_ptr);
4666
4667         /* Make an object (if possible) */
4668         if (!make_object(q_ptr, good, great)) return;
4669
4670
4671         /* Make an object */
4672         o_idx = o_pop();
4673
4674         /* Success */
4675         if (o_idx)
4676         {
4677                 object_type *o_ptr;
4678
4679                 /* Acquire object */
4680                 o_ptr = &o_list[o_idx];
4681
4682                 /* Structure Copy */
4683                 object_copy(o_ptr, q_ptr);
4684
4685                 /* Location */
4686                 o_ptr->iy = y;
4687                 o_ptr->ix = x;
4688
4689                 /* Acquire grid */
4690                 c_ptr = &cave[y][x];
4691
4692                 /* Build a stack */
4693                 o_ptr->next_o_idx = c_ptr->o_idx;
4694
4695                 /* Place the object */
4696                 c_ptr->o_idx = o_idx;
4697
4698                 /* Notice */
4699                 note_spot(y, x);
4700
4701                 /* Redraw */
4702                 lite_spot(y, x);
4703         }
4704         else
4705         {
4706                 /* Hack -- Preserve artifacts */
4707                 if (q_ptr->name1)
4708                 {
4709                         a_info[q_ptr->name1].cur_num = 0;
4710                 }
4711         }
4712 }
4713
4714
4715 /*
4716  * Make a treasure object
4717  *
4718  * The location must be a legal, clean, floor grid.
4719  */
4720 bool make_gold(object_type *j_ptr)
4721 {
4722         int i;
4723
4724         s32b base;
4725
4726
4727         /* Hack -- Pick a Treasure variety */
4728         i = ((randint1(object_level + 2) + 2) / 2) - 1;
4729
4730         /* Apply "extra" magic */
4731         if (one_in_(GREAT_OBJ))
4732         {
4733                 i += randint1(object_level + 1);
4734         }
4735
4736         /* Hack -- Creeping Coins only generate "themselves" */
4737         if (coin_type) i = coin_type;
4738
4739         /* Do not create "illegal" Treasure Types */
4740         if (i >= MAX_GOLD) i = MAX_GOLD - 1;
4741
4742         /* Prepare a gold object */
4743         object_prep(j_ptr, OBJ_GOLD_LIST + i);
4744
4745         /* Hack -- Base coin cost */
4746         base = k_info[OBJ_GOLD_LIST+i].cost;
4747
4748         /* Determine how much the treasure is "worth" */
4749         j_ptr->pval = (base + (8L * randint1(base)) + randint1(8));
4750
4751         /* Success */
4752         return (TRUE);
4753 }
4754
4755
4756 /*
4757  * Places a treasure (Gold or Gems) at given location
4758  *
4759  * The location must be a legal, clean, floor grid.
4760  */
4761 void place_gold(int y, int x)
4762 {
4763         s16b o_idx;
4764
4765         cave_type *c_ptr;
4766
4767         object_type forge;
4768         object_type *q_ptr;
4769
4770
4771         /* Paranoia -- check bounds */
4772         if (!in_bounds(y, x)) return;
4773
4774         /* Require clean floor space */
4775         if (!cave_clean_bold(y, x)) return;
4776
4777
4778         /* Get local object */
4779         q_ptr = &forge;
4780
4781         /* Wipe the object */
4782         object_wipe(q_ptr);
4783
4784         /* Make some gold */
4785         if (!make_gold(q_ptr)) return;
4786
4787
4788         /* Make an object */
4789         o_idx = o_pop();
4790
4791         /* Success */
4792         if (o_idx)
4793         {
4794                 object_type *o_ptr;
4795
4796                 /* Acquire object */
4797                 o_ptr = &o_list[o_idx];
4798
4799                 /* Copy the object */
4800                 object_copy(o_ptr, q_ptr);
4801
4802                 /* Save location */
4803                 o_ptr->iy = y;
4804                 o_ptr->ix = x;
4805
4806                 /* Acquire grid */
4807                 c_ptr = &cave[y][x];
4808
4809                 /* Build a stack */
4810                 o_ptr->next_o_idx = c_ptr->o_idx;
4811
4812                 /* Place the object */
4813                 c_ptr->o_idx = o_idx;
4814
4815                 /* Notice */
4816                 note_spot(y, x);
4817
4818                 /* Redraw */
4819                 lite_spot(y, x);
4820         }
4821 }
4822
4823
4824 /*
4825  * Let an object fall to the ground at or near a location.
4826  *
4827  * The initial location is assumed to be "in_bounds()".
4828  *
4829  * This function takes a parameter "chance".  This is the percentage
4830  * chance that the item will "disappear" instead of drop.  If the object
4831  * has been thrown, then this is the chance of disappearance on contact.
4832  *
4833  * Hack -- this function uses "chance" to determine if it should produce
4834  * some form of "description" of the drop event (under the player).
4835  *
4836  * We check several locations to see if we can find a location at which
4837  * the object can combine, stack, or be placed.  Artifacts will try very
4838  * hard to be placed, including "teleporting" to a useful grid if needed.
4839  */
4840 s16b drop_near(object_type *j_ptr, int chance, int y, int x)
4841 {
4842         int i, k, d, s;
4843
4844         int bs, bn;
4845         int by, bx;
4846         int dy, dx;
4847         int ty, tx;
4848
4849         s16b o_idx = 0;
4850
4851         s16b this_o_idx, next_o_idx = 0;
4852
4853         cave_type *c_ptr;
4854
4855         char o_name[MAX_NLEN];
4856
4857         bool flag = FALSE;
4858         bool done = FALSE;
4859
4860         bool plural = FALSE;
4861
4862
4863         /* Extract plural */
4864         if (j_ptr->number != 1) plural = TRUE;
4865
4866         /* Describe object */
4867         object_desc(o_name, j_ptr, FALSE, 0);
4868
4869
4870         /* Handle normal "breakage" */
4871         if (!(j_ptr->art_name || artifact_p(j_ptr)) && (randint0(100) < chance))
4872         {
4873                 /* Message */
4874 #ifdef JP
4875                 msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
4876 #else
4877                 msg_format("The %s disappear%s.",
4878                            o_name, (plural ? "" : "s"));
4879 #endif
4880
4881
4882                 /* Debug */
4883 #ifdef JP
4884                 if (p_ptr->wizard) msg_print("(ÇË»)");
4885 #else
4886                 if (p_ptr->wizard) msg_print("(breakage)");
4887 #endif
4888
4889
4890                 /* Failure */
4891                 return (0);
4892         }
4893
4894
4895         /* Score */
4896         bs = -1;
4897
4898         /* Picker */
4899         bn = 0;
4900
4901         /* Default */
4902         by = y;
4903         bx = x;
4904
4905         /* Scan local grids */
4906         for (dy = -3; dy <= 3; dy++)
4907         {
4908                 /* Scan local grids */
4909                 for (dx = -3; dx <= 3; dx++)
4910                 {
4911                         bool comb = FALSE;
4912
4913                         /* Calculate actual distance */
4914                         d = (dy * dy) + (dx * dx);
4915
4916                         /* Ignore distant grids */
4917                         if (d > 10) continue;
4918
4919                         /* Location */
4920                         ty = y + dy;
4921                         tx = x + dx;
4922
4923                         /* Skip illegal grids */
4924                         if (!in_bounds(ty, tx)) continue;
4925
4926                         /* Require line of sight */
4927                         if (!los(y, x, ty, tx)) continue;
4928
4929                         /* Obtain grid */
4930                         c_ptr = &cave[ty][tx];
4931
4932                         /* Require floor space */
4933                         if ((c_ptr->feat != FEAT_FLOOR) &&
4934                             (c_ptr->feat != FEAT_SHAL_WATER) &&
4935                             (c_ptr->feat != FEAT_GRASS) &&
4936                             (c_ptr->feat != FEAT_DIRT) &&
4937                             (c_ptr->feat != FEAT_FLOWER) &&
4938                             (c_ptr->feat != FEAT_DEEP_GRASS) &&
4939                             (c_ptr->feat != FEAT_SHAL_LAVA) &&
4940                             (c_ptr->feat != FEAT_TREES)) continue;
4941                         if (c_ptr->info & (CAVE_OBJECT)) continue;
4942
4943                         /* No objects */
4944                         k = 0;
4945
4946                         /* Scan objects in that grid */
4947                         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
4948                         {
4949                                 object_type *o_ptr;
4950
4951                                 /* Acquire object */
4952                                 o_ptr = &o_list[this_o_idx];
4953
4954                                 /* Acquire next object */
4955                                 next_o_idx = o_ptr->next_o_idx;
4956
4957                                 /* Check for possible combination */
4958                                 if (object_similar(o_ptr, j_ptr)) comb = TRUE;
4959
4960                                 /* Count objects */
4961                                 k++;
4962                         }
4963
4964                         /* Add new object */
4965                         if (!comb) k++;
4966
4967                         /* Paranoia */
4968                         if (k > 99) continue;
4969
4970                         /* Calculate score */
4971                         s = 1000 - (d + k * 5);
4972
4973                         /* Skip bad values */
4974                         if (s < bs) continue;
4975
4976                         /* New best value */
4977                         if (s > bs) bn = 0;
4978
4979                         /* Apply the randomizer to equivalent values */
4980                         if ((++bn >= 2) && !one_in_(bn)) continue;
4981
4982                         /* Keep score */
4983                         bs = s;
4984
4985                         /* Track it */
4986                         by = ty;
4987                         bx = tx;
4988
4989                         /* Okay */
4990                         flag = TRUE;
4991                 }
4992         }
4993
4994
4995         /* Handle lack of space */
4996         if (!flag && !(artifact_p(j_ptr) || j_ptr->art_name))
4997         {
4998                 /* Message */
4999 #ifdef JP
5000                 msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
5001 #else
5002                 msg_format("The %s disappear%s.",
5003                            o_name, (plural ? "" : "s"));
5004 #endif
5005
5006
5007                 /* Debug */
5008 #ifdef JP
5009                 if (p_ptr->wizard) msg_print("(¾²¥¹¥Ú¡¼¥¹¤¬¤Ê¤¤)");
5010 #else
5011                 if (p_ptr->wizard) msg_print("(no floor space)");
5012 #endif
5013
5014
5015                 /* Failure */
5016                 return (0);
5017         }
5018
5019
5020         /* Find a grid */
5021         for (i = 0; !flag; i++)
5022         {
5023                 /* Bounce around */
5024                 if (i < 1000)
5025                 {
5026                         ty = rand_spread(by, 1);
5027                         tx = rand_spread(bx, 1);
5028                 }
5029
5030                 /* Random locations */
5031                 else
5032                 {
5033                         ty = randint0(cur_hgt);
5034                         tx = randint0(cur_wid);
5035                 }
5036
5037                 /* Grid */
5038                 c_ptr = &cave[ty][tx];
5039
5040                 /* Require floor space (or shallow terrain) -KMW- */
5041                 if ((c_ptr->feat != FEAT_FLOOR) &&
5042                     (c_ptr->feat != FEAT_SHAL_WATER) &&
5043                     (c_ptr->feat != FEAT_GRASS) &&
5044                     (c_ptr->feat != FEAT_DIRT) &&
5045                     (c_ptr->feat != FEAT_SHAL_LAVA)) continue;
5046
5047                 /* Bounce to that location */
5048                 by = ty;
5049                 bx = tx;
5050
5051                 /* Require floor space */
5052                 if (!cave_clean_bold(by, bx)) continue;
5053
5054                 /* Okay */
5055                 flag = TRUE;
5056         }
5057
5058
5059         /* Grid */
5060         c_ptr = &cave[by][bx];
5061
5062         /* Scan objects in that grid for combination */
5063         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
5064         {
5065                 object_type *o_ptr;
5066
5067                 /* Acquire object */
5068                 o_ptr = &o_list[this_o_idx];
5069
5070                 /* Acquire next object */
5071                 next_o_idx = o_ptr->next_o_idx;
5072
5073                 /* Check for combination */
5074                 if (object_similar(o_ptr, j_ptr))
5075                 {
5076                         /* Combine the items */
5077                         object_absorb(o_ptr, j_ptr);
5078
5079                         /* Success */
5080                         done = TRUE;
5081
5082                         /* Done */
5083                         break;
5084                 }
5085         }
5086
5087         /* Get new object */
5088         if (!done) o_idx = o_pop();
5089
5090         /* Failure */
5091         if (!done && !o_idx)
5092         {
5093                 /* Message */
5094 #ifdef JP
5095                 msg_format("%s¤Ï¾Ã¤¨¤¿¡£", o_name);
5096 #else
5097                 msg_format("The %s disappear%s.",
5098                            o_name, (plural ? "" : "s"));
5099 #endif
5100
5101
5102                 /* Debug */
5103 #ifdef JP
5104                 if (p_ptr->wizard) msg_print("(¥¢¥¤¥Æ¥à¤¬Â¿²á¤®¤ë)");
5105 #else
5106                 if (p_ptr->wizard) msg_print("(too many objects)");
5107 #endif
5108
5109
5110                 /* Hack -- Preserve artifacts */
5111                 if (j_ptr->name1)
5112                 {
5113                         a_info[j_ptr->name1].cur_num = 0;
5114                 }
5115
5116                 /* Failure */
5117                 return (0);
5118         }
5119
5120         /* Stack */
5121         if (!done)
5122         {
5123                 /* Structure copy */
5124                 object_copy(&o_list[o_idx], j_ptr);
5125
5126                 /* Access new object */
5127                 j_ptr = &o_list[o_idx];
5128
5129                 /* Locate */
5130                 j_ptr->iy = by;
5131                 j_ptr->ix = bx;
5132
5133                 /* No monster */
5134                 j_ptr->held_m_idx = 0;
5135
5136                 /* Build a stack */
5137                 j_ptr->next_o_idx = c_ptr->o_idx;
5138
5139                 /* Place the object */
5140                 c_ptr->o_idx = o_idx;
5141
5142                 /* Success */
5143                 done = TRUE;
5144         }
5145
5146         /* Note the spot */
5147         note_spot(by, bx);
5148
5149         /* Draw the spot */
5150         lite_spot(by, bx);
5151
5152         /* Sound */
5153         sound(SOUND_DROP);
5154
5155         /* Mega-Hack -- no message if "dropped" by player */
5156         /* Message when an object falls under the player */
5157         if (chance && (by == py) && (bx == px))
5158         {
5159 #ifdef JP
5160                 msg_print("²¿¤«¤¬Â­²¼¤Ëž¤¬¤Ã¤Æ¤­¤¿¡£");
5161 #else
5162                 msg_print("You feel something roll beneath your feet.");
5163 #endif
5164
5165         }
5166
5167         /* XXX XXX XXX */
5168
5169         /* Result */
5170         return (o_idx);
5171 }
5172
5173
5174 /*
5175  * Scatter some "great" objects near the player
5176  */
5177 void acquirement(int y1, int x1, int num, bool great, bool known)
5178 {
5179         object_type *i_ptr;
5180         object_type object_type_body;
5181
5182         /* Acquirement */
5183         while (num--)
5184         {
5185                 /* Get local object */
5186                 i_ptr = &object_type_body;
5187
5188                 /* Wipe the object */
5189                 object_wipe(i_ptr);
5190
5191                 /* Make a good (or great) object (if possible) */
5192                 if (!make_object(i_ptr, TRUE, great)) continue;
5193
5194                 if (known)
5195                 {
5196                         object_aware(i_ptr);
5197                         object_known(i_ptr);
5198                 }
5199
5200                 /* Drop the object */
5201                 (void)drop_near(i_ptr, -1, y1, x1);
5202         }
5203 }
5204
5205
5206 #define MAX_TRAPS               18
5207
5208 static int trap_num[MAX_TRAPS] =
5209 {
5210         FEAT_TRAP_TRAPDOOR,
5211         FEAT_TRAP_PIT,
5212         FEAT_TRAP_SPIKED_PIT,
5213         FEAT_TRAP_POISON_PIT,
5214         FEAT_TRAP_TY_CURSE,
5215         FEAT_TRAP_TELEPORT,
5216         FEAT_TRAP_FIRE,
5217         FEAT_TRAP_ACID,
5218         FEAT_TRAP_SLOW,
5219         FEAT_TRAP_LOSE_STR,
5220         FEAT_TRAP_LOSE_DEX,
5221         FEAT_TRAP_LOSE_CON,
5222         FEAT_TRAP_BLIND,
5223         FEAT_TRAP_CONFUSE,
5224         FEAT_TRAP_POISON,
5225         FEAT_TRAP_SLEEP,
5226         FEAT_TRAP_TRAPS,
5227         FEAT_TRAP_ALARM,
5228 };
5229
5230
5231 /*
5232  * Get random trap
5233  *
5234  * XXX XXX XXX This routine should be redone to reflect trap "level".
5235  * That is, it does not make sense to have spiked pits at 50 feet.
5236  * Actually, it is not this routine, but the "trap instantiation"
5237  * code, which should also check for "trap doors" on quest levels.
5238  */
5239 byte choose_random_trap(void)
5240 {
5241         byte feat;
5242
5243         /* Pick a trap */
5244         while (1)
5245         {
5246                 /* Hack -- pick a trap */
5247                 feat = trap_num[randint0(MAX_TRAPS)];
5248
5249                 /* Accept non-trapdoors */
5250                 if (feat != FEAT_TRAP_TRAPDOOR) break;
5251
5252                 /* Hack -- no trap doors on special levels */
5253                 if (p_ptr->inside_arena || quest_number(dun_level)) continue;
5254
5255                 /* Hack -- no trap doors on the deepest level */
5256                 if (dun_level >= d_info[dungeon_type].maxdepth) continue;
5257
5258                 break;
5259         }
5260
5261         return feat;
5262 }
5263
5264 /*
5265  * Disclose an invisible trap
5266  */
5267 void disclose_grid(int y, int x)
5268 {
5269         cave_type *c_ptr = &cave[y][x];
5270
5271         /* Paranoia */
5272         if (!c_ptr->mimic) return;
5273
5274         /* No longer hidden */
5275         c_ptr->mimic = 0;
5276
5277         /* Notice */
5278         note_spot(y, x);
5279
5280         /* Redraw */
5281         lite_spot(y, x);
5282 }
5283
5284
5285 /*
5286  * Places a random trap at the given location.
5287  *
5288  * The location must be a legal, naked, floor grid.
5289  *
5290  * Note that all traps start out as "invisible" and "untyped", and then
5291  * when they are "discovered" (by detecting them or setting them off),
5292  * the trap is "instantiated" as a visible, "typed", trap.
5293  */
5294 void place_trap(int y, int x)
5295 {
5296         cave_type *c_ptr = &cave[y][x];
5297
5298         /* Paranoia -- verify location */
5299         if (!in_bounds(y, x)) return;
5300
5301         /* Require empty, clean, floor grid */
5302         if (!cave_naked_bold(y, x)) return;
5303
5304         /* Place an invisible trap */
5305         c_ptr->mimic = c_ptr->feat;
5306         c_ptr->feat = choose_random_trap();
5307 }
5308
5309
5310 /*
5311  * Describe the charges on an item in the inventory.
5312  */
5313 void inven_item_charges(int item)
5314 {
5315         object_type *o_ptr = &inventory[item];
5316
5317         /* Require staff/wand */
5318         if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
5319
5320         /* Require known item */
5321         if (!object_known_p(o_ptr)) return;
5322
5323 #ifdef JP
5324         if (o_ptr->pval <= 0)
5325         {
5326                 msg_print("¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
5327         }
5328         else
5329         {
5330                 msg_format("¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
5331         }
5332 #else
5333         /* Multiple charges */
5334         if (o_ptr->pval != 1)
5335         {
5336                 /* Print a message */
5337                 msg_format("You have %d charges remaining.", o_ptr->pval);
5338         }
5339
5340         /* Single charge */
5341         else
5342         {
5343                 /* Print a message */
5344                 msg_format("You have %d charge remaining.", o_ptr->pval);
5345         }
5346 #endif
5347
5348 }
5349
5350
5351 /*
5352  * Describe an item in the inventory.
5353  */
5354 void inven_item_describe(int item)
5355 {
5356         object_type *o_ptr = &inventory[item];
5357         char        o_name[MAX_NLEN];
5358
5359         /* Get a description */
5360         object_desc(o_name, o_ptr, TRUE, 3);
5361
5362         /* Print a message */
5363 #ifdef JP
5364         /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤¹¤ë */
5365         if (o_ptr->number <= 0)
5366         {
5367                 /*FIRST*//*¤³¤³¤Ï¤â¤¦Ä̤é¤Ê¤¤¤«¤â */
5368                 msg_format("¤â¤¦%s¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£", o_name);
5369         }
5370         else
5371         {
5372                 /* ¥¢¥¤¥Æ¥à̾¤ò±ÑÆüÀÚ¤êÂؤ¨µ¡Ç½Âбþ */
5373                 msg_format("¤Þ¤À %s¤ò»ý¤Ã¤Æ¤¤¤ë¡£", o_name);
5374         }
5375 #else
5376         msg_format("You have %s.", o_name);
5377 #endif
5378
5379 }
5380
5381
5382 /*
5383  * Increase the "number" of an item in the inventory
5384  */
5385 void inven_item_increase(int item, int num)
5386 {
5387         object_type *o_ptr = &inventory[item];
5388
5389         /* Apply */
5390         num += o_ptr->number;
5391
5392         /* Bounds check */
5393         if (num > 255) num = 255;
5394         else if (num < 0) num = 0;
5395
5396         /* Un-apply */
5397         num -= o_ptr->number;
5398
5399         /* Change the number and weight */
5400         if (num)
5401         {
5402                 /* Add the number */
5403                 o_ptr->number += num;
5404
5405                 /* Add the weight */
5406                 p_ptr->total_weight += (num * o_ptr->weight);
5407
5408                 /* Recalculate bonuses */
5409                 p_ptr->update |= (PU_BONUS);
5410
5411                 /* Recalculate mana XXX */
5412                 p_ptr->update |= (PU_MANA);
5413
5414                 /* Combine the pack */
5415                 p_ptr->notice |= (PN_COMBINE);
5416
5417                 /* Window stuff */
5418                 p_ptr->window |= (PW_INVEN | PW_EQUIP);
5419         }
5420 }
5421
5422
5423 /*
5424  * Erase an inventory slot if it has no more items
5425  */
5426 void inven_item_optimize(int item)
5427 {
5428         object_type *o_ptr = &inventory[item];
5429
5430         /* Only optimize real items */
5431         if (!o_ptr->k_idx) return;
5432
5433         /* Only optimize empty items */
5434         if (o_ptr->number) return;
5435
5436         /* The item is in the pack */
5437         if (item < INVEN_RARM)
5438         {
5439                 int i;
5440
5441                 /* One less item */
5442                 inven_cnt--;
5443
5444                 /* Slide everything down */
5445                 for (i = item; i < INVEN_PACK; i++)
5446                 {
5447                         /* Structure copy */
5448                         inventory[i] = inventory[i+1];
5449                 }
5450
5451                 /* Erase the "final" slot */
5452                 object_wipe(&inventory[i]);
5453
5454                 /* Window stuff */
5455                 p_ptr->window |= (PW_INVEN);
5456         }
5457
5458         /* The item is being wielded */
5459         else
5460         {
5461                 /* One less item */
5462                 equip_cnt--;
5463
5464                 /* Erase the empty slot */
5465                 object_wipe(&inventory[item]);
5466
5467                 /* Recalculate bonuses */
5468                 p_ptr->update |= (PU_BONUS);
5469
5470                 /* Recalculate torch */
5471                 p_ptr->update |= (PU_TORCH);
5472
5473                 /* Recalculate mana XXX */
5474                 p_ptr->update |= (PU_MANA);
5475
5476                 /* Window stuff */
5477                 p_ptr->window |= (PW_EQUIP);
5478         }
5479
5480         /* Window stuff */
5481         p_ptr->window |= (PW_SPELL);
5482 }
5483
5484
5485 /*
5486  * Describe the charges on an item on the floor.
5487  */
5488 void floor_item_charges(int item)
5489 {
5490         object_type *o_ptr = &o_list[item];
5491
5492         /* Require staff/wand */
5493         if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
5494
5495         /* Require known item */
5496         if (!object_known_p(o_ptr)) return;
5497
5498 #ifdef JP
5499         if (o_ptr->pval <= 0)
5500         {
5501                 msg_print("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤â¤¦ËâÎϤ¬»Ä¤Ã¤Æ¤¤¤Ê¤¤¡£");
5502         }
5503         else
5504         {
5505                 msg_format("¤³¤Î¾²¾å¤Î¥¢¥¤¥Æ¥à¤Ï¡¢¤¢¤È %d ²óʬ¤ÎËâÎϤ¬»Ä¤Ã¤Æ¤¤¤ë¡£", o_ptr->pval);
5506         }
5507 #else
5508         /* Multiple charges */
5509         if (o_ptr->pval != 1)
5510         {
5511                 /* Print a message */
5512                 msg_format("There are %d charges remaining.", o_ptr->pval);
5513         }
5514
5515         /* Single charge */
5516         else
5517         {
5518                 /* Print a message */
5519                 msg_format("There is %d charge remaining.", o_ptr->pval);
5520         }
5521 #endif
5522
5523 }
5524
5525
5526 /*
5527  * Describe an item in the inventory.
5528  */
5529 void floor_item_describe(int item)
5530 {
5531         object_type *o_ptr = &o_list[item];
5532         char        o_name[MAX_NLEN];
5533
5534         /* Get a description */
5535         object_desc(o_name, o_ptr, TRUE, 3);
5536
5537         /* Print a message */
5538 #ifdef JP
5539         /* "no more" ¤Î¾ì¹ç¤Ï¤³¤Á¤é¤Çɽ¼¨¤òʬ¤±¤ë */
5540         if (o_ptr->number <= 0)
5541         {
5542                 msg_format("¾²¾å¤Ë¤Ï¡¢¤â¤¦%s¤Ï¤Ê¤¤¡£", o_name);
5543         }
5544         else
5545         {
5546                 msg_format("¾²¾å¤Ë¤Ï¡¢¤Þ¤À %s¤¬¤¢¤ë¡£", o_name);
5547         }
5548 #else
5549         msg_format("You see %s.", o_name);
5550 #endif
5551
5552 }
5553
5554
5555 /*
5556  * Increase the "number" of an item on the floor
5557  */
5558 void floor_item_increase(int item, int num)
5559 {
5560         object_type *o_ptr = &o_list[item];
5561
5562         /* Apply */
5563         num += o_ptr->number;
5564
5565         /* Bounds check */
5566         if (num > 255) num = 255;
5567         else if (num < 0) num = 0;
5568
5569         /* Un-apply */
5570         num -= o_ptr->number;
5571
5572         /* Change the number */
5573         o_ptr->number += num;
5574 }
5575
5576
5577 /*
5578  * Optimize an item on the floor (destroy "empty" items)
5579  */
5580 void floor_item_optimize(int item)
5581 {
5582         object_type *o_ptr = &o_list[item];
5583
5584         /* Paranoia -- be sure it exists */
5585         if (!o_ptr->k_idx) return;
5586
5587         /* Only optimize empty items */
5588         if (o_ptr->number) return;
5589
5590         /* Delete the object */
5591         delete_object_idx(item);
5592 }
5593
5594
5595 /*
5596  * Check if we have space for an item in the pack without overflow
5597  */
5598 bool inven_carry_okay(object_type *o_ptr)
5599 {
5600         int j;
5601
5602         /* Empty slot? */
5603         if (inven_cnt < INVEN_PACK) return (TRUE);
5604
5605         /* Similar slot? */
5606         for (j = 0; j < INVEN_PACK; j++)
5607         {
5608                 object_type *j_ptr = &inventory[j];
5609
5610                 /* Skip non-objects */
5611                 if (!j_ptr->k_idx) continue;
5612
5613                 /* Check if the two items can be combined */
5614                 if (object_similar(j_ptr, o_ptr)) return (TRUE);
5615         }
5616
5617         /* Nope */
5618         return (FALSE);
5619 }
5620
5621
5622 /*
5623  * Add an item to the players inventory, and return the slot used.
5624  *
5625  * If the new item can combine with an existing item in the inventory,
5626  * it will do so, using "object_similar()" and "object_absorb()", else,
5627  * the item will be placed into the "proper" location in the inventory.
5628  *
5629  * This function can be used to "over-fill" the player's pack, but only
5630  * once, and such an action must trigger the "overflow" code immediately.
5631  * Note that when the pack is being "over-filled", the new item must be
5632  * placed into the "overflow" slot, and the "overflow" must take place
5633  * before the pack is reordered, but (optionally) after the pack is
5634  * combined.  This may be tricky.  See "dungeon.c" for info.
5635  *
5636  * Note that this code must remove any location/stack information
5637  * from the object once it is placed into the inventory.
5638  */
5639 s16b inven_carry(object_type *o_ptr)
5640 {
5641         int i, j, k;
5642         int n = -1;
5643
5644         object_type *j_ptr;
5645
5646
5647         /* Check for combining */
5648         for (j = 0; j < INVEN_PACK; j++)
5649         {
5650                 j_ptr = &inventory[j];
5651
5652                 /* Skip non-objects */
5653                 if (!j_ptr->k_idx) continue;
5654
5655                 /* Hack -- track last item */
5656                 n = j;
5657
5658                 /* Check if the two items can be combined */
5659                 if (object_similar(j_ptr, o_ptr))
5660                 {
5661                         /* Combine the items */
5662                         object_absorb(j_ptr, o_ptr);
5663
5664                         /* Increase the weight */
5665                         p_ptr->total_weight += (o_ptr->number * o_ptr->weight);
5666
5667                         /* Recalculate bonuses */
5668                         p_ptr->update |= (PU_BONUS);
5669
5670                         /* Window stuff */
5671                         p_ptr->window |= (PW_INVEN);
5672
5673                         /* Success */
5674                         return (j);
5675                 }
5676         }
5677
5678
5679         /* Paranoia */
5680         if (inven_cnt > INVEN_PACK) return (-1);
5681
5682         /* Find an empty slot */
5683         for (j = 0; j <= INVEN_PACK; j++)
5684         {
5685                 j_ptr = &inventory[j];
5686
5687                 /* Use it if found */
5688                 if (!j_ptr->k_idx) break;
5689         }
5690
5691         /* Use that slot */
5692         i = j;
5693
5694
5695         /* Reorder the pack */
5696         if (i < INVEN_PACK)
5697         {
5698                 s32b o_value, j_value;
5699
5700                 /* Get the "value" of the item */
5701                 o_value = object_value(o_ptr);
5702
5703                 /* Scan every occupied slot */
5704                 for (j = 0; j < INVEN_PACK; j++)
5705                 {
5706                         j_ptr = &inventory[j];
5707
5708                         /* Use empty slots */
5709                         if (!j_ptr->k_idx) break;
5710
5711                         /* Hack -- readable books always come first */
5712                         if ((o_ptr->tval == REALM1_BOOK) &&
5713                             (j_ptr->tval != REALM1_BOOK)) break;
5714                         if ((j_ptr->tval == REALM1_BOOK) &&
5715                             (o_ptr->tval != REALM1_BOOK)) continue;
5716
5717                         if ((o_ptr->tval == REALM2_BOOK) &&
5718                             (j_ptr->tval != REALM2_BOOK)) break;
5719                         if ((j_ptr->tval == REALM2_BOOK) &&
5720                             (o_ptr->tval != REALM2_BOOK)) continue;
5721
5722                         /* Objects sort by decreasing type */
5723                         if (o_ptr->tval > j_ptr->tval) break;
5724                         if (o_ptr->tval < j_ptr->tval) continue;
5725
5726                         /* Non-aware (flavored) items always come last */
5727                         if (!object_aware_p(o_ptr)) continue;
5728                         if (!object_aware_p(j_ptr)) break;
5729
5730                         /* Objects sort by increasing sval */
5731                         if (o_ptr->sval < j_ptr->sval) break;
5732                         if (o_ptr->sval > j_ptr->sval) continue;
5733
5734                         /* Unidentified objects always come last */
5735                         if (!object_known_p(o_ptr)) continue;
5736                         if (!object_known_p(j_ptr)) break;
5737
5738                         /* Hack:  otherwise identical rods sort by
5739                         increasing recharge time --dsb */
5740                         if (o_ptr->tval == TV_ROD)
5741                         {
5742                                 if (o_ptr->pval < j_ptr->pval) break;
5743                                 if (o_ptr->pval > j_ptr->pval) continue;
5744                         }
5745
5746                         /* Determine the "value" of the pack item */
5747                         j_value = object_value(j_ptr);
5748
5749                         /* Objects sort by decreasing value */
5750                         if (o_value > j_value) break;
5751                         if (o_value < j_value) continue;
5752                 }
5753
5754                 /* Use that slot */
5755                 i = j;
5756
5757                 /* Slide objects */
5758                 for (k = n; k >= i; k--)
5759                 {
5760                         /* Hack -- Slide the item */
5761                         object_copy(&inventory[k+1], &inventory[k]);
5762                 }
5763
5764                 /* Wipe the empty slot */
5765                 object_wipe(&inventory[i]);
5766         }
5767
5768
5769         /* Copy the item */
5770         object_copy(&inventory[i], o_ptr);
5771
5772         /* Access new object */
5773         j_ptr = &inventory[i];
5774
5775         /* Forget stack */
5776         j_ptr->next_o_idx = 0;
5777
5778         /* Forget monster */
5779         j_ptr->held_m_idx = 0;
5780
5781         /* Forget location */
5782         j_ptr->iy = j_ptr->ix = 0;
5783
5784         /* No longer marked */
5785         j_ptr->marked = 0;
5786
5787         /* Increase the weight */
5788         p_ptr->total_weight += (j_ptr->number * j_ptr->weight);
5789
5790         /* Count the items */
5791         inven_cnt++;
5792
5793         /* Recalculate bonuses */
5794         p_ptr->update |= (PU_BONUS);
5795
5796         /* Combine and Reorder pack */
5797         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
5798
5799         /* Window stuff */
5800         p_ptr->window |= (PW_INVEN);
5801
5802         /* Return the slot */
5803         return (i);
5804 }
5805
5806
5807 /*
5808  * Take off (some of) a non-cursed equipment item
5809  *
5810  * Note that only one item at a time can be wielded per slot.
5811  *
5812  * Note that taking off an item when "full" may cause that item
5813  * to fall to the ground.
5814  *
5815  * Return the inventory slot into which the item is placed.
5816  */
5817 s16b inven_takeoff(int item, int amt)
5818 {
5819         int slot;
5820
5821         object_type forge;
5822         object_type *q_ptr;
5823
5824         object_type *o_ptr;
5825
5826         cptr act;
5827
5828         char o_name[MAX_NLEN];
5829
5830
5831         /* Get the item to take off */
5832         o_ptr = &inventory[item];
5833
5834         /* Paranoia */
5835         if (amt <= 0) return (-1);
5836
5837         /* Verify */
5838         if (amt > o_ptr->number) amt = o_ptr->number;
5839
5840         /* Get local object */
5841         q_ptr = &forge;
5842
5843         /* Obtain a local object */
5844         object_copy(q_ptr, o_ptr);
5845
5846         /* Modify quantity */
5847         q_ptr->number = amt;
5848
5849         /* Describe the object */
5850         object_desc(o_name, q_ptr, TRUE, 3);
5851
5852         /* Took off weapon */
5853         if (item == INVEN_RARM)
5854         {
5855 #ifdef JP
5856                 act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
5857 #else
5858                 act = "You were wielding";
5859 #endif
5860
5861         }
5862
5863         /* Took off bow */
5864         else if (item == INVEN_BOW)
5865         {
5866 #ifdef JP
5867                 act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
5868 #else
5869                 act = "You were holding";
5870 #endif
5871
5872         }
5873
5874         /* Took off light */
5875         else if (item == INVEN_LITE)
5876         {
5877 #ifdef JP
5878                 act = "¤ò¸÷¸»¤«¤é¤Ï¤º¤·¤¿";
5879 #else
5880                 act = "You were holding";
5881 #endif
5882
5883         }
5884
5885         /* Took off something */
5886         else
5887         {
5888 #ifdef JP
5889                 act = "¤òÁõÈ÷¤«¤é¤Ï¤º¤·¤¿";
5890 #else
5891                 act = "You were wearing";
5892 #endif
5893
5894         }
5895
5896         /* Modify, Optimize */
5897         inven_item_increase(item, -amt);
5898         inven_item_optimize(item);
5899
5900         /* Carry the object */
5901         slot = inven_carry(q_ptr);
5902
5903         /* Message */
5904 #ifdef JP
5905         msg_format("%s(%c)%s¡£", o_name, index_to_label(slot), act);
5906 #else
5907         msg_format("%s %s (%c).", act, o_name, index_to_label(slot));
5908 #endif
5909
5910
5911         /* Return slot */
5912         return (slot);
5913 }
5914
5915
5916 /*
5917  * Drop (some of) a non-cursed inventory/equipment item
5918  *
5919  * The object will be dropped "near" the current location
5920  */
5921 void inven_drop(int item, int amt)
5922 {
5923         object_type forge;
5924         object_type *q_ptr;
5925
5926         object_type *o_ptr;
5927
5928         char o_name[MAX_NLEN];
5929
5930
5931         /* Access original object */
5932         o_ptr = &inventory[item];
5933
5934         /* Error check */
5935         if (amt <= 0) return;
5936
5937         /* Not too many */
5938         if (amt > o_ptr->number) amt = o_ptr->number;
5939
5940
5941         /* Take off equipment */
5942         if (item >= INVEN_RARM)
5943         {
5944                 /* Take off first */
5945                 item = inven_takeoff(item, amt);
5946
5947                 /* Access original object */
5948                 o_ptr = &inventory[item];
5949         }
5950
5951
5952         /* Get local object */
5953         q_ptr = &forge;
5954
5955         /* Obtain local object */
5956         object_copy(q_ptr, o_ptr);
5957
5958         /* Distribute charges of wands or rods */
5959         distribute_charges(o_ptr, q_ptr, amt);
5960
5961         /* Modify quantity */
5962         q_ptr->number = amt;
5963
5964         /* Describe local object */
5965         object_desc(o_name, q_ptr, TRUE, 3);
5966
5967         /* Message */
5968 #ifdef JP
5969         msg_format("%s(%c)¤òÍî¤È¤·¤¿¡£", o_name, index_to_label(item));
5970 #else
5971         msg_format("You drop %s (%c).", o_name, index_to_label(item));
5972 #endif
5973
5974
5975         /* Drop it near the player */
5976         (void)drop_near(q_ptr, 0, py, px);
5977
5978         /* Modify, Describe, Optimize */
5979         inven_item_increase(item, -amt);
5980         inven_item_describe(item);
5981         inven_item_optimize(item);
5982 }
5983
5984
5985 /*
5986  * Combine items in the pack
5987  *
5988  * Note special handling of the "overflow" slot
5989  */
5990 void combine_pack(void)
5991 {
5992         int             i, j, k;
5993         object_type     *o_ptr;
5994         object_type     *j_ptr;
5995         bool            flag = FALSE;
5996
5997
5998         /* Combine the pack (backwards) */
5999         for (i = INVEN_PACK; i > 0; i--)
6000         {
6001                 /* Get the item */
6002                 o_ptr = &inventory[i];
6003
6004                 /* Skip empty items */
6005                 if (!o_ptr->k_idx) continue;
6006
6007                 /* Scan the items above that item */
6008                 for (j = 0; j < i; j++)
6009                 {
6010                         /* Get the item */
6011                         j_ptr = &inventory[j];
6012
6013                         /* Skip empty items */
6014                         if (!j_ptr->k_idx) continue;
6015
6016                         /* Can we (partialy) drop "o_ptr" onto "j_ptr"? */
6017                         if (object_similar_part(j_ptr, o_ptr)
6018                             && j_ptr->number < MAX_STACK_SIZE-1)
6019                         {
6020                                 if (o_ptr->number + j_ptr->number < MAX_STACK_SIZE)
6021                                 {
6022                                         /* Take note */
6023                                         flag = TRUE;
6024
6025                                         /* Add together the item counts */
6026                                         object_absorb(j_ptr, o_ptr);
6027
6028                                         /* One object is gone */
6029                                         inven_cnt--;
6030
6031                                         /* Slide everything down */
6032                                         for (k = i; k < INVEN_PACK; k++)
6033                                         {
6034                                                 /* Structure copy */
6035                                                 inventory[k] = inventory[k+1];
6036                                         }
6037                                         
6038                                         /* Erase the "final" slot */
6039                                         object_wipe(&inventory[k]);
6040                                 }
6041                                 else
6042                                 {
6043                                         int remain = j_ptr->number + o_ptr->number - 99;
6044                                         
6045                                         o_ptr->number -= remain;
6046                                         
6047                                         /* Add together the item counts */
6048                                         object_absorb(j_ptr, o_ptr);
6049
6050                                         o_ptr->number = remain;
6051                                         
6052                                 }
6053                                 /* Window stuff */
6054                                 p_ptr->window |= (PW_INVEN);
6055                                 
6056                                 /* Done */
6057                                 break;
6058                         }
6059                 }
6060         }
6061
6062         /* Message */
6063 #ifdef JP
6064         if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤ò¤Þ¤È¤áľ¤·¤¿¡£");
6065 #else
6066         if (flag) msg_print("You combine some items in your pack.");
6067 #endif
6068
6069 }
6070
6071
6072 /*
6073  * Reorder items in the pack
6074  *
6075  * Note special handling of the "overflow" slot
6076  */
6077 void reorder_pack(void)
6078 {
6079         int             i, j, k;
6080         s32b            o_value;
6081         s32b            j_value;
6082         object_type     forge;
6083         object_type     *q_ptr;
6084         object_type     *j_ptr;
6085         object_type     *o_ptr;
6086         bool            flag = FALSE;
6087
6088
6089         /* Re-order the pack (forwards) */
6090         for (i = 0; i < INVEN_PACK; i++)
6091         {
6092                 /* Mega-Hack -- allow "proper" over-flow */
6093                 if ((i == INVEN_PACK) && (inven_cnt == INVEN_PACK)) break;
6094
6095                 /* Get the item */
6096                 o_ptr = &inventory[i];
6097
6098                 /* Skip empty slots */
6099                 if (!o_ptr->k_idx) continue;
6100
6101                 /* Get the "value" of the item */
6102                 o_value = object_value(o_ptr);
6103
6104                 /* Scan every occupied slot */
6105                 for (j = 0; j < INVEN_PACK; j++)
6106                 {
6107                         /* Get the item already there */
6108                         j_ptr = &inventory[j];
6109
6110                         /* Use empty slots */
6111                         if (!j_ptr->k_idx) break;
6112
6113                         /* Hack -- readable books always come first */
6114                         if ((o_ptr->tval == REALM1_BOOK) &&
6115                             (j_ptr->tval != REALM1_BOOK)) break;
6116                         if ((j_ptr->tval == REALM1_BOOK) &&
6117                             (o_ptr->tval != REALM1_BOOK)) continue;
6118
6119                         if ((o_ptr->tval == REALM2_BOOK) &&
6120                             (j_ptr->tval != REALM2_BOOK)) break;
6121                         if ((j_ptr->tval == REALM2_BOOK) &&
6122                             (o_ptr->tval != REALM2_BOOK)) continue;
6123
6124                         /* Objects sort by decreasing type */
6125                         if (o_ptr->tval > j_ptr->tval) break;
6126                         if (o_ptr->tval < j_ptr->tval) continue;
6127
6128                         /* Non-aware (flavored) items always come last */
6129                         if (!object_aware_p(o_ptr)) continue;
6130                         if (!object_aware_p(j_ptr)) break;
6131
6132                         /* Objects sort by increasing sval */
6133                         if (o_ptr->sval < j_ptr->sval) break;
6134                         if (o_ptr->sval > j_ptr->sval) continue;
6135
6136                         /* Unidentified objects always come last */
6137                         if (!object_known_p(o_ptr)) continue;
6138                         if (!object_known_p(j_ptr)) break;
6139
6140                         /* Hack:  otherwise identical rods sort by
6141                         increasing recharge time --dsb */
6142                         if (o_ptr->tval == TV_ROD)
6143                         {
6144                                 if (o_ptr->pval < j_ptr->pval) break;
6145                                 if (o_ptr->pval > j_ptr->pval) continue;
6146                         }
6147
6148                         /* Determine the "value" of the pack item */
6149                         j_value = object_value(j_ptr);
6150
6151
6152
6153                         /* Objects sort by decreasing value */
6154                         if (o_value > j_value) break;
6155                         if (o_value < j_value) continue;
6156                 }
6157
6158                 /* Never move down */
6159                 if (j >= i) continue;
6160
6161                 /* Take note */
6162                 flag = TRUE;
6163
6164                 /* Get local object */
6165                 q_ptr = &forge;
6166
6167                 /* Save a copy of the moving item */
6168                 object_copy(q_ptr, &inventory[i]);
6169
6170                 /* Slide the objects */
6171                 for (k = i; k > j; k--)
6172                 {
6173                         /* Slide the item */
6174                         object_copy(&inventory[k], &inventory[k-1]);
6175                 }
6176
6177                 /* Insert the moving item */
6178                 object_copy(&inventory[j], q_ptr);
6179
6180                 /* Window stuff */
6181                 p_ptr->window |= (PW_INVEN);
6182         }
6183
6184         /* Message */
6185 #ifdef JP
6186         if (flag) msg_print("¥¶¥Ã¥¯¤ÎÃæ¤Î¥¢¥¤¥Æ¥à¤òÊ¤Ùľ¤·¤¿¡£");
6187 #else
6188         if (flag) msg_print("You reorder some items in your pack.");
6189 #endif
6190
6191 }
6192
6193
6194 /*
6195  * Hack -- display an object kind in the current window
6196  *
6197  * Include list of usable spells for readible books
6198  */
6199 void display_koff(int k_idx)
6200 {
6201         int y;
6202
6203         object_type forge;
6204         object_type *q_ptr;
6205
6206         char o_name[MAX_NLEN];
6207
6208
6209         /* Erase the window */
6210         for (y = 0; y < Term->hgt; y++)
6211         {
6212                 /* Erase the line */
6213                 Term_erase(0, y, 255);
6214         }
6215
6216         /* No info */
6217         if (!k_idx) return;
6218
6219         /* Get local object */
6220         q_ptr = &forge;
6221
6222         /* Prepare the object */
6223         object_prep(q_ptr, k_idx);
6224
6225         /* Describe */
6226         object_desc_store(o_name, q_ptr, FALSE, 0);
6227
6228         /* Mention the object name */
6229         Term_putstr(0, 0, -1, TERM_WHITE, o_name);
6230
6231         /* Warriors are illiterate */
6232         if (!(p_ptr->realm1 || p_ptr->realm2)) return;
6233
6234         /* Display spells in readible books */
6235         if ((q_ptr->tval == REALM1_BOOK) ||
6236             (q_ptr->tval == REALM2_BOOK))
6237         {
6238                 int     sval;
6239                 int     spell = -1;
6240                 int     num = 0;
6241                 byte    spells[64];
6242
6243
6244                 /* Access the item's sval */
6245                 sval = q_ptr->sval;
6246
6247                 /* Extract spells */
6248                 for (spell = 0; spell < 32; spell++)
6249                 {
6250                         /* Check for this spell */
6251                         if (fake_spell_flags[sval] & (1L << spell))
6252                         {
6253                                 /* Collect this spell */
6254                                 spells[num++] = spell;
6255                         }
6256                 }
6257
6258                 /* Print spells */
6259                 print_spells(0, spells, num, 2, 0,
6260                     (q_ptr->tval == REALM1_BOOK ? p_ptr->realm1 : p_ptr->realm2));
6261         }
6262 }
6263
6264 /* Choose one of items that have warning flag */
6265 object_type *choose_warning_item(void)
6266 {
6267         int i;
6268         int choices[INVEN_TOTAL-INVEN_RARM];
6269         int number = 0;
6270
6271         /* Paranoia -- Player has no warning-item */
6272         if (!p_ptr->warning) return (NULL);
6273
6274         /* Search Inventry */
6275         for (i = INVEN_RARM; i < INVEN_TOTAL; i++)
6276         {
6277                 u32b flgs[TR_FLAG_SIZE];
6278                 object_type *o_ptr = &inventory[i];
6279
6280                 object_flags(o_ptr, flgs);
6281                 if (have_flag(flgs, TR_WARNING))
6282                 {
6283                         choices[number] = i;
6284                         number++;
6285                 }
6286         }
6287
6288         /* Choice one of them */
6289         return (&inventory[choices[randint0(number)]]);
6290 }
6291
6292 /* Examine the grid (xx,yy) and warn the player if there are any danger */
6293 bool process_frakir(int xx, int yy)
6294 {
6295         int mx,my;
6296         cave_type *c_ptr;
6297         char o_name[MAX_NLEN];
6298
6299 #define FRAKIR_AWARE_RANGE 12
6300         int dam_max = 0;
6301         static int old_damage = 0;
6302
6303         for (mx = xx-FRAKIR_AWARE_RANGE; mx < xx+FRAKIR_AWARE_RANGE+1; mx++)
6304         {
6305                 for (my = yy-FRAKIR_AWARE_RANGE; my < yy+FRAKIR_AWARE_RANGE+1; my++)
6306                 {
6307                         int dam_max0=0;
6308                         monster_type *m_ptr;
6309                         monster_race *r_ptr;
6310                         u32b f4, f5, f6;
6311                         int rlev;
6312
6313                         if (!in_bounds(my,mx) || (distance(my,mx,yy,xx)>FRAKIR_AWARE_RANGE)) continue;
6314
6315                         c_ptr = &cave[my][mx];
6316
6317                         if (!c_ptr->m_idx) continue;
6318
6319                         m_ptr = &m_list[c_ptr->m_idx];
6320                         r_ptr = &r_info[m_ptr->r_idx];
6321
6322                         f4 = r_ptr->flags4;
6323                         f5 = r_ptr->flags5;
6324                         f6 = r_ptr->flags6;
6325
6326                         rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
6327
6328                         if (m_ptr->csleep) continue;
6329                         if (is_pet(m_ptr)) continue;
6330
6331                         /* Monster spells (only powerful ones)*/
6332                         if(projectable(my,mx,yy,xx))
6333                         {
6334
6335 #define DAMCALC(f,val,max,im,vln,res,resx,resy,op,opx,opy,dmax) \
6336            if (f){ int dam = (val)>(max)? (max):(val); \
6337            if (im) dam=0; \
6338            if (vln) dam *= 2; \
6339            if (res) {dam = (dam * resx) / resy;} \
6340            if (op) {dam = (dam * opx) / opy;} \
6341            if (dam>dmax) dmax = dam; \
6342            }
6343
6344                                 DAMCALC(f4 & (RF4_BR_FIRE), m_ptr->hp / 3, 1600, 
6345                                         p_ptr->immune_fire, p_ptr->muta3 & MUT3_VULN_ELEM,
6346                                         p_ptr->resist_fire, 1, 3,
6347                                         p_ptr->oppose_fire, 1, 3, dam_max0);
6348
6349                                 DAMCALC(f4 & (RF4_BR_COLD), m_ptr->hp / 3, 1600, 
6350                                         p_ptr->immune_cold, p_ptr->muta3 & MUT3_VULN_ELEM,
6351                                         p_ptr->resist_cold, 1, 3,
6352                                         p_ptr->oppose_cold, 1, 3, dam_max0);
6353
6354                                 DAMCALC(f4 & (RF4_BR_ELEC), m_ptr->hp / 3, 1600, 
6355                                         p_ptr->immune_elec, p_ptr->muta3 & MUT3_VULN_ELEM,
6356                                         p_ptr->resist_elec, 1, 3,
6357                                         p_ptr->oppose_elec, 1, 3, dam_max0);
6358
6359                                 DAMCALC(f4 & (RF4_BR_ACID), m_ptr->hp / 3, 1600, 
6360                                         p_ptr->immune_acid, p_ptr->muta3 & MUT3_VULN_ELEM,
6361                                         p_ptr->resist_acid, 1, 3,
6362                                         p_ptr->oppose_acid, 1, 3, dam_max0);
6363
6364                                 DAMCALC(f4 & (RF4_BR_POIS), m_ptr->hp / 3, 800,
6365                                         FALSE , FALSE,
6366                                         p_ptr->resist_pois, 1, 3,
6367                                         p_ptr->oppose_pois, 1, 3, dam_max0);
6368
6369
6370                                 DAMCALC(f4 & (RF4_BR_NETH), m_ptr->hp / 6, 550, FALSE , FALSE,
6371                                         p_ptr->resist_neth, 6, 9, FALSE, 1, 1, dam_max0);
6372
6373                                 DAMCALC(f4 & (RF4_BR_LITE), m_ptr->hp / 6, 400, FALSE , FALSE,
6374                                         p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
6375
6376                                 DAMCALC(f4 & (RF4_BR_DARK), m_ptr->hp / 6, 400, FALSE , FALSE,
6377                                         p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
6378
6379                                 DAMCALC(f4 & (RF4_BR_CONF), m_ptr->hp / 6, 450, FALSE , FALSE,
6380                                         p_ptr->resist_conf, 5, 9, FALSE, 1, 1, dam_max0);
6381
6382                                 DAMCALC(f4 & (RF4_BR_SOUN), m_ptr->hp / 6, 450, FALSE , FALSE,
6383                                         p_ptr->resist_sound, 5, 9, FALSE, 1, 1, dam_max0);
6384
6385                                 DAMCALC(f4 & (RF4_BR_CHAO), m_ptr->hp / 6, 600, FALSE , FALSE,
6386                                         p_ptr->resist_chaos, 6, 9, FALSE, 1, 1, dam_max0);
6387
6388                                 DAMCALC(f4 & (RF4_BR_DISE), m_ptr->hp / 6, 500, FALSE , FALSE,
6389                                         p_ptr->resist_disen, 6, 9, FALSE, 1, 1, dam_max0);
6390
6391                                 DAMCALC(f4 & (RF4_BR_NEXU), m_ptr->hp / 3, 250, FALSE , FALSE,
6392                                         p_ptr->resist_nexus, 6, 9, FALSE, 1, 1, dam_max0);
6393
6394                                 DAMCALC(f4 & (RF4_BR_TIME), m_ptr->hp / 3, 150, FALSE , FALSE,
6395                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6396
6397                                 DAMCALC(f4 & (RF4_BR_INER), m_ptr->hp / 6, 200, FALSE , FALSE,
6398                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6399
6400                                 DAMCALC(f4 & (RF4_BR_GRAV), m_ptr->hp / 3, 200, FALSE , FALSE,
6401                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6402
6403                                 DAMCALC(f4 & (RF4_BR_SHAR), m_ptr->hp / 6, 500, FALSE , FALSE,
6404                                         p_ptr->resist_shard, 6, 9, FALSE, 1, 1, dam_max0);
6405
6406                                 DAMCALC(f4 & (RF4_BR_PLAS), m_ptr->hp / 6, 150, FALSE , FALSE,
6407                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6408
6409                                 DAMCALC(f4 & (RF4_BR_WALL), m_ptr->hp / 6, 200, FALSE , FALSE,
6410                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6411
6412                                 DAMCALC(f4 & (RF4_BR_MANA), m_ptr->hp / 3, 250, FALSE , FALSE,
6413                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6414
6415                                 DAMCALC(f4 & (RF4_BR_NUKE), m_ptr->hp / 3, 800, FALSE , FALSE,
6416                                         p_ptr->resist_pois, 2, 5, 
6417                                         p_ptr->oppose_pois, 2, 5, dam_max0);
6418
6419                                 DAMCALC(f4 & (RF4_BR_DISI), m_ptr->hp / 3, 300, FALSE , FALSE,
6420                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6421
6422
6423                                 DAMCALC(f4 & (RF4_ROCKET), m_ptr->hp / 4, 800, FALSE , FALSE,
6424                                         p_ptr->resist_shard, 1, 2, FALSE, 1, 1, dam_max0);
6425
6426                                 DAMCALC(f5 & (RF5_BA_MANA), rlev*4 + 150, 9999, FALSE , FALSE,
6427                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6428
6429                                 DAMCALC(f5 & (RF5_BA_DARK), rlev*4 + 150, 9999, FALSE , FALSE,
6430                                         p_ptr->resist_dark, 4, 9, FALSE, 1, 1, dam_max0);
6431
6432                                 DAMCALC(f5 & (RF5_BA_LITE), rlev*4 + 150, 9999, FALSE , FALSE,
6433                                         p_ptr->resist_lite, 4, 9, FALSE, 1, 1, dam_max0);
6434
6435
6436                                 DAMCALC(f6 & (RF6_HAND_DOOM), p_ptr->chp*6/10, 9999, FALSE , FALSE,
6437                                         FALSE, 1, 1, FALSE, 1, 1, dam_max0);
6438
6439                         }
6440
6441                         /* Monster melee attacks */
6442                         if(mx <= xx+1 && mx >= xx-1 && my <=yy+1 && my >= yy-1)
6443                         {
6444                                 int m;
6445                                 int dam_melee=0;
6446                                 for (m = 0; m < 4; m++)
6447                                 {
6448                                         int d1, d2;
6449
6450                                         /* Skip non-attacks */
6451                                         if (!r_ptr->blow[m].method) continue;
6452
6453                                         /* Extract the attack info */
6454                                         d1 = r_ptr->blow[m].d_dice;
6455                                         d2 = r_ptr->blow[m].d_side;
6456
6457                                         dam_melee += d1*d2;
6458                                 }
6459                                 if(dam_melee>dam_max0)dam_max0=dam_melee;
6460                         }
6461
6462                         /* Contribution from this monster */
6463                         dam_max+=dam_max0;
6464                 }
6465         }
6466
6467         /* Prevent excessive warning */
6468         if(dam_max > old_damage)
6469         {
6470                 old_damage=dam_max * 3 / 2;
6471
6472                 if (dam_max>(p_ptr->chp)/2)
6473                 {
6474                         object_type *o_ptr = choose_warning_item();
6475
6476                         object_desc(o_name, o_ptr, FALSE, 0);
6477 #ifdef JP
6478                         msg_format("%s¤¬±Ô¤¯¿Ì¤¨¤¿¡ª", o_name);
6479 #else
6480                         msg_format("Your %s pulsates sharply!", o_name);
6481 #endif
6482                         disturb(0,0);
6483 #ifdef JP
6484                         return (get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
6485 #else
6486                         return (get_check("Realy want to go ahead? "));
6487 #endif
6488                 }
6489         }
6490         else old_damage = old_damage/2;
6491
6492         c_ptr = &cave[yy][xx];
6493         if (((!easy_disarm && (is_trap(c_ptr->feat) || c_ptr->feat == FEAT_INVIS))
6494             || (c_ptr->mimic && is_trap(c_ptr->feat))) && !one_in_(13))
6495         {
6496                 object_type *o_ptr = choose_warning_item();
6497
6498                 object_desc(o_name, o_ptr, FALSE, 0);
6499 #ifdef JP
6500                 msg_format("%s¤¬¿Ì¤¨¤¿¡ª", o_name);
6501 #else
6502                 msg_format("Your %s pulsates!", o_name);
6503 #endif
6504                 disturb(0,0);
6505 #ifdef JP
6506                 return (get_check("ËÜÅö¤Ë¤³¤Î¤Þ¤Þ¿Ê¤à¤«¡©"));
6507 #else
6508                 return (get_check("Realy want to go ahead? "));
6509 #endif
6510         }
6511         return(TRUE);
6512 }
6513
6514
6515 static bool item_tester_hook_melee_ammo(object_type *o_ptr)
6516 {
6517         switch (o_ptr->tval)
6518         {
6519                 case TV_HAFTED:
6520                 case TV_POLEARM:
6521                 case TV_DIGGING:
6522                 case TV_BOLT:
6523                 case TV_ARROW:
6524                 case TV_SHOT:
6525                 {
6526                         return (TRUE);
6527                 }
6528                 case TV_SWORD:
6529                 {
6530                         if (o_ptr->sval != SV_DOKUBARI) return (TRUE);
6531                 }
6532         }
6533
6534         return (FALSE);
6535 }
6536
6537
6538 /*
6539  *  A structure for smithing
6540  */
6541 typedef struct {
6542         int add;       /* TR flag number or special essence id */
6543         cptr add_name; /* Name of this ability */
6544         int type;      /* Menu number */
6545         int essence;   /* Index for carrying essences */
6546         int value;     /* Needed value to add this ability */
6547 } essence_type;
6548
6549
6550 /*
6551  *  Smithing type data for Weapon smith
6552  */
6553 #ifdef JP
6554 static essence_type essence_info[] = 
6555 {
6556         {TR_STR, "ÏÓÎÏ", 4, TR_STR, 20},
6557         {TR_INT, "ÃÎǽ", 4, TR_INT, 20},
6558         {TR_WIS, "¸­¤µ", 4, TR_WIS, 20},
6559         {TR_DEX, "´ïÍѤµ", 4, TR_DEX, 20},
6560         {TR_CON, "Âѵ×ÎÏ", 4, TR_CON, 20},
6561         {TR_CHR, "Ì¥ÎÏ", 4, TR_CHR, 20},
6562         {TR_MAGIC_MASTERY, "ËâÎÏ»ÙÇÛ", 4, TR_MAGIC_MASTERY, 20},
6563         {TR_STEALTH, "±£Ì©", 4, TR_STEALTH, 40},
6564         {TR_SEARCH, "õº÷", 4, TR_SEARCH, 15},
6565         {TR_INFRA, "ÀÖ³°Àþ»ëÎÏ", 4, TR_INFRA, 15},
6566         {TR_TUNNEL, "ºÎ·¡", 4, TR_TUNNEL, 15},
6567         {TR_SPEED, "¥¹¥Ô¡¼¥É", 4, TR_SPEED, 12},
6568         {TR_BLOWS, "Äɲù¶·â", 1, TR_BLOWS, 20},
6569         {TR_CHAOTIC, "¥«¥ª¥¹¹¶·â", 1, TR_CHAOTIC, 15},
6570         {TR_VAMPIRIC, "µÛ·ì¹¶·â", 1, TR_VAMPIRIC, 60},
6571         {TR_IMPACT, "ÃÏ¿Ìȯư", 7, TR_IMPACT, 15},
6572         {TR_BRAND_POIS, "ÆÇ»¦", 1, TR_BRAND_POIS, 20},
6573         {TR_BRAND_ACID, "Íϲò", 1, TR_BRAND_ACID, 20},
6574         {TR_BRAND_ELEC, "ÅÅ·â", 1, TR_BRAND_ELEC, 20},
6575         {TR_BRAND_FIRE, "¾Æ´þ", 1, TR_BRAND_FIRE, 20},
6576         {TR_BRAND_COLD, "Åà·ë", 1, TR_BRAND_COLD, 20},
6577         {TR_SUST_STR, "ÏÓÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
6578         {TR_SUST_INT, "ÃÎǽ°Ý»ý", 3, TR_SUST_STR, 15},
6579         {TR_SUST_WIS, "¸­¤µ°Ý»ý", 3, TR_SUST_STR, 15},
6580         {TR_SUST_DEX, "´ïÍѤµ°Ý»ý", 3, TR_SUST_STR, 15},
6581         {TR_SUST_CON, "Âѵ×ÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
6582         {TR_SUST_CHR, "Ì¥ÎÏ°Ý»ý", 3, TR_SUST_STR, 15},
6583         {TR_IM_ACID, "»ÀÌȱÖ", 2, TR_IM_ACID, 20},
6584         {TR_IM_ELEC, "ÅÅ·âÌȱÖ", 2, TR_IM_ACID, 20},
6585         {TR_IM_FIRE, "²Ð±êÌȱÖ", 2, TR_IM_ACID, 20},
6586         {TR_IM_COLD, "Î䵤ÌȱÖ", 2, TR_IM_ACID, 20},
6587         {TR_REFLECT, "È¿¼Í", 2, TR_REFLECT, 20},
6588         {TR_FREE_ACT, "ËãáãÃΤ餺", 3, TR_FREE_ACT, 20},
6589         {TR_HOLD_LIFE, "À¸Ì¿ÎÏ°Ý»ý", 3, TR_HOLD_LIFE, 20},
6590         {TR_RES_ACID, "ÂÑ»À", 2, TR_RES_ACID, 15},
6591         {TR_RES_ELEC, "ÂÑÅÅ·â", 2, TR_RES_ELEC, 15},
6592         {TR_RES_FIRE, "ÂѲбê", 2, TR_RES_FIRE, 15},
6593         {TR_RES_COLD, "ÂÑÎ䵤", 2, TR_RES_COLD, 15},
6594         {TR_RES_POIS, "ÂÑÆÇ", 2, TR_RES_POIS, 25},
6595         {TR_RES_FEAR, "ÂѶ²ÉÝ", 2, TR_RES_FEAR, 20},
6596         {TR_RES_LITE, "ÂÑÁ®¸÷", 2, TR_RES_LITE, 20},
6597         {TR_RES_DARK, "ÂѰŹõ", 2, TR_RES_DARK, 20},
6598         {TR_RES_BLIND, "ÂÑÌÕÌÜ", 2, TR_RES_BLIND, 20},
6599         {TR_RES_CONF, "ÂѺ®Íð", 2, TR_RES_CONF, 20},
6600         {TR_RES_SOUND, "Âѹ첻", 2, TR_RES_SOUND, 20},
6601         {TR_RES_SHARDS, "ÂÑÇËÊÒ", 2, TR_RES_SHARDS, 20},
6602         {TR_RES_NETHER, "ÂÑÃϹö", 2, TR_RES_NETHER, 20},
6603         {TR_RES_NEXUS, "ÂÑ°ø²Ìº®Íð", 2, TR_RES_NEXUS, 20},
6604         {TR_RES_CHAOS, "ÂÑ¥«¥ª¥¹", 2, TR_RES_CHAOS, 20},
6605         {TR_RES_DISEN, "ÂÑÎô²½", 2, TR_RES_DISEN, 20},
6606         {TR_SH_FIRE, "", 0, -2, 0},
6607         {TR_SH_ELEC, "", 0, -2, 0},
6608         {TR_SH_COLD, "", 0, -2, 0},
6609         {TR_NO_MAGIC, "È¿ËâË¡", 3, TR_NO_MAGIC, 15},
6610         {TR_WARNING, "·Ù¹ð", 3, TR_WARNING, 20},
6611         {TR_FEATHER, "ÉâÍ·", 3, TR_FEATHER, 20},
6612         {TR_LITE, "±Êµ×¸÷¸»", 3, TR_LITE, 15},
6613         {TR_SEE_INVIS, "²Ä»ëÆ©ÌÀ", 3, TR_SEE_INVIS, 20},
6614         {TR_TELEPATHY, "¥Æ¥ì¥Ñ¥·¡¼", 6, TR_TELEPATHY, 15},
6615         {TR_SLOW_DIGEST, "Ãپò½", 3, TR_SLOW_DIGEST, 15},
6616         {TR_REGEN, "µÞ®²óÉü", 3, TR_REGEN, 20},
6617         {TR_TELEPORT, "¥Æ¥ì¥Ý¡¼¥È", 3, TR_TELEPORT, 25},
6618
6619         {TR_SLAY_EVIL, "¼Ù°­ÇÜÂÇ", 5, TR_SLAY_EVIL, 100},
6620         {TR_KILL_EVIL, "¼Ù°­ÇÜÇÜÂÇ", 0, TR_SLAY_EVIL, 60},
6621         {TR_SLAY_ANIMAL, "ưʪÇÜÂÇ", 5, TR_SLAY_ANIMAL, 20},
6622         {TR_KILL_ANIMAL, "ưʪÇÜÇÜÂÇ", 5, TR_SLAY_ANIMAL, 60},
6623         {TR_SLAY_UNDEAD, "ÉÔ»àÇÜÂÇ", 5, TR_SLAY_UNDEAD, 20},
6624         {TR_KILL_UNDEAD, "ÉÔ»àÇÜÇÜÂÇ", 5, TR_SLAY_UNDEAD, 60},
6625         {TR_SLAY_DEMON, "°­ËâÇÜÂÇ", 5, TR_SLAY_DEMON, 20},
6626         {TR_KILL_DEMON, "°­ËâÇÜÇÜÂÇ", 5, TR_SLAY_DEMON, 60},
6627         {TR_SLAY_ORC, "¥ª¡¼¥¯ÇÜÂÇ", 5, TR_SLAY_ORC, 15},
6628         {TR_KILL_ORC, "¥ª¡¼¥¯ÇÜÇÜÂÇ", 5, TR_SLAY_ORC, 60},
6629         {TR_SLAY_TROLL, "¥È¥í¥ëÇÜÂÇ", 5, TR_SLAY_TROLL, 15},
6630         {TR_KILL_TROLL, "¥È¥í¥ëÇÜÇÜÂÇ", 5, TR_SLAY_TROLL, 60},
6631         {TR_SLAY_GIANT, "µð¿ÍÇÜÂÇ", 5, TR_SLAY_GIANT, 20},
6632         {TR_KILL_GIANT, "µð¿ÍÇÜÇÜÂÇ", 5, TR_SLAY_GIANT, 60},       
6633         {TR_SLAY_DRAGON, "εÇÜÂÇ", 5, TR_SLAY_DRAGON, 20},
6634         {TR_KILL_DRAGON, "εÇÜÇÜÂÇ", 5, TR_SLAY_DRAGON, 60},
6635         {TR_SLAY_HUMAN, "¿Í´ÖÇÜÂÇ", 5, TR_SLAY_HUMAN, 20},
6636         {TR_KILL_HUMAN, "¿Í´ÖÇÜÇÜÂÇ", 5, TR_SLAY_HUMAN, 60},
6637
6638         {TR_ESP_ANIMAL, "ưʪESP", 6, TR_SLAY_ANIMAL, 40},
6639         {TR_ESP_UNDEAD, "ÉÔ»àESP", 6, TR_SLAY_UNDEAD, 40}, 
6640         {TR_ESP_DEMON, "°­ËâESP", 6, TR_SLAY_DEMON, 40},       
6641         {TR_ESP_ORC, "¥ª¡¼¥¯ESP", 6, TR_SLAY_ORC, 40},     
6642         {TR_ESP_TROLL, "¥È¥í¥ëESP", 6, TR_SLAY_TROLL, 40},   
6643         {TR_ESP_GIANT, "µð¿ÍESP", 6, TR_SLAY_GIANT, 40},       
6644         {TR_ESP_DRAGON, "εESP", 6, TR_SLAY_DRAGON, 40},
6645         {TR_ESP_HUMAN, "¿Í´ÖESP", 6, TR_SLAY_HUMAN, 40},
6646
6647         {ESSENCE_ATTACK, "¹¶·â", 10, TR_ES_ATTACK, 30},
6648         {ESSENCE_AC, "Ëɸæ", 10, TR_ES_AC, 15},
6649         {ESSENCE_TMP_RES_ACID, "»ÀÂÑÀ­È¯Æ°", 7, TR_RES_ACID, 50},
6650         {ESSENCE_TMP_RES_ELEC, "ÅÅ·âÂÑÀ­È¯Æ°", 7, TR_RES_ELEC, 50},
6651         {ESSENCE_TMP_RES_FIRE, "²Ð±êÂÑÀ­È¯Æ°", 7, TR_RES_FIRE, 50},
6652         {ESSENCE_TMP_RES_COLD, "Î䵤ÂÑÀ­È¯Æ°", 7, TR_RES_COLD, 50},
6653         {ESSENCE_SH_FIRE, "²Ð±ê¥ª¡¼¥é", 7, -1, 50},
6654         {ESSENCE_SH_ELEC, "Åŷ⥪¡¼¥é", 7, -1, 50},
6655         {ESSENCE_SH_COLD, "Î䵤¥ª¡¼¥é", 7, -1, 50},
6656         {ESSENCE_RESISTANCE, "Á´ÂÑÀ­", 2, -1, 150},
6657         {ESSENCE_SUSTAIN, "ÁõÈ÷ÊÝ»ý", 10, -1, 10},
6658         {ESSENCE_SLAY_GLOVE, "»¦Ù¤¤Î¾®¼ê", 1, TR_ES_ATTACK, 200},
6659
6660         {-1, NULL, 0, -1, 0}
6661 };
6662 #else
6663 static essence_type essence_info[] = 
6664 {
6665         {TR_STR, "strength", 4, TR_STR, 20},
6666         {TR_INT, "intelligence", 4, TR_INT, 20},
6667         {TR_WIS, "wisdom", 4, TR_WIS, 20},
6668         {TR_DEX, "dexterity", 4, TR_DEX, 20},
6669         {TR_CON, "constitution", 4, TR_CON, 20},
6670         {TR_CHR, "charisma", 4, TR_CHR, 20},
6671         {TR_MAGIC_MASTERY, "magic mastery", 4, TR_MAGIC_MASTERY, 20},
6672         {TR_STEALTH, "stealth", 4, TR_STEALTH, 40},
6673         {TR_SEARCH, "serching", 4, TR_SEARCH, 15},
6674         {TR_INFRA, "inflavision", 4, TR_INFRA, 15},
6675         {TR_TUNNEL, "digging", 4, TR_TUNNEL, 15},
6676         {TR_SPEED, "speed", 4, TR_SPEED, 12},
6677         {TR_BLOWS, "extra attack", 1, TR_BLOWS, 20},
6678         {TR_CHAOTIC, "chaos brand", 1, TR_CHAOTIC, 15},
6679         {TR_VAMPIRIC, "vampiric brand", 1, TR_VAMPIRIC, 60},
6680         {TR_IMPACT, "quake activation", 7, TR_IMPACT, 15},
6681         {TR_BRAND_POIS, "poison brand", 1, TR_BRAND_POIS, 20},
6682         {TR_BRAND_ACID, "acid brand", 1, TR_BRAND_ACID, 20},
6683         {TR_BRAND_ELEC, "electric brand", 1, TR_BRAND_ELEC, 20},
6684         {TR_BRAND_FIRE, "fire brand", 1, TR_BRAND_FIRE, 20},
6685         {TR_BRAND_COLD, "cold brand", 1, TR_BRAND_COLD, 20},
6686         {TR_SUST_STR, "sustain strength", 3, TR_SUST_STR, 15},
6687         {TR_SUST_INT, "sustain intelligence", 3, TR_SUST_STR, 15},
6688         {TR_SUST_WIS, "sustain wisdom", 3, TR_SUST_STR, 15},
6689         {TR_SUST_DEX, "sustain dexterity", 3, TR_SUST_STR, 15},
6690         {TR_SUST_CON, "sustain constitution", 3, TR_SUST_STR, 15},
6691         {TR_SUST_CHR, "sustain charisma", 3, TR_SUST_STR, 15},
6692         {TR_IM_ACID, "acid immunity", 2, TR_IM_ACID, 20},
6693         {TR_IM_ELEC, "electric immunity", 2, TR_IM_ACID, 20},
6694         {TR_IM_FIRE, "fire immunity", 2, TR_IM_ACID, 20},
6695         {TR_IM_COLD, "cold immunity", 2, TR_IM_ACID, 20},
6696         {TR_REFLECT, "reflection", 2, TR_REFLECT, 20},
6697         {TR_FREE_ACT, "free action", 3, TR_FREE_ACT, 20},
6698         {TR_HOLD_LIFE, "hold life", 3, TR_HOLD_LIFE, 20},
6699         {TR_RES_ACID, "resistance to acid", 2, TR_RES_ACID, 15},
6700         {TR_RES_ELEC, "resistance to electric", 2, TR_RES_ELEC, 15},
6701         {TR_RES_FIRE, "resistance to fire", 2, TR_RES_FIRE, 15},
6702         {TR_RES_COLD, "resistance to cold", 2, TR_RES_COLD, 15},
6703         {TR_RES_POIS, "resistance to poison", 2, TR_RES_POIS, 25},
6704         {TR_RES_FEAR, "resistance to fear", 2, TR_RES_FEAR, 20},
6705         {TR_RES_LITE, "resistance to light", 2, TR_RES_LITE, 20},
6706         {TR_RES_DARK, "resistance to dark", 2, TR_RES_DARK, 20},
6707         {TR_RES_BLIND, "resistance to blind", 2, TR_RES_BLIND, 20},
6708         {TR_RES_CONF, "resistance to confusion", 2, TR_RES_CONF, 20},
6709         {TR_RES_SOUND, "resistance to sound", 2, TR_RES_SOUND, 20},
6710         {TR_RES_SHARDS, "resistance to shard", 2, TR_RES_SHARDS, 20},
6711         {TR_RES_NETHER, "resistance to nether", 2, TR_RES_NETHER, 20},
6712         {TR_RES_NEXUS, "resistance to nexus", 2, TR_RES_NEXUS, 20},
6713         {TR_RES_CHAOS, "resistance to chaos", 2, TR_RES_CHAOS, 20},
6714         {TR_RES_DISEN, "resistance to disenchantment", 2, TR_RES_DISEN, 20},
6715         {TR_SH_FIRE, "", 0, -2, 0},
6716         {TR_SH_ELEC, "", 0, -2, 0},
6717         {TR_SH_COLD, "", 0, -2, 0},
6718         {TR_NO_MAGIC, "anti magic", 3, TR_NO_MAGIC, 15},
6719         {TR_WARNING, "warning", 3, TR_WARNING, 20},
6720         {TR_FEATHER, "levitation", 3, TR_FEATHER, 20},
6721         {TR_LITE, "permanent light", 3, TR_LITE, 15},
6722         {TR_SEE_INVIS, "see invisible", 3, TR_SEE_INVIS, 20},
6723         {TR_TELEPATHY, "telepathy", 6, TR_TELEPATHY, 15},
6724         {TR_SLOW_DIGEST, "slow digestion", 3, TR_SLOW_DIGEST, 15},
6725         {TR_REGEN, "regeneration", 3, TR_REGEN, 20},
6726         {TR_TELEPORT, "teleport", 3, TR_TELEPORT, 25},
6727
6728         {TR_SLAY_EVIL, "slay evil", 5, TR_SLAY_EVIL, 100},
6729         {TR_SLAY_ANIMAL, "slay animal", 5, TR_SLAY_ANIMAL, 20},
6730         {TR_KILL_ANIMAL, "kill animal", 5, TR_SLAY_ANIMAL, 60},
6731         {TR_KILL_EVIL, "kill evil", 0, TR_SLAY_EVIL, 60},
6732         {TR_SLAY_UNDEAD, "slay undead", 5, TR_SLAY_UNDEAD, 20},
6733         {TR_KILL_UNDEAD, "kill undead", 5, TR_SLAY_UNDEAD, 60},
6734         {TR_SLAY_DEMON, "slay demon", 5, TR_SLAY_DEMON, 20},
6735         {TR_KILL_DEMON, "kill demon", 5, TR_SLAY_DEMON, 60},
6736         {TR_SLAY_ORC, "slay orc", 5, TR_SLAY_ORC, 15},
6737         {TR_KILL_ORC, "kill orc", 5, TR_SLAY_ORC, 60},
6738         {TR_SLAY_TROLL, "slay troll", 5, TR_SLAY_TROLL, 15},
6739         {TR_KILL_TROLL, "kill troll", 5, TR_SLAY_TROLL, 60},
6740         {TR_SLAY_GIANT, "slay giant", 5, TR_SLAY_GIANT, 20},
6741         {TR_KILL_GIANT, "kill giant", 5, TR_SLAY_GIANT, 60},       
6742         {TR_SLAY_DRAGON, "slay dragon", 5, TR_SLAY_DRAGON, 20},
6743         {TR_KILL_DRAGON, "kill dragon", 5, TR_SLAY_DRAGON, 60},
6744         {TR_SLAY_HUMAN, "slay human", 5, TR_SLAY_HUMAN, 20},
6745         {TR_KILL_HUMAN, "kill human", 5, TR_SLAY_HUMAN, 60},
6746
6747         {TR_ESP_ANIMAL, "sense animal", 6, TR_SLAY_ANIMAL, 40},
6748         {TR_ESP_UNDEAD, "sense undead", 6, TR_SLAY_UNDEAD, 40}, 
6749         {TR_ESP_DEMON, "sense demon", 6, TR_SLAY_DEMON, 40},       
6750         {TR_ESP_ORC, "sense orc", 6, TR_SLAY_ORC, 40},     
6751         {TR_ESP_TROLL, "sense troll", 6, TR_SLAY_TROLL, 40},   
6752         {TR_ESP_GIANT, "sense giant", 6, TR_SLAY_GIANT, 40},       
6753         {TR_ESP_DRAGON, "sense dragon", 6, TR_SLAY_DRAGON, 40},
6754         {TR_ESP_HUMAN, "sense human", 6, TR_SLAY_HUMAN, 40},
6755
6756         {ESSENCE_ATTACK, "weapon enchant", 10, TR_ES_ATTACK, 30},
6757         {ESSENCE_AC, "armor enchant", 10, TR_ES_AC, 15},
6758         {ESSENCE_TMP_RES_ACID, "resist acid activation", 7, TR_RES_ACID, 50},
6759         {ESSENCE_TMP_RES_ELEC, "resist electricity activation", 7, TR_RES_ELEC, 50},
6760         {ESSENCE_TMP_RES_FIRE, "resist fire activation", 7, TR_RES_FIRE, 50},
6761         {ESSENCE_TMP_RES_COLD, "resist cold activation", 7, TR_RES_COLD, 50},
6762         {ESSENCE_SH_FIRE, "fiery sheath", 7, -1, 50},
6763         {ESSENCE_SH_ELEC, "electric sheath", 7, -1, 50},
6764         {ESSENCE_SH_COLD, "sheath of coldness", 7, -1, 50},
6765         {ESSENCE_RESISTANCE, "resistance", 2, -1, 150},
6766         {ESSENCE_SUSTAIN, "elements proof", 10, -1, 10},
6767         {ESSENCE_SLAY_GLOVE, "gauntlets of slaying", 1, TR_ES_ATTACK, 200},
6768
6769         {-1, NULL, 0, -1, 0}
6770 };
6771 #endif
6772
6773
6774 /*
6775  *  Essense names for Weapon smith
6776  */
6777 #ifdef JP
6778 static cptr essence_name[] = 
6779 {
6780         "ÏÓÎÏ",
6781         "ÃÎǽ",
6782         "¸­¤µ",
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         NULL
6877 };
6878
6879 #else
6880
6881 static cptr essence_name[] = 
6882 {
6883         "strength",
6884         "intelligen.",
6885         "wisdom",
6886         "dexterity",
6887         "constitut.",
6888         "charisma",
6889         "magic mast.",
6890         "",
6891         "stealth",
6892         "serching",
6893         "inflavision",
6894         "digging",
6895         "speed",
6896         "extra atk",
6897         "chaos brand",
6898         "vampiric",
6899         "slay animal",
6900         "slay evil",
6901         "slay undead",
6902         "slay demon",
6903         "slay orc",
6904         "slay troll",
6905         "slay giant",
6906         "slay dragon",
6907         "",
6908         "",
6909         "quake",
6910         "pois. brand",
6911         "acid brand",
6912         "elec. brand",
6913         "fire brand",
6914         "cold brand",
6915         "sustain",
6916         "",
6917         "",
6918         "",
6919         "",
6920         "",
6921         "",
6922         "",
6923         "immunity",
6924         "",
6925         "",
6926         "",
6927         "",
6928         "reflection",
6929         "free action",
6930         "hold life",
6931         "res. acid",
6932         "res. elec.",
6933         "res. fire",
6934         "res. cold",
6935         "res. poison",
6936         "res. fear",
6937         "res. light",
6938         "res. dark",
6939         "res. blind",
6940         "res.confuse",
6941         "res. sound",
6942         "res. shard",
6943         "res. nether",
6944         "res. nexus",
6945         "res. chaos",
6946         "res. disen.",
6947         "",
6948         "",
6949         "slay human",
6950         "",
6951         "",
6952         "anti magic",
6953         "",
6954         "",
6955         "warning",
6956         "",
6957         "",
6958         "",
6959         "levitation",
6960         "perm. light",
6961         "see invis.",
6962         "telepathy",
6963         "slow dige.",
6964         "regen.",
6965         "",
6966         "",
6967         "",
6968         "",
6969         "",
6970         "",
6971         "",
6972         "",
6973         "teleport",
6974         "",
6975         "",
6976         "weapon enc.",
6977         "armor enc.",
6978
6979         NULL
6980 };
6981 #endif
6982
6983
6984 static void display_essence(void)
6985 {
6986         int i, num = 0;
6987
6988         screen_save();
6989         for (i = 1; i < 22; i++)
6990         {
6991                 prt("",i,0);
6992         }
6993 #ifdef JP
6994         prt("¥¨¥Ã¥»¥ó¥¹   ¸Ä¿ô     ¥¨¥Ã¥»¥ó¥¹   ¸Ä¿ô     ¥¨¥Ã¥»¥ó¥¹   ¸Ä¿ô", 1, 8);
6995 #else
6996         prt("Essence      Num      Essence      Num      Essence      Num ", 1, 8);
6997 #endif
6998         for (i = 0; essence_name[i]; i++)
6999         {
7000                 if (!essence_name[i][0]) continue;
7001                 prt(format("%-11s %5d", essence_name[i], p_ptr->magic_num1[i]), 2+num%21, 8+num/21*22);
7002                 num++;
7003         }
7004 #ifdef JP
7005         prt("¸½ºß½ê»ý¤·¤Æ¤¤¤ë¥¨¥Ã¥»¥ó¥¹", 0, 0);
7006 #else
7007         prt("List of all essences you have.", 0, 0);
7008 #endif
7009         (void)inkey();
7010         screen_load();
7011         return;
7012 }
7013
7014 static void drain_essence(void)
7015 {
7016         int drain_value[sizeof(p_ptr->magic_num1) / sizeof(s32b)];
7017         int i, item;
7018         int dec = 4;
7019         bool observe = FALSE;
7020         int old_ds, old_dd, old_to_h, old_to_d, old_ac, old_to_a, old_pval, old_name2;
7021         u32b old_flgs[TR_FLAG_SIZE], new_flgs[TR_FLAG_SIZE];
7022         object_type *o_ptr;
7023         cptr            q, s;
7024         byte iy, ix, marked, number;
7025         s16b next_o_idx, weight;
7026
7027         for (i = 0; i < sizeof(drain_value) / sizeof(int); i++)
7028                 drain_value[i] = 0;
7029
7030         item_tester_hook = item_tester_hook_weapon_armour;
7031         item_tester_no_ryoute = TRUE;
7032
7033         /* Get an item */
7034 #ifdef JP
7035         q = "¤É¤Î¥¢¥¤¥Æ¥à¤«¤éÃê½Ð¤·¤Þ¤¹¤«¡©";
7036         s = "Ãê½Ð¤Ç¤­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
7037 #else
7038         q = "Extract from which item? ";
7039         s = "You have nothing you can extract from.";
7040 #endif
7041
7042         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
7043
7044         /* Get the item (in the pack) */
7045         if (item >= 0)
7046         {
7047                 o_ptr = &inventory[item];
7048         }
7049
7050         /* Get the item (on the floor) */
7051         else
7052         {
7053                 o_ptr = &o_list[0 - item];
7054         }
7055
7056         if (object_known_p(o_ptr) && (o_ptr->name1 || o_ptr->name2 || o_ptr->art_name || o_ptr->xtra3)) {
7057                 char o_name[MAX_NLEN];
7058                 object_desc(o_name, o_ptr, FALSE, 0);
7059 #ifdef JP
7060                 if (!get_check(format("ËÜÅö¤Ë%s¤«¤éÃê½Ð¤·¤Æ¤è¤í¤·¤¤¤Ç¤¹¤«¡©", o_name))) return;
7061 #else
7062                 if (!get_check(format("Really extract from %s? ", o_name))) return;
7063 #endif
7064         }
7065
7066         energy_use = 100;
7067
7068         object_flags(o_ptr, old_flgs);
7069         if (have_flag(old_flgs, TR_KILL_DRAGON)) add_flag(old_flgs, TR_SLAY_DRAGON);
7070         if (have_flag(old_flgs, TR_KILL_ANIMAL)) add_flag(old_flgs, TR_SLAY_ANIMAL);
7071         if (have_flag(old_flgs, TR_KILL_EVIL)) add_flag(old_flgs, TR_SLAY_EVIL);
7072         if (have_flag(old_flgs, TR_KILL_UNDEAD)) add_flag(old_flgs, TR_SLAY_UNDEAD);
7073         if (have_flag(old_flgs, TR_KILL_DEMON)) add_flag(old_flgs, TR_SLAY_DEMON);
7074         if (have_flag(old_flgs, TR_KILL_ORC)) add_flag(old_flgs, TR_SLAY_ORC);
7075         if (have_flag(old_flgs, TR_KILL_TROLL)) add_flag(old_flgs, TR_SLAY_TROLL);
7076         if (have_flag(old_flgs, TR_KILL_GIANT)) add_flag(old_flgs, TR_SLAY_GIANT);
7077         if (have_flag(old_flgs, TR_KILL_HUMAN)) add_flag(old_flgs, TR_SLAY_HUMAN);
7078
7079         old_to_a = o_ptr->to_a;
7080         old_ac = o_ptr->ac;
7081         old_to_h = o_ptr->to_h;
7082         old_to_d = o_ptr->to_d;
7083         old_ds = o_ptr->ds;
7084         old_dd = o_ptr->dd;
7085         old_pval = o_ptr->pval;
7086         old_name2 = o_ptr->name2;
7087         if (o_ptr->curse_flags & (TRC_CURSED | TRC_HEAVY_CURSE | TRC_PERMA_CURSE)) dec--;
7088         if (have_flag(old_flgs, TR_AGGRAVATE)) dec--;
7089         if (have_flag(old_flgs, TR_NO_TELE)) dec--;
7090         if (have_flag(old_flgs, TR_DRAIN_EXP)) dec--;
7091         if (have_flag(old_flgs, TR_TY_CURSE)) dec--;
7092
7093         iy = o_ptr->iy;
7094         ix = o_ptr->ix;
7095         next_o_idx = o_ptr->next_o_idx;
7096         marked = o_ptr->marked;
7097         weight = o_ptr->weight;
7098         number = o_ptr->number;
7099
7100         object_prep(o_ptr, o_ptr->k_idx);
7101
7102         o_ptr->iy=iy;
7103         o_ptr->ix=ix;
7104         o_ptr->next_o_idx=next_o_idx;
7105         o_ptr->marked=marked;
7106         o_ptr->number = number;
7107         if (item >= 0) p_ptr->total_weight += (o_ptr->weight*o_ptr->number - weight*number);
7108         o_ptr->ident |= (IDENT_MENTAL);
7109         object_aware(o_ptr);
7110         object_known(o_ptr);
7111
7112         object_flags(o_ptr, new_flgs);
7113
7114         for (i = 0; essence_info[i].add_name; i++)
7115         {
7116                 essence_type *es_ptr = &essence_info[i];
7117                 int pval = 0;
7118
7119                 if (es_ptr->add < TR_FLAG_MAX && is_pval_flag(es_ptr->add) && old_pval)
7120                         pval = (have_flag(new_flgs, es_ptr->add)) ? old_pval - o_ptr->pval : old_pval;
7121
7122                 if (es_ptr->add < TR_FLAG_MAX &&
7123                     (!have_flag(new_flgs, es_ptr->add) || pval) &&
7124                     have_flag(old_flgs, es_ptr->add))
7125                 {
7126                         if (pval)
7127                         {
7128                                 drain_value[es_ptr->essence] += 10 * pval;
7129                         }
7130                         else if (es_ptr->essence != -2)
7131                         {
7132                                 drain_value[es_ptr->essence] += 10;
7133                         }
7134                         else if (es_ptr->add == TR_SH_FIRE)
7135                         {
7136                                 drain_value[TR_BRAND_FIRE] += 10;
7137                                 drain_value[TR_RES_FIRE] += 10;
7138                         }
7139                         else if (es_ptr->add == TR_SH_ELEC)
7140                         {
7141                                 drain_value[TR_BRAND_ELEC] += 10;
7142                                 drain_value[TR_RES_ELEC] += 10;
7143                         }
7144                         else if (es_ptr->add == TR_SH_COLD)
7145                         {
7146                                 drain_value[TR_BRAND_COLD] += 10;
7147                                 drain_value[TR_RES_COLD] += 10;
7148                         }
7149                 }
7150         }
7151
7152         if ((have_flag(old_flgs, TR_FORCE_WEAPON)) && !(have_flag(new_flgs, TR_FORCE_WEAPON)))
7153         {
7154                 drain_value[TR_INT] += 5;
7155                 drain_value[TR_WIS] += 5;
7156         }
7157         if ((have_flag(old_flgs, TR_VORPAL)) && !(have_flag(new_flgs, TR_VORPAL)))
7158         {
7159                 drain_value[TR_BRAND_POIS] += 5;
7160                 drain_value[TR_BRAND_ACID] += 5;
7161                 drain_value[TR_BRAND_ELEC] += 5;
7162                 drain_value[TR_BRAND_FIRE] += 5;
7163                 drain_value[TR_BRAND_COLD] += 5;
7164         }
7165         if ((have_flag(old_flgs, TR_DEC_MANA)) && !(have_flag(new_flgs, TR_DEC_MANA)))
7166         {
7167                 drain_value[TR_INT] += 10;
7168         }
7169         if ((have_flag(old_flgs, TR_XTRA_MIGHT)) && !(have_flag(new_flgs, TR_XTRA_MIGHT)))
7170         {
7171                 drain_value[TR_STR] += 10;
7172         }
7173         if ((have_flag(old_flgs, TR_XTRA_SHOTS)) && !(have_flag(new_flgs, TR_XTRA_SHOTS)))
7174         {
7175                 drain_value[TR_DEX] += 10;
7176         }
7177         if (old_name2 == EGO_2WEAPON)
7178         {
7179                 drain_value[TR_DEX] += 20;
7180         }
7181         if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_SWORD) && (o_ptr->tval != TV_BOW))
7182         {
7183                 if (old_ds > o_ptr->ds) drain_value[TR_ES_ATTACK] += (old_ds-o_ptr->ds)*10;
7184
7185                 if (old_dd > o_ptr->dd) drain_value[TR_ES_ATTACK] += (old_dd-o_ptr->dd)*10;
7186         }
7187         if (old_to_h > o_ptr->to_h) drain_value[TR_ES_ATTACK] += (old_to_h-o_ptr->to_h)*10;
7188         if (old_to_d > o_ptr->to_d) drain_value[TR_ES_ATTACK] += (old_to_d-o_ptr->to_d)*10;
7189         if (old_ac > o_ptr->ac) drain_value[TR_ES_AC] += (old_ac-o_ptr->ac)*10;
7190         if (old_to_a > o_ptr->to_a) drain_value[TR_ES_AC] += (old_to_a-o_ptr->to_a)*10;
7191
7192         for (i = 0; i < sizeof(drain_value) / sizeof(int); i++)
7193         {
7194                 drain_value[i] *= number;
7195                 drain_value[i] = drain_value[i] * dec / 4;
7196                 drain_value[i] = MAX(drain_value[i], 0);
7197                 if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_BOLT)) drain_value[i] /= 10;
7198                 if (drain_value[i])
7199                 {
7200                         observe = TRUE;
7201                 }
7202         }
7203         if (!observe)
7204         {
7205 #ifdef JP
7206                 msg_print("¥¨¥Ã¥»¥ó¥¹¤ÏÃê½Ð¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£");
7207 #else
7208                 msg_print("You were not able to extract any essence.");
7209 #endif
7210         }
7211         else
7212         {
7213 #ifdef JP
7214                 msg_print("Ãê½Ð¤·¤¿¥¨¥Ã¥»¥ó¥¹:");
7215 #else
7216                 msg_print("Extracted essences:");
7217 #endif
7218                 for (i = 0; essence_name[i]; i++)
7219                 {
7220                         if (!essence_name[i][0]) continue;
7221                         if (!drain_value[i]) continue;
7222
7223                         p_ptr->magic_num1[i] += drain_value[i];
7224                         p_ptr->magic_num1[i] = MIN(20000, p_ptr->magic_num1[i]);
7225                         msg_print(NULL);
7226                         msg_format("%s...%d", essence_name[i], drain_value[i]);
7227                 }
7228         }
7229
7230         /* Combine the pack */
7231         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
7232
7233         /* Window stuff */
7234         p_ptr->window |= (PW_INVEN);
7235 }
7236
7237
7238
7239 static int choose_essence(void)
7240 {
7241         int mode = 0;
7242         char choice;
7243         int menu_line = (use_menu ? 1 : 0);
7244
7245 #ifdef JP
7246         cptr menu_name[] = {
7247                 "Éð´ï°À­", 
7248                 "ÂÑÀ­",
7249                 "ǽÎÏ",
7250                 "¿ôÃÍ",
7251                 "¥¹¥ì¥¤",
7252                 "ESP",
7253                 "¤½¤Î¾"
7254         };
7255 #else
7256         cptr menu_name[] = {
7257                 "Brand weapon",
7258                 "Resistance",
7259                 "Ability",
7260                 "Magic number", 
7261                 "Slay",
7262                 "ESP",
7263                 "Others"
7264         };
7265 #endif
7266         const int mode_max = 7;
7267
7268 #ifdef ALLOW_REPEAT
7269         if (repeat_pull(&mode) && 1 <= mode && mode <= mode_max)
7270                 return mode;
7271         mode = 0;
7272 #endif /* ALLOW_REPEAT */
7273
7274         if (use_menu)
7275         {
7276                 screen_save();
7277
7278                 while(!mode)
7279                 {
7280                         int i;
7281                         for (i = 0; i < mode_max; i++)
7282 #ifdef JP
7283                                 prt(format(" %s %s", (menu_line == 1+i) ? "¡Õ" : "  ", menu_name[i]), 2 + i, 14);
7284                         prt("¤É¤Î¼ïÎà¤Î¥¨¥Ã¥»¥ó¥¹Éղäò¹Ô¤¤¤Þ¤¹¤«¡©", 0, 0);
7285 #else
7286                                 prt(format(" %s %s", (menu_line == 1+i) ? "> " : "  ", menu_name[i]), 2 + i, 14);
7287                         prt("Choose from menu.", 0, 0);
7288 #endif
7289
7290                         choice = inkey();
7291                         switch(choice)
7292                         {
7293                         case ESCAPE:
7294                         case 'z':
7295                         case 'Z':
7296                                 screen_load();
7297                                 return 0;
7298                         case '2':
7299                         case 'j':
7300                         case 'J':
7301                                 menu_line++;
7302                                 break;
7303                         case '8':
7304                         case 'k':
7305                         case 'K':
7306                                 menu_line += mode_max - 1;
7307                                 break;
7308                         case '\r':
7309                         case '\n':
7310                         case 'x':
7311                         case 'X':
7312                                 mode = menu_line;
7313                                 break;
7314                         }
7315                         if (menu_line > mode_max) menu_line -= mode_max;
7316                 }
7317                 screen_load();
7318         }
7319         else
7320         {
7321                 screen_save();
7322                 while (!mode)
7323                 {
7324                         int i;
7325
7326                         for (i = 0; i < mode_max; i++)
7327                                 prt(format("  %c) %s", 'a' + i, menu_name[i]), 2 + i, 14);
7328
7329 #ifdef JP
7330                         if (!get_com("²¿¤òÉղä·¤Þ¤¹¤«:", &choice, TRUE))
7331 #else
7332                         if (!get_com("Command :", &choice, TRUE))
7333 #endif
7334                         {
7335                                 screen_load();
7336                                 return 0;
7337                         }
7338
7339                         if (isupper(choice)) choice = tolower(choice);
7340
7341                         if ('a' <= choice && choice <= 'a' + (char)mode_max - 1)
7342                                 mode = (int)choice - 'a' + 1;
7343                 }
7344                 screen_load();
7345         }
7346
7347 #ifdef ALLOW_REPEAT
7348         repeat_push(mode);
7349 #endif /* ALLOW_REPEAT */
7350         return mode;
7351 }
7352
7353 static void add_essence(int mode)
7354 {
7355         int item, max_num = 0;
7356         int i;
7357         bool flag,redraw;
7358         char choice;
7359         cptr            q, s;
7360         object_type *o_ptr;
7361         int ask = TRUE;
7362         char out_val[160];
7363         int num[22];
7364         char o_name[MAX_NLEN];
7365         int use_essence;
7366         essence_type *es_ptr;
7367
7368         int menu_line = (use_menu ? 1 : 0);
7369
7370         for (i = 0; essence_info[i].add_name; i++)
7371         {
7372                 es_ptr = &essence_info[i];
7373
7374                 if (es_ptr->type != mode) continue;
7375                 num[max_num++] = i;
7376         }
7377
7378 #ifdef ALLOW_REPEAT
7379         if (!repeat_pull(&i) || i<0 || i>=max_num)
7380         {
7381 #endif /* ALLOW_REPEAT */
7382
7383
7384         /* Nothing chosen yet */
7385         flag = FALSE;
7386
7387         /* No redraw yet */
7388         redraw = FALSE;
7389
7390         /* Build a prompt */
7391 #ifdef JP
7392         (void) strnfmt(out_val, 78, "('*'¤Ç°ìÍ÷, ESC¤ÇÃæÃÇ) ¤É¤ÎǽÎϤòÉղä·¤Þ¤¹¤«¡©");
7393 #else
7394         (void)strnfmt(out_val, 78, "(*=List, ESC=exit) Add which ability? ");
7395 #endif
7396         if (use_menu) screen_save();
7397
7398         /* Get a spell from the user */
7399
7400         choice = (always_show_list || use_menu) ? ESCAPE:1;
7401         while (!flag)
7402         {
7403                 bool able[22];
7404                 if( choice==ESCAPE ) choice = ' '; 
7405                 else if( !get_com(out_val, &choice, FALSE) )break; 
7406
7407                 if (use_menu && choice != ' ')
7408                 {
7409                         switch(choice)
7410                         {
7411                                 case '0':
7412                                 {
7413                                         screen_load();
7414                                         return;
7415                                         break;
7416                                 }
7417
7418                                 case '8':
7419                                 case 'k':
7420                                 case 'K':
7421                                 {
7422                                         menu_line += (max_num-1);
7423                                         break;
7424                                 }
7425
7426                                 case '2':
7427                                 case 'j':
7428                                 case 'J':
7429                                 {
7430                                         menu_line++;
7431                                         break;
7432                                 }
7433
7434                                 case '4':
7435                                 case 'h':
7436                                 case 'H':
7437                                 {
7438                                         menu_line = 1;
7439                                         break;
7440                                 }
7441                                 case '6':
7442                                 case 'l':
7443                                 case 'L':
7444                                 {
7445                                         menu_line = max_num;
7446                                         break;
7447                                 }
7448
7449                                 case 'x':
7450                                 case 'X':
7451                                 case '\r':
7452                                 case '\n':
7453                                 {
7454                                         i = menu_line - 1;
7455                                         ask = FALSE;
7456                                         break;
7457                                 }
7458                         }
7459                         if (menu_line > max_num) menu_line -= max_num;
7460                 }
7461                 /* Request redraw */
7462                 if ((choice == ' ') || (choice == '*') || (choice == '?') || (use_menu && ask))
7463                 {
7464                         /* Show the list */
7465                         if (!redraw || use_menu)
7466                         {
7467                                 byte y, x = 10;
7468                                 int ctr;
7469                                 char dummy[80], dummy2[80];
7470                                 byte col;
7471
7472                                 strcpy(dummy, "");
7473
7474                                 /* Show list */
7475                                 redraw = TRUE;
7476
7477                                 /* Save the screen */
7478                                 if (!use_menu) screen_save();
7479
7480                                 for (y = 1; y < 24; y++)
7481                                         prt("", y, x);
7482
7483                                 /* Print header(s) */
7484 #ifdef JP
7485                                 prt(format("   %-43s %6s/%s", "ǽÎÏ(ɬÍ×¥¨¥Ã¥»¥ó¥¹)", "ɬÍ׿ô", "½ê»ý¿ô"), 1, x);
7486
7487 #else
7488                                 prt(format("   %-43s %6s/%s", "Ability (needed essence)", "Needs", "Possess"), 1, x);
7489 #endif
7490                                 /* Print list */
7491                                 for (ctr = 0; ctr < max_num; ctr++)
7492                                 {
7493                                         es_ptr = &essence_info[num[ctr]];
7494
7495                                         if (use_menu)
7496                                         {
7497                                                 if (ctr == (menu_line-1))
7498 #ifdef JP
7499                                                         strcpy(dummy, "¡Õ ");
7500 #else
7501                                                         strcpy(dummy, ">  ");
7502 #endif
7503                                                 else strcpy(dummy, "   ");
7504                                                 
7505                                         }
7506                                         /* letter/number for power selection */
7507                                         else
7508                                         {
7509                                                 sprintf(dummy, "%c) ",I2A(ctr));
7510                                         }
7511
7512                                         strcat(dummy, es_ptr->add_name);
7513
7514                                         col = TERM_WHITE;
7515                                         able[ctr] = TRUE;
7516
7517                                         if (es_ptr->essence != -1)
7518                                         {
7519                                                 strcat(dummy, format("(%s)", essence_name[es_ptr->essence]));
7520                                                 if (p_ptr->magic_num1[es_ptr->essence] < es_ptr->value) able[ctr] = FALSE;
7521                                         }
7522                                         else
7523                                         {
7524                                                 switch(es_ptr->add)
7525                                                 {
7526                                                 case ESSENCE_SH_FIRE:
7527 #ifdef JP
7528                                                         strcat(dummy, "(¾Æ´þ+ÂѲбê)");
7529 #else
7530                                                         strcat(dummy, "(brand fire + res.fire)");
7531 #endif
7532                                                         if (p_ptr->magic_num1[TR_BRAND_FIRE] < es_ptr->value) able[ctr] = FALSE;
7533                                                         if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
7534                                                         break;
7535                                                 case ESSENCE_SH_ELEC:
7536 #ifdef JP
7537                                                         strcat(dummy, "(ÅÅ·â+ÂÑÅÅ·â)");
7538 #else
7539                                                         strcat(dummy, "(brand elec. + res. elec.)");
7540 #endif
7541                                                         if (p_ptr->magic_num1[TR_BRAND_ELEC] < es_ptr->value) able[ctr] = FALSE;
7542                                                         if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
7543                                                         break;
7544                                                 case ESSENCE_SH_COLD:
7545 #ifdef JP
7546                                                         strcat(dummy, "(Åà·ë+ÂÑÎ䵤)");
7547 #else
7548                                                         strcat(dummy, "(brand cold + res. cold)");
7549 #endif
7550                                                         if (p_ptr->magic_num1[TR_BRAND_COLD] < es_ptr->value) able[ctr] = FALSE;
7551                                                         if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
7552                                                         break;
7553                                                 case ESSENCE_RESISTANCE:
7554 #ifdef JP
7555                                                         strcat(dummy, "(ÂѲбê+ÂÑÎ䵤+ÂÑÅÅ·â+ÂÑ»À)");
7556 #else
7557                                                         strcat(dummy, "(r.fire+r.cold+r.elec+r.acid)");
7558 #endif
7559                                                         if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
7560                                                         if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
7561                                                         if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
7562                                                         if (p_ptr->magic_num1[TR_RES_ACID] < es_ptr->value) able[ctr] = FALSE;
7563                                                         break;
7564                                                 case ESSENCE_SUSTAIN:
7565 #ifdef JP
7566                                                         strcat(dummy, "(ÂѲбê+ÂÑÎ䵤+ÂÑÅÅ·â+ÂÑ»À)");
7567 #else
7568                                                         strcat(dummy, "(r.fire+r.cold+r.elec+r.acid)");
7569 #endif
7570                                                         if (p_ptr->magic_num1[TR_RES_FIRE] < es_ptr->value) able[ctr] = FALSE;
7571                                                         if (p_ptr->magic_num1[TR_RES_COLD] < es_ptr->value) able[ctr] = FALSE;
7572                                                         if (p_ptr->magic_num1[TR_RES_ELEC] < es_ptr->value) able[ctr] = FALSE;
7573                                                         if (p_ptr->magic_num1[TR_RES_ACID] < es_ptr->value) able[ctr] = FALSE;
7574                                                         break;
7575                                                 }
7576                                         }
7577
7578                                         if (!able[ctr]) col = TERM_RED;
7579
7580                                         if (es_ptr->essence != -1)
7581                                         {
7582                                                 sprintf(dummy2, "%-49s %3d/%d", dummy, es_ptr->value, (int)p_ptr->magic_num1[es_ptr->essence]);
7583                                         }
7584                                         else
7585                                         {
7586                                                 sprintf(dummy2, "%-49s %3d/(\?\?)", dummy, es_ptr->value);
7587                                         }
7588
7589                                         c_prt(col, dummy2, ctr+2, x);
7590                                 }
7591                         }
7592
7593                         /* Hide the list */
7594                         else
7595                         {
7596                                 /* Hide list */
7597                                 redraw = FALSE;
7598
7599                                 /* Restore the screen */
7600                                 screen_load();
7601                         }
7602
7603                         /* Redo asking */
7604                         continue;
7605                 }
7606
7607                 if (!use_menu)
7608                 {
7609                         /* Note verify */
7610                         ask = (isupper(choice));
7611
7612                         /* Lowercase */
7613                         if (ask) choice = tolower(choice);
7614
7615                         /* Extract request */
7616                         i = (islower(choice) ? A2I(choice) : -1);
7617                 }
7618
7619                 /* Totally Illegal */
7620                 if ((i < 0) || (i >= max_num) || !able[i])
7621                 {
7622                         bell();
7623                         continue;
7624                 }
7625
7626                 /* Verify it */
7627                 if (ask)
7628                 {
7629                         char tmp_val[160];
7630
7631                         /* Prompt */
7632 #ifdef JP
7633                         (void) strnfmt(tmp_val, 78, "%s¤òÉղä·¤Þ¤¹¤«¡© ", essence_info[num[i]].add_name);
7634 #else
7635                         (void) strnfmt(tmp_val, 78, "Add the abilitiy of %s? ", essence_info[num[i]].add_name);
7636 #endif
7637
7638                         /* Belay that order */
7639                         if (!get_check(tmp_val)) continue;
7640                 }
7641
7642                 /* Stop the loop */
7643                 flag = TRUE;
7644         }
7645
7646         /* Restore the screen */
7647         if (redraw) screen_load();
7648
7649         if (!flag) return;
7650
7651 #ifdef ALLOW_REPEAT
7652         repeat_push(i);
7653         }
7654 #endif /* ALLOW_REPEAT */
7655
7656         es_ptr = &essence_info[num[i]];
7657
7658         if (es_ptr->add == ESSENCE_SLAY_GLOVE)
7659                 item_tester_tval = TV_GLOVES;
7660         else if (mode == 1)
7661                 item_tester_hook = item_tester_hook_melee_ammo;
7662         else if (es_ptr->add == ESSENCE_ATTACK)
7663                 item_tester_hook = item_tester_hook_weapon;
7664         else if (es_ptr->add == ESSENCE_AC)
7665                 item_tester_hook = item_tester_hook_armour;
7666         else
7667                 item_tester_hook = item_tester_hook_weapon_armour;
7668         item_tester_no_ryoute = TRUE;
7669
7670         /* Get an item */
7671 #ifdef JP
7672         q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò²þÎɤ·¤Þ¤¹¤«¡©";
7673         s = "²þÎɤǤ­¤ë¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
7674 #else
7675         q = "Improve which item? ";
7676         s = "You have nothing to improve.";
7677 #endif
7678
7679         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
7680
7681         /* Get the item (in the pack) */
7682         if (item >= 0)
7683         {
7684                 o_ptr = &inventory[item];
7685         }
7686
7687         /* Get the item (on the floor) */
7688         else
7689         {
7690                 o_ptr = &o_list[0 - item];
7691         }
7692
7693         if ((mode != 10) && (o_ptr->name1 || o_ptr->art_name || o_ptr->xtra3))
7694         {
7695 #ifdef JP
7696                 msg_print("¤½¤Î¥¢¥¤¥Æ¥à¤Ï¤³¤ì°Ê¾å²þÎɤǤ­¤Ê¤¤¡£");
7697 #else
7698                 msg_print("This item is no more able to be improved");
7699 #endif
7700                 return;
7701         }
7702         
7703         object_desc(o_name, o_ptr, FALSE, 0);
7704
7705         use_essence = es_ptr->value;
7706         if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_BOLT)) use_essence = (use_essence+9)/10;
7707         if (o_ptr->number > 1)
7708         {
7709                 use_essence *= o_ptr->number;
7710 #ifdef JP
7711                 msg_format("%d¸Ä¤¢¤ë¤Î¤Ç¥¨¥Ã¥»¥ó¥¹¤Ï%dɬÍפǤ¹¡£", o_ptr->number, use_essence);
7712 #else
7713                 msg_format("It will take %d essences.",use_essence);
7714 #endif
7715
7716         }
7717
7718         if (es_ptr->essence != -1)
7719         {
7720                 if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
7721                 {
7722 #ifdef JP
7723                         msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
7724 #else
7725                         msg_print("You don't have enough essences.");
7726 #endif
7727                         return;
7728                 }
7729                 if (is_pval_flag(es_ptr->add))
7730                 {
7731                         if (es_ptr->add == TR_BLOWS)
7732                         {
7733                                 if (o_ptr->pval > 1)
7734                                 {
7735 #ifdef JP
7736                                         if (!get_check("½¤ÀµÃͤÏ1¤Ë¤Ê¤ê¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡©")) return;
7737 #else
7738                                         if (!get_check("The magic number of this weapon will become 1. Are you sure? ")) return;
7739 #endif
7740                                 }
7741                                 o_ptr->pval = 1;
7742                         }
7743                         else if (o_ptr->pval)
7744                         {
7745                                 use_essence *= o_ptr->pval;
7746 #ifdef JP
7747                                 msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£",use_essence);
7748 #else
7749                                 msg_format("It will take %d essences.",use_essence);
7750 #endif
7751                         }
7752                         else
7753                         {
7754                                 char tmp[80];
7755                                 char tmp_val[160];
7756                                 int pval;
7757                                 int limit = MIN(5, p_ptr->magic_num1[es_ptr->essence]/es_ptr->value);
7758
7759
7760 #ifdef JP
7761                                 sprintf(tmp, "¤¤¤¯¤ÄÉղä·¤Þ¤¹¤«¡© (1-%d): ", limit);
7762 #else
7763                                 sprintf(tmp, "Enchant how many? (1-%d): ", limit);
7764 #endif
7765                                 strcpy(tmp_val, "1");
7766
7767                                 if (!get_string(tmp, tmp_val, 1)) return;
7768                                 pval = atoi(tmp_val);
7769                                 if (pval > limit) pval = limit;
7770                                 else if (pval < 1) pval = 1;
7771                                 o_ptr->pval = pval;
7772                                 use_essence *= pval;
7773 #ifdef JP
7774                                 msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£",use_essence);
7775 #else
7776                                 msg_format("It will take %d essences.",use_essence);
7777 #endif
7778                         }
7779                         if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
7780                         {
7781 #ifdef JP
7782                                 msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
7783 #else
7784                                 msg_print("You don't have enough essences.");
7785 #endif
7786                                 return;
7787                         }
7788                 }
7789                 else if (es_ptr->add == ESSENCE_SLAY_GLOVE)
7790                 {
7791                         char tmp_val[160];
7792                         int val;
7793                         int get_to_h, get_to_d;
7794
7795                         strcpy(tmp_val, "1");
7796 #ifdef JP
7797                         if (!get_string(format("¤¤¤¯¤ÄÉղä·¤Þ¤¹¤«¡© (1-%d):", p_ptr->lev/7+3), tmp_val, 2)) return;
7798 #else
7799                         if (!get_string(format("Enchant how many? (1-%d):", p_ptr->lev/7+3), tmp_val, 2)) return;
7800 #endif
7801                         val = atoi(tmp_val);
7802                         if (val > p_ptr->lev/7+3) val = p_ptr->lev/7+3;
7803                         else if (val < 1) val = 1;
7804                         use_essence *= val;
7805 #ifdef JP
7806                         msg_format("¥¨¥Ã¥»¥ó¥¹¤ò%d¸Ä»ÈÍѤ·¤Þ¤¹¡£",use_essence);
7807 #else
7808                         msg_format("It will take %d essences.",use_essence);
7809 #endif
7810                         if (p_ptr->magic_num1[es_ptr->essence] < use_essence)
7811                         {
7812 #ifdef JP
7813                                 msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
7814 #else
7815                                 msg_print("You don't have enough essences");
7816 #endif
7817                                 return;
7818                         }
7819                         get_to_h = ((val+1)/2+randint0(val/2+1));
7820                         get_to_d = ((val+1)/2+randint0(val/2+1));
7821                         o_ptr->xtra4 = (get_to_h<<8)+get_to_d;
7822                         o_ptr->to_h += get_to_h;
7823                         o_ptr->to_d += get_to_d;
7824                 }
7825                 p_ptr->magic_num1[es_ptr->essence] -= use_essence;
7826                 if (es_ptr->add == ESSENCE_ATTACK)
7827                 {
7828                         if ((o_ptr->to_h >= p_ptr->lev/5+5) && (o_ptr->to_d >= p_ptr->lev/5+5))
7829                         {
7830 #ifdef JP
7831                                 msg_print("²þÎɤ˼ºÇÔ¤·¤¿¡£");
7832 #else
7833                                 msg_print("You failed to enchant.");
7834 #endif
7835                                 energy_use = 100;
7836                                 return;
7837                         }
7838                         else
7839                         {
7840                                 if (o_ptr->to_h < p_ptr->lev/5+5) o_ptr->to_h++;
7841                                 if (o_ptr->to_d < p_ptr->lev/5+5) o_ptr->to_d++;
7842                         }
7843                 }
7844                 else if (es_ptr->add == ESSENCE_AC)
7845                 {
7846                         if (o_ptr->to_a >= p_ptr->lev/5+5)
7847                         {
7848 #ifdef JP
7849                                 msg_print("²þÎɤ˼ºÇÔ¤·¤¿¡£");
7850 #else
7851                                 msg_print("You failed to enchant.");
7852 #endif
7853                                 energy_use = 100;
7854                                 return;
7855                         }
7856                         else
7857                         {
7858                                 if (o_ptr->to_a < p_ptr->lev/5+5) o_ptr->to_a++;
7859                         }
7860                 }
7861                 else
7862                 {
7863                         o_ptr->xtra3 = es_ptr->add + 1;
7864                 }
7865         }
7866         else
7867         {
7868                 bool success = TRUE;
7869
7870                 switch(es_ptr->add)
7871                 {
7872                 case ESSENCE_SH_FIRE:
7873                         if ((p_ptr->magic_num1[TR_BRAND_FIRE] < use_essence) || (p_ptr->magic_num1[TR_RES_FIRE] < use_essence))
7874                         {
7875                                 success = FALSE;
7876                                 break;
7877                         }
7878                         p_ptr->magic_num1[TR_BRAND_FIRE] -= use_essence;
7879                         p_ptr->magic_num1[TR_RES_FIRE] -= use_essence;
7880                         break;
7881                 case ESSENCE_SH_ELEC:
7882                         if ((p_ptr->magic_num1[TR_BRAND_ELEC] < use_essence) || (p_ptr->magic_num1[TR_RES_ELEC] < use_essence))
7883                         {
7884                                 success = FALSE;
7885                                 break;
7886                         }
7887                         p_ptr->magic_num1[TR_BRAND_ELEC] -= use_essence;
7888                         p_ptr->magic_num1[TR_RES_ELEC] -= use_essence;
7889                         break;
7890                 case ESSENCE_SH_COLD:
7891                         if ((p_ptr->magic_num1[TR_BRAND_COLD] < use_essence) || (p_ptr->magic_num1[TR_RES_COLD] < use_essence))
7892                         {
7893                                 success = FALSE;
7894                                 break;
7895                         }
7896                         p_ptr->magic_num1[TR_BRAND_COLD] -= use_essence;
7897                         p_ptr->magic_num1[TR_RES_COLD] -= use_essence;
7898                         break;
7899                 case ESSENCE_RESISTANCE:
7900                 case ESSENCE_SUSTAIN:
7901                         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))
7902                         {
7903                                 success = FALSE;
7904                                 break;
7905                         }
7906                         p_ptr->magic_num1[TR_RES_ACID] -= use_essence;
7907                         p_ptr->magic_num1[TR_RES_ELEC] -= use_essence;
7908                         p_ptr->magic_num1[TR_RES_FIRE] -= use_essence;
7909                         p_ptr->magic_num1[TR_RES_COLD] -= use_essence;
7910                         break;
7911                 }
7912                 if (!success)
7913                 {
7914 #ifdef JP
7915                         msg_print("¥¨¥Ã¥»¥ó¥¹¤¬Â­¤ê¤Ê¤¤¡£");
7916 #else
7917                         msg_print("You don't have enough essences");
7918 #endif
7919                         return;
7920                 }
7921                 if (es_ptr->add == ESSENCE_SUSTAIN)
7922                 {
7923                         add_flag(o_ptr->art_flags, TR_IGNORE_ACID);
7924                         add_flag(o_ptr->art_flags, TR_IGNORE_ELEC);
7925                         add_flag(o_ptr->art_flags, TR_IGNORE_FIRE);
7926                         add_flag(o_ptr->art_flags, TR_IGNORE_COLD);
7927                 }
7928                 else
7929                 {
7930                         o_ptr->xtra3 = es_ptr->add + 1;
7931                 }
7932         }
7933
7934         energy_use = 100;
7935
7936 #ifdef JP
7937         msg_format("%s¤Ë%s¤ÎǽÎϤòÉղä·¤Þ¤·¤¿¡£", o_name, es_ptr->add_name);
7938 #else
7939         msg_format("You have added ability of %s to %s.", es_ptr->add_name, o_name);
7940 #endif
7941
7942         /* Combine the pack */
7943         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
7944
7945         /* Window stuff */
7946         p_ptr->window |= (PW_INVEN);
7947 }
7948
7949
7950 static bool item_tester_hook_kaji(object_type *o_ptr)
7951 {
7952         switch (o_ptr->tval)
7953         {
7954                 case TV_SWORD:
7955                 case TV_HAFTED:
7956                 case TV_POLEARM:
7957                 case TV_DIGGING:
7958                 case TV_BOW:
7959                 case TV_BOLT:
7960                 case TV_ARROW:
7961                 case TV_SHOT:
7962                 case TV_DRAG_ARMOR:
7963                 case TV_HARD_ARMOR:
7964                 case TV_SOFT_ARMOR:
7965                 case TV_SHIELD:
7966                 case TV_CLOAK:
7967                 case TV_CROWN:
7968                 case TV_HELM:
7969                 case TV_BOOTS:
7970                 case TV_GLOVES:
7971                 {
7972                         if (o_ptr->xtra3) return (TRUE);
7973                 }
7974         }
7975
7976         return (FALSE);
7977 }
7978
7979
7980 static void erase_essence(void)
7981 {
7982         int item;
7983         cptr q, s;
7984         object_type *o_ptr;
7985         char o_name[MAX_NLEN];
7986         u32b flgs[TR_FLAG_SIZE];
7987
7988         item_tester_hook = item_tester_hook_kaji;
7989
7990         /* Get an item */
7991 #ifdef JP
7992         q = "¤É¤Î¥¢¥¤¥Æ¥à¤Î¥¨¥Ã¥»¥ó¥¹¤ò¾Ãµî¤·¤Þ¤¹¤«¡©";
7993         s = "¥¨¥Ã¥»¥ó¥¹¤òÉղä·¤¿¥¢¥¤¥Æ¥à¤¬¤¢¤ê¤Þ¤»¤ó¡£";
7994 #else
7995         q = "Remove from which item? ";
7996         s = "You have nothing to remove essence.";
7997 #endif
7998
7999         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR))) return;
8000
8001         /* Get the item (in the pack) */
8002         if (item >= 0)
8003         {
8004                 o_ptr = &inventory[item];
8005         }
8006
8007         /* Get the item (on the floor) */
8008         else
8009         {
8010                 o_ptr = &o_list[0 - item];
8011         }
8012
8013         object_desc(o_name, o_ptr, FALSE, 0);
8014 #ifdef JP
8015         if (!get_check(format("¤è¤í¤·¤¤¤Ç¤¹¤«¡©[%s]", o_name))) return;
8016 #else
8017         if (!get_check(format("Are you sure?[%s]", o_name))) return;
8018 #endif
8019
8020         energy_use = 100;
8021
8022         if (o_ptr->xtra3 == 1+ESSENCE_SLAY_GLOVE)
8023         {
8024                 o_ptr->to_h -= (o_ptr->xtra4>>8);
8025                 o_ptr->to_d -= (o_ptr->xtra4 & 0x000f);
8026                 o_ptr->xtra4 = 0;
8027         }
8028         o_ptr->xtra3 = 0;
8029         object_flags(o_ptr, flgs);
8030         if (!(have_pval_flags(flgs))) o_ptr->pval = 0;
8031 #ifdef JP
8032         msg_print("¥¨¥Ã¥»¥ó¥¹¤ò¼è¤êµî¤Ã¤¿¡£");
8033 #else
8034         msg_print("You removed all essence you have added");
8035 #endif
8036
8037         /* Combine the pack */
8038         p_ptr->notice |= (PN_COMBINE | PN_REORDER);
8039
8040         /* Window stuff */
8041         p_ptr->window |= (PW_INVEN);
8042 }
8043
8044 void do_cmd_kaji(bool only_browse)
8045 {
8046         int mode = 0;
8047         char choice;
8048
8049         int menu_line = (use_menu ? 1 : 0);
8050
8051         if (!only_browse)
8052         {
8053                 if (p_ptr->confused)
8054                 {
8055 #ifdef JP
8056                         msg_print("º®Í𤷤Ƥ¤¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
8057 #else
8058                         msg_print("You are too confused!");
8059 #endif
8060
8061                         return;
8062                 }
8063                 if (p_ptr->blind)
8064                 {
8065 #ifdef JP
8066                         msg_print("Ìܤ¬¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
8067 #else
8068                         msg_print("You are blind!");
8069 #endif
8070
8071                         return;
8072                 }
8073                 if (p_ptr->image)
8074                 {
8075 #ifdef JP
8076                         msg_print("¤¦¤Þ¤¯¸«¤¨¤Ê¤¯¤Æºî¶È¤Ç¤­¤Ê¤¤¡ª");
8077 #else
8078                         msg_print("You are hullcinating!");
8079 #endif
8080
8081                         return;
8082                 }
8083         }
8084
8085 #ifdef ALLOW_REPEAT
8086         if (!(repeat_pull(&mode) && 1 <= mode && mode <= 5))
8087         {
8088 #endif /* ALLOW_REPEAT */
8089
8090         if (only_browse) screen_save();
8091         do {
8092         if (!only_browse) screen_save();
8093         if (use_menu)
8094         {
8095                 while(!mode)
8096                 {
8097 #ifdef JP
8098                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹°ìÍ÷", (menu_line == 1) ? "¡Õ" : "  "), 2, 14);
8099                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹Ãê½Ð", (menu_line == 2) ? "¡Õ" : "  "), 3, 14);
8100                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹¾Ãµî", (menu_line == 3) ? "¡Õ" : "  "), 4, 14);
8101                         prt(format(" %s ¥¨¥Ã¥»¥ó¥¹ÉÕ²Ã", (menu_line == 4) ? "¡Õ" : "  "), 5, 14);
8102                         prt(format(" %s Éð´ï/Ëɶñ¶¯²½", (menu_line == 5) ? "¡Õ" : "  "), 6, 14);
8103                         prt(format("¤É¤Î¼ïÎà¤Îµ»½Ñ¤ò%s¤Þ¤¹¤«¡©", only_browse ? "Ä´¤Ù" : "»È¤¤"), 0, 0);
8104 #else
8105                         prt(format(" %s List essences", (menu_line == 1) ? "> " : "  "), 2, 14);
8106                         prt(format(" %s Extract essence", (menu_line == 2) ? "> " : "  "), 3, 14);
8107                         prt(format(" %s Remove essence", (menu_line == 3) ? "> " : "  "), 4, 14);
8108                         prt(format(" %s Add essence", (menu_line == 4) ? "> " : "  "), 5, 14);
8109                         prt(format(" %s Enchant weapon/armor", (menu_line == 5) ? "> " : "  "), 6, 14);
8110                         prt(format("Choose command from menu."), 0, 0);
8111 #endif
8112                         choice = inkey();
8113                         switch(choice)
8114                         {
8115                         case ESCAPE:
8116                         case 'z':
8117                         case 'Z':
8118                                 screen_load();
8119                                 return;
8120                         case '2':
8121                         case 'j':
8122                         case 'J':
8123                                 menu_line++;
8124                                 break;
8125                         case '8':
8126                         case 'k':
8127                         case 'K':
8128                                 menu_line+= 4;
8129                                 break;
8130                         case '\r':
8131                         case '\n':
8132                         case 'x':
8133                         case 'X':
8134                                 mode = menu_line;
8135                                 break;
8136                         }
8137                         if (menu_line > 5) menu_line -= 5;
8138                 }
8139         }
8140
8141         else
8142         {
8143                 while (!mode)
8144                 {
8145 #ifdef JP
8146                         prt("  a) ¥¨¥Ã¥»¥ó¥¹°ìÍ÷", 2, 14);
8147                         prt("  b) ¥¨¥Ã¥»¥ó¥¹Ãê½Ð", 3, 14);
8148                         prt("  c) ¥¨¥Ã¥»¥ó¥¹¾Ãµî", 4, 14);
8149                         prt("  d) ¥¨¥Ã¥»¥ó¥¹ÉÕ²Ã", 5, 14);
8150                         prt("  e) Éð´ï/Ëɶñ¶¯²½", 6, 14);
8151                         if (!get_com(format("¤É¤ÎǽÎϤò%s¤Þ¤¹¤«:", only_browse ? "Ä´¤Ù" : "»È¤¤"), &choice, TRUE))
8152 #else
8153                         prt("  a) List essences", 2, 14);
8154                         prt("  b) Extract essence", 3, 14);
8155                         prt("  c) Remove essence", 4, 14);
8156                         prt("  d) Add essence", 5, 14);
8157                         prt("  e) Enchant weapon/armor", 6, 14);
8158                         if (!get_com("Command :", &choice, TRUE))
8159 #endif
8160                         {
8161                                 screen_load();
8162                                 return;
8163                         }
8164                         switch (choice)
8165                         {
8166                         case 'A':
8167                         case 'a':
8168                                 mode = 1;
8169                                 break;
8170                         case 'B':
8171                         case 'b':
8172                                 mode = 2;
8173                                 break;
8174                         case 'C':
8175                         case 'c':
8176                                 mode = 3;
8177                                 break;
8178                         case 'D':
8179                         case 'd':
8180                                 mode = 4;
8181                                 break;
8182                         case 'E':
8183                         case 'e':
8184                                 mode = 5;
8185                                 break;
8186                         }
8187                 }
8188         }
8189
8190         if (only_browse)
8191         {
8192                 char temp[62*5];
8193                 int line, j;
8194
8195                 /* Clear lines, position cursor  (really should use strlen here) */
8196                 Term_erase(14, 21, 255);
8197                 Term_erase(14, 20, 255);
8198                 Term_erase(14, 19, 255);
8199                 Term_erase(14, 18, 255);
8200                 Term_erase(14, 17, 255);
8201                 Term_erase(14, 16, 255);
8202
8203                 roff_to_buf( kaji_tips[mode-1],62,temp);
8204                 for(j=0, line = 17;temp[j];j+=(1+strlen(&temp[j])))
8205                 {
8206                         prt(&temp[j], line, 15);
8207                         line++;
8208                 }
8209                 mode = 0;
8210         }
8211         if (!only_browse) screen_load();
8212         } while (only_browse);
8213 #ifdef ALLOW_REPEAT
8214         repeat_push(mode);
8215         }
8216 #endif /* ALLOW_REPEAT */
8217
8218         switch(mode)
8219         {
8220                 case 1: display_essence();break;
8221                 case 2: drain_essence();break;
8222                 case 3: erase_essence();break;
8223                 case 4:
8224                         mode = choose_essence();
8225                         if (mode == 0)
8226                                 break;
8227                         add_essence(mode);
8228                         break;
8229                 case 5: add_essence(10);break;
8230         }
8231 }