Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/worldentities/pawns/Pawn.cc @ 11099

Last change on this file since 11099 was 11099, checked in by muemart, 8 years ago

Fix loads of doxygen warnings and other documentation issues

  • Property svn:eol-style set to native
File size: 19.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 "core/EventIncludes.h"
37#include "network/NetworkFunction.h"
38
39#include "infos/PlayerInfo.h"
40#include "controllers/Controller.h"
41#include "gametypes/Gametype.h"
42#include "graphics/ParticleSpawner.h"
43#include "worldentities/ExplosionChunk.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 "weaponsystem/Munition.h"
50#include "sound/WorldSound.h"
51
52#include "controllers/FormationController.h"
53
54namespace orxonox
55{
56    RegisterClass(Pawn);
57
58    Pawn::Pawn(Context* context)
59        : ControllableEntity(context)
60        , RadarViewable(this, static_cast<WorldEntity*>(this))
61    {
62        RegisterObject(Pawn);
63
64        this->bAlive_ = true;
65        this->bVulnerable_ = true;
66
67        this->health_ = 0;
68        this->maxHealth_ = 0;
69        this->initialHealth_ = 0;
70
71        this->shieldHealth_ = 0;
72        this->initialShieldHealth_ = 0;
73        this->maxShieldHealth_ = 100; //otherwise shield might increase to float_max
74        this->shieldAbsorption_ = 0.5;
75        this->shieldRechargeRate_ = 0;
76        this->shieldRechargeWaitTime_ = 1.0f;
77        this->shieldRechargeWaitCountdown_ = 0;
78
79        this->lastHitOriginator_ = nullptr;
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_ = nullptr;
89
90        if (GameMode::isMaster())
91        {
92            this->weaponSystem_ = new WeaponSystem(this->getContext());
93            this->weaponSystem_->setPawn(this);
94        }
95        else
96            this->weaponSystem_ = nullptr;
97
98        this->setRadarObjectColour(ColourValue::Red);
99        this->setRadarObjectShape(RadarViewable::Shape::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_ = nullptr;
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, "vulnerable", setVulnerable, isVulnerable, xmlelement, mode).defaultValues(true);
141
142        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
143        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
144        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(0);
145
146        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
147        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
148        XMLPortObject(Pawn, WeaponPack, "weaponpacks", addWeaponPackXML, getWeaponPack, xmlelement, mode);
149        XMLPortObject(Pawn, Munition, "munition", addMunitionXML, getMunitionXML, xmlelement, mode);
150
151        XMLPortObject(Pawn, ExplosionPart, "explosion", addExplosionPart, getExplosionPart, xmlelement, mode);
152        XMLPortParam(Pawn, "shieldrechargerate", setShieldRechargeRate, getShieldRechargeRate, xmlelement, mode).defaultValues(0);
153        XMLPortParam(Pawn, "shieldrechargewaittime", setShieldRechargeWaitTime, getShieldRechargeWaitTime, xmlelement, mode).defaultValues(1.0f);
154
155        XMLPortParam(Pawn, "explosionSound",  setExplosionSound,  getExplosionSound,  xmlelement, mode);
156
157        XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode );
158    }
159
160    void Pawn::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
161    {
162        SUPER(Pawn, XMLEventPort, xmlelement, mode);
163
164        XMLPortEventState(Pawn, BaseObject, "vulnerability", setVulnerable, xmlelement, mode);
165    }
166
167    void Pawn::registerVariables()
168    {
169        registerVariable(this->bAlive_,            VariableDirection::ToClient);
170        registerVariable(this->health_,            VariableDirection::ToClient);
171        registerVariable(this->maxHealth_,         VariableDirection::ToClient);
172        registerVariable(this->shieldHealth_,      VariableDirection::ToClient);
173        registerVariable(this->maxShieldHealth_,   VariableDirection::ToClient);
174        registerVariable(this->shieldAbsorption_,  VariableDirection::ToClient);
175        registerVariable(this->aimPosition_,       VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
176    }
177
178    void Pawn::tick(float dt)
179    {
180        //BigExplosion* chunk = new BigExplosion(this->getContext());
181        SUPER(Pawn, tick, dt);
182
183        // Recharge the shield
184        // TODO: use the existing timer functions instead
185        if(this->shieldRechargeWaitCountdown_ > 0)
186        {
187            this->decreaseShieldRechargeCountdownTime(dt);
188        }
189        else
190        {
191            this->addShieldHealth(this->getShieldRechargeRate() * dt);
192            this->resetShieldRechargeCountdown();
193        }
194
195        if (GameMode::isMaster())
196        {
197            if (this->health_ <= 0 && bAlive_)
198            {
199                this->fireEvent(); // Event to notify anyone who wants to know about the death.
200                this->death();
201            }
202        }
203    }
204
205    void Pawn::preDestroy()
206    {
207        // yay, multiple inheritance!
208        this->ControllableEntity::preDestroy();
209        this->PickupCarrier::preDestroy();
210    }
211
212    void Pawn::setPlayer(PlayerInfo* player)
213    {
214        ControllableEntity::setPlayer(player);
215
216        if (this->getGametype())
217            this->getGametype()->playerStartsControllingPawn(player, this);
218    }
219
220    void Pawn::removePlayer()
221    {
222        if (this->getGametype())
223            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
224
225        ControllableEntity::removePlayer();
226    }
227
228
229    void Pawn::setHealth(float health)
230    {
231        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
232    }
233
234    void Pawn::setShieldHealth(float shieldHealth)
235    {
236        this->shieldHealth_ = std::min(shieldHealth, this->maxShieldHealth_);
237    }
238
239    void Pawn::setMaxShieldHealth(float maxshieldhealth)
240    {
241        this->maxShieldHealth_ = maxshieldhealth;
242    }
243
244    void Pawn::setShieldRechargeRate(float shieldRechargeRate)
245    {
246        this->shieldRechargeRate_ = shieldRechargeRate;
247    }
248
249    void Pawn::setShieldRechargeWaitTime(float shieldRechargeWaitTime)
250    {
251        this->shieldRechargeWaitTime_ = shieldRechargeWaitTime;
252    }
253
254    void Pawn::decreaseShieldRechargeCountdownTime(float dt)
255    {
256        this->shieldRechargeWaitCountdown_ -= dt;
257    }
258
259    void Pawn::changedVulnerability()
260    {
261
262    }
263
264    void Pawn::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs)
265    {
266        // A pawn can only get damaged if it is vulnerable
267        if (!isVulnerable())
268        {
269            return;
270        }
271
272        // Applies multiplier given by the DamageBoost Pickup.
273        if (originator)
274            damage *= originator->getDamageMultiplier();
275
276        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
277        {
278            // Health-damage cannot be absorbed by shields.
279            // Shield-damage only reduces shield health.
280            // Normal damage can be (partially) absorbed by shields.
281
282            if (shielddamage >= this->getShieldHealth())
283            {
284                this->setShieldHealth(0);
285                this->setHealth(this->health_ - (healthdamage + damage));
286            }
287            else
288            {
289                this->setShieldHealth(this->shieldHealth_ - shielddamage);
290
291                // remove remaining shieldAbsorpton-Part of damage from shield
292                shielddamage = damage * this->shieldAbsorption_;
293                shielddamage = std::min(this->getShieldHealth(),shielddamage);
294                this->setShieldHealth(this->shieldHealth_ - shielddamage);
295
296                // set remaining damage to health
297                this->setHealth(this->health_ - (damage - shielddamage) - healthdamage);
298            }
299
300            this->lastHitOriginator_ = originator;
301        }
302    }
303
304// TODO: Still valid?
305/* HIT-Funktionen
306    Die hit-Funktionen muessen auch in src/orxonox/controllers/Controller.h angepasst werden! (Visuelle Effekte)
307
308*/
309    void Pawn::hit(Pawn* originator, const Vector3& force, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage)
310    {
311        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
312        {
313            this->damage(damage, healthdamage, shielddamage, originator, cs);
314            this->setVelocity(this->getVelocity() + force);
315        }
316    }
317
318    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage)
319    {
320        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
321        {
322            this->damage(damage, healthdamage, shielddamage, originator, cs);
323
324            if ( this->getController() )
325                this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage?
326        }
327    }
328
329    void Pawn::kill()
330    {
331        this->damage(this->health_);
332        this->death();
333    }
334
335    void Pawn::spawneffect()
336    {
337        // play spawn effect
338        if (!this->spawnparticlesource_.empty())
339        {
340            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
341            effect->setPosition(this->getPosition());
342            effect->setOrientation(this->getOrientation());
343            effect->setDestroyAfterLife(true);
344            effect->setSource(this->spawnparticlesource_);
345            effect->setLifetime(this->spawnparticleduration_);
346        }
347    }
348
349    void Pawn::death()
350    {
351        this->setHealth(1);
352        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
353        {
354            // Set bAlive_ to false and wait for destroyLater() to do the destruction
355            this->bAlive_ = false;
356            this->destroyLater();
357
358            this->setDestroyWhenPlayerLeft(false);
359
360            if (this->getGametype())
361                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
362
363            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
364            {
365                // Start to control a new entity if you're the master of a formation
366                if(this->hasSlaves())
367                {
368                    Controller* slave = this->getSlave();
369                    ControllableEntity* entity = slave->getControllableEntity();
370
371
372                    if(!entity->hasHumanController())
373                    {
374                        // delete the AIController // <-- TODO: delete? nothing is deleted here... should we delete the controller?
375                        slave->setControllableEntity(nullptr);
376
377                        // set a new master within the formation
378                        orxonox_cast<FormationController*>(this->getController())->setNewMasterWithinFormation(orxonox_cast<FormationController*>(slave));
379
380                        // start to control a slave
381                        this->getPlayer()->startControl(entity);
382                    }
383                    else
384                    {
385                        this->getPlayer()->stopControl();
386                    }
387
388                }
389                else
390                {
391                    this->getPlayer()->stopControl();
392                }
393            }
394            if (GameMode::isMaster())
395            {
396                this->goWithStyle();
397            }
398        }
399    }
400    void Pawn::goWithStyle()
401    {
402
403        this->bAlive_ = false;
404        this->setDestroyWhenPlayerLeft(false);
405
406        while(!explosionPartList_.empty())
407        {
408            explosionPartList_.back()->setPosition(this->getPosition());
409            explosionPartList_.back()->setVelocity(this->getVelocity());
410            explosionPartList_.back()->setOrientation(this->getOrientation());
411            explosionPartList_.back()->Explode();
412            explosionPartList_.pop_back();
413        }
414
415        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
416        {
417            ExplosionChunk* chunk = new ExplosionChunk(this->getContext());
418            chunk->setPosition(this->getPosition());
419        }
420
421        this->explosionSound_->setPosition(this->getPosition());
422        this->explosionSound_->play();
423    }
424
425    /**
426    @brief
427        Check whether the Pawn has a @ref orxonox::WeaponSystem and fire it with the specified firemode if it has one.
428    */
429
430    void Pawn::fired(unsigned int firemode)
431    {
432        if (this->weaponSystem_)
433            this->weaponSystem_->fire(firemode);
434    }
435
436    void Pawn::postSpawn()
437    {
438        this->setHealth(this->initialHealth_);
439        if (GameMode::isMaster())
440            this->spawneffect();
441    }
442
443
444    void Pawn::addExplosionPart(ExplosionPart* ePart)
445    {this->explosionPartList_.push_back(ePart);}
446
447
448    ExplosionPart * Pawn::getExplosionPart()
449    {return this->explosionPartList_.back();}
450
451
452
453    /* WeaponSystem:
454    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
455    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
456    *       --> e.g. Pickup-Items
457    */
458    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
459    {
460        this->attach(wSlot);
461        if (this->weaponSystem_)
462            this->weaponSystem_->addWeaponSlot(wSlot);
463    }
464
465    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
466    {
467        if (this->weaponSystem_)
468            return this->weaponSystem_->getWeaponSlot(index);
469        else
470            return nullptr;
471    }
472
473    void Pawn::addWeaponSet(WeaponSet * wSet)
474    {
475        if (this->weaponSystem_)
476            this->weaponSystem_->addWeaponSet(wSet);
477    }
478
479    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
480    {
481        if (this->weaponSystem_)
482            return this->weaponSystem_->getWeaponSet(index);
483        else
484            return nullptr;
485    }
486
487    void Pawn::addWeaponPack(WeaponPack * wPack)
488    {
489        if (this->weaponSystem_)
490        {
491            this->weaponSystem_->addWeaponPack(wPack);
492            this->addedWeaponPack(wPack);
493        }
494    }
495
496    void Pawn::addWeaponPackXML(WeaponPack * wPack)
497    {
498        if (this->weaponSystem_)
499        {
500            if (!this->weaponSystem_->addWeaponPack(wPack))
501                wPack->destroy();
502            else
503                this->addedWeaponPack(wPack);
504        }
505    }
506
507    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
508    {
509        if (this->weaponSystem_)
510            return this->weaponSystem_->getWeaponPack(index);
511        else
512            return nullptr;
513    }
514
515    void Pawn::addMunitionXML(Munition* munition)
516    {
517        if (this->weaponSystem_ && munition)
518        {
519            this->weaponSystem_->addMunition(munition);
520        }
521    }
522
523    Munition* Pawn::getMunitionXML() const
524    {
525        return nullptr;
526    }
527
528    Munition* Pawn::getMunition(SubclassIdentifier<Munition> * identifier)
529    {
530        if (weaponSystem_)
531        {
532            return weaponSystem_->getMunition(identifier);
533        }
534
535        return nullptr;
536    }
537
538    //Tell the Map (RadarViewable), if this is a playership
539    void Pawn::startLocalHumanControl()
540    {
541//        SUPER(ControllableEntity, startLocalHumanControl());
542        ControllableEntity::startLocalHumanControl();
543        this->isHumanShip_ = true;
544    }
545
546    void Pawn::changedVisibility(void)
547    {
548        SUPER(Pawn, changedVisibility);
549
550        // enable proper radarviewability when the visibility is changed
551        this->RadarViewable::settingsChanged();
552    }
553
554
555    // A function to check if this pawn's controller is the master of any formationcontroller
556    bool Pawn::hasSlaves()
557    {
558        for (FormationController* controller : ObjectList<FormationController>())
559        {
560            // checks if the pawn's controller has a slave
561            if (this->hasHumanController() && controller->getMaster() == this->getPlayer()->getController())
562                return true;
563        }
564        return false;
565    }
566
567    // A function that returns a slave of the pawn's controller
568    Controller* Pawn::getSlave(){
569        for (FormationController* controller : ObjectList<FormationController>())
570        {
571            if (this->hasHumanController() && controller->getMaster() == this->getPlayer()->getController())
572                return controller->getController();
573        }
574        return nullptr;
575    }
576
577
578    void Pawn::setExplosionSound(const std::string &explosionSound)
579    {
580        if(explosionSound_ )
581            explosionSound_->setSource(explosionSound);
582        else
583            assert(0); // This should never happen, because soundpointer is only available on master
584    }
585
586    const std::string& Pawn::getExplosionSound()
587    {
588        if( explosionSound_ )
589            return explosionSound_->getSource();
590        else
591            assert(0);
592        return BLANKSTRING;
593    }
594}
Note: See TracBrowser for help on using the repository browser.