Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7648 was 7534, checked in by dafrick, 14 years ago

Forgot to initialize value.

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