Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9697 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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