Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Removing some debug output and come minor cleaning up.

  • Property svn:eol-style set to native
File size: 8.7 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        Damian 'Mozork' Frick
84    */
85    class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener
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            bool trigger(bool triggered, BaseObject* trigger); //!< Is called when an event comes in trough the event port.
95            void execute(unsigned int clientId, bool fromCallback = false); //!< Executes the Scripts code for the input client, depending on the mode.
96            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.
97
98            /**
99            @brief Sets the code that is executed by this Script.
100            @param code  The code that is executed by this Script.
101            */
102            inline void setCode(const std::string& code)
103                { code_ = code; }
104            /**
105            @brief Get the code that is executed by this Script.
106            @return Returns the code that is executed by this Script.
107            */
108            inline const std::string& getCode() const
109                { return code_; }
110
111            void setMode(const std::string& mode); //!< Sets the mode of the Script.
112            const std::string& getMode(void); //!< Get the mode of the Script.
113
114            /**
115            @brief Set whether this Script is executed onLoad or not.
116            @param onLoad if true the Script is executed onLoad, if false it's not.
117            */
118            inline void setOnLoad(bool onLoad)
119                { this->onLoad_ = onLoad; }
120            /**
121            @brief Get whether this Script is executed onLoad.
122            @return Returns true if this Script is executed onLoad, false if not.
123            */
124            inline bool isOnLoad(void)
125                { return this->onLoad_; }
126
127            void setTimes(int times); //!< Set the number of times this Script is executed at the most.
128            /**
129            @brief Get the number of times this Script is executed at the most.
130            @return Returns the number of times this Script is executed at the most. -1 denotes infinity.
131            */
132            inline int getTimes(void)
133                { return this->times_; }
134
135            /**
136            @brief Set whether the code to be executed needs graphics to work.
137            @param needsGraphics True if the cde needs graphics to be executed properly.
138            */
139            void setNeedsGraphics(bool needsGraphics)
140                { this->needsGraphics_ = needsGraphics; }
141            /**
142            @brief Get whether the code to be executed needs graphics to work.
143            @return Returns true if the code needs graphic, false if not.
144            */
145            bool getNeedsGraphics(void)
146                { return this->needsGraphics_; }
147
148            /**
149            @brief Set whether the code is executed for all players or just for the player triggering the Script.
150            @param forAll If true the code is executed for all players.
151            */
152            void setForAll(bool forAll)
153                { this->forAll_ = forAll; }
154            /**
155            @brief Get whether the Script executes its code for all players or just for the player triggering the Script.
156            @return Returns true if the code is executed for all players, false if not.
157            */
158            bool isForAll(void)
159                { return this->forAll_; }
160
161            virtual void clientConnected(unsigned int clientId); //!< Callback that is called when a new client has connected.
162            virtual void clientDisconnected(unsigned int clientid) {}
163
164        private:
165            //! Static variables to avoid magic strings.
166            static const std::string NORMAL;
167            static const std::string LUA;
168            static const int INF;
169
170            std::string code_; //!< The code that is executed by this Script.
171            ScriptMode::Value mode_; //!< The mode the Script is in. Determines whether the code is executed the normal way or in lua.
172            std::string modeStr_; //!< The mode the Script is in, as a string. Is used for networking purposes.
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            int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity.
179
180            void modeChanged(); //!< Sets the mode to the mode specified in this->modeStr_.
181
182            /**
183            @brief Sets the mode of the Script.
184            @param mode The mode of the Script.
185            */
186            inline void setMode(ScriptMode::Value mode)
187                { this->mode_ = mode; }
188    };
189}
190
191#endif /* _Script_H__ */
Note: See TracBrowser for help on using the repository browser.