adonthell-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Adonthell-commits] CVS: adonthell/src item_storage.cc,NONE,1.1 item_st


From: Kai Sterker <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src item_storage.cc,NONE,1.1 item_storage.h,NONE,1.1 Makefile.am,1.100,1.101 inventory.cc,1.2,1.3 inventory.h,1.7,1.8 item_base.cc,1.5,1.6 item_base.h,1.5,1.6 py_adonthell.i,1.36,1.37 slot.cc,1.1,1.2 slot.h,1.1,1.2
Date: Tue, 11 Feb 2003 14:59:28 -0500

Update of /cvsroot/adonthell/adonthell/src
In directory subversions:/tmp/cvs-serv5069

Modified Files:
        Makefile.am inventory.cc inventory.h item_base.cc item_base.h 
        py_adonthell.i slot.cc slot.h 
Added Files:
        item_storage.cc item_storage.h 
Log Message:
ADDED inventory class
ADDED global storage for immutable items
FIXED bug when moving more items than are in a slot


***** Error reading new file: [Errno 2] No such file or directory: 
'item_storage.cc'
***** Error reading new file: [Errno 2] No such file or directory: 
'item_storage.h'
Index: Makefile.am
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Makefile.am,v
retrieving revision 1.100
retrieving revision 1.101
diff -C2 -r1.100 -r1.101
*** Makefile.am 10 Feb 2003 20:01:13 -0000      1.100
--- Makefile.am 11 Feb 2003 19:59:26 -0000      1.101
***************
*** 22,26 ****
  drawing_area.h event.h event_handler.h event_handler_base.h event_list.h \
  fileops.h game.h gamedate.h gametime.h gettext.h getopt.h image.h input.h \
! inventory.h item_base.h landmap.h map_event.h map_event_handler.h mapsquare.h 
\
  mapsquare_walkable.h nls.h mapcharacter.h mapobject.h mapview.h path.h \
  pnm.h prefs.h python_class.h py_object.h quest.h screen.h slot.h surface.h 
storage.h \
--- 22,26 ----
  drawing_area.h event.h event_handler.h event_handler_base.h event_list.h \
  fileops.h game.h gamedate.h gametime.h gettext.h getopt.h image.h input.h \
! inventory.h item_base.h item_storage.h landmap.h map_event.h 
map_event_handler.h mapsquare.h \
  mapsquare_walkable.h nls.h mapcharacter.h mapobject.h mapview.h path.h \
  pnm.h prefs.h python_class.h py_object.h quest.h screen.h slot.h surface.h 
storage.h \
***************
*** 35,39 ****
  drawable.cc drawing_area.cc event.cc event_handler.cc event_list.cc \
  fileops.cc game.cc gamedata.cc gamedate.cc gametime.cc image.cc input.cc \
! item_base.cc label.cc label_input.cc landmap.cc lex.prefs.cc map_event.cc \
  map_event_handler.cc mapsquare_walkable.cc mapcharacter.cc mapsquare.cc \
  mapobject.cc mapview.cc nls.cc path.cc pnm.cc prefs.cc py_callback.cc \
--- 35,39 ----
  drawable.cc drawing_area.cc event.cc event_handler.cc event_list.cc \
  fileops.cc game.cc gamedata.cc gamedate.cc gametime.cc image.cc input.cc \
! inventory.cc item_base.cc item_storage.cc label.cc label_input.cc landmap.cc 
lex.prefs.cc map_event.cc \
  map_event_handler.cc mapsquare_walkable.cc mapcharacter.cc mapsquare.cc \
  mapobject.cc mapview.cc nls.cc path.cc pnm.cc prefs.cc py_callback.cc \

Index: inventory.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/inventory.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** inventory.cc        29 Jun 2001 20:40:18 -0000      1.2
--- inventory.cc        11 Feb 2003 19:59:26 -0000      1.3
***************
*** 1,28 ****
! /* this is just a simple test file.. */
  
- #include <iostream.h>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include "item.h"
  #include "inventory.h"
  
