Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/orxonox/worldentities/ControllableEntity.cc @ 8898

Last change on this file since 8898 was 8701, checked in by jo, 14 years ago

First successful attempt, to make bots shoot rockets. Unfortunately they're shooting the wrong ones.

  • Property svn:eol-style set to native
File size: 21.1 KB
RevLine 
[2072]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:
[2662]25 *      Reto Grieder
[2072]26 *
27 */
28
29#include "ControllableEntity.h"
30
[2662]31#include <OgreSceneManager.h>
[3196]32#include <OgreSceneNode.h>
[2662]33
[2072]34#include "core/CoreIncludes.h"
[2662]35#include "core/ConfigValueIncludes.h"
[2896]36#include "core/GameMode.h"
[2072]37#include "core/XMLPort.h"
[6417]38#include "network/NetworkFunction.h"
[2072]39
[5735]40#include "Scene.h"
41#include "infos/PlayerInfo.h"
42#include "controllers/Controller.h"
[5737]43#include "graphics/Camera.h"
[5735]44#include "worldentities/CameraPosition.h"
[2072]45#include "overlays/OverlayGroup.h"
46
47namespace orxonox
48{
49    CreateFactory(ControllableEntity);
50
[6417]51    registerMemberNetworkFunction( ControllableEntity, fire );
52    registerMemberNetworkFunction( ControllableEntity, setTargetInternal );
53
[2662]54    ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator)
[2072]55    {
56        RegisterObject(ControllableEntity);
57
[2662]58        this->bHasLocalController_ = false;
59        this->bHasHumanController_ = false;
60
[2072]61        this->server_overwrite_ = 0;
62        this->client_overwrite_ = 0;
63        this->player_ = 0;
[7534]64        this->formerPlayer_ = NULL;
[2171]65        this->playerID_ = OBJECTID_UNKNOWN;
[2072]66        this->hud_ = 0;
67        this->camera_ = 0;
[3049]68        this->xmlcontroller_ = 0;
[6417]69        this->controller_ = 0;
[3089]70        this->reverseCamera_ = 0;
[2072]71        this->bDestroyWhenPlayerLeft_ = false;
[2662]72        this->cameraPositionRootNode_ = this->node_->createChildSceneNode();
[6417]73        this->currentCameraPosition_ = 0;
[2662]74        this->bMouseLook_ = false;
75        this->mouseLookSpeed_ = 200;
[2072]76
[2662]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;
[2072]85
[2662]86
87        this->setConfigValues();
[3280]88        this->setPriority( Priority::VeryHigh );
[2072]89        this->registerVariables();
[8701]90        this->bIsRocket=false;
[2072]91    }
92
93    ControllableEntity::~ControllableEntity()
94    {
95        if (this->isInitialized())
96        {
[2662]97            this->bDestroyWhenPlayerLeft_ = false;
[2072]98
[2662]99            if (this->bHasLocalController_ && this->bHasHumanController_)
100                this->stopLocalHumanControl();
101
102            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
[3038]103                this->getPlayer()->stopControl();
[2662]104
[3049]105            if (this->xmlcontroller_)
[5929]106                this->xmlcontroller_->destroy();
[3049]107
[2072]108            if (this->hud_)
[5929]109                this->hud_->destroy();
[2072]110
111            if (this->camera_)
[5929]112                this->camera_->destroy();
[2072]113
[5929]114            for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
115                (*it)->destroy();
[2662]116
117            if (this->getScene()->getSceneManager())
118                this->getScene()->getSceneManager()->destroySceneNode(this->cameraPositionRootNode_->getName());
[2072]119        }
120    }
121
122    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
123    {
124        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
125
126        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
127        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
128
129        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
[3049]130        XMLPortObject(ControllableEntity, Controller,     "controller",      setXMLController,  getXMLController,  xmlelement, mode);
[2072]131    }
132
[2662]133    void ControllableEntity::setConfigValues()
134    {
135        SetConfigValue(mouseLookSpeed_, 3.0f);
136    }
137
[2072]138    void ControllableEntity::addCameraPosition(CameraPosition* position)
139    {
[2826]140        if (!position->getIsAbsolute())
141        {
142            if (position->getAllowMouseLook())
143                position->attachToNode(this->cameraPositionRootNode_);
144            else
145                this->attach(position);
146        }
[2662]147        else
[2826]148        {
149            WorldEntity* parent = this->getParent();
150            if (parent)
151                parent->attach(position);
152        }
[3089]153
154        if (!position->getRenderCamera())
155            this->cameraPositions_.push_back(position);
156        else
157            this->setReverseCamera(position);
[2072]158    }
159
160    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
161    {
162        unsigned int i = 0;
[5929]163        for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
[2072]164        {
165            if (i == index)
166                return (*it);
167            ++i;
168        }
169        return 0;
170    }
171
172    void ControllableEntity::switchCamera()
173    {
174        if (this->camera_)
175        {
176            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
177            {
178                this->cameraPositions_.front()->attachCamera(this->camera_);
[6417]179                this->currentCameraPosition_ = this->cameraPositions_.front().get();
[2072]180            }
181            else if (this->cameraPositions_.size() > 0)
182            {
[5929]183                for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
[2072]184                {
185                    if ((*it) == this->camera_->getParent())
186                    {
187                        ++it;
188                        if (it != this->cameraPositions_.end())
[6417]189                        {
[2072]190                            (*it)->attachCamera(this->camera_);
[6417]191                            this->currentCameraPosition_ = *it;
192                        }
[2072]193                        else
[6417]194                        {
[2072]195                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
[6417]196                            this->currentCameraPosition_ = *this->cameraPositions_.begin();
197                        }
[2072]198                        break;
199                    }
200                }
201            }
202            else
203            {
[2662]204                this->camera_->attachToNode(this->cameraPositionRootNode_);
[6417]205                this->currentCameraPosition_ = 0;
[2072]206            }
207        }
208    }
209
[2662]210    void ControllableEntity::mouseLook()
211    {
212        this->bMouseLook_ = !this->bMouseLook_;
213
214        if (!this->bMouseLook_)
215            this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
[6417]216        if (this->getCamera())
217        {
218            if (!this->bMouseLook_&& this->currentCameraPosition_->getDrag())
219                this->getCamera()->setDrag(true);
220            else
221                this->getCamera()->setDrag(false);
222        }
[2662]223    }
224
225    void ControllableEntity::rotateYaw(const Vector2& value)
226    {
227        if (this->bMouseLook_)
228            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
229    }
230
231    void ControllableEntity::rotatePitch(const Vector2& value)
232    {
233        if (this->bMouseLook_)
234            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
235    }
236
237    void ControllableEntity::rotateRoll(const Vector2& value)
238    {
239        if (this->bMouseLook_)
240            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
241    }
242
[6417]243    void ControllableEntity::fire(unsigned int firemode)
244    {
245        if(GameMode::isMaster())
246        {
247            this->fired(firemode);
248        }
249        else
250        {
251            callMemberNetworkFunction(ControllableEntity, fire, this->getObjectID(), 0, firemode);
252        }
253    }
254
255    void ControllableEntity::setTarget( WorldEntity* target )
256    {
257        this->target_ = target;
258        if ( !GameMode::isMaster() )
259        {
260            if ( target != 0 )
261            {
262                callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, target->getObjectID() );
263            }
264           else
265           {
266                callMemberNetworkFunction(ControllableEntity, setTargetInternal, this->getObjectID(), 0, OBJECTID_UNKNOWN );
267           }
268        }
269    }
270
271    void ControllableEntity::setTargetInternal( uint32_t targetID )
272    {
273        this->setTarget( orxonox_cast<WorldEntity*>(Synchronisable::getSynchronisable(targetID)) );
274    }
275
[2072]276    void ControllableEntity::setPlayer(PlayerInfo* player)
277    {
278        if (!player)
279        {
280            this->removePlayer();
281            return;
282        }
283
284        this->player_ = player;
[7533]285        this->formerPlayer_ = player;
[2072]286        this->playerID_ = player->getObjectID();
[2662]287        this->bHasLocalController_ = player->isLocalPlayer();
288        this->bHasHumanController_ = player->isHumanPlayer();
289
290        if (this->bHasLocalController_ && this->bHasHumanController_)
[2072]291        {
[2662]292            this->startLocalHumanControl();
[2072]293
[2896]294            if (!GameMode::isMaster())
[2072]295            {
296                this->client_overwrite_ = this->server_overwrite_;
[5929]297                this->setSyncMode(ObjectDirection::Bidirectional);
[2072]298            }
299        }
[2839]300
301        this->changedPlayer();
[2072]302    }
303
304    void ControllableEntity::removePlayer()
305    {
[2662]306        if (this->bHasLocalController_ && this->bHasHumanController_)
307            this->stopLocalHumanControl();
[2072]308
309        this->player_ = 0;
[2171]310        this->playerID_ = OBJECTID_UNKNOWN;
[2662]311        this->bHasLocalController_ = false;
312        this->bHasHumanController_ = false;
[5929]313        this->setSyncMode(ObjectDirection::ToClient);
[2072]314
[2839]315        this->changedPlayer();
316
[2072]317        if (this->bDestroyWhenPlayerLeft_)
[5929]318            this->destroy();
[2072]319    }
320
321    void ControllableEntity::networkcallback_changedplayerID()
322    {
323        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
[2171]324        if (this->playerID_ != OBJECTID_UNKNOWN)
[2072]325        {
[3325]326            this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
[2072]327            if (this->player_ && (this->player_->getControllableEntity() != this))
328                this->player_->startControl(this);
329        }
330    }
331
[2662]332    void ControllableEntity::startLocalHumanControl()
333    {
[5929]334        if (!this->camera_ && GameMode::showsGraphics())
[2072]335        {
[2662]336            this->camera_ = new Camera(this);
337            this->camera_->requestFocus();
[6417]338            if (!this->cameraPositionTemplate_.empty())
[2662]339                this->addTemplate(this->cameraPositionTemplate_);
340            if (this->cameraPositions_.size() > 0)
[6417]341            {
[2662]342                this->cameraPositions_.front()->attachCamera(this->camera_);
[6417]343                this->currentCameraPosition_ = this->cameraPositions_.front();
344            }
[2662]345            else
[6417]346            {
[2662]347                this->camera_->attachToNode(this->cameraPositionRootNode_);
[6417]348                this->currentCameraPosition_ = 0;
349            }
[2072]350        }
[2662]351
[5929]352        if (!this->hud_ && GameMode::showsGraphics())
[2662]353        {
[6417]354            if (!this->hudtemplate_.empty())
[2662]355            {
356                this->hud_ = new OverlayGroup(this);
357                this->hud_->addTemplate(this->hudtemplate_);
358                this->hud_->setOwner(this);
359            }
360        }
[2072]361    }
362
[2662]363    void ControllableEntity::stopLocalHumanControl()
[2072]364    {
[2662]365        if (this->camera_)
366        {
367            this->camera_->detachFromParent();
[5929]368            this->camera_->destroy();
[2662]369            this->camera_ = 0;
370        }
[2072]371
[2662]372        if (this->hud_)
373        {
[5929]374            this->hud_->destroy();
[2662]375            this->hud_ = 0;
376        }
[2072]377    }
378
[3049]379    void ControllableEntity::setXMLController(Controller* controller)
380    {
381        if (!this->xmlcontroller_)
382        {
383            this->xmlcontroller_ = controller;
384            this->bHasLocalController_ = true;
385            this->xmlcontroller_->setControllableEntity(this);
386        }
387        else
388            COUT(2) << "Warning: ControllableEntity \"" << this->getName() << "\" already has a Controller." << std::endl;
389    }
390
[2851]391    void ControllableEntity::parentChanged()
392    {
393        WorldEntity::parentChanged();
394
395        WorldEntity* parent = this->getParent();
396        if (parent)
397        {
[5929]398            for (std::list<SmartPtr<CameraPosition> >::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
[2851]399                if ((*it)->getIsAbsolute())
400                    parent->attach((*it));
401        }
402    }
403
[2072]404    void ControllableEntity::tick(float dt)
405    {
[2662]406        MobileEntity::tick(dt);
407
[2072]408        if (this->isActive())
409        {
[2662]410            // Check whether Bullet doesn't do the physics for us
411            if (!this->isDynamic())
[2072]412            {
[2896]413                if (GameMode::isMaster())
[2662]414                {
415                    this->server_position_ = this->getPosition();
416                    this->server_orientation_ = this->getOrientation();
417                    this->server_linear_velocity_ = this->getVelocity();
418                    this->server_angular_velocity_ = this->getAngularVelocity();
419                }
420                else if (this->bHasLocalController_)
421                {
422                    this->client_position_ = this->getPosition();
423                    this->client_orientation_ = this->getOrientation();
424                    this->client_linear_velocity_ = this->getVelocity();
425                    this->client_angular_velocity_ = this->getAngularVelocity();
426                }
[2072]427            }
428        }
429    }
430
431    void ControllableEntity::registerVariables()
432    {
[3280]433        registerVariable(this->cameraPositionTemplate_,  VariableDirection::ToClient);
434        registerVariable(this->hudtemplate_,             VariableDirection::ToClient);
[2072]435
[3280]436        registerVariable(this->server_position_,         VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
437        registerVariable(this->server_linear_velocity_,  VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
438        registerVariable(this->server_orientation_,      VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
439        registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
[2072]440
[3280]441        registerVariable(this->server_overwrite_,        VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
442        registerVariable(this->client_overwrite_,        VariableDirection::ToServer);
[2072]443
[3280]444        registerVariable(this->client_position_,         VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
445        registerVariable(this->client_linear_velocity_,  VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
446        registerVariable(this->client_orientation_,      VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
447        registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
[2072]448
[5735]449
[3280]450        registerVariable(this->playerID_,                VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
[2072]451    }
452
453    void ControllableEntity::processServerPosition()
454    {
[2662]455        if (!this->bHasLocalController_)
456            MobileEntity::setPosition(this->server_position_);
[2072]457    }
458
[2662]459    void ControllableEntity::processServerLinearVelocity()
[2072]460    {
[2662]461        if (!this->bHasLocalController_)
462            MobileEntity::setVelocity(this->server_linear_velocity_);
[2072]463    }
464
465    void ControllableEntity::processServerOrientation()
466    {
[2662]467        if (!this->bHasLocalController_)
468            MobileEntity::setOrientation(this->server_orientation_);
[2072]469    }
470
[2662]471    void ControllableEntity::processServerAngularVelocity()
472    {
473        if (!this->bHasLocalController_)
474            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
475    }
476
[2072]477    void ControllableEntity::processOverwrite()
478    {
[2662]479        if (this->bHasLocalController_)
[2072]480        {
481            this->setPosition(this->server_position_);
482            this->setOrientation(this->server_orientation_);
[2662]483            this->setVelocity(this->server_linear_velocity_);
484            this->setAngularVelocity(this->server_angular_velocity_);
[2072]485
486            this->client_overwrite_ = this->server_overwrite_;
487        }
488    }
489
490    void ControllableEntity::processClientPosition()
491    {
492        if (this->server_overwrite_ == this->client_overwrite_)
493        {
[2662]494            MobileEntity::setPosition(this->client_position_);
495            this->server_position_ = this->getPosition();
[2072]496        }
497    }
498
[2662]499    void ControllableEntity::processClientLinearVelocity()
[2072]500    {
501        if (this->server_overwrite_ == this->client_overwrite_)
502        {
[2662]503            MobileEntity::setVelocity(this->client_linear_velocity_);
504            this->server_linear_velocity_ = this->getVelocity();
[2072]505        }
506    }
507
508    void ControllableEntity::processClientOrientation()
509    {
510        if (this->server_overwrite_ == this->client_overwrite_)
511        {
[2662]512            MobileEntity::setOrientation(this->client_orientation_);
513            this->server_orientation_ = this->getOrientation();
[2072]514        }
515    }
516
[2662]517    void ControllableEntity::processClientAngularVelocity()
[2072]518    {
[2662]519        if (this->server_overwrite_ == this->client_overwrite_)
[2072]520        {
[2662]521            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
522            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]523        }
524    }
525
[2662]526    void ControllableEntity::setPosition(const Vector3& position)
[2072]527    {
[2896]528        if (GameMode::isMaster())
[2072]529        {
[2662]530            MobileEntity::setPosition(position);
531            this->server_position_ = this->getPosition();
[2072]532            ++this->server_overwrite_;
533        }
[2662]534        else if (this->bHasLocalController_)
[2072]535        {
[2662]536            MobileEntity::setPosition(position);
537            this->client_position_ = this->getPosition();
[2072]538        }
539    }
540
541    void ControllableEntity::setOrientation(const Quaternion& orientation)
542    {
[2896]543        if (GameMode::isMaster())
[2072]544        {
[2662]545            MobileEntity::setOrientation(orientation);
546            this->server_orientation_ = this->getOrientation();
[2072]547            ++this->server_overwrite_;
548        }
[2662]549        else if (this->bHasLocalController_)
[2072]550        {
[2662]551            MobileEntity::setOrientation(orientation);
552            this->client_orientation_ = this->getOrientation();
[2072]553        }
554    }
555
[2662]556    void ControllableEntity::setVelocity(const Vector3& velocity)
[2072]557    {
[2896]558        if (GameMode::isMaster())
[2072]559        {
[2662]560            MobileEntity::setVelocity(velocity);
561            this->server_linear_velocity_ = this->getVelocity();
[2072]562            ++this->server_overwrite_;
563        }
[2662]564        else if (this->bHasLocalController_)
[2072]565        {
[2662]566            MobileEntity::setVelocity(velocity);
567            this->client_linear_velocity_ = this->getVelocity();
[2072]568        }
569    }
570
[2662]571    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
[2072]572    {
[2896]573        if (GameMode::isMaster())
[2072]574        {
[2662]575            MobileEntity::setAngularVelocity(velocity);
576            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]577            ++this->server_overwrite_;
578        }
[2662]579        else if (this->bHasLocalController_)
[2072]580        {
[2662]581            MobileEntity::setAngularVelocity(velocity);
582            this->client_angular_velocity_ = this->getAngularVelocity();
[2072]583        }
584    }
585
[2662]586    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
[2072]587    {
[2662]588        MobileEntity::setWorldTransform(worldTrans);
[2896]589        if (GameMode::isMaster())
[2072]590        {
[2662]591            this->server_position_ = this->getPosition();
592            this->server_orientation_ = this->getOrientation();
593            this->server_linear_velocity_ = this->getVelocity();
594            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]595        }
[2662]596        else if (this->bHasLocalController_)
[2072]597        {
[2662]598            this->client_position_ = this->getPosition();
599            this->client_orientation_ = this->getOrientation();
600            this->client_linear_velocity_ = this->getVelocity();
601            this->client_angular_velocity_ = this->getAngularVelocity();
[2072]602        }
603    }
604}
Note: See TracBrowser for help on using the repository browser.