[1940] | 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 | |
---|
[1947] | 29 | #include "OrxonoxStableHeaders.h" |
---|
[1940] | 30 | #include "PlayerInfo.h" |
---|
| 31 | |
---|
| 32 | #include <OgreSceneManager.h> |
---|
| 33 | |
---|
| 34 | #include "core/CoreIncludes.h" |
---|
| 35 | #include "core/ConfigValueIncludes.h" |
---|
| 36 | #include "core/XMLPort.h" |
---|
[1949] | 37 | #include "core/Core.h" |
---|
[1940] | 38 | |
---|
| 39 | #include "network/Host.h" |
---|
| 40 | |
---|
| 41 | #include "GraphicsEngine.h" |
---|
| 42 | #include "objects/gametypes/Gametype.h" |
---|
[1989] | 43 | #include "objects/worldentities/ControllableEntity.h" |
---|
[1940] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | CreateUnloadableFactory(PlayerInfo); |
---|
| 48 | |
---|
| 49 | PlayerInfo::PlayerInfo() |
---|
| 50 | { |
---|
| 51 | RegisterObject(PlayerInfo); |
---|
| 52 | |
---|
| 53 | this->ping_ = -1; |
---|
[1953] | 54 | this->clientID_ = network::CLIENTID_UNKNOWN; |
---|
[1949] | 55 | this->bLocalPlayer_ = Core::isStandalone(); |
---|
[1940] | 56 | this->bLocalPlayer_ = false; |
---|
| 57 | this->bHumanPlayer_ = false; |
---|
| 58 | this->bFinishedSetup_ = false; |
---|
| 59 | |
---|
[1989] | 60 | this->pawn_ = 0; |
---|
| 61 | this->pawnID_ = network::OBJECTID_UNKNOWN; |
---|
| 62 | |
---|
[1940] | 63 | this->setConfigValues(); |
---|
| 64 | this->registerVariables(); |
---|
| 65 | |
---|
[1953] | 66 | //COUT(0) << "created PlayerInfo (" << this->getObjectID() << ")" << std::endl; |
---|
[1940] | 67 | } |
---|
| 68 | |
---|
| 69 | PlayerInfo::~PlayerInfo() |
---|
| 70 | { |
---|
| 71 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
| 72 | if (gametype) |
---|
| 73 | gametype->removePlayer(this); |
---|
[1953] | 74 | //COUT(0) << "destroyed PlayerInfo (" << this->getObjectID() << ")" << std::endl; |
---|
[1940] | 75 | } |
---|
| 76 | |
---|
| 77 | void PlayerInfo::setConfigValues() |
---|
| 78 | { |
---|
[1953] | 79 | SetConfigValue(nick_, "Player").callback(this, &PlayerInfo::checkNick); |
---|
[1940] | 80 | } |
---|
| 81 | |
---|
[1953] | 82 | void PlayerInfo::checkNick() |
---|
[1945] | 83 | { |
---|
[1953] | 84 | //std::cout << "# PI(" << this->getObjectID() << "): checkName: " << this->bLocalPlayer_ << std::endl; |
---|
| 85 | if (this->bLocalPlayer_) |
---|
| 86 | { |
---|
| 87 | this->playerName_ = this->nick_; |
---|
| 88 | |
---|
| 89 | if (Core::isMaster()) |
---|
| 90 | this->setName(this->playerName_); |
---|
| 91 | } |
---|
[1945] | 92 | } |
---|
| 93 | |
---|
[1946] | 94 | void PlayerInfo::changedName() |
---|
| 95 | { |
---|
[1953] | 96 | //std::cout << "# PI(" << this->getObjectID() << "): changedName to " << this->getName() << std::endl; |
---|
[1950] | 97 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
| 98 | if (gametype) |
---|
| 99 | gametype->playerChangedName(this); |
---|
[1946] | 100 | } |
---|
| 101 | |
---|
[1940] | 102 | void PlayerInfo::registerVariables() |
---|
| 103 | { |
---|
[1946] | 104 | REGISTERSTRING(name_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName)); |
---|
| 105 | REGISTERSTRING(playerName_, network::direction::toserver, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::clientChangedName)); |
---|
| 106 | REGISTERDATA(clientID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::checkClientID)); |
---|
| 107 | REGISTERDATA(ping_, network::direction::toclient); |
---|
| 108 | REGISTERDATA(bHumanPlayer_, network::direction::toclient); |
---|
[1989] | 109 | REGISTERDATA(pawnID_, network::direction::toclient, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::updatePawn)); |
---|
[1946] | 110 | REGISTERDATA(bFinishedSetup_, network::direction::bidirectional, new network::NetworkCallback<PlayerInfo>(this, &PlayerInfo::finishedSetup)); |
---|
| 111 | } |
---|
[1940] | 112 | |
---|
[1946] | 113 | void PlayerInfo::clientChangedName() |
---|
| 114 | { |
---|
[1953] | 115 | //std::cout << "# PI(" << this->getObjectID() << "): clientChangedName() from " << this->getName() << " to " << this->playerName_ << std::endl; |
---|
[1946] | 116 | this->setName(this->playerName_); |
---|
[1940] | 117 | } |
---|
| 118 | |
---|
| 119 | void PlayerInfo::checkClientID() |
---|
| 120 | { |
---|
[1953] | 121 | //std::cout << "# PI(" << this->getObjectID() << "): checkClientID()" << std::endl; |
---|
[1940] | 122 | this->bHumanPlayer_ = true; |
---|
| 123 | |
---|
| 124 | if (this->clientID_ == network::Host::getPlayerID()) |
---|
| 125 | { |
---|
[1953] | 126 | //std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): it's the client's ID" << std::endl; |
---|
[1940] | 127 | this->bLocalPlayer_ = true; |
---|
[1953] | 128 | this->playerName_ = this->nick_; |
---|
[1946] | 129 | //std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): name: " << this->getName() << std::endl; |
---|
[1940] | 130 | |
---|
[1949] | 131 | if (Core::isClient()) |
---|
[1940] | 132 | { |
---|
[1953] | 133 | //std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): we're on a client: set object mode to bidirectional" << std::endl; |
---|
[1946] | 134 | this->setObjectMode(network::direction::bidirectional); |
---|
[1953] | 135 | //std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): proposed name: " << this->playerName_ << std::endl; |
---|
[1946] | 136 | } |
---|
| 137 | else |
---|
| 138 | { |
---|
[1953] | 139 | //std::cout << "# PI(" << this->getObjectID() << "): checkClientID(): we're not on a client: finish setup" << std::endl; |
---|
[1946] | 140 | this->clientChangedName(); |
---|
[1940] | 141 | this->bFinishedSetup_ = true; |
---|
| 142 | this->finishedSetup(); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void PlayerInfo::finishedSetup() |
---|
| 148 | { |
---|
[1953] | 149 | //std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): " << this->bFinishedSetup_ << std::endl; |
---|
[1949] | 150 | if (Core::isClient()) |
---|
[1945] | 151 | { |
---|
[1953] | 152 | //std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a client: finish setup" << std::endl; |
---|
[1940] | 153 | this->bFinishedSetup_ = true; |
---|
[1945] | 154 | } |
---|
[1940] | 155 | else if (this->bFinishedSetup_) |
---|
| 156 | { |
---|
[1953] | 157 | //std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a server: add player" << std::endl; |
---|
[1940] | 158 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
| 159 | if (gametype) |
---|
| 160 | gametype->addPlayer(this); |
---|
| 161 | } |
---|
[1945] | 162 | else |
---|
| 163 | { |
---|
[1953] | 164 | //std::cout << "# PI(" << this->getObjectID() << "): finishedSetup(): we're a server: client not yet finished" << std::endl; |
---|
[1945] | 165 | } |
---|
[1940] | 166 | } |
---|
[1989] | 167 | |
---|
| 168 | void PlayerInfo::startControl(ControllableEntity* pawn) |
---|
| 169 | { |
---|
| 170 | pawn->setPlayer(this); |
---|
| 171 | this->pawn_ = pawn; |
---|
| 172 | this->pawnID_ = pawn->getObjectID(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void PlayerInfo::stopControl() |
---|
| 176 | { |
---|
| 177 | this->pawn_->removePlayer(); |
---|
| 178 | this->pawn_ = 0; |
---|
| 179 | this->pawnID_ = network::OBJECTID_UNKNOWN; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | void PlayerInfo::updatePawn() |
---|
| 183 | { |
---|
| 184 | this->pawn_ = dynamic_cast<ControllableEntity*>(network::Synchronisable::getSynchronisable(this->pawnID_)); |
---|
| 185 | if (this->pawn_ && (this->pawn_->getPlayer() != this)) |
---|
| 186 | this->pawn_->setPlayer(this); |
---|
| 187 | } |
---|
[1940] | 188 | } |
---|