[2072] | 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: |
---|
[2459] | 25 | * Reto Grieder |
---|
[2072] | 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 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | CreateFactory(ControllableEntity); |
---|
| 45 | |
---|
[2459] | 46 | ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator) |
---|
[2072] | 47 | { |
---|
| 48 | RegisterObject(ControllableEntity); |
---|
| 49 | |
---|
| 50 | this->bControlled_ = false; |
---|
| 51 | this->server_overwrite_ = 0; |
---|
| 52 | this->client_overwrite_ = 0; |
---|
| 53 | this->player_ = 0; |
---|
[2171] | 54 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 55 | this->hud_ = 0; |
---|
| 56 | this->camera_ = 0; |
---|
| 57 | this->bDestroyWhenPlayerLeft_ = false; |
---|
| 58 | |
---|
[2459] | 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; |
---|
[2072] | 67 | |
---|
[2459] | 68 | |
---|
[2415] | 69 | this->setPriority( priority::very_high ); |
---|
[2072] | 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_; |
---|
| 167 | COUT(0) << "CE: bidirectional synchronization" << std::endl; |
---|
[2371] | 168 | this->setObjectMode(objectDirection::bidirectional); |
---|
[2072] | 169 | } |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void ControllableEntity::removePlayer() |
---|
| 174 | { |
---|
| 175 | if (this->bControlled_) |
---|
| 176 | this->stopLocalControl(); |
---|
| 177 | |
---|
| 178 | this->player_ = 0; |
---|
[2171] | 179 | this->playerID_ = OBJECTID_UNKNOWN; |
---|
[2072] | 180 | this->bControlled_ = false; |
---|
[2371] | 181 | this->setObjectMode(objectDirection::toclient); |
---|
[2072] | 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 |
---|
[2171] | 190 | if (this->playerID_ != OBJECTID_UNKNOWN) |
---|
[2072] | 191 | { |
---|
[2171] | 192 | this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_)); |
---|
[2072] | 193 | if (this->player_ && (this->player_->getControllableEntity() != this)) |
---|
| 194 | this->player_->startControl(this); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | void ControllableEntity::startLocalControl() |
---|
| 199 | { |
---|
[2086] | 200 | // std::cout << this->getObjectID() << " ###### start local control" << std::endl; |
---|
[2072] | 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 | { |
---|
[2086] | 219 | // std::cout << "###### stop local control" << std::endl; |
---|
[2072] | 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 | { |
---|
[2459] | 230 | MobileEntity::tick(dt); |
---|
| 231 | |
---|
[2072] | 232 | if (this->isActive()) |
---|
| 233 | { |
---|
[2459] | 234 | // Check whether Bullet doesn't do the physics for us |
---|
| 235 | if (!this->isDynamic()) |
---|
[2072] | 236 | { |
---|
[2459] | 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 | } |
---|
[2072] | 251 | } |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | void ControllableEntity::registerVariables() |
---|
| 256 | { |
---|
[2371] | 257 | registerVariable(this->cameraPositionTemplate_, variableDirection::toclient); |
---|
[2459] | 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)); |
---|
[2072] | 273 | } |
---|
| 274 | |
---|
| 275 | void ControllableEntity::processServerPosition() |
---|
| 276 | { |
---|
| 277 | if (!this->bControlled_) |
---|
[2459] | 278 | this->setPosition(this->server_position_); |
---|
[2072] | 279 | } |
---|
| 280 | |
---|
[2459] | 281 | void ControllableEntity::processServerLinearVelocity() |
---|
[2072] | 282 | { |
---|
| 283 | if (!this->bControlled_) |
---|
[2459] | 284 | this->setVelocity(this->server_linear_velocity_); |
---|
[2072] | 285 | } |
---|
| 286 | |
---|
| 287 | void ControllableEntity::processServerOrientation() |
---|
| 288 | { |
---|
| 289 | if (!this->bControlled_) |
---|
[2459] | 290 | this->setOrientation(this->server_orientation_); |
---|
[2072] | 291 | } |
---|
| 292 | |
---|
[2459] | 293 | void ControllableEntity::processServerAngularVelocity() |
---|
| 294 | { |
---|
| 295 | if (!this->bControlled_) |
---|
| 296 | this->setAngularVelocity(this->server_angular_velocity_); |
---|
| 297 | } |
---|
| 298 | |
---|
[2072] | 299 | void ControllableEntity::processOverwrite() |
---|
| 300 | { |
---|
| 301 | if (this->bControlled_) |
---|
| 302 | { |
---|
| 303 | this->setPosition(this->server_position_); |
---|
| 304 | this->setOrientation(this->server_orientation_); |
---|
[2459] | 305 | this->setVelocity(this->server_linear_velocity_); |
---|
| 306 | this->setAngularVelocity(this->server_angular_velocity_); |
---|
[2072] | 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 | { |
---|
[2459] | 316 | this->setPosition(this->client_position_); |
---|
| 317 | // The call above increments the overwrite. This is not desired to reduce traffic |
---|
| 318 | --this->server_overwrite_; |
---|
[2072] | 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[2459] | 322 | void ControllableEntity::processClientLinearVelocity() |
---|
[2072] | 323 | { |
---|
| 324 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 325 | { |
---|
[2459] | 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_; |
---|
[2072] | 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | void ControllableEntity::processClientOrientation() |
---|
| 333 | { |
---|
| 334 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
| 335 | { |
---|
[2459] | 336 | this->setOrientation(this->client_orientation_); |
---|
| 337 | // The call above increments the overwrite. This is not desired to reduce traffic |
---|
| 338 | --this->server_overwrite_; |
---|
[2072] | 339 | } |
---|
| 340 | } |
---|
| 341 | |
---|
[2459] | 342 | void ControllableEntity::processClientAngularVelocity() |
---|
[2072] | 343 | { |
---|
[2459] | 344 | if (this->server_overwrite_ == this->client_overwrite_) |
---|
[2072] | 345 | { |
---|
[2459] | 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_; |
---|
[2072] | 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
[2459] | 352 | void ControllableEntity::setPosition(const Vector3& position) |
---|
[2072] | 353 | { |
---|
| 354 | if (Core::isMaster()) |
---|
| 355 | { |
---|
[2459] | 356 | MobileEntity::setPosition(position); |
---|
| 357 | this->server_position_ = this->getPosition(); |
---|
[2072] | 358 | ++this->server_overwrite_; |
---|
| 359 | } |
---|
| 360 | else if (this->bControlled_) |
---|
| 361 | { |
---|
[2459] | 362 | MobileEntity::setPosition(position); |
---|
| 363 | this->client_position_ = this->getPosition(); |
---|
[2072] | 364 | } |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | void ControllableEntity::setOrientation(const Quaternion& orientation) |
---|
| 368 | { |
---|
| 369 | if (Core::isMaster()) |
---|
| 370 | { |
---|
[2459] | 371 | MobileEntity::setOrientation(orientation); |
---|
| 372 | this->server_orientation_ = this->getOrientation(); |
---|
[2072] | 373 | ++this->server_overwrite_; |
---|
| 374 | } |
---|
| 375 | else if (this->bControlled_) |
---|
| 376 | { |
---|
[2459] | 377 | MobileEntity::setOrientation(orientation); |
---|
| 378 | this->client_orientation_ = this->getOrientation(); |
---|
[2072] | 379 | } |
---|
| 380 | } |
---|
| 381 | |
---|
[2459] | 382 | void ControllableEntity::setVelocity(const Vector3& velocity) |
---|
[2072] | 383 | { |
---|
| 384 | if (Core::isMaster()) |
---|
| 385 | { |
---|
[2459] | 386 | MobileEntity::setVelocity(velocity); |
---|
| 387 | this->server_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 388 | ++this->server_overwrite_; |
---|
| 389 | } |
---|
| 390 | else if (this->bControlled_) |
---|
| 391 | { |
---|
[2459] | 392 | MobileEntity::setVelocity(velocity); |
---|
| 393 | this->client_linear_velocity_ = this->getVelocity(); |
---|
[2072] | 394 | } |
---|
| 395 | } |
---|
| 396 | |
---|
[2459] | 397 | void ControllableEntity::setAngularVelocity(const Vector3& velocity) |
---|
[2072] | 398 | { |
---|
| 399 | if (Core::isMaster()) |
---|
| 400 | { |
---|
[2459] | 401 | MobileEntity::setAngularVelocity(velocity); |
---|
| 402 | this->server_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 403 | ++this->server_overwrite_; |
---|
| 404 | } |
---|
| 405 | else if (this->bControlled_) |
---|
| 406 | { |
---|
[2459] | 407 | MobileEntity::setAngularVelocity(velocity); |
---|
| 408 | this->client_angular_velocity_ = this->getAngularVelocity(); |
---|
[2072] | 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
[2459] | 412 | void ControllableEntity::setWorldTransform(const btTransform& worldTrans) |
---|
[2072] | 413 | { |
---|
[2459] | 414 | MobileEntity::setWorldTransform(worldTrans); |
---|
[2072] | 415 | if (Core::isMaster()) |
---|
| 416 | { |
---|
[2459] | 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(); |
---|
[2072] | 421 | } |
---|
| 422 | else if (this->bControlled_) |
---|
| 423 | { |
---|
[2459] | 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(); |
---|
[2072] | 428 | } |
---|
| 429 | } |
---|
| 430 | } |
---|