Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added explosioneffects to the xml files

  • Property svn:eol-style set to native
File size: 18.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/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->goWithStyle();
377            }
378        }
379    }
380    void Pawn::goWithStyle()
381    {
382        orxout() << "gowithstyle" << endl;
383
384
385        this->bAlive_ = false;
386        this->setDestroyWhenPlayerLeft(false);
387
388        while(!explosionPartList_.empty())
389        {
390            explosionPartList_.back()->setPosition(this->getPosition());
391            explosionPartList_.back()->setVelocity(this->getVelocity());
392            explosionPartList_.back()->Explode();
393            explosionPartList_.pop_back();
394        }
395
396        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
397        {
398            ExplosionChunk* chunk = new ExplosionChunk(this->getContext());
399            chunk->setPosition(this->getPosition());
400        }
401
402        this->explosionSound_->setPosition(this->getPosition());
403        this->explosionSound_->play();
404    }
405   
406    void Pawn::fired(unsigned int firemode)
407    {
408        if (this->weaponSystem_)
409            this->weaponSystem_->fire(firemode);
410    }
411
412    void Pawn::reload()
413    {
414        this->bReload_ = true;
415    }
416
417    void Pawn::postSpawn()
418    {
419        this->setHealth(this->initialHealth_);
420        if (GameMode::isMaster())
421            this->spawneffect();
422    }
423
424
425    void Pawn::addExplosionPart(ExplosionPart* ePart)
426    {this->explosionPartList_.push_back(ePart);}
427
428
429    ExplosionPart * Pawn::getExplosionPart()
430    {return this->explosionPartList_.back();}
431
432
433
434    /* WeaponSystem:
435    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
436    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
437    *       --> e.g. Pickup-Items
438    */
439    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
440    {
441        this->attach(wSlot);
442        if (this->weaponSystem_)
443            this->weaponSystem_->addWeaponSlot(wSlot);
444    }
445
446    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
447    {
448        if (this->weaponSystem_)
449            return this->weaponSystem_->getWeaponSlot(index);
450        else
451            return 0;
452    }
453
454    void Pawn::addWeaponSet(WeaponSet * wSet)
455    {
456        if (this->weaponSystem_)
457            this->weaponSystem_->addWeaponSet(wSet);
458    }
459
460    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
461    {
462        if (this->weaponSystem_)
463            return this->weaponSystem_->getWeaponSet(index);
464        else
465            return 0;
466    }
467
468    void Pawn::addWeaponPack(WeaponPack * wPack)
469    {
470        if (this->weaponSystem_)
471        {
472            this->weaponSystem_->addWeaponPack(wPack);
473            this->addedWeaponPack(wPack);
474        }
475    }
476
477    void Pawn::addWeaponPackXML(WeaponPack * wPack)
478    {
479        if (this->weaponSystem_)
480        {
481            if (!this->weaponSystem_->addWeaponPack(wPack))
482                wPack->destroy();
483            else
484                this->addedWeaponPack(wPack);
485        }
486    }
487
488    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
489    {
490        if (this->weaponSystem_)
491            return this->weaponSystem_->getWeaponPack(index);
492        else
493            return 0;
494    }
495
496    //Tell the Map (RadarViewable), if this is a playership
497    void Pawn::startLocalHumanControl()
498    {
499//        SUPER(ControllableEntity, changedPlayer());
500        ControllableEntity::startLocalHumanControl();
501        this->isHumanShip_ = true;
502    }
503
504    void Pawn::changedVisibility(void)
505    {
506        SUPER(Pawn, changedVisibility);
507
508        // enable proper radarviewability when the visibility is changed
509        this->RadarViewable::settingsChanged();
510    }
511
512
513    // A function to check if this pawn's controller is the master of any formationcontroller
514    bool Pawn::hasSlaves()
515    {
516        for (ObjectList<FormationController>::iterator it =
517             ObjectList<FormationController>::begin();
518             it != ObjectList<FormationController>::end(); ++it )
519        {
520            // checks if the pawn's controller has a slave
521            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
522                return true;
523        }
524        return false;
525    }
526
527    // A function that returns a slave of the pawn's controller
528    Controller* Pawn::getSlave(){
529        for (ObjectList<FormationController>::iterator it =
530                ObjectList<FormationController>::begin();
531                it != ObjectList<FormationController>::end(); ++it )
532        {
533            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
534                return it->getController();
535        }
536        return 0;
537    }
538
539
540    void Pawn::setExplosionSound(const std::string &explosionSound)
541    {
542        if(explosionSound_ )
543            explosionSound_->setSource(explosionSound);
544        else
545            assert(0); // This should never happen, because soundpointer is only available on master
546    }
547
548    const std::string& Pawn::getExplosionSound()
549    {
550        if( explosionSound_ )
551            return explosionSound_->getSource();
552        else
553            assert(0);
554        return BLANKSTRING;
555    }
556}
Note: See TracBrowser for help on using the repository browser.