Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8329 was 8329, checked in by scheusso, 13 years ago

preparing countermeasures against clients sending unwanted data

  • Property svn:eol-style set to native
File size: 12.7 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 *      ...
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 "PawnManager.h"
39#include "infos/PlayerInfo.h"
40#include "controllers/Controller.h"
41#include "gametypes/Gametype.h"
42#include "graphics/ParticleSpawner.h"
43#include "worldentities/ExplosionChunk.h"
44#include "worldentities/BigExplosion.h"
45#include "weaponsystem/WeaponSystem.h"
46#include "weaponsystem/WeaponSlot.h"
47#include "weaponsystem/WeaponPack.h"
48#include "weaponsystem/WeaponSet.h"
49
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        PawnManager::touch();
62        this->bAlive_ = true;
63        this->bReload_ = false;
64
65        this->health_ = 0;
66        this->maxHealth_ = 0;
67        this->initialHealth_ = 0;
68        this->shieldHealth_ = 0;
69        this->shieldAbsorption_ = 0.5;
70
71        this->lastHitOriginator_ = 0;
72
73        this->spawnparticleduration_ = 3.0f;
74
75        this->aimPosition_ = Vector3::ZERO;
76
77        if (GameMode::isMaster())
78        {
79            this->weaponSystem_ = new WeaponSystem(this);
80            this->weaponSystem_->setPawn(this);
81        }
82        else
83            this->weaponSystem_ = 0;
84
85        this->setRadarObjectColour(ColourValue::Red);
86        this->setRadarObjectShape(RadarViewable::Dot);
87
88        this->registerVariables();
89
90        this->isHumanShip_ = this->hasLocalController();
91       
92        this->setSyncMode(ObjectDirection::Bidirectional); // needed to synchronise e.g. aimposition
93    }
94
95    Pawn::~Pawn()
96    {
97        if (this->isInitialized())
98        {
99            if (this->weaponSystem_)
100                this->weaponSystem_->destroy();
101        }
102    }
103
104    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
105    {
106        SUPER(Pawn, XMLPort, xmlelement, mode);
107
108        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
109        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
110        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
111
112        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
113        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
114
115        XMLPortParam(Pawn, "spawnparticlesource", setSpawnParticleSource, getSpawnParticleSource, xmlelement, mode);
116        XMLPortParam(Pawn, "spawnparticleduration", setSpawnParticleDuration, getSpawnParticleDuration, xmlelement, mode).defaultValues(3.0f);
117        XMLPortParam(Pawn, "explosionchunks", setExplosionChunks, getExplosionChunks, xmlelement, mode).defaultValues(7);
118
119        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
120        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
121        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
122    }
123
124    void Pawn::registerVariables()
125    {
126        registerVariable(this->bAlive_,           VariableDirection::ToClient);
127        registerVariable(this->health_,           VariableDirection::ToClient);
128        registerVariable(this->initialHealth_,    VariableDirection::ToClient);
129        registerVariable(this->shieldHealth_,     VariableDirection::ToClient);
130        registerVariable(this->shieldAbsorption_, VariableDirection::ToClient);
131        registerVariable(this->bReload_,          VariableDirection::ToServer);
132        registerVariable(this->aimPosition_,      VariableDirection::ToServer);  // For the moment this variable gets only transfered to the server
133    }
134
135    void Pawn::tick(float dt)
136    {
137        SUPER(Pawn, tick, dt);
138
139        this->bReload_ = false;
140
141        if (GameMode::isMaster())
142            if (this->health_ <= 0 && bAlive_)
143            {
144                this->fireEvent(); // Event to notify anyone who want's to know about the death.
145                this->death();
146            }
147    }
148
149    void Pawn::preDestroy()
150    {
151        // yay, multiple inheritance!
152        this->ControllableEntity::preDestroy();
153        this->PickupCarrier::preDestroy();
154    }
155
156    void Pawn::setPlayer(PlayerInfo* player)
157    {
158        ControllableEntity::setPlayer(player);
159
160        if (this->getGametype())
161            this->getGametype()->playerStartsControllingPawn(player, this);
162    }
163
164    void Pawn::removePlayer()
165    {
166        if (this->getGametype())
167            this->getGametype()->playerStopsControllingPawn(this->getPlayer(), this);
168
169        ControllableEntity::removePlayer();
170    }
171
172    void Pawn::setHealth(float health)
173    {
174        this->health_ = std::min(health, this->maxHealth_);
175    }
176
177    void Pawn::damage(float damage, Pawn* originator)
178    {
179        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
180        {
181            //share the dealt damage to the shield and the Pawn.
182            float shielddamage = damage*this->shieldAbsorption_;
183            float healthdamage = damage*(1-this->shieldAbsorption_);
184
185            // In case the shield can not take all the shield damage.
186            if (shielddamage > this->getShieldHealth())
187            {
188                healthdamage += shielddamage-this->getShieldHealth();
189                this->setShieldHealth(0);
190            }
191
192            this->setHealth(this->health_ - healthdamage);
193
194            if (this->getShieldHealth() > 0)
195            {
196                this->setShieldHealth(this->shieldHealth_ - shielddamage);
197            }
198
199            this->lastHitOriginator_ = originator;
200
201            // play damage effect
202        }
203    }
204
205    void Pawn::hit(Pawn* originator, const Vector3& force, float damage)
206    {
207        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
208        {
209            this->damage(damage, originator);
210            this->setVelocity(this->getVelocity() + force);
211
212            // play hit effect
213        }
214    }
215
216    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage)
217    {
218        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
219        {
220            this->damage(damage, originator);
221
222            if ( this->getController() )
223                this->getController()->hit(originator, contactpoint, damage);
224
225            // play hit effect
226        }
227    }
228
229    void Pawn::kill()
230    {
231        this->damage(this->health_);
232        this->death();
233    }
234
235    void Pawn::spawneffect()
236    {
237        // play spawn effect
238        if (!this->spawnparticlesource_.empty())
239        {
240            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
241            effect->setPosition(this->getPosition());
242            effect->setOrientation(this->getOrientation());
243            effect->setDestroyAfterLife(true);
244            effect->setSource(this->spawnparticlesource_);
245            effect->setLifetime(this->spawnparticleduration_);
246        }
247    }
248
249    void Pawn::death()
250    {
251        this->setHealth(1);
252        if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
253        {
254            // Set bAlive_ to false and wait for PawnManager to do the destruction
255            this->bAlive_ = false;
256
257            this->setDestroyWhenPlayerLeft(false);
258
259            if (this->getGametype())
260                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
261
262            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
263                this->getPlayer()->stopControl();
264
265            if (GameMode::isMaster())
266            {
267//                this->deathEffect();
268                this->goWithStyle();
269            }
270        }
271    }
272    void Pawn::goWithStyle()
273    {
274        this->bAlive_ = false;
275        this->setDestroyWhenPlayerLeft(false);
276
277        BigExplosion* chunk = new BigExplosion(this->getCreator());
278        chunk->setPosition(this->getPosition());
279
280    }
281    void Pawn::deatheffect()
282    {
283        // play death effect
284        {
285            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
286            effect->setPosition(this->getPosition());
287            effect->setOrientation(this->getOrientation());
288            effect->setDestroyAfterLife(true);
289            effect->setSource("Orxonox/explosion2b");
290            effect->setLifetime(4.0f);
291        }
292        {
293            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
294            effect->setPosition(this->getPosition());
295            effect->setOrientation(this->getOrientation());
296            effect->setDestroyAfterLife(true);
297            effect->setSource("Orxonox/smoke6");
298            effect->setLifetime(4.0f);
299        }
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("Orxonox/sparks");
306            effect->setLifetime(4.0f);
307        }
308        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
309        {
310            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
311            chunk->setPosition(this->getPosition());
312        }
313    }
314
315    void Pawn::fired(unsigned int firemode)
316    {
317        if (this->weaponSystem_)
318            this->weaponSystem_->fire(firemode);
319    }
320
321    void Pawn::reload()
322    {
323        this->bReload_ = true;
324    }
325
326    void Pawn::postSpawn()
327    {
328        this->setHealth(this->initialHealth_);
329        if (GameMode::isMaster())
330            this->spawneffect();
331    }
332
333    /* WeaponSystem:
334    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
335    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
336    *       --> e.g. Pickup-Items
337    */
338    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
339    {
340        this->attach(wSlot);
341        if (this->weaponSystem_)
342            this->weaponSystem_->addWeaponSlot(wSlot);
343    }
344
345    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
346    {
347        if (this->weaponSystem_)
348            return this->weaponSystem_->getWeaponSlot(index);
349        else
350            return 0;
351    }
352
353    void Pawn::addWeaponSet(WeaponSet * wSet)
354    {
355        if (this->weaponSystem_)
356            this->weaponSystem_->addWeaponSet(wSet);
357    }
358
359    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
360    {
361        if (this->weaponSystem_)
362            return this->weaponSystem_->getWeaponSet(index);
363        else
364            return 0;
365    }
366
367    void Pawn::addWeaponPack(WeaponPack * wPack)
368    {
369        if (this->weaponSystem_)
370        {
371            this->weaponSystem_->addWeaponPack(wPack);
372            this->addedWeaponPack(wPack);
373        }
374    }
375
376    void Pawn::addWeaponPackXML(WeaponPack * wPack)
377    {
378        if (this->weaponSystem_)
379        {
380            if (!this->weaponSystem_->addWeaponPack(wPack))
381                wPack->destroy();
382            else
383                this->addedWeaponPack(wPack);
384        }
385    }
386
387    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
388    {
389        if (this->weaponSystem_)
390            return this->weaponSystem_->getWeaponPack(index);
391        else
392            return 0;
393    }
394
395    //Tell the Map (RadarViewable), if this is a playership
396    void Pawn::startLocalHumanControl()
397    {
398//        SUPER(ControllableEntity, changedPlayer());
399        ControllableEntity::startLocalHumanControl();
400        this->isHumanShip_ = true;
401    }
402}
Note: See TracBrowser for help on using the repository browser.