OSDN Git Service

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