Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7501 was 7495, checked in by scheusso, 14 years ago

buffering incoming function calls for non-existing objects works now

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