! void
! main (void)
  {
!     inventory *inv = new inventory ();
!     int a[] = { 0, 1, 2, 3, 4, 5 };
!     item *i = new item (1, "a Short Sword", a, 120, "", NULL);
!     item *k = new item (2, "a Holocaust Cloak", a, 50, "", NULL);
!     item *l = new item (3, "A sword named String", a, 100, "", NULL);
!     item *j = new item (4, "a Harbringer", a, 120, "", NULL);
       
!     item x;
!     cout << "Sizeof empty item " << sizeof (x) << "\n" << flush;
  
!     inv->insert (i);
!     inv->insert (k);
!     inv->insert (j);
!     inv->insert (l);
!     inv->display ();
  }
--- 1,160 ----
! /*
!    $Id$
!    
!    Copyright (C) 2003 Kai Sterker <address@hidden>
!    Part of the Adonthell Project http://adonthell.linuxgames.com
! 
!    This program is free software; you can redistribute it and/or modify
!    it under the terms of the GNU General Public License.
!    This program is distributed in the hope that it will be useful,
!    but WITHOUT ANY WARRANTY.
! 
!    See the COPYING file for more details.
! */
! 
! 
! /**
!  * @file   inventory.cc
!  * @author Kai Sterker <address@hidden> 
!  * 
!  * @brief  Container for items.
!  */
  
  #include "inventory.h"
+ #include "item_base.h"
+ 
+ // ctor
+ inventory::inventory (const bool & limited, const u_int16 & size)
+ {
+     Limited = limited;
+     grow (size);
+ }
+ 
+ // dtor
+ inventory::~inventory ()
+ {
+     std::vector<slot*>::iterator i;
+     
+     // delete all slots
+     for (i = Slots.begin (); i != Slots.end (); i++)
+         delete (*i);
+ 
+     Slots.clear ();
+ }
+ 
+ // grow inventory
+ void inventory::grow (const u_int16 & count)
+ {
+     for (u_int16 i = 0; i == count; i++)
+         Slots.push_back (new slot (this));
+ }
+ 
+ // add named slot to inventory
+ void inventory::add_slot (const std::string & id)
+ {
+     Slots.push_back (new slot (this, id));
+ }
+ 
+ // retrieve named slot from inventory
+ slot *inventory::get_slot (const std::string & id)
+ {
+     std::vector<slot*>::iterator i;
+     
+     for (i = Slots.begin (); i != Slots.end (); i++)
+         if ((*i)->id () == id) return *i;    
+ 
+     return NULL;
+ }
+ 
+ // start iteration
+ slot *inventory::first ()
+ {
+     if (Slots.empty ()) return NULL;
+     
+     I = Slots.begin ();
+     return *I;
+ }
+ 
+ // continue iteration
+ slot *inventory::next ()
+ {
+     if ((++I) == Slots.end ()) return NULL;
+     else return *I;
+ }
+ 
+ // insert item(s)
+ u_int32 inventory::add (item_base *item, const u_int32 & count)
+ {
+     std::vector<slot*>::iterator i;
+     s_int32 remaining = count;
+     
+     // if item stackable, look for matching stack
+     if (item->max_stack () > 1)
+     {
+         for (i = Slots.begin (); i != Slots.end (); i++)
+             if ((*i)->count () != 0 && item->equals ((*i)->get_item ()))
+             {
+                 remaining = (*i)->add (item, remaining);
+                 if (remaining == 0) return 0;
+             } 
+     }
+     
+     // add item(s) to empty slot(s)
+     for (i = Slots.begin (); i != Slots.end (); i++)
+         if ((*i)->count () == 0)
+         {
+             remaining = (*i)->add (item, remaining);
+             if (remaining == 0) return 0;
+         } 
+ 
+     // return how many items didn't fit
+     return remaining;  
+ }
  
