[10052] | 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 | * Noe Pedrazzini |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "PartDestructionEvent.h" |
---|
| 30 | |
---|
| 31 | #include <algorithm> |
---|
| 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/GameMode.h" |
---|
| 35 | #include "core/XMLPort.h" |
---|
| 36 | #include "network/NetworkFunction.h" |
---|
| 37 | #include "Item.h" |
---|
| 38 | #include "worldentities/pawns/Pawn.h" |
---|
| 39 | #include "worldentities/pawns/ModularSpaceShip.h" |
---|
[10055] | 40 | #include "items/Engine.h" |
---|
[10052] | 41 | #include "gametypes/Gametype.h" |
---|
[10055] | 42 | #include "chat/ChatManager.h" |
---|
[10052] | 43 | |
---|
| 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | RegisterClass(PartDestructionEvent); |
---|
| 48 | |
---|
| 49 | PartDestructionEvent::PartDestructionEvent(Context* context) |
---|
| 50 | : Item(context) |
---|
| 51 | { |
---|
| 52 | RegisterObject(PartDestructionEvent); |
---|
[10053] | 53 | this->setValid(true); |
---|
[10052] | 54 | } |
---|
| 55 | |
---|
| 56 | PartDestructionEvent::~PartDestructionEvent() |
---|
| 57 | { |
---|
| 58 | |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void PartDestructionEvent::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 62 | { |
---|
| 63 | SUPER(PartDestructionEvent, XMLPort, xmlelement, mode); |
---|
| 64 | |
---|
| 65 | XMLPortParam(PartDestructionEvent, "targetType", setTargetType, getTargetType, xmlelement, mode).defaultValues("NULL"); |
---|
| 66 | XMLPortParam(PartDestructionEvent, "targetName", setTargetName, getTargetName, xmlelement, mode).defaultValues("NULL"); |
---|
[10055] | 67 | XMLPortParam(PartDestructionEvent, "operation", setOperation, getOperation, xmlelement, mode).defaultValues("NULL"); |
---|
[10052] | 68 | XMLPortParam(PartDestructionEvent, "targetParam", setTargetParam, getTargetParam, xmlelement, mode).defaultValues("NULL"); |
---|
| 69 | XMLPortParam(PartDestructionEvent, "value", setEventValue, getEventValue, xmlelement, mode).defaultValues(0); |
---|
[10055] | 70 | XMLPortParam(PartDestructionEvent, "message", setMessage, getMessage, xmlelement, mode).defaultValues("NULL"); |
---|
[11424] | 71 | XMLPortPAram(PartDestructionEvent, "spawn", setSpawn, getSpawn, xmlelement, mode).defaultValues("NULL"); |
---|
[10052] | 72 | |
---|
| 73 | /* |
---|
| 74 | XMLPortParam(PartDestructionEvent, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
| 75 | XMLPortParam(PartDestructionEvent, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
| 76 | XMLPortParam(PartDestructionEvent, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
| 77 | */ |
---|
| 78 | } |
---|
| 79 | |
---|
[10055] | 80 | /** |
---|
| 81 | @brief |
---|
| 82 | Executes this event. |
---|
| 83 | */ |
---|
[10052] | 84 | void PartDestructionEvent::execute() |
---|
| 85 | { |
---|
[10053] | 86 | // Do not execute if this event is invalid |
---|
| 87 | if(!isValid()) |
---|
[10052] | 88 | { |
---|
[10053] | 89 | orxout(internal_warning) << "Attempted to execute an invalid PartDestructionEvent!" << endl; |
---|
[10052] | 90 | return; |
---|
| 91 | } |
---|
| 92 | |
---|
[10055] | 93 | // Output the destruction-message to the chat |
---|
| 94 | if(this->message_ != "NULL") |
---|
| 95 | ChatManager::message(this->message_); |
---|
| 96 | |
---|
| 97 | // Modify parameters as configured for all cases |
---|
[10053] | 98 | if (this->targetType_ == "ship") |
---|
| 99 | { |
---|
| 100 | switch (this->targetParam_) { |
---|
[11071] | 101 | case TargetParam::shieldhealth: |
---|
[10053] | 102 | this->parent_->getParent()->setShieldHealth(operate(this->parent_->getParent()->getShieldHealth())); |
---|
| 103 | break; |
---|
[11424] | 104 | case TargetParam::maxshieldhealth: |
---|
| 105 | this->parent_->getParent()->setMaxShieldHealth(operate(this->parent_->getParent()->getMaxShieldHealth())); |
---|
| 106 | break; |
---|
[11071] | 107 | case TargetParam::boostpower: |
---|
[10055] | 108 | this->parent_->getParent()->setInitialBoostPower(operate(this->parent_->getParent()->getInitialBoostPower())); |
---|
| 109 | break; |
---|
[11071] | 110 | case TargetParam::boostpowerrate: |
---|
[10055] | 111 | this->parent_->getParent()->setBoostPowerRate(operate(this->parent_->getParent()->getBoostPowerRate())); |
---|
| 112 | break; |
---|
[11071] | 113 | case TargetParam::rotationthrust: |
---|
[10058] | 114 | this->parent_->getParent()->setRotationThrust(operate(this->parent_->getParent()->getRotationThrust())); |
---|
| 115 | break; |
---|
[10053] | 116 | default: |
---|
| 117 | break; |
---|
| 118 | } |
---|
| 119 | this->setValid(false); |
---|
| 120 | return; |
---|
| 121 | } |
---|
[10055] | 122 | |
---|
| 123 | if (this->targetType_ == "engine") |
---|
| 124 | { |
---|
| 125 | switch (this->targetParam_) { |
---|
[11071] | 126 | case TargetParam::null: |
---|
[10214] | 127 | this->parent_->getParent()->getEngineByName(targetName_)->destroy(); |
---|
[10055] | 128 | break; |
---|
[11071] | 129 | case TargetParam::boostfactor: |
---|
[10058] | 130 | this->parent_->getParent()->getEngineByName(targetName_)->setBoostFactor(operate(this->parent_->getParent()->getEngineByName(targetName_)->getBoostFactor())); |
---|
[10055] | 131 | break; |
---|
[11071] | 132 | case TargetParam::speedfront: |
---|
[10058] | 133 | this->parent_->getParent()->getEngineByName(targetName_)->setMaxSpeedFront(operate(this->parent_->getParent()->getEngineByName(targetName_)->getMaxSpeedFront())); |
---|
| 134 | break; |
---|
[11071] | 135 | case TargetParam::accelerationfront: |
---|
[10058] | 136 | this->parent_->getParent()->getEngineByName(targetName_)->setAccelerationFront(operate(this->parent_->getParent()->getEngineByName(targetName_)->getAccelerationFront())); |
---|
| 137 | break; |
---|
[10055] | 138 | default: |
---|
| 139 | break; |
---|
| 140 | } |
---|
| 141 | this->setValid(false); |
---|
| 142 | return; |
---|
| 143 | } |
---|
[10058] | 144 | |
---|
| 145 | if (this->targetType_ == "part") |
---|
| 146 | { |
---|
| 147 | switch (this->targetParam_) { |
---|
[11071] | 148 | case TargetParam::null: |
---|
[10071] | 149 | if (!this->parent_->getParent()->getShipPartByName(targetName_)) |
---|
| 150 | return; |
---|
[10058] | 151 | this->parent_->getParent()->getShipPartByName(targetName_)->setEventExecution(false); |
---|
| 152 | this->parent_->getParent()->killShipPart(targetName_); |
---|
| 153 | break; |
---|
| 154 | default: |
---|
| 155 | break; |
---|
| 156 | } |
---|
| 157 | this->setValid(false); |
---|
| 158 | return; |
---|
| 159 | } |
---|
[10053] | 160 | } |
---|
[10052] | 161 | |
---|
[10053] | 162 | void PartDestructionEvent::setParent(ShipPart* part) |
---|
| 163 | { |
---|
| 164 | this->parent_ = part; |
---|
[10052] | 165 | } |
---|
| 166 | |
---|
[10055] | 167 | /** |
---|
| 168 | @brief |
---|
| 169 | Set type of the target |
---|
[11099] | 170 | @param type |
---|
[10055] | 171 | The desired target-type as string. Valid target-types: ship engine weapon |
---|
| 172 | */ |
---|
[10052] | 173 | void PartDestructionEvent::setTargetType(std::string type) |
---|
| 174 | { |
---|
[10058] | 175 | if ((type == "ship") || (type == "engine") || (type == "weapon") || (type == "part")) |
---|
[10052] | 176 | { |
---|
| 177 | this->targetType_ = type; |
---|
| 178 | return; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | // Error, if invalid target-type was entered. |
---|
[10058] | 182 | orxout(internal_warning) << "\"" << type << "\" is not a valid target-type for a PartDestructionEvent. Valid types are: ship engine weapon part" << endl; |
---|
[10053] | 183 | this->setValid(false); |
---|
[10052] | 184 | return; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | void PartDestructionEvent::setTargetName(std::string name) |
---|
| 188 | { |
---|
| 189 | // Error, if the target-type is "weapon" or "engine", but the name of said target was not defined. |
---|
| 190 | if (((this->targetType_ == "weapon") || (this->targetType_ == "engine")) && (name == "NULL")) |
---|
| 191 | { |
---|
| 192 | orxout(internal_warning) << "The target-name of a PartDestructionEvent with target-type \"" << this->targetType_ << "\" needs to be defined!" << endl; |
---|
| 193 | return; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | this->targetName_ = name; |
---|
| 197 | } |
---|
| 198 | |
---|
[10055] | 199 | /** |
---|
| 200 | @brief |
---|
| 201 | Set the operation to be applied. |
---|
| 202 | @param param |
---|
[11099] | 203 | The desired parameter as string. Valid parameters: c.f. @ref TargetParam |
---|
[10055] | 204 | */ |
---|
[10052] | 205 | void PartDestructionEvent::setTargetParam(std::string param) |
---|
| 206 | { |
---|
[10055] | 207 | // A target-type needs to be defined in order to choose a parameter. |
---|
[10052] | 208 | if (this->targetType_ == "NULL") |
---|
| 209 | { |
---|
| 210 | orxout(internal_warning) << "No valid target-type defined. Cannot set target-param for this PartDestructionEvent." << endl; |
---|
[10053] | 211 | this->setValid(false); |
---|
[10052] | 212 | return; |
---|
| 213 | } |
---|
| 214 | |
---|
[10055] | 215 | // engine: NULL boostfactor speedfront accelerationfront |
---|
| 216 | if (this->targetType_ == "engine") |
---|
| 217 | { |
---|
| 218 | if (param == "NULL") |
---|
| 219 | { |
---|
[11071] | 220 | this->targetParam_ = TargetParam::null; |
---|
[10055] | 221 | return; |
---|
| 222 | } |
---|
| 223 | if (param == "boostfactor") |
---|
| 224 | { |
---|
[11071] | 225 | this->targetParam_ = TargetParam::boostfactor; |
---|
[10055] | 226 | return; |
---|
| 227 | } |
---|
| 228 | if (param == "speedfront") |
---|
| 229 | { |
---|
[11071] | 230 | this->targetParam_ = TargetParam::speedfront; |
---|
[10055] | 231 | return; |
---|
| 232 | } |
---|
| 233 | if (param == "accelerationfront") |
---|
| 234 | { |
---|
[11071] | 235 | this->targetParam_ = TargetParam::accelerationfront; |
---|
[10055] | 236 | return; |
---|
| 237 | } |
---|
[10052] | 238 | |
---|
[10055] | 239 | orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"engine\". Valid types are: boostfactor speedfront accelerationfront" << endl; |
---|
| 240 | return; |
---|
| 241 | } |
---|
[10052] | 242 | |
---|
| 243 | // weapon: |
---|
| 244 | |
---|
[10058] | 245 | // ship: shieldhealth (maxshieldhealth shieldabsorption shieldrechargerate) boostpower boostpowerrate rotationthrust |
---|
[10052] | 246 | if (this->targetType_ == "ship") |
---|
| 247 | { |
---|
| 248 | if (param == "shieldhealth") |
---|
| 249 | { |
---|
[11071] | 250 | this->targetParam_ = TargetParam::shieldhealth; |
---|
[10052] | 251 | return; |
---|
| 252 | } |
---|
[10055] | 253 | if (param == "boostpower") |
---|
| 254 | { |
---|
[11071] | 255 | this->targetParam_ = TargetParam::boostpower; |
---|
[10055] | 256 | return; |
---|
| 257 | } |
---|
| 258 | if (param == "boostpowerrate") |
---|
| 259 | { |
---|
[11071] | 260 | this->targetParam_ = TargetParam::boostpowerrate; |
---|
[10055] | 261 | return; |
---|
| 262 | } |
---|
[10058] | 263 | if (param == "rotationthrust") |
---|
| 264 | { |
---|
[11071] | 265 | this->targetParam_ = TargetParam::rotationthrust; |
---|
[10058] | 266 | return; |
---|
| 267 | } |
---|
[10052] | 268 | |
---|
[10058] | 269 | orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"ship\". Valid types are: shieldhealth maxshieldhealth shieldabsorption shieldrechargerate boostpower boostpowerrate rotationthrust" << endl; |
---|
[10052] | 270 | return; |
---|
| 271 | } |
---|
[10053] | 272 | |
---|
[10058] | 273 | if (this->targetType_ == "part") |
---|
| 274 | { |
---|
| 275 | if (param == "NULL") |
---|
| 276 | { |
---|
[11071] | 277 | this->targetParam_ = TargetParam::null; |
---|
[10058] | 278 | return; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"part\". Valid types are: NULL (set operation to \"destroy\")" << endl; |
---|
| 282 | return; |
---|
| 283 | } |
---|
| 284 | |
---|
[10053] | 285 | orxout(internal_warning) << "No valid target-param defined. The chosen param is either invalid or not available for this target-type." << endl; |
---|
| 286 | this->setValid(false); |
---|
[10052] | 287 | } |
---|
| 288 | |
---|
[10055] | 289 | /** |
---|
| 290 | @brief |
---|
| 291 | Set the operation to be applied. |
---|
| 292 | @param operation |
---|
| 293 | The desired operator as string. Valid operators: * + - destroy |
---|
| 294 | */ |
---|
[10052] | 295 | void PartDestructionEvent::setOperation(std::string operation) |
---|
| 296 | { |
---|
| 297 | // * + - destroy |
---|
[10058] | 298 | if ((operation == "*") || (operation == "+") || (operation == "-") || (operation == "set") || (operation == "destroy")) |
---|
[10053] | 299 | { |
---|
| 300 | this->operation_ = operation; |
---|
| 301 | return; |
---|
| 302 | } |
---|
| 303 | this->operation_ = "NULL"; |
---|
[10058] | 304 | orxout(internal_warning) << "\"" << operation << "\" is not a valid operation for a PartDestructionEvent. Valid operations are: * + - set destroy" << endl; |
---|
[10052] | 305 | } |
---|
| 306 | |
---|
[10055] | 307 | /** |
---|
| 308 | @brief |
---|
| 309 | Set the message to be shown upon execution of the vent. |
---|
| 310 | @param msg |
---|
| 311 | The desired message as string. |
---|
| 312 | */ |
---|
| 313 | void PartDestructionEvent::setMessage(std::string msg) |
---|
| 314 | { |
---|
| 315 | this->message_ = msg; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | @brief |
---|
| 321 | Apply the configured operation and value to an input. |
---|
| 322 | @param input |
---|
| 323 | The value which should be modified |
---|
| 324 | @return |
---|
[10058] | 325 | Returns the product / sum / difference of input and configured value, |
---|
| 326 | the configured value if the operation is "set", |
---|
| 327 | or 0 if the operation is "destroy" |
---|
[10055] | 328 | */ |
---|
[10053] | 329 | float PartDestructionEvent::operate(float input) |
---|
| 330 | { |
---|
| 331 | if (this->operation_ == "*") |
---|
| 332 | return input * this->value_; |
---|
| 333 | if (this->operation_ == "+") |
---|
| 334 | return input + this->value_; |
---|
| 335 | if (this->operation_ == "-") |
---|
| 336 | return input - this->value_; |
---|
[10058] | 337 | if (this->operation_ == "set") |
---|
| 338 | return this->value_; |
---|
[10053] | 339 | if (this->operation_ == "destroy") |
---|
| 340 | { |
---|
| 341 | return 0; |
---|
| 342 | } |
---|
| 343 | return 0; |
---|
| 344 | } |
---|
| 345 | |
---|
[10055] | 346 | /** |
---|
| 347 | @brief |
---|
| 348 | Sets the value applied with the chosen operation. |
---|
| 349 | @param value |
---|
| 350 | The value as float. |
---|
| 351 | */ |
---|
[10052] | 352 | void PartDestructionEvent::setEventValue(float value) |
---|
| 353 | { |
---|
| 354 | this->value_ = value; |
---|
| 355 | } |
---|
| 356 | } |
---|