Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixing "bug", that caused crash in dedicated mode.
Script object can now specify whether the code that is executed by it needs graphics to work.

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