Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sagerjFS16/src/orxonox/worldentities/ControllableEntity.cc @ 11164

Last change on this file since 11164 was 11164, checked in by sagerj, 8 years ago

added orxout() for every release lvl for debug purpose

  • Property svn:eol-style set to native
File size: 24.4 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/config/ConfigValueIncludes.h"
36#include "core/GameMode.h"
37#include "core/XMLPort.h"
38#include "network/NetworkFunctionIncludes.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    RegisterClass(ControllableEntity);
50
51    registerMemberNetworkFunction( ControllableEntity, fire );
52    registerMemberNetworkFunction( ControllableEntity, setTargetInternal );
53
54    ControllableEntity::ControllableEntity(Context* context) : MobileEntity(context)
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_ = nullptr;
64        this->formerPlayer_ = nullptr;
65        this->playerID_ = OBJECTID_UNKNOWN;
66        this->hud_ = nullptr;
67        this->camera_ = nullptr;
68        this->xmlcontroller_ = nullptr;
69        //this->controller_ = nullptr;
70        this->reverseCamera_ = nullptr;
71        this->bDestroyWhenPlayerLeft_ = false;
72        this->cameraPositionRootNode_ = this->node_->createChildSceneNode();
73        this->currentCameraPosition_ = nullptr;
74        this->bMouseLook_ = false;
75        this->mouseLookSpeed_ = 200;
76
77        this->server_position_         = Vector3::ZERO;
78        this->client_position_         = Vector3::ZERO;
79        this->server_linear_velocity_  = Vector3::ZERO;
80        this->client_linear_velocity_  = Vector3::ZERO;
81        this->server_orientation_      = Quaternion::IDENTITY;
82        this->client_orientation_      = Quaternion::IDENTITY;
83        this->server_angular_velocity_ = Vector3::ZERO;
84        this->client_angular_velocity_ = Vector3::ZERO;
85
86        this->setConfigValues();
87        this->setPriority( Priority::VeryHigh );
88        this->registerVariables();
89        this->team_ = -1;
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 (CameraPosition* cameraPosition : this->cameraPositions_)
111                cameraPosition->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, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1);
123        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
124        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemplate, xmlelement, mode);
125
126        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
127        XMLPortObject(ControllableEntity, Controller,     "controller",      setXMLController,  getXMLController,  xmlelement, mode);
128    }
129
130    void ControllableEntity::setConfigValues()
131    {
132        SetConfigValue(mouseLookSpeed_, 3.0f);
133    }
134
135    void ControllableEntity::preDestroy()
136    {
137        // HACK - solve this clean and without preDestroy hook for multiplayer where removePlayer() isn't called
138        if (this->isInitialized() && this->bHasLocalController_ && this->bHasHumanController_)
139            this->stopLocalHumanControl();
140    }
141
142    void ControllableEntity::addCameraPosition(CameraPosition* position)
143    {
144        if (!position->getIsAbsolute())
145        {
146            if (position->getAllowMouseLook())
147                position->attachToNode(this->cameraPositionRootNode_);
148            else
149                this->attach(position);
150        }
151        else
152        {
153            WorldEntity* parent = this->getParent();
154            if (parent)
155                parent->attach(position);
156        }
157
158        if (!position->getRenderCamera())
159            this->cameraPositions_.push_back(position);
160        else
161            this->setReverseCamera(position);
162    }
163
164    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
165    {
166        unsigned int i = 0;
167        for (CameraPosition* cameraPosition : this->cameraPositions_)
168        {
169            if (i == index)
170                return cameraPosition;
171            ++i;
172        }
173        return nullptr;
174    }
175
176    unsigned int ControllableEntity::getCurrentCameraIndex() const
177    {
178        if (this->cameraPositions_.size() <= 0)
179            return 0;
180
181        unsigned int counter = 0;
182        for (CameraPosition* cameraPosition : this->cameraPositions_)
183        {
184            if (cameraPosition == this->currentCameraPosition_)
185                break;
186            counter++;
187        }
188        if (counter >= this->cameraPositions_.size())
189            return 0;
190
191        return counter;
192    }
193
194    bool ControllableEntity::setCameraPosition(unsigned int index)
195    {
196        if(this->camera_ != nullptr && this->cameraPositions_.size() > 0)
197        {
198            if(index >= this->cameraPositions_.size())
199                index = 0;
200
201            CameraPosition* position = this->getCameraPosition(index);
202            position->attachCamera(this->camera_);
203            this->currentCameraPosition_ = position;
204            return true;
205        }
206
207        return false;
208    }
209
210    void ControllableEntity::switchCamera()
211    {
212        if (this->camera_)
213        {
214            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
215            {
216                this->cameraPositions_.front()->attachCamera(this->camera_);
217                this->currentCameraPosition_ = this->cameraPositions_.front();
218            }
219            else if (this->cameraPositions_.size() > 0)
220            {
221                for (std::list<StrongPtr<CameraPosition>>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
222                {
223                    if ((*it) == this->camera_->getParent())
224                    {
225                        ++it;
226                        if (it != this->cameraPositions_.end())
227                        {
228                            (*it)->attachCamera(this->camera_);
229                            this->currentCameraPosition_ = *it;
230                        }
231                        else
232                        {
233                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
234                            this->currentCameraPosition_ = *this->cameraPositions_.begin();
235                        }
236                        break;
237                    }
238                }
239            }
240            else
241            {
242                this->camera_->attachToNode(this->cameraPositionRootNode_);
243                this->currentCameraPosition_ = nullptr;
244            }
245
246            // disable mouse look if the new camera position doesn't allow it
247            if (this->currentCameraPosition_ && !this->currentCameraPosition_->getAllowMouseLook() && this->bMouseLook_)
248                this->mouseLook();
249
250            // disable drag if in mouse look
251            if (this->bMouseLook_)
252                this->getCamera()->setDrag(false);
253        }
254    }
255
256    void ControllableEntity::mouseLook()
257    {
258        // enable mouse look only if allowed - disabling it works always
259        if (this->currentCameraPosition_ && (this->currentCameraPosition_->getAllowMouseLook() || this->bMouseLook_))
260        {
261            this->bMouseLook_ = !this->bMouseLook_;
262
263            if (!this->bMouseLook_)
264            {
265                this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
266                this->cameraPositionRootNode_->_update(true, false); // update the camera node because otherwise the camera will drag back in position which looks strange
267
268                NewHumanController* controller = orxonox_cast<NewHumanController*>(this->getController());
269                if (controller)
270                    controller->centerCursor();
271            }
272
273            if (this->getCamera())
274            {
275                if (!this->bMouseLook_ && this->currentCameraPosition_->getDrag())
276                    this->getCamera()->setDrag(true);
277                else
278                    this->getCamera()->setDrag(false);
279            }
280        }
281    }
282
283    void ControllableEntity::rotateYaw(const Vector2& value)
284    {
285        if (this->bMouseLook_)
286            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
287    }
288
289    void ControllableEntity::rotatePitch(const Vector2& value)
290    {
291        if (this->bMouseLook_)
292            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
293    }
294
295    void ControllableEntity::rotateRoll(const Vector2& value)
296    {
297        if (this->bMouseLook_)
298            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
299    }
300
301    void ControllableEntity::fire(unsigned int firemode)
302    {
303        if(GameMode::isMaster())
304        {
305            orxout() << "f CE IF " << firemode << endl;
306            this->fired(firemode);
307        }
308        else
309        {
310            orxout() << "f CE EL " << firemode << endl;
311            callMemberNetworkFunction(&ControllableEntity::fire, this->getObjectID(), 0, firemode);
312        }
313    }
314
315    void ControllableEntity::release(unsigned int firemode)
316    {
317        if(GameMode::isMaster())
318        {
319            orxout() << "r CE IF " << firemode << endl;
320            this->released(firemode);
321        }
322        else
323        {
324            orxout() << "r CE EL " << firemode << endl;
325            callMemberNetworkFunction(&ControllableEntity::fire, this->getObjectID(), 0, firemode);
326        }
327    }
328
329    void ControllableEntity::setController(Controller* val)
330    {
331        this->controller_ = val;
332    }
333
334    void ControllableEntity::setTarget( WorldEntity* target )
335    {
336        this->target_ = target;
337        if ( !GameMode::isMaster() )
338        {
339            if ( target != nullptr )
340            {
341                callMemberNetworkFunction(&ControllableEntity::setTargetInternal, this->getObjectID(), 0, target->getObjectID() );
342            }
343           else
344           {
345                callMemberNetworkFunction(&ControllableEntity::setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN );
346           }
347        }
348    }
349
350    void ControllableEntity::setTargetInternal( uint32_t targetID )
351    {
352        this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) );
353    }
354
355    void ControllableEntity::setPlayer(PlayerInfo* player)
356    {
357        if (!player)
358        {
359            this->removePlayer();
360            return;
361        }
362
363        this->player_ = player;
364        this->formerPlayer_ = player;
365        this->playerID_ = player->getObjectID();
366        this->bHasLocalController_ = player->isLocalPlayer();
367        this->bHasHumanController_ = player->isHumanPlayer();
368        if(controller_ != nullptr)
369            this->team_ = controller_->getTeam(); // forward controller team number
370
371        if (this->bHasLocalController_ && this->bHasHumanController_)
372        {
373            this->startLocalHumanControl();
374
375            if (!GameMode::isMaster())
376            {
377                this->client_overwrite_ = this->server_overwrite_;
378                this->setSyncMode(ObjectDirection::Bidirectional);
379            }
380        }
381
382        this->changedPlayer();
383    }
384
385    void ControllableEntity::removePlayer()
386    {
387        if (this->bHasLocalController_ && this->bHasHumanController_)
388            this->stopLocalHumanControl();
389
390        this->player_ = nullptr;
391        this->playerID_ = OBJECTID_UNKNOWN;
392        this->bHasLocalController_ = false;
393        this->bHasHumanController_ = false;
394        this->setSyncMode(ObjectDirection::ToClient);
395
396        this->changedPlayer();
397
398        if (this->bDestroyWhenPlayerLeft_)
399            this->destroy();
400    }
401
402    void ControllableEntity::networkcallback_changedplayerID()
403    {
404        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
405        if (this->playerID_ != OBJECTID_UNKNOWN)
406        {
407            this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
408            if (this->player_ && (this->player_->getControllableEntity() != this))
409                this->player_->startControl(this);
410        }
411    }
412
413    void ControllableEntity::startLocalHumanControl()
414    {
415        if (!this->camera_ && GameMode::showsGraphics())
416        {
417            this->camera_ = new Camera(this->getContext());
418            this->camera_->requestFocus();
419            if (!this->cameraPositionTemplate_.empty())
420                this->addTemplate(this->cameraPositionTemplate_);
421            if (this->cameraPositions_.size() > 0)
422            {
423                this->cameraPositions_.front()->attachCamera(this->camera_);
424                this->currentCameraPosition_ = this->cameraPositions_.front();
425            }
426            else
427            {
428                this->camera_->attachToNode(this->cameraPositionRootNode_);
429                this->currentCameraPosition_ = nullptr;
430            }
431        }
432
433        this->createHud();
434    }
435
436    // HACK-ish
437    void ControllableEntity::createHud(void)
438    {
439        if (!this->hud_ && GameMode::showsGraphics())
440        {
441            if (!this->hudtemplate_.empty())
442            {
443                this->hud_ = new OverlayGroup(this->getContext());
444                this->hud_->addTemplate(this->hudtemplate_);
445                this->hud_->setOwner(this);
446            }
447        }
448    }
449
450    void ControllableEntity::destroyHud(void)
451    {
452        if (this->hud_ != nullptr)
453        {
454            this->hud_->destroy();
455            this->hud_ = nullptr;
456        }
457    }
458
459    void ControllableEntity::stopLocalHumanControl()
460    {
461        if (this->camera_)
462        {
463            this->camera_->detachFromParent();
464            this->camera_->destroy();
465            this->camera_ = nullptr;
466        }
467
468        if (this->hud_)
469        {
470            this->hud_->destroy();
471            this->hud_ = nullptr;
472        }
473    }
474
475    void ControllableEntity::setXMLController(Controller* controller)
476    {
477        if (!this->xmlcontroller_)
478        {
479            this->xmlcontroller_ = controller;
480            this->bHasLocalController_ = true;
481            this->xmlcontroller_->setControllableEntity(this);
482            this->team_ = this->xmlcontroller_->getTeam(); //forward the team number
483        }
484        else
485            orxout(internal_warning) << "ControllableEntity \"" << this->getName() << "\" already has a Controller." << endl;
486    }
487
488    void ControllableEntity::parentChanged()
489    {
490        WorldEntity::parentChanged();
491
492        WorldEntity* parent = this->getParent();
493        if (parent)
494        {
495            for (CameraPosition* cameraPosition : this->cameraPositions_)
496                if (cameraPosition->getIsAbsolute())
497                    parent->attach(cameraPosition);
498        }
499    }
500
501    void ControllableEntity::tick(float dt)
502    {
503        MobileEntity::tick(dt);
504
505        if (this->isActive())
506        {
507            // Check whether Bullet doesn't do the physics for us
508            if (!this->isDynamic())
509            {
510                if (GameMode::isMaster())
511                {
512                    this->server_position_ = this->getPosition();
513                    this->server_orientation_ = this->getOrientation();
514                    this->server_linear_velocity_ = this->getVelocity();
515                    this->server_angular_velocity_ = this->getAngularVelocity();
516                }
517                else if (this->bHasLocalController_)
518                {
519                    this->client_position_ = this->getPosition();
520                    this->client_orientation_ = this->getOrientation();
521                    this->client_linear_velocity_ = this->getVelocity();
522                    this->client_angular_velocity_ = this->getAngularVelocity();
523                }
524            }
525        }
526    }
527
528    void ControllableEntity::registerVariables()
529    {
530        registerVariable(this->cameraPositionTemplate_,  VariableDirection::ToClient);
531        registerVariable(this->hudtemplate_,             VariableDirection::ToClient);
532
533        registerVariable(this->server_position_,         VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
534        registerVariable(this->server_linear_velocity_,  VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
535        registerVariable(this->server_orientation_,      VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
536        registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
537
538        registerVariable(this->server_overwrite_,        VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
539        registerVariable(this->client_overwrite_,        VariableDirection::ToServer);
540
541        registerVariable(this->client_position_,         VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
542        registerVariable(this->client_linear_velocity_,  VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
543        registerVariable(this->client_orientation_,      VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
544        registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
545
546
547        registerVariable(this->playerID_,                VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
548    }
549
550    void ControllableEntity::processServerPosition()
551    {
552        if (!this->bHasLocalController_)
553            MobileEntity::setPosition(this->server_position_);
554    }
555
556    void ControllableEntity::processServerLinearVelocity()
557    {
558        if (!this->bHasLocalController_)
559            MobileEntity::setVelocity(this->server_linear_velocity_);
560    }
561
562    void ControllableEntity::processServerOrientation()
563    {
564        if (!this->bHasLocalController_)
565            MobileEntity::setOrientation(this->server_orientation_);
566    }
567
568    void ControllableEntity::processServerAngularVelocity()
569    {
570        if (!this->bHasLocalController_)
571            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
572    }
573
574    void ControllableEntity::processOverwrite()
575    {
576        if (this->bHasLocalController_)
577        {
578            this->setPosition(this->server_position_);
579            this->setOrientation(this->server_orientation_);
580            this->setVelocity(this->server_linear_velocity_);
581            this->setAngularVelocity(this->server_angular_velocity_);
582
583            this->client_overwrite_ = this->server_overwrite_;
584        }
585    }
586
587    void ControllableEntity::processClientPosition()
588    {
589        if (this->server_overwrite_ == this->client_overwrite_)
590        {
591            MobileEntity::setPosition(this->client_position_);
592            this->server_position_ = this->getPosition();
593        }
594    }
595
596    void ControllableEntity::processClientLinearVelocity()
597    {
598        if (this->server_overwrite_ == this->client_overwrite_)
599        {
600            MobileEntity::setVelocity(this->client_linear_velocity_);
601            this->server_linear_velocity_ = this->getVelocity();
602        }
603    }
604
605    void ControllableEntity::processClientOrientation()
606    {
607        if (this->server_overwrite_ == this->client_overwrite_)
608        {
609            MobileEntity::setOrientation(this->client_orientation_);
610            this->server_orientation_ = this->getOrientation();
611        }
612    }
613
614    void ControllableEntity::processClientAngularVelocity()
615    {
616        if (this->server_overwrite_ == this->client_overwrite_)
617        {
618            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
619            this->server_angular_velocity_ = this->getAngularVelocity();
620        }
621    }
622
623    void ControllableEntity::setPosition(const Vector3& position)
624    {
625        if (GameMode::isMaster())
626        {
627            MobileEntity::setPosition(position);
628            this->server_position_ = this->getPosition();
629            ++this->server_overwrite_;
630        }
631        else if (this->bHasLocalController_)
632        {
633            MobileEntity::setPosition(position);
634            this->client_position_ = this->getPosition();
635        }
636    }
637
638    void ControllableEntity::setOrientation(const Quaternion& orientation)
639    {
640        if (GameMode::isMaster())
641        {
642            MobileEntity::setOrientation(orientation);
643            this->server_orientation_ = this->getOrientation();
644            ++this->server_overwrite_;
645        }
646        else if (this->bHasLocalController_)
647        {
648            MobileEntity::setOrientation(orientation);
649            this->client_orientation_ = this->getOrientation();
650        }
651    }
652
653    void ControllableEntity::setVelocity(const Vector3& velocity)
654    {
655        if (GameMode::isMaster())
656        {
657            MobileEntity::setVelocity(velocity);
658            this->server_linear_velocity_ = this->getVelocity();
659            ++this->server_overwrite_;
660        }
661        else if (this->bHasLocalController_)
662        {
663            MobileEntity::setVelocity(velocity);
664            this->client_linear_velocity_ = this->getVelocity();
665        }
666    }
667
668    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
669    {
670        if (GameMode::isMaster())
671        {
672            MobileEntity::setAngularVelocity(velocity);
673            this->server_angular_velocity_ = this->getAngularVelocity();
674            ++this->server_overwrite_;
675        }
676        else if (this->bHasLocalController_)
677        {
678            MobileEntity::setAngularVelocity(velocity);
679            this->client_angular_velocity_ = this->getAngularVelocity();
680        }
681    }
682
683    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
684    {
685        MobileEntity::setWorldTransform(worldTrans);
686        if (GameMode::isMaster())
687        {
688            this->server_position_ = this->getPosition();
689            this->server_orientation_ = this->getOrientation();
690            this->server_linear_velocity_ = this->getVelocity();
691            this->server_angular_velocity_ = this->getAngularVelocity();
692        }
693        else if (this->bHasLocalController_)
694        {
695            this->client_position_ = this->getPosition();
696            this->client_orientation_ = this->getOrientation();
697            this->client_linear_velocity_ = this->getVelocity();
698            this->client_angular_velocity_ = this->getAngularVelocity();
699        }
700    }
701}
Note: See TracBrowser for help on using the repository browser.