Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8733 was 8706, checked in by dafrick, 13 years ago

Merging presentation branch back into trunk.
There are many new features and also a lot of other changes and bugfixes, if you want to know, digg through the svn log.
Not everything is yet working as it should, but it should be fairly stable. If you habe any bug reports, just send me an email.

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