Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Finalized everything deleted now unnecessary BigExplosion

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