Angband 3.0.9b.
[angband.git] / src / object2.c
blob4d45400c82d9fd46923f4e772f96691dd1c4b6a2
1 /* File: object2.c */
3 /*
4 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies. Other copyrights may also apply.
9 */
11 #include "angband.h"
15 * Excise a dungeon object from any stacks
17 void excise_object_idx(int o_idx)
19 object_type *j_ptr;
21 s16b this_o_idx, next_o_idx = 0;
23 s16b prev_o_idx = 0;
26 /* Object */
27 j_ptr = &o_list[o_idx];
29 /* Monster */
30 if (j_ptr->held_m_idx)
32 monster_type *m_ptr;
34 /* Monster */
35 m_ptr = &mon_list[j_ptr->held_m_idx];
37 /* Scan all objects in the grid */
38 for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
40 object_type *o_ptr;
42 /* Get the object */
43 o_ptr = &o_list[this_o_idx];
45 /* Get the next object */
46 next_o_idx = o_ptr->next_o_idx;
48 /* Done */
49 if (this_o_idx == o_idx)
51 /* No previous */
52 if (prev_o_idx == 0)
54 /* Remove from list */
55 m_ptr->hold_o_idx = next_o_idx;
58 /* Real previous */
59 else
61 object_type *i_ptr;
63 /* Previous object */
64 i_ptr = &o_list[prev_o_idx];
66 /* Remove from list */
67 i_ptr->next_o_idx = next_o_idx;
70 /* Forget next pointer */
71 o_ptr->next_o_idx = 0;
73 /* Done */
74 break;
77 /* Save prev_o_idx */
78 prev_o_idx = this_o_idx;
82 /* Dungeon */
83 else
85 int y = j_ptr->iy;
86 int x = j_ptr->ix;
88 /* Scan all objects in the grid */
89 for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = next_o_idx)
91 object_type *o_ptr;
93 /* Get the object */
94 o_ptr = &o_list[this_o_idx];
96 /* Get the next object */
97 next_o_idx = o_ptr->next_o_idx;
99 /* Done */
100 if (this_o_idx == o_idx)
102 /* No previous */
103 if (prev_o_idx == 0)
105 /* Remove from list */
106 cave_o_idx[y][x] = next_o_idx;
109 /* Real previous */
110 else
112 object_type *i_ptr;
114 /* Previous object */
115 i_ptr = &o_list[prev_o_idx];
117 /* Remove from list */
118 i_ptr->next_o_idx = next_o_idx;
121 /* Forget next pointer */
122 o_ptr->next_o_idx = 0;
124 /* Done */
125 break;
128 /* Save prev_o_idx */
129 prev_o_idx = this_o_idx;
136 * Delete a dungeon object
138 * Handle "stacks" of objects correctly.
140 void delete_object_idx(int o_idx)
142 object_type *j_ptr;
144 /* Excise */
145 excise_object_idx(o_idx);
147 /* Object */
148 j_ptr = &o_list[o_idx];
150 /* Dungeon floor */
151 if (!(j_ptr->held_m_idx))
153 int y, x;
155 /* Location */
156 y = j_ptr->iy;
157 x = j_ptr->ix;
159 /* Visual update */
160 lite_spot(y, x);
163 /* Wipe the object */
164 object_wipe(j_ptr);
166 /* Count objects */
167 o_cnt--;
172 * Deletes all objects at given location
174 void delete_object(int y, int x)
176 s16b this_o_idx, next_o_idx = 0;
179 /* Paranoia */
180 if (!in_bounds(y, x)) return;
183 /* Scan all objects in the grid */
184 for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = next_o_idx)
186 object_type *o_ptr;
188 /* Get the object */
189 o_ptr = &o_list[this_o_idx];
191 /* Get the next object */
192 next_o_idx = o_ptr->next_o_idx;
194 /* Wipe the object */
195 object_wipe(o_ptr);
197 /* Count objects */
198 o_cnt--;
201 /* Objects are gone */
202 cave_o_idx[y][x] = 0;
204 /* Visual update */
205 lite_spot(y, x);
211 * Move an object from index i1 to index i2 in the object list
213 static void compact_objects_aux(int i1, int i2)
215 int i;
217 object_type *o_ptr;
220 /* Do nothing */
221 if (i1 == i2) return;
224 /* Repair objects */
225 for (i = 1; i < o_max; i++)
227 /* Get the object */
228 o_ptr = &o_list[i];
230 /* Skip "dead" objects */
231 if (!o_ptr->k_idx) continue;
233 /* Repair "next" pointers */
234 if (o_ptr->next_o_idx == i1)
236 /* Repair */
237 o_ptr->next_o_idx = i2;
242 /* Get the object */
243 o_ptr = &o_list[i1];
246 /* Monster */
247 if (o_ptr->held_m_idx)
249 monster_type *m_ptr;
251 /* Get the monster */
252 m_ptr = &mon_list[o_ptr->held_m_idx];
254 /* Repair monster */
255 if (m_ptr->hold_o_idx == i1)
257 /* Repair */
258 m_ptr->hold_o_idx = i2;
262 /* Dungeon */
263 else
265 int y, x;
267 /* Get location */
268 y = o_ptr->iy;
269 x = o_ptr->ix;
271 /* Repair grid */
272 if (cave_o_idx[y][x] == i1)
274 /* Repair */
275 cave_o_idx[y][x] = i2;
280 /* Hack -- move object */
281 COPY(&o_list[i2], &o_list[i1], object_type);
283 /* Hack -- wipe hole */
284 object_wipe(o_ptr);
289 * Compact and reorder the object list
291 * This function can be very dangerous, use with caution!
293 * When compacting objects, we first destroy gold, on the basis that by the
294 * time item compaction becomes an issue, the player really won't care.
295 * We also nuke items marked as squelch.
297 * When compacting other objects, we base the saving throw on a combination of
298 * object level, distance from player, and current "desperation".
300 * After compacting, we "reorder" the objects into a more compact order, and we
301 * reset the allocation info, and the "live" array.
303 void compact_objects(int size)
305 int py = p_ptr->py;
306 int px = p_ptr->px;
308 int i, y, x, cnt;
310 int cur_lev, cur_dis, chance;
313 /* Reorder objects when not passed a size */
314 if (!size)
316 /* Excise dead objects (backwards!) */
317 for (i = o_max - 1; i >= 1; i--)
319 object_type *o_ptr = &o_list[i];
321 /* Skip real objects */
322 if (o_ptr->k_idx) continue;
324 /* Move last object into open hole */
325 compact_objects_aux(o_max - 1, i);
327 /* Compress "o_max" */
328 o_max--;
331 return;
335 /* Message */
336 msg_print("Compacting objects...");
338 /* Redraw map */
339 p_ptr->redraw |= (PR_MAP);
341 /* Window stuff */
342 p_ptr->window |= (PW_OVERHEAD | PW_MAP);
347 /*** Try destroying objects ***/
349 /* First do gold */
350 for (i = 1; (i < o_max) && (size); i++)
352 object_type *o_ptr = &o_list[i];
354 /* Nuke gold or squelched items */
355 if (o_ptr->tval == TV_GOLD || squelch_item_ok(o_ptr))
357 delete_object_idx(i);
358 size--;
363 /* Compact at least 'size' objects */
364 for (cnt = 1; size; cnt++)
366 /* Get more vicious each iteration */
367 cur_lev = 5 * cnt;
369 /* Get closer each iteration */
370 cur_dis = 5 * (20 - cnt);
372 /* Examine the objects */
373 for (i = 1; (i < o_max) && (size); i++)
375 object_type *o_ptr = &o_list[i];
376 object_kind *k_ptr = &k_info[o_ptr->k_idx];
378 /* Skip dead objects */
379 if (!o_ptr->k_idx) continue;
381 /* Hack -- High level objects start out "immune" */
382 if (k_ptr->level > cur_lev && !k_ptr->squelch)
383 continue;
385 /* Monster */
386 if (o_ptr->held_m_idx)
388 monster_type *m_ptr;
390 /* Get the monster */
391 m_ptr = &mon_list[o_ptr->held_m_idx];
393 /* Get the location */
394 y = m_ptr->fy;
395 x = m_ptr->fx;
397 /* Monsters protect their objects */
398 if ((rand_int(100) < 90) && !k_ptr->squelch)
399 continue;
402 /* Dungeon */
403 else
405 /* Get the location */
406 y = o_ptr->iy;
407 x = o_ptr->ix;
410 /* Nearby objects start out "immune" */
411 if ((cur_dis > 0) && (distance(py, px, y, x) < cur_dis) && !k_ptr->squelch)
412 continue;
414 /* Saving throw */
415 chance = 90;
418 /* Hack -- only compact artifacts in emergencies */
419 if (artifact_p(o_ptr) && (cnt < 1000)) chance = 100;
421 /* Apply the saving throw */
422 if (rand_int(100) < chance) continue;
424 /* Delete the object */
425 delete_object_idx(i);
426 size--;
431 /* Reorder objects */
432 compact_objects(0);
439 * Delete all the items when player leaves the level
441 * Note -- we do NOT visually reflect these (irrelevant) changes
443 * Hack -- we clear the "cave_o_idx[y][x]" field for every grid,
444 * and the "m_ptr->next_o_idx" field for every monster, since
445 * we know we are clearing every object. Technically, we only
446 * clear those fields for grids/monsters containing objects,
447 * and we clear it once for every such object.
449 void wipe_o_list(void)
451 int i;
453 /* Delete the existing objects */
454 for (i = 1; i < o_max; i++)
456 object_type *o_ptr = &o_list[i];
458 /* Skip dead objects */
459 if (!o_ptr->k_idx) continue;
461 /* Preserve artifacts */
462 if (!character_dungeon || !adult_no_preserve)
464 /* Preserve only unknown artifacts */
465 if (artifact_p(o_ptr) && !object_known_p(o_ptr))
466 a_info[o_ptr->name1].cur_num = 0;
469 /* Monster */
470 if (o_ptr->held_m_idx)
472 monster_type *m_ptr;
474 /* Monster */
475 m_ptr = &mon_list[o_ptr->held_m_idx];
477 /* Hack -- see above */
478 m_ptr->hold_o_idx = 0;
481 /* Dungeon */
482 else
484 /* Get the location */
485 int y = o_ptr->iy;
486 int x = o_ptr->ix;
488 /* Hack -- see above */
489 cave_o_idx[y][x] = 0;
492 /* Wipe the object */
493 (void)WIPE(o_ptr, object_type);
496 /* Reset "o_max" */
497 o_max = 1;
499 /* Reset "o_cnt" */
500 o_cnt = 0;
505 * Get and return the index of a "free" object.
507 * This routine should almost never fail, but in case it does,
508 * we must be sure to handle "failure" of this routine.
510 s16b o_pop(void)
512 int i;
515 /* Initial allocation */
516 if (o_max < z_info->o_max)
518 /* Get next space */
519 i = o_max;
521 /* Expand object array */
522 o_max++;
524 /* Count objects */
525 o_cnt++;
527 /* Use this object */
528 return (i);
532 /* Recycle dead objects */
533 for (i = 1; i < o_max; i++)
535 object_type *o_ptr;
537 /* Get the object */
538 o_ptr = &o_list[i];
540 /* Skip live objects */
541 if (o_ptr->k_idx) continue;
543 /* Count objects */
544 o_cnt++;
546 /* Use this object */
547 return (i);
551 /* Warn the player (except during dungeon creation) */
552 if (character_dungeon) msg_print("Too many objects!");
554 /* Oops */
555 return (0);
560 * Get the first object at a dungeon location
561 * or NULL if there isn't one.
563 object_type* get_first_object(int y, int x)
565 s16b o_idx = cave_o_idx[y][x];
567 if (o_idx) return (&o_list[o_idx]);
569 /* No object */
570 return (NULL);
575 * Get the next object in a stack or
576 * NULL if there isn't one.
578 object_type* get_next_object(const object_type *o_ptr)
580 if (o_ptr->next_o_idx) return (&o_list[o_ptr->next_o_idx]);
582 /* No more objects */
583 return (NULL);
588 * Apply a "object restriction function" to the "object allocation table"
590 errr get_obj_num_prep(void)
592 int i;
594 /* Get the entry */
595 alloc_entry *table = alloc_kind_table;
597 /* Scan the allocation table */
598 for (i = 0; i < alloc_kind_size; i++)
600 /* Accept objects which pass the restriction, if any */
601 if (!get_obj_num_hook || (*get_obj_num_hook)(table[i].index))
603 /* Accept this object */
604 table[i].prob2 = table[i].prob1;
607 /* Do not use this object */
608 else
610 /* Decline this object */
611 table[i].prob2 = 0;
615 /* Success */
616 return (0);
622 * Choose an object kind that seems "appropriate" to the given level
624 * This function uses the "prob2" field of the "object allocation table",
625 * and various local information, to calculate the "prob3" field of the
626 * same table, which is then used to choose an "appropriate" object, in
627 * a relatively efficient manner.
629 * It is (slightly) more likely to acquire an object of the given level
630 * than one of a lower level. This is done by choosing several objects
631 * appropriate to the given level and keeping the "hardest" one.
633 * Note that if no objects are "appropriate", then this function will
634 * fail, and return zero, but this should *almost* never happen.
636 s16b get_obj_num(int level)
638 int i, j, p;
640 int k_idx;
642 long value, total;
644 object_kind *k_ptr;
646 alloc_entry *table = alloc_kind_table;
649 /* Boost level */
650 if (level > 0)
652 /* Occasional "boost" */
653 if (rand_int(GREAT_OBJ) == 0)
655 /* What a bizarre calculation */
656 level = 1 + (level * MAX_DEPTH / randint(MAX_DEPTH));
661 /* Reset total */
662 total = 0L;
664 /* Process probabilities */
665 for (i = 0; i < alloc_kind_size; i++)
667 /* Objects are sorted by depth */
668 if (table[i].level > level) break;
670 /* Default */
671 table[i].prob3 = 0;
673 /* Get the index */
674 k_idx = table[i].index;
676 /* Get the actual kind */
677 k_ptr = &k_info[k_idx];
679 /* Hack -- prevent embedded chests */
680 if (opening_chest && (k_ptr->tval == TV_CHEST)) continue;
682 /* Accept */
683 table[i].prob3 = table[i].prob2;
685 /* Total */
686 total += table[i].prob3;
689 /* No legal objects */
690 if (total <= 0) return (0);
693 /* Pick an object */
694 value = rand_int(total);
696 /* Find the object */
697 for (i = 0; i < alloc_kind_size; i++)
699 /* Found the entry */
700 if (value < table[i].prob3) break;
702 /* Decrement */
703 value = value - table[i].prob3;
707 /* Power boost */
708 p = rand_int(100);
710 /* Try for a "better" object once (50%) or twice (10%) */
711 if (p < 60)
713 /* Save old */
714 j = i;
716 /* Pick a object */
717 value = rand_int(total);
719 /* Find the monster */
720 for (i = 0; i < alloc_kind_size; i++)
722 /* Found the entry */
723 if (value < table[i].prob3) break;
725 /* Decrement */
726 value = value - table[i].prob3;
729 /* Keep the "best" one */
730 if (table[i].level < table[j].level) i = j;
733 /* Try for a "better" object twice (10%) */
734 if (p < 10)
736 /* Save old */
737 j = i;
739 /* Pick a object */
740 value = rand_int(total);
742 /* Find the object */
743 for (i = 0; i < alloc_kind_size; i++)
745 /* Found the entry */
746 if (value < table[i].prob3) break;
748 /* Decrement */
749 value = value - table[i].prob3;
752 /* Keep the "best" one */
753 if (table[i].level < table[j].level) i = j;
757 /* Result */
758 return (table[i].index);
769 * Known is true when the "attributes" of an object are "known".
771 * These attributes include tohit, todam, toac, cost, and pval (charges).
773 * Note that "knowing" an object gives you everything that an "awareness"
774 * gives you, and much more. In fact, the player is always "aware" of any
775 * item which he "knows", except items in stores.
777 * But having knowledge of, say, one "wand of wonder", does not, by itself,
778 * give you knowledge, or even awareness, of other "wands of wonder".
779 * It happens that most "identify" routines (including "buying from a shop")
780 * will make the player "aware" of the object as well as "know" it.
782 * This routine also removes any inscriptions generated by "feelings".
784 void object_known(object_type *o_ptr)
786 /* Remove special inscription, if any */
787 if (o_ptr->pseudo) o_ptr->pseudo = 0;
789 /* The object is not "sensed" */
790 o_ptr->ident &= ~(IDENT_SENSE);
792 /* Clear the "Empty" info */
793 o_ptr->ident &= ~(IDENT_EMPTY);
795 /* Now we know about the item */
796 o_ptr->ident |= (IDENT_KNOWN);
804 * The player is now aware of the effects of the given object.
806 void object_aware(object_type *o_ptr)
808 /* Fully aware of the effects */
809 k_info[o_ptr->k_idx].aware = TRUE;
811 /* Scrolls can change the graphics when becoming aware */
812 if (o_ptr->tval == TV_SCROLL)
814 /* Redraw map */
815 p_ptr->redraw |= (PR_MAP);
817 /* Window stuff */
818 p_ptr->window |= (PW_OVERHEAD | PW_MAP);
825 * Something has been "sampled"
827 void object_tried(object_type *o_ptr)
829 /* Mark it as tried (even if "aware") */
830 k_info[o_ptr->k_idx].tried = TRUE;
835 * Determine if a weapon is 'blessed'
837 bool is_blessed(const object_type *o_ptr)
839 u32b f1, f2, f3;
841 /* Get the flags */
842 object_flags(o_ptr, &f1, &f2, &f3);
844 /* Is the object blessed? */
845 return ((f3 & TR3_BLESSED) ? TRUE : FALSE);
851 * Return the "value" of an "unknown" item
852 * Make a guess at the value of non-aware items
854 static s32b object_value_base(const object_type *o_ptr)
856 object_kind *k_ptr = &k_info[o_ptr->k_idx];
858 /* Use template cost for aware objects */
859 if (object_aware_p(o_ptr)) return (k_ptr->cost);
861 /* Analyze the type */
862 switch (o_ptr->tval)
864 /* Un-aware Food */
865 case TV_FOOD: return (5L);
867 /* Un-aware Potions */
868 case TV_POTION: return (20L);
870 /* Un-aware Scrolls */
871 case TV_SCROLL: return (20L);
873 /* Un-aware Staffs */
874 case TV_STAFF: return (70L);
876 /* Un-aware Wands */
877 case TV_WAND: return (50L);
879 /* Un-aware Rods */
880 case TV_ROD: return (90L);
882 /* Un-aware Rings */
883 case TV_RING: return (45L);
885 /* Un-aware Amulets */
886 case TV_AMULET: return (45L);
889 /* Paranoia -- Oops */
890 return (0L);
895 * Return the "real" price of a "known" item, not including discounts.
897 * Wand and staffs get cost for each charge.
899 * Armor is worth an extra 100 gold per bonus point to armor class.
901 * Weapons are worth an extra 100 gold per bonus point (AC,TH,TD).
903 * Missiles are only worth 5 gold per bonus point, since they
904 * usually appear in groups of 20, and we want the player to get
905 * the same amount of cash for any "equivalent" item. Note that
906 * missiles never have any of the "pval" flags, and in fact, they
907 * only have a few of the available flags, primarily of the "slay"
908 * and "brand" and "ignore" variety.
910 * Weapons with negative hit+damage bonuses are worthless.
912 * Every wearable item with a "pval" bonus is worth extra (see below).
914 static s32b object_value_real(const object_type *o_ptr)
916 s32b value;
918 u32b f1, f2, f3;
920 object_kind *k_ptr = &k_info[o_ptr->k_idx];
923 /* Hack -- "worthless" items */
924 if (!k_ptr->cost) return (0L);
926 /* Base cost */
927 value = k_ptr->cost;
930 /* Extract some flags */
931 object_flags(o_ptr, &f1, &f2, &f3);
934 /* Artifact */
935 if (o_ptr->name1)
937 artifact_type *a_ptr = &a_info[o_ptr->name1];
939 /* Hack -- "worthless" artifacts */
940 if (!a_ptr->cost) return (0L);
942 /* Hack -- Use the artifact cost instead */
943 value = a_ptr->cost;
946 /* Ego-Item */
947 else if (o_ptr->name2)
949 ego_item_type *e_ptr = &e_info[o_ptr->name2];
951 /* Hack -- "worthless" ego-items */
952 if (!e_ptr->cost) return (0L);
954 /* Hack -- Reward the ego-item with a bonus */
955 value += e_ptr->cost;
959 /* Analyze pval bonus */
960 switch (o_ptr->tval)
962 case TV_SHOT:
963 case TV_ARROW:
964 case TV_BOLT:
965 case TV_BOW:
966 case TV_DIGGING:
967 case TV_HAFTED:
968 case TV_POLEARM:
969 case TV_SWORD:
970 case TV_BOOTS:
971 case TV_GLOVES:
972 case TV_HELM:
973 case TV_CROWN:
974 case TV_SHIELD:
975 case TV_CLOAK:
976 case TV_SOFT_ARMOR:
977 case TV_HARD_ARMOR:
978 case TV_DRAG_ARMOR:
979 case TV_LITE:
980 case TV_AMULET:
981 case TV_RING:
983 /* Hack -- Negative "pval" is always bad */
984 if (o_ptr->pval < 0) return (0L);
986 /* No pval */
987 if (!o_ptr->pval) break;
989 /* Give credit for stat bonuses */
990 if (f1 & (TR1_STR)) value += (o_ptr->pval * 200L);
991 if (f1 & (TR1_INT)) value += (o_ptr->pval * 200L);
992 if (f1 & (TR1_WIS)) value += (o_ptr->pval * 200L);
993 if (f1 & (TR1_DEX)) value += (o_ptr->pval * 200L);
994 if (f1 & (TR1_CON)) value += (o_ptr->pval * 200L);
995 if (f1 & (TR1_CHR)) value += (o_ptr->pval * 200L);
997 /* Give credit for stealth and searching */
998 if (f1 & (TR1_STEALTH)) value += (o_ptr->pval * 100L);
999 if (f1 & (TR1_SEARCH)) value += (o_ptr->pval * 100L);
1001 /* Give credit for infra-vision and tunneling */
1002 if (f1 & (TR1_INFRA)) value += (o_ptr->pval * 50L);
1003 if (f1 & (TR1_TUNNEL)) value += (o_ptr->pval * 50L);
1005 /* Give credit for extra attacks */
1006 if (f1 & (TR1_BLOWS)) value += (o_ptr->pval * 2000L);
1008 /* Give credit for speed bonus */
1009 if (f1 & (TR1_SPEED)) value += (o_ptr->pval * 30000L);
1011 break;
1016 /* Analyze the item */
1017 switch (o_ptr->tval)
1019 /* Wands/Staffs */
1020 case TV_WAND:
1021 case TV_STAFF:
1023 /* Pay extra for charges, depending on standard number of charges */
1024 value += ((value / 20) * (o_ptr->pval / o_ptr->number));
1026 /* Done */
1027 break;
1030 /* Rings/Amulets, Lites */
1031 case TV_RING:
1032 case TV_AMULET:
1034 /* Hack -- negative bonuses are bad */
1035 if (o_ptr->to_a < 0) return (0L);
1036 if (o_ptr->to_h < 0) return (0L);
1037 if (o_ptr->to_d < 0) return (0L);
1039 /* Give credit for bonuses */
1040 value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 100L);
1042 /* Done */
1043 break;
1046 /* Armor */
1047 case TV_BOOTS:
1048 case TV_GLOVES:
1049 case TV_CLOAK:
1050 case TV_CROWN:
1051 case TV_HELM:
1052 case TV_SHIELD:
1053 case TV_SOFT_ARMOR:
1054 case TV_HARD_ARMOR:
1055 case TV_DRAG_ARMOR:
1057 /* Give credit for hit bonus */
1058 value += ((o_ptr->to_h - k_ptr->to_h) * 100L);
1060 /* Give credit for damage bonus */
1061 value += ((o_ptr->to_d - k_ptr->to_d) * 100L);
1063 /* Give credit for armor bonus */
1064 value += (o_ptr->to_a * 100L);
1066 /* Done */
1067 break;
1070 /* Bows/Weapons */
1071 case TV_BOW:
1072 case TV_DIGGING:
1073 case TV_HAFTED:
1074 case TV_SWORD:
1075 case TV_POLEARM:
1077 /* Hack -- negative hit/damage bonuses */
1078 if (o_ptr->to_h + o_ptr->to_d < 0) return (0L);
1080 /* Factor in the bonuses */
1081 value += ((o_ptr->to_h + o_ptr->to_d + o_ptr->to_a) * 100L);
1083 /* Hack -- Factor in extra damage dice */
1084 if ((o_ptr->dd > k_ptr->dd) && (o_ptr->ds == k_ptr->ds))
1086 value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 100L;
1089 /* Done */
1090 break;
1093 /* Ammo */
1094 case TV_SHOT:
1095 case TV_ARROW:
1096 case TV_BOLT:
1098 /* Hack -- negative hit/damage bonuses */
1099 if (o_ptr->to_h + o_ptr->to_d < 0) return (0L);
1101 /* Factor in the bonuses */
1102 value += ((o_ptr->to_h + o_ptr->to_d) * 5L);
1104 /* Hack -- Factor in extra damage dice */
1105 if ((o_ptr->dd > k_ptr->dd) && (o_ptr->ds == k_ptr->ds))
1107 value += (o_ptr->dd - k_ptr->dd) * o_ptr->ds * 5L;
1110 /* Done */
1111 break;
1115 /* No negative value */
1116 if (value < 0) value = 0;
1118 /* Return the value */
1119 return (value);
1124 * Return the price of an item including plusses (and charges).
1126 * This function returns the "value" of the given item (qty one).
1128 * Never notice "unknown" bonuses or properties, including "curses",
1129 * since that would give the player information he did not have.
1131 * Note that discounted items stay discounted forever.
1133 s32b object_value(const object_type *o_ptr)
1135 s32b value;
1138 /* Unknown items -- acquire a base value */
1139 if (object_known_p(o_ptr))
1141 /* Broken items -- worthless */
1142 if (broken_p(o_ptr)) return (0L);
1144 /* Cursed items -- worthless */
1145 if (cursed_p(o_ptr)) return (0L);
1147 /* Real value (see above) */
1148 value = object_value_real(o_ptr);
1151 /* Known items -- acquire the actual value */
1152 else
1154 /* Hack -- Felt broken items */
1155 if ((o_ptr->ident & (IDENT_SENSE)) && broken_p(o_ptr)) return (0L);
1157 /* Hack -- Felt cursed items */
1158 if ((o_ptr->ident & (IDENT_SENSE)) && cursed_p(o_ptr)) return (0L);
1160 /* Base value (see above) */
1161 value = object_value_base(o_ptr);
1165 /* Return the final value */
1166 return (value);
1174 * Determine if an item can "absorb" a second item
1176 * See "object_absorb()" for the actual "absorption" code.
1178 * If permitted, we allow weapons/armor to stack, if "known".
1180 * Missiles will combine if both stacks have the same "known" status.
1181 * This is done to make unidentified stacks of missiles useful.
1183 * Food, potions, scrolls, and "easy know" items always stack.
1185 * Chests, and activatable items, except rods, never stack (for various
1186 * reasons).
1188 bool object_similar(const object_type *o_ptr, const object_type *j_ptr)
1190 int total = o_ptr->number + j_ptr->number;
1193 /* Require identical object types */
1194 if (o_ptr->k_idx != j_ptr->k_idx) return (0);
1197 /* Analyze the items */
1198 switch (o_ptr->tval)
1200 /* Chests */
1201 case TV_CHEST:
1203 /* Never okay */
1204 return (0);
1207 /* Food and Potions and Scrolls */
1208 case TV_FOOD:
1209 case TV_POTION:
1210 case TV_SCROLL:
1212 /* Assume okay */
1213 break;
1216 /* Staves and Wands */
1217 case TV_STAFF:
1218 case TV_WAND:
1220 /* Require either knowledge or known empty for both wands/staves */
1221 if ((!(o_ptr->ident & (IDENT_EMPTY)) &&
1222 !object_known_p(o_ptr)) ||
1223 (!(j_ptr->ident & (IDENT_EMPTY)) &&
1224 !object_known_p(j_ptr))) return(0);
1226 /* Assume okay */
1227 break;
1230 /* Rods */
1231 case TV_ROD:
1233 /* Assume okay */
1234 break;
1237 /* Weapons and Armor */
1238 case TV_BOW:
1239 case TV_DIGGING:
1240 case TV_HAFTED:
1241 case TV_POLEARM:
1242 case TV_SWORD:
1243 case TV_BOOTS:
1244 case TV_GLOVES:
1245 case TV_HELM:
1246 case TV_CROWN:
1247 case TV_SHIELD:
1248 case TV_CLOAK:
1249 case TV_SOFT_ARMOR:
1250 case TV_HARD_ARMOR:
1251 case TV_DRAG_ARMOR:
1253 /* Fall through */
1256 /* Rings, Amulets, Lites */
1257 case TV_RING:
1258 case TV_AMULET:
1259 case TV_LITE:
1261 /* Require both items to be known */
1262 if (!object_known_p(o_ptr) || !object_known_p(j_ptr)) return (0);
1264 /* Fall through */
1267 /* Missiles */
1268 case TV_BOLT:
1269 case TV_ARROW:
1270 case TV_SHOT:
1272 /* Require identical knowledge of both items */
1273 if (object_known_p(o_ptr) != object_known_p(j_ptr)) return (0);
1275 /* Require identical "bonuses" */
1276 if (o_ptr->to_h != j_ptr->to_h) return (FALSE);
1277 if (o_ptr->to_d != j_ptr->to_d) return (FALSE);
1278 if (o_ptr->to_a != j_ptr->to_a) return (FALSE);
1280 /* Require identical "pval" code */
1281 if (o_ptr->pval != j_ptr->pval) return (FALSE);
1283 /* Require identical "artifact" names */
1284 if (o_ptr->name1 != j_ptr->name1) return (FALSE);
1286 /* Require identical "ego-item" names */
1287 if (o_ptr->name2 != j_ptr->name2) return (FALSE);
1289 /* Hack -- Never stack "powerful" items */
1290 if (o_ptr->xtra1 || j_ptr->xtra1) return (FALSE);
1292 /* Hack - Never stack recharging items */
1293 if ((o_ptr->timeout || j_ptr->timeout) && o_ptr->tval != TV_LITE)
1294 return FALSE;
1296 /* Lites must have same amount of fuel */
1297 else if(o_ptr->timeout != j_ptr->timeout && o_ptr->tval == TV_LITE)
1298 return FALSE;
1300 /* Require identical "values" */
1301 if (o_ptr->ac != j_ptr->ac) return (FALSE);
1302 if (o_ptr->dd != j_ptr->dd) return (FALSE);
1303 if (o_ptr->ds != j_ptr->ds) return (FALSE);
1305 /* Probably okay */
1306 break;
1309 /* Various */
1310 default:
1312 /* Require knowledge */
1313 if (!object_known_p(o_ptr) || !object_known_p(j_ptr)) return (0);
1315 /* Probably okay */
1316 break;
1321 /* Hack -- Require identical "cursed" and "broken" status */
1322 if (((o_ptr->ident & (IDENT_CURSED)) != (j_ptr->ident & (IDENT_CURSED))) ||
1323 ((o_ptr->ident & (IDENT_BROKEN)) != (j_ptr->ident & (IDENT_BROKEN))))
1325 return (0);
1329 /* Hack -- Require compatible inscriptions */
1330 if (o_ptr->note != j_ptr->note)
1332 /* Never combine different inscriptions */
1333 if (o_ptr->note && j_ptr->note) return (0);
1337 /* Different pseudo-ID statuses preclude combination */
1338 if (o_ptr->pseudo != j_ptr->pseudo)
1339 return (0);
1342 /* Maximal "stacking" limit */
1343 if (total >= MAX_STACK_SIZE) return (0);
1346 /* They match, so they must be similar */
1347 return (TRUE);
1352 * Allow one item to "absorb" another, assuming they are similar.
1354 * The blending of the "note" field assumes that either (1) one has an
1355 * inscription and the other does not, or (2) neither has an inscription.
1356 * In both these cases, we can simply use the existing note, unless the
1357 * blending object has a note, in which case we use that note.
1359 * The blending of the "discount" field assumes that either (1) one is a
1360 * special inscription and one is nothing, or (2) one is a discount and
1361 * one is a smaller discount, or (3) one is a discount and one is nothing,
1362 * or (4) both are nothing. In all of these cases, we can simply use the
1363 * "maximum" of the two "discount" fields.
1365 * These assumptions are enforced by the "object_similar()" code.
1367 void object_absorb(object_type *o_ptr, const object_type *j_ptr)
1369 object_kind *k_ptr = &k_info[o_ptr->k_idx];
1371 int total = o_ptr->number + j_ptr->number;
1373 /* Add together the item counts */
1374 o_ptr->number = ((total < MAX_STACK_SIZE) ? total : (MAX_STACK_SIZE - 1));
1376 /* Hack -- Blend "known" status */
1377 if (object_known_p(j_ptr)) object_known(o_ptr);
1379 /* Hack -- Blend store status */
1380 if (j_ptr->ident & (IDENT_STORE)) o_ptr->ident |= (IDENT_STORE);
1382 /* Hack -- Blend "mental" status */
1383 if (j_ptr->ident & (IDENT_MENTAL)) o_ptr->ident |= (IDENT_MENTAL);
1385 /* Hack -- Blend "notes" */
1386 if (j_ptr->note != 0) o_ptr->note = j_ptr->note;
1388 /* Mega-Hack -- Blend "discounts" */
1389 o_ptr->pseudo = j_ptr->pseudo;
1392 * Hack -- if rods are stacking, re-calculate the
1393 * pvals (maximum timeouts) and current timeouts together
1395 if (o_ptr->tval == TV_ROD)
1397 o_ptr->pval = total * k_ptr->pval;
1398 o_ptr->timeout += j_ptr->timeout;
1401 /* Hack -- if wands or staves are stacking, combine the charges */
1402 if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF))
1404 o_ptr->pval += j_ptr->pval;
1411 * Find the index of the object_kind with the given tval and sval
1413 s16b lookup_kind(int tval, int sval)
1415 int k;
1417 /* Look for it */
1418 for (k = 1; k < z_info->k_max; k++)
1420 object_kind *k_ptr = &k_info[k];
1422 /* Found a match */
1423 if ((k_ptr->tval == tval) && (k_ptr->sval == sval)) return (k);
1426 /* Oops */
1427 msg_format("No object (%d,%d)", tval, sval);
1429 /* Oops */
1430 return (0);
1435 * Wipe an object clean.
1437 void object_wipe(object_type *o_ptr)
1439 /* Wipe the structure */
1440 (void)WIPE(o_ptr, object_type);
1445 * Prepare an object based on an existing object
1447 void object_copy(object_type *o_ptr, const object_type *j_ptr)
1449 /* Copy the structure */
1450 COPY(o_ptr, j_ptr, object_type);
1455 * Prepare an object based on an object kind.
1457 void object_prep(object_type *o_ptr, int k_idx)
1459 object_kind *k_ptr = &k_info[k_idx];
1461 /* Clear the record */
1462 (void)WIPE(o_ptr, object_type);
1464 /* Save the kind index */
1465 o_ptr->k_idx = k_idx;
1467 /* Efficiency -- tval/sval */
1468 o_ptr->tval = k_ptr->tval;
1469 o_ptr->sval = k_ptr->sval;
1471 /* Default "pval" */
1472 o_ptr->pval = k_ptr->pval;
1474 /* Default number */
1475 o_ptr->number = 1;
1477 /* Default weight */
1478 o_ptr->weight = k_ptr->weight;
1480 /* Default magic */
1481 o_ptr->to_h = k_ptr->to_h;
1482 o_ptr->to_d = k_ptr->to_d;
1483 o_ptr->to_a = k_ptr->to_a;
1485 /* Default power */
1486 o_ptr->ac = k_ptr->ac;
1487 o_ptr->dd = k_ptr->dd;
1488 o_ptr->ds = k_ptr->ds;
1490 /* Hack -- worthless items are always "broken" */
1491 if (k_ptr->cost <= 0) o_ptr->ident |= (IDENT_BROKEN);
1493 /* Hack -- cursed items are always "cursed" */
1494 if (k_ptr->flags3 & (TR3_LIGHT_CURSE)) o_ptr->ident |= (IDENT_CURSED);
1499 * Help determine an "enchantment bonus" for an object.
1501 * To avoid floating point but still provide a smooth distribution of bonuses,
1502 * we simply round the results of division in such a way as to "average" the
1503 * correct floating point value.
1505 * This function has been changed. It uses "Rand_normal()" to choose values
1506 * from a normal distribution, whose mean moves from zero towards the max as
1507 * the level increases, and whose standard deviation is equal to 1/4 of the
1508 * max, and whose values are forced to lie between zero and the max, inclusive.
1510 * Since the "level" rarely passes 100 before Morgoth is dead, it is very
1511 * rare to get the "full" enchantment on an object, even a deep levels.
1513 * It is always possible (albeit unlikely) to get the "full" enchantment.
1515 * A sample distribution of values from "m_bonus(10, N)" is shown below:
1517 * N 0 1 2 3 4 5 6 7 8 9 10
1518 * --- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1519 * 0 66.37 13.01 9.73 5.47 2.89 1.31 0.72 0.26 0.12 0.09 0.03
1520 * 8 46.85 24.66 12.13 8.13 4.20 2.30 1.05 0.36 0.19 0.08 0.05
1521 * 16 30.12 27.62 18.52 10.52 6.34 3.52 1.95 0.90 0.31 0.15 0.05
1522 * 24 22.44 15.62 30.14 12.92 8.55 5.30 2.39 1.63 0.62 0.28 0.11
1523 * 32 16.23 11.43 23.01 22.31 11.19 7.18 4.46 2.13 1.20 0.45 0.41
1524 * 40 10.76 8.91 12.80 29.51 16.00 9.69 5.90 3.43 1.47 0.88 0.65
1525 * 48 7.28 6.81 10.51 18.27 27.57 11.76 7.85 4.99 2.80 1.22 0.94
1526 * 56 4.41 4.73 8.52 11.96 24.94 19.78 11.06 7.18 3.68 1.96 1.78
1527 * 64 2.81 3.07 5.65 9.17 13.01 31.57 13.70 9.30 6.04 3.04 2.64
1528 * 72 1.87 1.99 3.68 7.15 10.56 20.24 25.78 12.17 7.52 4.42 4.62
1529 * 80 1.02 1.23 2.78 4.75 8.37 12.04 27.61 18.07 10.28 6.52 7.33
1530 * 88 0.70 0.57 1.56 3.12 6.34 10.06 15.76 30.46 12.58 8.47 10.38
1531 * 96 0.27 0.60 1.25 2.28 4.30 7.60 10.77 22.52 22.51 11.37 16.53
1532 * 104 0.22 0.42 0.77 1.36 2.62 5.33 8.93 13.05 29.54 15.23 22.53
1533 * 112 0.15 0.20 0.56 0.87 2.00 3.83 6.86 10.06 17.89 27.31 30.27
1534 * 120 0.03 0.11 0.31 0.46 1.31 2.48 4.60 7.78 11.67 25.53 45.72
1535 * 128 0.02 0.01 0.13 0.33 0.83 1.41 3.24 6.17 9.57 14.22 64.07
1537 static s16b m_bonus(int max, int level)
1539 int bonus, stand, extra, value;
1542 /* Paranoia -- enforce maximal "level" */
1543 if (level > MAX_DEPTH - 1) level = MAX_DEPTH - 1;
1546 /* The "bonus" moves towards the max */
1547 bonus = ((max * level) / MAX_DEPTH);
1549 /* Hack -- determine fraction of error */
1550 extra = ((max * level) % MAX_DEPTH);
1552 /* Hack -- simulate floating point computations */
1553 if (rand_int(MAX_DEPTH) < extra) bonus++;
1556 /* The "stand" is equal to one quarter of the max */
1557 stand = (max / 4);
1559 /* Hack -- determine fraction of error */
1560 extra = (max % 4);
1562 /* Hack -- simulate floating point computations */
1563 if (rand_int(4) < extra) stand++;
1566 /* Choose an "interesting" value */
1567 value = Rand_normal(bonus, stand);
1569 /* Enforce the minimum value */
1570 if (value < 0) return (0);
1572 /* Enforce the maximum value */
1573 if (value > max) return (max);
1575 /* Result */
1576 return (value);
1583 * Cheat -- describe a created object for the user
1585 static void object_mention(const object_type *o_ptr)
1587 char o_name[80];
1589 /* Describe */
1590 object_desc_spoil(o_name, sizeof(o_name), o_ptr, FALSE, 0);
1592 /* Artifact */
1593 if (artifact_p(o_ptr))
1595 /* Silly message */
1596 msg_format("Artifact (%s)", o_name);
1599 /* Ego-item */
1600 else if (ego_item_p(o_ptr))
1602 /* Silly message */
1603 msg_format("Ego-item (%s)", o_name);
1606 /* Normal item */
1607 else
1609 /* Silly message */
1610 msg_format("Object (%s)", o_name);
1616 * Attempt to change an object into an ego-item -MWK-
1617 * Better only called by apply_magic().
1618 * The return value says if we picked a cursed item (if allowed) and is
1619 * passed on to a_m_aux1/2().
1620 * If no legal ego item is found, this routine returns 0, resulting in
1621 * an unenchanted item.
1623 static int make_ego_item(object_type *o_ptr, bool only_good)
1625 int i, j, level;
1627 int e_idx;
1629 long value, total;
1631 ego_item_type *e_ptr;
1633 alloc_entry *table = alloc_ego_table;
1636 /* Fail if object already is ego or artifact */
1637 if (o_ptr->name1) return (FALSE);
1638 if (o_ptr->name2) return (FALSE);
1640 level = object_level;
1642 /* Boost level (like with object base types) */
1643 if (level > 0)
1645 /* Occasional "boost" */
1646 if (rand_int(GREAT_EGO) == 0)
1648 /* The bizarre calculation again */
1649 level = 1 + (level * MAX_DEPTH / randint(MAX_DEPTH));
1653 /* Reset total */
1654 total = 0L;
1656 /* Process probabilities */
1657 for (i = 0; i < alloc_ego_size; i++)
1659 /* Default */
1660 table[i].prob3 = 0;
1662 /* Objects are sorted by depth */
1663 if (table[i].level > level) continue;
1665 /* Get the index */
1666 e_idx = table[i].index;
1668 /* Get the actual kind */
1669 e_ptr = &e_info[e_idx];
1671 /* If we force good/great, don't create cursed */
1672 if (only_good && (e_ptr->flags3 & TR3_LIGHT_CURSE)) continue;
1674 /* Test if this is a legal ego-item type for this object */
1675 for (j = 0; j < EGO_TVALS_MAX; j++)
1677 /* Require identical base type */
1678 if (o_ptr->tval == e_ptr->tval[j])
1680 /* Require sval in bounds, lower */
1681 if (o_ptr->sval >= e_ptr->min_sval[j])
1683 /* Require sval in bounds, upper */
1684 if (o_ptr->sval <= e_ptr->max_sval[j])
1686 /* Accept */
1687 table[i].prob3 = table[i].prob2;
1693 /* Total */
1694 total += table[i].prob3;
1697 /* No legal ego-items -- create a normal unenchanted one */
1698 if (total == 0) return (0);
1701 /* Pick an ego-item */
1702 value = rand_int(total);
1704 /* Find the object */
1705 for (i = 0; i < alloc_ego_size; i++)
1707 /* Found the entry */
1708 if (value < table[i].prob3) break;
1710 /* Decrement */
1711 value = value - table[i].prob3;
1714 /* We have one */
1715 e_idx = (byte)table[i].index;
1716 o_ptr->name2 = e_idx;
1718 return ((e_info[e_idx].flags3 & TR3_LIGHT_CURSE) ? -2 : 2);
1723 * Mega-Hack -- Attempt to create one of the "Special Objects".
1725 * We are only called from "make_object()", and we assume that
1726 * "apply_magic()" is called immediately after we return.
1728 * Note -- see "make_artifact()" and "apply_magic()".
1730 * We *prefer* to create the special artifacts in order, but this is
1731 * normally outweighed by the "rarity" rolls for those artifacts. The
1732 * only major effect of this logic is that the Phial (with rarity one)
1733 * is always the first special artifact created.
1735 static bool make_artifact_special(object_type *o_ptr)
1737 int i;
1739 int k_idx;
1742 /* No artifacts, do nothing */
1743 if (adult_no_artifacts) return (FALSE);
1745 /* No artifacts in the town */
1746 if (!p_ptr->depth) return (FALSE);
1748 /* Check the special artifacts */
1749 for (i = 0; i < ART_MIN_NORMAL; ++i)
1751 artifact_type *a_ptr = &a_info[i];
1753 /* Skip "empty" artifacts */
1754 if (!a_ptr->name) continue;
1756 /* Cannot make an artifact twice */
1757 if (a_ptr->cur_num) continue;
1759 /* Enforce minimum "depth" (loosely) */
1760 if (a_ptr->level > p_ptr->depth)
1762 /* Get the "out-of-depth factor" */
1763 int d = (a_ptr->level - p_ptr->depth) * 2;
1765 /* Roll for out-of-depth creation */
1766 if (rand_int(d) != 0) continue;
1769 /* Artifact "rarity roll" */
1770 if (rand_int(a_ptr->rarity) != 0) continue;
1772 /* Find the base object */
1773 k_idx = lookup_kind(a_ptr->tval, a_ptr->sval);
1775 /* Enforce minimum "object" level (loosely) */
1776 if (k_info[k_idx].level > object_level)
1778 /* Get the "out-of-depth factor" */
1779 int d = (k_info[k_idx].level - object_level) * 5;
1781 /* Roll for out-of-depth creation */
1782 if (rand_int(d) != 0) continue;
1785 /* Assign the template */
1786 object_prep(o_ptr, k_idx);
1788 /* Mark the item as an artifact */
1789 o_ptr->name1 = i;
1791 /* Success */
1792 return (TRUE);
1795 /* Failure */
1796 return (FALSE);
1801 * Attempt to change an object into an artifact
1803 * This routine should only be called by "apply_magic()"
1805 * Note -- see "make_artifact_special()" and "apply_magic()"
1807 static bool make_artifact(object_type *o_ptr)
1809 int i;
1812 /* No artifacts, do nothing */
1813 if (adult_no_artifacts) return (FALSE);
1815 /* No artifacts in the town */
1816 if (!p_ptr->depth) return (FALSE);
1818 /* Paranoia -- no "plural" artifacts */
1819 if (o_ptr->number != 1) return (FALSE);
1821 /* Check the artifact list (skip the "specials") */
1822 for (i = ART_MIN_NORMAL; i < z_info->a_max; i++)
1824 artifact_type *a_ptr = &a_info[i];
1826 /* Skip "empty" items */
1827 if (!a_ptr->name) continue;
1829 /* Cannot make an artifact twice */
1830 if (a_ptr->cur_num) continue;
1832 /* Must have the correct fields */
1833 if (a_ptr->tval != o_ptr->tval) continue;
1834 if (a_ptr->sval != o_ptr->sval) continue;
1836 /* XXX XXX Enforce minimum "depth" (loosely) */
1837 if (a_ptr->level > p_ptr->depth)
1839 /* Get the "out-of-depth factor" */
1840 int d = (a_ptr->level - p_ptr->depth) * 2;
1842 /* Roll for out-of-depth creation */
1843 if (rand_int(d) != 0) continue;
1846 /* We must make the "rarity roll" */
1847 if (rand_int(a_ptr->rarity) != 0) continue;
1849 /* Mark the item as an artifact */
1850 o_ptr->name1 = i;
1852 /* Success */
1853 return (TRUE);
1856 /* Failure */
1857 return (FALSE);
1864 * Apply magic to an item known to be a "weapon"
1866 * Hack -- note special base damage dice boosting
1867 * Hack -- note special processing for weapon/digger
1868 * Hack -- note special rating boost for dragon scale mail
1870 static void a_m_aux_1(object_type *o_ptr, int level, int power)
1872 int tohit1 = randint(5) + m_bonus(5, level);
1873 int todam1 = randint(5) + m_bonus(5, level);
1875 int tohit2 = m_bonus(10, level);
1876 int todam2 = m_bonus(10, level);
1879 /* Good */
1880 if (power > 0)
1882 /* Enchant */
1883 o_ptr->to_h += tohit1;
1884 o_ptr->to_d += todam1;
1886 /* Very good */
1887 if (power > 1)
1889 /* Enchant again */
1890 o_ptr->to_h += tohit2;
1891 o_ptr->to_d += todam2;
1895 /* Cursed */
1896 else if (power < 0)
1898 /* Penalize */
1899 o_ptr->to_h -= tohit1;
1900 o_ptr->to_d -= todam1;
1902 /* Very cursed */
1903 if (power < -1)
1905 /* Penalize again */
1906 o_ptr->to_h -= tohit2;
1907 o_ptr->to_d -= todam2;
1910 /* Cursed (if "bad") */
1911 if (o_ptr->to_h + o_ptr->to_d < 0) o_ptr->ident |= (IDENT_CURSED);
1915 /* Analyze type */
1916 switch (o_ptr->tval)
1918 case TV_DIGGING:
1920 /* Very bad */
1921 if (power < -1)
1923 /* Hack -- Horrible digging bonus */
1924 o_ptr->pval = 0 - (5 + randint(5));
1927 /* Bad */
1928 else if (power < 0)
1930 /* Hack -- Reverse digging bonus */
1931 o_ptr->pval = 0 - (o_ptr->pval);
1934 break;
1938 case TV_HAFTED:
1939 case TV_POLEARM:
1940 case TV_SWORD:
1942 /* Very Good */
1943 if (power > 1)
1945 /* Hack -- Super-charge the damage dice */
1946 while ((o_ptr->dd * o_ptr->ds > 0) &&
1947 (rand_int(10L * o_ptr->dd * o_ptr->ds) == 0))
1949 o_ptr->dd++;
1952 /* Hack -- Lower the damage dice */
1953 if (o_ptr->dd > 9) o_ptr->dd = 9;
1956 break;
1960 case TV_BOLT:
1961 case TV_ARROW:
1962 case TV_SHOT:
1964 /* Very good */
1965 if (power > 1)
1967 /* Hack -- super-charge the damage dice */
1968 while ((o_ptr->dd * o_ptr->ds > 0) &&
1969 (rand_int(10L * o_ptr->dd * o_ptr->ds) == 0))
1971 o_ptr->dd++;
1974 /* Hack -- restrict the damage dice */
1975 if (o_ptr->dd > 9) o_ptr->dd = 9;
1978 break;
1985 * Apply magic to an item known to be "armor"
1987 * Hack -- note special processing for crown/helm
1988 * Hack -- note special processing for robe of permanence
1990 static void a_m_aux_2(object_type *o_ptr, int level, int power)
1992 int toac1 = randint(5) + m_bonus(5, level);
1994 int toac2 = m_bonus(10, level);
1997 /* Good */
1998 if (power > 0)
2000 /* Enchant */
2001 o_ptr->to_a += toac1;
2003 /* Very good */
2004 if (power > 1)
2006 /* Enchant again */
2007 o_ptr->to_a += toac2;
2011 /* Cursed */
2012 else if (power < 0)
2014 /* Penalize */
2015 o_ptr->to_a -= toac1;
2017 /* Very cursed */
2018 if (power < -1)
2020 /* Penalize again */
2021 o_ptr->to_a -= toac2;
2024 /* Cursed (if "bad") */
2025 if (o_ptr->to_a < 0) o_ptr->ident |= (IDENT_CURSED);
2029 /* Analyze type */
2030 switch (o_ptr->tval)
2032 case TV_DRAG_ARMOR:
2034 /* Rating boost */
2035 rating += 30;
2037 /* Mention the item */
2038 if (cheat_peek) object_mention(o_ptr);
2040 break;
2048 * Apply magic to an item known to be a "ring" or "amulet"
2050 * Hack -- note special rating boost for ring of speed
2051 * Hack -- note special rating boost for certain amulets
2052 * Hack -- note special "pval boost" code for ring of speed
2053 * Hack -- note that some items must be cursed (or blessed)
2055 static void a_m_aux_3(object_type *o_ptr, int level, int power)
2057 /* Apply magic (good or bad) according to type */
2058 switch (o_ptr->tval)
2060 case TV_RING:
2062 /* Analyze */
2063 switch (o_ptr->sval)
2065 /* Strength, Constitution, Dexterity, Intelligence */
2066 case SV_RING_STR:
2067 case SV_RING_CON:
2068 case SV_RING_DEX:
2069 case SV_RING_INT:
2071 /* Stat bonus */
2072 o_ptr->pval = 1 + m_bonus(5, level);
2074 /* Cursed */
2075 if (power < 0)
2077 /* Broken */
2078 o_ptr->ident |= (IDENT_BROKEN);
2080 /* Cursed */
2081 o_ptr->ident |= (IDENT_CURSED);
2083 /* Reverse pval */
2084 o_ptr->pval = 0 - (o_ptr->pval);
2087 break;
2090 /* Ring of Speed! */
2091 case SV_RING_SPEED:
2093 /* Base speed (1 to 10) */
2094 o_ptr->pval = randint(5) + m_bonus(5, level);
2096 /* Super-charge the ring */
2097 while (rand_int(100) < 50) o_ptr->pval++;
2099 /* Cursed Ring */
2100 if (power < 0)
2102 /* Broken */
2103 o_ptr->ident |= (IDENT_BROKEN);
2105 /* Cursed */
2106 o_ptr->ident |= (IDENT_CURSED);
2108 /* Reverse pval */
2109 o_ptr->pval = 0 - (o_ptr->pval);
2111 break;
2113 else
2115 /* Rating boost */
2116 rating += 25;
2119 /* Mention the item */
2120 if (cheat_peek) object_mention(o_ptr);
2122 break;
2125 /* Searching */
2126 case SV_RING_SEARCHING:
2128 /* Bonus to searching */
2129 o_ptr->pval = 1 + m_bonus(5, level);
2131 /* Cursed */
2132 if (power < 0)
2134 /* Broken */
2135 o_ptr->ident |= (IDENT_BROKEN);
2137 /* Cursed */
2138 o_ptr->ident |= (IDENT_CURSED);
2140 /* Reverse pval */
2141 o_ptr->pval = 0 - (o_ptr->pval);
2144 break;
2147 /* Flames, Acid, Ice, Lightning */
2148 case SV_RING_FLAMES:
2149 case SV_RING_ACID:
2150 case SV_RING_ICE:
2151 case SV_RING_LIGHTNING:
2153 /* Bonus to armor class */
2154 o_ptr->to_a = 5 + randint(5) + m_bonus(10, level);
2155 break;
2158 /* Weakness, Stupidity */
2159 case SV_RING_WEAKNESS:
2160 case SV_RING_STUPIDITY:
2162 /* Broken */
2163 o_ptr->ident |= (IDENT_BROKEN);
2165 /* Cursed */
2166 o_ptr->ident |= (IDENT_CURSED);
2168 /* Penalize */
2169 o_ptr->pval = 0 - (1 + m_bonus(5, level));
2171 break;
2174 /* WOE, Stupidity */
2175 case SV_RING_WOE:
2177 /* Broken */
2178 o_ptr->ident |= (IDENT_BROKEN);
2180 /* Cursed */
2181 o_ptr->ident |= (IDENT_CURSED);
2183 /* Penalize */
2184 o_ptr->to_a = 0 - (5 + m_bonus(10, level));
2185 o_ptr->pval = 0 - (1 + m_bonus(5, level));
2187 break;
2190 /* Ring of damage */
2191 case SV_RING_DAMAGE:
2193 /* Bonus to damage */
2194 o_ptr->to_d = 5 + randint(3) + m_bonus(7, level);
2196 /* Cursed */
2197 if (power < 0)
2199 /* Broken */
2200 o_ptr->ident |= (IDENT_BROKEN);
2202 /* Cursed */
2203 o_ptr->ident |= (IDENT_CURSED);
2205 /* Reverse bonus */
2206 o_ptr->to_d = 0 - (o_ptr->to_d);
2209 break;
2212 /* Ring of Accuracy */
2213 case SV_RING_ACCURACY:
2215 /* Bonus to hit */
2216 o_ptr->to_h = 5 + randint(3) + m_bonus(7, level);
2218 /* Cursed */
2219 if (power < 0)
2221 /* Broken */
2222 o_ptr->ident |= (IDENT_BROKEN);
2224 /* Cursed */
2225 o_ptr->ident |= (IDENT_CURSED);
2227 /* Reverse tohit */
2228 o_ptr->to_h = 0 - (o_ptr->to_h);
2231 break;
2234 /* Ring of Protection */
2235 case SV_RING_PROTECTION:
2237 /* Bonus to armor class */
2238 o_ptr->to_a = 5 + randint(5) + m_bonus(10, level);
2240 /* Cursed */
2241 if (power < 0)
2243 /* Broken */
2244 o_ptr->ident |= (IDENT_BROKEN);
2246 /* Cursed */
2247 o_ptr->ident |= (IDENT_CURSED);
2249 /* Reverse toac */
2250 o_ptr->to_a = 0 - (o_ptr->to_a);
2253 break;
2256 /* Ring of Slaying */
2257 case SV_RING_SLAYING:
2259 /* Bonus to damage and to hit */
2260 o_ptr->to_d = randint(5) + m_bonus(5, level);
2261 o_ptr->to_h = randint(5) + m_bonus(5, level);
2263 /* Cursed */
2264 if (power < 0)
2266 /* Broken */
2267 o_ptr->ident |= (IDENT_BROKEN);
2269 /* Cursed */
2270 o_ptr->ident |= (IDENT_CURSED);
2272 /* Reverse bonuses */
2273 o_ptr->to_h = 0 - (o_ptr->to_h);
2274 o_ptr->to_d = 0 - (o_ptr->to_d);
2277 break;
2281 break;
2284 case TV_AMULET:
2286 /* Analyze */
2287 switch (o_ptr->sval)
2289 /* Amulet of wisdom/charisma/infravision */
2290 case SV_AMULET_WISDOM:
2291 case SV_AMULET_CHARISMA:
2292 case SV_AMULET_INFRAVISION:
2294 o_ptr->pval = 1 + m_bonus(5, level);
2296 /* Cursed */
2297 if (power < 0)
2299 /* Broken */
2300 o_ptr->ident |= (IDENT_BROKEN);
2302 /* Cursed */
2303 o_ptr->ident |= (IDENT_CURSED);
2305 /* Reverse bonuses */
2306 o_ptr->pval = 0 - (o_ptr->pval);
2309 break;
2312 /* Amulet of searching */
2313 case SV_AMULET_SEARCHING:
2315 o_ptr->pval = randint(5) + m_bonus(5, level);
2317 /* Cursed */
2318 if (power < 0)
2320 /* Broken */
2321 o_ptr->ident |= (IDENT_BROKEN);
2323 /* Cursed */
2324 o_ptr->ident |= (IDENT_CURSED);
2326 /* Reverse bonuses */
2327 o_ptr->pval = 0 - (o_ptr->pval);
2330 break;
2333 /* Amulet of ESP -- never cursed */
2334 case SV_AMULET_ESP:
2336 o_ptr->pval = randint(5) + m_bonus(5, level);
2338 break;
2341 /* Amulet of the Magi -- never cursed */
2342 case SV_AMULET_THE_MAGI:
2344 o_ptr->pval = 1 + m_bonus(3, level);
2345 o_ptr->to_a = randint(5) + m_bonus(5, level);
2347 /* Boost the rating */
2348 rating += 25;
2350 /* Mention the item */
2351 if (cheat_peek) object_mention(o_ptr);
2353 break;
2356 /* Amulet of Devotion -- never cursed */
2357 case SV_AMULET_DEVOTION:
2359 o_ptr->pval = 1 + m_bonus(3, level);
2361 /* Boost the rating */
2362 rating += 25;
2364 /* Mention the item */
2365 if (cheat_peek) object_mention(o_ptr);
2367 break;
2370 /* Amulet of Weaponmastery -- never cursed */
2371 case SV_AMULET_WEAPONMASTERY:
2373 o_ptr->to_h = 1 + m_bonus(4, level);
2374 o_ptr->to_d = 1 + m_bonus(4, level);
2375 o_ptr->pval = 1 + m_bonus(2, level);
2377 /* Boost the rating */
2378 rating += 25;
2380 /* Mention the item */
2381 if (cheat_peek) object_mention(o_ptr);
2383 break;
2386 /* Amulet of Trickery -- never cursed */
2387 case SV_AMULET_TRICKERY:
2389 o_ptr->pval = randint(1) + m_bonus(3, level);
2391 /* Boost the rating */
2392 rating += 25;
2394 /* Mention the item */
2395 if (cheat_peek) object_mention(o_ptr);
2397 break;
2400 /* Amulet of Doom -- always cursed */
2401 case SV_AMULET_DOOM:
2403 /* Broken */
2404 o_ptr->ident |= (IDENT_BROKEN);
2406 /* Cursed */
2407 o_ptr->ident |= (IDENT_CURSED);
2409 /* Penalize */
2410 o_ptr->pval = 0 - (randint(5) + m_bonus(5, level));
2411 o_ptr->to_a = 0 - (randint(5) + m_bonus(5, level));
2413 break;
2417 break;
2424 * Apply magic to an item known to be "boring"
2426 * Hack -- note the special code for various items
2428 static void a_m_aux_4(object_type *o_ptr, int level, int power)
2430 object_kind *k_ptr = &k_info[o_ptr->k_idx];
2432 /* Unused parameters */
2433 (void)level;
2434 (void)power;
2436 /* Apply magic (good or bad) according to type */
2437 switch (o_ptr->tval)
2439 case TV_LITE:
2441 /* Hack -- Torches & lanterns -- random fuel at 1/2 max */
2442 if (o_ptr->sval == SV_LITE_TORCH)
2444 o_ptr->timeout = randint(FUEL_TORCH/2);
2447 else if (o_ptr->sval == SV_LITE_LANTERN)
2449 o_ptr->timeout = randint(FUEL_LAMP/2);
2452 break;
2455 case TV_WAND:
2456 case TV_STAFF:
2458 /* Charge staves and wands */
2459 o_ptr->pval = k_ptr->charge_base;
2461 if (k_ptr->charge_dd && k_ptr->charge_ds)
2462 o_ptr->pval += damroll(k_ptr->charge_dd, k_ptr->charge_ds);
2464 break;
2467 case TV_ROD:
2469 object_kind *k_ptr = &k_info[o_ptr->k_idx];
2471 /* Transfer the pval. */
2472 o_ptr->pval = k_ptr->pval;
2474 break;
2477 case TV_CHEST:
2479 /* Hack -- skip ruined chests */
2480 if (k_info[o_ptr->k_idx].level <= 0) break;
2482 /* Hack -- pick a "difficulty" */
2483 o_ptr->pval = randint(k_info[o_ptr->k_idx].level);
2485 /* Never exceed "difficulty" of 55 to 59 */
2486 if (o_ptr->pval > 55) o_ptr->pval = (s16b)(55 + rand_int(5));
2488 break;
2496 * Complete the "creation" of an object by applying "magic" to the item
2498 * This includes not only rolling for random bonuses, but also putting the
2499 * finishing touches on ego-items and artifacts, giving charges to wands and
2500 * staffs, giving fuel to lites, and placing traps on chests.
2502 * In particular, note that "Instant Artifacts", if "created" by an external
2503 * routine, must pass through this function to complete the actual creation.
2505 * The base "chance" of the item being "good" increases with the "level"
2506 * parameter, which is usually derived from the dungeon level, being equal
2507 * to the level plus 10, up to a maximum of 75. If "good" is true, then
2508 * the object is guaranteed to be "good". If an object is "good", then
2509 * the chance that the object will be "great" (ego-item or artifact), also
2510 * increases with the "level", being equal to half the level, plus 5, up to
2511 * a maximum of 20. If "great" is true, then the object is guaranteed to be
2512 * "great". At dungeon level 65 and below, 15/100 objects are "great".
2514 * If the object is not "good", there is a chance it will be "cursed", and
2515 * if it is "cursed", there is a chance it will be "broken". These chances
2516 * are related to the "good" / "great" chances above.
2518 * Otherwise "normal" rings and amulets will be "good" half the time and
2519 * "cursed" half the time, unless the ring/amulet is always good or cursed.
2521 * If "okay" is true, and the object is going to be "great", then there is
2522 * a chance that an artifact will be created. This is true even if both the
2523 * "good" and "great" arguments are false. Objects which are forced "great"
2524 * get three extra "attempts" to become an artifact.
2526 void apply_magic(object_type *o_ptr, int lev, bool okay, bool good, bool great)
2528 int i, rolls, f1, f2, power;
2531 /* Maximum "level" for various things */
2532 if (lev > MAX_DEPTH - 1) lev = MAX_DEPTH - 1;
2535 /* Base chance of being "good" */
2536 f1 = lev + 10;
2538 /* Maximal chance of being "good" */
2539 if (f1 > 75) f1 = 75;
2541 /* Base chance of being "great" */
2542 f2 = f1 / 2;
2544 /* Maximal chance of being "great" */
2545 if (f2 > 20) f2 = 20;
2548 /* Assume normal */
2549 power = 0;
2551 /* Roll for "good" */
2552 if (good || (rand_int(100) < f1))
2554 /* Assume "good" */
2555 power = 1;
2557 /* Roll for "great" */
2558 if (great || (rand_int(100) < f2)) power = 2;
2561 /* Roll for "cursed" */
2562 else if (rand_int(100) < f1)
2564 /* Assume "cursed" */
2565 power = -1;
2567 /* Roll for "broken" */
2568 if (rand_int(100) < f2) power = -2;
2571 /* Assume no rolls */
2572 rolls = 0;
2574 /* Get one roll if excellent */
2575 if (power >= 2) rolls = 1;
2577 /* Get four rolls if forced great */
2578 if (great) rolls = 4;
2580 /* Get no rolls if not allowed */
2581 if (!okay || o_ptr->name1) rolls = 0;
2583 /* Roll for artifacts if allowed */
2584 for (i = 0; i < rolls; i++)
2586 /* Roll for an artifact */
2587 if (make_artifact(o_ptr)) break;
2591 /* Hack -- analyze artifacts */
2592 if (o_ptr->name1)
2594 artifact_type *a_ptr = &a_info[o_ptr->name1];
2596 /* Hack -- Mark the artifact as "created" */
2597 a_ptr->cur_num = 1;
2599 /* Extract the other fields */
2600 o_ptr->pval = a_ptr->pval;
2601 o_ptr->ac = a_ptr->ac;
2602 o_ptr->dd = a_ptr->dd;
2603 o_ptr->ds = a_ptr->ds;
2604 o_ptr->to_a = a_ptr->to_a;
2605 o_ptr->to_h = a_ptr->to_h;
2606 o_ptr->to_d = a_ptr->to_d;
2607 o_ptr->weight = a_ptr->weight;
2609 /* Hack -- extract the "broken" flag */
2610 if (!a_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
2612 /* Hack -- extract the "cursed" flag */
2613 if (a_ptr->flags3 & (TR3_LIGHT_CURSE)) o_ptr->ident |= (IDENT_CURSED);
2615 /* Mega-Hack -- increase the rating */
2616 rating += 10;
2618 /* Mega-Hack -- increase the rating again */
2619 if (a_ptr->cost > 50000L) rating += 10;
2621 /* Set the good item flag */
2622 good_item_flag = TRUE;
2624 /* Cheat -- peek at the item */
2625 if (cheat_peek) object_mention(o_ptr);
2627 /* Done */
2628 return;
2632 /* Apply magic */
2633 switch (o_ptr->tval)
2635 case TV_DIGGING:
2636 case TV_HAFTED:
2637 case TV_POLEARM:
2638 case TV_SWORD:
2639 case TV_BOW:
2640 case TV_SHOT:
2641 case TV_ARROW:
2642 case TV_BOLT:
2644 if ((power > 1) || (power < -1))
2646 int ego_power;
2648 ego_power = make_ego_item(o_ptr, (bool)(good || great));
2650 if (ego_power) power = ego_power;
2653 if (power) a_m_aux_1(o_ptr, lev, power);
2655 break;
2658 case TV_DRAG_ARMOR:
2659 case TV_HARD_ARMOR:
2660 case TV_SOFT_ARMOR:
2661 case TV_SHIELD:
2662 case TV_HELM:
2663 case TV_CROWN:
2664 case TV_CLOAK:
2665 case TV_GLOVES:
2666 case TV_BOOTS:
2668 if ((power > 1) || (power < -1))
2670 int ego_power;
2672 ego_power = make_ego_item(o_ptr, (bool)(good || great));
2674 if (ego_power) power = ego_power;
2677 if (power) a_m_aux_2(o_ptr, lev, power);
2679 break;
2682 case TV_RING:
2683 case TV_AMULET:
2685 if (!power && (rand_int(100) < 50)) power = -1;
2686 a_m_aux_3(o_ptr, lev, power);
2687 break;
2690 case TV_LITE:
2692 if ((power > 1) || (power < -1))
2694 make_ego_item(o_ptr, (bool)(good || great));
2697 /* Fuel it */
2698 a_m_aux_4(o_ptr, lev, power);
2699 break;
2702 default:
2704 a_m_aux_4(o_ptr, lev, power);
2705 break;
2710 /* Hack -- analyze ego-items */
2711 if (o_ptr->name2)
2713 ego_item_type *e_ptr = &e_info[o_ptr->name2];
2715 /* Extra powers */
2716 if (e_ptr->xtra)
2718 o_ptr->xtra1 = e_ptr->xtra;
2719 switch (o_ptr->xtra1)
2721 case OBJECT_XTRA_TYPE_SUSTAIN:
2723 o_ptr->xtra2 = (byte)rand_int(OBJECT_XTRA_SIZE_SUSTAIN);
2724 break;
2727 case OBJECT_XTRA_TYPE_RESIST:
2729 o_ptr->xtra2 = (byte)rand_int(OBJECT_XTRA_SIZE_RESIST);
2730 break;
2733 case OBJECT_XTRA_TYPE_POWER:
2735 o_ptr->xtra2 = (byte)rand_int(OBJECT_XTRA_SIZE_POWER);
2736 break;
2741 /* Hack -- acquire "broken" flag */
2742 if (!e_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
2744 /* Hack -- acquire "cursed" flag */
2745 if (e_ptr->flags3 & (TR3_LIGHT_CURSE)) o_ptr->ident |= (IDENT_CURSED);
2747 /* Hack -- apply extra penalties if needed */
2748 if (cursed_p(o_ptr) || broken_p(o_ptr))
2750 /* Hack -- obtain bonuses */
2751 if (e_ptr->max_to_h > 0) o_ptr->to_h -= randint(e_ptr->max_to_h);
2752 if (e_ptr->max_to_d > 0) o_ptr->to_d -= randint(e_ptr->max_to_d);
2753 if (e_ptr->max_to_a > 0) o_ptr->to_a -= randint(e_ptr->max_to_a);
2755 /* Hack -- obtain pval */
2756 if (e_ptr->max_pval > 0) o_ptr->pval -= randint(e_ptr->max_pval);
2759 /* Hack -- apply extra bonuses if needed */
2760 else
2762 /* Hack -- obtain bonuses */
2763 if (e_ptr->max_to_h > 0) o_ptr->to_h += randint(e_ptr->max_to_h);
2764 if (e_ptr->max_to_d > 0) o_ptr->to_d += randint(e_ptr->max_to_d);
2765 if (e_ptr->max_to_a > 0) o_ptr->to_a += randint(e_ptr->max_to_a);
2767 /* Hack -- obtain pval */
2768 if (e_ptr->max_pval > 0) o_ptr->pval += randint(e_ptr->max_pval);
2771 /* Hack -- apply rating bonus */
2772 rating += e_ptr->rating;
2774 /* Cheat -- describe the item */
2775 if (cheat_peek) object_mention(o_ptr);
2777 /* Done */
2778 return;
2782 /* Examine real objects */
2783 if (o_ptr->k_idx)
2785 object_kind *k_ptr = &k_info[o_ptr->k_idx];
2787 /* Hack -- acquire "broken" flag */
2788 if (!k_ptr->cost) o_ptr->ident |= (IDENT_BROKEN);
2790 /* Hack -- acquire "cursed" flag */
2791 if (k_ptr->flags3 & (TR3_LIGHT_CURSE)) o_ptr->ident |= (IDENT_CURSED);
2798 * Hack -- determine if a template is "good".
2800 * Note that this test only applies to the object *kind*, so it is
2801 * possible to choose a kind which is "good", and then later cause
2802 * the actual object to be cursed. We do explicitly forbid objects
2803 * which are known to be boring or which start out somewhat damaged.
2805 static bool kind_is_good(int k_idx)
2807 object_kind *k_ptr = &k_info[k_idx];
2809 /* Analyze the item type */
2810 switch (k_ptr->tval)
2812 /* Armor -- Good unless damaged */
2813 case TV_HARD_ARMOR:
2814 case TV_SOFT_ARMOR:
2815 case TV_DRAG_ARMOR:
2816 case TV_SHIELD:
2817 case TV_CLOAK:
2818 case TV_BOOTS:
2819 case TV_GLOVES:
2820 case TV_HELM:
2821 case TV_CROWN:
2823 if (k_ptr->to_a < 0) return (FALSE);
2824 return (TRUE);
2827 /* Weapons -- Good unless damaged */
2828 case TV_BOW:
2829 case TV_SWORD:
2830 case TV_HAFTED:
2831 case TV_POLEARM:
2832 case TV_DIGGING:
2834 if (k_ptr->to_h < 0) return (FALSE);
2835 if (k_ptr->to_d < 0) return (FALSE);
2836 return (TRUE);
2839 /* Ammo -- Arrows/Bolts are good */
2840 case TV_BOLT:
2841 case TV_ARROW:
2843 return (TRUE);
2846 /* Books -- High level books are good */
2847 case TV_MAGIC_BOOK:
2848 case TV_PRAYER_BOOK:
2850 if (k_ptr->sval >= SV_BOOK_MIN_GOOD) return (TRUE);
2851 return (FALSE);
2854 /* Rings -- Rings of Speed are good */
2855 case TV_RING:
2857 if (k_ptr->sval == SV_RING_SPEED) return (TRUE);
2858 return (FALSE);
2861 /* Amulets -- Amulets of the Magi are good */
2862 case TV_AMULET:
2864 if (k_ptr->sval == SV_AMULET_THE_MAGI) return (TRUE);
2865 if (k_ptr->sval == SV_AMULET_DEVOTION) return (TRUE);
2866 if (k_ptr->sval == SV_AMULET_WEAPONMASTERY) return (TRUE);
2867 if (k_ptr->sval == SV_AMULET_TRICKERY) return (TRUE);
2868 return (FALSE);
2871 /* Potions -- Potions of life, healing, *healing* are good,
2872 * restore mana for spell casters are good,
2873 * as are stat potions, when the stat is not maximised,
2874 * as is augmentation (acts as a potion of 'restoration' if all
2875 * stats are maximised).
2877 * XXX If we make too many useful items 'good' we may want to
2878 * consider limiting the total number of good drops to uniques
2879 * and truely nasty monsters.
2881 case TV_POTION:
2883 if (k_ptr->sval == SV_POTION_HEALING) return (TRUE);
2884 if (k_ptr->sval == SV_POTION_STAR_HEALING) return (TRUE);
2885 if (k_ptr->sval == SV_POTION_LIFE) return (TRUE);
2886 if ((k_ptr->sval == SV_POTION_RESTORE_MANA) && (p_ptr->msp > 0)) return (TRUE);
2887 if ((k_ptr->sval >= SV_POTION_INC_STR) && (k_ptr->sval <= SV_POTION_INC_CHR))
2889 if (p_ptr->stat_cur[k_ptr->sval - SV_POTION_INC_STR] < 18+100) return (TRUE);
2891 if (k_ptr->sval == SV_POTION_AUGMENTATION) return (TRUE);
2892 return (FALSE);
2896 /* Assume not good */
2897 return (FALSE);
2903 * Attempt to make an object (normal or good/great)
2905 * This routine plays nasty games to generate the "special artifacts".
2907 * This routine uses "object_level" for the "generation level".
2909 * We assume that the given object has been "wiped".
2911 bool make_object(object_type *j_ptr, bool good, bool great)
2913 int prob, base;
2914 object_kind *k_ptr;
2917 /* Chance of "special object" */
2918 prob = (good ? 10 : 1000);
2920 /* Base level for the object */
2921 base = (good ? (object_level + 10) : object_level);
2924 /* Generate a special artifact, or a normal object */
2925 if ((rand_int(prob) != 0) || !make_artifact_special(j_ptr))
2927 int k_idx;
2929 /* Good objects */
2930 if (good)
2932 /* Activate restriction */
2933 get_obj_num_hook = kind_is_good;
2935 /* Prepare allocation table */
2936 get_obj_num_prep();
2939 /* Pick a random object */
2940 k_idx = get_obj_num(base);
2942 /* Good objects */
2943 if (good)
2945 /* Clear restriction */
2946 get_obj_num_hook = NULL;
2948 /* Prepare allocation table */
2949 get_obj_num_prep();
2952 /* Handle failure */
2953 if (!k_idx) return (FALSE);
2955 /* Prepare the object */
2956 object_prep(j_ptr, k_idx);
2959 /* Apply magic (allow artifacts) */
2960 apply_magic(j_ptr, object_level, TRUE, good, great);
2963 /* Generate multiple items */
2964 /* Imported from Steamband and Sangband */
2965 /* XXX Will probably not work so well for stacks of potions (yet) */
2966 k_ptr = &k_info[j_ptr->k_idx];
2968 if (k_ptr->gen_mult_prob >= 100 ||
2969 k_ptr->gen_mult_prob >= randint(100))
2971 j_ptr->number = damroll(k_ptr->gen_dice, k_ptr->gen_side);
2975 /* Notice "okay" out-of-depth objects */
2976 if (!cursed_p(j_ptr) && !broken_p(j_ptr) &&
2977 (k_info[j_ptr->k_idx].level > p_ptr->depth))
2979 /* Rating increase */
2980 rating += (k_info[j_ptr->k_idx].level - p_ptr->depth);
2982 /* Cheat -- peek at items */
2983 if (cheat_peek) object_mention(j_ptr);
2986 /* Success */
2987 return (TRUE);
2993 * XXX XXX XXX Do not use these hard-coded values.
2995 #define MAX_GOLD 18 /* Number of "gold" entries */
2998 * Make a treasure object
3000 * The location must be a legal, clean, floor grid.
3002 bool make_gold(object_type *j_ptr)
3004 int sval;
3005 int k_idx;
3006 s32b base;
3009 /* Hack -- Pick a Treasure variety */
3010 sval = ((randint(object_level + 2) + 2) / 2);
3012 /* Apply "extra" magic */
3013 if (rand_int(GREAT_OBJ) == 0)
3015 sval += randint(object_level + 1);
3018 /* Hack -- Creeping Coins only generate "themselves" */
3019 if (coin_type) sval = coin_type;
3021 /* Do not create "illegal" Treasure Types */
3022 if (sval > MAX_GOLD) sval = MAX_GOLD;
3024 k_idx = lookup_kind(TV_GOLD, sval);
3026 /* Prepare a gold object */
3027 object_prep(j_ptr, k_idx);
3029 /* Hack -- Base coin cost */
3030 base = k_info[k_idx].cost;
3032 /* Determine how much the treasure is "worth" */
3033 j_ptr->pval = (base + (8L * randint(base)) + randint(8));
3035 /* Success */
3036 return (TRUE);
3042 * Let the floor carry an object
3044 s16b floor_carry(int y, int x, object_type *j_ptr)
3046 int n = 0;
3048 s16b o_idx;
3050 s16b this_o_idx, next_o_idx = 0;
3053 /* Scan objects in that grid for combination */
3054 for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = next_o_idx)
3056 object_type *o_ptr;
3058 /* Get the object */
3059 o_ptr = &o_list[this_o_idx];
3061 /* Get the next object */
3062 next_o_idx = o_ptr->next_o_idx;
3064 /* Check for combination */
3065 if (object_similar(o_ptr, j_ptr))
3067 /* Combine the items */
3068 object_absorb(o_ptr, j_ptr);
3070 /* Result */
3071 return (this_o_idx);
3074 /* Count objects */
3075 n++;
3078 /* The stack is already too large */
3079 if (n > MAX_FLOOR_STACK) return (0);
3081 /* Option -- disallow stacking */
3082 if (adult_no_stacking && n) return (0);
3084 /* Make an object */
3085 o_idx = o_pop();
3087 /* Success */
3088 if (o_idx)
3090 object_type *o_ptr;
3092 /* Get the object */
3093 o_ptr = &o_list[o_idx];
3095 /* Structure Copy */
3096 object_copy(o_ptr, j_ptr);
3098 /* Location */
3099 o_ptr->iy = y;
3100 o_ptr->ix = x;
3102 /* Forget monster */
3103 o_ptr->held_m_idx = 0;
3105 /* Link the object to the pile */
3106 o_ptr->next_o_idx = cave_o_idx[y][x];
3108 /* Link the floor to the object */
3109 cave_o_idx[y][x] = o_idx;
3111 /* Notice */
3112 note_spot(y, x);
3114 /* Redraw */
3115 lite_spot(y, x);
3118 /* Result */
3119 return (o_idx);
3124 * Let an object fall to the ground at or near a location.
3126 * The initial location is assumed to be "in_bounds_fully()".
3128 * This function takes a parameter "chance". This is the percentage
3129 * chance that the item will "disappear" instead of drop. If the object
3130 * has been thrown, then this is the chance of disappearance on contact.
3132 * Hack -- this function uses "chance" to determine if it should produce
3133 * some form of "description" of the drop event (under the player).
3135 * We check several locations to see if we can find a location at which
3136 * the object can combine, stack, or be placed. Artifacts will try very
3137 * hard to be placed, including "teleporting" to a useful grid if needed.
3139 void drop_near(object_type *j_ptr, int chance, int y, int x)
3141 int i, k, n, d, s;
3143 int bs, bn;
3144 int by, bx;
3145 int dy, dx;
3146 int ty, tx;
3148 object_type *o_ptr;
3150 char o_name[80];
3152 bool flag = FALSE;
3154 bool plural = FALSE;
3157 /* Extract plural */
3158 if (j_ptr->number != 1) plural = TRUE;
3160 /* Describe object */
3161 object_desc(o_name, sizeof(o_name), j_ptr, FALSE, 0);
3164 /* Handle normal "breakage" */
3165 if (!artifact_p(j_ptr) && (rand_int(100) < chance))
3167 /* Message */
3168 msg_format("The %s disappear%s.",
3169 o_name, (plural ? "" : "s"));
3171 /* Debug */
3172 if (p_ptr->wizard) msg_print("Breakage (breakage).");
3174 /* Failure */
3175 return;
3179 /* Score */
3180 bs = -1;
3182 /* Picker */
3183 bn = 0;
3185 /* Default */
3186 by = y;
3187 bx = x;
3189 /* Scan local grids */
3190 for (dy = -3; dy <= 3; dy++)
3192 /* Scan local grids */
3193 for (dx = -3; dx <= 3; dx++)
3195 bool comb = FALSE;
3197 /* Calculate actual distance */
3198 d = (dy * dy) + (dx * dx);
3200 /* Ignore distant grids */
3201 if (d > 10) continue;
3203 /* Location */
3204 ty = y + dy;
3205 tx = x + dx;
3207 /* Skip illegal grids */
3208 if (!in_bounds_fully(ty, tx)) continue;
3210 /* Require line of sight */
3211 if (!los(y, x, ty, tx)) continue;
3213 /* Require floor space */
3214 if (cave_feat[ty][tx] != FEAT_FLOOR) continue;
3216 /* No objects */
3217 k = 0;
3218 n = 0;
3220 /* Scan objects in that grid */
3221 for (o_ptr = get_first_object(ty, tx); o_ptr; o_ptr = get_next_object(o_ptr))
3223 /* Check for possible combination */
3224 if (object_similar(o_ptr, j_ptr)) comb = TRUE;
3226 /* Count objects */
3227 if (!squelch_hide_item(o_ptr))
3228 k++;
3229 else
3230 n++;
3233 /* Add new object */
3234 if (!comb) k++;
3236 /* Option -- disallow stacking */
3237 if (adult_no_stacking && (k > 1)) continue;
3239 /* Paranoia? */
3240 if ((k + n) > MAX_FLOOR_STACK) continue;
3242 /* Calculate score */
3243 s = 1000 - (d + k * 5);
3245 /* Skip bad values */
3246 if (s < bs) continue;
3248 /* New best value */
3249 if (s > bs) bn = 0;
3251 /* Apply the randomizer to equivalent values */
3252 if ((++bn >= 2) && (rand_int(bn) != 0)) continue;
3254 /* Keep score */
3255 bs = s;
3257 /* Track it */
3258 by = ty;
3259 bx = tx;
3261 /* Okay */
3262 flag = TRUE;
3267 /* Handle lack of space */
3268 if (!flag && !artifact_p(j_ptr))
3270 /* Message */
3271 msg_format("The %s disappear%s.",
3272 o_name, (plural ? "" : "s"));
3274 /* Debug */
3275 if (p_ptr->wizard) msg_print("Breakage (no floor space).");
3277 /* Failure */
3278 return;
3282 /* Find a grid */
3283 for (i = 0; !flag; i++)
3285 /* Bounce around */
3286 if (i < 1000)
3288 ty = rand_spread(by, 1);
3289 tx = rand_spread(bx, 1);
3292 /* Random locations */
3293 else
3295 ty = rand_int(DUNGEON_HGT);
3296 tx = rand_int(DUNGEON_WID);
3299 /* Require floor space */
3300 if (cave_feat[ty][tx] != FEAT_FLOOR) continue;
3302 /* Bounce to that location */
3303 by = ty;
3304 bx = tx;
3306 /* Require floor space */
3307 if (!cave_clean_bold(by, bx)) continue;
3309 /* Okay */
3310 flag = TRUE;
3314 /* Give it to the floor */
3315 if (!floor_carry(by, bx, j_ptr))
3317 /* Message */
3318 msg_format("The %s disappear%s.",
3319 o_name, (plural ? "" : "s"));
3321 /* Debug */
3322 if (p_ptr->wizard) msg_print("Breakage (too many objects).");
3324 /* Hack -- Preserve artifacts */
3325 a_info[j_ptr->name1].cur_num = 0;
3327 /* Failure */
3328 return;
3332 /* Sound */
3333 sound(MSG_DROP);
3335 /* Mega-Hack -- no message if "dropped" by player */
3336 /* Message when an object falls under the player */
3337 if (chance && (cave_m_idx[by][bx] < 0))
3339 msg_print("You feel something roll beneath your feet.");
3345 * Scatter some "great" objects near the player
3347 void acquirement(int y1, int x1, int num, bool great)
3349 object_type *i_ptr;
3350 object_type object_type_body;
3352 /* Acquirement */
3353 while (num--)
3355 /* Get local object */
3356 i_ptr = &object_type_body;
3358 /* Wipe the object */
3359 object_wipe(i_ptr);
3361 /* Make a good (or great) object (if possible) */
3362 if (!make_object(i_ptr, TRUE, great)) continue;
3364 /* Drop the object */
3365 drop_near(i_ptr, -1, y1, x1);
3371 * Attempt to place an object (normal or good/great) at the given location.
3373 void place_object(int y, int x, bool good, bool great)
3375 object_type *i_ptr;
3376 object_type object_type_body;
3378 /* Paranoia */
3379 if (!in_bounds(y, x)) return;
3381 /* Hack -- clean floor space */
3382 if (!cave_clean_bold(y, x)) return;
3384 /* Get local object */
3385 i_ptr = &object_type_body;
3387 /* Wipe the object */
3388 object_wipe(i_ptr);
3390 /* Make an object (if possible) */
3391 if (make_object(i_ptr, good, great))
3393 /* Give it to the floor */
3394 if (!floor_carry(y, x, i_ptr))
3396 /* Hack -- Preserve artifacts */
3397 a_info[i_ptr->name1].cur_num = 0;
3404 * Places a treasure (Gold or Gems) at given location
3406 void place_gold(int y, int x)
3408 object_type *i_ptr;
3409 object_type object_type_body;
3411 /* Paranoia */
3412 if (!in_bounds(y, x)) return;
3414 /* Require clean floor space */
3415 if (!cave_clean_bold(y, x)) return;
3417 /* Get local object */
3418 i_ptr = &object_type_body;
3420 /* Wipe the object */
3421 object_wipe(i_ptr);
3423 /* Make some gold */
3424 if (make_gold(i_ptr))
3426 /* Give it to the floor */
3427 (void)floor_carry(y, x, i_ptr);
3434 * Hack -- instantiate a trap
3436 * XXX XXX XXX This routine should be redone to reflect trap "level".
3437 * That is, it does not make sense to have spiked pits at 50 feet.
3438 * Actually, it is not this routine, but the "trap instantiation"
3439 * code, which should also check for "trap doors" on quest levels.
3441 void pick_trap(int y, int x)
3443 int feat;
3445 /* Paranoia */
3446 if (cave_feat[y][x] != FEAT_INVIS) return;
3448 /* Pick a trap */
3449 while (1)
3451 /* Hack -- pick a trap */
3452 feat = FEAT_TRAP_HEAD + rand_int(16);
3454 /* Hack -- no trap doors on quest levels */
3455 if ((feat == FEAT_TRAP_HEAD + 0x00) && is_quest(p_ptr->depth)) continue;
3457 /* Hack -- no trap doors on the deepest level */
3458 if ((feat == FEAT_TRAP_HEAD + 0x00) && (p_ptr->depth >= MAX_DEPTH-1)) continue;
3460 /* Done */
3461 break;
3464 /* Activate the trap */
3465 cave_set_feat(y, x, feat);
3471 * Places a random trap at the given location.
3473 * The location must be a legal, naked, floor grid.
3475 * Note that all traps start out as "invisible" and "untyped", and then
3476 * when they are "discovered" (by detecting them or setting them off),
3477 * the trap is "instantiated" as a visible, "typed", trap.
3479 void place_trap(int y, int x)
3481 /* Paranoia */
3482 if (!in_bounds(y, x)) return;
3484 /* Require empty, clean, floor grid */
3485 if (!cave_naked_bold(y, x)) return;
3487 /* Place an invisible trap */
3488 cave_set_feat(y, x, FEAT_INVIS);
3493 * Place a secret door at the given location
3495 void place_secret_door(int y, int x)
3497 /* Create secret door */
3498 cave_set_feat(y, x, FEAT_SECRET);
3503 * Place a random type of closed door at the given location.
3505 void place_closed_door(int y, int x)
3507 int tmp;
3509 /* Choose an object */
3510 tmp = rand_int(400);
3512 /* Closed doors (300/400) */
3513 if (tmp < 300)
3515 /* Create closed door */
3516 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x00);
3519 /* Locked doors (99/400) */
3520 else if (tmp < 399)
3522 /* Create locked door */
3523 cave_set_feat(y, x, FEAT_DOOR_HEAD + randint(7));
3526 /* Stuck doors (1/400) */
3527 else
3529 /* Create jammed door */
3530 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x08 + rand_int(8));
3536 * Place a random type of door at the given location.
3538 void place_random_door(int y, int x)
3540 int tmp;
3542 /* Choose an object */
3543 tmp = rand_int(1000);
3545 /* Open doors (300/1000) */
3546 if (tmp < 300)
3548 /* Create open door */
3549 cave_set_feat(y, x, FEAT_OPEN);
3552 /* Broken doors (100/1000) */
3553 else if (tmp < 400)
3555 /* Create broken door */
3556 cave_set_feat(y, x, FEAT_BROKEN);
3559 /* Secret doors (200/1000) */
3560 else if (tmp < 600)
3562 /* Create secret door */
3563 cave_set_feat(y, x, FEAT_SECRET);
3566 /* Closed, locked, or stuck doors (400/1000) */
3567 else
3569 /* Create closed door */
3570 place_closed_door(y, x);
3576 * Describe the charges on an item in the inventory.
3578 void inven_item_charges(int item)
3580 object_type *o_ptr = &inventory[item];
3582 /* Require staff/wand */
3583 if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
3585 /* Require known item */
3586 if (!object_known_p(o_ptr)) return;
3588 /* Print a message */
3589 msg_format("You have %d charge%s remaining.", o_ptr->pval,
3590 (o_ptr->pval != 1) ? "s" : "");
3595 * Describe an item in the inventory.
3597 void inven_item_describe(int item)
3599 object_type *o_ptr = &inventory[item];
3601 char o_name[80];
3603 if (artifact_p(o_ptr) && object_known_p(o_ptr))
3605 /* Get a description */
3606 object_desc(o_name, sizeof(o_name), o_ptr, FALSE, 3);
3608 /* Print a message */
3609 msg_format("You no longer have the %s (%c).", o_name, index_to_label(item));
3611 else
3613 /* Get a description */
3614 object_desc(o_name, sizeof(o_name), o_ptr, TRUE, 3);
3616 /* Print a message */
3617 msg_format("You have %s (%c).", o_name, index_to_label(item));
3623 * Increase the "number" of an item in the inventory
3625 void inven_item_increase(int item, int num)
3627 object_type *o_ptr = &inventory[item];
3629 /* Apply */
3630 num += o_ptr->number;
3632 /* Bounds check */
3633 if (num > 255) num = 255;
3634 else if (num < 0) num = 0;
3636 /* Un-apply */
3637 num -= o_ptr->number;
3639 /* Change the number and weight */
3640 if (num)
3642 /* Add the number */
3643 o_ptr->number += num;
3645 /* Add the weight */
3646 p_ptr->total_weight += (num * o_ptr->weight);
3648 /* Recalculate bonuses */
3649 p_ptr->update |= (PU_BONUS);
3651 /* Recalculate mana XXX */
3652 p_ptr->update |= (PU_MANA);
3654 /* Combine the pack */
3655 p_ptr->notice |= (PN_COMBINE);
3657 /* Window stuff */
3658 p_ptr->window |= (PW_INVEN | PW_EQUIP);
3664 * Erase an inventory slot if it has no more items
3666 void inven_item_optimize(int item)
3668 object_type *o_ptr = &inventory[item];
3670 /* Only optimize real items */
3671 if (!o_ptr->k_idx) return;
3673 /* Only optimize empty items */
3674 if (o_ptr->number) return;
3676 /* The item is in the pack */
3677 if (item < INVEN_WIELD)
3679 int i;
3681 /* One less item */
3682 p_ptr->inven_cnt--;
3684 /* Slide everything down */
3685 for (i = item; i < INVEN_PACK; i++)
3687 /* Hack -- slide object */
3688 COPY(&inventory[i], &inventory[i+1], object_type);
3691 /* Hack -- wipe hole */
3692 (void)WIPE(&inventory[i], object_type);
3694 /* Window stuff */
3695 p_ptr->window |= (PW_INVEN);
3698 /* The item is being wielded */
3699 else
3701 /* One less item */
3702 p_ptr->equip_cnt--;
3704 /* Erase the empty slot */
3705 object_wipe(&inventory[item]);
3707 /* Recalculate bonuses */
3708 p_ptr->update |= (PU_BONUS);
3710 /* Recalculate torch */
3711 p_ptr->update |= (PU_TORCH);
3713 /* Recalculate mana XXX */
3714 p_ptr->update |= (PU_MANA);
3716 /* Window stuff */
3717 p_ptr->window |= (PW_EQUIP | PW_PLAYER_0 | PW_PLAYER_1);
3719 p_ptr->redraw |= (PR_EQUIPPY);
3725 * Describe the charges on an item on the floor.
3727 void floor_item_charges(int item)
3729 object_type *o_ptr = &o_list[item];
3731 /* Require staff/wand */
3732 if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
3734 /* Require known item */
3735 if (!object_known_p(o_ptr)) return;
3737 /* Print a message */
3738 msg_format("There are %d charge%s remaining.", o_ptr->pval,
3739 (o_ptr->pval != 1) ? "s" : "");
3745 * Describe an item in the inventory.
3747 void floor_item_describe(int item)
3749 object_type *o_ptr = &o_list[item];
3751 char o_name[80];
3753 /* Get a description */
3754 object_desc(o_name, sizeof(o_name), o_ptr, TRUE, 3);
3756 /* Print a message */
3757 msg_format("You see %s.", o_name);
3762 * Increase the "number" of an item on the floor
3764 void floor_item_increase(int item, int num)
3766 object_type *o_ptr = &o_list[item];
3768 /* Apply */
3769 num += o_ptr->number;
3771 /* Bounds check */
3772 if (num > 255) num = 255;
3773 else if (num < 0) num = 0;
3775 /* Un-apply */
3776 num -= o_ptr->number;
3778 /* Change the number */
3779 o_ptr->number += num;
3784 * Optimize an item on the floor (destroy "empty" items)
3786 void floor_item_optimize(int item)
3788 object_type *o_ptr = &o_list[item];
3790 /* Paranoia -- be sure it exists */
3791 if (!o_ptr->k_idx) return;
3793 /* Only optimize empty items */
3794 if (o_ptr->number) return;
3796 /* Delete the object */
3797 delete_object_idx(item);
3802 * Check if we have space for an item in the pack without overflow
3804 bool inven_carry_okay(const object_type *o_ptr)
3806 /* Empty slot? */
3807 if (p_ptr->inven_cnt < INVEN_PACK) return TRUE;
3809 /* Check if it can stack */
3810 if (inven_stack_okay(o_ptr)) return TRUE;
3812 /* Nope */
3813 return FALSE;
3817 * Check to see if an item is stackable in the inventory
3819 bool inven_stack_okay(const object_type *o_ptr)
3821 int j;
3823 /* Similar slot? */
3824 for (j = 0; j < INVEN_PACK; j++)
3826 object_type *j_ptr = &inventory[j];
3828 /* Skip non-objects */
3829 if (!j_ptr->k_idx) continue;
3831 /* Check if the two items can be combined */
3832 if (object_similar(j_ptr, o_ptr)) return (TRUE);
3835 /* Nope */
3836 return (FALSE);
3841 * Add an item to the players inventory, and return the slot used.
3843 * If the new item can combine with an existing item in the inventory,
3844 * it will do so, using "object_similar()" and "object_absorb()", else,
3845 * the item will be placed into the "proper" location in the inventory.
3847 * This function can be used to "over-fill" the player's pack, but only
3848 * once, and such an action must trigger the "overflow" code immediately.
3849 * Note that when the pack is being "over-filled", the new item must be
3850 * placed into the "overflow" slot, and the "overflow" must take place
3851 * before the pack is reordered, but (optionally) after the pack is
3852 * combined. This may be tricky. See "dungeon.c" for info.
3854 * Note that this code must remove any location/stack information
3855 * from the object once it is placed into the inventory.
3857 s16b inven_carry(object_type *o_ptr)
3859 int i, j, k;
3860 int n = -1;
3862 object_type *j_ptr;
3864 /* Apply an autoinscription */
3865 apply_autoinscription(o_ptr);
3867 /* Check for combining */
3868 for (j = 0; j < INVEN_PACK; j++)
3870 j_ptr = &inventory[j];
3872 /* Skip non-objects */
3873 if (!j_ptr->k_idx) continue;
3875 /* Hack -- track last item */
3876 n = j;
3878 /* Check if the two items can be combined */
3879 if (object_similar(j_ptr, o_ptr))
3881 /* Combine the items */
3882 object_absorb(j_ptr, o_ptr);
3884 /* Increase the weight */
3885 p_ptr->total_weight += (o_ptr->number * o_ptr->weight);
3887 /* Recalculate bonuses */
3888 p_ptr->update |= (PU_BONUS);
3890 /* Window stuff */
3891 p_ptr->window |= (PW_INVEN);
3893 /* Success */
3894 return (j);
3899 /* Paranoia */
3900 if (p_ptr->inven_cnt > INVEN_PACK) return (-1);
3903 /* Find an empty slot */
3904 for (j = 0; j <= INVEN_PACK; j++)
3906 j_ptr = &inventory[j];
3908 /* Use it if found */
3909 if (!j_ptr->k_idx) break;
3912 /* Use that slot */
3913 i = j;
3916 /* Reorder the pack */
3917 if (i < INVEN_PACK)
3919 s32b o_value, j_value;
3921 /* Get the "value" of the item */
3922 o_value = object_value(o_ptr);
3924 /* Scan every occupied slot */
3925 for (j = 0; j < INVEN_PACK; j++)
3927 j_ptr = &inventory[j];
3929 /* Use empty slots */
3930 if (!j_ptr->k_idx) break;
3932 /* Hack -- readable books always come first */
3933 if ((o_ptr->tval == cp_ptr->spell_book) &&
3934 (j_ptr->tval != cp_ptr->spell_book)) break;
3935 if ((j_ptr->tval == cp_ptr->spell_book) &&
3936 (o_ptr->tval != cp_ptr->spell_book)) continue;
3938 /* Objects sort by decreasing type */
3939 if (o_ptr->tval > j_ptr->tval) break;
3940 if (o_ptr->tval < j_ptr->tval) continue;
3942 /* Non-aware (flavored) items always come last */
3943 if (!object_aware_p(o_ptr)) continue;
3944 if (!object_aware_p(j_ptr)) break;
3946 /* Objects sort by increasing sval */
3947 if (o_ptr->sval < j_ptr->sval) break;
3948 if (o_ptr->sval > j_ptr->sval) continue;
3950 /* Unidentified objects always come last */
3951 if (!object_known_p(o_ptr)) continue;
3952 if (!object_known_p(j_ptr)) break;
3954 /* Lites sort by decreasing fuel */
3955 if (o_ptr->tval == TV_LITE)
3957 if (o_ptr->pval > j_ptr->pval) break;
3958 if (o_ptr->pval < j_ptr->pval) continue;
3961 /* Determine the "value" of the pack item */
3962 j_value = object_value(j_ptr);
3964 /* Objects sort by decreasing value */
3965 if (o_value > j_value) break;
3966 if (o_value < j_value) continue;
3969 /* Use that slot */
3970 i = j;
3972 /* Slide objects */
3973 for (k = n; k >= i; k--)
3975 /* Hack -- Slide the item */
3976 object_copy(&inventory[k+1], &inventory[k]);
3979 /* Wipe the empty slot */
3980 object_wipe(&inventory[i]);
3984 /* Copy the item */
3985 object_copy(&inventory[i], o_ptr);
3987 /* Get the new object */
3988 j_ptr = &inventory[i];
3990 /* Forget stack */
3991 j_ptr->next_o_idx = 0;
3993 /* Forget monster */
3994 j_ptr->held_m_idx = 0;
3996 /* Forget location */
3997 j_ptr->iy = j_ptr->ix = 0;
3999 /* No longer marked */
4000 j_ptr->marked = FALSE;
4002 /* Increase the weight */
4003 p_ptr->total_weight += (j_ptr->number * j_ptr->weight);
4005 /* Count the items */
4006 p_ptr->inven_cnt++;
4008 /* Recalculate bonuses */
4009 p_ptr->update |= (PU_BONUS);
4011 /* Combine and Reorder pack */
4012 p_ptr->notice |= (PN_COMBINE | PN_REORDER);
4014 /* Window stuff */
4015 p_ptr->window |= (PW_INVEN);
4017 /* Return the slot */
4018 return (i);
4023 * Take off (some of) a non-cursed equipment item
4025 * Note that only one item at a time can be wielded per slot.
4027 * Note that taking off an item when "full" may cause that item
4028 * to fall to the ground.
4030 * Return the inventory slot into which the item is placed.
4032 s16b inven_takeoff(int item, int amt)
4034 int slot;
4036 object_type *o_ptr;
4038 object_type *i_ptr;
4039 object_type object_type_body;
4041 cptr act;
4043 char o_name[80];
4046 /* Get the item to take off */
4047 o_ptr = &inventory[item];
4049 /* Paranoia */
4050 if (amt <= 0) return (-1);
4052 /* Verify */
4053 if (amt > o_ptr->number) amt = o_ptr->number;
4055 /* Get local object */
4056 i_ptr = &object_type_body;
4058 /* Obtain a local object */
4059 object_copy(i_ptr, o_ptr);
4061 /* Modify quantity */
4062 i_ptr->number = amt;
4064 /* Describe the object */
4065 object_desc(o_name, sizeof(o_name), i_ptr, TRUE, 3);
4067 /* Took off weapon */
4068 if (item == INVEN_WIELD)
4070 act = "You were wielding";
4073 /* Took off bow */
4074 else if (item == INVEN_BOW)
4076 act = "You were holding";
4079 /* Took off light */
4080 else if (item == INVEN_LITE)
4082 act = "You were holding";
4085 /* Took off something */
4086 else
4088 act = "You were wearing";
4091 /* Modify, Optimize */
4092 inven_item_increase(item, -amt);
4093 inven_item_optimize(item);
4095 /* Carry the object */
4096 slot = inven_carry(i_ptr);
4098 /* Message */
4099 sound(MSG_WIELD);
4100 msg_format("%s %s (%c).", act, o_name, index_to_label(slot));
4102 /* Return slot */
4103 return (slot);
4108 * Drop (some of) a non-cursed inventory/equipment item
4110 * The object will be dropped "near" the current location
4112 void inven_drop(int item, int amt)
4114 int py = p_ptr->py;
4115 int px = p_ptr->px;
4117 object_type *o_ptr;
4119 object_type *i_ptr;
4120 object_type object_type_body;
4122 char o_name[80];
4125 /* Get the original object */
4126 o_ptr = &inventory[item];
4128 /* Error check */
4129 if (amt <= 0) return;
4131 /* Not too many */
4132 if (amt > o_ptr->number) amt = o_ptr->number;
4135 /* Take off equipment */
4136 if (item >= INVEN_WIELD)
4138 /* Take off first */
4139 item = inven_takeoff(item, amt);
4141 /* Get the original object */
4142 o_ptr = &inventory[item];
4146 /* Get local object */
4147 i_ptr = &object_type_body;
4149 /* Obtain local object */
4150 object_copy(i_ptr, o_ptr);
4152 /* Distribute charges of wands, staves, or rods */
4153 distribute_charges(o_ptr, i_ptr, amt);
4155 /* Modify quantity */
4156 i_ptr->number = amt;
4158 /* Describe local object */
4159 object_desc(o_name, sizeof(o_name), i_ptr, TRUE, 3);
4161 /* Message */
4162 msg_format("You drop %s (%c).", o_name, index_to_label(item));
4164 /* Drop it near the player */
4165 drop_near(i_ptr, 0, py, px);
4167 /* Modify, Describe, Optimize */
4168 inven_item_increase(item, -amt);
4169 inven_item_describe(item);
4170 inven_item_optimize(item);
4176 * Combine items in the pack
4177 * Also "pick up" any gold in the inventory by accident
4179 * Note special handling of the "overflow" slot
4181 void combine_pack(void)
4183 int i, j, k;
4185 object_type *o_ptr;
4186 object_type *j_ptr;
4188 bool flag = FALSE;
4191 /* Combine the pack (backwards) */
4192 for (i = INVEN_PACK; i > 0; i--)
4194 bool slide = FALSE;
4196 /* Get the item */
4197 o_ptr = &inventory[i];
4199 /* Skip empty items */
4200 if (!o_ptr->k_idx) continue;
4202 /* Absorb gold */
4203 if (o_ptr->tval == TV_GOLD)
4205 /* Count the gold */
4206 slide = TRUE;
4207 p_ptr->au += o_ptr->pval;
4210 /* Scan the items above that item */
4211 else for (j = 0; j < i; j++)
4213 /* Get the item */
4214 j_ptr = &inventory[j];
4216 /* Skip empty items */
4217 if (!j_ptr->k_idx) continue;
4219 /* Can we drop "o_ptr" onto "j_ptr"? */
4220 if (object_similar(j_ptr, o_ptr))
4222 /* Take note */
4223 flag = slide = TRUE;
4225 /* Add together the item counts */
4226 object_absorb(j_ptr, o_ptr);
4228 break;
4233 /* Compact the inventory */
4234 if (slide)
4236 /* One object is gone */
4237 p_ptr->inven_cnt--;
4239 /* Slide everything down */
4240 for (k = i; k < INVEN_PACK; k++)
4242 /* Hack -- slide object */
4243 COPY(&inventory[k], &inventory[k+1], object_type);
4246 /* Hack -- wipe hole */
4247 object_wipe(&inventory[k]);
4249 /* Window stuff */
4250 p_ptr->window |= (PW_INVEN);
4254 /* Message */
4255 if (flag) msg_print("You combine some items in your pack.");
4260 * Reorder items in the pack
4262 * Note special handling of the "overflow" slot
4264 void reorder_pack(void)
4266 int i, j, k;
4268 s32b o_value;
4269 s32b j_value;
4271 object_type *o_ptr;
4272 object_type *j_ptr;
4274 object_type *i_ptr;
4275 object_type object_type_body;
4277 bool flag = FALSE;
4280 /* Re-order the pack (forwards) */
4281 for (i = 0; i < INVEN_PACK; i++)
4283 /* Mega-Hack -- allow "proper" over-flow */
4284 if ((i == INVEN_PACK) && (p_ptr->inven_cnt == INVEN_PACK)) break;
4286 /* Get the item */
4287 o_ptr = &inventory[i];
4289 /* Skip empty slots */
4290 if (!o_ptr->k_idx) continue;
4292 /* Get the "value" of the item */
4293 o_value = object_value(o_ptr);
4295 /* Scan every occupied slot */
4296 for (j = 0; j < INVEN_PACK; j++)
4298 /* Get the item already there */
4299 j_ptr = &inventory[j];
4301 /* Use empty slots */
4302 if (!j_ptr->k_idx) break;
4304 /* Hack -- readable books always come first */
4305 if ((o_ptr->tval == cp_ptr->spell_book) &&
4306 (j_ptr->tval != cp_ptr->spell_book)) break;
4307 if ((j_ptr->tval == cp_ptr->spell_book) &&
4308 (o_ptr->tval != cp_ptr->spell_book)) continue;
4310 /* Objects sort by decreasing type */
4311 if (o_ptr->tval > j_ptr->tval) break;
4312 if (o_ptr->tval < j_ptr->tval) continue;
4314 /* Non-aware (flavored) items always come last */
4315 if (!object_aware_p(o_ptr)) continue;
4316 if (!object_aware_p(j_ptr)) break;
4318 /* Objects sort by increasing sval */
4319 if (o_ptr->sval < j_ptr->sval) break;
4320 if (o_ptr->sval > j_ptr->sval) continue;
4322 /* Unidentified objects always come last */
4323 if (!object_known_p(o_ptr)) continue;
4324 if (!object_known_p(j_ptr)) break;
4326 /* Lites sort by decreasing fuel */
4327 if (o_ptr->tval == TV_LITE)
4329 if (o_ptr->pval > j_ptr->pval) break;
4330 if (o_ptr->pval < j_ptr->pval) continue;
4333 /* Determine the "value" of the pack item */
4334 j_value = object_value(j_ptr);
4336 /* Objects sort by decreasing value */
4337 if (o_value > j_value) break;
4338 if (o_value < j_value) continue;
4341 /* Never move down */
4342 if (j >= i) continue;
4344 /* Take note */
4345 flag = TRUE;
4347 /* Get local object */
4348 i_ptr = &object_type_body;
4350 /* Save a copy of the moving item */
4351 object_copy(i_ptr, &inventory[i]);
4353 /* Slide the objects */
4354 for (k = i; k > j; k--)
4356 /* Slide the item */
4357 object_copy(&inventory[k], &inventory[k-1]);
4360 /* Insert the moving item */
4361 object_copy(&inventory[j], i_ptr);
4363 /* Window stuff */
4364 p_ptr->window |= (PW_INVEN);
4367 /* Message */
4368 if (flag) msg_print("You reorder some items in your pack.");
4373 * Distribute charges of rods, staves, or wands.
4375 * o_ptr = source item
4376 * q_ptr = target item, must be of the same type as o_ptr
4377 * amt = number of items that are transfered
4379 void distribute_charges(object_type *o_ptr, object_type *q_ptr, int amt)
4382 * Hack -- If rods, staves, or wands are dropped, the total maximum
4383 * timeout or charges need to be allocated between the two stacks.
4384 * If all the items are being dropped, it makes for a neater message
4385 * to leave the original stack's pval alone. -LM-
4387 if ((o_ptr->tval == TV_WAND) ||
4388 (o_ptr->tval == TV_STAFF) ||
4389 (o_ptr->tval == TV_ROD))
4391 q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
4393 if (amt < o_ptr->number) o_ptr->pval -= q_ptr->pval;
4396 * Hack -- Rods also need to have their timeouts distributed.
4398 * The dropped stack will accept all time remaining to charge up to
4399 * its maximum.
4401 if ((o_ptr->tval == TV_ROD) && (o_ptr->timeout))
4403 if (q_ptr->pval > o_ptr->timeout)
4404 q_ptr->timeout = o_ptr->timeout;
4405 else
4406 q_ptr->timeout = q_ptr->pval;
4408 if (amt < o_ptr->number)
4409 o_ptr->timeout -= q_ptr->timeout;
4415 void reduce_charges(object_type *o_ptr, int amt)
4418 * Hack -- If rods or wand are destroyed, the total maximum timeout or
4419 * charges of the stack needs to be reduced, unless all the items are
4420 * being destroyed. -LM-
4422 if (((o_ptr->tval == TV_WAND) ||
4423 (o_ptr->tval == TV_STAFF) ||
4424 (o_ptr->tval == TV_ROD)) &&
4425 (amt < o_ptr->number))
4427 o_ptr->pval -= o_ptr->pval * amt / o_ptr->number;