Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11064 was 11052, checked in by landauf, 9 years ago

merged branch presentationHS15 back to trunk

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