! // save inventory
! bool inventory::put_state (ogzstream & file)
  {
!     std::vector<slot*>::iterator i;
! 
!     // save attributes
!     Limited >> file;
!     
!     // save contents
!     for (i = Slots.begin (); i != Slots.end (); i++)
!     {
!         true >> file;
!         (*i)->put_state (file);
!     }
!     
!     // no more slots
!     false >> file;
! 
!     return true;    
! }
! 
! // load inventory
! bool inventory::get_state (igzstream & file)
! {
!     bool more;
!     slot *s;
!     
!     // load attributs
!     Limited << file;
! 
!     // load contents
!     more << file;
!     
!     while (more)
!     {
!         // load slot
!         s = new slot (this);
!         s->get_state (file);
       
!         // add slot to inventory   
!         Slots.push_back (s);
!         
!         more << file;
!     }    
  
!     return true;    
  }

Index: inventory.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/inventory.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** inventory.h 4 May 2001 15:05:33 -0000       1.7
--- inventory.h 11 Feb 2003 19:59:26 -0000      1.8
***************
*** 1,3 ****
--- 1,6 ----
  /*
+    $Id$
+    
+    Copyright (C) 2003 Kai Sterker <address@hidden>
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 8,269 ****
  
     See the COPYING file for more details.
  
  
!   this is the main class for the inventory subsystem.
!   it uses a hash table based on the objects name to 
!   allocate storage in the inventory. It supports inventory
!   growth through prime sequences. A 101 element array can
!   easily store hundreds more using linked lists inside the
!   element. I felt this was optimal since a person isn't 
!   carry that many objects, but allows for growth if they
!   do. Searching is fast. A simple matter of grabbing the key
!   and doing a linear search down a short list...
!   
!   methods to add: remove, get, etc.
! */
  
! #ifndef _INVENTORY_H_
! #define _INVENTORY_H_
  
- #include <fstream>
- #include <string>
  #include <vector>
