Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9939 was 9939, checked in by jo, 11 years ago

presentationHS13 branch merged into trunk

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