Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Forgot to initialize value.

  • Property svn:eol-style set to native
File size: 21.1 KB
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    }
208
209    void ControllableEntity::mouseLook()
210    {
211        this->bMouseLook_ = !this->bMouseLook_;
212
213        if (!this->bMouseLook_)
214            this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
215        if (this->getCamera())
216        {
217            if (!this->bMouseLook_&& this->currentCameraPosition_->getDrag())
218                this->getCamera()->setDrag(true);
219            else
220                this->getCamera()->setDrag(false);
221        }
222    }
223
224    void ControllableEntity::rotateYaw(const Vector2& value)
225    {
226        if (this->bMouseLook_)
227            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
228    }
229
230    void ControllableEntity::rotatePitch(const Vector2& value)
231    {
232        if (this->bMouseLook_)
233            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
234    }
235
236    void ControllableEntity::rotateRoll(const Vector2& value)
237    {
238        if (this->bMouseLook_)
239            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
240    }
241
242    void ControllableEntity::fire(unsigned int firemode)
243    {
244        if(GameMode::isMaster())
245        {
246            this->fired(firemode);
247        }
248        else
249        {
250            callMemberNetworkFunction(ControllableEntity, fire, this->getObjectID(), 0, firemode);
251        }
252    }
253
254    void ControllableEntity::setTarget( WorldEntity* target )
255    {
256        this->target_ = target;
257        if ( !GameMode::isMaster() )
258        {
259            if ( target != 0 )
260            {
261                callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, target->getObjectID() );
262            }
263           else
264           {
265                callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN );
266           }
267        }
268    }
269
270    void ControllableEntity::setTargetInternal( uint32_t targetID )
271    {
272        this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) );
273    }
274
275    void ControllableEntity::setPlayer(PlayerInfo* player)
276    {
277        if (!player)
278        {
279            this->removePlayer();
280            return;
281        }
282
283        this->player_ = player;
284        this->formerPlayer_ = player;
285        this->playerID_ = player->getObjectID();
286        this->bHasLocalController_ = player->isLocalPlayer();
287        this->bHasHumanController_ = player->isHumanPlayer();
288
289        if (this->bHasLocalController_ && this->bHasHumanController_)
290        {
291            this->startLocalHumanControl();
292
293            if (!GameMode::isMaster())
294            {
295                this->client_overwrite_ = this->server_overwrite_;
296                this->setSyncMode(ObjectDirection::Bidirectional);
297            }
298        }
299
300        this->changedPlayer();
301    }
302
303    void ControllableEntity::removePlayer()
304    {
305        if (this->bHasLocalController_ && this->bHasHumanController_)
306            this->stopLocalHumanControl();
307
308        this->player_ = 0;
309        this->playerID_ = OBJECTID_UNKNOWN;
310        this->bHasLocalController_ = false;
311        this->bHasHumanController_ = false;
312        this->setSyncMode(ObjectDirection::ToClient);
313
314        this->changedPlayer();
315
316        if (this->bDestroyWhenPlayerLeft_)
317            this->destroy();
318    }
319
320    void ControllableEntity::networkcallback_changedplayerID()
321    {
322        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
323        if (this->playerID_ != OBJECTID_UNKNOWN)
324        {
325            this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
326            if (this->player_ && (this->player_->getControllableEntity() != this))
327                this->player_->startControl(this);
328        }
329    }
330
331    void ControllableEntity::startLocalHumanControl()
332    {
333        if (!this->camera_ && GameMode::showsGraphics())
334        {
335            this->camera_ = new Camera(this);
336            this->camera_->requestFocus();
337            if (!this->cameraPositionTemplate_.empty())
338                this->addTemplate(this->cameraPositionTemplate_);
339            if (this->cameraPositions_.size() > 0)
340            {
341                this->cameraPositions_.front()->attachCamera(this->camera_);
342                this->currentCameraPosition_ = this->cameraPositions_.front();
343            }
344            else
345            {
346                this->camera_->attachToNode(this->cameraPositionRootNode_);
347                this->currentCameraPosition_ = 0;
348            }
349        }
350
351        if (!this->hud_ && GameMode::showsGraphics())
352        {
353            if (!this->hudtemplate_.empty())
354            {
355                this->hud_ = new OverlayGroup(this);
356                this->hud_->addTemplate(this->hudtemplate_);
357                this->hud_->setOwner(this);
358            }
359        }
360    }
361
362    void ControllableEntity::stopLocalHumanControl()
363    {
364        if (this->camera_)
365        {
366            this->camera_->detachFromParent();
367            this->camera_->destroy();
368            this->camera_ = 0;
369        }
370
371        if (this->hud_)
372        {
373            this->hud_->destroy();
374            this->hud_ = 0;
375        }
376    }
377
378    void ControllableEntity::setXMLController(Controller* controller)
379    {
380        if (!this->xmlcontroller_)
381        {
382            this->xmlcontroller_ = controller;
383            this->bHasLocalController_ = true;
384            this->xmlcontroller_->setControllableEntity(this);
385        }
386        else
387            COUT(2) << "Warning: ControllableEntity \"" << this->getName() << "\" already has a Controller." << std::endl;
388    }
389
390    void ControllableEntity::parentChanged()
391    {
392        WorldEntity::parentChanged();
393
394        WorldEntity* parent = this->getParent();
395        if (parent)
396        {
397            for (std::list<SmartPtr<CameraPosition> >::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
398                if ((*it)->getIsAbsolute())
399                    parent->attach((*it));
400        }
401    }
402
403    void ControllableEntity::tick(float dt)
404    {
405        MobileEntity::tick(dt);
406
407        if (this->isActive())
408        {
409            // Check whether Bullet doesn't do the physics for us
410            if (!this->isDynamic())
411            {
412                if (GameMode::isMaster())
413                {
414                    this->server_position_ = this->getPosition();
415                    this->server_orientation_ = this->getOrientation();
416                    this->server_linear_velocity_ = this->getVelocity();
417                    this->server_angular_velocity_ = this->getAngularVelocity();
418                }
419                else if (this->bHasLocalController_)
420                {
421                    this->client_position_ = this->getPosition();
422                    this->client_orientation_ = this->getOrientation();
423                    this->client_linear_velocity_ = this->getVelocity();
424                    this->client_angular_velocity_ = this->getAngularVelocity();
425                }
426            }
427        }
428    }
429
430    void ControllableEntity::registerVariables()
431    {
432        registerVariable(this->cameraPositionTemplate_,  VariableDirection::ToClient);
433        registerVariable(this->hudtemplate_,             VariableDirection::ToClient);
434
435        registerVariable(this->server_position_,         VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
436        registerVariable(this->server_linear_velocity_,  VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
437        registerVariable(this->server_orientation_,      VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
438        registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
439
440        registerVariable(this->server_overwrite_,        VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
441        registerVariable(this->client_overwrite_,        VariableDirection::ToServer);
442
443        registerVariable(this->client_position_,         VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
444        registerVariable(this->client_linear_velocity_,  VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
445        registerVariable(this->client_orientation_,      VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
446        registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
447
448
449        registerVariable(this->playerID_,                VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
450    }
451
452    void ControllableEntity::processServerPosition()
453    {
454        if (!this->bHasLocalController_)
455            MobileEntity::setPosition(this->server_position_);
456    }
457
458    void ControllableEntity::processServerLinearVelocity()
459    {
460        if (!this->bHasLocalController_)
461            MobileEntity::setVelocity(this->server_linear_velocity_);
462    }
463
464    void ControllableEntity::processServerOrientation()
465    {
466        if (!this->bHasLocalController_)
467            MobileEntity::setOrientation(this->server_orientation_);
468    }
469
470    void ControllableEntity::processServerAngularVelocity()
471    {
472        if (!this->bHasLocalController_)
473            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
474    }
475
476    void ControllableEntity::processOverwrite()
477    {
478        if (this->bHasLocalController_)
479        {
480            this->setPosition(this->server_position_);
481            this->setOrientation(this->server_orientation_);
482            this->setVelocity(this->server_linear_velocity_);
483            this->setAngularVelocity(this->server_angular_velocity_);
484
485            this->client_overwrite_ = this->server_overwrite_;
486        }
487    }
488
489    void ControllableEntity::processClientPosition()
490    {
491        if (this->server_overwrite_ == this->client_overwrite_)
492        {
493            MobileEntity::setPosition(this->client_position_);
494            this->server_position_ = this->getPosition();
495        }
496    }
497
498    void ControllableEntity::processClientLinearVelocity()
499    {
500        if (this->server_overwrite_ == this->client_overwrite_)
501        {
502            MobileEntity::setVelocity(this->client_linear_velocity_);
503            this->server_linear_velocity_ = this->getVelocity();
504        }
505    }
506
507    void ControllableEntity::processClientOrientation()
508    {
509        if (this->server_overwrite_ == this->client_overwrite_)
510        {
511            MobileEntity::setOrientation(this->client_orientation_);
512            this->server_orientation_ = this->getOrientation();
513        }
514    }
515
516    void ControllableEntity::processClientAngularVelocity()
517    {
518        if (this->server_overwrite_ == this->client_overwrite_)
519        {
520            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
521            this->server_angular_velocity_ = this->getAngularVelocity();
522        }
523    }
524
525    void ControllableEntity::setPosition(const Vector3& position)
526    {
527        if (GameMode::isMaster())
528        {
529            MobileEntity::setPosition(position);
530            this->server_position_ = this->getPosition();
531            ++this->server_overwrite_;
532        }
533        else if (this->bHasLocalController_)
534        {
535            MobileEntity::setPosition(position);
536            this->client_position_ = this->getPosition();
537        }
538    }
539
540    void ControllableEntity::setOrientation(const Quaternion& orientation)
541    {
542        if (GameMode::isMaster())
543        {
544            MobileEntity::setOrientation(orientation);
545            this->server_orientation_ = this->getOrientation();
546            ++this->server_overwrite_;
547        }
548        else if (this->bHasLocalController_)
549        {
550            MobileEntity::setOrientation(orientation);
551            this->client_orientation_ = this->getOrientation();
552        }
553    }
554
555    void ControllableEntity::setVelocity(const Vector3& velocity)
556    {
557        if (GameMode::isMaster())
558        {
559            MobileEntity::setVelocity(velocity);
560            this->server_linear_velocity_ = this->getVelocity();
561            ++this->server_overwrite_;
562        }
563        else if (this->bHasLocalController_)
564        {
565            MobileEntity::setVelocity(velocity);
566            this->client_linear_velocity_ = this->getVelocity();
567        }
568    }
569
570    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
571    {
572        if (GameMode::isMaster())
573        {
574            MobileEntity::setAngularVelocity(velocity);
575            this->server_angular_velocity_ = this->getAngularVelocity();
576            ++this->server_overwrite_;
577        }
578        else if (this->bHasLocalController_)
579        {
580            MobileEntity::setAngularVelocity(velocity);
581            this->client_angular_velocity_ = this->getAngularVelocity();
582        }
583    }
584
585    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
586    {
587        MobileEntity::setWorldTransform(worldTrans);
588        if (GameMode::isMaster())
589        {
590            this->server_position_ = this->getPosition();
591            this->server_orientation_ = this->getOrientation();
592            this->server_linear_velocity_ = this->getVelocity();
593            this->server_angular_velocity_ = this->getAngularVelocity();
594        }
595        else if (this->bHasLocalController_)
596        {
597            this->client_position_ = this->getPosition();
598            this->client_orientation_ = this->getOrientation();
599            this->client_linear_velocity_ = this->getVelocity();
600            this->client_angular_velocity_ = this->getAngularVelocity();
601        }
602    }
603}
Note: See TracBrowser for help on using the repository browser.