adonthell-commits
[Top][All Lists]
Advanced

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

[Adonthell-commits] CVS: adonthell/src schedule.cc,NONE,1.1.2.1 schedule


From: Kai Sterker <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src schedule.cc,NONE,1.1.2.1 schedule.h,NONE,1.1.2.1 Makefile.am,1.80.2.26,1.80.2.27 event.cc,1.12.2.2,1.12.2.3 event.h,1.25.2.1,1.25.2.2 event_handler.cc,1.1.2.2,1.1.2.3 event_list.cc,1.1.2.1,1.1.2.2 event_list.h,1.1.2.1,1.1.2.2 gamedate.h,1.1.2.1,1.1.2.2 py_base.i,1.1.2.1,1.1.2.2 py_object.cc,1.10,1.10.2.1 py_object.h,1.6.2.1,1.6.2.2 python_class.cc,1.5.2.3,1.5.2.4 python_class.h,1.6.2.1,1.6.2.2 time_event.cc,1.1.2.2,1.1.2.3 time_event.h,1.1.2.3,1.1.2.4 time_event_handler.cc,1.1.2.1,1.1.2.2 time_event_handler.h,1.1.2.1,1.1.2.2
Date: Sat, 08 Jun 2002 17:15:38 -0400

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

Modified Files:
      Tag: Branch_road_to_0-4
        Makefile.am event.cc event.h event_handler.cc event_list.cc 
        event_list.h gamedate.h py_base.i py_object.cc py_object.h 
        python_class.cc python_class.h time_event.cc time_event.h 
        time_event_handler.cc time_event_handler.h 
Added Files:
      Tag: Branch_road_to_0-4
        schedule.cc schedule.h 
Log Message:
ADDED schedule.cc/.h - first go at advanced character schedules
ADDED state saving/loading to py_object
FIXES to event stuff

NOTE: nothing is tested yet, so there are certainly some bugs left


***** Error reading new file: [Errno 2] No such file or directory: 'schedule.cc'
***** Error reading new file: [Errno 2] No such file or directory: 'schedule.h'
Index: Makefile.am
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Makefile.am,v
retrieving revision 1.80.2.26
retrieving revision 1.80.2.27
diff -C2 -r1.80.2.26 -r1.80.2.27
*** Makefile.am 5 May 2002 19:50:14 -0000       1.80.2.26
--- Makefile.am 8 Jun 2002 21:15:35 -0000       1.80.2.27
***************
*** 21,33 ****
  libbase_a_SOURCES = \
          character_base.cc \
          fileops.cc \
        game.cc \
          gamedate.cc \
          gametime.cc \
          quest.cc \
          storage.cc \
!       py_base_wrap.cc \
          callback.h \
          character_base.h \
        fileops.h \
        game.h \
--- 21,43 ----
  libbase_a_SOURCES = \
          character_base.cc \
+         event.cc \
+         event_handler.cc \
+         event_list.cc \
          fileops.cc \
        game.cc \
          gamedate.cc \
          gametime.cc \
+         py_base_wrap.cc \
          quest.cc \
+         schedule.cc \
          storage.cc \
!         time_event.cc \
!         time_event_handler.cc \
!         yarg.cc \
          callback.h \
          character_base.h \
+         event.h \
+         event_handler.h \
+         event_list.h \
        fileops.h \
        game.h \
***************
*** 35,41 ****
          gametime.h \
          quest.h \
          storage.h \
        str_hash.h \
!         types.h 
  
  libbase_LDADD = $(SDL_LIBS)
--- 45,55 ----
          gametime.h \
          quest.h \
+         schedule.h \
          storage.h \
        str_hash.h \
!         time_event.h \
!         time_event_handler.h \
!         types.h \
!         yarg.h
  
  libbase_LDADD = $(SDL_LIBS)
***************
*** 94,98 ****
  
  libadonthell_a_SOURCES = input.cc \
-       yarg.cc \
        input.h 
  
--- 108,111 ----
***************
*** 107,111 ****
  alextest_LDADD = libmap.a libgfx.a libinput.a libbase.a $(libbase_LDADD)
  
! pytest_LDADD = libpython.a libgfx.a libinput.a libbase.a $(libbase_LDADD) 
$(libpython_LDADD)
  
  # Note: .py files are also built by this target.
--- 120,124 ----
  alextest_LDADD = libmap.a libgfx.a libinput.a libbase.a $(libbase_LDADD)
  
! pytest_LDADD = libbase.a libpython.a libgfx.a libinput.a $(libbase_LDADD) 
$(libpython_LDADD)
  
  # Note: .py files are also built by this target.

Index: event.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event.cc,v
retrieving revision 1.12.2.2
retrieving revision 1.12.2.3
diff -C2 -r1.12.2.2 -r1.12.2.3
*** event.cc    7 Jun 2002 07:41:23 -0000       1.12.2.2
--- event.cc    8 Jun 2002 21:15:35 -0000       1.12.2.3
***************
*** 27,30 ****
--- 27,39 ----
  event::~event ()
  {
+     clear ();
+ }
+ 
+ // cleanup
+ void event::clear ()
+ {
+     Script.clear ();
+     Py_XDECREF (Args);
+     Args = NULL;
  }
  
