[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Adonthell-commits] adonthell/src basediskio.cc basediskio.h basefl...
From: |
Kai Sterker |
Subject: |
[Adonthell-commits] adonthell/src basediskio.cc basediskio.h basefl... |
Date: |
Sun, 18 Jun 2006 19:25:53 +0000 |
CVSROOT: /cvsroot/adonthell
Module name: adonthell
Changes by: Kai Sterker <ksterker> 06/06/18 19:25:53
Modified files:
src/base : diskio.cc diskio.h flat.h
src/event : Makefile.am factory.cc factory.h listener.cc
listener.h
src/gfx : surface.cc
src/py-wrappers/adonthell: py_event.i py_rpg.i
src/python : script.cc script.h
src/rpg : Makefile.am
Added files:
src/event : listener_cxx.cc listener_cxx.h
listener_python.cc listener_python.h
src/rpg : schedule_data.cc schedule_data.h
Log message:
FIXED bug whith checksum when loading file
ADDED internal event callbacks
ADDED schedule stuff (untested)
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/base/diskio.cc?cvsroot=adonthell&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/base/diskio.h?cvsroot=adonthell&r1=1.5&r2=1.6
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/base/flat.h?cvsroot=adonthell&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/Makefile.am?cvsroot=adonthell&r1=1.5&r2=1.6
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/factory.cc?cvsroot=adonthell&r1=1.5&r2=1.6
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/factory.h?cvsroot=adonthell&r1=1.5&r2=1.6
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/listener.cc?cvsroot=adonthell&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/listener.h?cvsroot=adonthell&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/listener_cxx.cc?cvsroot=adonthell&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/listener_cxx.h?cvsroot=adonthell&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/listener_python.cc?cvsroot=adonthell&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/event/listener_python.h?cvsroot=adonthell&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/gfx/surface.cc?cvsroot=adonthell&r1=1.7&r2=1.8
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/py-wrappers/adonthell/py_event.i?cvsroot=adonthell&r1=1.4&r2=1.5
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/py-wrappers/adonthell/py_rpg.i?cvsroot=adonthell&r1=1.8&r2=1.9
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/python/script.cc?cvsroot=adonthell&r1=1.6&r2=1.7
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/python/script.h?cvsroot=adonthell&r1=1.9&r2=1.10
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/rpg/Makefile.am?cvsroot=adonthell&r1=1.6&r2=1.7
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/rpg/schedule_data.cc?cvsroot=adonthell&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/adonthell/src/rpg/schedule_data.h?cvsroot=adonthell&rev=1.1
Patches:
Index: base/diskio.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/base/diskio.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- base/diskio.cc 23 Apr 2006 17:12:06 -0000 1.3
+++ base/diskio.cc 18 Jun 2006 19:25:52 -0000 1.4
@@ -1,5 +1,5 @@
/*
- $Id: diskio.cc,v 1.3 2006/04/23 17:12:06 ksterker Exp $
+ $Id: diskio.cc,v 1.4 2006/06/18 19:25:52 ksterker Exp $
Copyright (C) 2004/2006 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
@@ -32,6 +32,7 @@
using base::diskio;
char *diskio::Bin2Hex = "0123456789ABCDEF";
+#define READ_BUFFER_SIZE 16384
// ctor
diskio::diskio () : flat (256)
@@ -60,7 +61,7 @@
checksum << in;
if (checksum != this->checksum ()) {
- fprintf (stderr, "*** diskio::get_record: checksum error. Data might
be corrupted.\n");
+ fprintf (stderr, "*** diskio::get_record: checksum error. Data might
be corrupt.\n");
return false;
}
@@ -81,6 +82,119 @@
// read record from ASCII file
bool diskio::get_ascii (FILE * in)
{
+ char *ptr, *val, *name;
+ char type[1];
+ char buffer[READ_BUFFER_SIZE];
+ char *end = &buffer[READ_BUFFER_SIZE];
+ u_int32 read = fread (buffer, 1, READ_BUFFER_SIZE, in);
+ u_int32 size;
+
+ // check that first byte is the beginning of a new record
+ if (read > 0 && buffer[0] == '{')
+ {
+ ptr = buffer;
+ while (ptr < end)
+ {
+ ptr++;
+
+ // skip whitespace
+ while (ptr < end && isspace (*ptr))
+ {
+ ptr++;
+ continue;
+ }
+
+ // read variable name --> everything before the '/'
+ val = ptr;
+ while (ptr < end && *ptr != '/')
+ {
+ ptr++;
+ continue;
+ }
+
+ u_int32 len = ptr-val;
+ name = new char[len];
+ memcpy (name, val, len);
+ name[len] = '\0';
+
+ // read variable type
+ type[0] = *(++ptr);
+
+ // read variable size
+ val = ptr++;
+ while (ptr < end && isdigit (*ptr))
+ {
+ ptr++;
+ continue;
+ }
+
+ // convert size
+ if (val == ptr)
+ {
+ size = 1;
+ }
+ else
+ {
+ size = (u_int32) strtol (val, NULL, 10);
+ }
+
+ // read value
+ switch (type[0])
+ {
+ // string
+ case 'A':
+ {
+ break;
+ }
+ // bool
+ case 'B':
+ {
+ break;
+ }
+ // char
+ case 'C':
+ {
+ break;
+ }
+ // double
+ case 'F':
+ {
+ break;
+ }
+ // signed int
+ case 'S':
+ {
+ break;
+ }
+ // unsigned int
+ case 'U':
+ {
+ break;
+ }
+ // record
+ case 'R':
+ {
+ break;
+ }
+ // binary
+ case 'X':
+ {
+ break;
+ }
+ default:
+ {
+ fprintf (stderr, "*** diskio::get_ascii: unknown type
'%c'!\n", type[0]);
+ return false;
+ }
+ }
+ }
+ }
+ else
+ {
+ fprintf (stderr, "*** diskio::get_ascii: file does not point to start
of new record\n!");
+ return false;
+ }
+
return true;
}
@@ -90,6 +204,7 @@
write_record (*this, out);
}
+// recursively saves records to file
void diskio::write_record (base::flat &record, FILE * out, const u_int16 &
indent)
{
const char *in2 = std::string (indent+1, '\t').c_str ();
Index: base/diskio.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/base/diskio.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- base/diskio.h 23 Apr 2006 17:12:06 -0000 1.5
+++ base/diskio.h 18 Jun 2006 19:25:52 -0000 1.6
@@ -1,5 +1,5 @@
/*
- $Id: diskio.h,v 1.5 2006/04/23 17:12:06 ksterker Exp $
+ $Id: diskio.h,v 1.6 2006/06/18 19:25:52 ksterker Exp $
Copyright (C) 2004/2006 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
@@ -73,12 +73,21 @@
*/
//@{
/**
- *
+ * Load this record from the given ASCII file. Compared to
get_record(),
+ * no checksum is kept in the ASCII file, so file corruption
cannot be
+ * detected prior to loading.
+ * @param in file to read data from.
+ * @return \b true on successful loading, \b false otherwise.
+ * @sa get_record()
*/
bool get_ascii (FILE * in);
/**
- *
+ * Save this record to an ASCII file. Compared to put_record(), no
+ * checksum will be saved with the record. Multiple records can be
+ * saved in the same file.
+ * @param out file to save record to.
+ * @sa put_record()
*/
void put_ascii (FILE * out);
//@}
Index: base/flat.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/base/flat.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- base/flat.h 23 Apr 2006 17:12:06 -0000 1.13
+++ base/flat.h 18 Jun 2006 19:25:52 -0000 1.14
@@ -1,5 +1,5 @@
/*
- $Id: flat.h,v 1.13 2006/04/23 17:12:06 ksterker Exp $
+ $Id: flat.h,v 1.14 2006/06/18 19:25:52 ksterker Exp $
Copyright (C) 2004 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
Index: event/Makefile.am
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event/Makefile.am,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- event/Makefile.am 14 Aug 2005 16:51:20 -0000 1.5
+++ event/Makefile.am 18 Jun 2006 19:25:52 -0000 1.6
@@ -8,6 +8,8 @@
event.h \
factory.h \
listener.h \
+ listener_cxx.h \
+ listener_python.h \
manager.h \
manager_base.h \
time_event.h \
@@ -23,6 +25,8 @@
event.cc \
factory.cc \
listener.cc \
+ listener_cxx.cc \
+ listener_python.cc \
types.cc \
time_event.cc \
time_event_manager.cc
Index: event/factory.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event/factory.cc,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- event/factory.cc 7 Dec 2004 16:46:27 -0000 1.5
+++ event/factory.cc 18 Jun 2006 19:25:52 -0000 1.6
@@ -1,5 +1,5 @@
/*
- $Id: factory.cc,v 1.5 2004/12/07 16:46:27 ksterker Exp $
+ $Id: factory.cc,v 1.6 2006/06/18 19:25:52 ksterker Exp $
Copyright (C) 2000/2001/2002/2003/2004 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
@@ -32,6 +32,8 @@
#include "base/diskio.h"
#include "event/factory.h"
#include "event/manager.h"
+#include "event/listener_cxx.h"
+#include "event/listener_python.h"
using std::vector;
using events::factory;
@@ -65,9 +67,14 @@
}
// Add an event to the factory and register it with the event manager.
-listener *factory::add (event* ev)
+listener *factory::add (event* ev, int type)
{
- listener *li = new listener (this, ev);
+ listener *li = NULL;
+
+ // create listener according to desired type
+ if (type == LISTENER_CXX) li = new events::listener_cxx (this, ev);
+ else li = new events::listener_python (this, ev);
+
Listeners.push_back (li);
// if the factory is paused, also pause new events
@@ -147,6 +154,7 @@
// get registered listeners
while ((type = listeners.next (&value, &size)) != -1)
{
+ // get listener container
if (type != base::flat::T_FLAT)
{
fprintf (stderr, "*** error: factory::get_state: expected type
T_FLAT but got %i!\n", type);
@@ -154,7 +162,14 @@
}
base::flat state ((const char*) value, size);
- li = new listener (this, NULL);
+ // get type of listener
+ type = state.get_uint8 ("ltp");
+
+ // create listener according to desired type
+ if (type == LISTENER_CXX) li = new events::listener_cxx (this, NULL);
+ else li = new events::listener_python (this, NULL);
+
+ // get listener data
if (li->get_state (state))
{
Listeners.push_back (li);
Index: event/factory.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event/factory.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -b -r1.5 -r1.6
--- event/factory.h 7 Dec 2004 16:46:27 -0000 1.5
+++ event/factory.h 18 Jun 2006 19:25:52 -0000 1.6
@@ -1,7 +1,7 @@
/*
- $Id: factory.h,v 1.5 2004/12/07 16:46:27 ksterker Exp $
+ $Id: factory.h,v 1.6 2006/06/18 19:25:52 ksterker Exp $
- Copyright (C) 2000/2001/2002/2003/2004 Kai Sterker <address@hidden>
+ Copyright (C) 2000/2001/2002/2003/2004/2006 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
Adonthell is free software; you can redistribute it and/or modify
@@ -78,9 +78,11 @@
* also keeps track of the %listener to take care of its deletion.
*
* @param ev_co pointer to the %event to add.
+ * @param type type of listener to create. Default is a listener with
+ * a python callback attached.
* @return pointer to the newly created %listener. Do not free it!
*/
- listener *add (event* ev_co);
+ listener *add (event* ev_co, int type = LISTENER_PYTHON);
/**
* Removes a %listener from the %factory. This is usually called when a
Index: event/listener.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event/listener.cc,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- event/listener.cc 3 Jun 2005 17:29:13 -0000 1.7
+++ event/listener.cc 18 Jun 2006 19:25:53 -0000 1.8
@@ -1,5 +1,5 @@
/*
- $Id: listener.cc,v 1.7 2005/06/03 17:29:13 ksterker Exp $
+ $Id: listener.cc,v 1.8 2006/06/18 19:25:53 ksterker Exp $
Copyright (C) 2004/2005 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
@@ -27,7 +27,6 @@
*
*/
-#include "python/pool.h"
#include "event/manager.h"
#include "event/listener.h"
@@ -38,8 +37,6 @@
listener::listener (factory *f, event *e)
{
Registered = false;
- Method = NULL;
- Args = NULL;
Factory = f;
Event = e;
Paused = 0;
@@ -55,98 +52,6 @@
if (Factory) Factory->remove (this);
delete Event;
-
- // we no longer use the callback
- delete Method;
- // ... and the arguments neither
- Py_XDECREF (Args);
-}
-
-// set python method to be called when the event occurs
-bool listener::connect_callback (const string & file, const string &
classname, const string & callback, PyObject *args)
-{
- u_int16 size;
-
- // cleanup
- delete Method;
-
- // just disconnect the callback
- if (file == "")
- {
- Method = NULL;
- return false;
- }
-
- // create the callback
- Method = python::pool::connect (EVENTS_DIR + file, classname, callback);
- if (!Method)
- {
- fprintf (stderr, "*** listener::connect_callback: connecting callback
failed!\n");
- return false;
- }
-
- // make sure the given arguments are a tuple
- if (!args || !PyTuple_Check (args))
- {
- if (args) fprintf (stderr, "*** warning: listener::connect_callback:
argument must be a tuple!\n");
- size = 2;
- }
- else size = PyTuple_GET_SIZE (args) + 2;
-
- // keep old argument tuple, if possible
- if (!Args || PyTuple_GET_SIZE (Args) != size)
- {
- // free old args
- Py_XDECREF (Args);
-
- // prepare callback arguments
- Args = PyTuple_New (size);
-
- // first argument is the listener itself
- PyTuple_SET_ITEM (Args, 0, python::pass_instance (this));
- }
-
- // second argument will be the event that triggered the callback
- for (u_int16 i = 2; i < size; i++)
- {
- // copy remaining arguments, if any
- PyObject *arg = PyTuple_GET_ITEM (args, i-2);
- Py_INCREF (arg);
- PyTuple_SET_ITEM (Args, i, arg);
- }
- return true;
-}
-
-// execute callback for given event
-s_int32 listener::raise_event (const event* evnt)
-{
- if (Method && Event->repeat ())
- {
- // make sure that arguments remain valid while the script executes
- PyObject *args = Args;
- Py_INCREF (args);
-
- // event that triggered the script is 2nd argument of callback
- PyTuple_SET_ITEM (args, 1, python::pass_instance ((event*) evnt));
-
- // adjust repeat count
- Event->do_repeat ();
-
- // execute callback
- Method->execute (args);
-
- // clean up
- Py_DECREF (PyTuple_GET_ITEM (args, 1));
- Py_DECREF (args);
- }
- else
- {
- if (!Method) fprintf (stderr, "*** warning: listener::raise_event: no
callback connected\n");
- return 0;
- }
-
- // return whether event needs be repeated or not
- return Event->repeat ();
}
// disable the event temporarily
@@ -174,22 +79,13 @@
// save the state of the script associated with the event
void listener::put_state (base::flat & out) const
{
- base::flat listener;
-
- listener.put_string ("lid", Id);
- listener.put_uint16 ("lps", Paused);
- listener.put_bool ("lmt", Method != NULL);
-
- if (Method != NULL)
- {
- Method->put_state (listener);
- python::put_tuple (Args, listener, 2);
- }
-
- listener.put_bool ("lev", Event != NULL);
- if (Event != NULL) Event->put_state (listener);
-
- out.put_flat ("", listener);
+ // save id and paused state
+ out.put_string ("lid", Id);
+ out.put_uint16 ("lps", Paused);
+
+ // save attached event
+ out.put_bool ("lev", Event != NULL);
+ if (Event != NULL) Event->put_state (out);
}
// load the state of the script associated with the event
@@ -198,20 +94,6 @@
Id = in.get_string ("lid");
Paused = in.get_uint16 ("lps");
- // load callback
- if (in.get_bool ("lmt") == true)
- {
- Method = new python::method ();
- if (Method->get_state (in) == false)
- {
- fprintf (stderr, "*** listener::get_state: restoring callback
failed!\n");
- return false;
- }
-
- Args = python::get_tuple (in, 2);
- PyTuple_SET_ITEM (Args, 0, python::pass_instance (this));
- }
-
// load event structure
if (in.get_bool ("lev") == true)
{
Index: event/listener.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/event/listener.h,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- event/listener.h 3 Jun 2005 17:29:13 -0000 1.8
+++ event/listener.h 18 Jun 2006 19:25:53 -0000 1.9
@@ -1,5 +1,5 @@
/*
- $Id: listener.h,v 1.8 2005/06/03 17:29:13 ksterker Exp $
+ $Id: listener.h,v 1.9 2006/06/18 19:25:53 ksterker Exp $
Copyright (C) 2004/2005 Kai Sterker <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
@@ -23,24 +23,30 @@
* @file event/listener.h
* @author Kai Sterker <address@hidden>
*
- * @brief Declares the %listener class.
+ * @brief Declares the %listener base class.
*
*/
#ifndef EVENT_LISTENER_H
#define EVENT_LISTENER_H
-#include "python/method.h"
#include "event/event.h"
+#include "base/callback.h"
+#include <Python.h>
namespace events
{
+ /** Types of listeners */
+ enum
+ {
+ LISTENER_CXX = 0,
+ LISTENER_PYTHON = 1
+ };
+
class factory;
/**
- * An %event %listener contains a callback to a %python %method that it
- * will execute when the event occurs that the %listener is waiting
- * for. A %listener is usually created by a certain %event %factory and
will
+ * %Listeners are usually created by a certain %event %factory and will
* be destroyed if the %factory is destroyed. That way, it's not neccessary
* to keep track of every single %listener. Instead, a few factories can
* be used to create groups of listeners that can be disposed together.
@@ -50,6 +56,8 @@
public:
/**
* Create a new listener, which listens to the given event.
+ * @param f the factory that created the listener or NULL
+ * @param e the event that will trigger the attached callback
*/
listener (factory *f, event *e);
@@ -57,7 +65,7 @@
* Destroy the %listener. Automatically removes it from
* the %event %manager and %factory if required.
*/
- ~listener ();
+ virtual ~listener ();
/**
* @name Member access
@@ -82,8 +90,8 @@
}
/**
- * Get the event's id.
- * @return id of the %event.
+ * Get the listener's id.
+ * @return id of the %listener.
*/
const string & id () const
{
@@ -91,8 +99,8 @@
}
/**
- * Assign an id to the %event, so it may be retrieved from an
- * event_list later on, without having a pointer to it.
+ * Assign an id to the %listener, so it may be retrieved from
+ * a %factory later on, without having a pointer to it.
*
* @param id a string to identify the %event.
*/
@@ -146,6 +154,10 @@
//@}
/**
+ * @name Event handling
+ */
+ //@{
+ /**
* Sets a python method to be executed whenever an
* %event occurs that this listener listens to.
*
@@ -155,8 +167,18 @@
* @param args Additional arguments to pass to the callback.
* @return \b false if connecting the callback failed, \b true
otherwise.
*/
- bool connect_callback (const string & file, const string & classname,
- const string & callback, PyObject *args = NULL);
+ virtual bool connect_callback (const string & file, const string &
classname,
+ const string & callback, PyObject *args = NULL) = 0;
+
+#ifndef SWIG
+ /**
+ * Sets a C++ method to be executed whenever an
+ * %event occurs that this listener listens to.
+ *
+ * @param callback The method to call.
+ */
+ virtual void connect_callback (base::functor_0 * callback) = 0;
+#endif // SWIG
/**
* Execute the associated python script or callback.
@@ -164,7 +186,7 @@
* @param evnt The %event that triggered the execution.
* @return The number of times the %event needs to be repeated.
*/
- s_int32 raise_event (const event* evnt);
+ virtual s_int32 raise_event (const event* evnt) = 0;
/**
* Check whether the given %event matches the %event attached to
@@ -176,6 +198,7 @@
{
return Event->equals (e);
}
+ //@}
/**
* @name Pausing / Resuming execution
@@ -214,23 +237,29 @@
*
* @param out stream where to add the %event %listener.
*/
- void put_state (base::flat& out) const;
+ virtual void put_state (base::flat& out) const;
/**
* Loads the %event %listener from given stream.
*
* @param in stream to load the %event %listener from.
*/
- bool get_state (base::flat& in);
+ virtual bool get_state (base::flat& in);
//@}
#ifndef SWIG
/**
* Allow %listener to be passed as python argument
*/
- GET_TYPE_NAME(events::listener)
+ GET_TYPE_NAME_VIRTUAL (events::listener)
#endif // SWIG
+ protected:
+ /**
+ * The %event this %listener will react to.
+ */
+ event *Event;
+
private:
/**
* (Optional) Id of the event
@@ -248,21 +277,6 @@
u_int16 Paused;
/**
- * The callback connected to this %listener
- */
- python::method *Method;
-
- /**
- * Arguments to pass to the method
- */
- PyObject *Args;
-
- /**
- * The %event this %listener will react to.
- */
- event *Event;
-
- /**
* The %event %factory that created this listener.
*/
factory *Factory;
Index: gfx/surface.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/gfx/surface.cc,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- gfx/surface.cc 3 Jun 2006 04:20:27 -0000 1.7
+++ gfx/surface.cc 18 Jun 2006 19:25:53 -0000 1.8
@@ -1,5 +1,5 @@
/*
- $Id: surface.cc,v 1.7 2006/06/03 04:20:27 Mithander Exp $
+ $Id: surface.cc,v 1.8 2006/06/18 19:25:53 ksterker Exp $
Copyright (C) 1999/2000/2001/2002/2003 Alexandre Courbot <address@hidden>
Copyright (C) 2006 Tyler Nielsen
@@ -30,7 +30,7 @@
* @brief Defines the surface global interface.
*/
-#include "surface.h"
+#include "gfx/surface.h"
#include "gfx/png_wrapper.h"
#ifdef __BIG_ENDIAN__
Index: py-wrappers/adonthell/py_event.i
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py-wrappers/adonthell/py_event.i,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- py-wrappers/adonthell/py_event.i 7 Dec 2004 16:46:27 -0000 1.4
+++ py-wrappers/adonthell/py_event.i 18 Jun 2006 19:25:53 -0000 1.5
@@ -6,6 +6,7 @@
#include "event/factory.h"
#include "event/manager.h"
#include "event/time_event.h"
+#include "event/listener_python.h"
%}
%include "std_string.i"
@@ -24,5 +25,6 @@
%include "event/event.h"
%include "event/time_event.h"
%include "event/listener.h"
+%include "event/listener_python.h"
%include "event/factory.h"
%include "event/manager.h"
Index: py-wrappers/adonthell/py_rpg.i
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py-wrappers/adonthell/py_rpg.i,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- py-wrappers/adonthell/py_rpg.i 15 Feb 2006 21:30:42 -0000 1.8
+++ py-wrappers/adonthell/py_rpg.i 18 Jun 2006 19:25:53 -0000 1.9
@@ -13,6 +13,7 @@
#include "rpg/log_index.h"
#include "rpg/quest_event.h"
#include "rpg/quest.h"
+#include "rpg/schedule.h"
using rpg::slot;
using rpg::quest_part;
Index: python/script.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/python/script.cc,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- python/script.cc 3 Jun 2005 17:29:13 -0000 1.6
+++ python/script.cc 18 Jun 2006 19:25:53 -0000 1.7
@@ -1,7 +1,7 @@
/*
- $Id: script.cc,v 1.6 2005/06/03 17:29:13 ksterker Exp $
+ $Id: script.cc,v 1.7 2006/06/18 19:25:53 ksterker Exp $
- Copyright (C) 1999/2000/2001/2003/2004 Kai Sterker
+ Copyright (C) 1999/2000/2001/2003/2004/2006 Kai Sterker
Copyright (C) 2001 Alexandre Courbot
Part of the Adonthell Project http://adonthell.linuxgames.com
@@ -41,6 +41,7 @@
Instance = NULL;
Filename = "";
Classname = "";
+ Args = NULL;
}
script::~script ()
@@ -53,7 +54,9 @@
{
// Delete our Instance
Py_XDECREF (Instance);
+ Py_XDECREF (Args);
Instance = NULL;
+ Args = NULL;
Filename = "";
Classname = "";
@@ -108,6 +111,9 @@
return false;
}
+ Args = args;
+ Py_XDECREF (Args);
+
Filename = file;
Classname = classname;
@@ -228,3 +234,26 @@
}
else return;
}
+
+// save script to disk
+void script::put_state (base::flat & out) const
+{
+ out.put_string ("scf", Filename);
+ out.put_string ("scn", Classname);
+ python::put_tuple (Args, out);
+}
+
+// load callback connection from disk and reconnect
+bool script::get_state (base::flat & in)
+{
+ std::string filename = in.get_string ("scf");
+ std::string classname = in.get_string ("scn");
+ PyObject *args = python::get_tuple (in);
+
+ if (in.success ())
+ {
+ return create_instance (filename, classname, args);
+ }
+
+ return false;
+}
Index: python/script.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/python/script.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- python/script.h 22 Jan 2006 21:32:39 -0000 1.9
+++ python/script.h 18 Jun 2006 19:25:53 -0000 1.10
@@ -1,7 +1,7 @@
/*
- $Id: script.h,v 1.9 2006/01/22 21:32:39 ksterker Exp $
+ $Id: script.h,v 1.10 2006/06/18 19:25:53 ksterker Exp $
- Copyright (C) 1999/2000/2001/2003 Kai Sterker <address@hidden>
+ Copyright (C) 1999/2000/2001/2003/2006 Kai Sterker <address@hidden>
Copyright (C) 2001 Alexandre Courbot <address@hidden>
Part of the Adonthell Project http://adonthell.linuxgames.com
@@ -227,6 +227,24 @@
}
//@}
+ /**
+ * @name Loading / Saving
+ */
+ //@{
+ /**
+ * Save the %script to a stream.
+ * @param out stream where to save the %script.
+ */
+ void put_state (base::flat& out) const;
+
+ /**
+ * Loads the %script from a stream.
+ * @param in stream to load the %script from.
+ * @return \e true if the %script was loaded successfully, \e false
otherwise.
+ */
+ bool get_state (base::flat& in);
+ //@}
+
#ifndef SWIG
/// allow script to be passed through SWIG
GET_TYPE_NAME_VIRTUAL(python::script);
@@ -245,6 +263,9 @@
/// The file name of the current script
std::string Filename;
+
+ /// The arguments passed to script ctor
+ PyObject *Args;
};
}
#endif // PYTHON_SCRIPT_H
Index: rpg/Makefile.am
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/rpg/Makefile.am,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- rpg/Makefile.am 22 Jan 2006 21:32:39 -0000 1.6
+++ rpg/Makefile.am 18 Jun 2006 19:25:53 -0000 1.7
@@ -15,6 +15,8 @@
quest.h \
quest_event.h \
quest_event_manager.h \
+ schedule.h \
+ schedule_data.h \
slot.h
## Rpg library
@@ -34,6 +36,8 @@
quest.cc \
quest_event.cc \
quest_event_manager.cc \
+ schedule.cc\
+ schedule_data.cc \
slot.cc
libadonthell_rpg_la_CXXFLAGS = $(PY_CFLAGS)
Index: event/listener_cxx.cc
===================================================================
RCS file: event/listener_cxx.cc
diff -N event/listener_cxx.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ event/listener_cxx.cc 18 Jun 2006 19:25:53 -0000 1.1
@@ -0,0 +1,104 @@
+/*
+ $Id: listener_cxx.cc,v 1.1 2006/06/18 19:25:53 ksterker Exp $
+
+ Copyright (C) 2006 Kai Sterker <address@hidden>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * @file event/listener_cxx.cc
+ * @author Kai Sterker <address@hidden>
+ *
+ * @brief Implements a %listener with python callback attached.
+ *
+ */
+
+#include "event/listener_cxx.h"
+
+using events::listener;
+using events::listener_cxx;
+
+// ctor
+listener_cxx::listener_cxx (factory *f, event *e) : listener (f, e)
+{
+ Callback = NULL;
+}
+
+// destructor
+listener_cxx::~listener_cxx ()
+{
+ Callback = NULL;
+}
+
+// set python method to be called when the event occurs
+bool listener_cxx::connect_callback (const string & file, const string &
classname, const string & callback, PyObject *args)
+{
+ fprintf (stderr, "*** error: listener_cxx::connect_callback (python):
unsupported operation!\n");
+ return false;
+}
+
+// set a C/C++ callback as event's action
+void listener_cxx::connect_callback (base::functor_0 * callback)
+{
+ Callback = callback;
+}
+
+// execute callback for given event
+s_int32 listener_cxx::raise_event (const event* evnt)
+{
+ if (Callback && Event->repeat ())
+ {
+ // adjust repeat count
+ Event->do_repeat ();
+
+ // execute callback
+ (*Callback) ();
+ }
+ else
+ {
+ if (!Callback) fprintf (stderr, "*** warning:
listener_cxx::raise_event: no callback connected\n");
+ return 0;
+ }
+
+ // return whether event needs be repeated or not
+ return Event->repeat ();
+}
+
+// save the state of the script associated with the event
+void listener_cxx::put_state (base::flat & out) const
+{
+ base::flat record;
+
+ // save listener type
+ record.put_uint8 ("ltp", LISTENER_CXX);
+
+ // save base data
+ listener::put_state (record);
+
+ out.put_flat ("", record);
+}
+
+// load the state of the script associated with the event
+bool listener_cxx::get_state (base::flat & in)
+{
+ listener::get_state (in);
+
+ // reconnect callback
+
+ return in.success ();
+}
+
Index: event/listener_cxx.h
===================================================================
RCS file: event/listener_cxx.h
diff -N event/listener_cxx.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ event/listener_cxx.h 18 Jun 2006 19:25:53 -0000 1.1
@@ -0,0 +1,131 @@
+/*
+ $Id: listener_cxx.h,v 1.1 2006/06/18 19:25:53 ksterker Exp $
+
+ Copyright (C) 2006 Kai Sterker <address@hidden>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * @file event/listener_cxx.h
+ * @author Kai Sterker <address@hidden>
+ *
+ * @brief Declares a %listener with python callback attached.
+ *
+ */
+
+#ifndef EVENT_LISTENER_CXX_H
+#define EVENT_LISTENER_CXX_H
+
+#include "event/listener.h"
+
+namespace events
+{
+ /**
+ * An %event %listener that contains a callback to a %python %method it
+ * will execute when the event occurs that the %listener is waiting
+ * for. A %listener is usually created by a certain %event %factory and
will
+ * be destroyed if the %factory is destroyed. That way, it's not neccessary
+ * to keep track of every single %listener. Instead, a few factories can
+ * be used to create groups of listeners that can be disposed together.
+ */
+ class listener_cxx : public listener
+ {
+ public:
+ /**
+ * Create a new listener, which listens to the given event.
+ * @param f the factory that created the listener or NULL
+ * @param e the event that will trigger the attached callback
+ */
+ listener_cxx (factory *f, event *e);
+
+ /**
+ * Destroy the %listener. Automatically removes it from
+ * the %event %manager and %factory if required.
+ */
+ virtual ~listener_cxx ();
+
+ /**
+ * @name Event handling
+ */
+ //@{
+ /**
+ * Sets a python method to be executed whenever an
+ * %event occurs that this listener listens to.
+ *
+ * @param file Name of the script to load.
+ * @param classname Name of the class containing the callback.
+ * @param callback Name of the method to call.
+ * @param args Additional arguments to pass to the callback.
+ * @return \b false if connecting the callback failed, \b true
otherwise.
+ */
+ bool connect_callback (const string & file, const string & classname,
+ const string & callback, PyObject *args = NULL);
+
+#ifndef SWIG
+ /**
+ * Sets a C++ method to be executed whenever an
+ * %event occurs that this listener listens to.
+ *
+ * @param callback The method to call.
+ */
+ void connect_callback (base::functor_0 * callback);
+#endif // SWIG
+
+ /**
+ * Execute the associated python script or callback.
+ *
+ * @param evnt The %event that triggered the execution.
+ * @return The number of times the %event needs to be repeated.
+ */
+ s_int32 raise_event (const event* evnt);
+ //@}
+
+ /**
+ * @name Loading / Saving
+ */
+ //@{
+ /**
+ * Flattens the %event %listener to the given stream.
+ *
+ * @param out stream where to add the %event %listener.
+ */
+ virtual void put_state (base::flat& out) const;
+
+ /**
+ * Loads the %event %listener from given stream.
+ *
+ * @param in stream to load the %event %listener from.
+ */
+ virtual bool get_state (base::flat& in);
+ //@}
+
+#ifndef SWIG
+ /**
+ * Allow %python %listener to be passed as python argument
+ */
+ GET_TYPE_NAME (events::listener_cxx);
+#endif // SWIG
+
+ private:
+ /**
+ * C++ callback that may be executed when the %event gets triggered.
+ */
+ base::functor_0 * Callback;
+ };
+}
+
+#endif // EVENT_LISTENER_CXX_H
Index: event/listener_python.cc
===================================================================
RCS file: event/listener_python.cc
diff -N event/listener_python.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ event/listener_python.cc 18 Jun 2006 19:25:53 -0000 1.1
@@ -0,0 +1,189 @@
+/*
+ $Id: listener_python.cc,v 1.1 2006/06/18 19:25:53 ksterker Exp $
+
+ Copyright (C) 2006 Kai Sterker <address@hidden>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * @file event/listener_python.cc
+ * @author Kai Sterker <address@hidden>
+ *
+ * @brief Implements a %listener with python callback attached.
+ *
+ */
+
+#include "listener_python.h"
+#include "python/pool.h"
+
+using events::listener;
+using events::listener_python;
+
+// ctor
+listener_python::listener_python (factory *f, event *e) : listener (f, e)
+{
+ Method = NULL;
+ Args = NULL;
+}
+
+// destructor
+listener_python::~listener_python ()
+{
+ // we no longer use the callback
+ delete Method;
+ // ... and the arguments neither
+ Py_XDECREF (Args);
+}
+
+// set python method to be called when the event occurs
+bool listener_python::connect_callback (const string & file, const string &
classname, const string & callback, PyObject *args)
+{
+ u_int16 size;
+
+ // cleanup
+ delete Method;
+
+ // just disconnect the callback
+ if (file == "")
+ {
+ Method = NULL;
+ return false;
+ }
+
+ // create the callback
+ Method = python::pool::connect (EVENTS_DIR + file, classname, callback);
+ if (!Method)
+ {
+ fprintf (stderr, "*** listener::connect_callback: connecting callback
failed!\n");
+ return false;
+ }
+
+ // make sure the given arguments are a tuple
+ if (!args || !PyTuple_Check (args))
+ {
+ if (args) fprintf (stderr, "*** warning: listener::connect_callback:
argument must be a tuple!\n");
+ size = 2;
+ }
+ else size = PyTuple_GET_SIZE (args) + 2;
+
+ // keep old argument tuple, if possible
+ if (!Args || PyTuple_GET_SIZE (Args) != size)
+ {
+ // free old args
+ Py_XDECREF (Args);
+
+ // prepare callback arguments
+ Args = PyTuple_New (size);
+
+ // first argument is the listener itself
+ PyTuple_SET_ITEM (Args, 0, python::pass_instance (this));
+ }
+
+ // second argument will be the event that triggered the callback
+ for (u_int16 i = 2; i < size; i++)
+ {
+ // copy remaining arguments, if any
+ PyObject *arg = PyTuple_GET_ITEM (args, i-2);
+ Py_INCREF (arg);
+ PyTuple_SET_ITEM (Args, i, arg);
+ }
+
+ return true;
+}
+
+// set a C/C++ callback as event's action
+void listener_python::connect_callback (base::functor_0 * callback)
+{
+ fprintf (stderr, "*** error: listener_cxx::connect_callback (cxx):
unsupported operation!\n");
+}
+
+// execute callback for given event
+s_int32 listener_python::raise_event (const event* evnt)
+{
+ if (Method && Event->repeat ())
+ {
+ // make sure that arguments remain valid while the script executes
+ PyObject *args = Args;
+ Py_INCREF (args);
+
+ // event that triggered the script is 2nd argument of callback
+ PyTuple_SET_ITEM (args, 1, python::pass_instance ((event*) evnt));
+
+ // adjust repeat count
+ Event->do_repeat ();
+
+ // execute callback
+ Method->execute (args);
+
+ // clean up
+ Py_DECREF (PyTuple_GET_ITEM (args, 1));
+ Py_DECREF (args);
+ }
+ else
+ {
+ if (!Method) fprintf (stderr, "*** warning: listener::raise_event: no
callback connected\n");
+ return 0;
+ }
+
+ // return whether event needs be repeated or not
+ return Event->repeat ();
+}
+
+// save the state of the script associated with the event
+void listener_python::put_state (base::flat & out) const
+{
+ base::flat record;
+
+ // save listener type
+ record.put_uint8 ("ltp", LISTENER_PYTHON);
+
+ // save base data
+ listener::put_state (record);
+
+ // save callback
+ record.put_bool ("lmt", Method != NULL);
+ if (Method != NULL)
+ {
+ Method->put_state (record);
+ python::put_tuple (Args, record, 2);
+ }
+
+ out.put_flat ("", record);
+}
+
+// load the state of the script associated with the event
+bool listener_python::get_state (base::flat & in)
+{
+ listener::get_state (in);
+
+ // load callback
+ if (in.get_bool ("lmt") == true)
+ {
+ Method = new python::method ();
+ if (Method->get_state (in) == false)
+ {
+ fprintf (stderr, "*** listener::get_state: restoring callback
failed!\n");
+ return false;
+ }
+
+ Args = python::get_tuple (in, 2);
+ PyTuple_SET_ITEM (Args, 0, python::pass_instance (this));
+ }
+
+ return in.success ();
+}
+
Index: event/listener_python.h
===================================================================
RCS file: event/listener_python.h
diff -N event/listener_python.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ event/listener_python.h 18 Jun 2006 19:25:53 -0000 1.1
@@ -0,0 +1,137 @@
+/*
+ $Id: listener_python.h,v 1.1 2006/06/18 19:25:53 ksterker Exp $
+
+ Copyright (C) 2006 Kai Sterker <address@hidden>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * @file event/listener_python.h
+ * @author Kai Sterker <address@hidden>
+ *
+ * @brief Declares a %listener with python callback attached.
+ *
+ */
+
+#ifndef EVENT_LISTENER_PYTHON_H
+#define EVENT_LISTENER_PYTHON_H
+
+#include "event/listener.h"
+#include "python/method.h"
+
+namespace events
+{
+ /**
+ * An %event %listener that contains a callback to a %python %method it
+ * will execute when the event occurs that the %listener is waiting
+ * for. A %listener is usually created by a certain %event %factory and
will
+ * be destroyed if the %factory is destroyed. That way, it's not neccessary
+ * to keep track of every single %listener. Instead, a few factories can
+ * be used to create groups of listeners that can be disposed together.
+ */
+ class listener_python : public listener
+ {
+ public:
+ /**
+ * Create a new listener, which listens to the given event.
+ * @param f the factory that created the listener or NULL
+ * @param e the event that will trigger the attached callback
+ */
+ listener_python (factory *f, event *e);
+
+ /**
+ * Destroy the %listener. Automatically removes it from
+ * the %event %manager and %factory if required.
+ */
+ virtual ~listener_python ();
+
+ /**
+ * @name Event handling
+ */
+ //@{
+ /**
+ * Sets a python method to be executed whenever an
+ * %event occurs that this listener listens to.
+ *
+ * @param file Name of the script to load.
+ * @param classname Name of the class containing the callback.
+ * @param callback Name of the method to call.
+ * @param args Additional arguments to pass to the callback.
+ * @return \b false if connecting the callback failed, \b true
otherwise.
+ */
+ bool connect_callback (const string & file, const string & classname,
+ const string & callback, PyObject *args
= NULL);
+
+#ifndef SWIG
+ /**
+ * Sets a C++ method to be executed whenever an
+ * %event occurs that this listener listens to.
+ *
+ * @param callback The method to call.
+ */
+ void connect_callback (base::functor_0 * callback);
+#endif // SWIG
+
+ /**
+ * Execute the associated python script or callback.
+ *
+ * @param evnt The %event that triggered the execution.
+ * @return The number of times the %event needs to be repeated.
+ */
+ s_int32 raise_event (const event* evnt);
+ //@}
+
+ /**
+ * @name Loading / Saving
+ */
+ //@{
+ /**
+ * Flattens the %event %listener to the given stream.
+ *
+ * @param out stream where to add the %event %listener.
+ */
+ virtual void put_state (base::flat& out) const;
+
+ /**
+ * Loads the %event %listener from given stream.
+ *
+ * @param in stream to load the %event %listener from.
+ */
+ virtual bool get_state (base::flat& in);
+ //@}
+
+#ifndef SWIG
+ /**
+ * Allow %python %listener to be passed as python argument
+ */
+ GET_TYPE_NAME (events::listener_python)
+#endif // SWIG
+
+ private:
+ /**
+ * The callback connected to this %listener
+ */
+ python::method *Method;
+
+ /**
+ * Arguments to pass to the method
+ */
+ PyObject *Args;
+ };
+}
+
+#endif // EVENT_LISTENER_PYTHON_H
Index: rpg/schedule_data.cc
===================================================================
RCS file: rpg/schedule_data.cc
diff -N rpg/schedule_data.cc
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ rpg/schedule_data.cc 18 Jun 2006 19:25:53 -0000 1.1
@@ -0,0 +1,66 @@
+/*
+ $Id: schedule_data.cc,v 1.1 2006/06/18 19:25:53 ksterker Exp $
+
+ Copyright (C) 2004/2005/2006 Kai Sterker <address@hidden>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+* @file rpg/schedule_data.cc
+ *
+ * @author Kai Sterker
+ * @brief Implements the %schedule data class.
+ */
+
+#include "rpg/schedule.h"
+#include "rpg/schedule_data.h"
+
+using rpg::schedule;
+using rpg::schedule_data;
+
+// activate queued schedule
+bool schedule_data::activate (schedule *s)
+{
+ if (!s->set_schedule (File, Args))
+ {
+ fprintf (stderr, "*** schedule_data::activate: activation of queued
schedule '%s' failed!\n", File.c_str ());
+ return false;
+ }
+
+ if (Time != "") s->set_alarm (Time, Absolute);
+ return true;
+}
+
+// save to stream
+void schedule_data::put_state (base::flat& out) const
+{
+ out.put_string ("qsf", File);
+ python::put_tuple (Args, out);
+ out.put_string ("qst", Time);
+ out.put_bool ("qsa", Absolute);
+}
+
+// load from stream
+bool schedule_data::get_state (base::flat & in)
+{
+ File = in.get_string ("qsf");
+ Args = python::get_tuple (in);
+ Time = in.get_string ("qst");
+ Absolute = in.get_bool ("qsa");
+
+ return in.success();
+}
\ No newline at end of file
Index: rpg/schedule_data.h
===================================================================
RCS file: rpg/schedule_data.h
diff -N rpg/schedule_data.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ rpg/schedule_data.h 18 Jun 2006 19:25:53 -0000 1.1
@@ -0,0 +1,137 @@
+/*
+ $Id: schedule_data.h,v 1.1 2006/06/18 19:25:53 ksterker Exp $
+
+ Copyright (C) 2004/2005/2006 Kai Sterker <address@hidden>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+/**
+ * @file rpg/schedule_data.h
+ *
+ * @author Kai Sterker
+ * @brief Declares the schedule_data class.
+ */
+
+#ifndef RPG_SCHEDULE_DATA_H
+#define RPG_SCHEDULE_DATA_H
+
+#include <Python.h>
+#include <string>
+
+namespace rpg
+{
+ class schedule;
+
+ /**
+ * Data needed to queue a schedule. Only used by the schedule
+ * class internally.
+ */
+ class schedule_data
+ {
+ public:
+ /**
+ * Default constructor.
+ */
+ schedule_data ()
+ {
+ Time = "";
+ Absolute = false;
+ }
+
+ /**
+ * Create new schedule data.
+ * @param file schedule file name.
+ * @param args arguments for schedule constructor.
+ */
+ schedule_data (const std::string & file, PyObject *args)
+ {
+ Py_XINCREF (args);
+
+ File = file;
+ Args = args;
+ Time = "";
+ Absolute = false;
+ }
+
+ /**
+ * Free arguments.
+ */
+ ~schedule_data ()
+ {
+ Py_XDECREF (Args);
+ }
+
+ /**
+ * Set active schedule from the stored values.
+ * @param s schedule to update from data.
+ * @return true on success, false otherwise.
+ */
+ bool activate (schedule *s);
+
+ /**
+ * Specify a period after which the manager script is run,
+ * whether the active %schedule is finished or not. This function
+ * makes use of the %event system and is much more efficient than
+ * querying the current time within the %schedule itself. It
+ * therefore is the preferred method of letting schedules run a
+ * fixed amount of gametime.
+ *
+ * @param time The amount of time the %schedule should be active,
+ * in the format used by time_event::time_event ().
+ * @param absolute If <b>true</b>, the current day will be added
+ * to the time. For example, set_alarm ("14h", true) means
+ * "today at 14:00", whereas set_alarm ("14h", false) means
+ * "14 hours from now on".
+ */
+ void set_alarm (const std::string & time, const bool & absolute =
false)
+ {
+ Time = time;
+ Absolute = absolute;
+ }
+
+ /**
+ * @name Loading / Saving
+ */
+ //@{
+ /**
+ * Save the %schedule to a stream.
+ * @param out stream where to save the %schedule.
+ */
+ void put_state (base::flat& out) const;
+
+ /**
+ * Loads the %schedule from a stream.
+ * @param in stream to load the %schedule from.
+ * @return \e true if the %schedule was loaded successfully, \e false
otherwise.
+ */
+ bool get_state (base::flat& in);
+ //@}
+
+ private:
+ /// name of schedule script
+ std::string File;
+ /// arguments for schedule script constructor
+ PyObject *Args;
+ /// alarm time for schedule script
+ std::string Time;
+ /// whether alarm time is relative of absolute
+ bool Absolute;
+ };
+} // namespace rpg
+
+#endif // RPG_SCHEDULE_DATA_H
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Adonthell-commits] adonthell/src basediskio.cc basediskio.h basefl...,
Kai Sterker <=