[1383] | 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: |
---|
[7404] | 25 | * Damian 'Mozork' Frick |
---|
[1383] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "Script.h" |
---|
[1906] | 30 | |
---|
[7404] | 31 | #include "core/command/CommandExecutor.h" |
---|
[1383] | 32 | #include "core/CoreIncludes.h" |
---|
[7404] | 33 | #include "core/EventIncludes.h" |
---|
[7463] | 34 | #include "core/GameMode.h" |
---|
[5695] | 35 | #include "core/LuaState.h" |
---|
| 36 | #include "core/XMLPort.h" |
---|
[1383] | 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[5695] | 40 | CreateFactory(Script); |
---|
[1383] | 41 | |
---|
[7404] | 42 | // Initializing constants. |
---|
| 43 | /*static*/ const std::string Script::NORMAL = "normal"; |
---|
| 44 | /*static*/ const std::string Script::LUA = "lua"; |
---|
[7406] | 45 | /*static*/ const int Script::INF = -1; |
---|
[7404] | 46 | |
---|
| 47 | /** |
---|
| 48 | @brief |
---|
| 49 | Constructor. Registers and initializes the object. |
---|
| 50 | @param creator |
---|
| 51 | The creator of this object. |
---|
| 52 | */ |
---|
[5695] | 53 | Script::Script(BaseObject* creator) : BaseObject(creator) |
---|
| 54 | { |
---|
| 55 | RegisterObject(Script); |
---|
[1383] | 56 | |
---|
[7404] | 57 | // Initialize variables. |
---|
| 58 | this->luaState_ = NULL; |
---|
| 59 | this->remainingExecutions_ = Script::INF; |
---|
[7463] | 60 | this->mode_ = ScriptMode::normal; |
---|
| 61 | this->onLoad_ = true; |
---|
| 62 | this->times_ = Script::INF; |
---|
| 63 | this->needsGraphics_ = false; |
---|
[7404] | 64 | |
---|
[5695] | 65 | } |
---|
[1383] | 66 | |
---|
[7404] | 67 | /** |
---|
| 68 | @brief |
---|
| 69 | Destructor. Cleans up. |
---|
| 70 | */ |
---|
[5695] | 71 | Script::~Script() |
---|
| 72 | { |
---|
[7404] | 73 | if(this->isInitialized() && this->luaState_ != NULL) |
---|
| 74 | delete this->luaState_; |
---|
[5695] | 75 | } |
---|
[1383] | 76 | |
---|
[7404] | 77 | /** |
---|
| 78 | @brief |
---|
| 79 | Method for creating a Script object through XML. |
---|
[7407] | 80 | @param xmlelement |
---|
[7404] | 81 | The element. |
---|
| 82 | @param mode |
---|
| 83 | The mode. |
---|
| 84 | */ |
---|
[7407] | 85 | void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[5695] | 86 | { |
---|
[7408] | 87 | SUPER(Script, XMLPort, xmlelement, mode); |
---|
[1383] | 88 | |
---|
[7407] | 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); |
---|
[7463] | 93 | XMLPortParam(Script, "needsGraphics", setNeedsGraphics, getNeedsGraphics, xmlelement, mode).defaultValues(false); |
---|
[7404] | 94 | |
---|
[7407] | 95 | XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlelement, mode); |
---|
[7404] | 96 | |
---|
| 97 | if(this->isOnLoad()) // If the object is onLoad the code is executed at once. |
---|
| 98 | this->execute(); |
---|
[5695] | 99 | } |
---|
[1383] | 100 | |
---|
[7404] | 101 | /** |
---|
| 102 | @brief |
---|
| 103 | Creates a port that can be used to channel events and react to them. |
---|
[7407] | 104 | @param xmlelement |
---|
[7404] | 105 | The element. |
---|
| 106 | @param mode |
---|
| 107 | The mode. |
---|
| 108 | */ |
---|
[7407] | 109 | void Script::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[7404] | 110 | { |
---|
[7408] | 111 | SUPER(Script, XMLEventPort, xmlelement, mode); |
---|
[7404] | 112 | |
---|
[7407] | 113 | XMLPortEventState(Script, BaseObject, "trigger", trigger, xmlelement, mode); |
---|
[7404] | 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 | */ |
---|
[5695] | 132 | void Script::execute() |
---|
| 133 | { |
---|
[7404] | 134 | if(this->times_ != Script::INF && this->remainingExecutions_ == 0) |
---|
| 135 | return; |
---|
| 136 | |
---|
[7463] | 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 | |
---|
[7404] | 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_--; |
---|
[5695] | 151 | } |
---|
[7404] | 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; |
---|
[7410] | 191 | default: // This will never happen... |
---|
| 192 | return Script::NORMAL; |
---|
[7404] | 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 | |
---|
[1383] | 217 | } |
---|