[2072] | 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 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "Pawn.h" |
---|
| 30 | |
---|
[3280] | 31 | #include <algorithm> |
---|
| 32 | |
---|
[3196] | 33 | #include "core/CoreIncludes.h" |
---|
[2896] | 34 | #include "core/GameMode.h" |
---|
[2072] | 35 | #include "core/XMLPort.h" |
---|
[3196] | 36 | #include "network/NetworkFunction.h" |
---|
| 37 | |
---|
[5735] | 38 | #include "infos/PlayerInfo.h" |
---|
[6417] | 39 | #include "controllers/Controller.h" |
---|
[5735] | 40 | #include "gametypes/Gametype.h" |
---|
[5737] | 41 | #include "graphics/ParticleSpawner.h" |
---|
[5735] | 42 | #include "worldentities/ExplosionChunk.h" |
---|
| 43 | #include "worldentities/BigExplosion.h" |
---|
| 44 | #include "weaponsystem/WeaponSystem.h" |
---|
| 45 | #include "weaponsystem/WeaponSlot.h" |
---|
| 46 | #include "weaponsystem/WeaponPack.h" |
---|
| 47 | #include "weaponsystem/WeaponSet.h" |
---|
[2072] | 48 | |
---|
[3084] | 49 | |
---|
[2072] | 50 | namespace orxonox |
---|
| 51 | { |
---|
| 52 | CreateFactory(Pawn); |
---|
| 53 | |
---|
[7163] | 54 | Pawn::Pawn(BaseObject* creator) |
---|
| 55 | : ControllableEntity(creator) |
---|
| 56 | , RadarViewable(creator, static_cast<WorldEntity*>(this)) |
---|
[2072] | 57 | { |
---|
| 58 | RegisterObject(Pawn); |
---|
| 59 | |
---|
[2662] | 60 | this->bAlive_ = true; |
---|
[3053] | 61 | this->bReload_ = false; |
---|
[2072] | 62 | |
---|
| 63 | this->health_ = 0; |
---|
| 64 | this->maxHealth_ = 0; |
---|
| 65 | this->initialHealth_ = 0; |
---|
[7163] | 66 | this->shieldHealth_ = 0; |
---|
| 67 | this->shieldAbsorption_ = 0.5; |
---|
[2072] | 68 | |
---|
| 69 | this->lastHitOriginator_ = 0; |
---|
| 70 | |
---|
[2662] | 71 | this->spawnparticleduration_ = 3.0f; |
---|
[2098] | 72 | |
---|
[6417] | 73 | this->aimPosition_ = Vector3::ZERO; |
---|
| 74 | |
---|
[2896] | 75 | if (GameMode::isMaster()) |
---|
[2662] | 76 | { |
---|
| 77 | this->weaponSystem_ = new WeaponSystem(this); |
---|
[3053] | 78 | this->weaponSystem_->setPawn(this); |
---|
[2662] | 79 | } |
---|
| 80 | else |
---|
| 81 | this->weaponSystem_ = 0; |
---|
| 82 | |
---|
| 83 | this->setRadarObjectColour(ColourValue::Red); |
---|
| 84 | this->setRadarObjectShape(RadarViewable::Dot); |
---|
| 85 | |
---|
[2072] | 86 | this->registerVariables(); |
---|
[3089] | 87 | |
---|
| 88 | this->isHumanShip_ = this->hasLocalController(); |
---|
[8329] | 89 | |
---|
| 90 | this->setSyncMode(ObjectDirection::Bidirectional); // needed to synchronise e.g. aimposition |
---|
[2072] | 91 | } |
---|
| 92 | |
---|
| 93 | Pawn::~Pawn() |
---|
| 94 | { |
---|
[2662] | 95 | if (this->isInitialized()) |
---|
| 96 | { |
---|
| 97 | if (this->weaponSystem_) |
---|
[5929] | 98 | this->weaponSystem_->destroy(); |
---|
[2662] | 99 | } |
---|
[2072] | 100 | } |
---|
| 101 | |
---|
| 102 | void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 103 | { |
---|
| 104 | SUPER(Pawn, XMLPort, xmlelement, mode); |
---|
| 105 | |
---|
[2662] | 106 | XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
---|
[2072] | 107 | XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
---|
| 108 | XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
---|
[7163] | 109 | |
---|
| 110 | XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0); |
---|
| 111 | XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0); |
---|
| 112 | |
---|
[2662] | 113 | XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode); |
---|
| 114 | XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f); |
---|
| 115 | XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); |
---|
| 116 | |
---|
[3053] | 117 | XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode); |
---|
| 118 | XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode); |
---|
[6417] | 119 | XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode); |
---|
[2072] | 120 | } |
---|
| 121 | |
---|
| 122 | void Pawn::registerVariables() |
---|
| 123 | { |
---|
[7163] | 124 | registerVariable(this->bAlive_, VariableDirection::ToClient); |
---|
| 125 | registerVariable(this->health_, VariableDirection::ToClient); |
---|
| 126 | registerVariable(this->initialHealth_, VariableDirection::ToClient); |
---|
| 127 | registerVariable(this->shieldHealth_, VariableDirection::ToClient); |
---|
| 128 | registerVariable(this->shieldAbsorption_, VariableDirection::ToClient); |
---|
| 129 | registerVariable(this->bReload_, VariableDirection::ToServer); |
---|
| 130 | registerVariable(this->aimPosition_, VariableDirection::ToServer); // For the moment this variable gets only transfered to the server |
---|
[2072] | 131 | } |
---|
| 132 | |
---|
| 133 | void Pawn::tick(float dt) |
---|
| 134 | { |
---|
[2809] | 135 | SUPER(Pawn, tick, dt); |
---|
[2072] | 136 | |
---|
[3053] | 137 | this->bReload_ = false; |
---|
[2662] | 138 | |
---|
[3084] | 139 | if (GameMode::isMaster()) |
---|
[3087] | 140 | if (this->health_ <= 0 && bAlive_) |
---|
[6864] | 141 | { |
---|
| 142 | this->fireEvent(); // Event to notify anyone who want's to know about the death. |
---|
[3087] | 143 | this->death(); |
---|
[6864] | 144 | } |
---|
[2072] | 145 | } |
---|
| 146 | |
---|
[7889] | 147 | void Pawn::preDestroy() |
---|
| 148 | { |
---|
| 149 | // yay, multiple inheritance! |
---|
| 150 | this->ControllableEntity::preDestroy(); |
---|
| 151 | this->PickupCarrier::preDestroy(); |
---|
| 152 | } |
---|
| 153 | |
---|
[2826] | 154 | void Pawn::setPlayer(PlayerInfo* player) |
---|
| 155 | { |
---|
| 156 | ControllableEntity::setPlayer(player); |
---|
| 157 | |
---|
| 158 | if (this->getGametype()) |
---|
| 159 | this->getGametype()->playerStartsControllingPawn(player, this); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | void Pawn::removePlayer() |
---|
| 163 | { |
---|
| 164 | if (this->getGametype()) |
---|
| 165 | this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this); |
---|
| 166 | |
---|
| 167 | ControllableEntity::removePlayer(); |
---|
| 168 | } |
---|
| 169 | |
---|
[2072] | 170 | void Pawn::setHealth(float health) |
---|
| 171 | { |
---|
[3280] | 172 | this->health_ = std::min(health, this->maxHealth_); |
---|
[2072] | 173 | } |
---|
| 174 | |
---|
| 175 | void Pawn::damage(float damage, Pawn* originator) |
---|
| 176 | { |
---|
[2826] | 177 | if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator)) |
---|
| 178 | { |
---|
[7163] | 179 | //share the dealt damage to the shield and the Pawn. |
---|
| 180 | float shielddamage = damage*this->shieldAbsorption_; |
---|
| 181 | float healthdamage = damage*(1-this->shieldAbsorption_); |
---|
| 182 | |
---|
| 183 | // In case the shield can not take all the shield damage. |
---|
| 184 | if (shielddamage > this->getShieldHealth()) |
---|
| 185 | { |
---|
| 186 | healthdamage += shielddamage-this->getShieldHealth(); |
---|
| 187 | this->setShieldHealth(0); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | this->setHealth(this->health_ - healthdamage); |
---|
| 191 | |
---|
| 192 | if (this->getShieldHealth() > 0) |
---|
| 193 | { |
---|
| 194 | this->setShieldHealth(this->shieldHealth_ - shielddamage); |
---|
| 195 | } |
---|
| 196 | |
---|
[2826] | 197 | this->lastHitOriginator_ = originator; |
---|
[2072] | 198 | |
---|
[2826] | 199 | // play damage effect |
---|
| 200 | } |
---|
[2072] | 201 | } |
---|
| 202 | |
---|
| 203 | void Pawn::hit(Pawn* originator, const Vector3& force, float damage) |
---|
| 204 | { |
---|
[6417] | 205 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) ) |
---|
[2826] | 206 | { |
---|
| 207 | this->damage(damage, originator); |
---|
| 208 | this->setVelocity(this->getVelocity() + force); |
---|
[2072] | 209 | |
---|
[2826] | 210 | // play hit effect |
---|
| 211 | } |
---|
[2072] | 212 | } |
---|
| 213 | |
---|
[6417] | 214 | void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage) |
---|
| 215 | { |
---|
| 216 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) ) |
---|
| 217 | { |
---|
| 218 | this->damage(damage, originator); |
---|
| 219 | |
---|
| 220 | if ( this->getController() ) |
---|
| 221 | this->getController()->hit(originator, contactpoint, damage); |
---|
| 222 | |
---|
| 223 | // play hit effect |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | |
---|
[2072] | 227 | void Pawn::kill() |
---|
| 228 | { |
---|
| 229 | this->damage(this->health_); |
---|
| 230 | this->death(); |
---|
| 231 | } |
---|
| 232 | |
---|
[2662] | 233 | void Pawn::spawneffect() |
---|
[2072] | 234 | { |
---|
| 235 | // play spawn effect |
---|
[6417] | 236 | if (!this->spawnparticlesource_.empty()) |
---|
[2662] | 237 | { |
---|
| 238 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 239 | effect->setPosition(this->getPosition()); |
---|
| 240 | effect->setOrientation(this->getOrientation()); |
---|
| 241 | effect->setDestroyAfterLife(true); |
---|
| 242 | effect->setSource(this->spawnparticlesource_); |
---|
| 243 | effect->setLifetime(this->spawnparticleduration_); |
---|
| 244 | } |
---|
[2072] | 245 | } |
---|
| 246 | |
---|
| 247 | void Pawn::death() |
---|
| 248 | { |
---|
[3033] | 249 | this->setHealth(1); |
---|
[2826] | 250 | if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_)) |
---|
| 251 | { |
---|
| 252 | // Set bAlive_ to false and wait for PawnManager to do the destruction |
---|
| 253 | this->bAlive_ = false; |
---|
[2662] | 254 | |
---|
[2826] | 255 | this->setDestroyWhenPlayerLeft(false); |
---|
[2662] | 256 | |
---|
[2826] | 257 | if (this->getGametype()) |
---|
| 258 | this->getGametype()->pawnKilled(this, this->lastHitOriginator_); |
---|
[2662] | 259 | |
---|
[3038] | 260 | if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this) |
---|
| 261 | this->getPlayer()->stopControl(); |
---|
[2072] | 262 | |
---|
[2896] | 263 | if (GameMode::isMaster()) |
---|
[3087] | 264 | { |
---|
| 265 | // this->deathEffect(); |
---|
| 266 | this->goWithStyle(); |
---|
| 267 | } |
---|
[2826] | 268 | } |
---|
[2662] | 269 | } |
---|
[3087] | 270 | void Pawn::goWithStyle() |
---|
| 271 | { |
---|
| 272 | this->bAlive_ = false; |
---|
| 273 | this->setDestroyWhenPlayerLeft(false); |
---|
[2072] | 274 | |
---|
[3087] | 275 | BigExplosion* chunk = new BigExplosion(this->getCreator()); |
---|
| 276 | chunk->setPosition(this->getPosition()); |
---|
| 277 | |
---|
| 278 | } |
---|
[2662] | 279 | void Pawn::deatheffect() |
---|
| 280 | { |
---|
[2072] | 281 | // play death effect |
---|
[2662] | 282 | { |
---|
| 283 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 284 | effect->setPosition(this->getPosition()); |
---|
| 285 | effect->setOrientation(this->getOrientation()); |
---|
| 286 | effect->setDestroyAfterLife(true); |
---|
| 287 | effect->setSource("Orxonox/explosion2b"); |
---|
| 288 | effect->setLifetime(4.0f); |
---|
| 289 | } |
---|
| 290 | { |
---|
| 291 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 292 | effect->setPosition(this->getPosition()); |
---|
| 293 | effect->setOrientation(this->getOrientation()); |
---|
| 294 | effect->setDestroyAfterLife(true); |
---|
| 295 | effect->setSource("Orxonox/smoke6"); |
---|
| 296 | effect->setLifetime(4.0f); |
---|
| 297 | } |
---|
| 298 | { |
---|
| 299 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
---|
| 300 | effect->setPosition(this->getPosition()); |
---|
| 301 | effect->setOrientation(this->getOrientation()); |
---|
| 302 | effect->setDestroyAfterLife(true); |
---|
| 303 | effect->setSource("Orxonox/sparks"); |
---|
| 304 | effect->setLifetime(4.0f); |
---|
| 305 | } |
---|
| 306 | for (unsigned int i = 0; i < this->numexplosionchunks_; ++i) |
---|
| 307 | { |
---|
| 308 | ExplosionChunk* chunk = new ExplosionChunk(this->getCreator()); |
---|
| 309 | chunk->setPosition(this->getPosition()); |
---|
| 310 | } |
---|
[2072] | 311 | } |
---|
| 312 | |
---|
[6417] | 313 | void Pawn::fired(unsigned int firemode) |
---|
[2098] | 314 | { |
---|
[6417] | 315 | if (this->weaponSystem_) |
---|
| 316 | this->weaponSystem_->fire(firemode); |
---|
[2098] | 317 | } |
---|
| 318 | |
---|
[3053] | 319 | void Pawn::reload() |
---|
| 320 | { |
---|
| 321 | this->bReload_ = true; |
---|
| 322 | } |
---|
| 323 | |
---|
[2072] | 324 | void Pawn::postSpawn() |
---|
| 325 | { |
---|
| 326 | this->setHealth(this->initialHealth_); |
---|
[2896] | 327 | if (GameMode::isMaster()) |
---|
[2662] | 328 | this->spawneffect(); |
---|
[2072] | 329 | } |
---|
[2662] | 330 | |
---|
[2893] | 331 | /* WeaponSystem: |
---|
| 332 | * functions load Slot, Set, Pack from XML and make sure all parent-pointers are set. |
---|
| 333 | * with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it. |
---|
| 334 | * --> e.g. Pickup-Items |
---|
| 335 | */ |
---|
[3053] | 336 | void Pawn::addWeaponSlot(WeaponSlot * wSlot) |
---|
[2662] | 337 | { |
---|
| 338 | this->attach(wSlot); |
---|
| 339 | if (this->weaponSystem_) |
---|
[3053] | 340 | this->weaponSystem_->addWeaponSlot(wSlot); |
---|
[2662] | 341 | } |
---|
| 342 | |
---|
| 343 | WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const |
---|
| 344 | { |
---|
| 345 | if (this->weaponSystem_) |
---|
[3053] | 346 | return this->weaponSystem_->getWeaponSlot(index); |
---|
[2662] | 347 | else |
---|
| 348 | return 0; |
---|
| 349 | } |
---|
| 350 | |
---|
[3053] | 351 | void Pawn::addWeaponSet(WeaponSet * wSet) |
---|
[2662] | 352 | { |
---|
| 353 | if (this->weaponSystem_) |
---|
[3053] | 354 | this->weaponSystem_->addWeaponSet(wSet); |
---|
[2662] | 355 | } |
---|
| 356 | |
---|
[3053] | 357 | WeaponSet * Pawn::getWeaponSet(unsigned int index) const |
---|
[2662] | 358 | { |
---|
| 359 | if (this->weaponSystem_) |
---|
[3053] | 360 | return this->weaponSystem_->getWeaponSet(index); |
---|
[2662] | 361 | else |
---|
| 362 | return 0; |
---|
| 363 | } |
---|
| 364 | |
---|
[3053] | 365 | void Pawn::addWeaponPack(WeaponPack * wPack) |
---|
[2662] | 366 | { |
---|
| 367 | if (this->weaponSystem_) |
---|
[7163] | 368 | { |
---|
[3053] | 369 | this->weaponSystem_->addWeaponPack(wPack); |
---|
[7163] | 370 | this->addedWeaponPack(wPack); |
---|
| 371 | } |
---|
[2662] | 372 | } |
---|
| 373 | |
---|
[6417] | 374 | void Pawn::addWeaponPackXML(WeaponPack * wPack) |
---|
| 375 | { |
---|
| 376 | if (this->weaponSystem_) |
---|
[7163] | 377 | { |
---|
[6417] | 378 | if (!this->weaponSystem_->addWeaponPack(wPack)) |
---|
| 379 | wPack->destroy(); |
---|
[7163] | 380 | else |
---|
| 381 | this->addedWeaponPack(wPack); |
---|
| 382 | } |
---|
[6417] | 383 | } |
---|
| 384 | |
---|
[3053] | 385 | WeaponPack * Pawn::getWeaponPack(unsigned int index) const |
---|
[2662] | 386 | { |
---|
| 387 | if (this->weaponSystem_) |
---|
[3053] | 388 | return this->weaponSystem_->getWeaponPack(index); |
---|
[2662] | 389 | else |
---|
| 390 | return 0; |
---|
| 391 | } |
---|
| 392 | |
---|
[3089] | 393 | //Tell the Map (RadarViewable), if this is a playership |
---|
| 394 | void Pawn::startLocalHumanControl() |
---|
| 395 | { |
---|
| 396 | // SUPER(ControllableEntity, changedPlayer()); |
---|
| 397 | ControllableEntity::startLocalHumanControl(); |
---|
| 398 | this->isHumanShip_ = true; |
---|
| 399 | } |
---|
[2072] | 400 | } |
---|