Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp2/src/orxonox/objects/worldentities/ControllableEntity.cc @ 3050

Last change on this file since 3050 was 2976, checked in by scheusso, 16 years ago

removed some debug output

  • Property svn:eol-style set to native
File size: 19.7 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 "OrxonoxStableHeaders.h"
30#include "ControllableEntity.h"
31
32#include <OgreSceneManager.h>
33
34#include "core/CoreIncludes.h"
35#include "core/ConfigValueIncludes.h"
36#include "core/Core.h"
37#include "core/XMLPort.h"
38#include "core/Template.h"
39
40#include "objects/Scene.h"
41#include "objects/infos/PlayerInfo.h"
42#include "objects/worldentities/Camera.h"
43#include "objects/worldentities/CameraPosition.h"
44#include "objects/gametypes/Gametype.h"
45#include "overlays/OverlayGroup.h"
46
47namespace orxonox
48{
49    CreateFactory(ControllableEntity);
50
51    ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator)
52    {
53        RegisterObject(ControllableEntity);
54
55        this->bHasLocalController_ = false;
56        this->bHasHumanController_ = false;
57
58        this->server_overwrite_ = 0;
59        this->client_overwrite_ = 0;
60        this->player_ = 0;
61        this->playerID_ = OBJECTID_UNKNOWN;
62        this->hud_ = 0;
63        this->camera_ = 0;
64        this->bDestroyWhenPlayerLeft_ = false;
65        this->cameraPositionRootNode_ = this->node_->createChildSceneNode();
66        this->bMouseLook_ = false;
67        this->mouseLookSpeed_ = 200;
68
69        this->gtinfo_ = 0;
70        this->gtinfoID_ = OBJECTID_UNKNOWN;
71        this->changedGametype();
72
73        this->common_position_         = Vector3::ZERO;
74//         this->client_position_         = Vector3::ZERO;
75        this->common_linear_velocity_  = Vector3::ZERO;
76//         this->client_linear_velocity_  = Vector3::ZERO;
77        this->common_orientation_      = Quaternion::IDENTITY;
78//         this->client_orientation_      = Quaternion::IDENTITY;
79        this->common_angular_velocity_ = Vector3::ZERO;
80//         this->client_angular_velocity_ = Vector3::ZERO;
81
82
83        this->setConfigValues();
84        this->setPriority( priority::very_high );
85        this->registerVariables();
86    }
87
88    ControllableEntity::~ControllableEntity()
89    {
90        if (this->isInitialized())
91        {
92            this->bDestroyWhenPlayerLeft_ = false;
93
94            if (this->bHasLocalController_ && this->bHasHumanController_)
95                this->stopLocalHumanControl();
96
97            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
98                this->getPlayer()->stopControl(this, false);
99
100            if (this->hud_)
101                delete this->hud_;
102
103            if (this->camera_)
104                delete this->camera_;
105
106            for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
107                delete (*it);
108
109            if (this->getScene()->getSceneManager())
110                this->getScene()->getSceneManager()->destroySceneNode(this->cameraPositionRootNode_->getName());
111        }
112    }
113
114    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
115    {
116        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
117
118        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
119        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
120
121        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
122    }
123
124    void ControllableEntity::setConfigValues()
125    {
126        SetConfigValue(mouseLookSpeed_, 3.0f);
127    }
128
129    void ControllableEntity::changedGametype()
130    {
131        //SUPER(ControllableEntity, changedGametype);
132        WorldEntity::changedGametype();
133
134        this->gtinfo_ = 0;
135        this->gtinfoID_ = OBJECTID_UNKNOWN;
136
137        if (this->getGametype() && this->getGametype()->getGametypeInfo())
138        {
139            this->gtinfo_ = this->getGametype()->getGametypeInfo();
140            this->gtinfoID_ = this->gtinfo_->getObjectID();
141        }
142    }
143
144    void ControllableEntity::addCameraPosition(CameraPosition* position)
145    {
146        if (!position->getIsAbsolute())
147        {
148            if (position->getAllowMouseLook())
149                position->attachToNode(this->cameraPositionRootNode_);
150            else
151                this->attach(position);
152        }
153        else
154        {
155            WorldEntity* parent = this->getParent();
156            if (parent)
157                parent->attach(position);
158        }
159        this->cameraPositions_.push_back(position);
160    }
161
162    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
163    {
164        unsigned int i = 0;
165        for (std::list<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            }
182            else if (this->cameraPositions_.size() > 0)
183            {
184                for (std::list<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                            (*it)->attachCamera(this->camera_);
191                        else
192                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
193                        break;
194                    }
195                }
196            }
197            else
198            {
199                this->camera_->attachToNode(this->cameraPositionRootNode_);
200            }
201        }
202    }
203
204    void ControllableEntity::mouseLook()
205    {
206        this->bMouseLook_ = !this->bMouseLook_;
207
208        if (!this->bMouseLook_)
209            this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
210    }
211
212    void ControllableEntity::rotateYaw(const Vector2& value)
213    {
214        if (this->bMouseLook_)
215            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
216    }
217
218    void ControllableEntity::rotatePitch(const Vector2& value)
219    {
220        if (this->bMouseLook_)
221            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
222    }
223
224    void ControllableEntity::rotateRoll(const Vector2& value)
225    {
226        if (this->bMouseLook_)
227            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
228    }
229
230    void ControllableEntity::setPlayer(PlayerInfo* player)
231    {
232        if (!player)
233        {
234            this->removePlayer();
235            return;
236        }
237
238        this->player_ = player;
239        this->playerID_ = player->getObjectID();
240        this->bHasLocalController_ = player->isLocalPlayer();
241        this->bHasHumanController_ = player->isHumanPlayer();
242
243        if (this->bHasLocalController_ && this->bHasHumanController_)
244        {
245            this->startLocalHumanControl();
246
247            if (!Core::isMaster())
248            {
249                this->client_overwrite_ = this->server_overwrite_;
250                this->setObjectMode(objectDirection::bidirectional);
251            }
252        }
253    }
254
255    void ControllableEntity::removePlayer()
256    {
257        if (this->bHasLocalController_ && this->bHasHumanController_)
258            this->stopLocalHumanControl();
259
260        this->player_ = 0;
261        this->playerID_ = OBJECTID_UNKNOWN;
262        this->bHasLocalController_ = false;
263        this->bHasHumanController_ = false;
264        this->setObjectMode(objectDirection::toclient);
265
266        if (this->bDestroyWhenPlayerLeft_)
267            delete this;
268    }
269
270    void ControllableEntity::networkcallback_changedplayerID()
271    {
272        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
273        if (this->playerID_ != OBJECTID_UNKNOWN)
274        {
275            this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
276            if (this->player_ && (this->player_->getControllableEntity() != this))
277                this->player_->startControl(this);
278        }
279    }
280
281    void ControllableEntity::networkcallback_changedgtinfoID()
282    {
283        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
284        {
285            this->gtinfo_ = dynamic_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
286
287            if (!this->gtinfo_)
288                this->gtinfoID_ = OBJECTID_UNKNOWN;
289        }
290    }
291
292    void ControllableEntity::startLocalHumanControl()
293    {
294        if (!this->camera_)
295        {
296            this->camera_ = new Camera(this);
297            this->camera_->requestFocus();
298            if (this->cameraPositionTemplate_ != "")
299                this->addTemplate(this->cameraPositionTemplate_);
300            if (this->cameraPositions_.size() > 0)
301                this->cameraPositions_.front()->attachCamera(this->camera_);
302            else
303                this->camera_->attachToNode(this->cameraPositionRootNode_);
304        }
305
306        if (!this->hud_)
307        {
308            if (this->hudtemplate_ != "")
309            {
310                this->hud_ = new OverlayGroup(this);
311                this->hud_->addTemplate(this->hudtemplate_);
312                this->hud_->setOwner(this);
313            }
314        }
315    }
316
317    void ControllableEntity::stopLocalHumanControl()
318    {
319        if (this->camera_)
320        {
321            this->camera_->detachFromParent();
322            delete this->camera_;
323            this->camera_ = 0;
324        }
325
326        if (this->hud_)
327        {
328            delete this->hud_;
329            this->hud_ = 0;
330        }
331    }
332
333    void ControllableEntity::tick(float dt)
334    {
335        MobileEntity::tick(dt);
336
337        if (this->isActive())
338        {
339            // Check whether Bullet doesn't do the physics for us
340            if (!this->isDynamic())
341            {
342//                 if (Core::isMaster())
343//                 {
344                    this->common_position_ = this->getPosition();
345                    this->common_orientation_ = this->getOrientation();
346                    this->common_linear_velocity_ = this->getVelocity();
347                    this->common_angular_velocity_ = this->getAngularVelocity();
348//                 }
349//                 else if (this->bHasLocalController_)
350//                 {
351//                     this->client_position_ = this->getPosition();
352//                     this->client_orientation_ = this->getOrientation();
353//                     this->client_linear_velocity_ = this->getVelocity();
354//                     this->client_angular_velocity_ = this->getAngularVelocity();
355//                 }
356            }
357        }
358    }
359
360    void ControllableEntity::registerVariables()
361    {
362        registerVariable(this->cameraPositionTemplate_,  variableDirection::toclient);
363        registerVariable(this->hudtemplate_,             variableDirection::toclient);
364
365//         registerVariable(this->server_position_,         variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
366//         registerVariable(this->server_linear_velocity_,  variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
367//         registerVariable(this->server_orientation_,      variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
368//         registerVariable(this->server_angular_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
369//
370//         registerVariable(this->server_overwrite_,        variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
371//         registerVariable(this->client_overwrite_,        variableDirection::toserver);
372//
373//         registerVariable(this->client_position_,         variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
374//         registerVariable(this->client_linear_velocity_,  variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
375//         registerVariable(this->client_orientation_,      variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
376//         registerVariable(this->client_angular_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
377       
378        registerVariable(this->common_position_,         variableDirection::serverMaster, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition), true);
379        registerVariable(this->common_linear_velocity_,  variableDirection::serverMaster, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity), true);
380        registerVariable(this->common_orientation_,      variableDirection::serverMaster, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation), true);
381        registerVariable(this->common_angular_velocity_, variableDirection::serverMaster, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity), true);
382
383        registerVariable(this->playerID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
384        registerVariable(this->gtinfoID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedgtinfoID));
385    }
386
387    void ControllableEntity::processServerPosition()
388    {
389        if (!this->bHasLocalController_)
390            MobileEntity::setPosition(this->common_position_);
391    }
392
393    void ControllableEntity::processServerLinearVelocity()
394    {
395        if (!this->bHasLocalController_)
396            MobileEntity::setVelocity(this->common_linear_velocity_);
397    }
398
399    void ControllableEntity::processServerOrientation()
400    {
401        if (!this->bHasLocalController_)
402            MobileEntity::setOrientation(this->common_orientation_);
403    }
404
405    void ControllableEntity::processServerAngularVelocity()
406    {
407        if (!this->bHasLocalController_)
408            MobileEntity::setAngularVelocity(this->common_angular_velocity_);
409    }
410
411    void ControllableEntity::processOverwrite()
412    {
413        if (this->bHasLocalController_)
414        {
415//             this->setPosition(this->server_position_);
416//             this->setOrientation(this->server_orientation_);
417//             this->setVelocity(this->server_linear_velocity_);
418//             this->setAngularVelocity(this->server_angular_velocity_);
419
420//             this->client_overwrite_ = this->server_overwrite_;
421        }
422    }
423
424    void ControllableEntity::processClientPosition()
425    {
426        if (this->server_overwrite_ == this->client_overwrite_)
427        {
428//             MobileEntity::setPosition(this->client_position_);
429//             this->server_position_ = this->getPosition();
430        }
431    }
432
433    void ControllableEntity::processClientLinearVelocity()
434    {
435        if (this->server_overwrite_ == this->client_overwrite_)
436        {
437//             MobileEntity::setVelocity(this->client_linear_velocity_);
438//             this->server_linear_velocity_ = this->getVelocity();
439        }
440    }
441
442    void ControllableEntity::processClientOrientation()
443    {
444        if (this->server_overwrite_ == this->client_overwrite_)
445        {
446//             MobileEntity::setOrientation(this->client_orientation_);
447//             this->server_orientation_ = this->getOrientation();
448        }
449    }
450
451    void ControllableEntity::processClientAngularVelocity()
452    {
453        if (this->server_overwrite_ == this->client_overwrite_)
454        {
455//             MobileEntity::setAngularVelocity(this->client_angular_velocity_);
456//             this->server_angular_velocity_ = this->getAngularVelocity();
457        }
458    }
459
460    void ControllableEntity::setPosition(const Vector3& position)
461    {
462//         if (Core::isMaster())
463//         {
464            MobileEntity::setPosition(position);
465            this->common_position_ = this->getPosition();
466//             ++this->server_overwrite_;
467//         }
468//         else if (this->bHasLocalController_)
469//         {
470//             MobileEntity::setPosition(position);
471//             this->client_position_ = this->getPosition();
472//         }
473    }
474
475    void ControllableEntity::setOrientation(const Quaternion& orientation)
476    {
477//         if (Core::isMaster())
478//         {
479            MobileEntity::setOrientation(orientation);
480            this->common_orientation_ = this->getOrientation();
481//             ++this->server_overwrite_;
482//         }
483//         else if (this->bHasLocalController_)
484//         {
485//             MobileEntity::setOrientation(orientation);
486//             this->client_orientation_ = this->getOrientation();
487//         }
488    }
489
490    void ControllableEntity::setVelocity(const Vector3& velocity)
491    {
492//         if (Core::isMaster())
493//         {
494            MobileEntity::setVelocity(velocity);
495            this->common_linear_velocity_ = this->getVelocity();
496//             ++this->server_overwrite_;
497//         }
498//         else if (this->bHasLocalController_)
499//         {
500//             MobileEntity::setVelocity(velocity);
501//             this->client_linear_velocity_ = this->getVelocity();
502//         }
503    }
504
505    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
506    {
507//         if (Core::isMaster())
508//         {
509            MobileEntity::setAngularVelocity(velocity);
510            this->common_angular_velocity_ = this->getAngularVelocity();
511//             ++this->server_overwrite_;
512//         }
513//         else if (this->bHasLocalController_)
514//         {
515//             MobileEntity::setAngularVelocity(velocity);
516//             this->client_angular_velocity_ = this->getAngularVelocity();
517//         }
518    }
519
520    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
521    {
522        MobileEntity::setWorldTransform(worldTrans);
523//         if (Core::isMaster())
524//         {
525            this->common_position_ = this->getPosition();
526            this->common_orientation_ = this->getOrientation();
527            this->common_linear_velocity_ = this->getVelocity();
528            this->common_angular_velocity_ = this->getAngularVelocity();
529//         }
530//         else if (this->bHasLocalController_)
531//         {
532//             this->client_position_ = this->getPosition();
533//             this->client_orientation_ = this->getOrientation();
534//             this->client_linear_velocity_ = this->getVelocity();
535//             this->client_angular_velocity_ = this->getAngularVelocity();
536//         }
537    }
538}
Note: See TracBrowser for help on using the repository browser.