/[adonthell]/adonthell/src/rpg/inventory.h
ViewVC logotype

Diff of /adonthell/src/rpg/inventory.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by ksterker, Mon Aug 23 06:33:47 2004 UTC revision 1.3 by ksterker, Tue Mar 8 09:41:48 2005 UTC
# Line 1  Line 1 
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 not.           * @param limited Whether %inventory has fixed size or can grow at runtime.
55           */           * @param owner (optional) %character to whom this inventory belongs.
56          inventory (const u_int16 & size = 0, const bool & limited = true,           */
57              character *owner = NULL);          inventory (const u_int16 & size = 0, const bool & limited = true,
58                            character *owner = NULL);
59          /**              
60           * Destroy inventory and its contents.          /**
61           */           * Destroy inventory and its contents.
62          ~inventory ();           */
63                    ~inventory ();
64          /**          
65           * @name Slot Handling          /**
66           */           * @name Slot Handling
67          //@{           */
68          /**          //@{
69           * Adds 'count' anonymous slots to the %inventory, thus increasing its          /**
70           * size.           * Adds 'count' anonymous slots to the %inventory, thus increasing its
71           * @param count Number of slots to add.           * size.
72           */           * @param count Number of slots to add.
73          void grow (const u_int16 & count);           */
74                    void grow (const u_int16 & count);
75          /**          
76           * Add a %slot with the given id. The id should be unique, otherwise          /**
77           * you might have troubles in retrieving the %slot with the given id.           * Add a %slot with the given id. The id should be unique, otherwise
78           * The size of the %inventory will grow by one.           * you might have troubles in retrieving the %slot with the given id.
79           * @param id Id of the %slot to add.           * The size of the %inventory will grow by one.
80           * @param equipment Whether the slot is suitable for equipping items           * @param id Id of the %slot to add.
81           */           * @param equipment Whether the slot is suitable for equipping items
82          void add_slot (const string & id, const bool & equipment = false);           */
83                    void add_slot (const string & id, const bool & equipment = false);
84          /**          
85           * Retrieve the %slot with given id from the %inventory.          /**
86           * @param id Id of the %slot to retrieve.           * Retrieve the %slot with given id from the %inventory.
87           * @return %slot with the given id, or \c NULL if no matching %slot exists.           * @param id Id of the %slot to retrieve.
88           */           * @return %slot with the given id, or \c NULL if no matching %slot exists.
89          slot *get_slot (const string & id);           */
90          //@}          slot *get_slot (const string & id);
91                        //@}
92          /**              
93          * @name Inventory iterators          /**
94          */          * @name Inventory iterators
95          //@{          */
96          /**          //@{
97           * Retrieves the first %slot of the %inventory. All subsequent slots          /**
98           * can be retrieved via inventory::next ().           * Retrieves the first %slot of the %inventory. All subsequent slots
99           * @return inventories first %slot, or \c NULL if there are no slots.           * can be retrieved via inventory::next ().
100           */           * @return inventories first %slot, or \c NULL if there are no slots.
101          slot *first ();           */
102                    slot *first ();
103          /**          
104           * After a call to inventory::first (), subsequent calls to this          /**
105           * method will return all slots of this %inventory, one by one.           * After a call to inventory::first (), subsequent calls to this
106           * Mainly useful for a GUI to display the inventories contents.           * method will return all slots of this %inventory, one by one.
107           * @return the next slot, or \c NULL if there are no more slots.           * Mainly useful for a GUI to display the inventories contents.
108           */           * @return the next slot, or \c NULL if there are no more slots.
109          slot *next ();           */
110          //@}          slot *next ();
111                    //@}
112          /**          
113           * @name Item handling          /**
114           */           * @name Item handling
115          //@{           */
116          /**          //@{
117           * Add item(s) to the %inventory. This method should be used when          /**
118           * the given items should not be added to a specific %slot, but to           * Add item(s) to the %inventory. This method should be used when
119           * this %inventory in general. It then tries to add them in the best           * the given items should not be added to a specific %slot, but to
120           * possible way: if the item(s) are stackable, they will be added to           * this %inventory in general. It then tries to add them in the best
121           * an already existing stack. Otherwise they will go into the first           * possible way: if the item(s) are stackable, they will be added to
122           * empty %slot. If not all items fitted, the number of the remaining           * an already existing stack. Otherwise they will go into the first
123           * items will be returned.           * empty %slot. If not all items fitted, the number of the remaining
124           *           * items will be returned.
125           * Note that there is no explicit remove method, as item removal will           *
126           * happen automatically.           * Note that there is no explicit remove method, as item removal will
127           *           * happen automatically.
128           * @param item The item(s) to add.           *
129           * @param count The number of items in the stack.           * @param item The item(s) to add.
130           * @return the number of items that did not fit.           * @param count The number of items in the stack.
131           */           * @return the number of items that did not fit.
132          u_int32 add (item *item, const u_int32 & count = 1);           */
133                    u_int32 add (item *item, const u_int32 & count = 1);
134          /**          
135           * Combine item(s) in the target %slot with the item(s) from the          /**
136           * agent %slot. The combination's outcome will depend on the items           * Combine item(s) in the target %slot with the item(s) from the
137           * involved. The target may be changed or transformed into a totally           * agent %slot. The combination's outcome will depend on the items
138           * different item. The agent could remain unchanged or even be           * involved. The target may be changed or transformed into a totally
139           * destroyed in the process. If no combination for target and agent           * different item. The agent could remain unchanged or even be
140           * is defined, nothing will happen.           * destroyed in the process. If no combination for target and agent
141           *           * is defined, nothing will happen.
142           * Note that target and agent need not be from the same inventory.           *
143           * The items resulting from the combination will always be added           * Note that target and agent need not be from the same inventory.
144           * to this inventory (which should be that of the target).           * The items resulting from the combination will always be added
145           *           * to this inventory (which should be that of the target).
146           * @param target %Slot containing the target items.           *
147           * @param agent %Slot containing the items to combine with target.           * @param target %Slot containing the target items.
148           * @return \b true if combination succeeded, \b false otherwise.           * @param agent %Slot containing the items to combine with target.
149           */           * @return \b true if combination succeeded, \b false otherwise.
150          bool combine (slot *target, slot *agent);           */
151          //@}          bool combine (slot *target, slot *agent);
152                //@}
153          /**      
154           * @name Query Methods          /**
155           */               * @name Query Methods
156          //@{           */    
157          /**          //@{
158           * Values representing the different query types. It is possible          /**
159           * to find items either by their name or by their category.           * Values representing the different query types. It is possible
160           */           * to find items either by their name or by their category.
161          enum           */
162          {          enum
163              MATCH_NAME      = 1,          {
164              MATCH_CATEGORY  = 2              MATCH_NAME      = 1,
165          };              MATCH_CATEGORY  = 2
166                };
167          /**      
168           * Find the first item that matches key. Depending on the          /**
169           * match parameter, items are either retrieved by name or by           * Find the first item that matches key. Depending on the
170           * category.           * match parameter, items are either retrieved by name or by
171           * @param key The key that items must match.           * category.
172           * @param match The type of query being performed.           * @param key The key that items must match.
173           * @return First item matching key, or \c NULL if no match found.           * @param match The type of query being performed.
174           */               * @return First item matching key, or \c NULL if no match found.
175          item *find (const string & key, const u_int8 & match);           */    
176                    item *find (const string & key, const u_int8 & match);
177          /**          
178           * Continue a query started by inventory::find. This will return          /**
179           * subsequent items matching the parameters used by the last query.           * Continue a query started by inventory::find. This will return
180           * Note that this will not retrieve items in the same stack as the           * subsequent items matching the parameters used by the last query.
181           * one returned by inventory::find. That is, only the first item           * Note that this will not retrieve items in the same stack as the
182           * of each matching stack is retrieved.           * one returned by inventory::find. That is, only the first item
183           * @return More matching items, or \c NULL if no more match found.           * of each matching stack is retrieved.
184           */           * @return More matching items, or \c NULL if no more match found.
185          item *find_next ();           */
186          //@}          item *find_next ();
187                    //@}
188          /**          
189           * @name Loading/Saving          /**
190           */           * @name Loading/Saving
191          //@{           */
192          /**          //@{
193           * Load %inventory (and its contents) from stream.          /**
194           * @param file stream to load %invnetory from.           * Load %inventory (and its contents) from stream.
195           * @return \b true if loading successful, \b false otherwise.           * @param file stream to load %invnetory from.
196           */           * @return \b true if loading successful, \b false otherwise.
197          bool get_state (base::flat & file);           */
198                    bool get_state (base::flat & file);
199          /**          
200           * Save %inventory (and contents) to a stream.          /**
201           * @param file stream to save %inventory to.           * Save %inventory (and contents) to a stream.
202           * @return \b true if saving successful, \b false otherwise.           * @param file stream to save %inventory to.
203           */           * @return \b true if saving successful, \b false otherwise.
204          bool put_state (base::flat & file);           */
205          //@}          bool put_state (base::flat & file);
206                //@}
207          /**      
208           * Retrieve the %character to whom this inventory belongs.          /**
209           * @return Owner of the inventory, or \c NULL if nobody owns it.           * Retrieve the %character to whom this inventory belongs.
210           */           * @return Owner of the inventory, or \c NULL if nobody owns it.
211          character *owner () const           */
212          {          character *owner () const
213              return Owner;          {
214          }              return Owner;
215                    }
216      private:          
217          /// The slots this inventory provides.      private:
218          std::vector<slot*> Slots;          /// The slots this inventory provides.
219                std::vector<slot*> Slots;
220          /// Inventory iterator.      
221          std::vector<slot*>::iterator I;          /// Inventory iterator.
222                    std::vector<slot*>::iterator I;
223          /// Whether this inventory provides unlimited space or not. Inventories          
224          /// with unlimited space will automatically grow as items are added.          /// Whether this inventory provides unlimited space or not. Inventories
225          bool Limited;          /// with unlimited space will automatically grow as items are added.
226                    bool Limited;
227          /// Key of last query.          
228          string QueryKey;          /// Key of last query.
229                string QueryKey;
230          /// Type of last query.      
231          u_int8 QueryType;          /// Type of last query.
232                    u_int8 QueryType;
233          /// Character to whom this inventory is assigned.          
234          character *Owner;          /// Character to whom this inventory is assigned.
235      };          character *Owner;
236  }      };
237  #endif // RPG_INVENTORY_H  }
238    #endif // RPG_INVENTORY_H

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26