Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup2012/src/orxonox/worldentities/pawns/Pawn.cc @ 9099

Last change on this file since 9099 was 9099, checked in by lkevin, 12 years ago

Found a way to implement damage modifiers by
adding a flag to the pawn and then using this
flag in pawn::damage().

More compilation errors to be fixed though, a tick
function seems to be missing.

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