Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/worldentities/ControllableEntity.cc @ 2051

Last change on this file since 2051 was 2048, checked in by scheusso, 16 years ago

network tick limit works now

File size: 16.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 *      ...
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_ = network::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->registerVariables();
70    }
71
72    ControllableEntity::~ControllableEntity()
73    {
74        if (this->isInitialized())
75        {
76            if (this->bControlled_)
77                this->stopLocalControl();
78
79            if (this->hud_)
80                delete this->hud_;
81
82            if (this->camera_)
83                delete this->camera_;
84
85            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
86                this->getPlayer()->stopControl(this, false);
87        }
88    }
89
90    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
91    {
92        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
93
94        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
95        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
96
97        XMLPortObject(ControllableEntity, WorldEntity, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
98    }
99
100    void ControllableEntity::addCameraPosition(CameraPosition* position)
101    {
102        this->attach(position);
103        this->cameraPositions_.push_back(position);
104    }
105
106    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
107    {
108        unsigned int i = 0;
109        for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
110        {
111            if (i == index)
112                return (*it);
113            ++i;
114        }
115        return 0;
116    }
117
118    void ControllableEntity::switchCamera()
119    {
120        if (this->camera_)
121        {
122            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
123            {
124                this->cameraPositions_.front()->attachCamera(this->camera_);
125            }
126            else if (this->cameraPositions_.size() > 0)
127            {
128                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
129                {
130                    if ((*it) == this->camera_->getParent())
131                    {
132                        ++it;
133                        if (it != this->cameraPositions_.end())
134                            (*it)->attachCamera(this->camera_);
135                        else
136                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
137                        break;
138                    }
139                }
140            }
141            else
142            {
143                this->attach(this->camera_);
144            }
145        }
146    }
147
148    void ControllableEntity::setPlayer(PlayerInfo* player)
149    {
150        if (!player)
151        {
152            this->removePlayer();
153            return;
154        }
155
156        this->player_ = player;
157        this->playerID_ = player->getObjectID();
158        this->bControlled_ = (player->isLocalPlayer() && player->isHumanPlayer());
159        if (this->bControlled_)
160        {
161            this->startLocalControl();
162
163            if (!Core::isMaster())
164            {
165COUT(0) << "CE: bidirectional synchronization" << std::endl;
166                this->setObjectMode(network::direction::bidirectional);
167            }
168        }
169    }
170
171    void ControllableEntity::removePlayer()
172    {
173        if (this->bControlled_)
174            this->stopLocalControl();
175
176        this->player_ = 0;
177        this->playerID_ = network::OBJECTID_UNKNOWN;
178        this->bControlled_ = false;
179        this->setObjectMode(network::direction::toclient);
180
181        if (this->bDestroyWhenPlayerLeft_)
182            delete this;
183    }
184
185    void ControllableEntity::networkcallback_changedplayerID()
186    {
187        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
188        if (this->playerID_ != network::OBJECTID_UNKNOWN)
189        {
190            this->player_ = dynamic_cast<PlayerInfo*>(network::Synchronisable::getSynchronisable(this->playerID_));
191            if (this->player_ && (this->player_->getControllableEntity() != this))
192                this->player_->startControl(this);
193        }
194    }
195
196    void ControllableEntity::startLocalControl()
197    {
198        std::cout << this->getObjectID() << " ###### start local control" << std::endl;
199        this->camera_ = new Camera(this);
200        this->camera_->requestFocus();
201        if (this->cameraPositionTemplate_ != "")
202            this->addTemplate(this->cameraPositionTemplate_);
203        if (this->cameraPositions_.size() > 0)
204            this->cameraPositions_.front()->attachCamera(this->camera_);
205        else
206            this->attach(this->camera_);
207
208        if (this->hudtemplate_ != "")
209        {
210            this->hud_ = new OverlayGroup(this);
211            this->hud_->addTemplate(this->hudtemplate_);
212        }
213    }
214
215    void ControllableEntity::stopLocalControl()
216    {
217        std::cout << "###### stop local control" << std::endl;
218        this->camera_->detachFromParent();
219        delete this->camera_;
220        this->camera_ = 0;
221
222        delete this->hud_;
223        this->hud_ = 0;
224    }
225
226    void ControllableEntity::tick(float dt)
227    {
228        if (this->isActive())
229        {
230            this->velocity_ += (dt * this->acceleration_);
231            this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
232
233            if (Core::isMaster())
234            {
235                this->server_velocity_ = this->velocity_;
236                this->server_position_ = this->node_->getPosition();
237            }
238            else if (this->bControlled_)
239            {
240                COUT(2) << "setting client position" << endl;
241                this->client_velocity_ = this->velocity_;
242                this->client_position_ = this->node_->getPosition();
243            }
244        }
245    }
246
247    void ControllableEntity::registerVariables()
248    {
249        REGISTERSTRING(this->cameraPositionTemplate_, network::direction::toclient);
250
251        REGISTERDATA(this->server_position_,    network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
252        REGISTERDATA(this->server_velocity_,    network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerVelocity));
253        REGISTERDATA(this->server_orientation_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
254
255        REGISTERDATA(this->server_overwrite_,   network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
256
257        REGISTERDATA(this->client_position_,    network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
258        REGISTERDATA(this->client_velocity_,    network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientVelocity));
259        REGISTERDATA(this->client_orientation_, network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
260
261        REGISTERDATA(this->client_overwrite_,   network::direction::toserver);
262
263        REGISTERDATA(this->playerID_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
264    }
265
266    void ControllableEntity::processServerPosition()
267    {
268        if (!this->bControlled_)
269            this->node_->setPosition(this->server_position_);
270    }
271
272    void ControllableEntity::processServerVelocity()
273    {
274        if (!this->bControlled_)
275            this->velocity_ = this->server_velocity_;
276    }
277
278    void ControllableEntity::processServerOrientation()
279    {
280        if (!this->bControlled_)
281            this->node_->setOrientation(this->server_orientation_);
282    }
283
284    void ControllableEntity::processOverwrite()
285    {
286        if (this->bControlled_)
287        {
288            this->setPosition(this->server_position_);
289            this->setVelocity(this->server_velocity_);
290            this->setOrientation(this->server_orientation_);
291
292            this->client_overwrite_ = this->server_overwrite_;
293        }
294    }
295
296    void ControllableEntity::processClientPosition()
297    {
298        if (this->server_overwrite_ == this->client_overwrite_)
299        {
300            COUT(2) << "callback: setting client position" << endl;
301            this->node_->setPosition(this->client_position_);
302            this->server_position_ = this->client_position_;
303        }
304    }
305
306    void ControllableEntity::processClientVelocity()
307    {
308        if (this->server_overwrite_ == this->client_overwrite_)
309        {
310            this->velocity_ = this->client_velocity_;
311            this->server_velocity_ = this->client_velocity_;
312        }
313    }
314
315    void ControllableEntity::processClientOrientation()
316    {
317        if (this->server_overwrite_ == this->client_overwrite_)
318        {
319            this->node_->setOrientation(this->client_orientation_);
320            this->server_orientation_ = this->client_orientation_;
321        }
322    }
323
324
325    void ControllableEntity::setPosition(const Vector3& position)
326    {
327        if (Core::isMaster())
328        {
329            this->node_->setPosition(position);
330            this->server_position_ = position;
331            ++this->server_overwrite_;
332        }
333        else if (this->bControlled_)
334        {
335            this->node_->setPosition(position);
336            this->client_position_ = position;
337        }
338    }
339
340    void ControllableEntity::setVelocity(const Vector3& velocity)
341    {
342        if (Core::isMaster())
343        {
344            this->velocity_ = velocity;
345            this->server_velocity_ = velocity;
346            ++this->server_overwrite_;
347        }
348        else if (this->bControlled_)
349        {
350            this->velocity_ = velocity;
351            this->client_velocity_ = velocity;
352        }
353    }
354
355    void ControllableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
356    {
357        if (Core::isMaster())
358        {
359            this->node_->translate(distance, relativeTo);
360            this->server_position_ = this->node_->getPosition();
361            ++this->server_overwrite_;
362        }
363        else if (this->bControlled_)
364        {
365            this->node_->translate(distance, relativeTo);
366            this->client_position_ = this->node_->getPosition();
367        }
368    }
369
370    void ControllableEntity::setOrientation(const Quaternion& orientation)
371    {
372        if (Core::isMaster())
373        {
374            this->node_->setOrientation(orientation);
375            this->server_orientation_ = orientation;
376            ++this->server_overwrite_;
377        }
378        else if (this->bControlled_)
379        {
380            this->node_->setOrientation(orientation);
381            this->client_orientation_ = orientation;
382        }
383    }
384
385    void ControllableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
386    {
387        if (Core::isMaster())
388        {
389            this->node_->rotate(rotation, relativeTo);
390            this->server_orientation_ = this->node_->getOrientation();
391            ++this->server_overwrite_;
392        }
393        else if (this->bControlled_)
394        {
395            this->node_->rotate(rotation, relativeTo);
396            this->client_orientation_ = this->node_->getOrientation();
397        }
398    }
399
400    void ControllableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
401    {
402        if (Core::isMaster())
403        {
404            this->node_->yaw(angle, relativeTo);
405            this->server_orientation_ = this->node_->getOrientation();
406            ++this->server_overwrite_;
407        }
408        else if (this->bControlled_)
409        {
410            this->node_->yaw(angle, relativeTo);
411            this->client_orientation_ = this->node_->getOrientation();
412        }
413    }
414
415    void ControllableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
416    {
417        if (Core::isMaster())
418        {
419            this->node_->pitch(angle, relativeTo);
420            this->server_orientation_ = this->node_->getOrientation();
421            ++this->server_overwrite_;
422        }
423        else if (this->bControlled_)
424        {
425            this->node_->pitch(angle, relativeTo);
426            this->client_orientation_ = this->node_->getOrientation();
427        }
428    }
429
430    void ControllableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
431    {
432        if (Core::isMaster())
433        {
434            this->node_->roll(angle, relativeTo);
435            this->server_orientation_ = this->node_->getOrientation();
436            ++this->server_overwrite_;
437        }
438        else if (this->bControlled_)
439        {
440            this->node_->roll(angle, relativeTo);
441            this->client_orientation_ = this->node_->getOrientation();
442        }
443    }
444
445    void ControllableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
446    {
447        if (Core::isMaster())
448        {
449            this->node_->lookAt(target, relativeTo, localDirectionVector);
450            this->server_orientation_ = this->node_->getOrientation();
451            ++this->server_overwrite_;
452        }
453        else if (this->bControlled_)
454        {
455            this->node_->lookAt(target, relativeTo, localDirectionVector);
456            this->client_orientation_ = this->node_->getOrientation();
457        }
458    }
459
460    void ControllableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
461    {
462        if (Core::isMaster())
463        {
464            this->node_->setDirection(direction, relativeTo, localDirectionVector);
465            this->server_orientation_ = this->node_->getOrientation();
466            ++this->server_overwrite_;
467        }
468        else if (this->bControlled_)
469        {
470            this->node_->setDirection(direction, relativeTo, localDirectionVector);
471            this->client_orientation_ = this->node_->getOrientation();
472        }
473    }
474}
Note: See TracBrowser for help on using the repository browser.