***************
*** 32,46 ****
  void event::set_script (string filename, PyObject * args = NULL)
  {
!     if (filename == "") 
!     {
!         Script.clear ();
!         Py_XDECREF (Args);
!         Args = NULL; 
!     }
      else 
      {
          Py_XINCREF (args);
          Args = args; 
!         
          u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1; 
          PyObject *theargs = PyTuple_New (argssize);
--- 41,51 ----
  void event::set_script (string filename, PyObject * args = NULL)
  {
!     if (filename == "") clear ();
      else 
      {
          Py_XINCREF (args);
          Args = args; 
!         Shared = false;
!        
          u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1; 
          PyObject *theargs = PyTuple_New (argssize);
***************
*** 60,63 ****
--- 65,81 ----
  }
  
+ // make the event script a reference to an existing script
+ void event::set_shared_script (py_object &script)
+ {
+     // cleanup
+     clear ();
+ 
+     // 'clone' the given script
+     Script = script;
+     
+     // tell the script not to save any arguments
+     Shared = true;
+ }
+ 
  // save the state of the script associated with the event
  void event::put_state (ogzstream & file) const
***************
*** 65,93 ****
      Type >> file;
      Repeat >> file;
!     Script.object_file () >> file;
!     if (Args) 
      {
!         true >> file; 
          python::put_tuple (Args, file);
      }
!     else false >> file; 
  }
  
  // load the state of the script associated with the event 
! void event::get_state (igzstream & file) 
  {
      string name;
      bool has_args;
!     PyObject * args = NULL; 
  
      // Note that »Type« is already read by event_list::load to
      // determine what event subclass to instanciate
      Repeat << file;
      name << file;
!     has_args << file; 
      
      if (has_args) args = python::get_tuple (file);
      
!     set_script (name, args);      
!     Py_XDECREF (args); 
  }
--- 83,123 ----
      Type >> file;
      Repeat >> file;
!     Shared >> file;
!     
!     if (Shared) return;
!     
!     Script.class_name () >> file;
!     
!     if (Args)
      {
!         true >> file;
          python::put_tuple (Args, file);
      }
!     else false >> file;
  }
  
  // load the state of the script associated with the event 
! bool event::get_state (igzstream & file) 
  {
      string name;
      bool has_args;
!     PyObject * args = NULL;
  
      // Note that »Type« is already read by event_list::load to
      // determine what event subclass to instanciate
      Repeat << file;
+     Shared << file;
+     
+     // shared scripts have to be restored by the event's owner
+     if (Shared) return true;
+     
      name << file;
!     has_args << file;
      
      if (has_args) args = python::get_tuple (file);
      
!     set_script (name, args);
!     Py_XDECREF (args);
!     
!     return true;
  }

Index: event.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event.h,v
retrieving revision 1.25.2.1
retrieving revision 1.25.2.2
diff -C2 -r1.25.2.1 -r1.25.2.2
*** event.h     12 May 2002 18:36:07 -0000      1.25.2.1
--- event.h     8 Jun 2002 21:15:35 -0000       1.25.2.2
***************
*** 30,37 ****
   * Directory where event scripts reside.
   */ 
! #define EVENTS_DIR "events/"
! 
! 
! class event_handler;
  
  /**
--- 30,34 ----
   * Directory where event scripts reside.
   */ 
! #define EVENTS_DIR "events."
  
  /**
***************
*** 42,46 ****
      ENTER_EVENT     = 0,            // Characters reach a new tile
      LEAVE_EVENT     = 1,            // Characters leave a tile
!     TIME_EVENT      = 2,            // A minute of gametime passed
      ACTION_EVENT    = 3,            // Character "acts" on a square 
      MAX_EVENT       = 4
--- 39,43 ----
      ENTER_EVENT     = 0,            // Characters reach a new tile
      LEAVE_EVENT     = 1,            // Characters leave a tile
!     TIME_EVENT      = 2,            // Certain point in gametime reached
      ACTION_EVENT    = 3,            // Character "acts" on a square 
      MAX_EVENT       = 4
***************
*** 58,64 ****
       * Destructor.
       */ 
!      virtual ~event ();
  
      /**
       * @name Event Handling
       */
--- 55,89 ----
       * Destructor.
       */ 
!     virtual ~event ();
  
      /**
+      * Cleanup. Clears script and its arguments. 
+      */
+     void clear ();    
+     
+     /**
+      * Get the event's type.
+      *
+      * @return type of the event
+      */
+     u_int8 type () const
+     { 
+         return Type;
+     }
+      
+     /**
+      * Return whether this event should be repeated.
+      *
+      * @return the number of times this event should be repeated or
+      *      -1 in case it should be repeated unlimited times.
+      */
+     s_int32 repeat ()
+     {
+         if (Repeat > 0) Repeat--;
+         
+         return Repeat;
+     }
+     
+     /**
       * @name Event Handling
       */
***************
*** 70,74 ****
       * @param evnt The event that triggered the execution.
       */ 
!     virtual void execute (event& evnt) = 0;
  
      /** 
--- 95,99 ----
       * @param evnt The event that triggered the execution.
       */ 
!     virtual void execute (const event& evnt) = 0;
  
      /** 
***************
*** 78,87 ****
       * @return \e true if the events are equal, \e false otherwise.
       */
!     virtual bool equals (event& evnt) = 0;
  
      //@}
      
      /** 
!      * Sets the script for an event.
       * 
       * @param filename filename of the script to set.
--- 103,112 ----
       * @return \e true if the events are equal, \e false otherwise.
       */
!     virtual bool equals (const event& evnt) = 0;
  
      //@}
      
      /** 
!      * Sets the script to be executed whenever the event occurs.
       * 
       * @param filename filename of the script to set.
***************
*** 90,95 ****
      void set_script (string filename, PyObject * args = NULL);
      
! protected:
! #ifndef SWIG
      /**
       * @name Loading / Saving
--- 115,132 ----
      void set_script (string filename, PyObject * args = NULL);
      
!     /**
!      * Sets the script to be executed whenever the event occurs.
!      * This method allows to specify a script that is already
!      * in use elsewhere.
!      *
!      * @warning After calling this method, both the event and the 
!      * original owner share the same script instance. Therefore, the 
!      * event will <b>not</b> save the script. When loading a game,
!      * the original owner has to supply the event with the script.
!      *
!      * @param script reference to a script initialized elsewhere.
!      */
!     void set_shared_script (py_object & script);
!     
      /**
       * @name Loading / Saving
***************
*** 116,119 ****
--- 153,158 ----
      //@}
      
+ protected:
+ #ifndef SWIG
      /**
       * @name Basic Event Data
***************
*** 126,129 ****
--- 165,175 ----
      u_int8 Type;
  
+     /**
+      * For events that share their script with another class, Shared
+      * has to be set <b>true</b>. This prevents the script from getting
+      * saved to file.
+      */
+     bool Shared;
+     
      /**
       * Defines how often the %event should be repeated. <b>0</b> means

Index: event_handler.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/event_handler.cc,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -C2 -r1.1.2.2 -r1.1.2.3
*** event_handler.cc    7 Jun 2002 07:41:23 -0000       1.1.2.2
--- event_handler.cc    8 Jun 2002 21:15:35 -0000       1.1.2.3
***************
*** 21,25 ****
   * 
   */
! 
  #include "event_handler.h"
  
--- 21,26 ----
   * 
   */
!  
! #include <algorithm>
  #include "event_handler.h"
  
***************
*** 34,38 ****
      vector<event*>::iterator i;
      // Search through all registered events with the type of the raised event
!     for (i = handlers[e.type].begin (); i != handlers[e.type].end (); i++)
          // Execute the script; pass recieved event on to get event data
          if ((*i)->equals (e)) (*i)->execute (e); 
--- 35,39 ----
      vector<event*>::iterator i;
      // Search through all registered events with the type of the raised event
!     for (i = handlers[e.type ()].begin (); i != handlers[e.type ()].end (); 
i++)
          // Execute the script; pass recieved event on to get event data
          if ((*i)->equals (e)) (*i)->execute (e); 
***************
*** 46,54 ****
  
      // Search for the event we want to remove
!     i = find (handlers[e->type].begin (), handlers[e->type].end (), e);
  
      // found? -> get rid of it :)
!     if (i != handlers[e->type].end ()) handlers[e->type].erase(i);
! 
  }
  