! #include <math.h>
! 
! #include "item.h"
! #include "types.h"
  
  class inventory
  {
!  public:
! 
!   // CONSTANTS
!   // ========================================================================
! 
!   const static u_int16 base = 37;     // needed by the hashcode generator
!   const static u_int16 HASH_SIZE = 1; /* size of the hash array itself; the
!                                        number of items that can be held by
!                                        the inventory defaults to HASH_SIZE
!                                        * 5, but this can be changed with a
!                                        call to setMaxItems() */
      
!   // ========================================================================
! 
!   // CONSTRUCTORS
!   // ========================================================================
! 
!   inventory(); // default constructor
!   inventory( const string fn ); /* from file constructor: given a filename, 
fn,
!                                  it sucks an inventory object in from file */
!   /*
!   ~inventory();
!   */
!   // The copy constructor is implicitly defined by the compiler
! 
!   // ========================================================================
! 
!   // OPERATORS
!   // ========================================================================
! 
!   inventory* operator=( const inventory& it ); // assignment operator
!   bool operator==( const inventory& it ) const; // equality operator
!   bool operator!=( const inventory& it ) const; // inequality operator
! 
!   /* These operators add and subract the contents of an inventory.
! 
!      += works like this: if you have an inventory, A, that contains 27 items,
!      and one, B, that contains 10 items, A += B will add the contents of B to
!      A, thus making the number of items contained in A 37.
! 
!      -= is a little different: Given the same A and B from the last example,
!      A -= B will subtract from A all the items that exist in a intersection of
!      A and B, that is, everything that exists in *both* A and B. So, supposing
!      that the intersection of A and B contains 7 items, A -= B will result in
!      A having 20 items. */
!      
!   bool operator+=( const inventory& it ); // plus assignment operator
!   bool operator-=( const inventory& it ); // minus assignment operator
! 
!   /* These operators are all comparisons on the number of items contained in
!      the inventory */
! 
!   bool operator>( const inventory& it ) const; // greater than operator
!   bool operator<( const inventory& it ) const; // less than operator
!   bool operator>=( const inventory& it ) const; // greater than or equal to?
!   bool operator<=( const inventory& it ) const; // less than or equal to?
!   
!   // ========================================================================
! 
!   // DEBUG FUNCTIONS
!   // ========================================================================
!   
!   bool debug( const bool val="true" ); // turns cout debugging on/off (t/f)
!   void showHash() const; // displays the contents of the hash
!   void showDisp() const; // displays the display list
! 
!   // ========================================================================
! 
!   // GET FUNCTIONS (VALUE RETURNING)
!   // ========================================================================
! 
!   u_int32 getNumItemO() const; /* returns the number of item objects currently
!                                 contained in the inventory */
!   u_int32 getMaxItemO() const; /* returns the maximum number of items that
!                                 can be stored in the inventory (the default
!                                 is HASH_SIZE * 5); as storage is dynamically
!                                 allocated, this may be changed at will */
!   u_int32 getNumItems() const; /* returns the number of items currently
!                                 contained in the inventory */
!   u_int32 getMaxItems() const; /* returns the maximum number of item objects
!                                 that may be stored in the inventory (the
!                                 default is 200) */
!   u_int32 getDispItems() const; /* returns the number of items and 
placeholders
!                                  in the display vector */
!   u_int32 getWeight() const; /* returns the total weight of all items
!                               contained in the inventory */
!   item* getItem( const u_int32 i ); /* returns a pointer to the item at
!                                      position i in the display list or NULL
!                                      if i is an invalid index */
!   item* getItem( const string name ); /* returns a point to the item with the
!                                        name name or NULL if there are no
!                                        items with said name in the
!                                        inventory */
! 
!   // ========================================================================
! 
!   // SET FUNCTIONS (SET MEMBERS)
!   // ========================================================================
! 
!   bool setMaxItemO( const u_int32 mi ); /* sets the maximum number of item
!                                          objects that may be stored in the
!                                          inventory; this defaults to
!                                          HASH_SIZE * 5 */
!   bool setMaxItems( const u_int32 mi ); /* sets the maximum number of items
!                                          that may be stored (default is
!                                          200) */
! 
!   // ========================================================================
! 
!   // INSERTION/DELETION FUNCTIONS
!   // ========================================================================
! 
!   bool insert( item* it ); // inserts an item into the inventory
!   bool remove( const u_int32 i, const u_int16 n ); /* removes the item at 
index
!                                                    i of the display list from
!                                                    the inventory */
!   bool remove( const string name, const u_int16 n ); /* removes n of the item
!                                                      with the name name
!                                                      from the inventory; if n
!                                                      is 0, removes all of the
!                                                      items with said name
!                                                      (please note that a
!                                                      deletion of an item with
!                                                      this function takes
!                                                      linear time, which I
!                                                      hope to fix, but it is
!                                                      much better to use the
!                                                      other remove() when
!                                                      possible) */
! 
!   // ========================================================================
! 
!   // MATHEMATICAL SET FUNCTIONS
!   // ========================================================================
! 
!   inventory interSet( const inventory& it ) const; /* returns an inventory 
that
!                                                     is the intersection of
!                                                     *this and it */
! 
!   inventory unionSet( const inventory& it ) const; /* returns an inventory 
that
!                                                     is the union of *this and
!                                                     it */
! 
!   // ========================================================================
! 
!   // DISPLAY FUNCTIONS
!   // ========================================================================
! 
!   bool swap( const u_int32 i1, const u_int32 i2 ); /* swaps the position in 
the
!                                                     display list of the item
!                                                     at indexes i1 and i2 */
! 
!   // ========================================================================
! 
!  private:
! 
!   // PROTECTED FUNCTIONS
!   // ========================================================================
!   
!   bool setNumItemO( const u_int32 ni ); /* sets the number of item objects
!                                          currently in the inventory */
!   bool setNumItems( const u_int32 ni ); /* sets the number of items currently
!                                          in the inventory */
!   bool setWeight( const u_int32 tw ); /* sets the total weight of all
!                                        items in the inventory */
!   u_int32 hashcode( const string name ); // generates a hashcode given a name
!   bool getHash( const string name, u_int32& i, u_int32& d, u_int32& di );
!                               /* for a given name 'name', sets the index 'i'
!                                and depth 'd' of the item's location in the
!                                hash table and index in the display vector
!                                'di' or returns false if the item is not
!                                found in the table */
!   bool removeDisp( const string nm, const u_int16 n, const u_int32 i );
!                               /* removes 'n' of the item with the index
!                                'i' from the display list; 'nm' is just passed
!                                as a precaution against accidental deletion */
!   bool removeHash( const string nm, u_int16 n, 
!                  const u_int32 i, const u_int32 d ); /* removes n of the item
!                                                         at index 'i', depth
!                                                         'd' from the hash and
!                                                         sets 'di' to the
!                                                         index of the item in
!                                                         the display list;
!                                                         'nm' is passed along
!                                                         simply to ensure that
!                                                         the wrong item is not
!                                                         accidentally 
!                                                         deleted */
!   item* getPlaceholder() const; // returns a pointer to the placeholder item
!   bool binarySearch( const u_int target, u_int32 first,
!                    u_int32 last, u_int32& posi ); /* the good old binary
!                                                      search algorithm, acting
!                                                      on the placeholder
!                                                      position stack */
!   bool binaryPairSearch( const u_int target, u_int32 first,
!                        u_int32 last, u_int32& posi ); /* the modified binary
!                                                          search algorithm
!                                                          that operates on the
!                                                          placeholder position
!                                                          stack and returns
!                                                          the place to stick
!                                                          the new placeholder
!                                                          position */
! 
!   // ========================================================================
! 
!   // PROTECTED MEMBER VARIABLES
!   // ========================================================================
! 
!   bool debug_mode; // if true, some cout debugging will be done
!   u_int32 total_weight; // total weight of all items in the inventory
!   u_int32 num_item_o; // number of item objects currently stored
!   u_int32 max_item_o; // maximum number of item objects that may be stored
!   u_int32 num_items; /* number of actual items that are stored in this
!                       inventory; this differs from num_items in that if you
!                       have an inventory containing:
!                       Short Sword (getNumber() == 1),
!                       Long Bow (getNumber() == 1), and
!                       Sheaf Arrows (getNumber == 27),
!                       num_item_o will be 3, but num_items will be 29 */
!   u_int32 max_items; /* maximum number of items that may be stored (default is
!                       200); see the above note on num_items for an 
!                       explanation of the difference between max_item_o and
!                       max_items */
!   item* inv[HASH_SIZE]; // hash table
!   vector< item* > disp; // display list
!   item* pl; // The placeholder item for the display vector
!   vector< u_int32 > pl_pos; /* the positions of all placeholders in the 
display
!                              vector */
  };
  
