Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/orxonox/worldentities/pawns/Pawn.cc @ 8735

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

Adjust weapon behaviour if bot dies and is respawned with a different weaponsetting than before.

  • Property svn:eol-style set to native
File size: 14.9 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            if ( this->getController()&& (!this->isHumanShip_) ) //announce death to the ai
307            {
308                 this->getController()->setDeathFlag();
309            }
310            // Set bAlive_ to false and wait for PawnManager to do the destruction
311            this->bAlive_ = false;
312
313            this->setDestroyWhenPlayerLeft(false);
314
315            if (this->getGametype())
316                this->getGametype()->pawnKilled(this, this->lastHitOriginator_);
317
318            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
319                this->getPlayer()->stopControl();
320
321            if (GameMode::isMaster())
322            {
323//                this->deathEffect();
324                this->goWithStyle();
325            }
326        }
327    }
328    void Pawn::goWithStyle()
329    {
330        this->bAlive_ = false;
331        this->setDestroyWhenPlayerLeft(false);
332
333        BigExplosion* chunk = new BigExplosion(this->getCreator());
334        chunk->setPosition(this->getPosition());
335
336    }
337    void Pawn::deatheffect()
338    {
339        // play death effect
340        {
341            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
342            effect->setPosition(this->getPosition());
343            effect->setOrientation(this->getOrientation());
344            effect->setDestroyAfterLife(true);
345            effect->setSource("Orxonox/explosion2b");
346            effect->setLifetime(4.0f);
347        }
348        {
349            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
350            effect->setPosition(this->getPosition());
351            effect->setOrientation(this->getOrientation());
352            effect->setDestroyAfterLife(true);
353            effect->setSource("Orxonox/smoke6");
354            effect->setLifetime(4.0f);
355        }
356        {
357            ParticleSpawner* effect = new ParticleSpawner(this->getCreator());
358            effect->setPosition(this->getPosition());
359            effect->setOrientation(this->getOrientation());
360            effect->setDestroyAfterLife(true);
361            effect->setSource("Orxonox/sparks");
362            effect->setLifetime(4.0f);
363        }
364        for (unsigned int i = 0; i < this->numexplosionchunks_; ++i)
365        {
366            ExplosionChunk* chunk = new ExplosionChunk(this->getCreator());
367            chunk->setPosition(this->getPosition());
368        }
369    }
370
371    void Pawn::fired(unsigned int firemode)
372    {
373        if (this->weaponSystem_)
374            this->weaponSystem_->fire(firemode);
375    }
376
377    void Pawn::reload()
378    {
379        this->bReload_ = true;
380    }
381
382    void Pawn::postSpawn()
383    {
384        this->setHealth(this->initialHealth_);
385        if (GameMode::isMaster())
386            this->spawneffect();
387    }
388
389    /* WeaponSystem:
390    *   functions load Slot, Set, Pack from XML and make sure all parent-pointers are set.
391    *   with setWeaponPack you can not just load a Pack from XML but if a Pack already exists anywhere, you can attach it.
392    *       --> e.g. Pickup-Items
393    */
394    void Pawn::addWeaponSlot(WeaponSlot * wSlot)
395    {
396        this->attach(wSlot);
397        if (this->weaponSystem_)
398            this->weaponSystem_->addWeaponSlot(wSlot);
399    }
400
401    WeaponSlot * Pawn::getWeaponSlot(unsigned int index) const
402    {
403        if (this->weaponSystem_)
404            return this->weaponSystem_->getWeaponSlot(index);
405        else
406            return 0;
407    }
408
409    void Pawn::addWeaponSet(WeaponSet * wSet)
410    {
411        if (this->weaponSystem_)
412            this->weaponSystem_->addWeaponSet(wSet);
413    }
414
415    WeaponSet * Pawn::getWeaponSet(unsigned int index) const
416    {
417        if (this->weaponSystem_)
418            return this->weaponSystem_->getWeaponSet(index);
419        else
420            return 0;
421    }
422
423    void Pawn::addWeaponPack(WeaponPack * wPack)
424    {
425        if (this->weaponSystem_)
426        {
427            this->weaponSystem_->addWeaponPack(wPack);
428            this->addedWeaponPack(wPack);
429        }
430    }
431
432    void Pawn::addWeaponPackXML(WeaponPack * wPack)
433    {
434        if (this->weaponSystem_)
435        {
436            if (!this->weaponSystem_->addWeaponPack(wPack))
437                wPack->destroy();
438            else
439                this->addedWeaponPack(wPack);
440        }
441    }
442
443    WeaponPack * Pawn::getWeaponPack(unsigned int index) const
444    {
445        if (this->weaponSystem_)
446            return this->weaponSystem_->getWeaponPack(index);
447        else
448            return 0;
449    }
450
451    //Tell the Map (RadarViewable), if this is a playership
452    void Pawn::startLocalHumanControl()
453    {
454//        SUPER(ControllableEntity, changedPlayer());
455        ControllableEntity::startLocalHumanControl();
456        this->isHumanShip_ = true;
457    }
458}
Note: See TracBrowser for help on using the repository browser.