Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8914 was 8891, checked in by jo, 13 years ago

Ai and tutorial improvements merged back to the trunk. AI features: all weapons are used, the ai-firestrength is configurable, bots are able to collect pickups . I've set the tutorial level as default level.

  • Property svn:eol-style set to native
File size: 23.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 "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::setController(Controller* val)
313    {
314        this->controller_ = val;
315    }
316
317    void ControllableEntity::setTarget( WorldEntity* target )
318    {
319        this->target_ = target;
320        if ( !GameMode::isMaster() )
321        {
322            if ( target != 0 )
323            {
324                callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, target->getObjectID() );
325            }
326           else
327           {
328                callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN );
329           }
330        }
331    }
332
333    void ControllableEntity::setTargetInternal( uint32_t targetID )
334    {
335        this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) );
336    }
337
338    void ControllableEntity::setPlayer(PlayerInfo* player)
339    {
340        if (!player)
341        {
342            this->removePlayer();
343            return;
344        }
345
346        this->player_ = player;
347        this->formerPlayer_ = player;
348        this->playerID_ = player->getObjectID();
349        this->bHasLocalController_ = player->isLocalPlayer();
350        this->bHasHumanController_ = player->isHumanPlayer();
351
352        if (this->bHasLocalController_ && this->bHasHumanController_)
353        {
354            this->startLocalHumanControl();
355
356            if (!GameMode::isMaster())
357            {
358                this->client_overwrite_ = this->server_overwrite_;
359                this->setSyncMode(ObjectDirection::Bidirectional);
360            }
361        }
362
363        this->changedPlayer();
364    }
365
366    void ControllableEntity::removePlayer()
367    {
368        if (this->bHasLocalController_ && this->bHasHumanController_)
369            this->stopLocalHumanControl();
370
371        this->player_ = 0;
372        this->playerID_ = OBJECTID_UNKNOWN;
373        this->bHasLocalController_ = false;
374        this->bHasHumanController_ = false;
375        this->setSyncMode(ObjectDirection::ToClient);
376
377        this->changedPlayer();
378
379        if (this->bDestroyWhenPlayerLeft_)
380            this->destroy();
381    }
382
383    void ControllableEntity::networkcallback_changedplayerID()
384    {
385        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
386        if (this->playerID_ != OBJECTID_UNKNOWN)
387        {
388            this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
389            if (this->player_ && (this->player_->getControllableEntity() != this))
390                this->player_->startControl(this);
391        }
392    }
393
394    void ControllableEntity::startLocalHumanControl()
395    {
396        if (!this->camera_ && GameMode::showsGraphics())
397        {
398            this->camera_ = new Camera(this);
399            this->camera_->requestFocus();
400            if (!this->cameraPositionTemplate_.empty())
401                this->addTemplate(this->cameraPositionTemplate_);
402            if (this->cameraPositions_.size() > 0)
403            {
404                this->cameraPositions_.front()->attachCamera(this->camera_);
405                this->currentCameraPosition_ = this->cameraPositions_.front();
406            }
407            else
408            {
409                this->camera_->attachToNode(this->cameraPositionRootNode_);
410                this->currentCameraPosition_ = 0;
411            }
412        }
413
414        this->createHud();
415    }
416
417    // HACK-ish
418    void ControllableEntity::createHud(void)
419    {
420        if (!this->hud_ && GameMode::showsGraphics())
421        {
422            if (!this->hudtemplate_.empty())
423            {
424                this->hud_ = new OverlayGroup(this);
425                this->hud_->addTemplate(this->hudtemplate_);
426                this->hud_->setOwner(this);
427            }
428        }
429    }
430
431    void ControllableEntity::destroyHud(void)
432    {
433        if (this->hud_ != NULL)
434        {
435            this->hud_->destroy();
436            this->hud_ = NULL;
437        }
438    }
439
440    void ControllableEntity::stopLocalHumanControl()
441    {
442        if (this->camera_)
443        {
444            this->camera_->detachFromParent();
445            this->camera_->destroy();
446            this->camera_ = 0;
447        }
448
449        if (this->hud_)
450        {
451            this->hud_->destroy();
452            this->hud_ = 0;
453        }
454    }
455
456    void ControllableEntity::setXMLController(Controller* controller)
457    {
458        if (!this->xmlcontroller_)
459        {
460            this->xmlcontroller_ = controller;
461            this->bHasLocalController_ = true;
462            this->xmlcontroller_->setControllableEntity(this);
463        }
464        else
465            orxout(internal_warning) << "ControllableEntity \"" << this->getName() << "\" already has a Controller." << endl;
466    }
467
468    void ControllableEntity::parentChanged()
469    {
470        WorldEntity::parentChanged();
471
472        WorldEntity* parent = this->getParent();
473        if (parent)
474        {
475            for (std::list<SmartPtr<CameraPosition> >::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
476                if ((*it)->getIsAbsolute())
477                    parent->attach((*it));
478        }
479    }
480
481    void ControllableEntity::tick(float dt)
482    {
483        MobileEntity::tick(dt);
484
485        if (this->isActive())
486        {
487            // Check whether Bullet doesn't do the physics for us
488            if (!this->isDynamic())
489            {
490                if (GameMode::isMaster())
491                {
492                    this->server_position_ = this->getPosition();
493                    this->server_orientation_ = this->getOrientation();
494                    this->server_linear_velocity_ = this->getVelocity();
495                    this->server_angular_velocity_ = this->getAngularVelocity();
496                }
497                else if (this->bHasLocalController_)
498                {
499                    this->client_position_ = this->getPosition();
500                    this->client_orientation_ = this->getOrientation();
501                    this->client_linear_velocity_ = this->getVelocity();
502                    this->client_angular_velocity_ = this->getAngularVelocity();
503                }
504            }
505        }
506    }
507
508    void ControllableEntity::registerVariables()
509    {
510        registerVariable(this->cameraPositionTemplate_,  VariableDirection::ToClient);
511        registerVariable(this->hudtemplate_,             VariableDirection::ToClient);
512
513        registerVariable(this->server_position_,         VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
514        registerVariable(this->server_linear_velocity_,  VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
515        registerVariable(this->server_orientation_,      VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
516        registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
517
518        registerVariable(this->server_overwrite_,        VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
519        registerVariable(this->client_overwrite_,        VariableDirection::ToServer);
520
521        registerVariable(this->client_position_,         VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
522        registerVariable(this->client_linear_velocity_,  VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
523        registerVariable(this->client_orientation_,      VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
524        registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
525
526
527        registerVariable(this->playerID_,                VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
528    }
529
530    void ControllableEntity::processServerPosition()
531    {
532        if (!this->bHasLocalController_)
533            MobileEntity::setPosition(this->server_position_);
534    }
535
536    void ControllableEntity::processServerLinearVelocity()
537    {
538        if (!this->bHasLocalController_)
539            MobileEntity::setVelocity(this->server_linear_velocity_);
540    }
541
542    void ControllableEntity::processServerOrientation()
543    {
544        if (!this->bHasLocalController_)
545            MobileEntity::setOrientation(this->server_orientation_);
546    }
547
548    void ControllableEntity::processServerAngularVelocity()
549    {
550        if (!this->bHasLocalController_)
551            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
552    }
553
554    void ControllableEntity::processOverwrite()
555    {
556        if (this->bHasLocalController_)
557        {
558            this->setPosition(this->server_position_);
559            this->setOrientation(this->server_orientation_);
560            this->setVelocity(this->server_linear_velocity_);
561            this->setAngularVelocity(this->server_angular_velocity_);
562
563            this->client_overwrite_ = this->server_overwrite_;
564        }
565    }
566
567    void ControllableEntity::processClientPosition()
568    {
569        if (this->server_overwrite_ == this->client_overwrite_)
570        {
571            MobileEntity::setPosition(this->client_position_);
572            this->server_position_ = this->getPosition();
573        }
574    }
575
576    void ControllableEntity::processClientLinearVelocity()
577    {
578        if (this->server_overwrite_ == this->client_overwrite_)
579        {
580            MobileEntity::setVelocity(this->client_linear_velocity_);
581            this->server_linear_velocity_ = this->getVelocity();
582        }
583    }
584
585    void ControllableEntity::processClientOrientation()
586    {
587        if (this->server_overwrite_ == this->client_overwrite_)
588        {
589            MobileEntity::setOrientation(this->client_orientation_);
590            this->server_orientation_ = this->getOrientation();
591        }
592    }
593
594    void ControllableEntity::processClientAngularVelocity()
595    {
596        if (this->server_overwrite_ == this->client_overwrite_)
597        {
598            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
599            this->server_angular_velocity_ = this->getAngularVelocity();
600        }
601    }
602
603    void ControllableEntity::setPosition(const Vector3& position)
604    {
605        if (GameMode::isMaster())
606        {
607            MobileEntity::setPosition(position);
608            this->server_position_ = this->getPosition();
609            ++this->server_overwrite_;
610        }
611        else if (this->bHasLocalController_)
612        {
613            MobileEntity::setPosition(position);
614            this->client_position_ = this->getPosition();
615        }
616    }
617
618    void ControllableEntity::setOrientation(const Quaternion& orientation)
619    {
620        if (GameMode::isMaster())
621        {
622            MobileEntity::setOrientation(orientation);
623            this->server_orientation_ = this->getOrientation();
624            ++this->server_overwrite_;
625        }
626        else if (this->bHasLocalController_)
627        {
628            MobileEntity::setOrientation(orientation);
629            this->client_orientation_ = this->getOrientation();
630        }
631    }
632
633    void ControllableEntity::setVelocity(const Vector3& velocity)
634    {
635        if (GameMode::isMaster())
636        {
637            MobileEntity::setVelocity(velocity);
638            this->server_linear_velocity_ = this->getVelocity();
639            ++this->server_overwrite_;
640        }
641        else if (this->bHasLocalController_)
642        {
643            MobileEntity::setVelocity(velocity);
644            this->client_linear_velocity_ = this->getVelocity();
645        }
646    }
647
648    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
649    {
650        if (GameMode::isMaster())
651        {
652            MobileEntity::setAngularVelocity(velocity);
653            this->server_angular_velocity_ = this->getAngularVelocity();
654            ++this->server_overwrite_;
655        }
656        else if (this->bHasLocalController_)
657        {
658            MobileEntity::setAngularVelocity(velocity);
659            this->client_angular_velocity_ = this->getAngularVelocity();
660        }
661    }
662
663    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
664    {
665        MobileEntity::setWorldTransform(worldTrans);
666        if (GameMode::isMaster())
667        {
668            this->server_position_ = this->getPosition();
669            this->server_orientation_ = this->getOrientation();
670            this->server_linear_velocity_ = this->getVelocity();
671            this->server_angular_velocity_ = this->getAngularVelocity();
672        }
673        else if (this->bHasLocalController_)
674        {
675            this->client_position_ = this->getPosition();
676            this->client_orientation_ = this->getOrientation();
677            this->client_linear_velocity_ = this->getVelocity();
678            this->client_angular_velocity_ = this->getAngularVelocity();
679        }
680    }
681}
Note: See TracBrowser for help on using the repository browser.