! #endif
--- 11,161 ----
  
     See the COPYING file for more details.
+ */
  
  
! /**
!  * @file   inventory.h
!  * @author Kai Sterker <address@hidden> 
!  * 
!  * @brief  Container for items.
!  */
  
! #ifndef INVENTORY_H
! #define INVENTORY_H
  
  #include <vector>
! #include "slot.h"
  
+ /**
+  * Whenever items need to be stored by an object -- be it %character,
+  * chest, shop or %map -- an %inventory should be used. Inventories are
+  * more than a list of items. They allow combining and using of items
+  * (if the items involved provide this functionality), take care of
+  * passing items between inventories and provide means to query for
+  * items with certain properties.
+  */
  class inventory
  {
! public:
!     /**
!      * Create an %inventory with 'count' anonymous slots.
!      * @param limited Whether %inventory has fixed size or not.
!      * @param size Initial size of the %inventory.
!      */
!     inventory (const bool & limited = true, const u_int16 & size = 0);
!         
!     /**
!      * Destroy inventory and its contents.
!      */
!     ~inventory ();
      
!     /**
!      * @name Slot Handling
!      */
!     //@{
!     /**
!      * Adds 'count' anonymous slots to the %inventory, thus increasing its
!      * size.
!      * @param count Number of slots to add.
!      */
!     void grow (const u_int16 & count);
!     
!     /**
!      * Add a %slot with the given id. The id should be unique, otherwise
!      * you might have troubles in retrieving the %slot with the given id.
!      * The size of the %inventory will grow by one.
!      * @param id Id of the %slot to add.
!      */
!     void add_slot (const string & id);
!     
!     /**
!      * Retrieve the %slot with given id from the %inventory.
!      * @param id Id of the %slot to retrieve.
!      * @return %slot with the given id, or \c NULL if no matching %slot 
exists.
!      */
!     slot *get_slot (const string & id);
!     //@}
!         
!     /**
!      * @name Inventory iterators
!      */
!     //@{
!     /**
!      * Retrieves the first %slot of the %inventory. All subsequent slots
!      * can be retrieved via inventory::next ().
!      * @return inventories first %slot, or \c NULL if there are no slots.
!      */
!     slot *first ();
!     
!     /**
!      * After a call to inventory::first (), subsequent calls to this
!      * method will return all slots of this %inventory, one by one.
!      * Mainly useful for a GUI to display the inventories contents.
!      * @return the next slot, or \c NULL if there are no more slots.
!      */
!     slot *next ();
!     //@}
!     
!     /**
!      * @name Item handling
!      */
!     //@{
!     /**
!      * Add item(s) to the %inventory. This method should be used when
!      * the given items should not be added to a specific %slot, but to
!      * this %inventory in general. It then tries to add them in the best
!      * possible way: if the item(s) are stackable, they will be added to
!      * an already existing stack. Otherwise they will go into the first 
!      * empty %slot. If not all items fitted, the number of the remaining
!      * items will be returned.
!      *
!      * Not that there is no explicit remove method, as item removal will
!      * happen automatically.
!      *
!      * @param item The item(s) to add.
!      * @param count The number of items in the stack.
!      * @return the number of items that did not fit.
!      */
!     u_int32 add (item_base *item, const u_int32 & count);
!     //@}
!     
!     /**
!      * @name Loading/Saving
!      */
!     //@{
!     /**
!      * Load %inventory (and its contents) from stream. 
!      * @param file stream to load %invnetory from.
!      * @return \b true if loading successful, \b false otherwise.
!      */
!     bool get_state (igzstream & file);
!     
!     /**
!      * Save %inventory (and contents) to a stream.
!      * @param file stream to save %inventory to.
!      * @return \b true if saving successful, \b false otherwise.
!      */
!     bool put_state (ogzstream & file);
!     //@}
! 
! private:
! #ifndef SWIG
!     /**
!      * The slots this inventory provides.
!      */
!     std::vector<slot*> Slots;
! 
!     /**
!      * Inventory iterator.
!      */
!     std::vector<slot*>::iterator I;
!     
!     /**
!      * Whether this inventory provides unlimited space or not. Inventories
!      * with unlimited space will automatically grow as items are added.
!      */
!     bool Limited;
! #endif // SWIG
  };
  
