Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/orxonox/worldentities/ControllableEntity.cc @ 8761

Last change on this file since 8761 was 8733, checked in by jo, 13 years ago

Further narrowing down. The bug is triggered, when a bot calls fire(3) - whenever a bot launches a rocket.

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