[10011] | 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 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * Noe Pedrazzini |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ModularSpaceShip.h" |
---|
| 30 | |
---|
| 31 | #include <BulletDynamics/Dynamics/btRigidBody.h> |
---|
| 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/config/ConfigValueIncludes.h" |
---|
| 35 | #include "core/Template.h" |
---|
| 36 | #include "core/XMLPort.h" |
---|
| 37 | #include "util/Math.h" |
---|
| 38 | #include "gametypes/Gametype.h" |
---|
[10055] | 39 | #include "core/command/ConsoleCommand.h" |
---|
[10011] | 40 | |
---|
[10019] | 41 | #include "items/ShipPart.h" |
---|
[10058] | 42 | #include "items/Engine.h" |
---|
[10019] | 43 | #include "worldentities/StaticEntity.h" |
---|
| 44 | #include <BulletCollision/CollisionShapes/btCollisionShape.h> |
---|
[10036] | 45 | #include <BulletCollision/CollisionShapes/btCompoundShape.h> |
---|
[10011] | 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | namespace orxonox |
---|
| 50 | { |
---|
[10071] | 51 | SetConsoleCommand("ModularSpaceShip", "killshippart", &ModularSpaceShip::killShipPartStatic); |
---|
[10055] | 52 | |
---|
[10011] | 53 | RegisterClass(ModularSpaceShip); |
---|
| 54 | |
---|
[10055] | 55 | std::map<StaticEntity*, ShipPart*>* ModularSpaceShip::partMap_s = 0; |
---|
| 56 | |
---|
[10011] | 57 | ModularSpaceShip::ModularSpaceShip(Context* context) : SpaceShip(context) |
---|
| 58 | { |
---|
| 59 | RegisterObject(ModularSpaceShip); |
---|
| 60 | |
---|
| 61 | this->registerVariables(); |
---|
| 62 | |
---|
[10055] | 63 | ModularSpaceShip::partMap_s = &(this->partMap_); |
---|
| 64 | |
---|
[10011] | 65 | } |
---|
| 66 | |
---|
| 67 | ModularSpaceShip::~ModularSpaceShip() |
---|
| 68 | { |
---|
| 69 | if (this->isInitialized()) |
---|
| 70 | { |
---|
| 71 | |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void ModularSpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 76 | { |
---|
| 77 | SUPER(ModularSpaceShip, XMLPort, xmlelement, mode); |
---|
| 78 | XMLPortObject(ModularSpaceShip, ShipPart, "parts", addShipPart, getShipPart, xmlelement, mode); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void ModularSpaceShip::registerVariables() |
---|
| 82 | { |
---|
| 83 | return; |
---|
| 84 | } |
---|
| 85 | |
---|
[10055] | 86 | /** |
---|
| 87 | @brief |
---|
| 88 | Searches for ShipParts matching to StaticEntities. |
---|
| 89 | */ |
---|
[10011] | 90 | void ModularSpaceShip::updatePartAssignment() |
---|
| 91 | { |
---|
[10019] | 92 | // iterate through all attached objects |
---|
[10212] | 93 | for (unsigned int i=0; i < this->getAttachedObjects().size(); i++) |
---|
[10019] | 94 | { |
---|
| 95 | if (this->getAttachedObject(i) == NULL) |
---|
[10029] | 96 | { |
---|
[10019] | 97 | break; |
---|
[10029] | 98 | } |
---|
[10019] | 99 | // iterate through all attached parts |
---|
| 100 | for(unsigned int j = 0; j < this->partList_.size(); j++) |
---|
| 101 | { |
---|
| 102 | // if the name of the part matches the name of the object, add the object to that parts entitylist (unless it was already done). |
---|
| 103 | if((this->partList_[j]->getName() == this->getAttachedObject(i)->getName()) && !this->partList_[j]->hasEntity(orxonox_cast<StaticEntity*>(this->getAttachedObject(i)))) |
---|
| 104 | { |
---|
[10023] | 105 | // The Entity is added to the part's entityList_ |
---|
[10019] | 106 | this->partList_[j]->addEntity(orxonox_cast<StaticEntity*>(this->getAttachedObject(i))); |
---|
[10023] | 107 | // An entry in the partMap_ is created, assigning the part to the entity. |
---|
| 108 | this->addPartEntityAssignment((StaticEntity*)(this->getAttachedObject(i)), this->partList_[j]); |
---|
[10019] | 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
[10011] | 113 | |
---|
[10055] | 114 | /** |
---|
| 115 | @brief |
---|
| 116 | Creates a new assignment for the given StaticEntity and ShipPart in the partMap_ |
---|
| 117 | @param entity |
---|
| 118 | A pointer to the StaticEntity |
---|
| 119 | @param part |
---|
| 120 | A pointer to the ShipPart. |
---|
| 121 | */ |
---|
[10019] | 122 | void ModularSpaceShip::addPartEntityAssignment(StaticEntity* entity, ShipPart* part) |
---|
| 123 | { |
---|
| 124 | if (!entity || !part) |
---|
| 125 | return; |
---|
| 126 | |
---|
| 127 | if (this->partMap_.find(entity) != this->partMap_.end()) |
---|
| 128 | { |
---|
| 129 | orxout(internal_warning) << "Assigning an Entity to multiple parts is not yet supported." << endl; |
---|
| 130 | return; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | this->partMap_[entity] = part; |
---|
| 134 | } |
---|
| 135 | |
---|
[10036] | 136 | |
---|
[10019] | 137 | /** |
---|
| 138 | @brief |
---|
| 139 | Get the ShipPart an attached entity belongs to. |
---|
| 140 | @param entity |
---|
| 141 | The entity to be searched. |
---|
| 142 | @return |
---|
| 143 | Returns a pointer to the ShipPart the entity belongs to. |
---|
| 144 | */ |
---|
| 145 | ShipPart* ModularSpaceShip::getPartOfEntity(StaticEntity* entity) const |
---|
| 146 | { |
---|
| 147 | for (std::map<StaticEntity*, ShipPart*>::const_iterator it = this->partMap_.begin(); it != this->partMap_.end(); ++it) |
---|
| 148 | { |
---|
| 149 | if (it->first == entity) |
---|
| 150 | return it->second; |
---|
| 151 | } |
---|
| 152 | return NULL; |
---|
| 153 | } |
---|
| 154 | |
---|
[10055] | 155 | /** |
---|
| 156 | @brief |
---|
| 157 | If the damage occurred on an attached StaticEntity, the damage is given to the corresponding ShipPart to handle. |
---|
| 158 | */ |
---|
[10011] | 159 | void ModularSpaceShip::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs) |
---|
| 160 | { |
---|
[10019] | 161 | if (this->getPartOfEntity((StaticEntity*)(cs->getUserPointer())) != NULL) |
---|
| 162 | this->getPartOfEntity((StaticEntity*)(cs->getUserPointer()))->handleHit(damage, healthdamage, shielddamage, originator); |
---|
[10023] | 163 | else |
---|
| 164 | SpaceShip::damage(damage, healthdamage, shielddamage, originator, cs); |
---|
[10055] | 165 | } |
---|
[10019] | 166 | |
---|
[10055] | 167 | /** |
---|
| 168 | @brief |
---|
[10071] | 169 | STATIC: Needed for consolecommand. Kills the ShipPart with the given name. Used from the console-command "ModularSpaceShip killshippart [string]". |
---|
| 170 | @param name |
---|
| 171 | The name of the part to be killed. |
---|
| 172 | */ |
---|
| 173 | void ModularSpaceShip::killShipPartStatic(std::string name) |
---|
| 174 | { |
---|
| 175 | for (std::map<StaticEntity*, ShipPart*>::const_iterator it = ModularSpaceShip::partMap_s->begin(); it != ModularSpaceShip::partMap_s->end(); ++it) |
---|
| 176 | { |
---|
| 177 | if (it->second->getName() == name) |
---|
| 178 | { |
---|
| 179 | it->second->setAlive(false); |
---|
| 180 | return; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | orxout(internal_warning) << "Could not apply damage to ShipPart \"" << name << "\". Part not found." << endl; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | @brief |
---|
[10055] | 188 | Kills the ShipPart with the given name. Used from the console-command "ModularSpaceShip killshippart [string]". |
---|
| 189 | @param name |
---|
| 190 | The name of the part to be killed. |
---|
| 191 | */ |
---|
| 192 | void ModularSpaceShip::killShipPart(std::string name) |
---|
| 193 | { |
---|
[10071] | 194 | for (std::map<StaticEntity*, ShipPart*>::const_iterator it = ModularSpaceShip::partMap_.begin(); it != ModularSpaceShip::partMap_.end(); ++it) |
---|
[10011] | 195 | { |
---|
[10055] | 196 | if (it->second->getName() == name) |
---|
[10011] | 197 | { |
---|
[10071] | 198 | it->second->setAlive(false); |
---|
[10055] | 199 | return; |
---|
[10011] | 200 | } |
---|
[10055] | 201 | } |
---|
| 202 | orxout(internal_warning) << "Could not apply damage to ShipPart \"" << name << "\". Part not found." << endl; |
---|
[10011] | 203 | } |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | @brief |
---|
| 207 | Add a ShipPart to the SpaceShip. |
---|
| 208 | @param engine |
---|
| 209 | A pointer to the ShipPart to be added. |
---|
| 210 | */ |
---|
| 211 | void ModularSpaceShip::addShipPart(ShipPart* part) |
---|
| 212 | { |
---|
| 213 | OrxAssert(part != NULL, "The ShipPart cannot be NULL."); |
---|
| 214 | this->partList_.push_back(part); |
---|
[10023] | 215 | part->setParent(this); |
---|
[10011] | 216 | this->updatePartAssignment(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | @brief |
---|
| 221 | Get the i-th ShipPart of the SpaceShip. |
---|
| 222 | @return |
---|
| 223 | Returns a pointer to the i-the ShipPart. NULL if there is no ShipPart with that index. |
---|
| 224 | */ |
---|
| 225 | ShipPart* ModularSpaceShip::getShipPart(unsigned int index) |
---|
| 226 | { |
---|
[10053] | 227 | if(this->partList_.size() <= index) |
---|
[10011] | 228 | return NULL; |
---|
| 229 | else |
---|
| 230 | return this->partList_[index]; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | @brief |
---|
[10058] | 235 | Looks for an attached ShipPart with a certain name. |
---|
| 236 | @param name |
---|
| 237 | The name of the ShipPart to be returned. |
---|
| 238 | @return |
---|
| 239 | Pointer to the ShipPart with the given name, or NULL if not found. |
---|
| 240 | */ |
---|
| 241 | ShipPart* ModularSpaceShip::getShipPartByName(std::string name) |
---|
| 242 | { |
---|
| 243 | for(std::vector<ShipPart*>::iterator it = this->partList_.begin(); it != this->partList_.end(); ++it) |
---|
| 244 | { |
---|
| 245 | if(orxonox_cast<ShipPart*>(*it)->getName() == name) |
---|
| 246 | { |
---|
| 247 | return orxonox_cast<ShipPart*>(*it); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | orxout(internal_warning) << "Couldn't find ShipPart with name \"" << name << "\"." << endl; |
---|
| 251 | return NULL; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | @brief |
---|
[10011] | 256 | Check whether the SpaceShip has a particular Engine. |
---|
| 257 | @param engine |
---|
| 258 | A pointer to the Engine to be checked. |
---|
| 259 | */ |
---|
| 260 | bool ModularSpaceShip::hasShipPart(ShipPart* part) const |
---|
| 261 | { |
---|
| 262 | for(unsigned int i = 0; i < this->partList_.size(); i++) |
---|
| 263 | { |
---|
| 264 | if(this->partList_[i] == part) |
---|
| 265 | return true; |
---|
| 266 | } |
---|
| 267 | return false; |
---|
| 268 | } |
---|
| 269 | |
---|
[10055] | 270 | |
---|
| 271 | /** |
---|
| 272 | @brief |
---|
| 273 | Removes a ShipPart from the SpaceShip, destroying the corresponding StaticEntity |
---|
| 274 | @param part |
---|
| 275 | The ShipPart to be removed. |
---|
| 276 | */ |
---|
[10023] | 277 | void ModularSpaceShip::removeShipPart(ShipPart* part) |
---|
| 278 | { |
---|
| 279 | // Remove the part from the partList_ |
---|
| 280 | std::vector<ShipPart*>::iterator it = this->partList_.begin(); |
---|
| 281 | for(unsigned int i = 0; i < this->partList_.size(); i++) |
---|
| 282 | { |
---|
| 283 | if(this->partList_[i] == part) |
---|
[10029] | 284 | { |
---|
[10023] | 285 | this->partList_.erase(it); |
---|
[10029] | 286 | break; |
---|
| 287 | } |
---|
[10023] | 288 | it++; |
---|
| 289 | } |
---|
| 290 | // Remove the part-entity assignment and detach the Entity of this ShipPart |
---|
[10052] | 291 | for (std::map<StaticEntity*, ShipPart*>::iterator itt = this->partMap_.begin(); itt != this->partMap_.end(); ) |
---|
[10023] | 292 | { |
---|
| 293 | if (itt->second == part) |
---|
| 294 | { |
---|
[10033] | 295 | this->detach(itt->first); |
---|
[10054] | 296 | itt->first->destroy(); |
---|
[10033] | 297 | //itt->first->setActive(false); |
---|
| 298 | //itt->first->setVisible(false); |
---|
| 299 | //itt->first->setCollisionResponse(false); |
---|
| 300 | //itt->first->setCollisionType(None); |
---|
| 301 | //itt->first->deactivatePhysics(); |
---|
[10052] | 302 | this->partMap_.erase(itt++); |
---|
| 303 | } else { |
---|
| 304 | ++itt; |
---|
[10023] | 305 | } |
---|
| 306 | } |
---|
| 307 | } |
---|
[10011] | 308 | } |
---|