! #endif // INVENTORY_H

Index: item_base.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_base.cc,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** item_base.cc        10 Feb 2003 20:01:13 -0000      1.5
--- item_base.cc        11 Feb 2003 19:59:26 -0000      1.6
***************
*** 195,198 ****
--- 195,205 ----
  */
  
+ // compare two items for equality
+ bool item_base::equals (item_base *item)
+ {
+     if (!Mutable) return this == item;
+     else return name () == item->name ();
+ }
+         
  // add item(s) to this stack
  void item_base::stack (item_base *item)

Index: item_base.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_base.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** item_base.h 10 Feb 2003 20:01:13 -0000      1.5
--- item_base.h 11 Feb 2003 19:59:26 -0000      1.6
***************
*** 297,300 ****
--- 297,309 ----
      //@}
  
+     /**
+      * Chack whether two are of the same kind. For mutable items this is
+      * the case if they have the same name. For immutable items, they need
+      * to be the same instances.
+      * @param the item to compare this one to.
+      * @return \b true if items are (roughly) equal, \b false otherwise.
+      */
+     bool equals (item_base *item);
+              
  protected:
  #ifndef SWIG

Index: py_adonthell.i
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_adonthell.i,v
retrieving revision 1.36
retrieving revision 1.37
diff -C2 -r1.36 -r1.37
*** py_adonthell.i      10 Feb 2003 20:01:13 -0000      1.36
--- py_adonthell.i      11 Feb 2003 19:59:26 -0000      1.37
***************
*** 16,21 ****
  #include "audio.h"
  #include "character_base.h"
