OSDN Git Service

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