Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2481 was 2474, checked in by rgrieder, 15 years ago

A little tweak and physics seems to work over the network ;)

  • Property svn:eol-style set to native
File size: 15.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 *      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            MobileEntity::setPosition(this->server_position_);
279    }
280
281    void ControllableEntity::processServerLinearVelocity()
282    {
283        if (!this->bControlled_)
284            MobileEntity::setVelocity(this->server_linear_velocity_);
285    }
286
287    void ControllableEntity::processServerOrientation()
288    {
289        if (!this->bControlled_)
290            MobileEntity::setOrientation(this->server_orientation_);
291    }
292
293    void ControllableEntity::processServerAngularVelocity()
294    {
295        if (!this->bControlled_)
296            MobileEntity::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            MobileEntity::setPosition(this->client_position_);
317            this->server_position_ = this->getPosition();
318        }
319    }
320
321    void ControllableEntity::processClientLinearVelocity()
322    {
323        if (this->server_overwrite_ == this->client_overwrite_)
324        {
325            MobileEntity::setVelocity(this->client_linear_velocity_);
326            this->server_linear_velocity_ = this->getVelocity();
327        }
328    }
329
330    void ControllableEntity::processClientOrientation()
331    {
332        if (this->server_overwrite_ == this->client_overwrite_)
333        {
334            MobileEntity::setOrientation(this->client_orientation_);
335            this->server_orientation_ = this->getOrientation();
336        }
337    }
338
339    void ControllableEntity::processClientAngularVelocity()
340    {
341        if (this->server_overwrite_ == this->client_overwrite_)
342        {
343            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
344            this->server_angular_velocity_ = this->getAngularVelocity();
345        }
346    }
347
348    void ControllableEntity::setPosition(const Vector3& position)
349    {
350        if (Core::isMaster())
351        {
352            MobileEntity::setPosition(position);
353            this->server_position_ = this->getPosition();
354            ++this->server_overwrite_;
355        }
356        else if (this->bControlled_)
357        {
358            MobileEntity::setPosition(position);
359            this->client_position_ = this->getPosition();
360        }
361    }
362
363    void ControllableEntity::setOrientation(const Quaternion& orientation)
364    {
365        if (Core::isMaster())
366        {
367            MobileEntity::setOrientation(orientation);
368            this->server_orientation_ = this->getOrientation();
369            ++this->server_overwrite_;
370        }
371        else if (this->bControlled_)
372        {
373            MobileEntity::setOrientation(orientation);
374            this->client_orientation_ = this->getOrientation();
375        }
376    }
377
378    void ControllableEntity::setVelocity(const Vector3& velocity)
379    {
380        if (Core::isMaster())
381        {
382            MobileEntity::setVelocity(velocity);
383            this->server_linear_velocity_ = this->getVelocity();
384            ++this->server_overwrite_;
385        }
386        else if (this->bControlled_)
387        {
388            MobileEntity::setVelocity(velocity);
389            this->client_linear_velocity_ = this->getVelocity();
390        }
391    }
392
393    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
394    {
395        if (Core::isMaster())
396        {
397            MobileEntity::setAngularVelocity(velocity);
398            this->server_angular_velocity_ = this->getAngularVelocity();
399            ++this->server_overwrite_;
400        }
401        else if (this->bControlled_)
402        {
403            MobileEntity::setAngularVelocity(velocity);
404            this->client_angular_velocity_ = this->getAngularVelocity();
405        }
406    }
407
408    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
409    {
410        MobileEntity::setWorldTransform(worldTrans);
411        if (Core::isMaster())
412        {
413            this->server_position_ = this->getPosition();
414            this->server_orientation_ = this->getOrientation();
415            this->server_linear_velocity_ = this->getVelocity();
416            this->server_angular_velocity_ = this->getAngularVelocity();
417        }
418        else if (this->bControlled_)
419        {
420            this->client_position_ = this->getPosition();
421            this->client_orientation_ = this->getOrientation();
422            this->client_linear_velocity_ = this->getVelocity();
423            this->client_angular_velocity_ = this->getAngularVelocity();
424        }
425    }
426}
Note: See TracBrowser for help on using the repository browser.