1 |
/* |
/* |
2 |
$Id$ |
$Id$ |
3 |
|
|
4 |
Copyright (C) 2003/2004 Kai Sterker <kaisterker@linuxgames.com> |
Copyright (C) 2003/2004 Kai Sterker <kaisterker@linuxgames.com> |
5 |
Part of the Adonthell Project http://adonthell.linuxgames.com |
Part of the Adonthell Project http://adonthell.linuxgames.com |
6 |
|
|
7 |
Adonthell is free software; you can redistribute it and/or modify |
Adonthell is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
(at your option) any later version. |
11 |
|
|
12 |
Adonthell is distributed in the hope that it will be useful, |
Adonthell is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
GNU General Public License for more details. |
16 |
|
|
17 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
18 |
along with Adonthell; if not, write to the Free Software |
along with Adonthell; if not, write to the Free Software |
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
|
|
23 |
/** |
/** |
24 |
* @file rpg/inventory.h |
* @file rpg/inventory.h |
25 |
* @author Kai Sterker <kaisterker@linuxgames.com> |
* @author Kai Sterker <kaisterker@linuxgames.com> |
26 |
* |
* |
27 |
* @brief Container for items. |
* @brief Container for items. |
28 |
*/ |
*/ |
29 |
|
|
30 |
#ifndef RPG_INVENTORY_H |
#ifndef RPG_INVENTORY_H |
31 |
#define RPG_INVENTORY_H |
#define RPG_INVENTORY_H |
32 |
|
|
33 |
#include <vector> |
#include <vector> |
34 |
#include "rpg/slot.h" |
#include "rpg/slot.h" |
35 |
|
|
36 |
namespace rpg |
namespace rpg |
37 |
{ |
{ |
38 |
class character; |
class character; |
39 |
|
|
40 |
/** |
/** |
41 |
* Whenever items need to be stored by an object -- be it %character, |
* Whenever items need to be stored by an object -- be it %character, |
42 |
* chest, shop or %map -- an %inventory should be used. Inventories are |
* chest, shop or %map -- an %inventory should be used. Inventories are |
43 |
* more than a list of items. They allow combining and using of items |
* more than a list of items. They allow combining and using of items |
44 |
* (if the items involved provide this functionality), take care of |
* (if the items involved provide this functionality), take care of |
45 |
* passing items between inventories and provide means to query for |
* passing items between inventories and provide means to query for |
46 |
* items with certain properties. |
* items with certain properties. |
47 |
*/ |
*/ |
48 |
class inventory |
class inventory |
49 |
{ |
{ |
50 |
public: |
public: |
51 |
/** |
/** |
52 |
* Create an %inventory with 'count' anonymous slots. |
* Create an %inventory with 'count' anonymous slots. |
53 |
* @param size Initial size of the %inventory. |
* @param size Initial size of the %inventory. |
54 |
* @param limited Whether %inventory has fixed size or can grow at runtime. |
* @param limited Whether %inventory has fixed size or can grow at runtime. |
55 |
* @param owner (optional) %character to whom this inventory belongs. |
* @param owner (optional) %character to whom this inventory belongs. |
56 |
*/ |
*/ |
57 |
inventory (const u_int16 & size = 0, const bool & limited = true, |
inventory (const u_int16 & size = 0, const bool & limited = true, |
58 |
character *owner = NULL); |
character *owner = NULL); |
59 |
|
|
60 |
/** |
/** |
61 |
* Destroy inventory and its contents. |
* Destroy inventory and its contents. |
62 |
*/ |
*/ |
63 |
~inventory (); |
~inventory (); |
64 |
|
|
65 |
/** |
/** |
66 |
* @name Slot Handling |
* @name Slot Handling |
67 |
*/ |
*/ |
68 |
//@{ |
//@{ |
69 |
/** |
/** |
70 |
* Adds 'count' anonymous slots to the %inventory, thus increasing its |
* Adds 'count' anonymous slots to the %inventory, thus increasing its |
71 |
* size. |
* size. |
72 |
* @param count Number of slots to add. |
* @param count Number of slots to add. |
73 |
*/ |
*/ |
74 |
void grow (const u_int16 & count); |
void grow (const u_int16 & count); |
75 |
|
|
76 |
/** |
/** |
77 |
* Add a %slot with the given id. The id should be unique, otherwise |
* Add a %slot with the given id. The id should be unique, otherwise |
78 |
* you might have troubles in retrieving the %slot with the given id. |
* you might have troubles in retrieving the %slot with the given id. |
79 |
* The size of the %inventory will grow by one. |
* The size of the %inventory will grow by one. |
80 |
* @param id Id of the %slot to add. |
* @param id Id of the %slot to add. |
81 |
* @param equipment Whether the slot is suitable for equipping items |
* @param equipment Whether the slot is suitable for equipping items |
82 |
*/ |
*/ |
83 |
void add_slot (const string & id, const bool & equipment = false); |
void add_slot (const string & id, const bool & equipment = false); |
84 |
|
|
85 |
/** |
/** |
86 |
* Retrieve the %slot with given id from the %inventory. |
* Retrieve the %slot with given id from the %inventory. |
87 |
* @param id Id of the %slot to retrieve. |
* @param id Id of the %slot to retrieve. |
88 |
* @return %slot with the given id, or \c NULL if no matching %slot exists. |
* @return %slot with the given id, or \c NULL if no matching %slot exists. |
89 |
*/ |
*/ |
90 |
slot *get_slot (const string & id); |
slot *get_slot (const string & id); |
91 |
//@} |
//@} |
92 |
|
|
93 |
/** |
/** |
94 |
* @name Inventory iterators |
* @name Inventory iterators |
95 |
*/ |
*/ |
96 |
//@{ |
//@{ |
97 |
/** |
/** |
98 |
* Retrieves the first %slot of the %inventory. All subsequent slots |
* Retrieves the first %slot of the %inventory. All subsequent slots |
99 |
* can be retrieved via inventory::next (). |
* can be retrieved via inventory::next (). |
100 |
* @return inventories first %slot, or \c NULL if there are no slots. |
* @return inventories first %slot, or \c NULL if there are no slots. |
101 |
*/ |
*/ |
102 |
slot *first (); |
slot *first (); |
103 |
|
|
104 |
/** |
/** |
105 |
* After a call to inventory::first (), subsequent calls to this |
* After a call to inventory::first (), subsequent calls to this |
106 |
* method will return all slots of this %inventory, one by one. |
* method will return all slots of this %inventory, one by one. |
107 |
* Mainly useful for a GUI to display the inventories contents. |
* Mainly useful for a GUI to display the inventories contents. |
108 |
* @return the next slot, or \c NULL if there are no more slots. |
* @return the next slot, or \c NULL if there are no more slots. |
109 |
*/ |
*/ |
110 |
slot *next (); |
slot *next (); |
111 |
//@} |
//@} |
112 |
|
|
113 |
/** |
/** |
114 |
* @name Item handling |
* @name Item handling |
115 |
*/ |
*/ |
116 |
//@{ |
//@{ |
117 |
/** |
/** |
118 |
* Add item(s) to the %inventory. This method should be used when |
* Add item(s) to the %inventory. This method should be used when |
119 |
* the given items should not be added to a specific %slot, but to |
* the given items should not be added to a specific %slot, but to |
120 |
* this %inventory in general. It then tries to add them in the best |
* this %inventory in general. It then tries to add them in the best |
121 |
* possible way: if the item(s) are stackable, they will be added to |
* possible way: if the item(s) are stackable, they will be added to |
122 |
* an already existing stack. Otherwise they will go into the first |
* an already existing stack. Otherwise they will go into the first |
123 |
* empty %slot. If not all items fitted, the number of the remaining |
* empty %slot. If not all items fitted, the number of the remaining |
124 |
* items will be returned. |
* items will be returned. |
125 |
* |
* |
126 |
* Note that there is no explicit remove method, as item removal will |
* Note that there is no explicit remove method, as item removal will |
127 |
* happen automatically. |
* happen automatically. |
128 |
* |
* |
129 |
* @param item The item(s) to add. |
* @param item The item(s) to add. |
130 |
* @param count The number of items in the stack. |
* @param count The number of items in the stack. |
131 |
* @return the number of items that did not fit. |
* @return the number of items that did not fit. |
132 |
*/ |
*/ |
133 |
u_int32 add (item *item, const u_int32 & count = 1); |
u_int32 add (item *item, const u_int32 & count = 1); |
134 |
|
|
135 |
/** |
/** |
136 |
* Combine item(s) in the target %slot with the item(s) from the |
* Combine item(s) in the target %slot with the item(s) from the |
137 |
* agent %slot. The combination's outcome will depend on the items |
* agent %slot. The combination's outcome will depend on the items |
138 |
* involved. The target may be changed or transformed into a totally |
* involved. The target may be changed or transformed into a totally |
139 |
* different item. The agent could remain unchanged or even be |
* different item. The agent could remain unchanged or even be |
140 |
* destroyed in the process. If no combination for target and agent |
* destroyed in the process. If no combination for target and agent |
141 |
* is defined, nothing will happen. |
* is defined, nothing will happen. |
142 |
* |
* |
143 |
* Note that target and agent need not be from the same inventory. |
* Note that target and agent need not be from the same inventory. |
144 |
* The items resulting from the combination will always be added |
* The items resulting from the combination will always be added |
145 |
* to this inventory (which should be that of the target). |
* to this inventory (which should be that of the target). |
146 |
* |
* |
147 |
* @param target %Slot containing the target items. |
* @param target %Slot containing the target items. |
148 |
* @param agent %Slot containing the items to combine with target. |
* @param agent %Slot containing the items to combine with target. |
149 |
* @return \b true if combination succeeded, \b false otherwise. |
* @return \b true if combination succeeded, \b false otherwise. |
150 |
*/ |
*/ |
151 |
bool combine (slot *target, slot *agent); |
bool combine (slot *target, slot *agent); |
152 |
//@} |
//@} |
153 |
|
|
154 |
/** |
/** |
155 |
* @name Query Methods |
* @name Query Methods |
156 |
*/ |
*/ |
157 |
//@{ |
//@{ |
158 |
/** |
/** |
159 |
* Values representing the different query types. It is possible |
* Values representing the different query types. It is possible |
160 |
* to find items either by their name or by their category. |
* to find items either by their name or by their category. |
161 |
*/ |
*/ |
162 |
enum |
enum |
163 |
{ |
{ |
164 |
MATCH_NAME = 1, |
MATCH_NAME = 1, |
165 |
MATCH_CATEGORY = 2 |
MATCH_CATEGORY = 2 |
166 |
}; |
}; |
167 |
|
|
168 |
/** |
/** |
169 |
* Find the first item that matches key. Depending on the |
* Find the first item that matches key. Depending on the |
170 |
* match parameter, items are either retrieved by name or by |
* match parameter, items are either retrieved by name or by |
171 |
* category. |
* category. |
172 |
* @param key The key that items must match. |
* @param key The key that items must match. |
173 |
* @param match The type of query being performed. |
* @param match The type of query being performed. |
174 |
* @return First item matching key, or \c NULL if no match found. |
* @return First item matching key, or \c NULL if no match found. |
175 |
*/ |
*/ |
176 |
item *find (const string & key, const u_int8 & match); |
item *find (const string & key, const u_int8 & match); |
177 |
|
|
178 |
/** |
/** |
179 |
* Continue a query started by inventory::find. This will return |
* Continue a query started by inventory::find. This will return |
180 |
* subsequent items matching the parameters used by the last query. |
* subsequent items matching the parameters used by the last query. |
181 |
* Note that this will not retrieve items in the same stack as the |
* Note that this will not retrieve items in the same stack as the |
182 |
* one returned by inventory::find. That is, only the first item |
* one returned by inventory::find. That is, only the first item |
183 |
* of each matching stack is retrieved. |
* of each matching stack is retrieved. |
184 |
* @return More matching items, or \c NULL if no more match found. |
* @return More matching items, or \c NULL if no more match found. |
185 |
*/ |
*/ |
186 |
item *find_next (); |
item *find_next (); |
187 |
//@} |
//@} |
188 |
|
|
189 |
/** |
/** |
190 |
* @name Loading/Saving |
* @name Loading/Saving |
191 |
*/ |
*/ |
192 |
//@{ |
//@{ |
193 |
/** |
/** |
194 |
* Load %inventory (and its contents) from stream. |
* Load %inventory (and its contents) from stream. |
195 |
* @param file stream to load %invnetory from. |
* @param file stream to load %invnetory from. |
196 |
* @return \b true if loading successful, \b false otherwise. |
* @return \b true if loading successful, \b false otherwise. |
197 |
*/ |
*/ |
198 |
bool get_state (base::flat & file); |
bool get_state (base::flat & file); |
199 |
|
|
200 |
/** |
/** |
201 |
* Save %inventory (and contents) to a stream. |
* Save %inventory (and contents) to a stream. |
202 |
* @param file stream to save %inventory to. |
* @param file stream to save %inventory to. |
203 |
* @return \b true if saving successful, \b false otherwise. |
* @return \b true if saving successful, \b false otherwise. |
204 |
*/ |
*/ |
205 |
bool put_state (base::flat & file); |
bool put_state (base::flat & file); |
206 |
//@} |
//@} |
207 |
|
|
208 |
/** |
/** |
209 |
* Retrieve the %character to whom this inventory belongs. |
* Retrieve the %character to whom this inventory belongs. |
210 |
* @return Owner of the inventory, or \c NULL if nobody owns it. |
* @return Owner of the inventory, or \c NULL if nobody owns it. |
211 |
*/ |
*/ |
212 |
character *owner () const |
character *owner () const |
213 |
{ |
{ |
214 |
return Owner; |
return Owner; |
215 |
} |
} |
216 |
|
|
217 |
private: |
private: |
218 |
/// The slots this inventory provides. |
/// The slots this inventory provides. |
219 |
std::vector<slot*> Slots; |
std::vector<slot*> Slots; |
220 |
|
|
221 |
/// Inventory iterator. |
/// Inventory iterator. |
222 |
std::vector<slot*>::iterator I; |
std::vector<slot*>::iterator I; |
223 |
|
|
224 |
/// Whether this inventory provides unlimited space or not. Inventories |
/// Whether this inventory provides unlimited space or not. Inventories |
225 |
/// with unlimited space will automatically grow as items are added. |
/// with unlimited space will automatically grow as items are added. |
226 |
bool Limited; |
bool Limited; |
227 |
|
|
228 |
/// Key of last query. |
/// Key of last query. |
229 |
string QueryKey; |
string QueryKey; |
230 |
|
|
231 |
/// Type of last query. |
/// Type of last query. |
232 |
u_int8 QueryType; |
u_int8 QueryType; |
233 |
|
|
234 |
/// Character to whom this inventory is assigned. |
/// Character to whom this inventory is assigned. |
235 |
character *Owner; |
character *Owner; |
236 |
}; |
}; |
237 |
} |
} |
238 |
#endif // RPG_INVENTORY_H |
#endif // RPG_INVENTORY_H |