Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sagerjFS16/src/orxonox/worldentities/pawns/Pawn.cc @ 11593

Last change on this file since 11593 was 11174, checked in by sagerj, 9 years ago

everything beneath weaponmode changed to push/release - wm modified only need to implement timer/ticker

  • Property svn:eol-style set to native
File size: 19.7 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::pushed(unsigned int firemode)
431    {
432        if (this->weaponSystem_)
433            this->weaponSystem_->push(firemode);
434    }
435
436    void Pawn::released(unsigned int firemode)
437    {
438        if (this->weaponSystem_)
439            this->weaponSystem_->release(firemode);
440    }
441
442    void Pawn::postSpawn()
443    {
444        this->setHealth(this->initialHealth_);
445        if (GameMode::isMaster())
446            this->spawneffect();
447    }
448
449
450    void Pawn::addExplosionPart(ExplosionPart* ePart)
451    {this->explosionPartList_.push_back(ePart);}
452
453
454    ExplosionPart * Pawn::getExplosionPart()
455    {return this->explosionPartList_.back();}
456
457
458
459    /* WeaponSystem:
460    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
461    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
462    *       --> e.g. Pickup-Items
463    */
464    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
465    {
466        this->attach(wSlot);
467        if (this->weaponSystem_)
468            this->weaponSystem_->addWeaponSlot(wSlot);
469    }
470
471    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
472    {
473        if (this->weaponSystem_)
474            return this->weaponSystem_->getWeaponSlot(index);
475        else
476            return nullptr;
477    }
478
479    void Pawn::addWeaponSet(WeaponSet * wSet)
480    {
481        if (this->weaponSystem_)
482            this->weaponSystem_->addWeaponSet(wSet);
483    }
484
485    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
486    {
487        if (this->weaponSystem_)
488            return this->weaponSystem_->getWeaponSet(index);
489        else
490            return nullptr;
491    }
492
493    void Pawn::addWeaponPack(WeaponPack * wPack)
494    {
495        if (this->weaponSystem_)
496        {
497            this->weaponSystem_->addWeaponPack(wPack);
498            this->addedWeaponPack(wPack);
499        }
500    }
501
502    void Pawn::addWeaponPackXML(WeaponPack * wPack)
503    {
504        if (this->weaponSystem_)
505        {
506            if (!this->weaponSystem_->addWeaponPack(wPack))
507                wPack->destroy();
508            else
509                this->addedWeaponPack(wPack);
510        }
511    }
512
513    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
514    {
515        if (this->weaponSystem_)
516            return this->weaponSystem_->getWeaponPack(index);
517        else
518            return nullptr;
519    }
520
521    void Pawn::addMunitionXML(Munition* munition)
522    {
523        if (this->weaponSystem_ && munition)
524        {
525            this->weaponSystem_->addMunition(munition);
526        }
527    }
528
529    Munition* Pawn::getMunitionXML() const
530    {
531        return nullptr;
532    }
533
534    Munition* Pawn::getMunition(SubclassIdentifier<Munition> * identifier)
535    {
536        if (weaponSystem_)
537        {
538            return weaponSystem_->getMunition(identifier);
539        }
540
541        return nullptr;
542    }
543
544    //Tell the Map (RadarViewable), if this is a playership
545    void Pawn::startLocalHumanControl()
546    {
547//        SUPER(ControllableEntity, startLocalHumanControl());
548        ControllableEntity::startLocalHumanControl();
549        this->isHumanShip_ = true;
550    }
551
552    void Pawn::changedVisibility(void)
553    {
554        SUPER(Pawn, changedVisibility);
555
556        // enable proper radarviewability when the visibility is changed
557        this->RadarViewable::settingsChanged();
558    }
559
560
561    // A function to check if this pawn's controller is the master of any formationcontroller
562    bool Pawn::hasSlaves()
563    {
564        for (FormationController* controller : ObjectList<FormationController>())
565        {
566            // checks if the pawn's controller has a slave
567            if (this->hasHumanController() && controller->getMaster() == this->getPlayer()->getController())
568                return true;
569        }
570        return false;
571    }
572
573    // A function that returns a slave of the pawn's controller
574    Controller* Pawn::getSlave(){
575        for (FormationController* controller : ObjectList<FormationController>())
576        {
577            if (this->hasHumanController() && controller->getMaster() == this->getPlayer()->getController())
578                return controller->getController();
579        }
580        return nullptr;
581    }
582
583
584    void Pawn::setExplosionSound(const std::string &explosionSound)
585    {
586        if(explosionSound_ )
587            explosionSound_->setSource(explosionSound);
588        else
589            assert(0); // This should never happen, because soundpointer is only available on master
590    }
591
592    const std::string& Pawn::getExplosionSound()
593    {
594        if( explosionSound_ )
595            return explosionSound_->getSource();
596        else
597            assert(0);
598        return BLANKSTRING;
599    }
600}
Note: See TracBrowser for help on using the repository browser.