Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/Script.h @ 7484

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

Doing some documentation.

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
1
2/*
3 *   ORXONOX - the hottest 3D action shooter ever to exist
4 *                    > www.orxonox.net <
5 *
6 *
7 *   License notice:
8 *
9 *   This program is free software; you can redistribute it and/or
10 *   modify it under the terms of the GNU General Public License
11 *   as published by the Free Software Foundation; either version 2
12 *   of the License, or (at your option) any later version.
13 *
14 *   This program is distributed in the hope that it will be useful,
15 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *   GNU General Public License for more details.
18 *
19 *   You should have received a copy of the GNU General Public License
20 *   along with this program; if not, write to the Free Software
21 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 *
23 *   Author:
24 *      Benjamin Knecht
25 *   Co-authors:
26 *      Damian 'Mozork' Frick
27 *
28 */
29
30#ifndef _Script_H__
31#define _Script_H__
32
33#include "objects/ObjectsPrereqs.h"
34
35#include <string>
36#include <vector>
37
38#include "core/BaseObject.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    @author
84        Damian 'Mozork' Frick
85    */
86    class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener
87    {
88        public:
89            Script(BaseObject* creator);
90            virtual ~Script();
91
92            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a Script object through XML.
93            virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them.
94
95            bool trigger(bool triggered, BaseObject* trigger); //!< Is called when an event comes in trough the event port.
96            void execute(unsigned int clientId, bool fromCallback = false); //!< Executes the Scripts code for the input client, depending on the mode.
97            static void executeHelper(const std::string& code, const std::string& mode, bool needsGraphics); //!< Helper method that is used to reach this Script object on other clients.
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); //!< Callback that is called when a new client has connected.
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            std::string modeStr_; //!< The mode the Script is in, as a string. Is used for networking purposes.
174            bool onLoad_; //!< Whether the Scripts code is executed upon loading (creation) of this Script.
175            int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity.
176            bool needsGraphics_; //!< Whether the code to be executed needs graphics.
177            bool forAll_; //!< Whether the code is executed for all players (in a multiplayer setup) or just for the one triggering the Script.
178
179            int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity.
180
181            void modeChanged(); //!< Sets the mode to the mode specified in this->modeStr_.
182
183            /**
184            @brief Sets the mode of the Script.
185            @param mode The mode of the Script.
186            */
187            inline void setMode(ScriptMode::Value mode)
188                { this->mode_ = mode; }
189    };
190}
191
192#endif /* _Script_H__ */
Note: See TracBrowser for help on using the repository browser.