--- 47,54 ----
  
      // Search for the event we want to remove
!     i = find (handlers[e->type ()].begin (), handlers[e->type ()].end (), e);
  
      // found? -> get rid of it :)
!     if (i != handlers[e->type ()].end ()) handlers[e->type ()].erase(i);
  }
  
***************
*** 56,59 ****
  void event_handler::register_event (event *e)
  {
!     handlers[e->type].push_back (e);
  }
--- 56,59 ----
  void event_handler::register_event (event *e)
  {
!     handlers[e->type ()].push_back (e);
  }

Index: event_list.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/event_list.cc,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** event_list.cc       12 May 2002 18:36:07 -0000      1.1.2.1
--- event_list.cc       8 Jun 2002 21:15:35 -0000       1.1.2.2
***************
*** 21,25 ****
   * 
   */
!  
  #include "event_handler.h"
   
--- 21,26 ----
   * 
   */
! 
! #include "event_list.h"
  #include "event_handler.h"
   
***************
*** 70,74 ****
  
      for (i = events.begin (); i != events.end (); i++)
!         (*i)->save (out); 
  }
  
--- 71,75 ----
  
      for (i = events.begin (); i != events.end (); i++)
!         (*i)->put_state (out); 
  }
  
***************
*** 91,95 ****
   
          // try to load it, ...
!         if (e != NULL && e->load (in))
              add_event (e);
          
--- 92,96 ----
   
          // try to load it, ...
