Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/ControllableEntity.cc @ 2415

Last change on this file since 2415 was 2415, checked in by scheusso, 15 years ago
  • adjusted some priorities of objects (movableentity, controllableentity, positionableentity, model)
  • dedicated server mode doesn't consume 100%cpu load anymore now
  • Property svn:eol-style set to native
File size: 16.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "ControllableEntity.h"
31
32#include "core/CoreIncludes.h"
33#include "core/Core.h"
34#include "core/XMLPort.h"
35#include "core/Template.h"
36
37#include "objects/infos/PlayerInfo.h"
38#include "objects/worldentities/Camera.h"
39#include "objects/worldentities/CameraPosition.h"
40#include "overlays/OverlayGroup.h"
41
42namespace orxonox
43{
44    CreateFactory(ControllableEntity);
45
46    ControllableEntity::ControllableEntity(BaseObject* creator) : WorldEntity(creator)
47    {
48        RegisterObject(ControllableEntity);
49
50        this->bControlled_ = false;
51        this->server_overwrite_ = 0;
52        this->client_overwrite_ = 0;
53        this->player_ = 0;
54        this->playerID_ = OBJECTID_UNKNOWN;
55        this->hud_ = 0;
56        this->camera_ = 0;
57        this->bDestroyWhenPlayerLeft_ = false;
58
59        this->velocity_ = Vector3::ZERO;
60        this->acceleration_ = Vector3::ZERO;
61
62        this->server_position_ = Vector3::ZERO;
63        this->client_position_ = Vector3::ZERO;
64        this->server_velocity_ = Vector3::ZERO;
65        this->client_velocity_ = Vector3::ZERO;
66        this->server_orientation_ = Quaternion::IDENTITY;
67        this->client_orientation_ = Quaternion::IDENTITY;
68       
69        this->setPriority( priority::very_high );
70
71        this->registerVariables();
72    }
73
74    ControllableEntity::~ControllableEntity()
75    {
76        if (this->isInitialized())
77        {
78            if (this->bControlled_)
79                this->stopLocalControl();
80
81            if (this->hud_)
82                delete this->hud_;
83
84            if (this->camera_)
85                delete this->camera_;
86
87            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
88                this->getPlayer()->stopControl(this, false);
89        }
90    }
91
92    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
93    {
94        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
95
96        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
97        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
98
99        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
100    }
101
102    void ControllableEntity::addCameraPosition(CameraPosition* position)
103    {
104        this->attach(position);
105        this->cameraPositions_.push_back(position);
106    }
107
108    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
109    {
110        unsigned int i = 0;
111        for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
112        {
113            if (i == index)
114                return (*it);
115            ++i;
116        }
117        return 0;
118    }
119
120    void ControllableEntity::switchCamera()
121    {
122        if (this->camera_)
123        {
124            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
125            {
126                this->cameraPositions_.front()->attachCamera(this->camera_);
127            }
128            else if (this->cameraPositions_.size() > 0)
129            {
130                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
131                {
132                    if ((*it) == this->camera_->getParent())
133                    {
134                        ++it;
135                        if (it != this->cameraPositions_.end())
136                            (*it)->attachCamera(this->camera_);
137                        else
138                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
139                        break;
140                    }
141                }
142            }
143            else
144            {
145                this->attach(this->camera_);
146            }
147        }
148    }
149
150    void ControllableEntity::setPlayer(PlayerInfo* player)
151    {
152        if (!player)
153        {
154            this->removePlayer();
155            return;
156        }
157
158        this->player_ = player;
159        this->playerID_ = player->getObjectID();
160        this->bControlled_ = (player->isLocalPlayer() && player->isHumanPlayer());
161        if (this->bControlled_)
162        {
163            this->startLocalControl();
164
165            if (!Core::isMaster())
166            {
167                this->client_overwrite_ = this->server_overwrite_;
168COUT(0) << "CE: bidirectional synchronization" << std::endl;
169                this->setObjectMode(objectDirection::bidirectional);
170            }
171        }
172    }
173
174    void ControllableEntity::removePlayer()
175    {
176        if (this->bControlled_)
177            this->stopLocalControl();
178
179        this->player_ = 0;
180        this->playerID_ = OBJECTID_UNKNOWN;
181        this->bControlled_ = false;
182        this->setObjectMode(objectDirection::toclient);
183
184        if (this->bDestroyWhenPlayerLeft_)
185            delete this;
186    }
187
188    void ControllableEntity::networkcallback_changedplayerID()
189    {
190        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
191        if (this->playerID_ != OBJECTID_UNKNOWN)
192        {
193            this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
194            if (this->player_ && (this->player_->getControllableEntity() != this))
195                this->player_->startControl(this);
196        }
197    }
198
199    void ControllableEntity::startLocalControl()
200    {
201//        std::cout << this->getObjectID() << " ###### start local control" << std::endl;
202        this->camera_ = new Camera(this);
203        this->camera_->requestFocus();
204        if (this->cameraPositionTemplate_ != "")
205            this->addTemplate(this->cameraPositionTemplate_);
206        if (this->cameraPositions_.size() > 0)
207            this->cameraPositions_.front()->attachCamera(this->camera_);
208        else
209            this->attach(this->camera_);
210
211        if (this->hudtemplate_ != "")
212        {
213            this->hud_ = new OverlayGroup(this);
214            this->hud_->addTemplate(this->hudtemplate_);
215        }
216    }
217
218    void ControllableEntity::stopLocalControl()
219    {
220//        std::cout << "###### stop local control" << std::endl;
221        this->camera_->detachFromParent();
222        delete this->camera_;
223        this->camera_ = 0;
224
225        delete this->hud_;
226        this->hud_ = 0;
227    }
228
229    void ControllableEntity::tick(float dt)
230    {
231        if (this->isActive())
232        {
233            this->velocity_ += (dt * this->acceleration_);
234            this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
235
236            if (Core::isMaster())
237            {
238                this->server_velocity_ = this->velocity_;
239                this->server_position_ = this->node_->getPosition();
240            }
241            else if (this->bControlled_)
242            {
243//                COUT(2) << "setting client position" << endl;
244                this->client_velocity_ = this->velocity_;
245                this->client_position_ = this->node_->getPosition();
246            }
247        }
248    }
249
250    void ControllableEntity::registerVariables()
251    {
252        registerVariable(this->cameraPositionTemplate_, variableDirection::toclient);
253 
254        registerVariable(this->client_overwrite_,   variableDirection::toserver);
255         
256        registerVariable(this->server_position_,    variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
257        registerVariable(this->server_velocity_,    variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerVelocity));
258        registerVariable(this->server_orientation_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
259        registerVariable(this->server_overwrite_,   variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
260 
261        registerVariable(this->client_position_,    variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
262        registerVariable(this->client_velocity_,    variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientVelocity));
263        registerVariable(this->client_orientation_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
264 
265 
266        registerVariable(this->playerID_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
267    }
268
269    void ControllableEntity::processServerPosition()
270    {
271        if (!this->bControlled_)
272            this->node_->setPosition(this->server_position_);
273    }
274
275    void ControllableEntity::processServerVelocity()
276    {
277        if (!this->bControlled_)
278            this->velocity_ = this->server_velocity_;
279    }
280
281    void ControllableEntity::processServerOrientation()
282    {
283        if (!this->bControlled_)
284            this->node_->setOrientation(this->server_orientation_);
285    }
286
287    void ControllableEntity::processOverwrite()
288    {
289        if (this->bControlled_)
290        {
291            this->setPosition(this->server_position_);
292            this->setVelocity(this->server_velocity_);
293            this->setOrientation(this->server_orientation_);
294
295            this->client_overwrite_ = this->server_overwrite_;
296        }
297    }
298
299    void ControllableEntity::processClientPosition()
300    {
301        if (this->server_overwrite_ == this->client_overwrite_)
302        {
303//            COUT(2) << "callback: setting client position" << endl;
304            this->node_->setPosition(this->client_position_);
305            this->server_position_ = this->client_position_;
306        }
307//        else
308//          COUT(2) << "callback: not setting client position" << endl;
309    }
310
311    void ControllableEntity::processClientVelocity()
312    {
313        if (this->server_overwrite_ == this->client_overwrite_)
314        {
315            this->velocity_ = this->client_velocity_;
316            this->server_velocity_ = this->client_velocity_;
317        }
318    }
319
320    void ControllableEntity::processClientOrientation()
321    {
322        if (this->server_overwrite_ == this->client_overwrite_)
323        {
324            this->node_->setOrientation(this->client_orientation_);
325            this->server_orientation_ = this->client_orientation_;
326        }
327    }
328
329
330    void ControllableEntity::setPosition(const Vector3& position)
331    {
332        if (Core::isMaster())
333        {
334            this->node_->setPosition(position);
335            this->server_position_ = position;
336            ++this->server_overwrite_;
337        }
338        else if (this->bControlled_)
339        {
340            this->node_->setPosition(position);
341            this->client_position_ = position;
342        }
343    }
344
345    void ControllableEntity::setVelocity(const Vector3& velocity)
346    {
347        if (Core::isMaster())
348        {
349            this->velocity_ = velocity;
350            this->server_velocity_ = velocity;
351            ++this->server_overwrite_;
352        }
353        else if (this->bControlled_)
354        {
355            this->velocity_ = velocity;
356            this->client_velocity_ = velocity;
357        }
358    }
359
360    void ControllableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
361    {
362        if (Core::isMaster())
363        {
364            this->node_->translate(distance, relativeTo);
365            this->server_position_ = this->node_->getPosition();
366            ++this->server_overwrite_;
367        }
368        else if (this->bControlled_)
369        {
370            this->node_->translate(distance, relativeTo);
371            this->client_position_ = this->node_->getPosition();
372        }
373    }
374
375    void ControllableEntity::setOrientation(const Quaternion& orientation)
376    {
377        if (Core::isMaster())
378        {
379            this->node_->setOrientation(orientation);
380            this->server_orientation_ = orientation;
381            ++this->server_overwrite_;
382        }
383        else if (this->bControlled_)
384        {
385            this->node_->setOrientation(orientation);
386            this->client_orientation_ = orientation;
387        }
388    }
389
390    void ControllableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
391    {
392        if (Core::isMaster())
393        {
394            this->node_->rotate(rotation, relativeTo);
395            this->server_orientation_ = this->node_->getOrientation();
396            ++this->server_overwrite_;
397        }
398        else if (this->bControlled_)
399        {
400            this->node_->rotate(rotation, relativeTo);
401            this->client_orientation_ = this->node_->getOrientation();
402        }
403    }
404
405    void ControllableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
406    {
407        if (Core::isMaster())
408        {
409            this->node_->yaw(angle, relativeTo);
410            this->server_orientation_ = this->node_->getOrientation();
411            ++this->server_overwrite_;
412        }
413        else if (this->bControlled_)
414        {
415            this->node_->yaw(angle, relativeTo);
416            this->client_orientation_ = this->node_->getOrientation();
417        }
418    }
419
420    void ControllableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
421    {
422        if (Core::isMaster())
423        {
424            this->node_->pitch(angle, relativeTo);
425            this->server_orientation_ = this->node_->getOrientation();
426            ++this->server_overwrite_;
427        }
428        else if (this->bControlled_)
429        {
430            this->node_->pitch(angle, relativeTo);
431            this->client_orientation_ = this->node_->getOrientation();
432        }
433    }
434
435    void ControllableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
436    {
437        if (Core::isMaster())
438        {
439            this->node_->roll(angle, relativeTo);
440            this->server_orientation_ = this->node_->getOrientation();
441            ++this->server_overwrite_;
442        }
443        else if (this->bControlled_)
444        {
445            this->node_->roll(angle, relativeTo);
446            this->client_orientation_ = this->node_->getOrientation();
447        }
448    }
449
450    void ControllableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
451    {
452        if (Core::isMaster())
453        {
454            this->node_->lookAt(target, relativeTo, localDirectionVector);
455            this->server_orientation_ = this->node_->getOrientation();
456            ++this->server_overwrite_;
457        }
458        else if (this->bControlled_)
459        {
460            this->node_->lookAt(target, relativeTo, localDirectionVector);
461            this->client_orientation_ = this->node_->getOrientation();
462        }
463    }
464
465    void ControllableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
466    {
467        if (Core::isMaster())
468        {
469            this->node_->setDirection(direction, relativeTo, localDirectionVector);
470            this->server_orientation_ = this->node_->getOrientation();
471            ++this->server_overwrite_;
472        }
473        else if (this->bControlled_)
474        {
475            this->node_->setDirection(direction, relativeTo, localDirectionVector);
476            this->client_orientation_ = this->node_->getOrientation();
477        }
478    }
479}
Note: See TracBrowser for help on using the repository browser.