Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/orxonox/objects/worldentities/ControllableEntity.cc @ 2518

Last change on this file since 2518 was 2442, checked in by rgrieder, 16 years ago

Finally merged physics stuff. Target is physics_merge because I'll have to do some testing first.

  • Property svn:eol-style set to native
File size: 15.3 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 "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) : MobileEntity(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->server_position_         = Vector3::ZERO;
60        this->client_position_         = Vector3::ZERO;
61        this->server_linear_velocity_  = Vector3::ZERO;
62        this->client_linear_velocity_  = Vector3::ZERO;
63        this->server_orientation_      = Quaternion::IDENTITY;
64        this->client_orientation_      = Quaternion::IDENTITY;
65        this->server_angular_velocity_ = Vector3::ZERO;
66        this->client_angular_velocity_ = Vector3::ZERO;
67
68
69        this->setPriority( priority::very_high );
70        this->registerVariables();
71    }
72
73    ControllableEntity::~ControllableEntity()
74    {
75        if (this->isInitialized())
76        {
77            if (this->bControlled_)
78                this->stopLocalControl();
79
80            if (this->hud_)
81                delete this->hud_;
82
83            if (this->camera_)
84                delete this->camera_;
85
86            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
87                this->getPlayer()->stopControl(this, false);
88        }
89    }
90
91    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
92    {
93        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
94
95        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
96        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
97
98        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
99    }
100
101    void ControllableEntity::addCameraPosition(CameraPosition* position)
102    {
103        this->attach(position);
104        this->cameraPositions_.push_back(position);
105    }
106
107    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
108    {
109        unsigned int i = 0;
110        for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
111        {
112            if (i == index)
113                return (*it);
114            ++i;
115        }
116        return 0;
117    }
118
119    void ControllableEntity::switchCamera()
120    {
121        if (this->camera_)
122        {
123            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
124            {
125                this->cameraPositions_.front()->attachCamera(this->camera_);
126            }
127            else if (this->cameraPositions_.size() > 0)
128            {
129                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
130                {
131                    if ((*it) == this->camera_->getParent())
132                    {
133                        ++it;
134                        if (it != this->cameraPositions_.end())
135                            (*it)->attachCamera(this->camera_);
136                        else
137                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
138                        break;
139                    }
140                }
141            }
142            else
143            {
144                this->attach(this->camera_);
145            }
146        }
147    }
148
149    void ControllableEntity::setPlayer(PlayerInfo* player)
150    {
151        if (!player)
152        {
153            this->removePlayer();
154            return;
155        }
156
157        this->player_ = player;
158        this->playerID_ = player->getObjectID();
159        this->bControlled_ = (player->isLocalPlayer() && player->isHumanPlayer());
160        if (this->bControlled_)
161        {
162            this->startLocalControl();
163
164            if (!Core::isMaster())
165            {
166                this->client_overwrite_ = this->server_overwrite_;
167COUT(0) << "CE: bidirectional synchronization" << std::endl;
168                this->setObjectMode(objectDirection::bidirectional);
169            }
170        }
171    }
172
173    void ControllableEntity::removePlayer()
174    {
175        if (this->bControlled_)
176            this->stopLocalControl();
177
178        this->player_ = 0;
179        this->playerID_ = OBJECTID_UNKNOWN;
180        this->bControlled_ = false;
181        this->setObjectMode(objectDirection::toclient);
182
183        if (this->bDestroyWhenPlayerLeft_)
184            delete this;
185    }
186
187    void ControllableEntity::networkcallback_changedplayerID()
188    {
189        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
190        if (this->playerID_ != OBJECTID_UNKNOWN)
191        {
192            this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
193            if (this->player_ && (this->player_->getControllableEntity() != this))
194                this->player_->startControl(this);
195        }
196    }
197
198    void ControllableEntity::startLocalControl()
199    {
200//        std::cout << this->getObjectID() << " ###### start local control" << std::endl;
201        this->camera_ = new Camera(this);
202        this->camera_->requestFocus();
203        if (this->cameraPositionTemplate_ != "")
204            this->addTemplate(this->cameraPositionTemplate_);
205        if (this->cameraPositions_.size() > 0)
206            this->cameraPositions_.front()->attachCamera(this->camera_);
207        else
208            this->attach(this->camera_);
209
210        if (this->hudtemplate_ != "")
211        {
212            this->hud_ = new OverlayGroup(this);
213            this->hud_->addTemplate(this->hudtemplate_);
214        }
215    }
216
217    void ControllableEntity::stopLocalControl()
218    {
219//        std::cout << "###### stop local control" << std::endl;
220        this->camera_->detachFromParent();
221        delete this->camera_;
222        this->camera_ = 0;
223
224        delete this->hud_;
225        this->hud_ = 0;
226    }
227
228    void ControllableEntity::tick(float dt)
229    {
230        MobileEntity::tick(dt);
231
232        if (this->isActive())
233        {
234            // Check whether Bullet doesn't do the physics for us
235            if (!this->isDynamic())
236            {
237                if (Core::isMaster())
238                {
239                    this->server_position_ = this->getPosition();
240                    this->server_orientation_ = this->getOrientation();
241                    this->server_linear_velocity_ = this->getVelocity();
242                    this->server_angular_velocity_ = this->getAngularVelocity();
243                }
244                else if (this->bControlled_)
245                {
246                    this->client_position_ = this->getPosition();
247                    this->client_orientation_ = this->getOrientation();
248                    this->client_linear_velocity_ = this->getVelocity();
249                    this->client_angular_velocity_ = this->getAngularVelocity();
250                }
251            }
252        }
253    }
254
255    void ControllableEntity::registerVariables()
256    {
257        registerVariable(this->cameraPositionTemplate_, variableDirection::toclient);
258
259        registerVariable(this->server_position_,         variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
260        registerVariable(this->server_linear_velocity_,  variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
261        registerVariable(this->server_orientation_,      variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
262        registerVariable(this->server_angular_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
263
264        registerVariable(this->server_overwrite_,        variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
265        registerVariable(this->client_overwrite_,        variableDirection::toserver);
266
267        registerVariable(this->client_position_,         variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
268        registerVariable(this->client_linear_velocity_,  variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
269        registerVariable(this->client_orientation_,      variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
270        registerVariable(this->client_angular_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
271
272        registerVariable(this->playerID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
273    }
274
275    void ControllableEntity::processServerPosition()
276    {
277        if (!this->bControlled_)
278            this->setPosition(this->server_position_);
279    }
280
281    void ControllableEntity::processServerLinearVelocity()
282    {
283        if (!this->bControlled_)
284            this->setVelocity(this->server_linear_velocity_);
285    }
286
287    void ControllableEntity::processServerOrientation()
288    {
289        if (!this->bControlled_)
290            this->setOrientation(this->server_orientation_);
291    }
292
293    void ControllableEntity::processServerAngularVelocity()
294    {
295        if (!this->bControlled_)
296            this->setAngularVelocity(this->server_angular_velocity_);
297    }
298
299    void ControllableEntity::processOverwrite()
300    {
301        if (this->bControlled_)
302        {
303            this->setPosition(this->server_position_);
304            this->setOrientation(this->server_orientation_);
305            this->setVelocity(this->server_linear_velocity_);
306            this->setAngularVelocity(this->server_angular_velocity_);
307
308            this->client_overwrite_ = this->server_overwrite_;
309        }
310    }
311
312    void ControllableEntity::processClientPosition()
313    {
314        if (this->server_overwrite_ == this->client_overwrite_)
315        {
316            this->setPosition(this->client_position_);
317            // The call above increments the overwrite. This is not desired to reduce traffic
318            --this->server_overwrite_;
319        }
320    }
321
322    void ControllableEntity::processClientLinearVelocity()
323    {
324        if (this->server_overwrite_ == this->client_overwrite_)
325        {
326            this->setVelocity(this->client_linear_velocity_);
327            // The call above increments the overwrite. This is not desired to reduce traffic
328            --this->server_overwrite_;
329        }
330    }
331
332    void ControllableEntity::processClientOrientation()
333    {
334        if (this->server_overwrite_ == this->client_overwrite_)
335        {
336            this->setOrientation(this->client_orientation_);
337            // The call above increments the overwrite. This is not desired to reduce traffic
338            --this->server_overwrite_;
339        }
340    }
341
342    void ControllableEntity::processClientAngularVelocity()
343    {
344        if (this->server_overwrite_ == this->client_overwrite_)
345        {
346            this->setAngularVelocity(this->client_angular_velocity_);
347            // The call above increments the overwrite. This is not desired to reduce traffic
348            --this->server_overwrite_;
349        }
350    }
351
352    void ControllableEntity::setPosition(const Vector3& position)
353    {
354        if (Core::isMaster())
355        {
356            MobileEntity::setPosition(position);
357            this->server_position_ = this->getPosition();
358            ++this->server_overwrite_;
359        }
360        else if (this->bControlled_)
361        {
362            MobileEntity::setPosition(position);
363            this->client_position_ = this->getPosition();
364        }
365    }
366
367    void ControllableEntity::setOrientation(const Quaternion& orientation)
368    {
369        if (Core::isMaster())
370        {
371            MobileEntity::setOrientation(orientation);
372            this->server_orientation_ = this->getOrientation();
373            ++this->server_overwrite_;
374        }
375        else if (this->bControlled_)
376        {
377            MobileEntity::setOrientation(orientation);
378            this->client_orientation_ = this->getOrientation();
379        }
380    }
381
382    void ControllableEntity::setVelocity(const Vector3& velocity)
383    {
384        if (Core::isMaster())
385        {
386            MobileEntity::setVelocity(velocity);
387            this->server_linear_velocity_ = this->getVelocity();
388            ++this->server_overwrite_;
389        }
390        else if (this->bControlled_)
391        {
392            MobileEntity::setVelocity(velocity);
393            this->client_linear_velocity_ = this->getVelocity();
394        }
395    }
396
397    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
398    {
399        if (Core::isMaster())
400        {
401            MobileEntity::setAngularVelocity(velocity);
402            this->server_angular_velocity_ = this->getAngularVelocity();
403            ++this->server_overwrite_;
404        }
405        else if (this->bControlled_)
406        {
407            MobileEntity::setAngularVelocity(velocity);
408            this->client_angular_velocity_ = this->getAngularVelocity();
409        }
410    }
411
412    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
413    {
414        MobileEntity::setWorldTransform(worldTrans);
415        if (Core::isMaster())
416        {
417            this->server_position_ = this->getPosition();
418            this->server_orientation_ = this->getOrientation();
419            this->server_linear_velocity_ = this->getVelocity();
420            this->server_angular_velocity_ = this->getAngularVelocity();
421        }
422        else if (this->bControlled_)
423        {
424            this->client_position_ = this->getPosition();
425            this->client_orientation_ = this->getOrientation();
426            this->client_linear_velocity_ = this->getVelocity();
427            this->client_angular_velocity_ = this->getAngularVelocity();
428        }
429    }
430}
Note: See TracBrowser for help on using the repository browser.