Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/formationupdate/src/orxonox/worldentities/pawns/Pawn.cc @ 9584

Last change on this file since 9584 was 9584, checked in by maxima, 12 years ago

Second Formation in Maxim-Level

  • Property svn:eol-style set to native
File size: 16.0 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    CreateFactory(Pawn);
54
55    Pawn::Pawn(BaseObject* creator)
56        : ControllableEntity(creator)
57        , RadarViewable(creator, 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);
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->getCreator());
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    void Pawn::death()
311    {
312        /* TEST TEST This is used to find out if the current pawn is also
313         * the master of the formation.
314         *
315         * NOTE: This does not yet check if the current pawn is actually
316         *       the humanplayer or not!
317         */
318        for (ObjectList<FormationController>::iterator it = 
319          ObjectList<FormationController>::begin(); 
320          it != ObjectList<FormationController>::end(); ++it )
321        {
322          orxout(user_warning) << "Test! Master: " << it->getMaster() 
323            << " My controller: " << this->getPlayer()->getController() << endl;
324        }
325        /* TEST TEST */
326
327           
328
329
330        this->setHealth(1);
331        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
332        {
333            // Set bAlive_ to false and wait for PawnManager to do the destruction
334            this->bAlive_ = false;
335
336            this->setDestroyWhenPlayerLeft(false);
337
338            if (this->getGametype())
339                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
340
341            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
342                this->getPlayer()->stopControl();
343
344            if (GameMode::isMaster())
345            {
346//                this->deathEffect();
347                this->goWithStyle();
348            }
349        }
350    }
351    void Pawn::goWithStyle()
352    {
353        this->bAlive_ = false;
354        this->setDestroyWhenPlayerLeft(false);
355
356        BigExplosion* chunk = new BigExplosion(this->getCreator());
357        chunk->setPosition(this->getPosition());
358
359    }
360    void Pawn::deatheffect()
361    {
362        // play death effect
363        {
364            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
365            effect->setPosition(this->getPosition());
366            effect->setOrientation(this->getOrientation());
367            effect->setDestroyAfterLife(true);
368            effect->setSource("Orxonox/explosion2b");
369            effect->setLifetime(4.0f);
370        }
371        {
372            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
373            effect->setPosition(this->getPosition());
374            effect->setOrientation(this->getOrientation());
375            effect->setDestroyAfterLife(true);
376            effect->setSource("Orxonox/smoke6");
377            effect->setLifetime(4.0f);
378        }
379        {
380            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
381            effect->setPosition(this->getPosition());
382            effect->setOrientation(this->getOrientation());
383            effect->setDestroyAfterLife(true);
384            effect->setSource("Orxonox/sparks");
385            effect->setLifetime(4.0f);
386        }
387        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
388        {
389            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
390            chunk->setPosition(this->getPosition());
391        }
392    }
393
394    void Pawn::fired(unsigned int firemode)
395    {
396        if (this->weaponSystem_)
397            this->weaponSystem_->fire(firemode);
398    }
399
400    void Pawn::reload()
401    {
402        this->bReload_ = true;
403    }
404
405    void Pawn::postSpawn()
406    {
407        this->setHealth(this->initialHealth_);
408        if (GameMode::isMaster())
409            this->spawneffect();
410    }
411
412    /* WeaponSystem:
413    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
414    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
415    *       --> e.g. Pickup-Items
416    */
417    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
418    {
419        this->attach(wSlot);
420        if (this->weaponSystem_)
421            this->weaponSystem_->addWeaponSlot(wSlot);
422    }
423
424    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
425    {
426        if (this->weaponSystem_)
427            return this->weaponSystem_->getWeaponSlot(index);
428        else
429            return 0;
430    }
431
432    void Pawn::addWeaponSet(WeaponSet * wSet)
433    {
434        if (this->weaponSystem_)
435            this->weaponSystem_->addWeaponSet(wSet);
436    }
437
438    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
439    {
440        if (this->weaponSystem_)
441            return this->weaponSystem_->getWeaponSet(index);
442        else
443            return 0;
444    }
445
446    void Pawn::addWeaponPack(WeaponPack * wPack)
447    {
448        if (this->weaponSystem_)
449        {
450            this->weaponSystem_->addWeaponPack(wPack);
451            this->addedWeaponPack(wPack);
452        }
453    }
454
455    void Pawn::addWeaponPackXML(WeaponPack * wPack)
456    {
457        if (this->weaponSystem_)
458        {
459            if (!this->weaponSystem_->addWeaponPack(wPack))
460                wPack->destroy();
461            else
462                this->addedWeaponPack(wPack);
463        }
464    }
465
466    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
467    {
468        if (this->weaponSystem_)
469            return this->weaponSystem_->getWeaponPack(index);
470        else
471            return 0;
472    }
473
474    //Tell the Map (RadarViewable), if this is a playership
475    void Pawn::startLocalHumanControl()
476    {
477//        SUPER(ControllableEntity, changedPlayer());
478        ControllableEntity::startLocalHumanControl();
479        this->isHumanShip_ = true;
480    }
481
482    void Pawn::changedVisibility(void)
483    {
484        SUPER(Pawn, changedVisibility);
485
486        // enable proper radarviewability when the visibility is changed
487        this->RadarViewable::settingsChanged();
488    }
489
490}
Note: See TracBrowser for help on using the repository browser.