Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9946 was 9946, checked in by landauf, 10 years ago

removed debug output

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