Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/worldentities/ControllableEntity.cc @ 8580

Last change on this file since 8580 was 8580, checked in by dafrick, 13 years ago

Merging game immersion branch into presentation branch.

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