Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particles2/src/orxonox/worldentities/ControllableEntity.cc @ 6101

Last change on this file since 6101 was 6101, checked in by scheusso, 14 years ago

rocket now steerable also on client
AIController shoots again now ;)
fire network function is now in CE instead of Pawn
some changes in PlayerInfo that allow controlling of temporary objects (such as Rocket)

  • Property svn:eol-style set to native
File size: 19.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29#include "ControllableEntity.h"
30
31#include <OgreSceneManager.h>
32#include <OgreSceneNode.h>
33
34#include "core/CoreIncludes.h"
35#include "core/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/Controller.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
53    ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator)
54    {
55        RegisterObject(ControllableEntity);
56
57        this->bHasLocalController_ = false;
58        this->bHasHumanController_ = false;
59
60        this->server_overwrite_ = 0;
61        this->client_overwrite_ = 0;
62        this->player_ = 0;
63        this->playerID_ = OBJECTID_UNKNOWN;
64        this->hud_ = 0;
65        this->camera_ = 0;
66        this->xmlcontroller_ = 0;
67        this->reverseCamera_ = 0;
68        this->bDestroyWhenPlayerLeft_ = false;
69        this->cameraPositionRootNode_ = this->node_->createChildSceneNode();
70        this->bMouseLook_ = false;
71        this->mouseLookSpeed_ = 200;
72
73        this->server_position_         = Vector3::ZERO;
74        this->client_position_         = Vector3::ZERO;
75        this->server_linear_velocity_  = Vector3::ZERO;
76        this->client_linear_velocity_  = Vector3::ZERO;
77        this->server_orientation_      = Quaternion::IDENTITY;
78        this->client_orientation_      = Quaternion::IDENTITY;
79        this->server_angular_velocity_ = Vector3::ZERO;
80        this->client_angular_velocity_ = Vector3::ZERO;
81
82
83        this->setConfigValues();
84        this->setPriority( Priority::VeryHigh );
85        this->registerVariables();
86    }
87
88    ControllableEntity::~ControllableEntity()
89    {
90        if (this->isInitialized())
91        {
92            this->bDestroyWhenPlayerLeft_ = false;
93
94            if (this->bHasLocalController_ && this->bHasHumanController_)
95                this->stopLocalHumanControl();
96
97            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
98                this->getPlayer()->stopControl();
99
100            if (this->xmlcontroller_)
101                this->xmlcontroller_->destroy();
102
103            if (this->hud_)
104                this->hud_->destroy();
105
106            if (this->camera_)
107                this->camera_->destroy();
108
109            for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
110                (*it)->destroy();
111
112            if (this->getScene()->getSceneManager())
113                this->getScene()->getSceneManager()->destroySceneNode(this->cameraPositionRootNode_->getName());
114        }
115    }
116
117    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
118    {
119        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
120
121        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
122        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
123
124        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
125        XMLPortObject(ControllableEntity, Controller,     "controller",      setXMLController,  getXMLController,  xmlelement, mode);
126    }
127
128    void ControllableEntity::setConfigValues()
129    {
130        SetConfigValue(mouseLookSpeed_, 3.0f);
131    }
132
133    void ControllableEntity::addCameraPosition(CameraPosition* position)
134    {
135        if (!position->getIsAbsolute())
136        {
137            if (position->getAllowMouseLook())
138                position->attachToNode(this->cameraPositionRootNode_);
139            else
140                this->attach(position);
141        }
142        else
143        {
144            WorldEntity* parent = this->getParent();
145            if (parent)
146                parent->attach(position);
147        }
148
149        if (!position->getRenderCamera())
150            this->cameraPositions_.push_back(position);
151        else
152            this->setReverseCamera(position);
153    }
154
155    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
156    {
157        unsigned int i = 0;
158        for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
159        {
160            if (i == index)
161                return (*it);
162            ++i;
163        }
164        return 0;
165    }
166
167    void ControllableEntity::switchCamera()
168    {
169        if (this->camera_)
170        {
171            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
172            {
173                this->cameraPositions_.front()->attachCamera(this->camera_);
174            }
175            else if (this->cameraPositions_.size() > 0)
176            {
177                for (std::list<SmartPtr<CameraPosition> >::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
178                {
179                    if ((*it) == this->camera_->getParent())
180                    {
181                        ++it;
182                        if (it != this->cameraPositions_.end())
183                            (*it)->attachCamera(this->camera_);
184                        else
185                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
186                        break;
187                    }
188                }
189            }
190            else
191            {
192                this->camera_->attachToNode(this->cameraPositionRootNode_);
193            }
194        }
195    }
196
197    void ControllableEntity::mouseLook()
198    {
199        this->bMouseLook_ = !this->bMouseLook_;
200
201        if (!this->bMouseLook_)
202            this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
203    }
204
205    void ControllableEntity::rotateYaw(const Vector2& value)
206    {
207        if (this->bMouseLook_)
208            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
209    }
210
211    void ControllableEntity::rotatePitch(const Vector2& value)
212    {
213        if (this->bMouseLook_)
214            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
215    }
216
217    void ControllableEntity::rotateRoll(const Vector2& value)
218    {
219        if (this->bMouseLook_)
220            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
221    }
222   
223    void ControllableEntity::fire(unsigned int firemode)
224    {
225        if(GameMode::isMaster())
226        {
227            this->fired(firemode);
228        }
229        else
230        {
231            callMemberNetworkFunction(ControllableEntity, fire, this->getObjectID(), 0, firemode);
232        }
233    }
234
235    void ControllableEntity::setPlayer(PlayerInfo* player)
236    {
237        if (!player)
238        {
239            this->removePlayer();
240            return;
241        }
242
243        this->player_ = player;
244        this->playerID_ = player->getObjectID();
245        this->bHasLocalController_ = player->isLocalPlayer();
246        this->bHasHumanController_ = player->isHumanPlayer();
247
248        if (this->bHasLocalController_ && this->bHasHumanController_)
249        {
250            this->startLocalHumanControl();
251
252            if (!GameMode::isMaster())
253            {
254                this->client_overwrite_ = this->server_overwrite_;
255                this->setSyncMode(ObjectDirection::Bidirectional);
256            }
257        }
258
259        this->changedPlayer();
260    }
261
262    void ControllableEntity::removePlayer()
263    {
264        if (this->bHasLocalController_ && this->bHasHumanController_)
265            this->stopLocalHumanControl();
266
267        this->player_ = 0;
268        this->playerID_ = OBJECTID_UNKNOWN;
269        this->bHasLocalController_ = false;
270        this->bHasHumanController_ = false;
271        this->setSyncMode(ObjectDirection::ToClient);
272
273        this->changedPlayer();
274
275        if (this->bDestroyWhenPlayerLeft_)
276            this->destroy();
277    }
278
279    void ControllableEntity::networkcallback_changedplayerID()
280    {
281        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
282        if (this->playerID_ != OBJECTID_UNKNOWN)
283        {
284            this->player_ = orxonox_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
285            if (this->player_ && (this->player_->getControllableEntity() != this))
286                this->player_->startControl(this);
287        }
288    }
289
290    void ControllableEntity::startLocalHumanControl()
291    {
292        if (!this->camera_ && GameMode::showsGraphics())
293        {
294            this->camera_ = new Camera(this);
295            this->camera_->requestFocus();
296            if (this->cameraPositionTemplate_ != "")
297                this->addTemplate(this->cameraPositionTemplate_);
298            if (this->cameraPositions_.size() > 0)
299                this->cameraPositions_.front()->attachCamera(this->camera_);
300            else
301                this->camera_->attachToNode(this->cameraPositionRootNode_);
302        }
303
304        if (!this->hud_ && GameMode::showsGraphics())
305        {
306            if (this->hudtemplate_ != "")
307            {
308                this->hud_ = new OverlayGroup(this);
309                this->hud_->addTemplate(this->hudtemplate_);
310                this->hud_->setOwner(this);
311            }
312        }
313    }
314
315    void ControllableEntity::stopLocalHumanControl()
316    {
317        if (this->camera_)
318        {
319            this->camera_->detachFromParent();
320            this->camera_->destroy();
321            this->camera_ = 0;
322        }
323
324        if (this->hud_)
325        {
326            this->hud_->destroy();
327            this->hud_ = 0;
328        }
329    }
330
331    void ControllableEntity::setXMLController(Controller* controller)
332    {
333        if (!this->xmlcontroller_)
334        {
335            this->xmlcontroller_ = controller;
336            this->bHasLocalController_ = true;
337            this->xmlcontroller_->setControllableEntity(this);
338        }
339        else
340            COUT(2) << "Warning: ControllableEntity \"" << this->getName() << "\" already has a Controller." << std::endl;
341    }
342
343    void ControllableEntity::parentChanged()
344    {
345        WorldEntity::parentChanged();
346
347        WorldEntity* parent = this->getParent();
348        if (parent)
349        {
350            for (std::list<SmartPtr<CameraPosition> >::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
351                if ((*it)->getIsAbsolute())
352                    parent->attach((*it));
353        }
354    }
355
356    void ControllableEntity::tick(float dt)
357    {
358        MobileEntity::tick(dt);
359
360        if (this->isActive())
361        {
362            // Check whether Bullet doesn't do the physics for us
363            if (!this->isDynamic())
364            {
365                if (GameMode::isMaster())
366                {
367                    this->server_position_ = this->getPosition();
368                    this->server_orientation_ = this->getOrientation();
369                    this->server_linear_velocity_ = this->getVelocity();
370                    this->server_angular_velocity_ = this->getAngularVelocity();
371                }
372                else if (this->bHasLocalController_)
373                {
374                    this->client_position_ = this->getPosition();
375                    this->client_orientation_ = this->getOrientation();
376                    this->client_linear_velocity_ = this->getVelocity();
377                    this->client_angular_velocity_ = this->getAngularVelocity();
378                }
379            }
380        }
381    }
382
383    void ControllableEntity::registerVariables()
384    {
385        registerVariable(this->cameraPositionTemplate_,  VariableDirection::ToClient);
386        registerVariable(this->hudtemplate_,             VariableDirection::ToClient);
387
388        registerVariable(this->server_position_,         VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
389        registerVariable(this->server_linear_velocity_,  VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
390        registerVariable(this->server_orientation_,      VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
391        registerVariable(this->server_angular_velocity_, VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
392
393        registerVariable(this->server_overwrite_,        VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
394        registerVariable(this->client_overwrite_,        VariableDirection::ToServer);
395
396        registerVariable(this->client_position_,         VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
397        registerVariable(this->client_linear_velocity_,  VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
398        registerVariable(this->client_orientation_,      VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
399        registerVariable(this->client_angular_velocity_, VariableDirection::ToServer, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
400
401
402        registerVariable(this->playerID_,                VariableDirection::ToClient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
403    }
404
405    void ControllableEntity::processServerPosition()
406    {
407        if (!this->bHasLocalController_)
408            MobileEntity::setPosition(this->server_position_);
409    }
410
411    void ControllableEntity::processServerLinearVelocity()
412    {
413        if (!this->bHasLocalController_)
414            MobileEntity::setVelocity(this->server_linear_velocity_);
415    }
416
417    void ControllableEntity::processServerOrientation()
418    {
419        if (!this->bHasLocalController_)
420            MobileEntity::setOrientation(this->server_orientation_);
421    }
422
423    void ControllableEntity::processServerAngularVelocity()
424    {
425        if (!this->bHasLocalController_)
426            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
427    }
428
429    void ControllableEntity::processOverwrite()
430    {
431        if (this->bHasLocalController_)
432        {
433            this->setPosition(this->server_position_);
434            this->setOrientation(this->server_orientation_);
435            this->setVelocity(this->server_linear_velocity_);
436            this->setAngularVelocity(this->server_angular_velocity_);
437
438            this->client_overwrite_ = this->server_overwrite_;
439        }
440    }
441
442    void ControllableEntity::processClientPosition()
443    {
444        if (this->server_overwrite_ == this->client_overwrite_)
445        {
446            MobileEntity::setPosition(this->client_position_);
447            this->server_position_ = this->getPosition();
448        }
449    }
450
451    void ControllableEntity::processClientLinearVelocity()
452    {
453        if (this->server_overwrite_ == this->client_overwrite_)
454        {
455            MobileEntity::setVelocity(this->client_linear_velocity_);
456            this->server_linear_velocity_ = this->getVelocity();
457        }
458    }
459
460    void ControllableEntity::processClientOrientation()
461    {
462        if (this->server_overwrite_ == this->client_overwrite_)
463        {
464            MobileEntity::setOrientation(this->client_orientation_);
465            this->server_orientation_ = this->getOrientation();
466        }
467    }
468
469    void ControllableEntity::processClientAngularVelocity()
470    {
471        if (this->server_overwrite_ == this->client_overwrite_)
472        {
473            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
474            this->server_angular_velocity_ = this->getAngularVelocity();
475        }
476    }
477
478    void ControllableEntity::setPosition(const Vector3& position)
479    {
480        if (GameMode::isMaster())
481        {
482            MobileEntity::setPosition(position);
483            this->server_position_ = this->getPosition();
484            ++this->server_overwrite_;
485        }
486        else if (this->bHasLocalController_)
487        {
488            MobileEntity::setPosition(position);
489            this->client_position_ = this->getPosition();
490        }
491    }
492
493    void ControllableEntity::setOrientation(const Quaternion& orientation)
494    {
495        if (GameMode::isMaster())
496        {
497            MobileEntity::setOrientation(orientation);
498            this->server_orientation_ = this->getOrientation();
499            ++this->server_overwrite_;
500        }
501        else if (this->bHasLocalController_)
502        {
503            MobileEntity::setOrientation(orientation);
504            this->client_orientation_ = this->getOrientation();
505        }
506    }
507
508    void ControllableEntity::setVelocity(const Vector3& velocity)
509    {
510        if (GameMode::isMaster())
511        {
512            MobileEntity::setVelocity(velocity);
513            this->server_linear_velocity_ = this->getVelocity();
514            ++this->server_overwrite_;
515        }
516        else if (this->bHasLocalController_)
517        {
518            MobileEntity::setVelocity(velocity);
519            this->client_linear_velocity_ = this->getVelocity();
520        }
521    }
522
523    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
524    {
525        if (GameMode::isMaster())
526        {
527            MobileEntity::setAngularVelocity(velocity);
528            this->server_angular_velocity_ = this->getAngularVelocity();
529            ++this->server_overwrite_;
530        }
531        else if (this->bHasLocalController_)
532        {
533            MobileEntity::setAngularVelocity(velocity);
534            this->client_angular_velocity_ = this->getAngularVelocity();
535        }
536    }
537
538    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
539    {
540        MobileEntity::setWorldTransform(worldTrans);
541        if (GameMode::isMaster())
542        {
543            this->server_position_ = this->getPosition();
544            this->server_orientation_ = this->getOrientation();
545            this->server_linear_velocity_ = this->getVelocity();
546            this->server_angular_velocity_ = this->getAngularVelocity();
547        }
548        else if (this->bHasLocalController_)
549        {
550            this->client_position_ = this->getPosition();
551            this->client_orientation_ = this->getOrientation();
552            this->client_linear_velocity_ = this->getVelocity();
553            this->client_angular_velocity_ = this->getAngularVelocity();
554        }
555    }
556}
Note: See TracBrowser for help on using the repository browser.