summaryrefslogtreecommitdiff
path: root/src/scripting/script.h
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-09 21:30:42 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-10 18:07:29 +0100
commit78c912fb4007c3e5f0b43de02646772acb21ecf2 (patch)
treee45409ff061de75b0e4273763a87f5a25de6a65b /src/scripting/script.h
parent2fa7d1f39b24714ee6dc72b6b9e61ec5a1997724 (diff)
downloadmanaserv-78c912fb4007c3e5f0b43de02646772acb21ecf2.tar.gz
manaserv-78c912fb4007c3e5f0b43de02646772acb21ecf2.tar.bz2
manaserv-78c912fb4007c3e5f0b43de02646772acb21ecf2.tar.xz
manaserv-78c912fb4007c3e5f0b43de02646772acb21ecf2.zip
Moved the managing of NPC script coroutines into C++
Rather than wrapping NPC functions up in coroutines in the Lua side, they are now managed on the C++ side as "script threads", which are essentially the same thing. The main purpose is that the server can now know whether any of these long running script interactions are still active, which will probably be useful when adding the ability to reload scripts. Reviewed-by: Erik Schilling
Diffstat (limited to 'src/scripting/script.h')
-rw-r--r--src/scripting/script.h76
1 files changed, 67 insertions, 9 deletions
diff --git a/src/scripting/script.h b/src/scripting/script.h
index 2fa3a0bf..b55f204e 100644
--- a/src/scripting/script.h
+++ b/src/scripting/script.h
@@ -26,6 +26,7 @@
#include <list>
#include <string>
+#include <vector>
class MapComposite;
class Thing;
@@ -56,16 +57,40 @@ class Script
* custom initialization and a definition of valid. It also makes the
* purpose clear.
*/
- class Ref {
- public:
- Ref() : value(-1) {}
- bool isValid() const { return value != -1; }
- int value;
+ class Ref
+ {
+ public:
+ Ref() : value(-1) {}
+ bool isValid() const { return value != -1; }
+ int value;
+ };
+
+ enum ThreadState {
+ ThreadPending,
+ ThreadPaused,
+ ThreadExpectingNumber,
+ ThreadExpectingString,
+ ThreadExpectingTwoStrings
+ };
+
+ /**
+ * A script thread. Meant to be extended by the Script subclass to
+ * store additional information.
+ */
+ class Thread
+ {
+ public:
+ Thread(Script *script);
+ virtual ~Thread();
+
+ Script * const mScript;
+ ThreadState mState;
+ MapComposite *mMap;
};
Script();
- virtual ~Script() {}
+ virtual ~Script();
/**
* Loads a chunk of text into script context and executes its global
@@ -96,12 +121,28 @@ class Script
virtual void update();
/**
+ * Creates a new script thread and makes it the current one. Script
+ * threads do not execute in parallel, but they can suspend execution
+ * and be resumed later.
+ *
+ * The new thread should be prepared as usual, but instead of
+ * execute(), the resume() function should be called.
+ */
+ virtual Thread *newThread() = 0;
+
+ /**
* Prepares a call to the referenced function.
* Only one function can be prepared at once.
*/
virtual void prepare(Ref function) = 0;
/**
+ * Prepares for resuming the given script thread.
+ * Only one thread can be resumed at once.
+ */
+ virtual void prepareResume(Thread *thread) = 0;
+
+ /**
* Pushes an integer argument for the function being prepared.
*/
virtual void push(int) = 0;
@@ -120,8 +161,7 @@ class Script
virtual void push(Thing *) = 0;
/**
- * Pushes a list of items with amounts to the
- * script engine.
+ * Pushes a list of items with amounts to the script engine.
*/
virtual void push(const std::list<InventoryItem> &itemList) = 0;
@@ -132,6 +172,14 @@ class Script
virtual int execute() = 0;
/**
+ * Starts or resumes the current thread. Deletes the thread when it is
+ * done.
+ *
+ * @return whether the thread is done executing.
+ */
+ virtual bool resume() = 0;
+
+ /**
* Assigns the current callback to the given \a function.
*
* Where the callback exactly comes from is up to the script engine.
@@ -139,6 +187,13 @@ class Script
virtual void assignCallback(Ref &function) = 0;
/**
+ * Returns the currently executing thread, or null when no thread is
+ * currently executing.
+ */
+ Thread *getCurrentThread() const
+ { return mCurrentThread; }
+
+ /**
* Sets associated map.
*/
void setMap(MapComposite *m)
@@ -165,15 +220,18 @@ class Script
protected:
std::string mScriptFile;
+ Thread *mCurrentThread;
private:
MapComposite *mMap;
EventListener mEventListener; /**< Tracking of being deaths. */
+ std::vector<Thread*> mThreads;
static Ref mCreateNpcDelayedCallback;
static Ref mUpdateCallback;
friend struct ScriptEventDispatch;
+ friend class Thread;
};
struct ScriptEventDispatch: EventDispatch
@@ -188,4 +246,4 @@ struct ScriptEventDispatch: EventDispatch
static ScriptEventDispatch scriptEventDispatch;
-#endif
+#endif // SCRIPTING_SCRIPT_H