Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/PresentationFS18/src/orxonox/worldentities/pawns/Pawn.cc @ 12012

Last change on this file since 12012 was 11783, checked in by landauf, 7 years ago

merged Presentation_HS17_merge back to trunk

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