Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removed lag at first explosion

  • Property svn:eol-style set to native
File size: 21.5 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        while(!explosionPartList_.empty())
390        {
391            explosionPartList_.back()->setPosition(this->getPosition());
392            explosionPartList_.back()->setVelocity(this->getVelocity());
393            explosionPartList_.back()->Explode();
394            explosionPartList_.pop_back();
395        }
396
397        this->explosionSound_->setPosition(this->getPosition());
398        this->explosionSound_->play();
399    }
400    void Pawn::deatheffect()
401    {
402        orxout() << "deatheffect" << endl;
403        // play death effect
404        /*{
405            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
406            effect->setPosition(this->getPosition());
407            effect->setOrientation(this->getOrientation());
408            effect->setDestroyAfterLife(true);
409            effect->setSource("Orxonox/explosion2b");
410            effect->setLifetime(4.0f);
411        }
412        {
413            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
414            effect->setPosition(this->getPosition());
415            effect->setOrientation(this->getOrientation());
416            effect->setDestroyAfterLife(true);
417            effect->setSource("Orxonox/smoke6");
418            effect->setLifetime(4.0f);
419        }
420        {
421            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
422            effect->setPosition(this->getPosition());
423            effect->setOrientation(this->getOrientation());
424            effect->setDestroyAfterLife(true);
425            effect->setSource("Orxonox/sparks");
426            effect->setLifetime(4.0f);
427        }*/
428       
429       
430        {
431            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
432            effect->setPosition(this->getPosition());
433            effect->setOrientation(this->getOrientation());
434            effect->setDestroyAfterLife(true);
435            effect->setSource("orxonox/explosion_flash2");
436            effect->setLifetime(5.0f);
437        }
438        {
439            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
440            effect->setPosition(this->getPosition());
441            effect->setOrientation(this->getOrientation());
442            effect->setDestroyAfterLife(true);
443            effect->setSource("orxonox/explosion_flame2");
444            effect->setLifetime(5.0f);
445        }
446        {
447            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
448            effect->setPosition(this->getPosition());
449            effect->setOrientation(this->getOrientation());
450            effect->setDestroyAfterLife(true);
451            effect->setSource("orxonox/explosion_shockwave2");
452            effect->scale(20);
453            effect->setLifetime(5.0f);
454        }{
455            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
456            effect->setPosition(this->getPosition());
457            effect->setOrientation(this->getOrientation());
458            effect->setDestroyAfterLife(true);
459            effect->setSource("orxonox/explosion_sparks2");
460            effect->setLifetime(5.0f);
461        }
462        {
463            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
464            effect->setPosition(this->getPosition());
465            effect->setOrientation(this->getOrientation());
466            effect->setDestroyAfterLife(true);
467            effect->setSource("orxonox/explosion_streak2");
468            effect->setLifetime(5.0f);
469        }
470        {
471            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
472            effect->setPosition(this->getPosition());
473            effect->setOrientation(this->getOrientation());
474            effect->setDestroyAfterLife(true);
475            effect->setSource("orxonox/explosion_afterglow");
476            effect->scale(20);
477            effect->setLifetime(5.0f);
478        }
479       
480       
481        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
482        {
483            ExplosionChunk* chunk = new ExplosionChunk(this->getContext());
484            chunk->setPosition(this->getPosition());
485        }
486    }
487
488    void Pawn::fired(unsigned int firemode)
489    {
490        if (this->weaponSystem_)
491            this->weaponSystem_->fire(firemode);
492    }
493
494    void Pawn::reload()
495    {
496        this->bReload_ = true;
497    }
498
499    void Pawn::postSpawn()
500    {
501        this->setHealth(this->initialHealth_);
502        if (GameMode::isMaster())
503            this->spawneffect();
504    }
505
506
507    void Pawn::addExplosionPart(ExplosionPart* ePart)
508    {this->explosionPartList_.push_back(ePart);}
509
510
511    ExplosionPart * Pawn::getExplosionPart()
512    {return this->explosionPartList_.back();}
513
514
515
516    /* WeaponSystem:
517    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
518    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
519    *       --> e.g. Pickup-Items
520    */
521    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
522    {
523        this->attach(wSlot);
524        if (this->weaponSystem_)
525            this->weaponSystem_->addWeaponSlot(wSlot);
526    }
527
528    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
529    {
530        if (this->weaponSystem_)
531            return this->weaponSystem_->getWeaponSlot(index);
532        else
533            return 0;
534    }
535
536    void Pawn::addWeaponSet(WeaponSet * wSet)
537    {
538        if (this->weaponSystem_)
539            this->weaponSystem_->addWeaponSet(wSet);
540    }
541
542    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
543    {
544        if (this->weaponSystem_)
545            return this->weaponSystem_->getWeaponSet(index);
546        else
547            return 0;
548    }
549
550    void Pawn::addWeaponPack(WeaponPack * wPack)
551    {
552        if (this->weaponSystem_)
553        {
554            this->weaponSystem_->addWeaponPack(wPack);
555            this->addedWeaponPack(wPack);
556        }
557    }
558
559    void Pawn::addWeaponPackXML(WeaponPack * wPack)
560    {
561        if (this->weaponSystem_)
562        {
563            if (!this->weaponSystem_->addWeaponPack(wPack))
564                wPack->destroy();
565            else
566                this->addedWeaponPack(wPack);
567        }
568    }
569
570    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
571    {
572        if (this->weaponSystem_)
573            return this->weaponSystem_->getWeaponPack(index);
574        else
575            return 0;
576    }
577
578    //Tell the Map (RadarViewable), if this is a playership
579    void Pawn::startLocalHumanControl()
580    {
581//        SUPER(ControllableEntity, changedPlayer());
582        ControllableEntity::startLocalHumanControl();
583        this->isHumanShip_ = true;
584    }
585
586    void Pawn::changedVisibility(void)
587    {
588        SUPER(Pawn, changedVisibility);
589
590        // enable proper radarviewability when the visibility is changed
591        this->RadarViewable::settingsChanged();
592    }
593
594
595    // A function to check if this pawn's controller is the master of any formationcontroller
596    bool Pawn::hasSlaves()
597    {
598        for (ObjectList<FormationController>::iterator it =
599             ObjectList<FormationController>::begin();
600             it != ObjectList<FormationController>::end(); ++it )
601        {
602            // checks if the pawn's controller has a slave
603            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
604                return true;
605        }
606        return false;
607    }
608
609    // A function that returns a slave of the pawn's controller
610    Controller* Pawn::getSlave(){
611        for (ObjectList<FormationController>::iterator it =
612                ObjectList<FormationController>::begin();
613                it != ObjectList<FormationController>::end(); ++it )
614        {
615            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
616                return it->getController();
617        }
618        return 0;
619    }
620
621
622    void Pawn::setExplosionSound(const std::string &explosionSound)
623    {
624        if(explosionSound_ )
625            explosionSound_->setSource(explosionSound);
626        else
627            assert(0); // This should never happen, because soundpointer is only available on master
628    }
629
630    const std::string& Pawn::getExplosionSound()
631    {
632        if( explosionSound_ )
633            return explosionSound_->getSource();
634        else
635            assert(0);
636        return BLANKSTRING;
637    }
638}
Note: See TracBrowser for help on using the repository browser.