OSDN Git Service

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