[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" |
---|
[5695] | 34 | #include "core/LuaState.h" |
---|
| 35 | #include "core/XMLPort.h" |
---|
[1383] | 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
[5695] | 39 | CreateFactory(Script); |
---|
[1383] | 40 | |
---|
[7404] | 41 | // Initializing constants. |
---|
| 42 | /*static*/ const std::string Script::NORMAL = "normal"; |
---|
| 43 | /*static*/ const std::string Script::LUA = "lua"; |
---|
[7406] | 44 | /*static*/ const int Script::INF = -1; |
---|
[7404] | 45 | |
---|
| 46 | /** |
---|
| 47 | @brief |
---|
| 48 | Constructor. Registers and initializes the object. |
---|
| 49 | @param creator |
---|
| 50 | The creator of this object. |
---|
| 51 | */ |
---|
[5695] | 52 | Script::Script(BaseObject* creator) : BaseObject(creator) |
---|
| 53 | { |
---|
| 54 | RegisterObject(Script); |
---|
[1383] | 55 | |
---|
[7404] | 56 | // Initialize variables. |
---|
| 57 | this->luaState_ = NULL; |
---|
| 58 | this->remainingExecutions_ = Script::INF; |
---|
| 59 | |
---|
[5695] | 60 | } |
---|
[1383] | 61 | |
---|
[7404] | 62 | /** |
---|
| 63 | @brief |
---|
| 64 | Destructor. Cleans up. |
---|
| 65 | */ |
---|
[5695] | 66 | Script::~Script() |
---|
| 67 | { |
---|
[7404] | 68 | if(this->isInitialized() && this->luaState_ != NULL) |
---|
| 69 | delete this->luaState_; |
---|
[5695] | 70 | } |
---|
[1383] | 71 | |
---|
[7404] | 72 | /** |
---|
| 73 | @brief |
---|
| 74 | Method for creating a Script object through XML. |
---|
[7407] | 75 | @param xmlelement |
---|
[7404] | 76 | The element. |
---|
| 77 | @param mode |
---|
| 78 | The mode. |
---|
| 79 | */ |
---|
[7407] | 80 | void Script::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[5695] | 81 | { |
---|
[7408] | 82 | SUPER(Script, XMLPort, xmlelement, mode); |
---|
[1383] | 83 | |
---|
[7407] | 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); |
---|
[7404] | 88 | |
---|
[7407] | 89 | XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlelement, mode); |
---|
[7404] | 90 | |
---|
| 91 | if(this->isOnLoad()) // If the object is onLoad the code is executed at once. |
---|
| 92 | this->execute(); |
---|
[5695] | 93 | } |
---|
[1383] | 94 | |
---|
[7404] | 95 | /** |
---|
| 96 | @brief |
---|
| 97 | Creates a port that can be used to channel events and react to them. |
---|
[7407] | 98 | @param xmlelement |
---|
[7404] | 99 | The element. |
---|
| 100 | @param mode |
---|
| 101 | The mode. |
---|
| 102 | */ |
---|
[7407] | 103 | void Script::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[7404] | 104 | { |
---|
[7408] | 105 | SUPER(Script, XMLEventPort, xmlelement, mode); |
---|
[7404] | 106 | |
---|
[7407] | 107 | XMLPortEventState(Script, BaseObject, "trigger", trigger, xmlelement, mode); |
---|
[7404] | 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 | */ |
---|
[5695] | 126 | void Script::execute() |
---|
| 127 | { |
---|
[7404] | 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_--; |
---|
[5695] | 141 | } |
---|
[7404] | 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; |
---|
[7410] | 181 | default: // This will never happen... |
---|
| 182 | return Script::NORMAL; |
---|
[7404] | 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 | |
---|
[1383] | 207 | } |
---|