[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Adonthell-commits] CVS: adonthell/src event_handler.cc,NONE,1.1.2.1 eve
From: |
Kai Sterker <address@hidden> |
Subject: |
[Adonthell-commits] CVS: adonthell/src event_handler.cc,NONE,1.1.2.1 event_handler.h,NONE,1.1.2.1 event_list.cc,NONE,1.1.2.1 event_list.h,NONE,1.1.2.1 time_event_handler.cc,NONE,1.1.2.1 time_event_handler.h,NONE,1.1.2.1 event.cc,1.12,1.12.2.1 event.h,1.25,1.25.2.1 gamedate.cc,1.1.2.1,1.1.2.2 time_event.cc,1.1.2.1,1.1.2.2 time_event.h,1.1.2.1,1.1.2.2 |
Date: |
Sun, 12 May 2002 14:36:09 -0400 |
Update of /cvsroot/adonthell/adonthell/src
In directory subversions:/tmp/cvs-serv22498
Modified Files:
Tag: Branch_road_to_0-4
event.cc event.h gamedate.cc time_event.cc time_event.h
Added Files:
Tag: Branch_road_to_0-4
event_handler.cc event_handler.h event_list.cc event_list.h
time_event_handler.cc time_event_handler.h
Log Message:
Start of event system cleanup/improvements - don't look too close at the code
yet ;)
***** Error reading new file: [Errno 2] No such file or directory:
'event_handler.cc'
***** Error reading new file: [Errno 2] No such file or directory:
'event_handler.h'
***** Error reading new file: [Errno 2] No such file or directory:
'event_list.cc'
***** Error reading new file: [Errno 2] No such file or directory:
'event_list.h'
***** Error reading new file: [Errno 2] No such file or directory:
'time_event_handler.cc'
***** Error reading new file: [Errno 2] No such file or directory:
'time_event_handler.h'
Index: event.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event.cc,v
retrieving revision 1.12
retrieving revision 1.12.2.1
diff -C2 -r1.12 -r1.12.2.1
*** event.cc 2 Nov 2001 13:19:34 -0000 1.12
--- event.cc 12 May 2002 18:36:06 -0000 1.12.2.1
***************
*** 20,138 ****
* @brief Defines the event_list, event and event_handler class.
*
- *
*/
- #include <algorithm>
-
- #include "fileops.h"
- #include "python_class.h"
#include "event.h"
! // Array with callbacks to return a newly instanciated event
! new_event event_list::instanciate_event[MAX_EVENT];
!
!
! event_list::~event_list ()
! {
! clear ();
! }
!
! // Unregisters and deletes all events.
! void event_list::clear ()
! {
! event *e;
!
! while (!events.empty ())
! {
! e = events.back ();
! events.pop_back ();
! event_handler::remove_event (e);
! delete e;
! }
! }
!
! // Adds an event to the list and register it with the event_handler.
! void event_list::add_event (event* ev)
! {
! events.push_back (ev);
! event_handler::register_event (ev);
! }
!
! // Register an event for loading
! void event_list::register_event (u_int8 type, new_event e)
! {
! if (type < MAX_EVENT)
! instanciate_event[type] = e;
! }
!
! // Save an event_list to file
! void event_list::save (ogzstream& out) const
! {
! vector <event *>::iterator i;
! u_int32 nbr_events = events.size ();
!
! nbr_events >> out;
!
! for (i = events.begin (); i != events.end (); i++)
! (*i)->save (out);
! }
!
! // Loads an event_list from file
! bool event_list::load (igzstream& in)
! {
! u_int32 nbr_events;
! u_int8 type;
!
! nbr_events << in;
!
! while (nbr_events--)
! {
! event * e = NULL;
! type << in;
!
! // Instanciate an event of the given type
! if (type < MAX_EVENT && instanciate_event[type] != NULL)
! e = instanciate_event[type]();
!
! // try to load it, ...
! if (e != NULL && e->load (in))
! add_event (e);
!
! // ... otherwise fail.
! else
! {
! fprintf (stderr, "Could not load event #%i. Aborting ...\n",
type);
! return false;
! }
! }
!
! return true;
! }
!
! event::event ()
! {
! script_args = NULL;
! }
!
event::~event ()
{
}
void event::set_script (string filename, PyObject * args = NULL)
{
if (filename == "")
{
! script.clear ();
! Py_XDECREF (script_args);
! script_args = NULL;
}
else
{
Py_XINCREF (args);
! script_args = args;
! u_int16 argssize = args == NULL ? 1 : PyTuple_Size (args) + 1;
! PyObject * theargs;
! theargs = PyTuple_New (argssize);
// We can pass_instance directly 'cause PyTuple_SetItem steals a
--- 20,48 ----
* @brief Defines the event_list, event and event_handler class.
*
*/
#include "event.h"
! // destructor
event::~event ()
{
}
+ // set the script asspciated with the event
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);
// We can pass_instance directly 'cause PyTuple_SetItem steals a
***************
*** 141,210 ****
for (u_int16 i = 1; i < argssize; i++)
{
! PyObject * intref = PyTuple_GetItem (args, i - 1);
Py_INCREF (intref);
PyTuple_SetItem (theargs, i, intref);
}
! script.create_instance (EVENTS_DIR + filename, filename, theargs);
Py_DECREF (theargs);
}
- script_file_ = filename;
}
! void event::put_script_state (ogzstream & file) const
{
! script_file () >> file;
! if (script_args)
{
true >> file;
! python::put_tuple (script_args, file);
}
else false >> file;
}
! void event::get_script_state (igzstream & file)
{
! string t;
! bool bo;
!
PyObject * args = NULL;
- t << file;
- bo << file;
- if (bo) args = python::get_tuple (file);
- set_script (t, args);
- Py_XDECREF (args);
- }
! // Array with the registered events; each type of event is kept in
! // a vector of its own for faster access
! vector<event*> event_handler::handlers[MAX_EVENT];
!
! // See whether a matching event is registered and execute the
! // according script(s)
! void event_handler::raise_event (event& e)
! {
! 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);
! }
!
!
! // Unregister an event
! void event_handler::remove_event (event *e)
! {
! vector<event*>::iterator i;
!
! // 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);
!
! }
!
! // Register a event with it's script
! void event_handler::register_event (event *e)
! {
! handlers[e->type].push_back (e);
}
--- 51,93 ----
for (u_int16 i = 1; i < argssize; i++)
{
! PyObject *intref = PyTuple_GetItem (args, i - 1);
Py_INCREF (intref);
PyTuple_SetItem (theargs, i, intref);
}
! Script.create_instance (EVENTS_DIR + filename, filename, theargs);
Py_DECREF (theargs);
}
}
! // save the state of the script associated with the event
! void event::put_state (ogzstream & file) const
{
! 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);
}
Index: event.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event.h,v
retrieving revision 1.25
retrieving revision 1.25.2.1
diff -C2 -r1.25 -r1.25.2.1
*** event.h 10 Sep 2001 14:28:54 -0000 1.25
--- event.h 12 May 2002 18:36:07 -0000 1.25.2.1
***************
*** 2,6 ****
$Id$
! Copyright (C) 2000/2001 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
--- 2,6 ----
$Id$
! Copyright (C) 2000/2001/2002 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
***************
*** 13,37 ****
*/
-
/**
* @file event.h
* @author Kai Sterker <address@hidden>
*
! * @brief Declares the event_list, event and event_handler class.
! *
*
*/
-
! #ifndef EVENT_H_
! #define EVENT_H_
- #include <vector>
#include "fileops.h"
#include "py_object.h"
/**
! * Directory where events scripts resides.
! *
*/
#define EVENTS_DIR "events/"
--- 13,32 ----
*/
/**
* @file event.h
* @author Kai Sterker <address@hidden>
*
! * @brief Declares the event class.
*
*/
! #ifndef EVENT_H__
! #define EVENT_H__
#include "fileops.h"
#include "py_object.h"
/**
! * Directory where event scripts reside.
*/
#define EVENTS_DIR "events/"
***************
*** 41,54 ****
/**
! * Events types.
! *
*/
enum
{
! 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
};
--- 36,48 ----
/**
! * Available Event types.
*/
enum
{
! 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
};
***************
*** 57,289 ****
* be handled by the event list and event handler by inheriting them from
* this class.
- *
*/
class event
{
public:
-
- /**
- * Default constructor.
- *
- */
- event ();
-
/**
* Destructor.
- *
*/
virtual ~event ();
! /**
! * Returns the file name of the event's script.
! *
! *
! * @return file name of the script.
*/
! string script_file () const
! {
! return script_file_;
! }
! /**
! * Save the event to a file.
*
! * @param out file where to save the event.
*/
! virtual void save (ogzstream& out) const = 0;
!
/**
! * Loads an event from a file.
*
! * @param in file to load the event from.
! *
! * @return \e true if the event could be loaded, \e false otherwise
*/
! virtual bool load (igzstream& in) = 0;
/**
* Sets the script for an event.
*
* @param filename filename of the script to set.
*/
void set_script (string filename, PyObject * args = NULL);
-
- void get_script_state (igzstream & file);
- void put_script_state (ogzstream & file) const;
protected:
/**
! * Event type - see enum above.
! *
! */
! u_int8 type;
!
! /**
! * Script object.
! *
! */
! py_object script;
!
! PyObject * script_args;
!
! /**
! * Script file.
! *
*/
! string script_file_;
!
! /**
! * Execute the script.
! *
! */
! virtual void execute (event& e) = 0;
/**
! * Compare two events for equality.
! *
! * @param ev pointer to the event to compare with.
! *
! * @return \e true if the events are equal, \e false otherwise.
! */
! virtual bool equals (event& ev) = 0;
!
! #ifndef SWIG
! friend class event_handler;
! #endif
!
! };
!
! /**
! * Pointer to a function returning a newly allocated event
! *
! */
! typedef event* (*new_event)();
!
!
! /**
! * Base class for objects that want to register events
! *
! */
! class event_list
! {
! public:
! /**
! * Destructor - unregisters and deletes all events owned by this list.
*
*/
! virtual ~event_list ();
!
! /**
! * Unregisters and deletes all events owned by this list.
! *
! */
! void clear ();
!
/**
! * Adds an event to this list. The event will be
! * registered with the event_handler and the list will then
! * take care of it's deletion.
*
! * @param ev pointer to the event to add.
*/
! void add_event (event* ev);
/**
! * Register an event for loading. Before the event_list can load
! * an event from file, it needs a callback function that returns
! * a new instance of the event of the given type.
! *
! * @param type the type of the event to register
! * @param e a callback returning a new instance of an event of the given
type.
! *
! * @sa load ()
*/
! static void register_event (u_int8 type, new_event e);
!
! /**
! * Save the event_list to a file.
! *
! * @param out file where to save the event_list.
! */
! void save (ogzstream& out) const;
- /**
- * Loads the event_list from a file and registers all loaded events.
- * @warning Before the event_list can load an event from file, it needs
- * a callback function that returns a new instance of that event.
- *
- * @param in file to load the event_list from.
- *
- * @return \e true if the event_list was loaded successfully, \e false
otherwise.
- * @sa register_event ()
- */
- bool load (igzstream& in);
-
- #ifndef SWIG
- protected:
/**
! * List of events.
! *
*/
! mutable vector<event*> events;
- private:
/**
! * Array with callbacks that return a newly allocated instance of an
event.
! * The event's type is the postion of the according callback in the array.
! */
! static new_event instanciate_event[MAX_EVENT];
! #endif // SWIG
! };
!
! /**
! * Keeps track of registered scripts, recieves triggered events
! * and executes scripts handling those events
! *
! */
! class event_handler
! {
! public:
! /**
! * Registers an event.
! *
! * @param ev pointer to the event to register.
*/
! static void register_event (event* ev);
!
! /**
! * Unregister an event.
! *
! * @param event* pointer to the event to unregister.
*/
! static void remove_event (event* ev);
! /**
! * Check if an event corresponding to ev exists, and execute it.
! *
! * @param ev event to raise.
*/
! static void raise_event (event& ev);
! private:
! #ifndef SWIG
! static vector<event*> handlers[MAX_EVENT]; // registered events
storage
! #endif
};
! #ifndef SWIG
!
! /**
! * A function that returns a new instance of an event.
! */
! #define NEW_EVENT(evt)\
! event* new_ ## evt () { return (event*) new evt; }
! /**
! * Registers an event with the event_list, allowing it to load this event
! * without knowing about it at compile time.
! */
! #define REGISTER_EVENT(type,evt)\
! event_list::register_event (type, (new_event) &new_ ## evt);
!
! #endif // SWIG
! #endif // EVENT_H_
--- 51,151 ----
* be handled by the event list and event handler by inheriting them from
* this class.
*/
class event
{
public:
/**
* Destructor.
*/
virtual ~event ();
! /**
! * @name Event Handling
*/
! //@{
! /**
! * Execute the associated python script.
*
! * @param evnt The event that triggered the execution.
*/
! virtual void execute (event& evnt) = 0;
!
/**
! * Compare two events for equality.
*
! * @param evnt pointer to the event to compare with.
! * @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.
+ * @param args The arguments to pass to the script's constructor
*/
void set_script (string filename, PyObject * args = NULL);
protected:
+ #ifndef SWIG
/**
! * @name Loading / Saving
*/
! //@{
/**
! * Saves the basic %event %data (such as the type or script data)
! * to a file. Call this method from the derived class.
*
+ * @param out file where to save the %event.
*/
! virtual void put_state (ogzstream& out) const;
!
/**
! * Loads the basic %event %date from a file. Call this method from
! * the derived class.
*
! * @param in file to load the %event from.
! * @return \e true if the %event could be loaded, \e false otherwise
*/
! virtual bool get_state (igzstream& in);
+ //@}
+
/**
! * @name Basic Event Data
*/
! //@{
/**
! * Event type - see enum above.
*/
! u_int8 Type;
/**
! * Defines how often the %event should be repeated. <b>0</b> means
! * never, <b>-1</b> means infinitely and <b>n</b> (n > 0) means
! * exactly n times.
*/
! s_int32 Repeat;
!
! /**
! * The Python script accociated with this %event. It is executed
! * whenever the %event gets triggered.
*/
! py_object Script;
! /**
! * The arguments passed to the script. This needs to be a PyTuple
! * or NULL if there are no arguments.
*/
! PyObject *Args;
! //@}
! #endif // SWIG
};
! #endif // EVENT_H__
Index: gamedate.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/gamedate.cc,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -C2 -r1.1.2.1 -r1.1.2.2
*** gamedate.cc 5 May 2002 19:51:07 -0000 1.1.2.1
--- gamedate.cc 12 May 2002 18:36:07 -0000 1.1.2.2
***************
*** 22,25 ****
--- 22,26 ----
#include "gamedate.h"
#include "gametime.h"
+ #include "time_event.h"
// gametime minutes spent in the gameworld so far
***************
*** 33,37 ****
{
// fts contains the number of cycles that passed since the last
! // call to gametime::tick
Ticks += gametime::frames_to_skip ();
--- 34,38 ----
{
// fts contains the number of cycles that passed since the last
! // call to gamedate::update
Ticks += gametime::frames_to_skip ();
***************
*** 42,46 ****
Time++;
! // TODO: raise time event
}
}
--- 43,48 ----
Time++;
! // raise time event
! event_handler::raise_event (time_event (Time));
}
}
***************
*** 74,78 ****
static u_int day_in_minutes = 60 * HOURS_PER_DAY;
! return day_in_minutes / Time;
}
--- 76,80 ----
static u_int day_in_minutes = 60 * HOURS_PER_DAY;
! return Time / day_in_minutes;
}
Index: time_event.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event.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.cc 8 May 2002 07:58:33 -0000 1.1.2.1
--- time_event.cc 12 May 2002 18:36:07 -0000 1.1.2.2
***************
*** 20,23 ****
--- 20,24 ----
*/
+ #include "stdlib.h"
#include "time_event.h"
#include "gamedate.h"
***************
*** 26,32 ****
time_event::time_event (const string & time, bool absolute)
{
Time = parse_date (time);
if (!absolute) Time += gamedate::time ();
- Repeat = 0;
}
--- 27,34 ----
time_event::time_event (const string & time, bool absolute)
{
+ Type = TIME_EVENT;
+ Repeat = 0;
Time = parse_date (time);
if (!absolute) Time += gamedate::time ();
}
***************
*** 38,41 ****
--- 40,80 ----
}
+ // execute the time event
+ void time_event::execute (event & evnt)
+ {
+ // nothing needs be passed to the script; it can get the
+ // current time from the gametime class if it is needed.
+ Script.run ();
+
+ if (Repeat > 0) Repeat--;
+
+ // when the script needs be repeated, do so.
+ if (Repeat != 0) Time += Interval;
+ }
+
+ // Save time event to file
+ void time_event::put_state (ogzstream& out) const
+ {
+ // save basic event data first
+ event::put_state (out);
+
+ // save time event data
+ Time >> out;
+ Interval >> out;
+ }
+
+ // load time event from file
+ bool time_event::get_state (igzstream& in)
+ {
+ // get basic event data
+ event::get_state (in);
+
+ // get time event data
+ Time << in;
+ Interval << in;
+
+ return true;
+ }
+
// convert the time string to gametime minutes
u_int32 time_event::parse_date (const string & date)
***************
*** 91,93 ****
return minutes;
! }
\ No newline at end of file
--- 130,132 ----
return minutes;
! }
Index: time_event.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/Attic/time_event.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.h 8 May 2002 07:58:33 -0000 1.1.2.1
--- time_event.h 12 May 2002 18:36:07 -0000 1.1.2.2
***************
*** 23,28 ****
--- 23,31 ----
#define TIME_EVENT_H__
+ #include <string>
#include "event.h"
+ using std::string;
+
/**
* The time %event executes the attached python script at a certain point in
***************
*** 35,39 ****
--- 38,48 ----
public:
/**
+ * @name Initialization
+ */
+ //@{
+
+ /**
* Create a new time %event.
+ *
* @param time The time when the %event should be raised. The string
* specifies week, day, hour and minute in the format
***************
*** 45,50 ****
time_event (const string & time, bool absolute = false);
/**
! * Set whether the %event should be raised at fixed intervals
* @param interval The time between two occurences of the %event.
* @param count The number of times the %event shall be repeated.
--- 54,74 ----
time_event (const string & time, bool absolute = false);
+ #ifndef SWIG
/**
! * Create a new time %event. This constructor is primarily used for
! * raising time events.
! *
! * @param time The "alarm" time in %gametime minutes.
! */
! time_event (const u_int32 & time)
! {
! Type = TIME_EVENT;
! Time = time;
! }
! #endif // SWIG
!
! /**
! * Set whether the %event should be raised at fixed intervals.
! *
* @param interval The time between two occurences of the %event.
* @param count The number of times the %event shall be repeated.
***************
*** 52,56 ****
--- 76,140 ----
*/
void repeat (const string & interval, s_int32 count = -1);
+ //@}
+
+ /**
+ * @name Event Handling
+ */
+ //@{
+
+ /**
+ * Compare two time events for equality.
+ *
+ * @param evnt The time event to compare this to.
+ * @return <b>True</b> if the two events equal, <b>false</b> otherwise.
+ */
+ bool equals (event & evnt)
+ {
+ return Time == event->time ();
+ }
+
+ /**
+ * Executes the script associated with this time %event. If the
+ * event repeats it is re-registered with the %event handler.
+ *
+ * @param evnt The %event that triggered this time %event.
+ */
+ void execute (event & evnt);
+ //@}
+
+ /**
+ * @name Loading / Saving
+ */
+ //@{
+
+ /**
+ * Saves the basic %event %data (such as the type or script data)
+ * to a file.
+ *
+ * @param out file where to save the %event.
+ */
+ void put_state (ogzstream& out) const;
+
+ /**
+ * Loads the basic %event %date from a file.
+ *
+ * @param in file to load the %event from.
+ * @return \e true if the %event could be loaded, \e false otherwise
+ */
+ bool get_state (igzstream& in);
+ //@}
+
+ /**
+ * Get the event's "alarm" time, i.e. the time when it needs to be
+ * executed.
+ *
+ * @return the "alarm" time in %gametime minutes.
+ */
+ u_int32 time ()
+ {
+ return Time;
+ }
+
private:
#ifndef SWIG
***************
*** 66,68 ****
};
! #endif // TIME_EVENT_H__
\ No newline at end of file
--- 150,152 ----
};
! #endif // TIME_EVENT_H__
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Adonthell-commits] CVS: adonthell/src event_handler.cc,NONE,1.1.2.1 event_handler.h,NONE,1.1.2.1 event_list.cc,NONE,1.1.2.1 event_list.h,NONE,1.1.2.1 time_event_handler.cc,NONE,1.1.2.1 time_event_handler.h,NONE,1.1.2.1 event.cc,1.12,1.12.2.1 event.h,1.25,1.25.2.1 gamedate.cc,1.1.2.1,1.1.2.2 time_event.cc,1.1.2.1,1.1.2.2 time_event.h,1.1.2.1,1.1.2.2,
Kai Sterker <address@hidden> <=