| 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 "OrxonoxStableHeaders.h" |
|---|
| 30 | #include "Pawn.h" |
|---|
| 31 | |
|---|
| 32 | #include "core/Core.h" |
|---|
| 33 | #include "core/CoreIncludes.h" |
|---|
| 34 | #include "core/XMLPort.h" |
|---|
| 35 | #include "util/Math.h" |
|---|
| 36 | #include "PawnManager.h" |
|---|
| 37 | #include "objects/infos/PlayerInfo.h" |
|---|
| 38 | #include "objects/gametypes/Gametype.h" |
|---|
| 39 | #include "objects/worldentities/ParticleSpawner.h" |
|---|
| 40 | #include "objects/worldentities/ExplosionChunk.h" |
|---|
| 41 | #include "network/NetworkFunction.h" |
|---|
| 42 | |
|---|
| 43 | namespace orxonox |
|---|
| 44 | { |
|---|
| 45 | CreateFactory(Pawn); |
|---|
| 46 | |
|---|
| 47 | registerMemberNetworkFunction( Pawn, doFire ); |
|---|
| 48 | |
|---|
| 49 | Pawn::Pawn(BaseObject* creator) : ControllableEntity(creator) |
|---|
| 50 | { |
|---|
| 51 | RegisterObject(Pawn); |
|---|
| 52 | |
|---|
| 53 | PawnManager::touch(); |
|---|
| 54 | this->bAlive_ = true; |
|---|
| 55 | this->fire_ = 0x0; |
|---|
| 56 | this->firehack_ = 0x0; |
|---|
| 57 | |
|---|
| 58 | this->health_ = 0; |
|---|
| 59 | this->maxHealth_ = 0; |
|---|
| 60 | this->initialHealth_ = 0; |
|---|
| 61 | |
|---|
| 62 | this->lastHitOriginator_ = 0; |
|---|
| 63 | |
|---|
| 64 | this->spawnparticleduration_ = 3.0f; |
|---|
| 65 | |
|---|
| 66 | this->getPickUp().setPlayer(this); |
|---|
| 67 | |
|---|
| 68 | if (Core::isMaster()) |
|---|
| 69 | { |
|---|
| 70 | this->weaponSystem_ = new WeaponSystem(this); |
|---|
| 71 | this->weaponSystem_->setParentPawn(this); |
|---|
| 72 | } |
|---|
| 73 | else |
|---|
| 74 | this->weaponSystem_ = 0; |
|---|
| 75 | |
|---|
| 76 | this->setRadarObjectColour(ColourValue::Red); |
|---|
| 77 | this->setRadarObjectShape(RadarViewable::Dot); |
|---|
| 78 | |
|---|
| 79 | this->registerVariables(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | Pawn::~Pawn() |
|---|
| 83 | { |
|---|
| 84 | if (this->isInitialized()) |
|---|
| 85 | { |
|---|
| 86 | for (ObjectList<PawnListener>::iterator it = ObjectList<PawnListener>::begin(); it != ObjectList<PawnListener>::end(); ++it) |
|---|
| 87 | it->destroyedPawn(this); |
|---|
| 88 | |
|---|
| 89 | if (this->weaponSystem_) |
|---|
| 90 | delete this->weaponSystem_; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 95 | { |
|---|
| 96 | SUPER(Pawn, XMLPort, xmlelement, mode); |
|---|
| 97 | |
|---|
| 98 | XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100); |
|---|
| 99 | XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200); |
|---|
| 100 | XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100); |
|---|
| 101 | XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode); |
|---|
| 102 | XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f); |
|---|
| 103 | XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7); |
|---|
| 104 | |
|---|
| 105 | XMLPortObject(Pawn, WeaponSlot, "weaponslots", setWeaponSlot, getWeaponSlot, xmlelement, mode); |
|---|
| 106 | XMLPortObject(Pawn, WeaponSet, "weaponsets", setWeaponSet, getWeaponSet, xmlelement, mode); |
|---|
| 107 | XMLPortObject(Pawn, WeaponPack, "weapons", setWeaponPack, getWeaponPack, xmlelement, mode); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void Pawn::registerVariables() |
|---|
| 111 | { |
|---|
| 112 | registerVariable(this->bAlive_, variableDirection::toclient); |
|---|
| 113 | registerVariable(this->health_, variableDirection::toclient); |
|---|
| 114 | registerVariable(this->initialHealth_, variableDirection::toclient); |
|---|
| 115 | registerVariable(this->fire_, variableDirection::toserver); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void Pawn::tick(float dt) |
|---|
| 119 | { |
|---|
| 120 | SUPER(Pawn, tick, dt); |
|---|
| 121 | |
|---|
| 122 | // if (this->weaponSystem_) |
|---|
| 123 | // { |
|---|
| 124 | // if (this->fire_ & WeaponMode::fire) |
|---|
| 125 | // this->weaponSystem_->fire(WeaponMode::fire); |
|---|
| 126 | // if (this->fire_ & WeaponMode::altFire) |
|---|
| 127 | // this->weaponSystem_->fire(WeaponMode::altFire); |
|---|
| 128 | // if (this->fire_ & WeaponMode::altFire2) |
|---|
| 129 | // this->weaponSystem_->fire(WeaponMode::altFire2); |
|---|
| 130 | // } |
|---|
| 131 | // this->fire_ = this->firehack_; |
|---|
| 132 | // this->firehack_ = 0x0; |
|---|
| 133 | |
|---|
| 134 | if (Core::isMaster()) |
|---|
| 135 | if (this->health_ <= 0) |
|---|
| 136 | this->death(); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void Pawn::setPlayer(PlayerInfo* player) |
|---|
| 140 | { |
|---|
| 141 | ControllableEntity::setPlayer(player); |
|---|
| 142 | |
|---|
| 143 | if (this->getGametype()) |
|---|
| 144 | this->getGametype()->playerStartsControllingPawn(player, this); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void Pawn::removePlayer() |
|---|
| 148 | { |
|---|
| 149 | if (this->getGametype()) |
|---|
| 150 | this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this); |
|---|
| 151 | |
|---|
| 152 | ControllableEntity::removePlayer(); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | void Pawn::setHealth(float health) |
|---|
| 156 | { |
|---|
| 157 | this->health_ = min(health, this->maxHealth_); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | void Pawn::damage(float damage, Pawn* originator) |
|---|
| 161 | { |
|---|
| 162 | if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator)) |
|---|
| 163 | { |
|---|
| 164 | this->setHealth(this->health_ - damage); |
|---|
| 165 | this->lastHitOriginator_ = originator; |
|---|
| 166 | |
|---|
| 167 | // play damage effect |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void Pawn::hit(Pawn* originator, const Vector3& force, float damage) |
|---|
| 172 | { |
|---|
| 173 | if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator)) |
|---|
| 174 | { |
|---|
| 175 | this->damage(damage, originator); |
|---|
| 176 | this->setVelocity(this->getVelocity() + force); |
|---|
| 177 | |
|---|
| 178 | // play hit effect |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | void Pawn::kill() |
|---|
| 183 | { |
|---|
| 184 | this->damage(this->health_); |
|---|
| 185 | this->death(); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void Pawn::spawneffect() |
|---|
| 189 | { |
|---|
| 190 | // play spawn effect |
|---|
| 191 | if (this->spawnparticlesource_ != "") |
|---|
| 192 | { |
|---|
| 193 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
|---|
| 194 | effect->setPosition(this->getPosition()); |
|---|
| 195 | effect->setOrientation(this->getOrientation()); |
|---|
| 196 | effect->setDestroyAfterLife(true); |
|---|
| 197 | effect->setSource(this->spawnparticlesource_); |
|---|
| 198 | effect->setLifetime(this->spawnparticleduration_); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | void Pawn::death() |
|---|
| 203 | { |
|---|
| 204 | if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_)) |
|---|
| 205 | { |
|---|
| 206 | // Set bAlive_ to false and wait for PawnManager to do the destruction |
|---|
| 207 | this->bAlive_ = false; |
|---|
| 208 | |
|---|
| 209 | this->setDestroyWhenPlayerLeft(false); |
|---|
| 210 | |
|---|
| 211 | if (this->getGametype()) |
|---|
| 212 | this->getGametype()->pawnKilled(this, this->lastHitOriginator_); |
|---|
| 213 | |
|---|
| 214 | if (this->getPlayer()) |
|---|
| 215 | this->getPlayer()->stopControl(this); |
|---|
| 216 | |
|---|
| 217 | if (Core::isMaster()) |
|---|
| 218 | this->deatheffect(); |
|---|
| 219 | } |
|---|
| 220 | else |
|---|
| 221 | this->setHealth(1); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | void Pawn::deatheffect() |
|---|
| 225 | { |
|---|
| 226 | // play death effect |
|---|
| 227 | { |
|---|
| 228 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
|---|
| 229 | effect->setPosition(this->getPosition()); |
|---|
| 230 | effect->setOrientation(this->getOrientation()); |
|---|
| 231 | effect->setDestroyAfterLife(true); |
|---|
| 232 | effect->setSource("Orxonox/explosion2b"); |
|---|
| 233 | effect->setLifetime(4.0f); |
|---|
| 234 | } |
|---|
| 235 | { |
|---|
| 236 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
|---|
| 237 | effect->setPosition(this->getPosition()); |
|---|
| 238 | effect->setOrientation(this->getOrientation()); |
|---|
| 239 | effect->setDestroyAfterLife(true); |
|---|
| 240 | effect->setSource("Orxonox/smoke6"); |
|---|
| 241 | effect->setLifetime(4.0f); |
|---|
| 242 | } |
|---|
| 243 | { |
|---|
| 244 | ParticleSpawner* effect = new ParticleSpawner(this->getCreator()); |
|---|
| 245 | effect->setPosition(this->getPosition()); |
|---|
| 246 | effect->setOrientation(this->getOrientation()); |
|---|
| 247 | effect->setDestroyAfterLife(true); |
|---|
| 248 | effect->setSource("Orxonox/sparks"); |
|---|
| 249 | effect->setLifetime(4.0f); |
|---|
| 250 | } |
|---|
| 251 | for (unsigned int i = 0; i < this->numexplosionchunks_; ++i) |
|---|
| 252 | { |
|---|
| 253 | ExplosionChunk* chunk = new ExplosionChunk(this->getCreator()); |
|---|
| 254 | chunk->setPosition(this->getPosition()); |
|---|
| 255 | |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | void Pawn::fire(WeaponMode::Enum fireMode) |
|---|
| 260 | { |
|---|
| 261 | doFire(fireMode); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | void Pawn::doFire(uint8_t fireMode) |
|---|
| 265 | { |
|---|
| 266 | if(Core::isMaster()) |
|---|
| 267 | { |
|---|
| 268 | if (this->weaponSystem_) |
|---|
| 269 | this->weaponSystem_->fire((WeaponMode::Enum)fireMode); |
|---|
| 270 | } |
|---|
| 271 | else |
|---|
| 272 | { |
|---|
| 273 | callMemberNetworkFunction( Pawn, doFire, this->getObjectID(), 0, ((uint8_t)fireMode)); |
|---|
| 274 | if (this->weaponSystem_) |
|---|
| 275 | this->weaponSystem_->fire((WeaponMode::Enum)fireMode); |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | void Pawn::postSpawn() |
|---|
| 280 | { |
|---|
| 281 | this->setHealth(this->initialHealth_); |
|---|
| 282 | if (Core::isMaster()) |
|---|
| 283 | this->spawneffect(); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | void Pawn::dropItems() |
|---|
| 287 | { |
|---|
| 288 | pickUp.eraseAll(); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | void Pawn::setWeaponSlot(WeaponSlot * wSlot) |
|---|
| 292 | { |
|---|
| 293 | this->attach(wSlot); |
|---|
| 294 | if (this->weaponSystem_) |
|---|
| 295 | this->weaponSystem_->attachWeaponSlot(wSlot); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const |
|---|
| 299 | { |
|---|
| 300 | if (this->weaponSystem_) |
|---|
| 301 | return this->weaponSystem_->getWeaponSlotPointer(index); |
|---|
| 302 | else |
|---|
| 303 | return 0; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | void Pawn::setWeaponPack(WeaponPack * wPack) |
|---|
| 307 | { |
|---|
| 308 | if (this->weaponSystem_) |
|---|
| 309 | { |
|---|
| 310 | wPack->setParentWeaponSystem(this->weaponSystem_); |
|---|
| 311 | wPack->setParentWeaponSystemToAllWeapons(this->weaponSystem_); |
|---|
| 312 | this->weaponSystem_->attachWeaponPack( wPack,wPack->getFireMode() ); |
|---|
| 313 | wPack->attachNeededMunitionToAllWeapons(); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | WeaponPack * Pawn::getWeaponPack(unsigned int firemode) const |
|---|
| 318 | { |
|---|
| 319 | if (this->weaponSystem_) |
|---|
| 320 | return this->weaponSystem_->getWeaponPackPointer(firemode); |
|---|
| 321 | else |
|---|
| 322 | return 0; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | void Pawn::setWeaponSet(WeaponSet * wSet) |
|---|
| 326 | { |
|---|
| 327 | if (this->weaponSystem_) |
|---|
| 328 | this->weaponSystem_->attachWeaponSet(wSet); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | WeaponSet * Pawn::getWeaponSet(unsigned int index) const |
|---|
| 332 | { |
|---|
| 333 | if (this->weaponSystem_) |
|---|
| 334 | return this->weaponSystem_->getWeaponSetPointer(index); |
|---|
| 335 | else |
|---|
| 336 | return 0; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | /////////////////// |
|---|
| 341 | // Pawn Listener // |
|---|
| 342 | /////////////////// |
|---|
| 343 | PawnListener::PawnListener() |
|---|
| 344 | { |
|---|
| 345 | RegisterRootObject(PawnListener); |
|---|
| 346 | } |
|---|
| 347 | } |
|---|