Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/ControllableEntity.cc @ 2917

Last change on this file since 2917 was 2896, checked in by landauf, 16 years ago

Merged gui branch back to trunk.

I did 2 small changes in IngameManager.cc on line 777 and 888 (yes, really), because const_reverse_iterator strangely doesn't work on MinGW.

  • Property svn:eol-style set to native
File size: 19.2 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:
[2662]25 *      Reto Grieder
[2072]26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "ControllableEntity.h"
31
[2662]32#include <OgreSceneManager.h>
33
[2072]34#include "core/CoreIncludes.h"
[2662]35#include "core/ConfigValueIncludes.h"
[2896]36#include "core/GameMode.h"
[2072]37#include "core/XMLPort.h"
38#include "core/Template.h"
39
[2662]40#include "objects/Scene.h"
[2072]41#include "objects/infos/PlayerInfo.h"
42#include "objects/worldentities/Camera.h"
43#include "objects/worldentities/CameraPosition.h"
[2662]44#include "objects/gametypes/Gametype.h"
[2072]45#include "overlays/OverlayGroup.h"
46
47namespace orxonox
48{
49    CreateFactory(ControllableEntity);
50
[2662]51    ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator)
[2072]52    {
53        RegisterObject(ControllableEntity);
54
[2662]55        this->bHasLocalController_ = false;
56        this->bHasHumanController_ = false;
57
[2072]58        this->server_overwrite_ = 0;
59        this->client_overwrite_ = 0;
60        this->player_ = 0;
[2171]61        this->playerID_ = OBJECTID_UNKNOWN;
[2072]62        this->hud_ = 0;
63        this->camera_ = 0;
64        this->bDestroyWhenPlayerLeft_ = false;
[2662]65        this->cameraPositionRootNode_ = this->node_->createChildSceneNode();
66        this->bMouseLook_ = false;
67        this->mouseLookSpeed_ = 200;
[2072]68
[2662]69        this->gtinfo_ = 0;
70        this->gtinfoID_ = OBJECTID_UNKNOWN;
71        this->changedGametype();
[2072]72
[2662]73        this->server_position_         = Vector3::ZERO;
74        this->client_position_         = Vector3::ZERO;
75        this->server_linear_velocity_  = Vector3::ZERO;
76        this->client_linear_velocity_  = Vector3::ZERO;
77        this->server_orientation_      = Quaternion::IDENTITY;
78        this->client_orientation_      = Quaternion::IDENTITY;
79        this->server_angular_velocity_ = Vector3::ZERO;
80        this->client_angular_velocity_ = Vector3::ZERO;
[2072]81
[2662]82
83        this->setConfigValues();
84        this->setPriority( priority::very_high );
[2072]85        this->registerVariables();
86    }
87
88    ControllableEntity::~ControllableEntity()
89    {
90        if (this->isInitialized())
91        {
[2662]92            this->bDestroyWhenPlayerLeft_ = false;
[2072]93
[2662]94            if (this->bHasLocalController_ && this->bHasHumanController_)
95                this->stopLocalHumanControl();
96
97            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
98                this->getPlayer()->stopControl(this, false);
99
[2072]100            if (this->hud_)
101                delete this->hud_;
102
103            if (this->camera_)
104                delete this->camera_;
105
[2662]106            for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
107                delete (*it);
108
109            if (this->getScene()->getSceneManager())
110                this->getScene()->getSceneManager()->destroySceneNode(this->cameraPositionRootNode_->getName());
[2072]111        }
112    }
113
114    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
115    {
116        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
117
118        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
119        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
120
121        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
122    }
123
[2662]124    void ControllableEntity::setConfigValues()
125    {
126        SetConfigValue(mouseLookSpeed_, 3.0f);
127    }
128
129    void ControllableEntity::changedGametype()
130    {
131        //SUPER(ControllableEntity, changedGametype);
132        WorldEntity::changedGametype();
133
134        this->gtinfo_ = 0;
135        this->gtinfoID_ = OBJECTID_UNKNOWN;
136
137        if (this->getGametype() && this->getGametype()->getGametypeInfo())
138        {
139            this->gtinfo_ = this->getGametype()->getGametypeInfo();
140            this->gtinfoID_ = this->gtinfo_->getObjectID();
141        }
142    }
143
[2072]144    void ControllableEntity::addCameraPosition(CameraPosition* position)
145    {
[2826]146        if (!position->getIsAbsolute())
147        {
148            if (position->getAllowMouseLook())
149                position->attachToNode(this->cameraPositionRootNode_);
150            else
151                this->attach(position);
152        }
[2662]153        else
[2826]154        {
155            WorldEntity* parent = this->getParent();
156            if (parent)
157                parent->attach(position);
158        }
[2072]159        this->cameraPositions_.push_back(position);
160    }
161
162    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
163    {
164        unsigned int i = 0;
165        for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
166        {
167            if (i == index)
168                return (*it);
169            ++i;
170        }
171        return 0;
172    }
173
174    void ControllableEntity::switchCamera()
175    {
176        if (this->camera_)
177        {
178            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
179            {
180                this->cameraPositions_.front()->attachCamera(this->camera_);
181            }
182            else if (this->cameraPositions_.size() > 0)
183            {
184                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
185                {
186                    if ((*it) == this->camera_->getParent())
187                    {
188                        ++it;
189                        if (it != this->cameraPositions_.end())
190                            (*it)->attachCamera(this->camera_);
191                        else
192                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
193                        break;
194                    }
195                }
196            }
197            else
198            {
[2662]199                this->camera_->attachToNode(this->cameraPositionRootNode_);
[2072]200            }
201        }
202    }
203
[2662]204    void ControllableEntity::mouseLook()
205    {
206        this->bMouseLook_ = !this->bMouseLook_;
207
208        if (!this->bMouseLook_)
209            this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
210    }
211
212    void ControllableEntity::rotateYaw(const Vector2& value)
213    {
214        if (this->bMouseLook_)
215            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
216    }
217
218    void ControllableEntity::rotatePitch(const Vector2& value)
219    {
220        if (this->bMouseLook_)
221            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
222    }
223
224    void ControllableEntity::rotateRoll(const Vector2& value)
225    {
226        if (this->bMouseLook_)
227            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
228    }
229
[2072]230    void ControllableEntity::setPlayer(PlayerInfo* player)
231    {
232        if (!player)
233        {
234            this->removePlayer();
235            return;
236        }
237
238        this->player_ = player;
239        this->playerID_ = player->getObjectID();
[2662]240        this->bHasLocalController_ = player->isLocalPlayer();
241        this->bHasHumanController_ = player->isHumanPlayer();
242
243        if (this->bHasLocalController_ && this->bHasHumanController_)
[2072]244        {
[2662]245            this->startLocalHumanControl();
[2072]246
[2896]247            if (!GameMode::isMaster())
[2072]248            {
249                this->client_overwrite_ = this->server_overwrite_;
[2662]250                this->setObjectMode(objectDirection::bidirectional);
[2072]251            }
252        }
[2839]253
254        this->changedPlayer();
[2072]255    }
256
257    void ControllableEntity::removePlayer()
258    {
[2662]259        if (this->bHasLocalController_ && this->bHasHumanController_)
260            this->stopLocalHumanControl();
[2072]261
262        this->player_ = 0;
[2171]263        this->playerID_ = OBJECTID_UNKNOWN;
[2662]264        this->bHasLocalController_ = false;
265        this->bHasHumanController_ = false;
266        this->setObjectMode(objectDirection::toclient);
[2072]267
[2839]268        this->changedPlayer();
269
[2072]270        if (this->bDestroyWhenPlayerLeft_)
271            delete this;
272    }
273
274    void ControllableEntity::networkcallback_changedplayerID()
275    {
276        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
[2171]277        if (this->playerID_ != OBJECTID_UNKNOWN)
[2072]278        {
[2171]279            this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
[2072]280            if (this->player_ && (this->player_->getControllableEntity() != this))
281                this->player_->startControl(this);
282        }
283    }
284
[2662]285    void ControllableEntity::networkcallback_changedgtinfoID()
[2072]286    {
[2662]287        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
288        {
289            this->gtinfo_ = dynamic_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
[2072]290
[2662]291            if (!this->gtinfo_)
292                this->gtinfoID_ = OBJECTID_UNKNOWN;
293        }
294    }
295
296    void ControllableEntity::startLocalHumanControl()
297    {
298        if (!this->camera_)
[2072]299        {
[2662]300            this->camera_ = new Camera(this);
301            this->camera_->requestFocus();
302            if (this->cameraPositionTemplate_ != "")
303                this->addTemplate(this->cameraPositionTemplate_);
304            if (this->cameraPositions_.size() > 0)
305                this->cameraPositions_.front()->attachCamera(this->camera_);
306            else
307                this->camera_->attachToNode(this->cameraPositionRootNode_);
[2072]308        }
[2662]309
310        if (!this->hud_)
311        {
312            if (this->hudtemplate_ != "")
313            {
314                this->hud_ = new OverlayGroup(this);
315                this->hud_->addTemplate(this->hudtemplate_);
316                this->hud_->setOwner(this);
317            }
318        }
[2072]319    }
320
[2662]321    void ControllableEntity::stopLocalHumanControl()
[2072]322    {
[2662]323        if (this->camera_)
324        {
325            this->camera_->detachFromParent();
326            delete this->camera_;
327            this->camera_ = 0;
328        }
[2072]329
[2662]330        if (this->hud_)
331        {
332            delete this->hud_;
333            this->hud_ = 0;
334        }
[2072]335    }
336
[2851]337    void ControllableEntity::parentChanged()
338    {
339        WorldEntity::parentChanged();
340
341        WorldEntity* parent = this->getParent();
342        if (parent)
343        {
344            for (std::list<CameraPosition*>::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
345                if ((*it)->getIsAbsolute())
346                    parent->attach((*it));
347        }
348    }
349
[2072]350    void ControllableEntity::tick(float dt)
351    {
[2662]352        MobileEntity::tick(dt);
353
[2072]354        if (this->isActive())
355        {
[2662]356            // Check whether Bullet doesn't do the physics for us
357            if (!this->isDynamic())
[2072]358            {
[2896]359                if (GameMode::isMaster())
[2662]360                {
361                    this->server_position_ = this->getPosition();
362                    this->server_orientation_ = this->getOrientation();
363                    this->server_linear_velocity_ = this->getVelocity();
364                    this->server_angular_velocity_ = this->getAngularVelocity();
365                }
366                else if (this->bHasLocalController_)
367                {
368                    this->client_position_ = this->getPosition();
369                    this->client_orientation_ = this->getOrientation();
370                    this->client_linear_velocity_ = this->getVelocity();
371                    this->client_angular_velocity_ = this->getAngularVelocity();
372                }
[2072]373            }
374        }
375    }
376
377    void ControllableEntity::registerVariables()
378    {
[2662]379        registerVariable(this->cameraPositionTemplate_,  variableDirection::toclient);
380        registerVariable(this->hudtemplate_,             variableDirection::toclient);
[2072]381
[2662]382        registerVariable(this->server_position_,         variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
383        registerVariable(this->server_linear_velocity_,  variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
384        registerVariable(this->server_orientation_,      variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
385        registerVariable(this->server_angular_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
[2072]386
[2662]387        registerVariable(this->server_overwrite_,        variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
388        registerVariable(this->client_overwrite_,        variableDirection::toserver);
[2072]389
[2662]390        registerVariable(this->client_position_,         variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
391        registerVariable(this->client_linear_velocity_,  variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
392        registerVariable(this->client_orientation_,      variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
393        registerVariable(this->client_angular_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
[2072]394
[2662]395        registerVariable(this->playerID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
396        registerVariable(this->gtinfoID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedgtinfoID));
[2072]397    }
398
399    void ControllableEntity::processServerPosition()
400    {
[2662]401        if (!this->bHasLocalController_)
402            MobileEntity::setPosition(this->server_position_);
[2072]403    }
404
[2662]405    void ControllableEntity::processServerLinearVelocity()
[2072]406    {
[2662]407        if (!this->bHasLocalController_)
408            MobileEntity::setVelocity(this->server_linear_velocity_);
[2072]409    }
410
411    void ControllableEntity::processServerOrientation()
412    {
[2662]413        if (!this->bHasLocalController_)
414            MobileEntity::setOrientation(this->server_orientation_);
[2072]415    }
416
[2662]417    void ControllableEntity::processServerAngularVelocity()
418    {
419        if (!this->bHasLocalController_)
420            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
421    }
422
[2072]423    void ControllableEntity::processOverwrite()
424    {
[2662]425        if (this->bHasLocalController_)
[2072]426        {
427            this->setPosition(this->server_position_);
428            this->setOrientation(this->server_orientation_);
[2662]429            this->setVelocity(this->server_linear_velocity_);
430            this->setAngularVelocity(this->server_angular_velocity_);
[2072]431
432            this->client_overwrite_ = this->server_overwrite_;
433        }
434    }
435
436    void ControllableEntity::processClientPosition()
437    {
438        if (this->server_overwrite_ == this->client_overwrite_)
439        {
[2662]440            MobileEntity::setPosition(this->client_position_);
441            this->server_position_ = this->getPosition();
[2072]442        }
443    }
444
[2662]445    void ControllableEntity::processClientLinearVelocity()
[2072]446    {
447        if (this->server_overwrite_ == this->client_overwrite_)
448        {
[2662]449            MobileEntity::setVelocity(this->client_linear_velocity_);
450            this->server_linear_velocity_ = this->getVelocity();
[2072]451        }
452    }
453
454    void ControllableEntity::processClientOrientation()
455    {
456        if (this->server_overwrite_ == this->client_overwrite_)
457        {
[2662]458            MobileEntity::setOrientation(this->client_orientation_);
459            this->server_orientation_ = this->getOrientation();
[2072]460        }
461    }
462
[2662]463    void ControllableEntity::processClientAngularVelocity()
[2072]464    {
[2662]465        if (this->server_overwrite_ == this->client_overwrite_)
[2072]466        {
[2662]467            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
468            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]469        }
470    }
471
[2662]472    void ControllableEntity::setPosition(const Vector3& position)
[2072]473    {
[2896]474        if (GameMode::isMaster())
[2072]475        {
[2662]476            MobileEntity::setPosition(position);
477            this->server_position_ = this->getPosition();
[2072]478            ++this->server_overwrite_;
479        }
[2662]480        else if (this->bHasLocalController_)
[2072]481        {
[2662]482            MobileEntity::setPosition(position);
483            this->client_position_ = this->getPosition();
[2072]484        }
485    }
486
487    void ControllableEntity::setOrientation(const Quaternion& orientation)
488    {
[2896]489        if (GameMode::isMaster())
[2072]490        {
[2662]491            MobileEntity::setOrientation(orientation);
492            this->server_orientation_ = this->getOrientation();
[2072]493            ++this->server_overwrite_;
494        }
[2662]495        else if (this->bHasLocalController_)
[2072]496        {
[2662]497            MobileEntity::setOrientation(orientation);
498            this->client_orientation_ = this->getOrientation();
[2072]499        }
500    }
501
[2662]502    void ControllableEntity::setVelocity(const Vector3& velocity)
[2072]503    {
[2896]504        if (GameMode::isMaster())
[2072]505        {
[2662]506            MobileEntity::setVelocity(velocity);
507            this->server_linear_velocity_ = this->getVelocity();
[2072]508            ++this->server_overwrite_;
509        }
[2662]510        else if (this->bHasLocalController_)
[2072]511        {
[2662]512            MobileEntity::setVelocity(velocity);
513            this->client_linear_velocity_ = this->getVelocity();
[2072]514        }
515    }
516
[2662]517    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
[2072]518    {
[2896]519        if (GameMode::isMaster())
[2072]520        {
[2662]521            MobileEntity::setAngularVelocity(velocity);
522            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]523            ++this->server_overwrite_;
524        }
[2662]525        else if (this->bHasLocalController_)
[2072]526        {
[2662]527            MobileEntity::setAngularVelocity(velocity);
528            this->client_angular_velocity_ = this->getAngularVelocity();
[2072]529        }
530    }
531
[2662]532    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
[2072]533    {
[2662]534        MobileEntity::setWorldTransform(worldTrans);
[2896]535        if (GameMode::isMaster())
[2072]536        {
[2662]537            this->server_position_ = this->getPosition();
538            this->server_orientation_ = this->getOrientation();
539            this->server_linear_velocity_ = this->getVelocity();
540            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]541        }
[2662]542        else if (this->bHasLocalController_)
[2072]543        {
[2662]544            this->client_position_ = this->getPosition();
545            this->client_orientation_ = this->getOrientation();
546            this->client_linear_velocity_ = this->getVelocity();
547            this->client_angular_velocity_ = this->getAngularVelocity();
[2072]548        }
549    }
550}
Note: See TracBrowser for help on using the repository browser.