[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" |
---|
| 40 | #include "gametypes/Gametype.h" |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
| 45 | RegisterClass(PartDestructionEvent); |
---|
| 46 | |
---|
| 47 | PartDestructionEvent::PartDestructionEvent(Context* context) |
---|
| 48 | : Item(context) |
---|
| 49 | { |
---|
| 50 | RegisterObject(PartDestructionEvent); |
---|
[10053] | 51 | this->setValid(true); |
---|
[10052] | 52 | } |
---|
| 53 | |
---|
| 54 | PartDestructionEvent::~PartDestructionEvent() |
---|
| 55 | { |
---|
| 56 | |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void PartDestructionEvent::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 60 | { |
---|
| 61 | SUPER(PartDestructionEvent, XMLPort, xmlelement, mode); |
---|
| 62 | |
---|
| 63 | XMLPortParam(PartDestructionEvent, "targetType", setTargetType, getTargetType, xmlelement, mode).defaultValues("NULL"); |
---|
| 64 | XMLPortParam(PartDestructionEvent, "targetName", setTargetName, getTargetName, xmlelement, mode).defaultValues("NULL"); |
---|
| 65 | XMLPortParam(PartDestructionEvent, "targetParam", setTargetParam, getTargetParam, xmlelement, mode).defaultValues("NULL"); |
---|
| 66 | XMLPortParam(PartDestructionEvent, "operation", setOperation, getOperation, xmlelement, mode).defaultValues("NULL"); |
---|
| 67 | XMLPortParam(PartDestructionEvent, "value", setEventValue, getEventValue, xmlelement, mode).defaultValues(0); |
---|
| 68 | |
---|
| 69 | /* |
---|
| 70 | XMLPortParam(PartDestructionEvent, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
| 71 | XMLPortParam(PartDestructionEvent, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
| 72 | XMLPortParam(PartDestructionEvent, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
| 73 | */ |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void PartDestructionEvent::execute() |
---|
| 77 | { |
---|
[10053] | 78 | // Do not execute if this event is invalid |
---|
| 79 | if(!isValid()) |
---|
[10052] | 80 | { |
---|
[10053] | 81 | orxout(internal_warning) << "Attempted to execute an invalid PartDestructionEvent!" << endl; |
---|
[10052] | 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
[10053] | 85 | if (this->targetType_ == "ship") |
---|
| 86 | { |
---|
| 87 | switch (this->targetParam_) { |
---|
| 88 | case shieldhealth: |
---|
| 89 | this->parent_->getParent()->setShieldHealth(operate(this->parent_->getParent()->getShieldHealth())); |
---|
| 90 | break; |
---|
| 91 | default: |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | this->setValid(false); |
---|
| 95 | return; |
---|
| 96 | } |
---|
| 97 | } |
---|
[10052] | 98 | |
---|
[10053] | 99 | void PartDestructionEvent::setParent(ShipPart* part) |
---|
| 100 | { |
---|
| 101 | this->parent_ = part; |
---|
[10052] | 102 | } |
---|
| 103 | |
---|
| 104 | void PartDestructionEvent::setTargetType(std::string type) |
---|
| 105 | { |
---|
| 106 | // ship engine weapon |
---|
| 107 | if ((type == "ship") || (type == "engine") || (type == "weapon")) |
---|
| 108 | { |
---|
| 109 | this->targetType_ = type; |
---|
| 110 | return; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | // Error, if invalid target-type was entered. |
---|
[10053] | 114 | orxout(internal_warning) << "\"" << type << "\" is not a valid target-type for a PartDestructionEvent. Valid types are: ship engine weapon" << endl; |
---|
| 115 | this->setValid(false); |
---|
[10052] | 116 | return; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void PartDestructionEvent::setTargetName(std::string name) |
---|
| 120 | { |
---|
| 121 | // Error, if the target-type is "weapon" or "engine", but the name of said target was not defined. |
---|
| 122 | if (((this->targetType_ == "weapon") || (this->targetType_ == "engine")) && (name == "NULL")) |
---|
| 123 | { |
---|
| 124 | orxout(internal_warning) << "The target-name of a PartDestructionEvent with target-type \"" << this->targetType_ << "\" needs to be defined!" << endl; |
---|
| 125 | return; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | this->targetName_ = name; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void PartDestructionEvent::setTargetParam(std::string param) |
---|
| 132 | { |
---|
| 133 | if (this->targetType_ == "NULL") |
---|
| 134 | { |
---|
| 135 | orxout(internal_warning) << "No valid target-type defined. Cannot set target-param for this PartDestructionEvent." << endl; |
---|
[10053] | 136 | this->setValid(false); |
---|
[10052] | 137 | return; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | // ship: shieldhealth maxshieldhealth shieldabsorption shieldrechargerate |
---|
| 141 | |
---|
| 142 | // engine: |
---|
| 143 | |
---|
| 144 | // weapon: |
---|
| 145 | |
---|
| 146 | if (this->targetType_ == "ship") |
---|
| 147 | { |
---|
| 148 | if (param == "shieldhealth") |
---|
| 149 | { |
---|
[10053] | 150 | this->targetParam_ = shieldhealth; |
---|
[10052] | 151 | return; |
---|
| 152 | } |
---|
| 153 | |
---|
[10053] | 154 | orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"ship\". Valid types are: shieldhealth maxshieldhealth shieldabsorption shieldrechargerate" << endl; |
---|
[10052] | 155 | return; |
---|
| 156 | } |
---|
[10053] | 157 | |
---|
| 158 | orxout(internal_warning) << "No valid target-param defined. The chosen param is either invalid or not available for this target-type." << endl; |
---|
| 159 | this->setValid(false); |
---|
[10052] | 160 | } |
---|
| 161 | |
---|
| 162 | void PartDestructionEvent::setOperation(std::string operation) |
---|
| 163 | { |
---|
| 164 | // * + - destroy |
---|
[10053] | 165 | if ((operation == "*") || (operation == "+") || (operation == "-") || (operation == "destroy")) |
---|
| 166 | { |
---|
| 167 | this->operation_ = operation; |
---|
| 168 | return; |
---|
| 169 | } |
---|
| 170 | this->operation_ = "NULL"; |
---|
| 171 | orxout(internal_warning) << "\"" << operation << "\" is not a valid operation for a PartDestructionEvent. Valid operations are: * + - destroy" << endl; |
---|
[10052] | 172 | } |
---|
| 173 | |
---|
[10053] | 174 | float PartDestructionEvent::operate(float input) |
---|
| 175 | { |
---|
| 176 | if (this->operation_ == "*") |
---|
| 177 | return input * this->value_; |
---|
| 178 | if (this->operation_ == "+") |
---|
| 179 | return input + this->value_; |
---|
| 180 | if (this->operation_ == "-") |
---|
| 181 | return input - this->value_; |
---|
| 182 | if (this->operation_ == "destroy") |
---|
| 183 | { |
---|
| 184 | return 0; |
---|
| 185 | } |
---|
| 186 | return 0; |
---|
| 187 | } |
---|
| 188 | |
---|
[10052] | 189 | void PartDestructionEvent::setEventValue(float value) |
---|
| 190 | { |
---|
| 191 | this->value_ = value; |
---|
| 192 | } |
---|
| 193 | } |
---|