!         if (e != NULL && e->get_state (in))
              add_event (e);
          

Index: event_list.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/event_list.h,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** event_list.h        12 May 2002 18:36:07 -0000      1.1.2.1
--- event_list.h        8 Jun 2002 21:15:35 -0000       1.1.2.2
***************
*** 26,29 ****
--- 26,30 ----
  #define EVENT_LIST_H__
  
+ #include <vector>
  #include "event.h"
  

Index: gamedate.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/gamedate.h,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** gamedate.h  5 May 2002 19:51:07 -0000       1.1.2.1
--- gamedate.h  8 Jun 2002 21:15:35 -0000       1.1.2.2
***************
*** 55,58 ****
--- 55,64 ----
  
      /**
+      * Get the current %gametime.
+      * @return %gametime in minutes since start of the game.
+      */
+     static u_int32 time ()      { return Time; }
+ 
+     /**
       * Get the current weekday.
       * @return weekday as a number between 0 and DAYS_PER_WEEK - 1

Index: py_base.i
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/py_base.i,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** py_base.i   11 Apr 2002 14:08:42 -0000      1.1.2.1
--- py_base.i   8 Jun 2002 21:15:35 -0000       1.1.2.2
***************
*** 7,10 ****
--- 7,11 ----
  #include "fileops.h"
  #include "gametime.h"
+ #include "schedule.h"
  
  %}
***************
*** 15,16 ****
--- 16,18 ----
  %include "fileops.h"
  %include "gametime.h"
+ %include "schedule.h"

Index: py_object.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_object.cc,v
retrieving revision 1.10
retrieving revision 1.10.2.1
diff -C2 -r1.10 -r1.10.2.1
*** py_object.cc        7 Jan 2002 13:41:38 -0000       1.10
--- py_object.cc        8 Jun 2002 21:15:35 -0000       1.10.2.1
***************
*** 2,6 ****
    $Id$
    
!   Copyright (C) 1999/2000/2001   Kai Sterker
    Copyright (C) 2001    Alexandre Courbot
    Part of the Adonthell Project http://adonthell.linuxgames.com
--- 2,6 ----
    $Id$
    
!   Copyright (C) 1999/2000/2001/2002 Kai Sterker
    Copyright (C) 2001    Alexandre Courbot
    Part of the Adonthell Project http://adonthell.linuxgames.com
***************
*** 29,34 ****
  py_object::py_object ()
  {
!     instance = NULL;
!     script_file_ = "";
  }
  
--- 29,36 ----
  py_object::py_object ()
  {
!     Instance = NULL;
!     CtorArgs = NULL;
!     Classname = "";
!     Filename = "";
  }
  
***************
*** 42,53 ****
  {
      // Delete our instance
!     Py_XDECREF (instance);
!     instance = NULL;
      
!     script_file_ = "";
  }
  
  // Pass a (new) Python module to be used
! bool py_object::create_instance (string file, string classname, PyObject * 
args = NULL)
  {
      // Try to import the given script
--- 44,59 ----
  {
      // Delete our instance
!     Py_XDECREF (Instance);
!     Py_XDECREF (CtorArgs);
      
!     Instance = NULL;
!     CtorArgs = NULL;
!     
!     Classname = "";
!     Filename = "";
  }
  
  // Pass a (new) Python module to be used
! bool py_object::create_instance (string file, string classname, PyObject 
*args)
  {
      // Try to import the given script
***************
*** 60,64 ****
  
  // Reload a python module in case it has changed on disk
! bool py_object::reload_instance (string file, string classname, PyObject * 
args = NULL)
  {
      // Try to import the given script
--- 66,70 ----
  
  // Reload a python module in case it has changed on disk
! bool py_object::reload_instance (string file, string classname, PyObject 
*args)
  {
      // Try to import the given script
***************
*** 75,79 ****
  
  // Instanciate the given class from the module
! bool py_object::instanciate (PyObject *module, string file, string classname, 
PyObject * args)
  {
      // Cleanup
--- 81,85 ----
  
  // Instanciate the given class from the module
! bool py_object::instanciate (PyObject *module, string file, string classname, 
PyObject *args)
  {
      // Cleanup
***************
*** 85,94 ****
  
      // Create the instance
!     instance = PyObject_CallObject (classobj, args);
      Py_DECREF (classobj);
!     if (!instance) return false;
! 
!     script_file_ = classname;
  
      return true;
  }
--- 91,103 ----
  
      // Create the instance
!     Instance = PyObject_CallObject (classobj, args);
      Py_DECREF (classobj);
!     if (!Instance) return false;
  
+     Classname = classname;
+     Filename = file;
+     Py_XINCREF (args);
+     CtorArgs = args;
+     
      return true;
  }
***************
*** 97,103 ****
  void py_object::call_method (const string & name, PyObject * args = NULL)
  {
!     if (instance)
      {
!         PyObject *tocall = PyObject_GetAttrString (instance, (char *) 
name.c_str ());
  
          if (PyCallable_Check (tocall) == 1)
--- 106,112 ----
  void py_object::call_method (const string & name, PyObject * args = NULL)
  {
!     if (Instance)
      {
!         PyObject *tocall = PyObject_GetAttrString (Instance, (char *) 
name.c_str ());
  
          if (PyCallable_Check (tocall) == 1)
***************
*** 116,121 ****
  PyObject *py_object::get_attribute (const string &name)
  {
!     if (!instance) return NULL;
  
!     return PyObject_GetAttrString (instance, (char *) name.c_str ());
  }
--- 125,192 ----
  PyObject *py_object::get_attribute (const string &name)
  {
!     if (Instance) 
!         return PyObject_GetAttrString (Instance, (char *) name.c_str ());
!     else
!         return NULL;
! }
! 
! // Save internal state of the script to disk
! void py_object::put_state (ogzstream &file)
! {
!     Filename >> file;
!     Classname >> file;
!     python::put_tuple (CtorArgs, file);
!     python::put_dict (PyObject_GetAttrString (Instance, "__dict__"), file);
! 
! }
! 
! // Get internal script state from disk
! bool py_object::get_state (igzstream &file)
! {
!     PyObject *state;
! 
!     // clear the current state    
!     clear ();
!     
!     // get file- and classname, constructor arguments and script state
!     Filename << file;
!     Classname << file;
!     CtorArgs = python::get_tuple (file);
!     state = python::get_dict (file);
!     
!     // instanciate the script and restore script state
!     if (create_instance (Filename, Classname, CtorArgs))
!     {
!         PyObject *dict, *key, *value;
!         s_int32 pos = 0;
!         
!         // we'll have to merge the state with the object's current
!         // __dict__, as only part of it has been saved
!         dict = PyObject_GetAttrString (Instance, "__dict__");
!         
!         while (PyDict_Next (state, &pos, &key, &value))
!             PyDict_SetItem (dict, key, value);
!         
!         Py_DECREF (state);
!         return true;
!     }
!     
!     return false;
! }
  
! // 'clone' a py_object
! void py_object::operator= (const py_object & script)
! {
!     // cleanup
!     clear ();
!     
!     // copy
!     Classname = script.class_name ();
!     Filename = script.file_name ();
!     CtorArgs = script.get_ctorargs ();
!     Instance = script.get_instance ();
!     
!     // make sure the refcount is correct
!     Py_XINCREF (CtorArgs);
!     Py_XINCREF (Instance);
  }

Index: py_object.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_object.h,v
retrieving revision 1.6.2.1
retrieving revision 1.6.2.2
diff -C2 -r1.6.2.1 -r1.6.2.2
*** py_object.h 11 Apr 2002 14:08:42 -0000      1.6.2.1
--- py_object.h 8 Jun 2002 21:15:35 -0000       1.6.2.2
***************
*** 2,6 ****
     $Id$
  
!    Copyright (C) 1999/2000/2001    Kai Sterker
     Copyright (C) 2001    Alexandre Courbot
     Part of the Adonthell Project http://adonthell.linuxgames.com
--- 2,6 ----
     $Id$
  
!    Copyright (C) 1999/2000/2001/2002 Kai Sterker
     Copyright (C) 2001    Alexandre Courbot
     Part of the Adonthell Project http://adonthell.linuxgames.com
***************
*** 41,46 ****
   * Use this class to create instances of Python classes
   * contained in Python modules, then control their execution. You can pass
!  * arguments to the class constructor when the script is imported, then to
!  * any method you want to run.
   * 
   */ 
