From 4559ca444daacfd02ebb05f1657148a2b4cf3d8b Mon Sep 17 00:00:00 2001 From: Erik Schilling Date: Wed, 20 Feb 2013 22:46:55 +0100 Subject: Introduced Script::Context This should allow to finally call functions to lua without having to care about working around situations where a lua call causes a c++ call which needs to call to lua again. Tested against the source of tales repository data. --- src/scripting/script.h | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) (limited to 'src/scripting/script.h') diff --git a/src/scripting/script.h b/src/scripting/script.h index 8dee23a..574d1b9 100644 --- a/src/scripting/script.h +++ b/src/scripting/script.h @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -40,6 +41,15 @@ class Entity; class Script : public sigc::trackable { public: + struct Context + { + MapComposite *map; + + Context() + : map(0) + {} + }; + /** * Defines a function that creates a Script instance. */ @@ -87,9 +97,12 @@ class Script : public sigc::trackable Thread(Script *script); virtual ~Thread(); + Context &getContext() + { return mContext; } + Script * const mScript; ThreadState mState; - MapComposite *mMap; + Context mContext; }; Script(); @@ -102,14 +115,18 @@ class Script : public sigc::trackable * * @param prog the program text to load * @param name the name of the text, used for error reporting + * @param context the context that is supposed to be used for loading */ - virtual void load(const char *prog, const char *name) = 0; + virtual void load(const char *prog, + const char *name, + const Context &context = Context()) = 0; /** * Loads a text file into script context and executes its global * statements. */ - virtual bool loadFile(const std::string &); + virtual bool loadFile(const std::string &, + const Context &context = Context()); /** * Loads a chunk of text and considers it as an NPC handler. This @@ -119,7 +136,8 @@ class Script : public sigc::trackable int id, ManaServ::BeingGender gender, int x, int y, - const char *); + const char *, + MapComposite *map); /** * Called every tick for the script to manage its data. @@ -174,9 +192,17 @@ class Script : public sigc::trackable /** * Executes the function being prepared. + * @param context the context that is supposed to be used for executing * @return the value returned by the script. */ - virtual int execute() = 0; + virtual int execute(const Context &context = Context()) = 0; + + /** + * Executes the function being prepared. + * @param the map which is set as context + * @return the value returned by the script. + */ + int execute(MapComposite *map); /** * Starts or resumes the current thread. Deletes the thread when it is @@ -207,16 +233,10 @@ class Script : public sigc::trackable { return mCurrentThread; } /** - * Sets associated map. - */ - void setMap(MapComposite *m) - { mMap = m; } - - /** - * Gets associated map. + * Returns the current context. */ - MapComposite *getMap() const - { return mMap; } + const Context *getContext() const + { return mContext; } virtual void processDeathEvent(Being *entity) = 0; @@ -231,9 +251,9 @@ class Script : public sigc::trackable protected: std::string mScriptFile; Thread *mCurrentThread; + const Context *mContext; private: - MapComposite *mMap; std::vector mThreads; static Ref mCreateNpcDelayedCallback; -- cgit