adonthell-commits
[Top][All Lists]
Advanced

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

[Adonthell-commits] CVS: adonthell/src fileops.cc,1.9,1.10 fileops.h,1.


From: Kai Sterker <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src fileops.cc,1.9,1.10 fileops.h,1.14,1.15 item_base.cc,1.3,1.4 item_base.h,1.2,1.3 label_input.cc,1.11,1.12 py_object.cc,1.14,1.15 py_object.h,1.10,1.11
Date: Sat, 18 Jan 2003 18:23:01 -0500

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

Modified Files:
        fileops.cc fileops.h item_base.cc item_base.h label_input.cc 
        py_object.cc py_object.h 
Log Message:
CHANGED item_base class to a mere wrapper around item.py
IMPROVED fileops and py_object code a little


Index: fileops.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/fileops.cc,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -r1.9 -r1.10
*** fileops.cc  26 May 2002 09:27:27 -0000      1.9
--- fileops.cc  18 Jan 2003 23:22:59 -0000      1.10
***************
*** 2,6 ****
     $Id$
  
!    Copyright (C) 1999/2000/2001/2002 Alexandre Courbot
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    Copyright (C) 1999/2000/2001/2002/2003 Alexandre Courbot
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 37,42 ****
  }
  
! gz_file::gz_file (string fname, gz_type t)
  {
      open (fname, t);
  }
--- 37,43 ----
  }
  
! gz_file::gz_file (const string & fname, gz_type t)
  {
+     opened = false;
      open (fname, t);
  }
***************
*** 47,51 ****
  }
  
! bool gz_file::open (string fname, gz_type t)
  {
      if (t == READ) file = gzopen (fname.c_str (),"rb6");
--- 48,52 ----
  }
  
! bool gz_file::open (const string & fname, gz_type t)
  {
      if (t == READ) file = gzopen (fname.c_str (),"rb6");
***************
*** 66,70 ****
  }
  
! igzstream::igzstream (string fname) : gz_file (fname, READ)
  {
  }
--- 67,71 ----
  }
  
! igzstream::igzstream (const string & fname) : gz_file (fname, READ)
  {
  }
***************
*** 74,78 ****
  }
  
! bool igzstream::open (string fname)
  {
      return gz_file::open (fname, READ);
--- 75,79 ----
  }
  
! bool igzstream::open (const string & fname)
  {
      return gz_file::open (fname, READ);
***************
*** 165,169 ****
  }
  
! ogzstream::ogzstream (string fname) : gz_file (fname, WRITE)
  {
  }
--- 166,170 ----
  }
  
! ogzstream::ogzstream (const string & fname) : gz_file (fname, WRITE)
  {
  }
***************
*** 173,177 ****
  }
  
! bool ogzstream::open (string fname)
  {
      return gz_file::open (fname, WRITE);
--- 174,178 ----
  }
  
