1 /**
2  * @file
3  * @brief Item definition.
4 **/
5 
6 #pragma once
7 
8 #include "description-level-type.h"
9 #include "level-id.h"
10 #include "monster-type.h"
11 #include "object-class-type.h"
12 #include "skill-type.h"
13 #include "store.h"
14 
15 // We are not 64 bits clean here yet since many places still pass (or store!)
16 // it as 32 bits or, worse, longs. I considered setting this as uint32_t,
17 // however, since free bits are exhausted, it's very likely we'll have to
18 // extend this in the future, so this should be easier than undoing the change.
19 typedef uint32_t iflags_t;
20 
21 struct item_def
22 {
23     object_class_type base_type; ///< basic class (eg OBJ_WEAPON)
24     uint8_t        sub_type;       ///< type within that class (eg WPN_DAGGER)
25 #pragma pack(push,2)
26     union
27     {
28         // These must all be the same size!
29         short plus;                 ///< + to hit/dam (weapons)
30         monster_type mon_type:16;   ///< corpse/chunk monster type
31         skill_type skill:16;        ///< the skill provided by a manual
32         short charges;              ///< # of charges held by a wand, etc
33         short net_durability;       ///< damage dealt to a net
34         short tithe_state;          ///< tithe state of a stack of gold
35     };
36     union
37     {
38         // These must all be the same size!
39         short plus2;        ///< legacy/generic name for this union
40         short net_placed;   ///< is this throwing net trapping something?
41         short skill_points; ///< # of skill points a manual gives
42         short stash_freshness; ///< where stash.cc stores corpse freshness
43     };
44 #pragma pack(pop)
45     union
46     {
47         // These must all be the same size!
48         int special;            ///< legacy/generic name
49         int unrand_idx;         ///< unrandart index (for get_unrand_entry)
50         uint32_t subtype_rnd;   ///< appearance of un-ID'd items, by subtype.
51                                 /// jewellery, scroll, staff, wand, potions
52                                 // see comment in item_colour()
53         int brand;              ///< weapon and armour brands
54         int freshness;          ///< remaining time until a corpse rots
55     };
56     uint8_t        rnd;            ///< random number, used for tile choice,
57                                    /// randart colours, and other per-item
58                                    /// random cosmetics. 0 = uninitialized
59     short          quantity;       ///< number of items
60     iflags_t       flags;          ///< item status flags
61 
62     /// The location of the item. Items in player inventory are indicated by
63     /// pos (-1, -1), items in monster inventory by (-2, -2), and items
64     /// in shops by (0, y) for y >= 5.
65     coord_def pos;
66     /// For floor items, index in the env.item array of the next item in the
67     /// pile. NON_ITEM for the last item in a pile. For items in player
68     /// inventory, instead the index into you.inv. For items in monster
69     /// inventory, equal to NON_ITEM + 1 + mindex. For items in shops,
70     /// equal to ITEM_IN_SHOP.
71     short  link;
72     /// Inventory letter of the item. For items in player inventory, equal
73     /// to index_to_letter(link). For other items, equal to the slot letter
74     /// the item had when it was last in player inventory.
75     short  slot;
76 
77     level_id orig_place;
78     short    orig_monnum;
79 
80     string inscription;
81 
82     CrawlHashTable props;
83 
84 public:
item_defitem_def85     item_def() : base_type(OBJ_UNASSIGNED), sub_type(0), plus(0), plus2(0),
86                  special(0), rnd(0), quantity(0), flags(0),
87                  pos(), link(NON_ITEM), slot(0), orig_place(),
88                  orig_monnum(0), inscription()
89     {
90     }
91 
92     string name(description_level_type descrip, bool terse = false,
93                 bool ident = false, bool with_inscription = true,
94                 bool quantity_in_words = false,
95                 iflags_t ignore_flags = 0x0) const;
96     bool has_spells() const;
97     bool cursed() const;
98     colour_t get_colour() const;
99 
is_typeitem_def100     bool is_type(int base, int sub) const
101     {
102         return base_type == base && sub_type == sub;
103     }
104 
105     /**
106      * Find the index of an item in the env.item array. Results are undefined
107      * if this item is not in the array!
108      *
109      * @pre The item is actually in the env.item array.
110      * @return  The index of this item in the env.item array, between
111      *          0 and MAX_ITEMS-1.
112      */
113     int  index() const;
114 
115     int  armour_rating() const;
116 
117     bool launched_by(const item_def &launcher) const;
118 
clearitem_def119     void clear()
120     {
121         *this = item_def();
122     }
123 
124     /**
125      * Sets this item as being held by a given monster.
126      *
127      * @param mon The monster. Must be in env.mons!
128      */
129     void set_holding_monster(const monster& mon);
130 
131     /**
132      * Get the monster holding this item.
133      *
134      * @return A pointer to the monster holding this item, null if none.
135      */
136     monster* holding_monster() const;
137 
138     /** Is this item being held by a monster? */
139     bool held_by_monster() const;
140 
141     bool defined() const;
142     bool appearance_initialized() const;
143     bool is_valid(bool info = false) const;
144 
145     /** Should this item be preserved as far as possible? */
146     bool is_critical() const;
147 
148     /** Is this item of a type that should not be generated enchanted? */
149     bool is_mundane() const;
150 
151 private:
152     string name_aux(description_level_type desc, bool terse, bool ident,
153                     bool with_inscription, iflags_t ignore_flags) const;
154 
155     colour_t randart_colour() const;
156 
157     colour_t ring_colour() const;
158     colour_t amulet_colour() const;
159 
160     colour_t rune_colour() const;
161 
162     colour_t weapon_colour() const;
163     colour_t missile_colour() const;
164     colour_t armour_colour() const;
165     colour_t wand_colour() const;
166     colour_t jewellery_colour() const;
167     colour_t potion_colour() const;
168     colour_t book_colour() const;
169     colour_t miscellany_colour() const;
170     colour_t corpse_colour() const;
171 };
172 
173 item_def get_item_known_info(const item_def& info);
174