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