! bool ogzstream::open (const string & fname)
  {
      return gz_file::open (fname, WRITE);

Index: fileops.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/fileops.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -r1.14 -r1.15
*** fileops.h   24 Jul 2001 20:12:43 -0000      1.14
--- fileops.h   18 Jan 2003 23:22:59 -0000      1.15
***************
*** 2,6 ****
     $Id$
  
!    Copyright (C) 2001 Alexandre Courbot
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    Copyright (C) 2001/2003 Alexandre Courbot
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
***************
*** 66,70 ****
       * @param t access (READ or WRITE).
       */
!     gz_file (string fname, gz_type t);
  #endif
  
--- 66,70 ----
       * @param t access (READ or WRITE).
       */
!     gz_file (const string & fname, gz_type t);
  #endif
  
***************
*** 73,77 ****
       * 
       */
!     ~gz_file ();
      
      /** 
--- 73,77 ----
       * 
       */
!     virtual ~gz_file ();
      
      /** 
***************
*** 83,87 ****
       * @return true if succeed, false otherwise.
       */
!     bool open (string fname, gz_type t);
  
      /** 
--- 83,87 ----
       * @return true if succeed, false otherwise.
       */
!     bool open (const string & fname, gz_type t);
  
      /** 
***************
*** 145,149 ****
       * 
       */ 
!     igzstream (string fname);
  #endif
      
--- 145,149 ----
       * 
       */ 
!     igzstream (const string & fname);
  #endif
      
***************
*** 161,165 ****
       * @return true if succeed, false otherwise.
       */
!     bool open (string fname);
  
      /** 
--- 161,165 ----
       * @return true if succeed, false otherwise.
       */
!     bool open (const string & fname);
  
      /** 
***************
*** 224,228 ****
       * 
       */ 
!     ogzstream (string fname);
  #endif
      
--- 224,228 ----
       * 
       */ 
!     ogzstream (const string & fname);
  #endif
      
***************
*** 240,244 ****
       * @return true if succeed, false otherwise.
       */
!     bool open (string fname);
  
      /** 
--- 240,244 ----
       * @return true if succeed, false otherwise.
       */
!     bool open (const string & fname);
  
      /** 

Index: item_base.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_base.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** item_base.cc        17 Jan 2003 21:53:39 -0000      1.3
--- item_base.cc        18 Jan 2003 23:22:59 -0000      1.4
***************
*** 21,24 ****
--- 21,25 ----
   */
  
+ #include <stdio.h>
  #include <iostream>
  #include <algorithm>
***************
*** 26,34 ****
  
  // ctor
! item_base::item_base (const std::string & item)
  {
!     std::cout << "Item instance of '" << item << "' created" << endl;
!     
!     create_instance (item, item);
  }
  
--- 27,35 ----
  
  // ctor
! item_base::item_base (const std::string & item) : py_object ()
  {
!     std::cout << "Item instance of '" << item << "' created" << std::endl;
! 
!     get_state (item);    
  }
  
***************
*** 41,62 ****
  }
  
- // init item
- void item_base::init (PyObject *item)
- {
-     std::cout << "Item instance initialized" << endl;
-     
-     // set derived item instance
-     Py_XDECREF (Instance);
-     Instance = item;
-     
-     // init basic attributes
-     Charge = 0;
-     MaxCharge = 0;
- }
- 
  // trigger item's main functionality
  bool item_base::use (character_base *character)
  {
!     std::cout << "Item instance used" << endl;
      
      // can't be used
--- 42,49 ----
  }
  
  // trigger item's main functionality
  bool item_base::use (character_base *character)
  {
!     std::cout << "Item '" << name () << "' used" << std::endl;
      
      // can't be used
***************
*** 82,128 ****
  }        
  
! // make the item member of the given category.
! void item_base::add_type (const std::string &type)
  {
!     // only add if item does not already belong to that category
!     if (find (Types.begin (), Types.end (), type) != Types.end ())
!         Types.push_back (type);
  }
  
! // remove the given category from the item.
! void item_base::remove_type (const std::string &type)
  {
!     Types.erase (remove (Types.begin (), Types.end (), type), Types.end ());
  }
  
! // save state
! void item_base::put_state (ogzstream& file) const
  {
!     /** Move to inventory for efficiency! Means we need to pass_instance
!         'file' only once and can use if for all items of the inventory.
!      
!     // save derived item class' state
!     if (has_attribute ("put_state"))
      {
!         // pass file
!         PyObject *args = PyTuple_New (1);
!         PyTuple_SetItem (args, 0, python::pass_instance (file, "ogzstream"));
!     
!         // call method
!         call_method ("put_state", args);
!         
!         // cleanup
!         Py_XDECREF (args); 
      }
      
!     **/
  }
  
! // load state
! bool item_base::get_state (igzstream& file)
  {
!     return true;
  }
  
  // recharge item
  u_int16 item_base::recharge (u_int16 &charge)
