Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/pawns/Pawn.cc @ 9550

Last change on this file since 9550 was 9546, checked in by smerkli, 12 years ago

Added a BigExplosion creation to the Pawn constructor.
This loads all the files for the first time and hence
gets rid of the long delay when the first ship is
killed.

  • Property svn:eol-style set to native
File size: 15.6 KB
RevLine 
[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:
[8706]25 *      Simon Miescher
[2072]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
49namespace orxonox
50{
51    CreateFactory(Pawn);
52
[7163]53    Pawn::Pawn(BaseObject* creator)
54        : ControllableEntity(creator)
55        , RadarViewable(creator, static_cast<WorldEntity*>(this))
[2072]56    {
57        RegisterObject(Pawn);
58
[2662]59        this->bAlive_ = true;
[3053]60        this->bReload_ = false;
[2072]61
62        this->health_ = 0;
63        this->maxHealth_ = 0;
64        this->initialHealth_ = 0;
[8706]65
[7163]66        this->shieldHealth_ = 0;
[8706]67        this->initialShieldHealth_ = 0;
68        this->maxShieldHealth_ = 100; //otherwise shield might increase to float_max
[7163]69        this->shieldAbsorption_ = 0.5;
[2072]70
[8706]71        this->reloadRate_ = 0;
72        this->reloadWaitTime_ = 1.0f;
73        this->reloadWaitCountdown_ = 0;
74
[2072]75        this->lastHitOriginator_ = 0;
76
[9348]77        // set damage multiplier to default value 1, meaning nominal damage
78        this->damageMultiplier_ = 1;
79
[2662]80        this->spawnparticleduration_ = 3.0f;
[2098]81
[6417]82        this->aimPosition_ = Vector3::ZERO;
83
[2896]84        if (GameMode::isMaster())
[2662]85        {
86            this->weaponSystem_ = new WeaponSystem(this);
[3053]87            this->weaponSystem_->setPawn(this);
[2662]88        }
89        else
90            this->weaponSystem_ = 0;
91
92        this->setRadarObjectColour(ColourValue::Red);
93        this->setRadarObjectShape(RadarViewable::Dot);
94
[2072]95        this->registerVariables();
[3089]96
97        this->isHumanShip_ = this->hasLocalController();
[8891]98
[8329]99        this->setSyncMode(ObjectDirection::Bidirectional); // needed to synchronise e.g. aimposition
[9546]100
101        /* sewper awesome optimization. This loads all the files required for
102         * the big explosion once in the beginning to make sure the game does
103         * not start lagging at the first in-action explosion.
104         */
105        BigExplosion *chunk = new BigExplosion(this->getCreator());
[2072]106    }
107
108    Pawn::~Pawn()
109    {
[2662]110        if (this->isInitialized())
111        {
112            if (this->weaponSystem_)
[5929]113                this->weaponSystem_->destroy();
[2662]114        }
[2072]115    }
116
117    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
118    {
119        SUPER(Pawn, XMLPort, xmlelement, mode);
120
[2662]121        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
[2072]122        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
123        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
[7163]124
125        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
[8706]126        XMLPortParam(Pawn, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0);
127        XMLPortParam(Pawn, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
[7163]128        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
129
[2662]130        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
131        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
132        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
133
[3053]134        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
135        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
[6417]136        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
[8706]137
138        XMLPortParam(Pawn, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0);
139        XMLPortParam(Pawn, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f);
[9254]140
[9257]141        XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode );
[2072]142    }
143
144    void Pawn::registerVariables()
145    {
[7163]146        registerVariable(this->bAlive_,           VariableDirection::ToClient);
147        registerVariable(this->health_,           VariableDirection::ToClient);
[8706]148        registerVariable(this->maxHealth_,        VariableDirection::ToClient);
[7163]149        registerVariable(this->shieldHealth_,     VariableDirection::ToClient);
[8706]150        registerVariable(this->maxShieldHealth_,  VariableDirection::ToClient);
[7163]151        registerVariable(this->shieldAbsorption_, VariableDirection::ToClient);
152        registerVariable(this->bReload_,          VariableDirection::ToServer);
153        registerVariable(this->aimPosition_,      VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
[2072]154    }
155
156    void Pawn::tick(float dt)
157    {
[2809]158        SUPER(Pawn, tick, dt);
[2072]159
[3053]160        this->bReload_ = false;
[2662]161
[8706]162        // TODO: use the existing timer functions instead
163        if(this->reloadWaitCountdown_ > 0)
164        {
165            this->decreaseReloadCountdownTime(dt);
166        }
167        else
168        {
169            this->addShieldHealth(this->getReloadRate() * dt);
170            this->resetReloadCountdown();
171        }
172
[3084]173        if (GameMode::isMaster())
[8706]174        {
[3087]175            if (this->health_ <= 0 && bAlive_)
[6864]176            {
[8706]177                this->fireEvent(); // Event to notify anyone who wants to know about the death.
[3087]178                this->death();
[6864]179            }
[8706]180        }
[2072]181    }
182
[7889]183    void Pawn::preDestroy()
184    {
185        // yay, multiple inheritance!
186        this->ControllableEntity::preDestroy();
187        this->PickupCarrier::preDestroy();
188    }
189
[2826]190    void Pawn::setPlayer(PlayerInfo* player)
191    {
192        ControllableEntity::setPlayer(player);
193
194        if (this->getGametype())
195            this->getGametype()->playerStartsControllingPawn(player, this);
196    }
197
198    void Pawn::removePlayer()
199    {
200        if (this->getGametype())
201            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
202
203        ControllableEntity::removePlayer();
204    }
205
[8706]206
[2072]207    void Pawn::setHealth(float health)
208    {
[8706]209        this->health_ = std::min(health, this->maxHealth_); //Health can't be set to a value bigger than maxHealth, otherwise it will be reduced at first hit
[2072]210    }
211
[8706]212    void Pawn::setShieldHealth(float shieldHealth)
[2072]213    {
[8706]214        this->shieldHealth_ = std::min(shieldHealth, this->maxShieldHealth_);
215    }
216
217    void Pawn::setMaxShieldHealth(float maxshieldhealth)
218    {
219        this->maxShieldHealth_ = maxshieldhealth;
220    }
221
222    void Pawn::setReloadRate(float reloadrate)
223    {
224        this->reloadRate_ = reloadrate;
225    }
226
227    void Pawn::setReloadWaitTime(float reloadwaittime)
228    {
229        this->reloadWaitTime_ = reloadwaittime;
230    }
231
232    void Pawn::decreaseReloadCountdownTime(float dt)
233    {
234        this->reloadWaitCountdown_ -= dt;
235    }
236
237    void Pawn::damage(float damage, float healthdamage, float shielddamage, Pawn* originator)
238    {
[9348]239        // Applies multiplier given by the DamageBoost Pickup.
240        if (originator)
241            damage *= originator->getDamageMultiplier();
242
[2826]243        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
244        {
[8706]245            if (shielddamage >= this->getShieldHealth())
[7163]246            {
247                this->setShieldHealth(0);
[8706]248                this->setHealth(this->health_ - (healthdamage + damage));
[7163]249            }
[8706]250            else
251            {
252                this->setShieldHealth(this->shieldHealth_ - shielddamage);
[7163]253
[8706]254                // remove remaining shieldAbsorpton-Part of damage from shield
255                shielddamage = damage * this->shieldAbsorption_;
256                shielddamage = std::min(this->getShieldHealth(),shielddamage);
257                this->setShieldHealth(this->shieldHealth_ - shielddamage);
[7163]258
[8706]259                // set remaining damage to health
260                this->setHealth(this->health_ - (damage - shielddamage) - healthdamage);
[7163]261            }
262
[2826]263            this->lastHitOriginator_ = originator;
264        }
[2072]265    }
266
[8706]267// TODO: Still valid?
268/* HIT-Funktionen
269    Die hit-Funktionen muessen auch in src/orxonox/controllers/Controller.h angepasst werden! (Visuelle Effekte)
270
271*/
272    void Pawn::hit(Pawn* originator, const Vector3& force, float damage, float healthdamage, float shielddamage)
[2072]273    {
[6417]274        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
[2826]275        {
[8706]276            this->damage(damage, healthdamage, shielddamage, originator);
[2826]277            this->setVelocity(this->getVelocity() + force);
278        }
[2072]279    }
280
[8706]281
282    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage, float healthdamage, float shielddamage)
[6417]283    {
284        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
285        {
[8706]286            this->damage(damage, healthdamage, shielddamage, originator);
[6417]287
288            if ( this->getController() )
[8706]289                this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage?
[6417]290        }
291    }
292
[8706]293
[2072]294    void Pawn::kill()
295    {
296        this->damage(this->health_);
297        this->death();
298    }
299
[2662]300    void Pawn::spawneffect()
[2072]301    {
302        // play spawn effect
[6417]303        if (!this->spawnparticlesource_.empty())
[2662]304        {
305            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
306            effect->setPosition(this->getPosition());
307            effect->setOrientation(this->getOrientation());
308            effect->setDestroyAfterLife(true);
309            effect->setSource(this->spawnparticlesource_);
310            effect->setLifetime(this->spawnparticleduration_);
311        }
[2072]312    }
313
314    void Pawn::death()
315    {
[3033]316        this->setHealth(1);
[2826]317        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
318        {
319            // Set bAlive_ to false and wait for PawnManager to do the destruction
320            this->bAlive_ = false;
[2662]321
[2826]322            this->setDestroyWhenPlayerLeft(false);
[2662]323
[2826]324            if (this->getGametype())
325                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
[2662]326
[3038]327            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
328                this->getPlayer()->stopControl();
[2072]329
[2896]330            if (GameMode::isMaster())
[3087]331            {
332//                this->deathEffect();
333                this->goWithStyle();
334            }
[2826]335        }
[2662]336    }
[3087]337    void Pawn::goWithStyle()
338    {
339        this->bAlive_ = false;
340        this->setDestroyWhenPlayerLeft(false);
[2072]341
[3087]342        BigExplosion* chunk = new BigExplosion(this->getCreator());
343        chunk->setPosition(this->getPosition());
344
345    }
[2662]346    void Pawn::deatheffect()
347    {
[2072]348        // play death effect
[2662]349        {
350            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
351            effect->setPosition(this->getPosition());
352            effect->setOrientation(this->getOrientation());
353            effect->setDestroyAfterLife(true);
354            effect->setSource("Orxonox/explosion2b");
355            effect->setLifetime(4.0f);
356        }
357        {
358            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
359            effect->setPosition(this->getPosition());
360            effect->setOrientation(this->getOrientation());
361            effect->setDestroyAfterLife(true);
362            effect->setSource("Orxonox/smoke6");
363            effect->setLifetime(4.0f);
364        }
365        {
366            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
367            effect->setPosition(this->getPosition());
368            effect->setOrientation(this->getOrientation());
369            effect->setDestroyAfterLife(true);
370            effect->setSource("Orxonox/sparks");
371            effect->setLifetime(4.0f);
372        }
373        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
374        {
375            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
376            chunk->setPosition(this->getPosition());
377        }
[2072]378    }
379
[6417]380    void Pawn::fired(unsigned int firemode)
[2098]381    {
[6417]382        if (this->weaponSystem_)
383            this->weaponSystem_->fire(firemode);
[2098]384    }
385
[3053]386    void Pawn::reload()
387    {
388        this->bReload_ = true;
389    }
390
[2072]391    void Pawn::postSpawn()
392    {
393        this->setHealth(this->initialHealth_);
[2896]394        if (GameMode::isMaster())
[2662]395            this->spawneffect();
[2072]396    }
[2662]397
[2893]398    /* WeaponSystem:
399    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
400    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
401    *       --> e.g. Pickup-Items
402    */
[3053]403    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
[2662]404    {
405        this->attach(wSlot);
406        if (this->weaponSystem_)
[3053]407            this->weaponSystem_->addWeaponSlot(wSlot);
[2662]408    }
409
410    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
411    {
412        if (this->weaponSystem_)
[3053]413            return this->weaponSystem_->getWeaponSlot(index);
[2662]414        else
415            return 0;
416    }
417
[3053]418    void Pawn::addWeaponSet(WeaponSet * wSet)
[2662]419    {
420        if (this->weaponSystem_)
[3053]421            this->weaponSystem_->addWeaponSet(wSet);
[2662]422    }
423
[3053]424    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
[2662]425    {
426        if (this->weaponSystem_)
[3053]427            return this->weaponSystem_->getWeaponSet(index);
[2662]428        else
429            return 0;
430    }
431
[3053]432    void Pawn::addWeaponPack(WeaponPack * wPack)
[2662]433    {
434        if (this->weaponSystem_)
[7163]435        {
[3053]436            this->weaponSystem_->addWeaponPack(wPack);
[7163]437            this->addedWeaponPack(wPack);
438        }
[2662]439    }
440
[6417]441    void Pawn::addWeaponPackXML(WeaponPack * wPack)
442    {
443        if (this->weaponSystem_)
[7163]444        {
[6417]445            if (!this->weaponSystem_->addWeaponPack(wPack))
446                wPack->destroy();
[7163]447            else
448                this->addedWeaponPack(wPack);
449        }
[6417]450    }
451
[3053]452    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
[2662]453    {
454        if (this->weaponSystem_)
[3053]455            return this->weaponSystem_->getWeaponPack(index);
[2662]456        else
457            return 0;
458    }
459
[3089]460    //Tell the Map (RadarViewable), if this is a playership
461    void Pawn::startLocalHumanControl()
462    {
463//        SUPER(ControllableEntity, changedPlayer());
464        ControllableEntity::startLocalHumanControl();
465        this->isHumanShip_ = true;
466    }
[8891]467
468    void Pawn::changedVisibility(void)
469    {
470        SUPER(Pawn, changedVisibility);
[9254]471
472        // enable proper radarviewability when the visibility is changed
473        this->RadarViewable::settingsChanged();
[8891]474    }
475
[2072]476}
Note: See TracBrowser for help on using the repository browser.