OSDN Git Service

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