--- 69,149 ----
  }        
  
! // save a single item to file
! bool item_base::put_state (const std::string & file) const
  {
!     ogzstream out (file);
!     
!     // has file been opened?
!     if (!out.is_open ())
!     {
!         fprintf (stderr, "*** item_base::save: cannot write '%s'\n", 
file.c_str ());
!         return false;
!     }
!     
!     return put_state (out);
  }
  
! // save item to stream
! bool item_base::put_state (ogzstream & file) const
  {
!     // do we have a valid item?
!     if (!Instance) return false;
!     
!     // save the template this item uses
!     class_name () >> file;
!     
!     // pass file
!     PyObject *args = PyTuple_New (1);
!     PyTuple_SetItem (args, 0, python::pass_instance (&file, "ogzstream"));
!     
!     // save the actual item data
!     call_method ("put_state", args);
!     Py_DECREF (args);    
!     
!     return true;
  }
  
! // load a single item from file
! bool item_base::get_state (const std::string & file)
  {
!     igzstream in (file);
!     
!     // has file been opened?
!     if (!in.is_open ()) 
      {
!         fprintf (stderr, "*** item_base::load: cannot read '%s'\n", 
file.c_str ());
!         return false;
      }
      
!     // load item
!     return get_state (in);
  }
  
! // load item from stream
! bool item_base::get_state (igzstream & file)
  {
!     std::string tmpl;
!     
!     // clean up, if neccessary
!     if (Instance) clear ();
!     
!     // get template to use for item
!     tmpl << file;
!     
!     // instanciate
!     if (!create_instance (tmpl, tmpl)) return false;
! 
!     // pass file
!     PyObject *args = PyTuple_New (1);
!     PyTuple_SetItem (args, 0, python::pass_instance (&file, "igzstream"));
!     
!     // load actual item data
!     call_method ("get_state", args);
!     Py_DECREF (args);
!     
!     return true;  
  }
  
+ /*
  // recharge item
  u_int16 item_base::recharge (u_int16 &charge)
***************
*** 138,140 ****
      
      return charge;
! }
\ No newline at end of file
--- 159,162 ----
      
      return charge;
! }
! */

Index: item_base.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_base.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** item_base.h 16 Jan 2003 11:22:45 -0000      1.2
--- item_base.h 18 Jan 2003 23:22:59 -0000      1.3
***************
*** 32,36 ****
--- 32,45 ----
  
  /**
+  * It is a wrapper for item_base.py, which is the actual item superclass.
+  * For flexibility, items are implemented on python side. But since they
+  * are often used on C++ side, this class provides methods to the most
+  * basic item attributes and all possible actions an item might support.
   *
+  * So while each item does have the wrapped attributes, an item may only
+  * implement a subset of the available actions ("pick_up", "drop", "equip",
+  * "unequip", "combine" and "use"). Further, an item may have additional
+  * attributes. If required, they can be accessed via methods provided by
+  * the py_object class.
   */
  class item_base : public py_object 
***************
*** 47,51 ****
       * @param item Name of the item data file defining the desired item.
       */
!     item_base (const string &item);
      
      /**
--- 56,60 ----
       * @param item Name of the item data file defining the desired item.
       */
!     item_base (const string & item);
      
      /**
***************
*** 55,64 ****
      
      /**
-      * Initialize the basic item attributes from python side.
-      * @param item Derived item instance.
-      */
-     void init (PyObject *item);
-     
-     /**
       * @name Item Actions
       */
--- 64,67 ----
***************
*** 77,118 ****
      //@{
      /**
       * Make the item member of the given category.
!      * @param type the category this item should belong to.
       */
!     void add_type (const string &type);
      
      /**
       * Take a category away from the item.
!      * @param type category to remove from the item. 
       */
!     void remove_type (const string &type);
! 
      /**
       * Get the charge the item has left.
       * @return number of charges the item has.
       */
