OSDN Git Service

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