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