!     u_int16 charge ()                       { return Charge; }
  
      /**
!      * Add charges to the item. Recharges the item up to MaxCharge.
!      * @param charge number of charges to add.
!      * @return number of charges not added to the item
!      */
!     u_int16 recharge (u_int16 & charge);
! 
      /**
       * Get the maximum number of charges the item may hold.
       * @return item's maximum number of charges.
       */
!     u_int16 max_charge ()                   { return MaxCharge; }
  
      /**
       * Set the maximum number of charges the item may hold.
!      * @param item's maximum number of charges.
       */
!     void set_max_charge (u_int max_charge)  { MaxCharge = max_charge; }
      //@}
      
- protected:
      /**
       * @name Loading/Saving
--- 80,174 ----
      //@{
      /**
+      * Get the item's name.
+      * @return name of the item.
+      */
+     string name ()
+     {
+         return get_attribute_string ("Name");
+     }
+ 
+     /**
+      * Set the item's name.
+      * @param name the item's new name.
+      */
+     void set_name (const string & name)  
+     {
+         set_attribute_string ("Name", name); 
+     }
+     
+     /**
       * Make the item member of the given category.
!      * @param category the category this item should belong to.
       */
!     void add_category (const string & category)
!     {
!         PyObject *args = Py_BuildValue ("(s)", (char *) category.c_str ());
!         call_method ("add_category", args);
!     }
      
      /**
       * Take a category away from the item.
!      * @param category the category to remove from the item. 
       */
!     void remove_category (const string & category)
!     {
!         PyObject *args = Py_BuildValue ("(s)", (char *) category.c_str ());
!         call_method ("remove_category", args);
!     }
! 
!     /**
!      * Check whether item belongs to a certain category.
!      * @param category the category to test against.
!      * @return \b true if item belongs to given category, \b false otherwise.
!      */
!     bool is_a (const string & category)
!     {
!         int result = 0;
!         PyObject *args = Py_BuildValue ("(s)", (char *) category.c_str ());
!         PyObject *retval = call_method_ret ("is_a", args);
!         
!         if (retval && PyInt_Check (retval)) result = PyInt_AS_LONG (retval);
!         Py_XDECREF (retval);
!         
!         return (result != 0);
!     }
!     
      /**
       * Get the charge the item has left.
       * @return number of charges the item has.
       */
!     u_int16 charge ()
!     {
!         return (u_int16) get_attribute_int ("Charge");
!     }
  
      /**
!      * Set the number of charges the item may hold.
!      * @param charge item's number of charges.
!      */
!     void set_charge (u_int16 charge)  
!     {
!         set_attribute_int ("Charge", charge); 
!     }
!     
      /**
       * Get the maximum number of charges the item may hold.
       * @return item's maximum number of charges.
       */
!     u_int16 max_charge ()
!     {
!         return (u_int16) get_attribute_int ("MaxCharge");
!     }
  
      /**
       * Set the maximum number of charges the item may hold.
!      * @param max_charge item's maximum number of charges.
       */
!     void set_max_charge (u_int16 max_charge)
!     { 
!         set_attribute_int ("MaxCharge", max_charge); 
!     }
      //@}
      
      /**
       * @name Loading/Saving
***************
*** 120,161 ****
      //@{
      /**
!      * Load item state from file. This method has to be called from the
!      * derived class before loading itself. Calling it from C++ side will 
!      * fail to load the complete item.
       *
!      * @param file stream to load item from
!      * @return \b true if loading successful, \b false otherwise
       */
!     bool get_state (igzstream& file);
      
      /**
!      * Save item state to file. This method has to be called from the
!      * derived class before saving itself. Calling it from C++ side will 
!      * fail to save the complete item.
       *
!      * @param file stream to save item to
       */
!     void put_state (ogzstream &file) const;
!     //@}
! 
! private:
!     /**
!      * @name Basic Item Attributes
!      */
!     //@{
!     /**
!      * List of categories this item belongs to.
!      */
!     std::vector<std::string> Types;
      
      /**
!      * Fuel or number of uses an item has.
       */
