Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

buffering incoming function calls for non-existing objects works now

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