Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8904 was 8891, checked in by jo, 13 years ago

Ai and tutorial improvements merged back to the trunk. AI features: all weapons are used, the ai-firestrength is configurable, bots are able to collect pickups . I've set the tutorial level as default level.

  • Property svn:eol-style set to native
File size: 15.1 KB
RevLine 
[2072]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:
[8706]25 *      Simon Miescher
[2072]26 *
27 */
28
29#include "Pawn.h"
30
[3280]31#include <algorithm>
32
[3196]33#include "core/CoreIncludes.h"
[2896]34#include "core/GameMode.h"
[2072]35#include "core/XMLPort.h"
[3196]36#include "network/NetworkFunction.h"
37
[5735]38#include "infos/PlayerInfo.h"
[6417]39#include "controllers/Controller.h"
[5735]40#include "gametypes/Gametype.h"
[5737]41#include "graphics/ParticleSpawner.h"
[5735]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"
[2072]48
49namespace orxonox
50{
51    CreateFactory(Pawn);
52
[7163]53    Pawn::Pawn(BaseObject* creator)
54        : ControllableEntity(creator)
55        , RadarViewable(creator, static_cast<WorldEntity*>(this))
[2072]56    {
57        RegisterObject(Pawn);
58
[2662]59        this->bAlive_ = true;
[3053]60        this->bReload_ = false;
[2072]61
62        this->health_ = 0;
63        this->maxHealth_ = 0;
64        this->initialHealth_ = 0;
[8706]65
[7163]66        this->shieldHealth_ = 0;
[8706]67        this->initialShieldHealth_ = 0;
68        this->maxShieldHealth_ = 100; //otherwise shield might increase to float_max
[7163]69        this->shieldAbsorption_ = 0.5;
[2072]70
[8706]71        this->reloadRate_ = 0;
72        this->reloadWaitTime_ = 1.0f;
73        this->reloadWaitCountdown_ = 0;
74
[2072]75        this->lastHitOriginator_ = 0;
76
[2662]77        this->spawnparticleduration_ = 3.0f;
[2098]78
[6417]79        this->aimPosition_ = Vector3::ZERO;
80
[2896]81        if (GameMode::isMaster())
[2662]82        {
83            this->weaponSystem_ = new WeaponSystem(this);
[3053]84            this->weaponSystem_->setPawn(this);
[2662]85        }
86        else
87            this->weaponSystem_ = 0;
88
89        this->setRadarObjectColour(ColourValue::Red);
90        this->setRadarObjectShape(RadarViewable::Dot);
91
[2072]92        this->registerVariables();
[3089]93
94        this->isHumanShip_ = this->hasLocalController();
[8891]95
[8329]96        this->setSyncMode(ObjectDirection::Bidirectional); // needed to synchronise e.g. aimposition
[2072]97    }
98
99    Pawn::~Pawn()
100    {
[2662]101        if (this->isInitialized())
102        {
103            if (this->weaponSystem_)
[5929]104                this->weaponSystem_->destroy();
[2662]105        }
[2072]106    }
107
108    void Pawn::XMLPort(Element& xmlelement, XMLPort::Mode mode)
109    {
110        SUPER(Pawn, XMLPort, xmlelement, mode);
111
[2662]112        XMLPortParam(Pawn, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
[2072]113        XMLPortParam(Pawn, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
114        XMLPortParam(Pawn, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
[7163]115
116        XMLPortParam(Pawn, "shieldhealth", setShieldHealth, getShieldHealth, xmlelement, mode).defaultValues(0);
[8706]117        XMLPortParam(Pawn, "initialshieldhealth", setInitialShieldHealth, getInitialShieldHealth, xmlelement, mode).defaultValues(0);
118        XMLPortParam(Pawn, "maxshieldhealth", setMaxShieldHealth, getMaxShieldHealth, xmlelement, mode).defaultValues(100);
[7163]119        XMLPortParam(Pawn, "shieldabsorption", setShieldAbsorption, getShieldAbsorption, xmlelement, mode).defaultValues(0);
120
[2662]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
[3053]125        XMLPortObject(Pawn, WeaponSlot, "weaponslots", addWeaponSlot, getWeaponSlot, xmlelement, mode);
126        XMLPortObject(Pawn, WeaponSet, "weaponsets", addWeaponSet, getWeaponSet, xmlelement, mode);
[6417]127        XMLPortObject(Pawn, WeaponPack, "weapons", addWeaponPackXML, getWeaponPack, xmlelement, mode);
[8706]128
129        XMLPortParam(Pawn, "reloadrate", setReloadRate, getReloadRate, xmlelement, mode).defaultValues(0);
130        XMLPortParam(Pawn, "reloadwaittime", setReloadWaitTime, getReloadWaitTime, xmlelement, mode).defaultValues(1.0f);
[2072]131    }
132
133    void Pawn::registerVariables()
134    {
[7163]135        registerVariable(this->bAlive_,           VariableDirection::ToClient);
136        registerVariable(this->health_,           VariableDirection::ToClient);
[8706]137        registerVariable(this->maxHealth_,        VariableDirection::ToClient);
[7163]138        registerVariable(this->shieldHealth_,     VariableDirection::ToClient);
[8706]139        registerVariable(this->maxShieldHealth_,  VariableDirection::ToClient);
[7163]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
[2072]143    }
144
145    void Pawn::tick(float dt)
146    {
[2809]147        SUPER(Pawn, tick, dt);
[2072]148
[3053]149        this->bReload_ = false;
[2662]150
[8706]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
[3084]162        if (GameMode::isMaster())
[8706]163        {
[3087]164            if (this->health_ <= 0 && bAlive_)
[6864]165            {
[8706]166                this->fireEvent(); // Event to notify anyone who wants to know about the death.
[3087]167                this->death();
[6864]168            }
[8706]169        }
[2072]170    }
171
[7889]172    void Pawn::preDestroy()
173    {
174        // yay, multiple inheritance!
175        this->ControllableEntity::preDestroy();
176        this->PickupCarrier::preDestroy();
177    }
178
[2826]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
[8706]195
[2072]196    void Pawn::setHealth(float health)
197    {
[8706]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
[2072]199    }
200
[8706]201    void Pawn::setShieldHealth(float shieldHealth)
[2072]202    {
[8706]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    {
[2826]228        if (this->getGametype() && this->getGametype()->allowPawnDamage(this, originator))
229        {
[8706]230            if (shielddamage >= this->getShieldHealth())
[7163]231            {
232                this->setShieldHealth(0);
[8706]233                this->setHealth(this->health_ - (healthdamage + damage));
[7163]234            }
[8706]235            else
236            {
237                this->setShieldHealth(this->shieldHealth_ - shielddamage);
[7163]238
[8706]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);
[7163]243
[8706]244                // set remaining damage to health
245                this->setHealth(this->health_ - (damage - shielddamage) - healthdamage);
[7163]246            }
247
[2826]248            this->lastHitOriginator_ = originator;
249        }
[2072]250    }
251
[8706]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)
[2072]258    {
[6417]259        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
[2826]260        {
[8706]261            this->damage(damage, healthdamage, shielddamage, originator);
[2826]262            this->setVelocity(this->getVelocity() + force);
263        }
[2072]264    }
265
[8706]266
267    void Pawn::hit(Pawn* originator, btManifoldPoint& contactpoint, float damage, float healthdamage, float shielddamage)
[6417]268    {
269        if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator) && (!this->getController() || !this->getController()->getGodMode()) )
270        {
[8706]271            this->damage(damage, healthdamage, shielddamage, originator);
[6417]272
273            if ( this->getController() )
[8706]274                this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage?
[6417]275        }
276    }
277
[8706]278
[2072]279    void Pawn::kill()
280    {
281        this->damage(this->health_);
282        this->death();
283    }
284
[2662]285    void Pawn::spawneffect()
[2072]286    {
287        // play spawn effect
[6417]288        if (!this->spawnparticlesource_.empty())
[2662]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        }
[2072]297    }
298
299    void Pawn::death()
300    {
[3033]301        this->setHealth(1);
[2826]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;
[2662]306
[2826]307            this->setDestroyWhenPlayerLeft(false);
[2662]308
[2826]309            if (this->getGametype())
310                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
[2662]311
[3038]312            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
313                this->getPlayer()->stopControl();
[2072]314
[2896]315            if (GameMode::isMaster())
[3087]316            {
317//                this->deathEffect();
318                this->goWithStyle();
319            }
[2826]320        }
[2662]321    }
[3087]322    void Pawn::goWithStyle()
323    {
324        this->bAlive_ = false;
325        this->setDestroyWhenPlayerLeft(false);
[2072]326
[3087]327        BigExplosion* chunk = new BigExplosion(this->getCreator());
328        chunk->setPosition(this->getPosition());
329
330    }
[2662]331    void Pawn::deatheffect()
332    {
[2072]333        // play death effect
[2662]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        }
[2072]363    }
364
[6417]365    void Pawn::fired(unsigned int firemode)
[2098]366    {
[6417]367        if (this->weaponSystem_)
368            this->weaponSystem_->fire(firemode);
[2098]369    }
370
[3053]371    void Pawn::reload()
372    {
373        this->bReload_ = true;
374    }
375
[2072]376    void Pawn::postSpawn()
377    {
378        this->setHealth(this->initialHealth_);
[2896]379        if (GameMode::isMaster())
[2662]380            this->spawneffect();
[2072]381    }
[2662]382
[2893]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    */
[3053]388    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
[2662]389    {
390        this->attach(wSlot);
391        if (this->weaponSystem_)
[3053]392            this->weaponSystem_->addWeaponSlot(wSlot);
[2662]393    }
394
395    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
396    {
397        if (this->weaponSystem_)
[3053]398            return this->weaponSystem_->getWeaponSlot(index);
[2662]399        else
400            return 0;
401    }
402
[3053]403    void Pawn::addWeaponSet(WeaponSet * wSet)
[2662]404    {
405        if (this->weaponSystem_)
[3053]406            this->weaponSystem_->addWeaponSet(wSet);
[2662]407    }
408
[3053]409    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
[2662]410    {
411        if (this->weaponSystem_)
[3053]412            return this->weaponSystem_->getWeaponSet(index);
[2662]413        else
414            return 0;
415    }
416
[3053]417    void Pawn::addWeaponPack(WeaponPack * wPack)
[2662]418    {
419        if (this->weaponSystem_)
[7163]420        {
[3053]421            this->weaponSystem_->addWeaponPack(wPack);
[7163]422            this->addedWeaponPack(wPack);
423        }
[2662]424    }
425
[6417]426    void Pawn::addWeaponPackXML(WeaponPack * wPack)
427    {
428        if (this->weaponSystem_)
[7163]429        {
[6417]430            if (!this->weaponSystem_->addWeaponPack(wPack))
431                wPack->destroy();
[7163]432            else
433                this->addedWeaponPack(wPack);
434        }
[6417]435    }
436
[3053]437    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
[2662]438    {
439        if (this->weaponSystem_)
[3053]440            return this->weaponSystem_->getWeaponPack(index);
[2662]441        else
442            return 0;
443    }
444
[3089]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    }
[8891]452
453    void Pawn::changedActivity(void)
454    {
455        SUPER(Pawn, changedActivity);
456
457        this->setRadarVisibility(this->isActive());
458    }
459
460    void Pawn::changedVisibility(void)
461    {
462        SUPER(Pawn, changedVisibility);
463        //this->setVisible(this->isVisible());
464        this->setRadarVisibility(this->isVisible());
465    }
466
[2072]467}
Note: See TracBrowser for help on using the repository browser.