Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/ControllableEntity.cc @ 2407

Last change on this file since 2407 was 2407, checked in by rgrieder, 15 years ago
  • Removed debug output
  • Fixed a bug with parentID_ in CollisionShape
  • Reactivated overwrite mechanism in ControllableEntity
  • Build fix in WE for last commit.
  • Property svn:eol-style set to native
File size: 13.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 "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) : MovableEntity(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->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        this->registerVariables();
69    }
70
71    ControllableEntity::~ControllableEntity()
72    {
73        if (this->isInitialized())
74        {
75            if (this->bControlled_)
76                this->stopLocalControl();
77
78            if (this->hud_)
79                delete this->hud_;
80
81            if (this->camera_)
82                delete this->camera_;
83
84            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
85                this->getPlayer()->stopControl(this, false);
86        }
87    }
88
89    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
90    {
91        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
92
93        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
94        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
95
96        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
97    }
98
99    void ControllableEntity::addCameraPosition(CameraPosition* position)
100    {
101        this->attach(position);
102        this->cameraPositions_.push_back(position);
103    }
104
105    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
106    {
107        unsigned int i = 0;
108        for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
109        {
110            if (i == index)
111                return (*it);
112            ++i;
113        }
114        return 0;
115    }
116
117    void ControllableEntity::switchCamera()
118    {
119        if (this->camera_)
120        {
121            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
122            {
123                this->cameraPositions_.front()->attachCamera(this->camera_);
124            }
125            else if (this->cameraPositions_.size() > 0)
126            {
127                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
128                {
129                    if ((*it) == this->camera_->getParent())
130                    {
131                        ++it;
132                        if (it != this->cameraPositions_.end())
133                            (*it)->attachCamera(this->camera_);
134                        else
135                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
136                        break;
137                    }
138                }
139            }
140            else
141            {
142                this->attach(this->camera_);
143            }
144        }
145    }
146
147    void ControllableEntity::setPlayer(PlayerInfo* player)
148    {
149        if (!player)
150        {
151            this->removePlayer();
152            return;
153        }
154
155        this->player_ = player;
156        this->playerID_ = player->getObjectID();
157        this->bControlled_ = (player->isLocalPlayer() && player->isHumanPlayer());
158        if (this->bControlled_)
159        {
160            this->startLocalControl();
161
162            if (!Core::isMaster())
163            {
164                this->client_overwrite_ = this->server_overwrite_;
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        MovableEntity::tick(dt);
229
230        if (this->isActive())
231        {
232        }
233    }
234
235    void ControllableEntity::registerVariables()
236    {
237        REGISTERSTRING(this->cameraPositionTemplate_, network::direction::toclient);
238
239        REGISTERDATA(this->server_position_,         network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
240        REGISTERDATA(this->server_linear_velocity_,  network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
241        REGISTERDATA(this->server_orientation_,      network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
242        REGISTERDATA(this->server_angular_velocity_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
243
244        REGISTERDATA(this->server_overwrite_,        network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
245        REGISTERDATA(this->client_overwrite_,        network::direction::toserver);
246
247        REGISTERDATA(this->client_position_,         network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
248        REGISTERDATA(this->client_linear_velocity_,  network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
249        REGISTERDATA(this->client_orientation_,      network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
250        REGISTERDATA(this->client_angular_velocity_, network::direction::toserver, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
251
252        REGISTERDATA(this->playerID_, network::direction::toclient, new network::NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
253    }
254
255    void ControllableEntity::processServerPosition()
256    {
257        if (!this->bControlled_)
258            this->setPosition(this->server_position_);
259    }
260
261    void ControllableEntity::processServerLinearVelocity()
262    {
263        if (!this->bControlled_)
264            this->setVelocity(this->server_linear_velocity_);
265    }
266
267    void ControllableEntity::processServerOrientation()
268    {
269        if (!this->bControlled_)
270            this->setOrientation(this->server_orientation_);
271    }
272
273    void ControllableEntity::processServerAngularVelocity()
274    {
275        if (!this->bControlled_)
276            this->setAngularVelocity(this->server_angular_velocity_);
277    }
278
279    void ControllableEntity::processOverwrite()
280    {
281        if (this->bControlled_)
282        {
283            this->setPosition(this->server_position_);
284            this->setOrientation(this->server_orientation_);
285            this->setVelocity(this->server_linear_velocity_);
286            this->setAngularVelocity(this->server_angular_velocity_);
287
288            this->client_overwrite_ = this->server_overwrite_;
289        }
290    }
291
292    void ControllableEntity::processClientPosition()
293    {
294        if (this->server_overwrite_ == this->client_overwrite_)
295        {
296            this->setPosition(this->client_position_);
297            // The call above increments the overwrite. This is not desired to reduce traffic
298            --this->server_overwrite_;
299        }
300    }
301
302    void ControllableEntity::processClientLinearVelocity()
303    {
304        if (this->server_overwrite_ == this->client_overwrite_)
305        {
306            this->setVelocity(this->client_linear_velocity_);
307            // The call above increments the overwrite. This is not desired to reduce traffic
308            --this->server_overwrite_;
309        }
310    }
311
312    void ControllableEntity::processClientOrientation()
313    {
314        if (this->server_overwrite_ == this->client_overwrite_)
315        {
316            this->setOrientation(this->client_orientation_);
317            // The call above increments the overwrite. This is not desired to reduce traffic
318            --this->server_overwrite_;
319        }
320    }
321
322    void ControllableEntity::processClientAngularVelocity()
323    {
324        if (this->server_overwrite_ == this->client_overwrite_)
325        {
326            this->setAngularVelocity(this->client_angular_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::positionChanged(bool bContinuous)
333    {
334        if (Core::isMaster())
335        {
336            this->server_position_ = this->getPosition();
337            if (!bContinuous)
338                ++this->server_overwrite_;
339        }
340        else if (this->bControlled_)
341        {
342            this->client_position_ = this->getPosition();
343        }
344    }
345
346    void ControllableEntity::orientationChanged(bool bContinuous)
347    {
348        if (Core::isMaster())
349        {
350            this->server_orientation_ = this->getOrientation();
351            if (!bContinuous)
352                ++this->server_overwrite_;
353        }
354        else if (this->bControlled_)
355        {
356            this->client_orientation_ = this->getOrientation();
357        }
358    }
359
360    void ControllableEntity::linearVelocityChanged(bool bContinuous)
361    {
362        if (Core::isMaster())
363        {
364            this->server_linear_velocity_ = this->getVelocity();
365            if (!bContinuous)
366                ++this->server_overwrite_;
367        }
368        else if (this->bControlled_)
369        {
370            this->client_linear_velocity_ = this->getVelocity();
371        }
372    }
373
374    void ControllableEntity::angularVelocityChanged(bool bContinuous)
375    {
376        if (Core::isMaster())
377        {
378            this->server_angular_velocity_ = this->getAngularVelocity();
379            if (!bContinuous)
380                ++this->server_overwrite_;
381        }
382        else if (this->bControlled_)
383        {
384            this->client_angular_velocity_ = this->getAngularVelocity();
385        }
386    }
387}
Note: See TracBrowser for help on using the repository browser.