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