--- 41,46 ----
   * Use this class to create instances of Python classes
   * contained in Python modules, then control their execution. You can pass
!  * arguments to the class constructor when the script is imported and to
!  * any method you wish to run.
   * 
   */ 
***************
*** 93,111 ****
       * @return the Python class instance
       */
!     PyObject *get_instance ()
      {
!         return instance;
      }
  
      /**
!      * Returns the module name of this object.
       * 
!      * @return module name of this object.
       */
!     string object_file () const
      {
!         return script_file_;
      }
       
      /** 
       * Call a method of this object.
--- 93,131 ----
       * @return the Python class instance
       */
!     PyObject *get_instance () const
      {
!         return Instance;
      }
  
      /**
!      * Returns the arguments of this object.
       * 
!      * @return arguments of this object.
       */
!     PyObject *get_ctorargs () const
      {
!         return CtorArgs;
!     }
! 
!     /**
!      * Returns the class name of this object.
!      * 
!      * @return class name of this object.
!      */
!     string class_name () const
!     {
!         return Classname;
      }
       
+     /**
+      * Returns the file name of this object.
+      * 
+      * @return fiöe name of this object.
+      */
+     string file_name () const
+     {
+         return Filename;
+     }
+ 
      /** 
       * Call a method of this object.
***************
*** 120,124 ****
       * Equivalent to call_method ("run", args); 
       *
!      * @param args Python tuple containing the arguments to pass to the 
method.  
       */
      void run (PyObject * args = NULL)
--- 140,144 ----
       * Equivalent to call_method ("run", args); 
       *