! #include "slot.h"
! #include "item_base.h"
  #include "quest.h"
  #include "drawing_area.h"
--- 16,20 ----
  #include "audio.h"
  #include "character_base.h"
! #include "item_storage.h"
  #include "quest.h"
  #include "drawing_area.h"
***************
*** 150,153 ****
--- 149,153 ----
  %include "py_object.h"
  %include "item_base.h"
+ %include "item_storage.h"
  %include "drawing_area.h"
  %include "quest.h"

Index: slot.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/slot.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** slot.cc     10 Feb 2003 20:01:13 -0000      1.1
--- slot.cc     11 Feb 2003 19:59:26 -0000      1.2
***************
*** 22,31 ****
  
  #include "slot.h"
! #include "item_base.h"
  
  // ctor
! slot::slot (/*inventory *owner,*/ const std::string & id)
  {
!     // Owner = owner;
      Id = id;
      Count = 0;
--- 22,31 ----
  
  #include "slot.h"
! #include "item_storage.h"
  
  // ctor
! slot::slot (inventory *owner, const std::string & id)
  {
!     Owner = owner;
      Id = id;
      Count = 0;
***************
*** 59,71 ****
          if (Count >= Item->max_stack ()) return count;
          
!         // immutable items of same kind?
!         if (!item->is_mutable () && item != Item) return count;
          
!         // mutable item of same kind?
!         if (item->is_mutable ())
!         {
!             if (item->name () == Item->name ()) Item->stack (item);
!             else return count;
!         }
      }
      
--- 59,67 ----
          if (Count >= Item->max_stack ()) return count;
          
!         // items of same kind?
!         if (!(Item->equals (item))) return count;
          
!         // actually stack mutable items
!         if (item->is_mutable ()) Item->stack (item);
      }
      
***************
*** 74,83 ****
      fitting = (fitting > count) ? count : fitting;
      
      // adjust counts
      Count += fitting;
      
-     // remove items from the inventory they came
-     if (item->get_slot ()) item->get_slot ()->remove (item, fitting);
-     
      // adjust slots
      if (item->is_mutable ()) item->set_slot (this);
--- 70,81 ----
      fitting = (fitting > count) ? count : fitting;
      
+     // remove items from the inventory they came
+     if (item->get_slot ())
+         // make sure we don't add more items than we remove
+         fitting -= item->get_slot ()->remove (item, fitting);
+     
      // adjust counts
      Count += fitting;
      
      // adjust slots
      if (item->is_mutable ()) item->set_slot (this);
***************
*** 88,95 ****
  
  // remove item(s) from slot
! bool slot::remove (item_base *item, const u_int32 & count)
  {
      // nothing needs be done
!     if (count == 0) return false;
  
      // in case items match, remove count items
--- 86,93 ----
  
  // remove item(s) from slot
! u_int32 slot::remove (item_base *item, const u_int32 & count)
  {
      // nothing needs be done
!     if (count == 0) return 0;
  
      // in case items match, remove count items
***************
*** 107,112 ****
          if (Count == 0) Item = NULL;
          
!         // return true if count items have been removed
!         return remove == count;
      }
      
--- 105,110 ----
          if (Count == 0) Item = NULL;
          
!         // return number of items that couldn't be removed
!         return count - remove;
      }
      
***************
*** 120,126 ****
              Count--;
              
!             return true;
          }
          
!     return false;
  }
--- 118,194 ----
              Count--;
              
!             return count - 1;
          }
          
