Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController_HS17/src/orxonox/worldentities/ControllableEntity.cc @ 11518

Last change on this file since 11518 was 11518, checked in by kohlia, 6 years ago

Nothing to see yet, really.

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