!      * @param args Python tuple containing the arguments to pass to the 
method. 
       */
      void run (PyObject * args = NULL)
***************
*** 135,150 ****
      PyObject* get_attribute (const string & name);
  
  private:
      /**
!      * Helper for create_instance and reload_instance
       *
       */
!     bool instanciate (PyObject*, string, string, PyObject*);
  
!     string script_file_;
  
!     PyObject *instance;
  };
- 
  
  #endif
--- 155,224 ----
      PyObject* get_attribute (const string & name);
  
+     /**
+      * Saves the state of the associated script, so that it can be
+      * completely restored later on. Writes the file- and class name
+      * of the script, the arguments passed to its constructor and
+      * finally its internal state (i.e. the __dict__) to file. 
+      *
+      * @param file The stream to save the state to.
+      */
+     void put_state (ogzstream & file);
+     
+     /**
+      * Restores the state of the py_object from file. Loads file-,
+      * class name and constructor arguement. Then instanciates the
+      * script and finally merges its __dict__ with the key/value pairs 
+      * read from file.
+      *
+      * @param file The stream to load the state from.
+      * @return <b>true</b>if loading succeeded, <b>false</b> otherwise.
+      */
+     bool get_state (igzstream & file);
+ 
+ #ifndef SWIG
+     /**
+      * Clone the %py_pbject. Note that this implementation makes
+      * a shallow copy of the underlying python instance. However, thanks
+      * to python's reference counting, deleting the clone will not effect
+      * the original or vice versa. But changing one will change the other.
+      *
+      * @param script The &py_object to clone
+      */
+      void operator= (const py_object &script);
+ #endif // SWIG
+ 
  private:
      /**
!      * Helper for create_instance and reload_instance. Instanciates a
!      * class contained with a python module (i.e. at the toplevel of
!      * a Python script).
       *
+      * @param module The Python module that contains the class to instanciate.
+      * @param path The full path to the module
+      * @param classname The name of the class to instanciate
+      * @param args A tuple of the arguments to pass to the class constructor.
       */
!     bool instanciate (PyObject *module, string path, string classname, 
PyObject *args);
  
!     /**
!      * The class name of the current script
!      */
!     string Classname;
!     
!     /**
!      * The file name of the current script
!      */
!     string Filename;
  
!     /**
!      * The actual instance of the Python class
!      */
!     PyObject *Instance;
!     
!     /**
!      * Constructor arguments
!      */
!     PyObject *CtorArgs;
  };
  
  #endif

Index: python_class.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/python_class.cc,v
retrieving revision 1.5.2.3
retrieving revision 1.5.2.4
diff -C2 -r1.5.2.3 -r1.5.2.4
*** python_class.cc     7 Jun 2002 07:41:23 -0000       1.5.2.3
--- python_class.cc     8 Jun 2002 21:15:35 -0000       1.5.2.4
***************
*** 37,41 ****
  void python::init (void)
  {
!     Py_Initialize();
  }
  
--- 37,41 ----
  void python::init (void)
  {
!     Py_Initialize ();
  }
  
***************
*** 48,57 ****
   * Insert a string into the module search path.
   */
! void python::insert_path( char *name )
  {
!     char buf[256];
      
!     sprintf ( buf, "import sys ; sys.path.insert(0, '%s')", name );
!     PyRun_SimpleString ( buf );
  }
  
--- 48,56 ----
   * Insert a string into the module search path.
   */
! void python::insert_path (char *name)
  {
!     string cmd = string ("import sys ; sys.path.insert (0, '") + string 
(name) + string ("')");
      
!     PyRun_SimpleString ((char *) cmd.c_str ());
  }
  
***************
*** 143,154 ****
  
  // Convert a pointer to a string (like SWIG 1.3.7 does)
