Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationHS15/src/orxonox/worldentities/pawns/Pawn.cc @ 10962

Last change on this file since 10962 was 10962, checked in by maxima, 8 years ago

Merged presentation and exlposionChunks branches. Works fine. Added explosion parts to hovership.

  • Property svn:eol-style set to native
File size: 19.3 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 "weaponsystem/Munition.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
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        this->shieldRechargeRate_ = 0;
74        this->shieldRechargeWaitTime_ = 1.0f;
75        this->shieldRechargeWaitCountdown_ = 0;
76
77        this->lastHitOriginator_ = 0;
78
79        // set damage multiplier to default value 1, meaning nominal damage
80        this->damageMultiplier_ = 1;
81
82        this->spawnparticleduration_ = 3.0f;
83
84        this->aimPosition_ = Vector3::ZERO;
85
86        //this->explosionPartList_ = NULL;
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(0);
141
142        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
143        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
144        XMLPortObject(Pawn, WeaponPack, "weaponpacks", addWeaponPackXML, getWeaponPack, xmlelement, mode);
145        XMLPortObject(Pawn, Munition, "munition", addMunitionXML, getMunitionXML, xmlelement, mode);
146
147        XMLPortParam(Pawn, "shieldrechargerate", setShieldRechargeRate, getShieldRechargeRate, xmlelement, mode).defaultValues(0);
148        XMLPortParam(Pawn, "shieldrechargewaittime", setShieldRechargeWaitTime, getShieldRechargeWaitTime, xmlelement, mode).defaultValues(1.0f);
149        XMLPortObject(Pawn, ExplosionPart, "explosion", addExplosionPart, getExplosionPart, xmlelement, mode);
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->aimPosition_,       VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
165    }
166
167    void Pawn::tick(float dt)
168    {
169        //BigExplosion* chunk = new BigExplosion(this->getContext());
170        SUPER(Pawn, tick, dt);
171
172        // Recharge the shield
173        // TODO: use the existing timer functions instead
174        if(this->shieldRechargeWaitCountdown_ > 0)
175        {
176            this->decreaseShieldRechargeCountdownTime(dt);
177        }
178        else
179        {
180            this->addShieldHealth(this->getShieldRechargeRate() * dt);
181            this->resetShieldRechargeCountdown();
182        }
183
184        if (GameMode::isMaster())
185        {
186            if (this->health_ <= 0 && bAlive_)
187            {
188                this->fireEvent(); // Event to notify anyone who wants to know about the death.
189                this->death();
190            }
191        }
192    }
193
194    void Pawn::preDestroy()
195    {
196        // yay, multiple inheritance!
197        this->ControllableEntity::preDestroy();
198        this->PickupCarrier::preDestroy();
199    }
200
201    void Pawn::setPlayer(PlayerInfo* player)
202    {
203        ControllableEntity::setPlayer(player);
204
205        if (this->getGametype())
206            this->getGametype()->playerStartsControllingPawn(player, this);
207    }
208
209    void Pawn::removePlayer()
210    {
211        if (this->getGametype())
212            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
213
214        ControllableEntity::removePlayer();
215    }
216
217
218    void Pawn::setHealth(float health)
219    {
220        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
221    }
222
223    void Pawn::setShieldHealth(float shieldHealth)
224    {
225        this->shieldHealth_ = std::min(shieldHealth, this->maxShieldHealth_);
226    }
227
228    void Pawn::setMaxShieldHealth(float maxshieldhealth)
229    {
230        this->maxShieldHealth_ = maxshieldhealth;
231    }
232
233    void Pawn::setShieldRechargeRate(float shieldRechargeRate)
234    {
235        this->shieldRechargeRate_ = shieldRechargeRate;
236    }
237
238    void Pawn::setShieldRechargeWaitTime(float shieldRechargeWaitTime)
239    {
240        this->shieldRechargeWaitTime_ = shieldRechargeWaitTime;
241    }
242
243    void Pawn::decreaseShieldRechargeCountdownTime(float dt)
244    {
245        this->shieldRechargeWaitCountdown_ -= dt;
246    }
247
248    void Pawn::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs)
249    {
250        // Applies multiplier given by the DamageBoost Pickup.
251        if (originator)
252            damage *= originator->getDamageMultiplier();
253
254        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
255        {
256            // Health-damage cannot be absorbed by shields.
257            // Shield-damage only reduces shield health.
258            // Normal damage can be (partially) absorbed by shields.
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    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    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->goWithStyle();
375            }
376        }
377    }
378    void Pawn::goWithStyle()
379    {
380
381        this->bAlive_ = false;
382        this->setDestroyWhenPlayerLeft(false);
383
384        while(!explosionPartList_.empty())
385        {
386            explosionPartList_.back()->setPosition(this->getPosition());
387            explosionPartList_.back()->setVelocity(this->getVelocity());
388            explosionPartList_.back()->setOrientation(this->getOrientation());
389            explosionPartList_.back()->Explode();
390            explosionPartList_.pop_back();
391        }
392
393        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
394        {
395            ExplosionChunk* chunk = new ExplosionChunk(this->getContext());
396            chunk->setPosition(this->getPosition());
397        }
398
399        this->explosionSound_->setPosition(this->getPosition());
400        this->explosionSound_->play();
401    }
402
403    /**
404    @brief
405        Check whether the Pawn has a @ref Orxonox::WeaponSystem and fire it with the specified firemode if it has one.
406    */
407
408    void Pawn::fired(unsigned int firemode)
409    {
410        if (this->weaponSystem_)
411            this->weaponSystem_->fire(firemode);
412    }
413
414    void Pawn::postSpawn()
415    {
416        this->setHealth(this->initialHealth_);
417        if (GameMode::isMaster())
418            this->spawneffect();
419    }
420
421
422    void Pawn::addExplosionPart(ExplosionPart* ePart)
423    {this->explosionPartList_.push_back(ePart);}
424
425
426    ExplosionPart * Pawn::getExplosionPart()
427    {return this->explosionPartList_.back();}
428
429
430
431    /* WeaponSystem:
432    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
433    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
434    *       --> e.g. Pickup-Items
435    */
436    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
437    {
438        this->attach(wSlot);
439        if (this->weaponSystem_)
440            this->weaponSystem_->addWeaponSlot(wSlot);
441    }
442
443    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
444    {
445        if (this->weaponSystem_)
446            return this->weaponSystem_->getWeaponSlot(index);
447        else
448            return 0;
449    }
450
451    void Pawn::addWeaponSet(WeaponSet * wSet)
452    {
453        if (this->weaponSystem_)
454            this->weaponSystem_->addWeaponSet(wSet);
455    }
456
457    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
458    {
459        if (this->weaponSystem_)
460            return this->weaponSystem_->getWeaponSet(index);
461        else
462            return 0;
463    }
464
465    void Pawn::addWeaponPack(WeaponPack * wPack)
466    {
467        if (this->weaponSystem_)
468        {
469            this->weaponSystem_->addWeaponPack(wPack);
470            this->addedWeaponPack(wPack);
471        }
472    }
473
474    void Pawn::addWeaponPackXML(WeaponPack * wPack)
475    {
476        if (this->weaponSystem_)
477        {
478            if (!this->weaponSystem_->addWeaponPack(wPack))
479                wPack->destroy();
480            else
481                this->addedWeaponPack(wPack);
482        }
483    }
484
485    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
486    {
487        if (this->weaponSystem_)
488            return this->weaponSystem_->getWeaponPack(index);
489        else
490            return 0;
491    }
492
493    std::vector<WeaponPack *> * Pawn::getAllWeaponPacks()
494    {
495        if (this->weaponSystem_)
496            return this->weaponSystem_->getAllWeaponPacks();
497        else
498            return 0;       
499    }
500
501    void Pawn::addMunitionXML(Munition* munition)
502    {
503        if (this->weaponSystem_ && munition)
504        {
505            this->weaponSystem_->addMunition(munition);
506        }
507    }
508
509    Munition* Pawn::getMunitionXML() const
510    {
511        return NULL;
512    }
513
514    Munition* Pawn::getMunition(SubclassIdentifier<Munition> * identifier)
515    {
516        if (weaponSystem_)
517        {
518            return weaponSystem_->getMunition(identifier);
519        }
520
521        return NULL;
522    }
523
524    //Tell the Map (RadarViewable), if this is a playership
525    void Pawn::startLocalHumanControl()
526    {
527//        SUPER(ControllableEntity, startLocalHumanControl());
528        ControllableEntity::startLocalHumanControl();
529        this->isHumanShip_ = true;
530    }
531
532    void Pawn::changedVisibility(void)
533    {
534        SUPER(Pawn, changedVisibility);
535
536        // enable proper radarviewability when the visibility is changed
537        this->RadarViewable::settingsChanged();
538    }
539
540
541    // A function to check if this pawn's controller is the master of any formationcontroller
542    bool Pawn::hasSlaves()
543    {
544        for (ObjectList<FormationController>::iterator it =
545             ObjectList<FormationController>::begin();
546             it != ObjectList<FormationController>::end(); ++it )
547        {
548            // checks if the pawn's controller has a slave
549            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
550                return true;
551        }
552        return false;
553    }
554
555    // A function that returns a slave of the pawn's controller
556    Controller* Pawn::getSlave(){
557        for (ObjectList<FormationController>::iterator it =
558                ObjectList<FormationController>::begin();
559                it != ObjectList<FormationController>::end(); ++it )
560        {
561            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
562                return it->getController();
563        }
564        return 0;
565    }
566
567
568    void Pawn::setExplosionSound(const std::string &explosionSound)
569    {
570        if(explosionSound_ )
571            explosionSound_->setSource(explosionSound);
572        else
573            assert(0); // This should never happen, because soundpointer is only available on master
574    }
575
576    const std::string& Pawn::getExplosionSound()
577    {
578        if( explosionSound_ )
579            return explosionSound_->getSource();
580        else
581            assert(0);
582        return BLANKSTRING;
583    }
584}
Note: See TracBrowser for help on using the repository browser.