Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/strongTurret/src/orxonox/worldentities/pawns/Pawn.cc @ 10169

Last change on this file since 10169 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
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
49#include "controllers/FormationController.h"
50
51namespace orxonox
52{
53    RegisterClass(Pawn);
54
55    Pawn::Pawn(Context* context)
56        : ControllableEntity(context)
57        , RadarViewable(this, static_cast<WorldEntity*>(this))
58    {
59        RegisterObject(Pawn);
60
61        this->bAlive_ = true;
62        this->bReload_ = false;
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
73        this->reloadRate_ = 0;
74        this->reloadWaitTime_ = 1.0f;
75        this->reloadWaitCountdown_ = 0;
76
77        this->lastHitOriginator_ = 0;
78
79        // set damage multiplier to default value 1, meaning nominal damage
80        this->damageMultiplier_ = 1;
81
82        this->spawnparticleduration_ = 3.0f;
83
84        this->aimPosition_ = Vector3::ZERO;
85
86        if (GameMode::isMaster())
87        {
88            this->weaponSystem_ = new WeaponSystem(this->getContext());
89            this->weaponSystem_->setPawn(this);
90        }
91        else
92            this->weaponSystem_ = 0;
93
94        this->setRadarObjectColour(ColourValue::Red);
95        this->setRadarObjectShape(RadarViewable::Dot);
96
97        this->registerVariables();
98
99        this->isHumanShip_ = this->hasLocalController();
100
101        this->setSyncMode(ObjectDirection::Bidirectional); // needed to synchronise e.g. aimposition
102    }
103
104    Pawn::~Pawn()
105    {
106        if (this->isInitialized())
107        {
108            if (this->weaponSystem_)
109                this->weaponSystem_->destroy();
110        }
111    }
112
113    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
114    {
115        SUPER(Pawn, XMLPort, xmlelement, mode);
116
117        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
118        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
119        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
120
121        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
122        XMLPortParam(Pawn, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0);
123        XMLPortParam(Pawn, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
124        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
125
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
130        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
131        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
132        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
133
134        XMLPortParam(Pawn, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0);
135        XMLPortParam(Pawn, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f);
136
137        XMLPortParam ( RadarViewable, "radarname", setRadarName, getRadarName, xmlelement, mode );
138    }
139
140    void Pawn::registerVariables()
141    {
142        registerVariable(this->bAlive_,           VariableDirection::ToClient);
143        registerVariable(this->health_,           VariableDirection::ToClient);
144        registerVariable(this->maxHealth_,        VariableDirection::ToClient);
145        registerVariable(this->shieldHealth_,     VariableDirection::ToClient);
146        registerVariable(this->maxShieldHealth_,  VariableDirection::ToClient);
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
150    }
151
152    void Pawn::tick(float dt)
153    {
154        SUPER(Pawn, tick, dt);
155
156        this->bReload_ = false;
157
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
169        if (GameMode::isMaster())
170        {
171            if (this->health_ <= 0 && bAlive_)
172            {
173                this->fireEvent(); // Event to notify anyone who wants to know about the death.
174                this->death();
175            }
176        }
177    }
178
179    void Pawn::preDestroy()
180    {
181        // yay, multiple inheritance!
182        this->ControllableEntity::preDestroy();
183        this->PickupCarrier::preDestroy();
184    }
185
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
202
203    void Pawn::setHealth(float health)
204    {
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
206    }
207
208    void Pawn::setShieldHealth(float shieldHealth)
209    {
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    {
235        // Applies multiplier given by the DamageBoost Pickup.
236        if (originator)
237            damage *= originator->getDamageMultiplier();
238
239        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
240        {
241            if (shielddamage >= this->getShieldHealth())
242            {
243                this->setShieldHealth(0);
244                this->setHealth(this->health_ - (healthdamage + damage));
245            }
246            else
247            {
248                this->setShieldHealth(this->shieldHealth_ - shielddamage);
249
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);
254
255                // set remaining damage to health
256                this->setHealth(this->health_ - (damage - shielddamage) - healthdamage);
257            }
258
259            this->lastHitOriginator_ = originator;
260        }
261    }
262
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)
269    {
270        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
271        {
272            this->damage(damage, healthdamage, shielddamage, originator);
273            this->setVelocity(this->getVelocity() + force);
274        }
275    }
276
277
278    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage, float healthdamage, float shielddamage)
279    {
280        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
281        {
282            this->damage(damage, healthdamage, shielddamage, originator);
283
284            if ( this->getController() )
285                this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage?
286        }
287    }
288
289
290    void Pawn::kill()
291    {
292        this->damage(this->health_);
293        this->death();
294    }
295
296    void Pawn::spawneffect()
297    {
298        // play spawn effect
299        if (!this->spawnparticlesource_.empty())
300        {
301            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
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        }
308    }
309
310
311    void Pawn::death()
312    {
313        this->setHealth(1);
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;
318
319            this->setDestroyWhenPlayerLeft(false);
320
321            if (this->getGametype())
322                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
323
324            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
325            {
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();
331
332
333                    if(!entity->hasHumanController())
334                    {
335                        // delete the AIController // <-- TODO: delete? nothing is deleted here... should we delete the controller?
336                        slave->setControllableEntity(0);
337
338                        // set a new master within the formation
339                        orxonox_cast<FormationController*>(this->getController())->setNewMasterWithinFormation(orxonox_cast<FormationController*>(slave));
340
341                        // start to control a slave
342                        this->getPlayer()->startControl(entity);
343                    }
344                    else
345                    {
346                        this->getPlayer()->stopControl();
347                    }
348
349                }
350                else
351                {
352                    this->getPlayer()->stopControl();
353                }
354            }
355            if (GameMode::isMaster())
356            {
357//                this->deathEffect();
358                this->goWithStyle();
359            }
360        }
361    }
362    void Pawn::goWithStyle()
363    {
364        this->bAlive_ = false;
365        this->setDestroyWhenPlayerLeft(false);
366
367        BigExplosion* chunk = new BigExplosion(this->getContext());
368        chunk->setPosition(this->getPosition());
369
370    }
371    void Pawn::deatheffect()
372    {
373        // play death effect
374        {
375            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
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        {
383            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
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        {
391            ParticleSpawner* effect = new ParticleSpawner(this->getContext());
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        {
400            ExplosionChunk* chunk = new ExplosionChunk(this->getContext());
401            chunk->setPosition(this->getPosition());
402        }
403    }
404
405    void Pawn::fired(unsigned int firemode)
406    {
407        if (this->weaponSystem_)
408            this->weaponSystem_->fire(firemode);
409    }
410
411    void Pawn::reload()
412    {
413        this->bReload_ = true;
414    }
415
416    void Pawn::postSpawn()
417    {
418        this->setHealth(this->initialHealth_);
419        if (GameMode::isMaster())
420            this->spawneffect();
421    }
422
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    */
428    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
429    {
430        this->attach(wSlot);
431        if (this->weaponSystem_)
432            this->weaponSystem_->addWeaponSlot(wSlot);
433    }
434
435    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
436    {
437        if (this->weaponSystem_)
438            return this->weaponSystem_->getWeaponSlot(index);
439        else
440            return 0;
441    }
442
443    void Pawn::addWeaponSet(WeaponSet * wSet)
444    {
445        if (this->weaponSystem_)
446            this->weaponSystem_->addWeaponSet(wSet);
447    }
448
449    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
450    {
451        if (this->weaponSystem_)
452            return this->weaponSystem_->getWeaponSet(index);
453        else
454            return 0;
455    }
456
457    void Pawn::addWeaponPack(WeaponPack * wPack)
458    {
459        if (this->weaponSystem_)
460        {
461            this->weaponSystem_->addWeaponPack(wPack);
462            this->addedWeaponPack(wPack);
463        }
464    }
465
466    void Pawn::addWeaponPackXML(WeaponPack * wPack)
467    {
468        if (this->weaponSystem_)
469        {
470            if (!this->weaponSystem_->addWeaponPack(wPack))
471                wPack->destroy();
472            else
473                this->addedWeaponPack(wPack);
474        }
475    }
476
477    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
478    {
479        if (this->weaponSystem_)
480            return this->weaponSystem_->getWeaponPack(index);
481        else
482            return 0;
483    }
484
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    }
492
493    void Pawn::changedVisibility(void)
494    {
495        SUPER(Pawn, changedVisibility);
496
497        // enable proper radarviewability when the visibility is changed
498        this->RadarViewable::settingsChanged();
499    }
500
501
502    // A function to check if this pawn's controller is the master of any formationcontroller
503    bool Pawn::hasSlaves()
504    {
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;
514    }
515
516    // A function that returns a slave of the pawn's controller
517    Controller* Pawn::getSlave(){
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;
526    }
527
528
529
530}
Note: See TracBrowser for help on using the repository browser.