Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10669 was 10669, checked in by vaydin, 9 years ago

created new explosion class

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