!     u_int16 Charge;
! 
      /**
!      * Maximum amount of charge the item may have.
       */
!     u_int16 MaxCharge;
      //@}
  };
--- 176,219 ----
      //@{
      /**
!      * Load item from named file. This will first load the item template
!      * to instanciate the underlying Python item class. Then it will
!      * restore the actual item data. If an item is already instanciated,
!      * it will be replaced.
       *
!      * @param file name of the file to load item from.
!      * @return \b true if loading successful, \b false otherwise.
       */
!     bool get_state (const string & file);
      
      /**
!      * Save item to named file. This will save both the item template
!      * plus the actual data to the given file. The file will be replaced
!      * if it already exists.
       *
!      * @param file name of the file to save item to.
!      * @return \b true if saving successful, \b false otherwise.
       */
!     bool put_state (const string & file) const;
      
      /**
!      * Load item from stream. This will first load the item template
!      * to instanciate the underlying Python item class. Then it will
!      * restore the actual item data. If an item is already instanciated,
!      * it will be replaced.
!      *
!      * @param file stream to load item from.
!      * @return \b true if loading successful, \b false otherwise.
       */
!     bool get_state (igzstream & file);
!     
      /**
!      * Save item to stream. This will save both the item template
!      * plus the actual data to the given file. The file will be replaced
!      * if it already exists.
!      *
!      * @param file stream to save item to.
!      * @return \b true if saving successful, \b false otherwise.
       */
!     bool put_state (ogzstream & file) const;
      //@}
  };

Index: label_input.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/label_input.cc,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -r1.11 -r1.12
*** label_input.cc      17 Jan 2003 21:33:35 -0000      1.11
--- label_input.cc      18 Jan 2003 23:22:59 -0000      1.12
***************
*** 2,6 ****
     $Id$
  
!    (C) Copyright 2000/2001 Joel Vennin
     Part of the Adonthell Project http://adonthell.linuxgames.com
  
--- 2,6 ----
     $Id$
  
!    (C) Copyright 2000/2001/2003 Joel Vennin
     Part of the Adonthell Project http://adonthell.linuxgames.com
  

Index: py_object.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_object.cc,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -r1.14 -r1.15
*** py_object.cc        16 Jan 2003 11:22:45 -0000      1.14
--- py_object.cc        18 Jan 2003 23:22:59 -0000      1.15
***************
*** 106,110 ****
  
  // Execute a method of the script
! PyObject* py_object::call_method_ret (const string & name, PyObject * args)
  {
      PyObject *result = NULL;
--- 106,110 ----
  
  // Execute a method of the script
! PyObject* py_object::call_method_ret (const string &name, PyObject *args) 
const
  {
      PyObject *result = NULL;

Index: py_object.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_object.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -r1.10 -r1.11
*** py_object.h 16 Jan 2003 11:22:45 -0000      1.10
--- py_object.h 18 Jan 2003 23:22:59 -0000      1.11
***************
*** 102,106 ****
       *     Py_DECREF'd when no longer needed.
       */
!     PyObject *call_method_ret (const string & name, PyObject * args = NULL);
       
      /** 
--- 102,106 ----
       *     Py_DECREF'd when no longer needed.
       */
!     PyObject *call_method_ret (const string &name, PyObject *args = NULL) 
const;
       
      /** 
***************
*** 110,114 ****
       * @param args Python tuple containing the arguments to pass to the 
method.
       */
!     void call_method (const string & name, PyObject * args = NULL)
      {
          PyObject *result = call_method_ret (name, args);
--- 110,114 ----
       * @param args Python tuple containing the arguments to pass to the 
method.
       */
!     void call_method (const string & name, PyObject * args = NULL) const
      {
          PyObject *result = call_method_ret (name, args);





reply via email to

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