OSDN Git Service

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