Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/explosionChunksHS15/src/orxonox/worldentities/pawns/Pawn.cc @ 10752

Last change on this file since 10752 was 10752, checked in by vaydin, 8 years ago

Deleted previously created VaydinExplosion class, created ExplosionPart class

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