Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10852 was 10650, checked in by fvultier, 9 years ago

There is now a spaceship that uses the gravity bomb. Minor documentation improvements.

  • Property svn:eol-style set to native
File size: 21.0 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"
[9939]48#include "sound/WorldSound.h"
[2072]49
[9625]50#include "controllers/FormationController.h"
51
[2072]52namespace orxonox
53{
[9667]54    RegisterClass(Pawn);
[2072]55
[9667]56    Pawn::Pawn(Context* context)
57        : ControllableEntity(context)
58        , RadarViewable(this, static_cast<WorldEntity*>(this))
[2072]59    {
60        RegisterObject(Pawn);
61
[2662]62        this->bAlive_ = true;
[3053]63        this->bReload_ = false;
[2072]64
65        this->health_ = 0;
66        this->maxHealth_ = 0;
67        this->initialHealth_ = 0;
[8706]68
[7163]69        this->shieldHealth_ = 0;
[8706]70        this->initialShieldHealth_ = 0;
71        this->maxShieldHealth_ = 100; //otherwise shield might increase to float_max
[7163]72        this->shieldAbsorption_ = 0.5;
[2072]73
[8706]74        this->reloadRate_ = 0;
75        this->reloadWaitTime_ = 1.0f;
76        this->reloadWaitCountdown_ = 0;
77
[2072]78        this->lastHitOriginator_ = 0;
79
[9348]80        // set damage multiplier to default value 1, meaning nominal damage
81        this->damageMultiplier_ = 1;
82
[2662]83        this->spawnparticleduration_ = 3.0f;
[2098]84
[6417]85        this->aimPosition_ = Vector3::ZERO;
86
[2896]87        if (GameMode::isMaster())
[2662]88        {
[9667]89            this->weaponSystem_ = new WeaponSystem(this->getContext());
[3053]90            this->weaponSystem_->setPawn(this);
[2662]91        }
92        else
93            this->weaponSystem_ = 0;
94
95        this->setRadarObjectColour(ColourValue::Red);
96        this->setRadarObjectShape(RadarViewable::Dot);
97
[2072]98        this->registerVariables();
[3089]99
100        this->isHumanShip_ = this->hasLocalController();
[8891]101
[8329]102        this->setSyncMode(ObjectDirection::Bidirectional); // needed to synchronise e.g. aimposition
[9939]103
104        if (GameMode::isMaster())
105        {
106            this->explosionSound_ = new WorldSound(this->getContext());
107            this->explosionSound_->setVolume(1.0f);
108        }
109        else
110        {
111            this->explosionSound_ = 0;
112        }
[2072]113    }
114
115    Pawn::~Pawn()
116    {
[2662]117        if (this->isInitialized())
118        {
119            if (this->weaponSystem_)
[5929]120                this->weaponSystem_->destroy();
[2662]121        }
[2072]122    }
123
124    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
125    {
126        SUPER(Pawn, XMLPort, xmlelement, mode);
127
[2662]128        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
[2072]129        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
130        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
[7163]131
132        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
[8706]133        XMLPortParam(Pawn, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0);
134        XMLPortParam(Pawn, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
[7163]135        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
136
[2662]137        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
138        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
139        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
140
[3053]141        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
142        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
[6417]143        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
[8706]144
145        XMLPortParam(Pawn, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0);
146        XMLPortParam(Pawn, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f);
[9254]147
[9939]148        XMLPortParam(Pawn, "explosionSound",  setExplosionSound,  getExplosionSound,  xmlelement, mode);
149
[9257]150        XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode );
[2072]151    }
152
153    void Pawn::registerVariables()
154    {
[7163]155        registerVariable(this->bAlive_,           VariableDirection::ToClient);
156        registerVariable(this->health_,           VariableDirection::ToClient);
[8706]157        registerVariable(this->maxHealth_,        VariableDirection::ToClient);
[7163]158        registerVariable(this->shieldHealth_,     VariableDirection::ToClient);
[8706]159        registerVariable(this->maxShieldHealth_,  VariableDirection::ToClient);
[7163]160        registerVariable(this->shieldAbsorption_, VariableDirection::ToClient);
161        registerVariable(this->bReload_,          VariableDirection::ToServer);
162        registerVariable(this->aimPosition_,      VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
[2072]163    }
164
165    void Pawn::tick(float dt)
166    {
[2809]167        SUPER(Pawn, tick, dt);
[2072]168
[3053]169        this->bReload_ = false;
[2662]170
[8706]171        // TODO: use the existing timer functions instead
172        if(this->reloadWaitCountdown_ > 0)
173        {
174            this->decreaseReloadCountdownTime(dt);
175        }
176        else
177        {
178            this->addShieldHealth(this->getReloadRate() * dt);
179            this->resetReloadCountdown();
180        }
181
[3084]182        if (GameMode::isMaster())
[8706]183        {
[3087]184            if (this->health_ <= 0 && bAlive_)
[6864]185            {
[8706]186                this->fireEvent(); // Event to notify anyone who wants to know about the death.
[3087]187                this->death();
[6864]188            }
[8706]189        }
[2072]190    }
191
[7889]192    void Pawn::preDestroy()
193    {
194        // yay, multiple inheritance!
195        this->ControllableEntity::preDestroy();
196        this->PickupCarrier::preDestroy();
197    }
198
[2826]199    void Pawn::setPlayer(PlayerInfo* player)
200    {
201        ControllableEntity::setPlayer(player);
202
203        if (this->getGametype())
204            this->getGametype()->playerStartsControllingPawn(player, this);
205    }
206
207    void Pawn::removePlayer()
208    {
209        if (this->getGametype())
210            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
211
212        ControllableEntity::removePlayer();
213    }
214
[8706]215
[2072]216    void Pawn::setHealth(float health)
217    {
[8706]218        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]219    }
220
[8706]221    void Pawn::setShieldHealth(float shieldHealth)
[2072]222    {
[8706]223        this->shieldHealth_ = std::min(shieldHealth, this->maxShieldHealth_);
224    }
225
226    void Pawn::setMaxShieldHealth(float maxshieldhealth)
227    {
228        this->maxShieldHealth_ = maxshieldhealth;
229    }
230
231    void Pawn::setReloadRate(float reloadrate)
232    {
233        this->reloadRate_ = reloadrate;
234    }
235
236    void Pawn::setReloadWaitTime(float reloadwaittime)
237    {
238        this->reloadWaitTime_ = reloadwaittime;
239    }
240
241    void Pawn::decreaseReloadCountdownTime(float dt)
242    {
243        this->reloadWaitCountdown_ -= dt;
244    }
245
[10216]246    void Pawn::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs)
[8706]247    {
[9348]248        // Applies multiplier given by the DamageBoost Pickup.
249        if (originator)
250            damage *= originator->getDamageMultiplier();
251
[2826]252        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
253        {
[8706]254            if (shielddamage >= this->getShieldHealth())
[7163]255            {
256                this->setShieldHealth(0);
[8706]257                this->setHealth(this->health_ - (healthdamage + damage));
[7163]258            }
[8706]259            else
260            {
261                this->setShieldHealth(this->shieldHealth_ - shielddamage);
[7163]262
[8706]263                // remove remaining shieldAbsorpton-Part of damage from shield
264                shielddamage = damage * this->shieldAbsorption_;
265                shielddamage = std::min(this->getShieldHealth(),shielddamage);
266                this->setShieldHealth(this->shieldHealth_ - shielddamage);
[7163]267
[8706]268                // set remaining damage to health
269                this->setHealth(this->health_ - (damage - shielddamage) - healthdamage);
[7163]270            }
271
[2826]272            this->lastHitOriginator_ = originator;
273        }
[2072]274    }
275
[8706]276// TODO: Still valid?
277/* HIT-Funktionen
278    Die hit-Funktionen muessen auch in src/orxonox/controllers/Controller.h angepasst werden! (Visuelle Effekte)
279
280*/
[10216]281    void Pawn::hit(Pawn* originator, const Vector3& force, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage)
[2072]282    {
[6417]283        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
[2826]284        {
[10216]285            this->damage(damage, healthdamage, shielddamage, originator, cs);
[2826]286            this->setVelocity(this->getVelocity() + force);
287        }
[2072]288    }
289
[10216]290    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage)
[6417]291    {
292        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
293        {
[10216]294            this->damage(damage, healthdamage, shielddamage, originator, cs);
[6417]295
296            if ( this->getController() )
[8706]297                this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage?
[6417]298        }
299    }
300
[2072]301    void Pawn::kill()
302    {
[9945]303        this->damage(this->health_);
[2072]304        this->death();
305    }
306
[2662]307    void Pawn::spawneffect()
[2072]308    {
309        // play spawn effect
[6417]310        if (!this->spawnparticlesource_.empty())
[2662]311        {
[9667]312            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
[2662]313            effect->setPosition(this->getPosition());
314            effect->setOrientation(this->getOrientation());
315            effect->setDestroyAfterLife(true);
316            effect->setSource(this->spawnparticlesource_);
317            effect->setLifetime(this->spawnparticleduration_);
318        }
[2072]319    }
320
321    void Pawn::death()
322    {
[3033]323        this->setHealth(1);
[2826]324        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
325        {
[10624]326            // Set bAlive_ to false and wait for destroyLater() to do the destruction
[2826]327            this->bAlive_ = false;
[10624]328            this->destroyLater();
[2662]329
[2826]330            this->setDestroyWhenPlayerLeft(false);
[2662]331
[2826]332            if (this->getGametype())
333                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
[2662]334
[3038]335            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
[9625]336            {
[9663]337                // Start to control a new entity if you're the master of a formation
338                if(this->hasSlaves())
339                {
340                    Controller* slave = this->getSlave();
341                    ControllableEntity* entity = slave->getControllableEntity();
[2072]342
[9625]343
[9663]344                    if(!entity->hasHumanController())
345                    {
[9666]346                        // delete the AIController // <-- TODO: delete? nothing is deleted here... should we delete the controller?
[9663]347                        slave->setControllableEntity(0);
[9625]348
[9663]349                        // set a new master within the formation
350                        orxonox_cast<FormationController*>(this->getController())->setNewMasterWithinFormation(orxonox_cast<FormationController*>(slave));
[9625]351
[9663]352                        // start to control a slave
353                        this->getPlayer()->startControl(entity);
354                    }
355                    else
356                    {
357                        this->getPlayer()->stopControl();
358                    }
[9625]359
[9663]360                }
361                else
362                {
363                    this->getPlayer()->stopControl();
364                }
[9625]365            }
[2896]366            if (GameMode::isMaster())
[3087]367            {
[10622]368                this->deatheffect();
[3087]369                this->goWithStyle();
370            }
[2826]371        }
[2662]372    }
[3087]373    void Pawn::goWithStyle()
374    {
375        this->bAlive_ = false;
376        this->setDestroyWhenPlayerLeft(false);
[2072]377
[9949]378        BigExplosion* chunk = new BigExplosion(this->getContext());
[3087]379        chunk->setPosition(this->getPosition());
[9949]380        chunk->setVelocity(this->getVelocity());
[3087]381
[9947]382        this->explosionSound_->setPosition(this->getPosition());
383        this->explosionSound_->play();
[3087]384    }
[2662]385    void Pawn::deatheffect()
386    {
[2072]387        // play death effect
[10622]388        /*{
[9667]389            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
[2662]390            effect->setPosition(this->getPosition());
391            effect->setOrientation(this->getOrientation());
392            effect->setDestroyAfterLife(true);
393            effect->setSource("Orxonox/explosion2b");
394            effect->setLifetime(4.0f);
395        }
396        {
[9667]397            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
[2662]398            effect->setPosition(this->getPosition());
399            effect->setOrientation(this->getOrientation());
400            effect->setDestroyAfterLife(true);
401            effect->setSource("Orxonox/smoke6");
402            effect->setLifetime(4.0f);
403        }
404        {
[9667]405            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
[2662]406            effect->setPosition(this->getPosition());
407            effect->setOrientation(this->getOrientation());
408            effect->setDestroyAfterLife(true);
409            effect->setSource("Orxonox/sparks");
410            effect->setLifetime(4.0f);
[10622]411        }*/
412       
413       
414        {
415            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
416            effect->setPosition(this->getPosition());
417            effect->setOrientation(this->getOrientation());
418            effect->setDestroyAfterLife(true);
419            effect->setSource("orxonox/explosion_flash2");
420            effect->setLifetime(5.0f);
[2662]421        }
[10622]422        {
423            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
424            effect->setPosition(this->getPosition());
425            effect->setOrientation(this->getOrientation());
426            effect->setDestroyAfterLife(true);
427            effect->setSource("orxonox/explosion_flame2");
428            effect->setLifetime(5.0f);
429        }
430        {
431            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
432            effect->setPosition(this->getPosition());
433            effect->setOrientation(this->getOrientation());
434            effect->setDestroyAfterLife(true);
435            effect->setSource("orxonox/explosion_shockwave2");
436            effect->scale(20);
437            effect->setLifetime(5.0f);
438        }{
439            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
440            effect->setPosition(this->getPosition());
441            effect->setOrientation(this->getOrientation());
442            effect->setDestroyAfterLife(true);
443            effect->setSource("orxonox/explosion_sparks2");
444            effect->setLifetime(5.0f);
445        }
446        {
447            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
448            effect->setPosition(this->getPosition());
449            effect->setOrientation(this->getOrientation());
450            effect->setDestroyAfterLife(true);
451            effect->setSource("orxonox/explosion_streak2");
452            effect->setLifetime(5.0f);
453        }
454        {
455            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
456            effect->setPosition(this->getPosition());
457            effect->setOrientation(this->getOrientation());
458            effect->setDestroyAfterLife(true);
459            effect->setSource("orxonox/explosion_afterglow");
460            effect->scale(20);
461            effect->setLifetime(5.0f);
462        }
463       
464       
[2662]465        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
466        {
[9667]467            ExplosionChunk* chunk = new ExplosionChunk(this->getContext());
[2662]468            chunk->setPosition(this->getPosition());
469        }
[2072]470    }
471
[10650]472    /**
473    @brief
474        Check whether the Pawn has a @ref Orxonox::WeaponSystem and fire it with the specified firemode if it has one.
475    */
[6417]476    void Pawn::fired(unsigned int firemode)
[2098]477    {
[6417]478        if (this->weaponSystem_)
479            this->weaponSystem_->fire(firemode);
[2098]480    }
481
[3053]482    void Pawn::reload()
483    {
484        this->bReload_ = true;
485    }
486
[2072]487    void Pawn::postSpawn()
488    {
489        this->setHealth(this->initialHealth_);
[2896]490        if (GameMode::isMaster())
[2662]491            this->spawneffect();
[2072]492    }
[2662]493
[2893]494    /* WeaponSystem:
495    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
496    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
497    *       --> e.g. Pickup-Items
498    */
[3053]499    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
[2662]500    {
501        this->attach(wSlot);
502        if (this->weaponSystem_)
[3053]503            this->weaponSystem_->addWeaponSlot(wSlot);
[2662]504    }
505
506    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
507    {
508        if (this->weaponSystem_)
[3053]509            return this->weaponSystem_->getWeaponSlot(index);
[2662]510        else
511            return 0;
512    }
513
[3053]514    void Pawn::addWeaponSet(WeaponSet * wSet)
[2662]515    {
516        if (this->weaponSystem_)
[3053]517            this->weaponSystem_->addWeaponSet(wSet);
[2662]518    }
519
[3053]520    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
[2662]521    {
522        if (this->weaponSystem_)
[3053]523            return this->weaponSystem_->getWeaponSet(index);
[2662]524        else
525            return 0;
526    }
527
[3053]528    void Pawn::addWeaponPack(WeaponPack * wPack)
[2662]529    {
530        if (this->weaponSystem_)
[7163]531        {
[3053]532            this->weaponSystem_->addWeaponPack(wPack);
[7163]533            this->addedWeaponPack(wPack);
534        }
[2662]535    }
536
[6417]537    void Pawn::addWeaponPackXML(WeaponPack * wPack)
538    {
539        if (this->weaponSystem_)
[7163]540        {
[6417]541            if (!this->weaponSystem_->addWeaponPack(wPack))
542                wPack->destroy();
[7163]543            else
544                this->addedWeaponPack(wPack);
545        }
[6417]546    }
547
[3053]548    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
[2662]549    {
550        if (this->weaponSystem_)
[3053]551            return this->weaponSystem_->getWeaponPack(index);
[2662]552        else
553            return 0;
554    }
555
[3089]556    //Tell the Map (RadarViewable), if this is a playership
557    void Pawn::startLocalHumanControl()
558    {
559//        SUPER(ControllableEntity, changedPlayer());
560        ControllableEntity::startLocalHumanControl();
561        this->isHumanShip_ = true;
562    }
[8891]563
564    void Pawn::changedVisibility(void)
565    {
566        SUPER(Pawn, changedVisibility);
[9254]567
568        // enable proper radarviewability when the visibility is changed
569        this->RadarViewable::settingsChanged();
[8891]570    }
571
[9625]572
573    // A function to check if this pawn's controller is the master of any formationcontroller
574    bool Pawn::hasSlaves()
575    {
[9663]576        for (ObjectList<FormationController>::iterator it =
577             ObjectList<FormationController>::begin();
578             it != ObjectList<FormationController>::end(); ++it )
579        {
580            // checks if the pawn's controller has a slave
581            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
582                return true;
583        }
584        return false;
[9625]585    }
586
587    // A function that returns a slave of the pawn's controller
588    Controller* Pawn::getSlave(){
[9663]589        for (ObjectList<FormationController>::iterator it =
590                ObjectList<FormationController>::begin();
591                it != ObjectList<FormationController>::end(); ++it )
592        {
593            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
594                return it->getController();
595        }
596        return 0;
[9625]597    }
598
599
[9939]600    void Pawn::setExplosionSound(const std::string &explosionSound)
601    {
602        if(explosionSound_ )
603            explosionSound_->setSource(explosionSound);
604        else
605            assert(0); // This should never happen, because soundpointer is only available on master
606    }
[9625]607
[9939]608    const std::string& Pawn::getExplosionSound()
609    {
610        if( explosionSound_ )
611            return explosionSound_->getSource();
612        else
613            assert(0);
614        return BLANKSTRING;
615    }
[2072]616}
Note: See TracBrowser for help on using the repository browser.