Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/objects/Script.cc @ 7444

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

Removing some warnings (both compiler and doxygen).

  • Property svn:eol-style set to native
File size: 5.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#include "Script.h"
30
31#include "core/command/CommandExecutor.h"
32#include "core/CoreIncludes.h"
33#include "core/EventIncludes.h"
34#include "core/LuaState.h"
35#include "core/XMLPort.h"
36
37namespace orxonox
38{
39    CreateFactory(Script);
40
41    // Initializing constants.
42    /*static*/ const std::string Script::NORMAL = "normal";
43    /*static*/ const std::string Script::LUA = "lua";
44    /*static*/ const int Script::INF = -1;
45
46    /**
47    @brief
48        Constructor. Registers and initializes the object.
49    @param creator
50        The creator of this object.
51    */
52    Script::Script(BaseObject* creator) : BaseObject(creator)
53    {
54        RegisterObject(Script);
55
56        // Initialize variables.
57        this->luaState_ = NULL;
58        this->remainingExecutions_ = Script::INF;
59
60    }
61
62    /**
63    @brief
64        Destructor. Cleans up.
65    */
66    Script::~Script()
67    {
68        if(this->isInitialized() && this->luaState_ != NULL)
69            delete this->luaState_;
70    }
71
72    /**
73    @brief
74        Method for creating a Script object through XML.
75    @param xmlelement
76        The element.
77    @param mode
78        The mode.
79    */
80    void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode)
81    {
82        SUPER(Script, XMLPort, xmlelement, mode);
83
84        XMLPortParam(Script, "code", setCode, getCode, xmlelement, mode);
85        XMLPortParamTemplate(Script, "mode", setMode, getMode, xmlelement, mode, const std::string&).defaultValues(Script::NORMAL);
86        XMLPortParam(Script, "onLoad", setOnLoad, isOnLoad, xmlelement, mode).defaultValues(true);
87        XMLPortParam(Script, "times", setTimes, getTimes, xmlelement, mode).defaultValues(Script::INF);
88
89        XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlelement, mode);
90
91        if(this->isOnLoad()) // If the object is onLoad the code is executed at once.
92            this->execute();
93    }
94
95    /**
96    @brief
97        Creates a port that can be used to channel events and react to them.
98    @param xmlelement
99        The element.
100    @param mode
101        The mode.
102    */
103    void Script::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
104    {
105        SUPER(Script, XMLEventPort, xmlelement, mode);
106
107        XMLPortEventState(Script, BaseObject, "trigger", trigger, xmlelement, mode);
108    }
109
110    /**
111    @brief
112        Is called when an event comes in trough the event port.
113    @param triggered
114        Whether the event is triggering or un-triggering.
115    */
116    void Script::trigger(bool triggered)
117    {
118        if(triggered) // If the event is triggering (instead of un-triggering) the code of this Script  is executed.
119            this->execute();
120    }
121
122    /**
123    @brief
124        Executes the Scripts code, depending on the mode.
125    */
126    void Script::execute()
127    {
128        if(this->times_ != Script::INF && this->remainingExecutions_ == 0)
129            return;
130
131        if(this->mode_ == ScriptMode::normal) // If the mode is 'normal'.
132            CommandExecutor::execute(this->code_);
133        else if(this->mode_ == ScriptMode::lua) // If it's 'lua'.
134        {
135            assert(this->luaState_);
136            this->luaState_->doString(this->code_);
137        }
138
139        if(this->times_ != Script::INF)
140            this->remainingExecutions_--;
141    }
142
143    /**
144    @brief
145        Sets the mode of the Script.
146    @param mode
147        The mode as a string.
148    */
149    void Script::setMode(const std::string& mode)
150    {
151        if(mode == Script::NORMAL)
152            this->setMode(ScriptMode::normal);
153        else if(mode == Script::LUA)
154        {
155            this->setMode(ScriptMode::lua);
156            // Creates a new LuaState.
157            if(this->luaState_ == NULL)
158                this->luaState_ = new LuaState();
159        }
160        else
161        {
162            COUT(2) << "Invalid mode '" << mode << "' in Script object." << std::endl;
163            this->setMode(ScriptMode::normal);
164        }
165    }
166
167    /**
168    @brief
169        Get the mode of the Script.
170    @return
171        Returns the mode as a string.
172    */
173    const std::string& Script::getMode(void)
174    {
175        switch(this->mode_)
176        {
177            case ScriptMode::normal:
178                return Script::NORMAL;
179            case ScriptMode::lua:
180                return Script::LUA;
181            default: // This will never happen...
182                return Script::NORMAL;
183        }
184    }
185
186    /**
187    @brief
188        Set the number of times this Script is executed at the most.
189        -1 denotes infinity.
190    @param times
191        The number of times to be set.
192    */
193    void Script::setTimes(int times)
194    {
195        if(times >= -1)
196        {
197            this->times_ = times;
198            this->remainingExecutions_ = times;
199        }
200        else
201        {
202            COUT(2) << "Invalid times '" << times << "' in Script." << std::endl;
203            this->times_ = Script::INF;
204        }
205    }
206
207}
Note: See TracBrowser for help on using the repository browser.