| 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 "HumanPlayer.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "core/ConfigValueIncludes.h" | 
|---|
| 33 | #include "core/GameMode.h" | 
|---|
| 34 | #include "network/ClientInformation.h" | 
|---|
| 35 | #include "network/Host.h" | 
|---|
| 36 | #include "controllers/HumanController.h" | 
|---|
| 37 | #include "controllers/NewHumanController.h" | 
|---|
| 38 | #include "gametypes/Gametype.h" | 
|---|
| 39 | #include "overlays/OverlayGroup.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 |     CreateUnloadableFactory(HumanPlayer); | 
|---|
| 44 |  | 
|---|
| 45 |     HumanPlayer::HumanPlayer(BaseObject* creator) : PlayerInfo(creator) | 
|---|
| 46 |     { | 
|---|
| 47 |         RegisterObject(HumanPlayer); | 
|---|
| 48 |  | 
|---|
| 49 |         this->server_initialized_ = GameMode::isMaster(); | 
|---|
| 50 |         this->client_initialized_ = false; | 
|---|
| 51 |  | 
|---|
| 52 |         this->bHumanPlayer_ = true; | 
|---|
| 53 |         this->defaultController_ = Class(NewHumanController); | 
|---|
| 54 |  | 
|---|
| 55 |         this->humanHud_ = 0; | 
|---|
| 56 |         this->gametypeHud_ = 0; | 
|---|
| 57 |  | 
|---|
| 58 |         this->setConfigValues(); | 
|---|
| 59 |         this->registerVariables(); | 
|---|
| 60 |     } | 
|---|
| 61 |  | 
|---|
| 62 |     HumanPlayer::~HumanPlayer() | 
|---|
| 63 |     { | 
|---|
| 64 |         if (this->BaseObject::isInitialized()) | 
|---|
| 65 |         { | 
|---|
| 66 |             if (this->humanHud_) | 
|---|
| 67 |                 this->humanHud_->destroy(); | 
|---|
| 68 |  | 
|---|
| 69 |             if (this->gametypeHud_) | 
|---|
| 70 |                 this->gametypeHud_->destroy(); | 
|---|
| 71 |         } | 
|---|
| 72 |     } | 
|---|
| 73 |  | 
|---|
| 74 |     void HumanPlayer::setConfigValues() | 
|---|
| 75 |     { | 
|---|
| 76 |         SetConfigValue(nick_, "Player").callback(this, &HumanPlayer::configvaluecallback_changednick); | 
|---|
| 77 |         SetConfigValue(hudtemplate_, "defaultHUD").callback(this, &HumanPlayer::configvaluecallback_changedHUDTemplate); | 
|---|
| 78 |     } | 
|---|
| 79 |  | 
|---|
| 80 |     void HumanPlayer::registerVariables() | 
|---|
| 81 |     { | 
|---|
| 82 |         registerVariable(this->synchronize_nick_, VariableDirection::ToServer, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_changednick)); | 
|---|
| 83 |  | 
|---|
| 84 |         registerVariable(this->clientID_,           VariableDirection::ToClient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_clientIDchanged)); | 
|---|
| 85 |         registerVariable(this->server_initialized_, VariableDirection::ToClient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_server_initialized)); | 
|---|
| 86 |         registerVariable(this->client_initialized_, VariableDirection::ToServer, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_client_initialized)); | 
|---|
| 87 |     } | 
|---|
| 88 |  | 
|---|
| 89 |     void HumanPlayer::configvaluecallback_changednick() | 
|---|
| 90 |     { | 
|---|
| 91 |         if (this->isLocalPlayer()) | 
|---|
| 92 |         { | 
|---|
| 93 |             this->synchronize_nick_ = this->nick_; | 
|---|
| 94 |  | 
|---|
| 95 |             if (GameMode::isMaster()) | 
|---|
| 96 |                 this->setName(this->nick_); | 
|---|
| 97 |         } | 
|---|
| 98 |     } | 
|---|
| 99 |  | 
|---|
| 100 |     void HumanPlayer::configvaluecallback_changedHUDTemplate() | 
|---|
| 101 |     { | 
|---|
| 102 |         this->setHumanHUDTemplate(this->hudtemplate_); | 
|---|
| 103 |     } | 
|---|
| 104 |  | 
|---|
| 105 |     void HumanPlayer::networkcallback_changednick() | 
|---|
| 106 |     { | 
|---|
| 107 |         this->setName(this->synchronize_nick_); | 
|---|
| 108 |     } | 
|---|
| 109 |  | 
|---|
| 110 |     void HumanPlayer::networkcallback_clientIDchanged() | 
|---|
| 111 |     { | 
|---|
| 112 |         if (this->clientID_ == Host::getPlayerID()) | 
|---|
| 113 |         { | 
|---|
| 114 |             this->bLocalPlayer_ = true; | 
|---|
| 115 |             this->synchronize_nick_ = this->nick_; | 
|---|
| 116 |             this->client_initialized_ = true; | 
|---|
| 117 |  | 
|---|
| 118 |             if (!GameMode::isMaster()) | 
|---|
| 119 |                 this->setSyncMode(ObjectDirection::Bidirectional); | 
|---|
| 120 |             else | 
|---|
| 121 |                 this->setName(this->nick_); | 
|---|
| 122 |  | 
|---|
| 123 |             this->createController(); | 
|---|
| 124 |             this->updateHumanHUD(); | 
|---|
| 125 |         } | 
|---|
| 126 |     } | 
|---|
| 127 |  | 
|---|
| 128 |     void HumanPlayer::networkcallback_server_initialized() | 
|---|
| 129 |     { | 
|---|
| 130 |         this->client_initialized_ = true; | 
|---|
| 131 |     } | 
|---|
| 132 |  | 
|---|
| 133 |     void HumanPlayer::networkcallback_client_initialized() | 
|---|
| 134 |     { | 
|---|
| 135 |         if (this->getGametype()) | 
|---|
| 136 |             this->getGametype()->playerEntered(this); | 
|---|
| 137 |     } | 
|---|
| 138 |  | 
|---|
| 139 |     bool HumanPlayer::isInitialized() const | 
|---|
| 140 |     { | 
|---|
| 141 |         return (this->server_initialized_ && this->client_initialized_); | 
|---|
| 142 |     } | 
|---|
| 143 |  | 
|---|
| 144 |     float HumanPlayer::getPing() const | 
|---|
| 145 |     { | 
|---|
| 146 |         return static_cast<float>(ClientInformation::findClient(this->getClientID())->getRTT()); | 
|---|
| 147 |     } | 
|---|
| 148 |  | 
|---|
| 149 |     float HumanPlayer::getPacketLossRatio() const | 
|---|
| 150 |     { | 
|---|
| 151 |         return static_cast<float>(ClientInformation::findClient(this->getClientID())->getPacketLoss()); | 
|---|
| 152 |     } | 
|---|
| 153 |  | 
|---|
| 154 |     void HumanPlayer::setClientID(unsigned int clientID) | 
|---|
| 155 |     { | 
|---|
| 156 |         this->clientID_ = clientID; | 
|---|
| 157 |         this->networkcallback_clientIDchanged(); | 
|---|
| 158 |     } | 
|---|
| 159 |  | 
|---|
| 160 |     void HumanPlayer::changedGametype() | 
|---|
| 161 |     { | 
|---|
| 162 |         PlayerInfo::changedGametype(); | 
|---|
| 163 |  | 
|---|
| 164 |         if (this->isInitialized() && this->isLocalPlayer()) | 
|---|
| 165 |         { | 
|---|
| 166 |             if (this->getGametype() && !this->getGametype()->getHUDTemplate().empty()) | 
|---|
| 167 |                 this->setGametypeHUDTemplate(this->getGametype()->getHUDTemplate()); | 
|---|
| 168 |             else | 
|---|
| 169 |                 this->setGametypeHUDTemplate(""); | 
|---|
| 170 |         } | 
|---|
| 171 |     } | 
|---|
| 172 |  | 
|---|
| 173 |     void HumanPlayer::updateHumanHUD() | 
|---|
| 174 |     { | 
|---|
| 175 |         if (this->humanHud_) | 
|---|
| 176 |         { | 
|---|
| 177 |             this->humanHud_->destroy(); | 
|---|
| 178 |             this->humanHud_ = 0; | 
|---|
| 179 |         } | 
|---|
| 180 |  | 
|---|
| 181 |         if (this->isLocalPlayer() && !this->humanHudTemplate_.empty() && GameMode::showsGraphics()) | 
|---|
| 182 |         { | 
|---|
| 183 |             this->humanHud_ = new OverlayGroup(this); | 
|---|
| 184 |             this->humanHud_->addTemplate(this->humanHudTemplate_); | 
|---|
| 185 |             this->humanHud_->setOwner(this); | 
|---|
| 186 |         } | 
|---|
| 187 |     } | 
|---|
| 188 |  | 
|---|
| 189 |     void HumanPlayer::updateGametypeHUD() | 
|---|
| 190 |     { | 
|---|
| 191 |         if (this->gametypeHud_) | 
|---|
| 192 |         { | 
|---|
| 193 |             this->gametypeHud_->destroy(); | 
|---|
| 194 |             this->gametypeHud_ = 0; | 
|---|
| 195 |         } | 
|---|
| 196 |  | 
|---|
| 197 |         if (this->isLocalPlayer() && !this->gametypeHudTemplate_.empty()) | 
|---|
| 198 |         { | 
|---|
| 199 |             this->gametypeHud_ = new OverlayGroup(this); | 
|---|
| 200 |             this->gametypeHud_->addTemplate(this->gametypeHudTemplate_); | 
|---|
| 201 |             this->gametypeHud_->setOwner(this); | 
|---|
| 202 |         } | 
|---|
| 203 |     } | 
|---|
| 204 | } | 
|---|