Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9546 was 9546, checked in by smerkli, 11 years ago

Added a BigExplosion creation to the Pawn constructor.
This loads all the files for the first time and hence
gets rid of the long delay when the first ship is
killed.

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