! char *python::ptr_to_string (char *c, void *ptr, int sz)
  {
      static char hex[17] = "0123456789abcdef";
-     int i;
      unsigned char *u = (unsigned char *) ptr;
      register unsigned char uu;
  
!     for (i = 0; i < sz; i++,u++)
      {
          uu = *u;
--- 142,152 ----
  
  // Convert a pointer to a string (like SWIG 1.3.7 does)
! char *python::ptr_to_string (char *c, void *ptr, u_int32 sz)
  {
      static char hex[17] = "0123456789abcdef";
      unsigned char *u = (unsigned char *) ptr;
      register unsigned char uu;
  
!     for (u_int32 i = 0; i < sz; i++,u++)
      {
          uu = *u;
***************
*** 163,171 ****
  PyObject * python::get_tuple (igzstream & file)
  {
!     PyObject *tuple, *item;
      u_int32 size;
  
      size << file;
!     tuple = PyTuple_New (size);
  
      for (u_int32 i = 0; i < size; i++)
--- 161,169 ----
  PyObject * python::get_tuple (igzstream & file)
  {
!     PyObject *tuple = NULL, *item;
      u_int32 size;
  
      size << file;
!     if (size > 0) tuple = PyTuple_New (size);
  
      for (u_int32 i = 0; i < size; i++)
***************
*** 181,191 ****
  void python::put_tuple (PyObject * tuple, ogzstream & file)
  {
      if (!PyTuple_Check (tuple))
      {
!         fprintf (stderr, "*** python::put_tuple: argument is no tuple!");
          return;
      }
  
!     u_int32 size = PyTuple_Size (tuple);
      size >> file;
  
--- 179,197 ----
  void python::put_tuple (PyObject * tuple, ogzstream & file)
  {
+     u_int32 size = 0;
+ 
+     if (tuple == NULL)
+     {
+         size >> file;
+         return;
+     }
+     
      if (!PyTuple_Check (tuple))
      {
!         fprintf (stderr, "*** python::put_tuple: argument is no tuple!\n");
          return;
      }
  
!     size = PyTuple_Size (tuple);
      size >> file;
  
***************
*** 220,224 ****
      if (!PyDict_Check (dict))
      {
!         fprintf (stderr, "*** python::put_dict: argument is no dict!");
          return;
      }
--- 226,230 ----
      if (!PyDict_Check (dict))
      {
!         fprintf (stderr, "*** python::put_dict: argument is no dict!\n");
          return;
      }
***************
*** 273,277 ****
          default:
          {
!             fprintf (stderr, "*** python::get_object: unknown object code 
'%c'", c);
          }
      }
--- 279,283 ----
          default:
          {
!             fprintf (stderr, "*** python::get_object: unknown object code 
'%c'\n", c);
          }
      }

Index: python_class.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/python_class.h,v
retrieving revision 1.6.2.1
retrieving revision 1.6.2.2
diff -C2 -r1.6.2.1 -r1.6.2.2
*** python_class.h      7 Jun 2002 07:41:23 -0000       1.6.2.1
--- python_class.h      8 Jun 2002 21:15:35 -0000       1.6.2.2
***************
*** 63,67 ****
       * @param name directory to add to Python's include path.
       */
!     static void insert_path( char * name);
      
      /** 
--- 63,67 ----
       * @param name directory to add to Python's include path.
       */
!     static void insert_path (char * name);
      
      /** 
***************
*** 70,74 ****
       * @param s string containing Python statements to execute.
       */
!     static void exec_string(char * s);
      
      /** 
--- 70,74 ----
       * @param s string containing Python statements to execute.
       */
!     static void exec_string (char * s);
      
      /** 
***************
*** 91,98 ****
      
      /** 
!      * Dumps any error information to stderr.
       * 
       */
!     static void show_traceback( void );
      
      /** 
--- 91,98 ----
      
      /** 
!      * Dumps Python error information to stderr.
       * 
       */
!     static void show_traceback (void);
      
      /** 
***************
*** 151,155 ****
       *
       */
!     static char *ptr_to_string (char *c, void *ptr, int sz);
  
      /**
--- 151,155 ----
       *
       */
!     static char *ptr_to_string (char *c, void *ptr, u_int32 sz);
  
      /**

Index: time_event.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event.cc,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -C2 -r1.1.2.2 -r1.1.2.3
*** time_event.cc       12 May 2002 18:36:07 -0000      1.1.2.2
--- time_event.cc       8 Jun 2002 21:15:35 -0000       1.1.2.3
***************
*** 27,33 ****
  time_event::time_event (const string & time, bool absolute)
  {
-     Type = TIME_EVENT;
      Repeat = 0;
!     Time = parse_date (time);
      if (!absolute) Time += gamedate::time ();
  }
--- 27,33 ----
  time_event::time_event (const string & time, bool absolute)
  {
      Repeat = 0;
!     Type = TIME_EVENT;
!     Time = parse_time (time);
      if (!absolute) Time += gamedate::time ();
  }
***************
*** 36,45 ****
  void time_event::repeat (const string & interval, s_int32 count)
  {
!     Interval = parse_date (interval);
      Repeat = count;
  }
  
  // execute the time event
! void time_event::execute (event & evnt)
  {
      // nothing needs be passed to the script; it can get the
--- 36,45 ----
  void time_event::repeat (const string & interval, s_int32 count)
  {
!     Interval = parse_time (interval);
      Repeat = count;
  }
  
  // execute the time event
! void time_event::execute (const event & evnt)
  {
      // nothing needs be passed to the script; it can get the
***************
*** 78,97 ****
  
  // convert the time string to gametime minutes
! u_int32 time_event::parse_date (const string & date)
  {
      u_int32 minutes = 0, number = 0;
! 
!     for (u_int32 i = 0; i < date.length (); i++)
      {
          // got a number
!         if (isdigit (date[i])
!             number = 10 * number + atoi (date[i]);
          // got a letter
          else
          {
!             switch (date[i])
              {
                  // weeks
!                 case w:
                  {
                      minutes += number * DAYS_PER_WEEK * HOURS_PER_DAY * 60;
--- 78,101 ----
  
  // convert the time string to gametime minutes
! u_int32 time_event::parse_time (const string & time)
  {
      u_int32 minutes = 0, number = 0;
!     char num[2] = "0";
!     
!     for (u_int32 i = 0; i < time.length (); i++)
      {
          // got a number
!         if (isdigit (time[i]))
!         {
!             num[0] = time[i];
!             number = 10 * number + atoi (num);
!         }
          // got a letter
          else
          {
!             switch (time[i])
              {
                  // weeks
!                 case 'w':
                  {
                      minutes += number * DAYS_PER_WEEK * HOURS_PER_DAY * 60;
***************
*** 99,103 ****
                  }
                  // days
!                 case d:
                  {
                      minutes += number * HOURS_PER_DAY * 60;
--- 103,107 ----
                  }
                  // days
!                 case 'd':
                  {
                      minutes += number * HOURS_PER_DAY * 60;
***************
*** 105,109 ****
                  }
                  // hours
!                 case h:
                  {
                      minutes += number * 60;
--- 109,113 ----
                  }
                  // hours
!                 case 'h':
                  {
                      minutes += number * 60;
***************
*** 111,115 ****
                  }
                  // minutes
!                 case m:
                  {
                      minutes += number;
--- 115,119 ----
                  }
                  // minutes
!                 case 'm':
                  {
                      minutes += number;
***************
*** 119,124 ****
                  default:
                  {
!                     cerr << "*** time_event::parse_date: Unknown time 
specifier '"
!                          << date[i] << "'\n" << flush;
                      break;
                  }
--- 123,127 ----
                  default:
                  {
!                     fprintf (stderr, "*** time_event::parse_date: Unknown 
time specifier '%c'\n", time[i]);
                      break;
                  }

Index: time_event.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event.h,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -C2 -r1.1.2.3 -r1.1.2.4
*** time_event.h        7 Jun 2002 07:41:23 -0000       1.1.2.3
--- time_event.h        8 Jun 2002 21:15:35 -0000       1.1.2.4
***************
*** 56,59 ****
--- 56,67 ----
  #ifndef SWIG
      /**
+      * Standart constructor.
+      */
+     time_event ()
+     {
+         Type = TIME_EVENT;
+     }
+      
+     /**
       * Create a new time %event. This constructor is primarily used for
       * raising time events.
***************
*** 89,93 ****
       * @return <b>True</b> if the two events equal, <b>false</b> otherwise.
       */
!     bool equals (event & evnt)
      {
          time_event e = (time_event &) evnt;
--- 97,101 ----
       * @return <b>True</b> if the two events equal, <b>false</b> otherwise.
       */
!     bool equals (const event & evnt)
      {
          time_event e = (time_event &) evnt;
***************
*** 101,105 ****
       * @param evnt The %event that triggered this time %event.
       */
!     void execute (event & evnt);
      //@}
      
--- 109,113 ----
       * @param evnt The %event that triggered this time %event.
       */
!     void execute (const event & evnt);
      //@}
      
***************
*** 133,137 ****
       * @return the "alarm" time in %gametime minutes.
       */
!     u_int32 time ()
      {
          return Time;
--- 141,145 ----
       * @return the "alarm" time in %gametime minutes.
       */
!     u_int32 time () const
      {
          return Time;

Index: time_event_handler.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event_handler.cc,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** time_event_handler.cc       12 May 2002 18:36:07 -0000      1.1.2.1
--- time_event_handler.cc       8 Jun 2002 21:15:35 -0000       1.1.2.2
***************
*** 20,25 ****
   */
  
  #include "time_event_handler.h"
! #include "time_event_h"
  
  // See whether a matching event is registered and execute the
--- 20,26 ----
   */
  
+ #include <algorithm>
  #include "time_event_handler.h"
! #include "time_event.h"
  
  // See whether a matching event is registered and execute the
***************
*** 46,52 ****
  }
  
- 
  // Unregister an event
! void event_handler::remove_event (event *e)
  {
      vector<event*>::iterator i;
--- 47,52 ----
  }
  
  // Unregister an event
! void time_event_handler::remove_event (event *e)
  {
      vector<event*>::iterator i;
***************
*** 60,64 ****
  
  // register an event with the handler
! void event_handler::register_event (event *e)
  {
      vector<event*>::iterator i = Events.begin ();
--- 60,64 ----
  
  // register an event with the handler
! void time_event_handler::register_event (event *e)
  {
      vector<event*>::iterator i = Events.begin ();
***************
*** 68,72 ****
      {
          // skip events that are raised earlier than e
!         if (e->time () <= (*i)->time ()) break;
          i++;
      }
--- 68,72 ----
      {
          // skip events that are raised earlier than e
!         if (((time_event *) e)->time () <= ((time_event *) (*i))->time ()) 
break;
          i++;
      }

Index: time_event_handler.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event_handler.h,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** time_event_handler.h        12 May 2002 18:36:07 -0000      1.1.2.1
--- time_event_handler.h        8 Jun 2002 21:15:35 -0000       1.1.2.2
***************
*** 63,67 ****
       *      minutes.
       */
!     void raise_event (const event &evnt);
      
  private:
--- 63,67 ----
       *      minutes.
       */
!     void raise_event (event &evnt);
      
  private:




reply via email to

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