Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/fabienHS15/src/orxonox/worldentities/pawns/Pawn.cc @ 10746

Last change on this file since 10746 was 10746, checked in by fvultier, 8 years ago
  • 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 "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
64        this->health_ = 0;
65        this->maxHealth_ = 0;
66        this->initialHealth_ = 0;
67
68        this->shieldHealth_ = 0;
69        this->initialShieldHealth_ = 0;
70        this->maxShieldHealth_ = 100; //otherwise shield might increase to float_max
71        this->shieldAbsorption_ = 0.5;
72        this->shieldRechargeRate_ = 0;
73        this->shieldRechargeWaitTime_ = 1.0f;
74        this->shieldRechargeWaitCountdown_ = 0;
75
76        this->lastHitOriginator_ = 0;
77
78        // set damage multiplier to default value 1, meaning nominal damage
79        this->damageMultiplier_ = 1;
80
81        this->spawnparticleduration_ = 3.0f;
82
83        this->aimPosition_ = Vector3::ZERO;
84
85        if (GameMode::isMaster())
86        {
87            this->weaponSystem_ = new WeaponSystem(this->getContext());
88            this->weaponSystem_->setPawn(this);
89        }
90        else
91            this->weaponSystem_ = 0;
92
93        this->setRadarObjectColour(ColourValue::Red);
94        this->setRadarObjectShape(RadarViewable::Dot);
95
96        this->registerVariables();
97
98        this->isHumanShip_ = this->hasLocalController();
99
100        this->setSyncMode(ObjectDirection::Bidirectional); // needed to synchronise e.g. aimposition
101
102        if (GameMode::isMaster())
103        {
104            this->explosionSound_ = new WorldSound(this->getContext());
105            this->explosionSound_->setVolume(1.0f);
106        }
107        else
108        {
109            this->explosionSound_ = 0;
110        }
111    }
112
113    Pawn::~Pawn()
114    {
115        if (this->isInitialized())
116        {
117            if (this->weaponSystem_)
118                this->weaponSystem_->destroy();
119        }
120    }
121
122    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
123    {
124        SUPER(Pawn, XMLPort, xmlelement, mode);
125
126        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
127        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
128        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
129
130        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
131        XMLPortParam(Pawn, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0);
132        XMLPortParam(Pawn, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
133        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
134
135        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
136        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
137        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
138
139        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
140        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
141        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
142
143        XMLPortParam(Pawn, "reloadrate", setShieldRechargeRate, getShieldRechargeRate, xmlelement, mode).defaultValues(0);
144        XMLPortParam(Pawn, "reloadwaittime", setShieldRechargeWaitTime, getShieldRechargeWaitTime, xmlelement, mode).defaultValues(1.0f);
145
146        XMLPortParam(Pawn, "explosionSound",  setExplosionSound,  getExplosionSound,  xmlelement, mode);
147
148        XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode );
149    }
150
151    void Pawn::registerVariables()
152    {
153        registerVariable(this->bAlive_,            VariableDirection::ToClient);
154        registerVariable(this->health_,            VariableDirection::ToClient);
155        registerVariable(this->maxHealth_,         VariableDirection::ToClient);
156        registerVariable(this->shieldHealth_,      VariableDirection::ToClient);
157        registerVariable(this->maxShieldHealth_,   VariableDirection::ToClient);
158        registerVariable(this->shieldAbsorption_,  VariableDirection::ToClient);
159        registerVariable(this->aimPosition_,       VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
160    }
161
162    void Pawn::tick(float dt)
163    {
164        SUPER(Pawn, tick, dt);
165
166        // Recharge the shield
167        // TODO: use the existing timer functions instead
168        if(this->shieldRechargeWaitCountdown_ > 0)
169        {
170            this->decreaseShieldRechargeCountdownTime(dt);
171        }
172        else
173        {
174            this->addShieldHealth(this->getShieldRechargeRate() * dt);
175            this->resetShieldRechargeCountdown();
176        }
177
178        if (GameMode::isMaster())
179        {
180            if (this->health_ <= 0 && bAlive_)
181            {
182                this->fireEvent(); // Event to notify anyone who wants to know about the death.
183                this->death();
184            }
185        }
186    }
187
188    void Pawn::preDestroy()
189    {
190        // yay, multiple inheritance!
191        this->ControllableEntity::preDestroy();
192        this->PickupCarrier::preDestroy();
193    }
194
195    void Pawn::setPlayer(PlayerInfo* player)
196    {
197        ControllableEntity::setPlayer(player);
198
199        if (this->getGametype())
200            this->getGametype()->playerStartsControllingPawn(player, this);
201    }
202
203    void Pawn::removePlayer()
204    {
205        if (this->getGametype())
206            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
207
208        ControllableEntity::removePlayer();
209    }
210
211
212    void Pawn::setHealth(float health)
213    {
214        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
215    }
216
217    void Pawn::setShieldHealth(float shieldHealth)
218    {
219        this->shieldHealth_ = std::min(shieldHealth, this->maxShieldHealth_);
220    }
221
222    void Pawn::setMaxShieldHealth(float maxshieldhealth)
223    {
224        this->maxShieldHealth_ = maxshieldhealth;
225    }
226
227    void Pawn::setShieldRechargeRate(float shieldRechargeRate)
228    {
229        this->shieldRechargeRate_ = shieldRechargeRate;
230    }
231
232    void Pawn::setShieldRechargeWaitTime(float shieldRechargeWaitTime)
233    {
234        this->shieldRechargeWaitTime_ = shieldRechargeWaitTime;
235    }
236
237    void Pawn::decreaseShieldRechargeCountdownTime(float dt)
238    {
239        this->shieldRechargeWaitCountdown_ -= dt;
240    }
241
242    void Pawn::damage(float damage, float healthdamage, float shielddamage, Pawn* originator, const btCollisionShape* cs)
243    {
244        // Applies multiplier given by the DamageBoost Pickup.
245        if (originator)
246            damage *= originator->getDamageMultiplier();
247
248        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
249        {
250            // Health-damage cannot be absorbed by shields.
251            // Shield-damage only reduces shield health.
252            // Normal damage can be (partially) absorbed by shields.
253
254            if (shielddamage >= this->getShieldHealth())
255            {
256                this->setShieldHealth(0);
257                this->setHealth(this->health_ - (healthdamage + damage));
258            }
259            else
260            {
261                this->setShieldHealth(this->shieldHealth_ - shielddamage);
262
263                // remove remaining shieldAbsorpton-Part of damage from shield
264                shielddamage = damage * this->shieldAbsorption_;
265                shielddamage = std::min(this->getShieldHealth(),shielddamage);
266                this->setShieldHealth(this->shieldHealth_ - shielddamage);
267
268                // set remaining damage to health
269                this->setHealth(this->health_ - (damage - shielddamage) - healthdamage);
270            }
271
272            this->lastHitOriginator_ = originator;
273        }
274    }
275
276// TODO: Still valid?
277/* HIT-Funktionen
278    Die hit-Funktionen muessen auch in src/orxonox/controllers/Controller.h angepasst werden! (Visuelle Effekte)
279
280*/
281    void Pawn::hit(Pawn* originator, const Vector3& force, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage)
282    {
283        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
284        {
285            this->damage(damage, healthdamage, shielddamage, originator, cs);
286            this->setVelocity(this->getVelocity() + force);
287        }
288    }
289
290    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage)
291    {
292        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
293        {
294            this->damage(damage, healthdamage, shielddamage, originator, cs);
295
296            if ( this->getController() )
297                this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage?
298        }
299    }
300
301    void Pawn::kill()
302    {
303        this->damage(this->health_);
304        this->death();
305    }
306
307    void Pawn::spawneffect()
308    {
309        // play spawn effect
310        if (!this->spawnparticlesource_.empty())
311        {
312            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
313            effect->setPosition(this->getPosition());
314            effect->setOrientation(this->getOrientation());
315            effect->setDestroyAfterLife(true);
316            effect->setSource(this->spawnparticlesource_);
317            effect->setLifetime(this->spawnparticleduration_);
318        }
319    }
320
321    void Pawn::death()
322    {
323        this->setHealth(1);
324        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
325        {
326            // Set bAlive_ to false and wait for destroyLater() to do the destruction
327            this->bAlive_ = false;
328            this->destroyLater();
329
330            this->setDestroyWhenPlayerLeft(false);
331
332            if (this->getGametype())
333                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
334
335            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
336            {
337                // Start to control a new entity if you're the master of a formation
338                if(this->hasSlaves())
339                {
340                    Controller* slave = this->getSlave();
341                    ControllableEntity* entity = slave->getControllableEntity();
342
343
344                    if(!entity->hasHumanController())
345                    {
346                        // delete the AIController // <-- TODO: delete? nothing is deleted here... should we delete the controller?
347                        slave->setControllableEntity(0);
348
349                        // set a new master within the formation
350                        orxonox_cast<FormationController*>(this->getController())->setNewMasterWithinFormation(orxonox_cast<FormationController*>(slave));
351
352                        // start to control a slave
353                        this->getPlayer()->startControl(entity);
354                    }
355                    else
356                    {
357                        this->getPlayer()->stopControl();
358                    }
359
360                }
361                else
362                {
363                    this->getPlayer()->stopControl();
364                }
365            }
366            if (GameMode::isMaster())
367            {
368                this->deatheffect();
369                this->goWithStyle();
370            }
371        }
372    }
373    void Pawn::goWithStyle()
374    {
375        this->bAlive_ = false;
376        this->setDestroyWhenPlayerLeft(false);
377
378        BigExplosion* chunk = new BigExplosion(this->getContext());
379        chunk->setPosition(this->getPosition());
380        chunk->setVelocity(this->getVelocity());
381
382        this->explosionSound_->setPosition(this->getPosition());
383        this->explosionSound_->play();
384    }
385    void Pawn::deatheffect()
386    {
387        // play death effect
388        /*{
389            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
390            effect->setPosition(this->getPosition());
391            effect->setOrientation(this->getOrientation());
392            effect->setDestroyAfterLife(true);
393            effect->setSource("Orxonox/explosion2b");
394            effect->setLifetime(4.0f);
395        }
396        {
397            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
398            effect->setPosition(this->getPosition());
399            effect->setOrientation(this->getOrientation());
400            effect->setDestroyAfterLife(true);
401            effect->setSource("Orxonox/smoke6");
402            effect->setLifetime(4.0f);
403        }
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/sparks");
410            effect->setLifetime(4.0f);
411        }*/
412       
413       
414        {
415            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
416            effect->setPosition(this->getPosition());
417            effect->setOrientation(this->getOrientation());
418            effect->setDestroyAfterLife(true);
419            effect->setSource("orxonox/explosion_flash2");
420            effect->setLifetime(5.0f);
421        }
422        {
423            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
424            effect->setPosition(this->getPosition());
425            effect->setOrientation(this->getOrientation());
426            effect->setDestroyAfterLife(true);
427            effect->setSource("orxonox/explosion_flame2");
428            effect->setLifetime(5.0f);
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_shockwave2");
436            effect->scale(20);
437            effect->setLifetime(5.0f);
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_sparks2");
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_streak2");
452            effect->setLifetime(5.0f);
453        }
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_afterglow");
460            effect->scale(20);
461            effect->setLifetime(5.0f);
462        }
463       
464       
465        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
466        {
467            ExplosionChunk* chunk = new ExplosionChunk(this->getContext());
468            chunk->setPosition(this->getPosition());
469        }
470    }
471
472    /**
473    @brief
474        Check whether the Pawn has a @ref Orxonox::WeaponSystem and fire it with the specified firemode if it has one.
475    */
476    void Pawn::fired(unsigned int firemode)
477    {
478        if (this->weaponSystem_)
479            this->weaponSystem_->fire(firemode);
480    }
481
482    void Pawn::postSpawn()
483    {
484        this->setHealth(this->initialHealth_);
485        if (GameMode::isMaster())
486            this->spawneffect();
487    }
488
489    /* WeaponSystem:
490    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
491    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
492    *       --> e.g. Pickup-Items
493    */
494    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
495    {
496        this->attach(wSlot);
497        if (this->weaponSystem_)
498            this->weaponSystem_->addWeaponSlot(wSlot);
499    }
500
501    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
502    {
503        if (this->weaponSystem_)
504            return this->weaponSystem_->getWeaponSlot(index);
505        else
506            return 0;
507    }
508
509    void Pawn::addWeaponSet(WeaponSet * wSet)
510    {
511        if (this->weaponSystem_)
512            this->weaponSystem_->addWeaponSet(wSet);
513    }
514
515    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
516    {
517        if (this->weaponSystem_)
518            return this->weaponSystem_->getWeaponSet(index);
519        else
520            return 0;
521    }
522
523    void Pawn::addWeaponPack(WeaponPack * wPack)
524    {
525        if (this->weaponSystem_)
526        {
527            this->weaponSystem_->addWeaponPack(wPack);
528            this->addedWeaponPack(wPack);
529        }
530    }
531
532    void Pawn::addWeaponPackXML(WeaponPack * wPack)
533    {
534        if (this->weaponSystem_)
535        {
536            if (!this->weaponSystem_->addWeaponPack(wPack))
537                wPack->destroy();
538            else
539                this->addedWeaponPack(wPack);
540        }
541    }
542
543    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
544    {
545        if (this->weaponSystem_)
546            return this->weaponSystem_->getWeaponPack(index);
547        else
548            return 0;
549    }
550
551    std::vector<WeaponPack *> * Pawn::getAllWeaponPacks()
552    {
553        if (this->weaponSystem_)
554            return this->weaponSystem_->getAllWeaponPacks();
555        else
556            return 0;       
557    }
558
559    Munition* Pawn::getMunition(SubclassIdentifier<Munition> * identifier)
560    {
561        if (weaponSystem_)
562        {
563            return weaponSystem_->getMunition(identifier);
564        }
565
566        return NULL;
567    }
568
569    //Tell the Map (RadarViewable), if this is a playership
570    void Pawn::startLocalHumanControl()
571    {
572//        SUPER(ControllableEntity, changedPlayer());
573        ControllableEntity::startLocalHumanControl();
574        this->isHumanShip_ = true;
575    }
576
577    void Pawn::changedVisibility(void)
578    {
579        SUPER(Pawn, changedVisibility);
580
581        // enable proper radarviewability when the visibility is changed
582        this->RadarViewable::settingsChanged();
583    }
584
585
586    // A function to check if this pawn's controller is the master of any formationcontroller
587    bool Pawn::hasSlaves()
588    {
589        for (ObjectList<FormationController>::iterator it =
590             ObjectList<FormationController>::begin();
591             it != ObjectList<FormationController>::end(); ++it )
592        {
593            // checks if the pawn's controller has a slave
594            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
595                return true;
596        }
597        return false;
598    }
599
600    // A function that returns a slave of the pawn's controller
601    Controller* Pawn::getSlave(){
602        for (ObjectList<FormationController>::iterator it =
603                ObjectList<FormationController>::begin();
604                it != ObjectList<FormationController>::end(); ++it )
605        {
606            if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
607                return it->getController();
608        }
609        return 0;
610    }
611
612
613    void Pawn::setExplosionSound(const std::string &explosionSound)
614    {
615        if(explosionSound_ )
616            explosionSound_->setSource(explosionSound);
617        else
618            assert(0); // This should never happen, because soundpointer is only available on master
619    }
620
621    const std::string& Pawn::getExplosionSound()
622    {
623        if( explosionSound_ )
624            return explosionSound_->getSource();
625        else
626            assert(0);
627        return BLANKSTRING;
628    }
629}
Note: See TracBrowser for help on using the repository browser.