Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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