| 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 "ShipPart.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 | #include "worldentities/StaticEntity.h" | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox | 
|---|
| 45 | { | 
|---|
| 46 |     RegisterClass(ShipPart); | 
|---|
| 47 |  | 
|---|
| 48 |     ShipPart::ShipPart(Context* context) | 
|---|
| 49 |         : Item(context) | 
|---|
| 50 |     { | 
|---|
| 51 |         RegisterObject(ShipPart); | 
|---|
| 52 |     } | 
|---|
| 53 |  | 
|---|
| 54 |     ShipPart::~ShipPart() | 
|---|
| 55 |     { | 
|---|
| 56 |  | 
|---|
| 57 |     } | 
|---|
| 58 |  | 
|---|
| 59 |     void ShipPart::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 60 |     { | 
|---|
| 61 |         SUPER(ShipPart, XMLPort, xmlelement, mode); | 
|---|
| 62 |  | 
|---|
| 63 |         XMLPortParam(ShipPart, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); | 
|---|
| 64 |         XMLPortParam(ShipPart, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); | 
|---|
| 65 |         XMLPortParam(ShipPart, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); | 
|---|
| 66 |  | 
|---|
| 67 |         XMLPortParam(ShipPart, "damageabsorption", setDamageAbsorption, getDamageAbsorption, xmlelement, mode).defaultValues(0.5); | 
|---|
| 68 |  | 
|---|
| 69 |         /* | 
|---|
| 70 |         XMLPortParam(ShipPart, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0); | 
|---|
| 71 |         XMLPortParam(ShipPart, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0); | 
|---|
| 72 |         XMLPortParam(ShipPart, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100); | 
|---|
| 73 |         XMLPortParam(ShipPart, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0); | 
|---|
| 74 |  | 
|---|
| 75 |         XMLPortParam(ShipPart, "sShipPartparticlesource", setSShipPartParticleSource, getSShipPartParticleSource, xmlelement, mode); | 
|---|
| 76 |         XMLPortParam(ShipPart, "sShipPartparticleduration", setSShipPartParticleDuration, getSShipPartParticleDuration, xmlelement, mode).defaultValues(3.0f); | 
|---|
| 77 |         XMLPortParam(ShipPart, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); | 
|---|
| 78 |  | 
|---|
| 79 |         XMLPortParam(ShipPart, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0); | 
|---|
| 80 |         XMLPortParam(ShipPart, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f); | 
|---|
| 81 |  | 
|---|
| 82 |         XMLPortParam(ShipPart, "explosionSound",  setExplosionSound,  getExplosionSound,  xmlelement, mode); | 
|---|
| 83 |  | 
|---|
| 84 |         XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode ); | 
|---|
| 85 |         */ | 
|---|
| 86 |     } | 
|---|
| 87 |  | 
|---|
| 88 |     void ShipPart::death() | 
|---|
| 89 |     { | 
|---|
| 90 |         this->parent_->removeShipPart(this); | 
|---|
| 91 |         orxout() << this->getName() << " has died." << endl; | 
|---|
| 92 |     } | 
|---|
| 93 |  | 
|---|
| 94 |     /** | 
|---|
| 95 |     @brief | 
|---|
| 96 |         Add a StaticEntity to the ShipPart. | 
|---|
| 97 |     @param engine | 
|---|
| 98 |         A pointer to the StaticEntity to be added. | 
|---|
| 99 |     */ | 
|---|
| 100 |     void ShipPart::addEntity(StaticEntity* entity) | 
|---|
| 101 |     { | 
|---|
| 102 |         OrxAssert(entity != NULL, "The Entity cannot be NULL."); | 
|---|
| 103 |         this->entityList_.push_back(entity); | 
|---|
| 104 |         //part->addToSpaceShip(this); //FIXME: (noep) add | 
|---|
| 105 |     } | 
|---|
| 106 |  | 
|---|
| 107 |     /** | 
|---|
| 108 |     @brief | 
|---|
| 109 |         Get the i-th StaticEntity of the ShipPart. | 
|---|
| 110 |     @return | 
|---|
| 111 |         Returns a pointer to the i-the StaticEntity. NULL if there is no StaticEntity with that index. | 
|---|
| 112 |     */ | 
|---|
| 113 |     StaticEntity* ShipPart::getEntity(unsigned int index) | 
|---|
| 114 |     { | 
|---|
| 115 |         if(this->entityList_.size() >= index) | 
|---|
| 116 |             return NULL; | 
|---|
| 117 |         else | 
|---|
| 118 |             return this->entityList_[index]; | 
|---|
| 119 |     } | 
|---|
| 120 |  | 
|---|
| 121 |     /** | 
|---|
| 122 |     @brief | 
|---|
| 123 |         Check whether the ShipPart has a particular Entity. | 
|---|
| 124 |     @param engine | 
|---|
| 125 |         A pointer to the Entity to be checked. | 
|---|
| 126 |     */ | 
|---|
| 127 |     bool ShipPart::hasEntity(StaticEntity* entity) const | 
|---|
| 128 |     { | 
|---|
| 129 |         for(unsigned int i = 0; i < this->entityList_.size(); i++) | 
|---|
| 130 |         { | 
|---|
| 131 |             if(this->entityList_[i] == entity) | 
|---|
| 132 |                 return true; | 
|---|
| 133 |         } | 
|---|
| 134 |         return false; | 
|---|
| 135 |     } | 
|---|
| 136 |  | 
|---|
| 137 |     void ShipPart::printEntities() | 
|---|
| 138 |     { | 
|---|
| 139 |         orxout() << "ShipPart " << this->getName() << " has the following entities assigned:" << endl; | 
|---|
| 140 |         for(unsigned int j = 0; j < this->entityList_.size(); j++) | 
|---|
| 141 |         { | 
|---|
| 142 |             orxout() << "  " << this->entityList_[j]->getName() << endl; | 
|---|
| 143 |         } | 
|---|
| 144 |     } | 
|---|
| 145 |  | 
|---|
| 146 |     void ShipPart::setDamageAbsorption(float value) | 
|---|
| 147 |     { | 
|---|
| 148 |         this->damageAbsorption_ = value; | 
|---|
| 149 |     } | 
|---|
| 150 |  | 
|---|
| 151 |     void ShipPart::setParent(ModularSpaceShip* ship) | 
|---|
| 152 |     { | 
|---|
| 153 |         this->parent_ = ship; | 
|---|
| 154 |     } | 
|---|
| 155 |  | 
|---|
| 156 |     /** | 
|---|
| 157 |     @brief | 
|---|
| 158 |         Sets the health of the ShipPart. | 
|---|
| 159 |     */ | 
|---|
| 160 |     void ShipPart::setHealth(float health) | 
|---|
| 161 |     { | 
|---|
| 162 |         this->health_ = health; | 
|---|
| 163 |     } | 
|---|
| 164 |  | 
|---|
| 165 |     /** | 
|---|
| 166 |     @brief | 
|---|
| 167 |         Handles a received hit. | 
|---|
| 168 |     */ | 
|---|
| 169 |     void ShipPart::handleHit(float damage, float healthdamage, float shielddamage, Pawn* originator) | 
|---|
| 170 |     { | 
|---|
| 171 |         orxout() << "ShipPart " <<this->getName() << " is handling a hit!" << endl; | 
|---|
| 172 |         if (parent_->getGametype() && parent_->getGametype()->allowPawnDamage(parent_, originator)) | 
|---|
| 173 |         { | 
|---|
| 174 |             if (shielddamage >= parent_->getShieldHealth()) | 
|---|
| 175 |             { | 
|---|
| 176 |                 parent_->setShieldHealth(0); | 
|---|
| 177 |                 this->setHealth(this->health_ - (healthdamage + damage) * this->damageAbsorption_); | 
|---|
| 178 |                 parent_->setHealth(parent_->getHealth() - (healthdamage + damage) * (1 - this->damageAbsorption_)); | 
|---|
| 179 |             } | 
|---|
| 180 |             else | 
|---|
| 181 |             { | 
|---|
| 182 |                 parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); | 
|---|
| 183 |  | 
|---|
| 184 |                 // remove remaining shieldAbsorpton-Part of damage from shield | 
|---|
| 185 |                 shielddamage = damage * parent_->getShieldAbsorption(); | 
|---|
| 186 |                 shielddamage = std::min(parent_->getShieldHealth(),shielddamage); | 
|---|
| 187 |                 parent_->setShieldHealth(parent_->getShieldHealth() - shielddamage); | 
|---|
| 188 |  | 
|---|
| 189 |                 // set remaining damage to health | 
|---|
| 190 |                 this->setHealth(this->health_ - ((damage - shielddamage) - healthdamage) * this->damageAbsorption_); | 
|---|
| 191 |                 parent_->setHealth(parent_->getHealth() - ((damage - shielddamage) - healthdamage) * (1- this->damageAbsorption_)); | 
|---|
| 192 |             } | 
|---|
| 193 |         } | 
|---|
| 194 |         if (this->health_ < 0) | 
|---|
| 195 |             this->death(); | 
|---|
| 196 |         orxout() << "Health of ShipPart " << this->getName() << " is " << this->getHealth() << endl; | 
|---|
| 197 |     } | 
|---|
| 198 |  | 
|---|
| 199 |  | 
|---|
| 200 |     /** | 
|---|
| 201 |     @brief | 
|---|
| 202 |         Adds the ShipPart to the input SpaceShip. | 
|---|
| 203 |     @param ship | 
|---|
| 204 |         A pointer to the SpaceShip to which the ShipPart is added. | 
|---|
| 205 |     */ | 
|---|
| 206 |     /*void ShipPart::addToSpaceShip(ModularSpaceShip* ship) | 
|---|
| 207 |     { | 
|---|
| 208 |         this->parent_ = ship; | 
|---|
| 209 |  | 
|---|
| 210 |         if (ship) | 
|---|
| 211 |         { | 
|---|
| 212 |             this->parentID_ = ship->getObjectID(); | 
|---|
| 213 |             if (!ship->hasShipPart(this)) | 
|---|
| 214 |                 ship->addShipPart(this); | 
|---|
| 215 |         } | 
|---|
| 216 |     }*/ | 
|---|
| 217 |  | 
|---|
| 218 | } | 
|---|