[847] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1056] | 3 | * > www.orxonox.net < |
---|
[847] | 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 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | * Inspiration: Executor by Benjamin Grauer |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "Executor.h" |
---|
[1062] | 31 | #include "util/Math.h" |
---|
[847] | 32 | #include "Language.h" |
---|
| 33 | |
---|
| 34 | namespace orxonox |
---|
| 35 | { |
---|
[1052] | 36 | Executor::Executor(Functor* functor, const std::string& name, AccessLevel::Level level) |
---|
[847] | 37 | { |
---|
| 38 | this->functor_ = functor; |
---|
| 39 | this->name_ = name; |
---|
[1052] | 40 | this->accessLevel_ = level; |
---|
[1349] | 41 | this->keybindMode_ = KeybindMode::OnPress; |
---|
| 42 | this->axisParamIndex_ = -1; |
---|
| 43 | this->bAxisRelative_ = false; |
---|
[1052] | 44 | |
---|
[847] | 45 | this->bAddedDescription_ = false; |
---|
| 46 | this->bAddedDescriptionReturnvalue_ = false; |
---|
[1052] | 47 | |
---|
[847] | 48 | this->bAddedDescriptionParam_[0] = false; |
---|
| 49 | this->bAddedDescriptionParam_[1] = false; |
---|
| 50 | this->bAddedDescriptionParam_[2] = false; |
---|
| 51 | this->bAddedDescriptionParam_[3] = false; |
---|
| 52 | this->bAddedDescriptionParam_[4] = false; |
---|
[1052] | 53 | |
---|
| 54 | this->bAddedDefaultValue_[0] = false; |
---|
| 55 | this->bAddedDefaultValue_[1] = false; |
---|
| 56 | this->bAddedDefaultValue_[2] = false; |
---|
| 57 | this->bAddedDefaultValue_[3] = false; |
---|
| 58 | this->bAddedDefaultValue_[4] = false; |
---|
[847] | 59 | } |
---|
| 60 | |
---|
| 61 | Executor::~Executor() |
---|
| 62 | { |
---|
[1052] | 63 | delete this->functor_; |
---|
[847] | 64 | } |
---|
| 65 | |
---|
[1052] | 66 | bool Executor::parse(const std::string& params, const std::string& delimiter) const |
---|
[847] | 67 | { |
---|
[1052] | 68 | EXECUTOR_PARSE(normal); |
---|
[847] | 69 | } |
---|
| 70 | |
---|
[1052] | 71 | bool Executor::evaluate(const std::string& params, MultiTypeMath param[5], const std::string& delimiter) const |
---|
[847] | 72 | { |
---|
[1052] | 73 | unsigned int paramCount = this->functor_->getParamCount(); |
---|
| 74 | |
---|
| 75 | if (paramCount == 1) |
---|
| 76 | { |
---|
| 77 | // only one param: check if there are params given, otherwise try to use default values |
---|
| 78 | std::string temp = getStripped(params); |
---|
| 79 | if ((temp != "") && (temp.size() != 0)) |
---|
| 80 | { |
---|
| 81 | param[0] = params; |
---|
| 82 | this->functor_->evaluateParam(0, param[0]); |
---|
| 83 | return true; |
---|
| 84 | } |
---|
| 85 | else if (this->bAddedDefaultValue_[0]) |
---|
| 86 | { |
---|
| 87 | param[0] = this->defaultValue_[0]; |
---|
| 88 | this->functor_->evaluateParam(0, param[0]); |
---|
| 89 | return true; |
---|
| 90 | } |
---|
| 91 | return false; |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | // more than one param |
---|
| 96 | SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); |
---|
| 97 | |
---|
| 98 | // if there are not enough params given, check if there are default values |
---|
| 99 | for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) |
---|
| 100 | if (!this->bAddedDefaultValue_[i]) |
---|
| 101 | return false; |
---|
| 102 | |
---|
| 103 | // assign all given arguments to the multitypes |
---|
| 104 | for (unsigned int i = 0; i < min(tokens.size(), (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++) |
---|
| 105 | param[i] = tokens[i]; |
---|
| 106 | |
---|
| 107 | // fill the remaining multitypes with default values |
---|
| 108 | for (unsigned int i = tokens.size(); i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++) |
---|
| 109 | param[i] = this->defaultValue_[i]; |
---|
| 110 | |
---|
| 111 | // evaluate the param types through the functor |
---|
| 112 | for (unsigned int i = 0; i < min(paramCount, (unsigned int)MAX_FUNCTOR_ARGUMENTS); i++) |
---|
| 113 | this->functor_->evaluateParam(i, param[i]); |
---|
| 114 | |
---|
| 115 | return true; |
---|
| 116 | } |
---|
[847] | 117 | } |
---|
| 118 | |
---|
[1052] | 119 | Executor& Executor::setDescription(const std::string& description) |
---|
[847] | 120 | { |
---|
| 121 | if (!this->bAddedDescription_) |
---|
| 122 | { |
---|
| 123 | this->description_ = std::string("ExecutorDescription::" + this->name_ + "::function"); |
---|
| 124 | AddLanguageEntry(this->description_, description); |
---|
| 125 | this->bAddedDescription_ = true; |
---|
| 126 | } |
---|
[1052] | 127 | return (*this); |
---|
[847] | 128 | } |
---|
| 129 | |
---|
| 130 | const std::string& Executor::getDescription() const |
---|
| 131 | { |
---|
| 132 | return GetLocalisation(this->description_); |
---|
| 133 | } |
---|
| 134 | |
---|
[1052] | 135 | Executor& Executor::setDescriptionParam(int param, const std::string& description) |
---|
[847] | 136 | { |
---|
[1052] | 137 | if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS) |
---|
[847] | 138 | { |
---|
| 139 | if (!this->bAddedDescriptionParam_[param]) |
---|
| 140 | { |
---|
| 141 | std::string paramnumber; |
---|
| 142 | if (!Convert::ToString(¶mnumber, param)) |
---|
[1052] | 143 | return (*this); |
---|
[847] | 144 | |
---|
| 145 | this->descriptionParam_[param] = std::string("ExecutorDescription::" + this->name_ + "::param" + paramnumber); |
---|
| 146 | AddLanguageEntry(this->descriptionParam_[param], description); |
---|
| 147 | this->bAddedDescriptionParam_[param] = true; |
---|
| 148 | } |
---|
| 149 | } |
---|
[1052] | 150 | return (*this); |
---|
[847] | 151 | } |
---|
| 152 | |
---|
| 153 | const std::string& Executor::getDescriptionParam(int param) const |
---|
| 154 | { |
---|
[1052] | 155 | if (param >= 0 && param < MAX_FUNCTOR_ARGUMENTS) |
---|
[847] | 156 | return GetLocalisation(this->descriptionParam_[param]); |
---|
| 157 | |
---|
| 158 | return this->descriptionParam_[0]; |
---|
| 159 | } |
---|
| 160 | |
---|
[1052] | 161 | Executor& Executor::setDescriptionReturnvalue(const std::string& description) |
---|
[847] | 162 | { |
---|
| 163 | if (!this->bAddedDescriptionReturnvalue_) |
---|
| 164 | { |
---|
| 165 | this->descriptionReturnvalue_ = std::string("ExecutorDescription::" + this->name_ + "::returnvalue"); |
---|
| 166 | AddLanguageEntry(this->descriptionReturnvalue_, description); |
---|
| 167 | this->bAddedDescriptionReturnvalue_ = true; |
---|
| 168 | } |
---|
[1052] | 169 | return (*this); |
---|
[847] | 170 | } |
---|
| 171 | |
---|
| 172 | const std::string& Executor::getDescriptionReturnvalue(int param) const |
---|
| 173 | { |
---|
| 174 | return GetLocalisation(this->descriptionReturnvalue_); |
---|
| 175 | } |
---|
[1052] | 176 | |
---|
| 177 | Executor& Executor::setDefaultValues(const MultiTypeMath& param1) |
---|
| 178 | { |
---|
| 179 | this->defaultValue_[0] = param1; |
---|
| 180 | this->bAddedDefaultValue_[0] = true; |
---|
| 181 | |
---|
| 182 | return (*this); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2) |
---|
| 186 | { |
---|
| 187 | this->defaultValue_[0] = param1; |
---|
| 188 | this->bAddedDefaultValue_[0] = true; |
---|
| 189 | this->defaultValue_[1] = param2; |
---|
| 190 | this->bAddedDefaultValue_[1] = true; |
---|
| 191 | |
---|
| 192 | return (*this); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3) |
---|
| 196 | { |
---|
| 197 | this->defaultValue_[0] = param1; |
---|
| 198 | this->bAddedDefaultValue_[0] = true; |
---|
| 199 | this->defaultValue_[1] = param2; |
---|
| 200 | this->bAddedDefaultValue_[1] = true; |
---|
| 201 | this->defaultValue_[2] = param3; |
---|
| 202 | this->bAddedDefaultValue_[2] = true; |
---|
| 203 | |
---|
| 204 | return (*this); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4) |
---|
| 208 | { |
---|
| 209 | this->defaultValue_[0] = param1; |
---|
| 210 | this->bAddedDefaultValue_[0] = true; |
---|
| 211 | this->defaultValue_[1] = param2; |
---|
| 212 | this->bAddedDefaultValue_[1] = true; |
---|
| 213 | this->defaultValue_[2] = param3; |
---|
| 214 | this->bAddedDefaultValue_[2] = true; |
---|
| 215 | this->defaultValue_[3] = param4; |
---|
| 216 | this->bAddedDefaultValue_[3] = true; |
---|
| 217 | |
---|
| 218 | return (*this); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | Executor& Executor::setDefaultValues(const MultiTypeMath& param1, const MultiTypeMath& param2, const MultiTypeMath& param3, const MultiTypeMath& param4, const MultiTypeMath& param5) |
---|
| 222 | { |
---|
| 223 | this->defaultValue_[0] = param1; |
---|
| 224 | this->bAddedDefaultValue_[0] = true; |
---|
| 225 | this->defaultValue_[1] = param2; |
---|
| 226 | this->bAddedDefaultValue_[1] = true; |
---|
| 227 | this->defaultValue_[2] = param3; |
---|
| 228 | this->bAddedDefaultValue_[2] = true; |
---|
| 229 | this->defaultValue_[3] = param4; |
---|
| 230 | this->bAddedDefaultValue_[3] = true; |
---|
| 231 | this->defaultValue_[4] = param5; |
---|
| 232 | this->bAddedDefaultValue_[4] = true; |
---|
| 233 | |
---|
| 234 | return (*this); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | Executor& Executor::setDefaultValue(unsigned int index, const MultiTypeMath& param) |
---|
| 238 | { |
---|
| 239 | if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS) |
---|
| 240 | { |
---|
| 241 | this->defaultValue_[index] = param; |
---|
| 242 | this->bAddedDefaultValue_[index] = true; |
---|
| 243 | } |
---|
| 244 | return (*this); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | bool Executor::allDefaultValuesSet() const |
---|
| 248 | { |
---|
| 249 | for (unsigned int i = 0; i < this->functor_->getParamCount(); i++) |
---|
| 250 | if (!this->bAddedDefaultValue_[i]) |
---|
| 251 | return false; |
---|
| 252 | |
---|
| 253 | return true; |
---|
| 254 | } |
---|
[847] | 255 | } |
---|