!     return count;
! }
! 
! // empty the slot
! void slot::clear ()
! {
!     // if the slot contains mutable item(s), delete them
!     if (Item && Item->is_mutable ()) 
!     {
!         Item->set_slot (NULL);
!         delete Item;
!     }
!     
!     Item = NULL;
!     Count = 0;
! }
! 
! // save slot to disk
! bool slot::put_state (ogzstream & file) const
! {
!     // save slot attributes
!     Id >> file;
!     Count >> file;
!     
!     // save the item(s) in the slot
!     if (Count > 0) 
!     {
!         Item->is_mutable () >> file;
!         
!         // completely save mutable items
!         if (Item->is_mutable ()) Item->put_state (file);
!         
!         // otherwise only save unique item id
!         else Item->name () >> file; 
!     }
!     
!     return true;
! }
! 
! // load slot from disk
! bool slot::get_state (igzstream & file)
! {
!     // get slot attributes
!     Id << file;
!     Count << file;
! 
!     // get item(s) if any
!     if (Count > 0)
!     {
!         bool is_mutable;
!         is_mutable << file;
!         
!         // mutable items need to be instanciated and loaded
!         if (is_mutable) 
!         {
!             Item = new item_base (true);
!             Item->get_state (file);
!             Item->set_slot (this);
!         }
!         
!         // immutable items can be retrieved from the global item storage
!         else
!         {
!             std::string name;
!             name << file;
!             
!             Item = item_storage::get (name);
!         }
!     }
!     
!     return true;
  }

Index: slot.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/slot.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** slot.h      10 Feb 2003 20:01:13 -0000      1.1
--- slot.h      11 Feb 2003 19:59:26 -0000      1.2
***************
*** 30,34 ****
  
  class item_base;
! // class inventory;
  
  /**
--- 30,34 ----
  
  class item_base;
! class inventory;
  
  /**
***************
*** 46,50 ****
       * @param id The slot's id
       */
!      slot (/*inventory *owner,*/ const string & id = "");
  
      /**
--- 46,50 ----
       * @param id The slot's id
       */
!      slot (inventory *owner, const string & id = "");
  
      /**
***************
*** 89,95 ****
       * @param item the item to be removed from the slot.
       * @param count number of items to be removed.
!      * @return \b true if removal successful, \b false otherwise.
       */
!     bool remove (item_base * item, const u_int32 & count = 1);
      
      /**
--- 89,95 ----
       * @param item the item to be removed from the slot.
       * @param count number of items to be removed.
!      * @return number of items that could not be removed.
       */
!     u_int32 remove (item_base * item, const u_int32 & count = 1);
      
      /**
***************
*** 97,101 ****
       * or not.
       */
!     // void empty ();
      
      /**
--- 97,101 ----
       * or not.
       */
!     void clear ();
      
      /**
***************
*** 103,111 ****
       * @return number of items this %slot contains.
       */
!     u_int32 count ()
      {
          return Count;
      }
      //@}
      
      /**
--- 103,134 ----
       * @return number of items this %slot contains.
       */
!     u_int32 count () const
      {
          return Count;
      }
      //@}
+ 
+     /**
+      * Attribute access
+      */
+     //@{
+     /**
+      * Retrieve the slot's id.
+      * @return id of the %slot.
+      */
+     const string & id () const
+     {
+         return Id;
+     }
+     
+     /**
+      * Give this %slot a new Id.
+      * @param id A new identifier for this %slot. 
+      */
+     void set_id (const string & id)
+     {
+         Id = id;
+     }
+     //@}
      
      /**
***************
*** 115,123 ****
      /**
       * Load %slot and its contents from stream. 
-      *
       * @param file stream to load %slot from.
       * @return \b true if loading successful, \b false otherwise.
       */
!     // bool get_state (igzstream & file);
      
      /**
--- 138,145 ----
      /**
       * Load %slot and its contents from stream. 
       * @param file stream to load %slot from.
       * @return \b true if loading successful, \b false otherwise.
       */
!     bool get_state (igzstream & file);
      
      /**
***************
*** 126,130 ****
       * @return \b true if saving successful, \b false otherwise.
       */
!     // bool put_state (ogzstream & file) const;
      //@}
      
--- 148,152 ----
       * @return \b true if saving successful, \b false otherwise.
       */
!     bool put_state (ogzstream & file) const;
      //@}
      
***************
*** 140,144 ****
       * The %inventory this %slot belongs to.
       */
!     // inventory *Owner;
      
      /**
--- 162,166 ----
       * The %inventory this %slot belongs to.
       */
!     inventory *Owner;
      
      /**





reply via email to

[Prev in Thread] Current Thread [Next in Thread]