Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/lastmanstanding/src/modules/objects/Script.h @ 7693

Last change on this file since 7693 was 7474, checked in by dafrick, 14 years ago

Synchronizing Notifications.
In the course of that, notifications are not longer sent by creating a Notification and the calling notification.send() bur by letting the NotificationManager handle all this: NotificationManager::getInstance().sendNotification(message)
This made QuestNotification obsolete, thus it was removde.

Also did some work on synchronizing the Script class. It should work properly most of the time, but the current solution is unreliable and unsatisfactory. So this will change as soon as I know how.

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Benjamin Knecht
24 *   Co-authors:
25 *      Damian 'Mozork' Frick
26 *
27 */
28
29#ifndef _Script_H__
30#define _Script_H__
31
32#include "objects/ObjectsPrereqs.h"
33
34#include <string>
35#include <vector>
36
37#include "core/BaseObject.h"
38#include "tools/interfaces/Tickable.h"
39#include "network/synchronisable/Synchronisable.h"
40#include "network/ClientConnectionListener.h"
41
42namespace orxonox
43{
44
45    namespace ScriptMode
46    {
47        //! Modes of the Script class.
48        enum Value
49        {
50            normal,
51            lua
52        };
53    }
54
55    /**
56    @brief
57        The Script class lets you execute a piece of code, either the normal way or in lua, through XML. It can be specified whether the code is executed upon loading (creation) of the object. Additionally the code is executed each time a trigger event comes in.
58        There are three parameters:
59        'code': The code that should be executed.
60        'mode': The mode, specifying whether the set code should be executed the normal way ('normal') or in lua ('lua'). Default is 'normal'.
61        'onLoad': Whether the code is executed upon loading (creation) of this object. If this is set the code is executed ofr all players, regardless of the value of parameter 'forAll'. Default is true.
62        'needsGraphics': Whether the code needs graphics to be executed or not. Default is false.
63        'forAll': Whether the code is executed for all players each time the Script is triggered or jut for the player triggering the Script. If forAll is false, which is default, the event that triggers the Script must come from a PlayerTrigger.
64
65        Here are two examples illustrating the usage:
66        @code
67        <Script code="showGUI QuestGUI" needsGraphics=true />
68        @endcode
69        This would show the QuestGUI opon creation of the object. The mode is 'normal', not specified here since that is the default, also onLoad is true, also not specified, since it is the default as well. Also needsGraphics is set to true because showGUI needs graphics to work.
70
71        @code
72        <Script code="hideGUI QuestGUI" mode="normal" onLoad="false" needsGraphics=true >
73            <events>
74                <trigger>
75                    <DistanceTrigger distance=10 target="Pawn" />
76                </trigger>
77            </events>
78        </Script>
79        @endcode
80        This would hide the QuestGUI as soon as a Pawn got in range of the DistanceTrigger. The mode is 'normal', it is specified here, but could be ommitted as well, since it is the default. OnLoad is false, that is why it can't be ommitted. Also needsGraphics is set to true because showGUI needs graphics to work.
81    @author
82        Benjamin Knecht
83        Damian 'Mozork' Frick
84    */
85    class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener, public Tickable
86    {
87        public:
88            Script(BaseObject* creator);
89            virtual ~Script();
90
91            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a Script object through XML.
92            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them.
93
94            virtual void tick(float dt);
95
96            bool trigger(bool triggered, BaseObject* trigger); //!< Is called when an event comes in trough the event port.
97            void execute(unsigned int clientId, bool fromCallback = false); //!< Executes the Scripts code for the input client, depending on the mode.
98
99            /**
100            @brief Sets the code that is executed by this Script.
101            @param code  The code that is executed by this Script.
102            */
103            inline void setCode(const std::string& code)
104                { code_ = code; }
105            /**
106            @brief Get the code that is executed by this Script.
107            @return Returns the code that is executed by this Script.
108            */
109            inline const std::string& getCode() const
110                { return code_; }
111
112            void setMode(const std::string& mode); //!< Sets the mode of the Script.
113            const std::string& getMode(void); //!< Get the mode of the Script.
114
115            /**
116            @brief Set whether this Script is executed onLoad or not.
117            @param onLoad if true the Script is executed onLoad, if false it's not.
118            */
119            inline void setOnLoad(bool onLoad)
120                { this->onLoad_ = onLoad; }
121            /**
122            @brief Get whether this Script is executed onLoad.
123            @return Returns true if this Script is executed onLoad, false if not.
124            */
125            inline bool isOnLoad(void)
126                { return this->onLoad_; }
127
128            void setTimes(int times); //!< Set the number of times this Script is executed at the most.
129            /**
130            @brief Get the number of times this Script is executed at the most.
131            @return Returns the number of times this Script is executed at the most. -1 denotes infinity.
132            */
133            inline int getTimes(void)
134                { return this->times_; }
135
136            /**
137            @brief Set whether the code to be executed needs graphics to work.
138            @param needsGraphics True if the cde needs graphics to be executed properly.
139            */
140            void setNeedsGraphics(bool needsGraphics)
141                { this->needsGraphics_ = needsGraphics; }
142            /**
143            @brief Get whether the code to be executed needs graphics to work.
144            @return Returns true if the code needs graphic, false if not.
145            */
146            bool getNeedsGraphics(void)
147                { return this->needsGraphics_; }
148
149            /**
150            @brief Set whether the code is executed for all players or just for the player triggering the Script.
151            @param forAll If true the code is executed for all players.
152            */
153            void setForAll(bool forAll)
154                { this->forAll_ = forAll; }
155            /**
156            @brief Get whether the Script executes its code for all players or just for the player triggering the Script.
157            @return Returns true if the code is executed for all players, false if not.
158            */
159            bool isForAll(void)
160                { return this->forAll_; }
161
162            virtual void clientConnected(unsigned int clientId);
163            virtual void clientDisconnected(unsigned int clientid) {}
164
165        private:
166            //! Static variables to avoid magic strings.
167            static const std::string NORMAL;
168            static const std::string LUA;
169            static const int INF;
170
171            std::string code_; //!< The code that is executed by this Script.
172            ScriptMode::Value mode_; //!< The mode the Script is in. Determines whether the code is executed the normal way or in lua.
173            bool onLoad_; //!< Whether the Scripts code is executed upon loading (creation) of this Script.
174            int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity.
175            bool needsGraphics_; //!< Whether the code to be executed needs graphics.
176            bool forAll_; //!< Whether the code is executed for all players (in a multiplayer setup) or just for the one triggering the Script.
177
178            std::string modeStr_;
179
180            std::vector<unsigned int> clientCallbacks_;
181            float counter_;
182
183            LuaState* luaState_; //!< The LuaState to execute the code in lua.
184            int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity.
185
186            void registerVariables(void);
187            void modeChanged();
188
189            /**
190            @brief Sets the mode of the Script.
191            @param mode The mode of the Script.
192            */
193            inline void setMode(ScriptMode::Value mode)
194                { this->mode_ = mode; }
195    };
196}
197
198#endif /* _Script_H__ */
Note: See TracBrowser for help on using the repository browser.