[2019] | 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 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "HumanPlayer.h" |
---|
| 31 | |
---|
| 32 | #include "core/Core.h" |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/ConfigValueIncludes.h" |
---|
| 35 | #include "network/ClientInformation.h" |
---|
| 36 | #include "network/Host.h" |
---|
| 37 | #include "objects/controllers/HumanController.h" |
---|
| 38 | #include "objects/gametypes/Gametype.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | CreateUnloadableFactory(HumanPlayer); |
---|
| 43 | |
---|
| 44 | HumanPlayer::HumanPlayer(BaseObject* creator) : PlayerInfo(creator) |
---|
| 45 | { |
---|
| 46 | RegisterObject(HumanPlayer); |
---|
| 47 | |
---|
| 48 | this->server_ready_ = Core::isMaster(); |
---|
| 49 | this->client_ready_ = false; |
---|
| 50 | |
---|
| 51 | this->bHumanPlayer_ = true; |
---|
| 52 | this->defaultController_ = Class(HumanController); |
---|
| 53 | |
---|
| 54 | this->setConfigValues(); |
---|
| 55 | this->registerVariables(); |
---|
| 56 | |
---|
| 57 | COUT(0) << "HumanPlayer created" << std::endl; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | HumanPlayer::~HumanPlayer() |
---|
| 61 | { |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void HumanPlayer::setConfigValues() |
---|
| 65 | { |
---|
| 66 | SetConfigValue(nick_, "Player").callback(this, &HumanPlayer::configvaluecallback_changednick); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void HumanPlayer::registerVariables() |
---|
| 70 | { |
---|
| 71 | REGISTERSTRING(this->synchronize_nick_, network::direction::toserver, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_changednick)); |
---|
| 72 | |
---|
| 73 | REGISTERDATA(this->clientID_, network::direction::toclient, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_clientIDchanged)); |
---|
| 74 | REGISTERDATA(this->server_ready_, network::direction::toclient, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_server_ready)); |
---|
| 75 | REGISTERDATA(this->client_ready_, network::direction::toserver, new network::NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_client_ready)); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void HumanPlayer::configvaluecallback_changednick() |
---|
| 79 | { |
---|
| 80 | if (this->isLocalPlayer()) |
---|
| 81 | { |
---|
| 82 | this->synchronize_nick_ = this->nick_; |
---|
| 83 | |
---|
| 84 | if (Core::isMaster()) |
---|
| 85 | this->setName(this->nick_); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void HumanPlayer::networkcallback_changednick() |
---|
| 90 | { |
---|
| 91 | this->setName(this->synchronize_nick_); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void HumanPlayer::networkcallback_clientIDchanged() |
---|
| 95 | { |
---|
| 96 | if (this->clientID_ == network::Host::getPlayerID()) |
---|
| 97 | { |
---|
| 98 | this->bLocalPlayer_ = true; |
---|
| 99 | this->synchronize_nick_ = this->nick_; |
---|
| 100 | this->client_ready_ = true; |
---|
| 101 | |
---|
| 102 | if (!Core::isMaster()) |
---|
| 103 | this->setObjectMode(network::direction::bidirectional); |
---|
| 104 | else |
---|
| 105 | this->setName(this->nick_); |
---|
| 106 | |
---|
| 107 | this->createController(); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void HumanPlayer::networkcallback_server_ready() |
---|
| 112 | { |
---|
| 113 | this->client_ready_ = true; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void HumanPlayer::networkcallback_client_ready() |
---|
| 117 | { |
---|
| 118 | if (this->getGametype()) |
---|
| 119 | this->getGametype()->playerEntered(this); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | bool HumanPlayer::isReady() const |
---|
| 123 | { |
---|
| 124 | return (this->server_ready_ && this->client_ready_); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | float HumanPlayer::getPing() const |
---|
| 128 | { |
---|
| 129 | return network::ClientInformation::findClient(this->getClientID())->getRTT(); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | float HumanPlayer::getPacketLossRatio() const |
---|
| 133 | { |
---|
| 134 | return network::ClientInformation::findClient(this->getClientID())->getPacketLoss(); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void HumanPlayer::setClientID(unsigned int clientID) |
---|
| 138 | { |
---|
| 139 | this->clientID_ = clientID; |
---|
| 140 | this->networkcallback_clientIDchanged(); |
---|
| 141 | } |
---|
| 142 | } |
---|