Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound6/src/orxonox/worldentities/ControllableEntity.cc @ 8004

Last change on this file since 8004 was 7892, checked in by landauf, 14 years ago

fixed bug in bug fix

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