Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/menue/src/orxonox/worldentities/pawns/Pawn.cc @ 9019

Last change on this file since 9019 was 8855, checked in by dafrick, 13 years ago

Cleaning up game immersion. Roughly documenting weapons module.

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