| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 | ### File Specific: | 
|---|
| 12 |    main-programmer: Patrick Boenzli | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #define DEBUG_MODULE_GAME_RULES | 
|---|
| 16 |  | 
|---|
| 17 | #include <map> | 
|---|
| 18 |  | 
|---|
| 19 | #include "multiplayer_team_deathmatch.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "util/loading/load_param.h" | 
|---|
| 22 | #include "util/loading/factory.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "render2D/image_plane.h" | 
|---|
| 25 | #include "state.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "player.h" | 
|---|
| 28 | #include "playable.h" | 
|---|
| 29 | #include "space_ships/space_ship.h" | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | #include "shared_network_data.h" | 
|---|
| 33 | #include "terrain.h" | 
|---|
| 34 | #include "space_ships/space_ship.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "network_game_manager.h" | 
|---|
| 37 |  | 
|---|
| 38 | #include "event_handler.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "glgui.h" | 
|---|
| 41 |  | 
|---|
| 42 | #include "story_entity.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include "shell_command.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include "spawning_point.h" | 
|---|
| 47 |  | 
|---|
| 48 | #include "creatures/fps_player.h" | 
|---|
| 49 |  | 
|---|
| 50 | ObjectListDefinition(MultiplayerTeamDeathmatch); | 
|---|
| 51 | CREATE_FACTORY(MultiplayerTeamDeathmatch); | 
|---|
| 52 | /** | 
|---|
| 53 |  * constructor | 
|---|
| 54 |  */ | 
|---|
| 55 | MultiplayerTeamDeathmatch::MultiplayerTeamDeathmatch(const TiXmlElement* root) | 
|---|
| 56 |     : NetworkGameRules(root) | 
|---|
| 57 | { | 
|---|
| 58 |   this->registerObject(this, MultiplayerTeamDeathmatch::_objectList); | 
|---|
| 59 |  | 
|---|
| 60 |   this->bLocalPlayerDead = false; | 
|---|
| 61 |   this->deathTimeout = 10.0f;     // 5 seconds | 
|---|
| 62 |   this->timeout = 0.0f; | 
|---|
| 63 |   this->numTeams = 2; | 
|---|
| 64 |   this->currentGameState = GAMESTATE_PRE_GAME; | 
|---|
| 65 |   this->gameStateTimer = 3.0f; | 
|---|
| 66 |   this->bShowTeamChange = false; | 
|---|
| 67 |  | 
|---|
| 68 |   this->box = NULL; | 
|---|
| 69 |   this->table = NULL; | 
|---|
| 70 |   this->statsBox = NULL; | 
|---|
| 71 |  | 
|---|
| 72 |   this->localPlayer = State::getPlayer(); | 
|---|
| 73 |  | 
|---|
| 74 |   if( root != NULL) | 
|---|
| 75 |     this->loadParams(root); | 
|---|
| 76 |  | 
|---|
| 77 |   subscribeEvent( ES_GAME, SDLK_o ); | 
|---|
| 78 |   subscribeEvent( ES_GAME, SDLK_TAB ); | 
|---|
| 79 |   subscribeEvent( ES_GAME, SDLK_F1 ); | 
|---|
| 80 |   subscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 ); | 
|---|
| 81 |  | 
|---|
| 82 |   this->input = new OrxGui::GLGuiInputLine(); | 
|---|
| 83 |   this->input->setAbsCoor2D(180, 5); | 
|---|
| 84 |   this->input->enterPushed.connect(this, &MultiplayerTeamDeathmatch::onInputEnter); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 |  * decontsructor | 
|---|
| 89 |  */ | 
|---|
| 90 | MultiplayerTeamDeathmatch::~MultiplayerTeamDeathmatch() | 
|---|
| 91 | { | 
|---|
| 92 |   unsubscribeEvent( ES_GAME, SDLK_o ); | 
|---|
| 93 |   unsubscribeEvent( ES_GAME, SDLK_TAB ); | 
|---|
| 94 |   unsubscribeEvent( ES_GAME, SDLK_F1 ); | 
|---|
| 95 |   unsubscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 ); | 
|---|
| 96 |  | 
|---|
| 97 |   if ( this->input ) | 
|---|
| 98 |   { | 
|---|
| 99 |     delete this->input; | 
|---|
| 100 |     this->input = NULL; | 
|---|
| 101 |   } | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 | void MultiplayerTeamDeathmatch::loadParams(const TiXmlElement* root) | 
|---|
| 107 | { | 
|---|
| 108 |   GameRules::loadParams(root) ; | 
|---|
| 109 |  | 
|---|
| 110 |   LoadParam(root, "death-penalty-timeout", this, MultiplayerTeamDeathmatch, setDeathPenaltyTimeout) | 
|---|
| 111 |   .describe("sets the time in seconds a player has to wait for respawn"); | 
|---|
| 112 |  | 
|---|
| 113 |   LoadParam(root, "max-kills", this, MultiplayerTeamDeathmatch, setMaxKills) | 
|---|
| 114 |   .describe("sets the maximal kills for winning condition"); | 
|---|
| 115 |  | 
|---|
| 116 |   LoadParam(root, "num-teams", this, MultiplayerTeamDeathmatch, setNumTeams) | 
|---|
| 117 |   .describe("sets number of teams"); | 
|---|
| 118 |  | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 | /** | 
|---|
| 123 |  * time tick | 
|---|
| 124 |  * @param dt time | 
|---|
| 125 |  */ | 
|---|
| 126 | void MultiplayerTeamDeathmatch::tick(float dt) | 
|---|
| 127 | { | 
|---|
| 128 |   tickStatsTable(); | 
|---|
| 129 |   //on client side hostId is -1 until hanshake finished | 
|---|
| 130 |   if ( SharedNetworkData::getInstance()->getHostID() < 0 ) | 
|---|
| 131 |     return; | 
|---|
| 132 |  | 
|---|
| 133 |   if ( currentGameState == GAMESTATE_PRE_GAME || currentGameState == GAMESTATE_GAME ) | 
|---|
| 134 |   { | 
|---|
| 135 |     if ( PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) && | 
|---|
| 136 |          box == NULL && | 
|---|
| 137 |          (PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPreferedTeamId() == TEAM_NOTEAM || bShowTeamChange ) | 
|---|
| 138 |  | 
|---|
| 139 |        ) | 
|---|
| 140 |     { | 
|---|
| 141 |       EventHandler::getInstance()->pushState( ES_MENU ); | 
|---|
| 142 |  | 
|---|
| 143 |       OrxGui::GLGuiHandler::getInstance()->activateCursor(); | 
|---|
| 144 |  | 
|---|
| 145 |       box = new OrxGui::GLGuiBox(); | 
|---|
| 146 |       box->setAbsCoor2D( 300, 100 ); | 
|---|
| 147 |  | 
|---|
| 148 |       OrxGui::GLGuiPushButton * buttonSpectator = new OrxGui::GLGuiPushButton("Spectator"); | 
|---|
| 149 |       box->pack( buttonSpectator ); | 
|---|
| 150 |       buttonSpectator->released.connect(this, &MultiplayerTeamDeathmatch::onButtonSpectator); | 
|---|
| 151 |  | 
|---|
| 152 |       OrxGui::GLGuiPushButton * buttonRandom = new OrxGui::GLGuiPushButton("Random"); | 
|---|
| 153 |       box->pack( buttonRandom ); | 
|---|
| 154 |       buttonRandom->released.connect(this, &MultiplayerTeamDeathmatch::onButtonRandom); | 
|---|
| 155 |  | 
|---|
| 156 |       OrxGui::GLGuiPushButton * buttonTeam0 = new OrxGui::GLGuiPushButton("Blue Team"); | 
|---|
| 157 |       box->pack( buttonTeam0 ); | 
|---|
| 158 |       buttonTeam0->released.connect(this, &MultiplayerTeamDeathmatch::onButtonTeam0); | 
|---|
| 159 |  | 
|---|
| 160 |       OrxGui::GLGuiPushButton * buttonTeam1 = new OrxGui::GLGuiPushButton("Red Team"); | 
|---|
| 161 |       box->pack( buttonTeam1 ); | 
|---|
| 162 |       buttonTeam1->released.connect(this, &MultiplayerTeamDeathmatch::onButtonTeam1); | 
|---|
| 163 |  | 
|---|
| 164 |       if ( bShowTeamChange ) | 
|---|
| 165 |       { | 
|---|
| 166 |         OrxGui::GLGuiPushButton * buttonCancel = new OrxGui::GLGuiPushButton("Cancel"); | 
|---|
| 167 |         box->pack( buttonCancel ); | 
|---|
| 168 |         buttonCancel->released.connect(this, &MultiplayerTeamDeathmatch::onButtonCancel); | 
|---|
| 169 |       } | 
|---|
| 170 |  | 
|---|
| 171 |       OrxGui::GLGuiPushButton * buttonExit = new OrxGui::GLGuiPushButton("Exit"); | 
|---|
| 172 |       box->pack( buttonExit ); | 
|---|
| 173 |       buttonExit->released.connect(this, &MultiplayerTeamDeathmatch::onButtonExit); | 
|---|
| 174 |  | 
|---|
| 175 |       box->showAll(); | 
|---|
| 176 |     } | 
|---|
| 177 |   } | 
|---|
| 178 |  | 
|---|
| 179 |   //   if( PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) ) | 
|---|
| 180 |   //   { | 
|---|
| 181 |   //     PRINTF(0)("prefered team id: %i, noteam: %i\n", PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPreferedTeamId(), TEAM_NOTEAM); | 
|---|
| 182 |   //   } | 
|---|
| 183 |  | 
|---|
| 184 |   // check if the menu should be removed and the game state should be entered | 
|---|
| 185 |   if ( box != NULL && | 
|---|
| 186 |        PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) && | 
|---|
| 187 |        PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPreferedTeamId() != TEAM_NOTEAM && | 
|---|
| 188 |        !bShowTeamChange | 
|---|
| 189 |      ) | 
|---|
| 190 |   { | 
|---|
| 191 |     delete box; | 
|---|
| 192 |     box = NULL; | 
|---|
| 193 |  | 
|---|
| 194 |     OrxGui::GLGuiHandler::getInstance()->deactivateCursor( true ); | 
|---|
| 195 |  | 
|---|
| 196 |     EventHandler::getInstance()->popState(); | 
|---|
| 197 |   } | 
|---|
| 198 |  | 
|---|
| 199 |   if ( box != NULL ) | 
|---|
| 200 |   { | 
|---|
| 201 |     OrxGui::GLGuiHandler::getInstance()->tick( dt ); | 
|---|
| 202 |   } | 
|---|
| 203 |  | 
|---|
| 204 |   assignPlayable(); | 
|---|
| 205 |  | 
|---|
| 206 |   if ( SharedNetworkData::getInstance()->isClient() || SharedNetworkData::getInstance()->isProxyServerActive()) | 
|---|
| 207 |     return; | 
|---|
| 208 |  | 
|---|
| 209 |   //handle kills | 
|---|
| 210 |   while ( this->killList.begin() != this->killList.end() ) | 
|---|
| 211 |   { | 
|---|
| 212 |     PRINTF(0)("KKKKKKKKIIIIIIIIILLLLLLLLLLLLL\n"); | 
|---|
| 213 |     onKill( this->killList.begin()->getVictim(), this->killList.begin()->getKiller() ); | 
|---|
| 214 |     this->killList.erase( this->killList.begin() ); | 
|---|
| 215 |   } | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 |   gameStateTimer -= dt; | 
|---|
| 220 |   //PRINTF(0)("TICK %f\n", gameStateTimer); | 
|---|
| 221 |  | 
|---|
| 222 |   if ( currentGameState != GAMESTATE_GAME && gameStateTimer < 0 ) | 
|---|
| 223 |     nextGameState(); | 
|---|
| 224 |  | 
|---|
| 225 |   this->currentGameState = NetworkGameManager::getInstance()->getGameState(); | 
|---|
| 226 |  | 
|---|
| 227 |   if ( currentGameState == GAMESTATE_GAME ) | 
|---|
| 228 |   { | 
|---|
| 229 |     handleTeamChanges(); | 
|---|
| 230 |   } | 
|---|
| 231 |  | 
|---|
| 232 |   this->calculateTeamScore(); | 
|---|
| 233 |  | 
|---|
| 234 |   this->checkGameRules(); | 
|---|
| 235 |  | 
|---|
| 236 |   // is the local player dead and inactive | 
|---|
| 237 |   if( unlikely(this->bLocalPlayerDead)) | 
|---|
| 238 |   { | 
|---|
| 239 |     this->timeout += dt; | 
|---|
| 240 |     PRINTF(0)("TICK DEATH: %f of %f\n", this->timeout, this->deathTimeout); | 
|---|
| 241 |     // long enough dead? | 
|---|
| 242 |     if( this->timeout >= this->deathTimeout) | 
|---|
| 243 |     { | 
|---|
| 244 |       this->timeout = 0.0f; | 
|---|
| 245 |       // respawn | 
|---|
| 246 |       PRINTF(0)("RESPAWN\n"); | 
|---|
| 247 |       (State::getPlayer())->getPlayable()->respawn(); | 
|---|
| 248 |     } | 
|---|
| 249 |   } | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | /** | 
|---|
| 254 |  * draws the stuff | 
|---|
| 255 |  */ | 
|---|
| 256 | void MultiplayerTeamDeathmatch::draw() | 
|---|
| 257 | { | 
|---|
| 258 |   if( unlikely( this->bLocalPlayerDead)) | 
|---|
| 259 |   { | 
|---|
| 260 |   } | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 |  | 
|---|
| 264 | /** | 
|---|
| 265 |  * check the game rules for consistency | 
|---|
| 266 |  */ | 
|---|
| 267 | void MultiplayerTeamDeathmatch::checkGameRules() | 
|---|
| 268 | { | 
|---|
| 269 |   if ( SharedNetworkData::getInstance()->isClient() || SharedNetworkData::getInstance()->isProxyServerActive()) | 
|---|
| 270 |     return; | 
|---|
| 271 |  | 
|---|
| 272 |   // check for max killing count | 
|---|
| 273 |   for ( int i = 0; i<numTeams; i++ ) | 
|---|
| 274 |   { | 
|---|
| 275 |     if ( teamScore[i] >= maxKills ) | 
|---|
| 276 |     { | 
|---|
| 277 |       nextGameState(); | 
|---|
| 278 |     } | 
|---|
| 279 |   } | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | /** | 
|---|
| 283 |  * find group for new player | 
|---|
| 284 |  * @return group id | 
|---|
| 285 |  */ | 
|---|
| 286 | int MultiplayerTeamDeathmatch::getTeamForNewUser() | 
|---|
| 287 | { | 
|---|
| 288 |   return TEAM_NOTEAM; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | ClassID MultiplayerTeamDeathmatch::getPlayableClassId( int userId, int team ) | 
|---|
| 292 | { | 
|---|
| 293 |   if ( team == TEAM_NOTEAM || team == TEAM_SPECTATOR ) | 
|---|
| 294 |     return ObjectListBase::retrieveIdentity("Spectator"); | 
|---|
| 295 |  | 
|---|
| 296 |   if ( team == 0 || team == 1 ) | 
|---|
| 297 |     return ObjectListBase::retrieveIdentity("FPSPlayer"); | 
|---|
| 298 |  | 
|---|
| 299 |   assert( false ); | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 |  | 
|---|
| 303 | std::string MultiplayerTeamDeathmatch::getPlayableModelFileName( int userId, int team, const ClassID& classId ) | 
|---|
| 304 | { | 
|---|
| 305 |   if ( team == 0 ) | 
|---|
| 306 |     return "models/creatures/doom_guy.md2"; | 
|---|
| 307 |   else if ( team == 1 ) | 
|---|
| 308 |     return "models/creatures/male.md2"; | 
|---|
| 309 |   else | 
|---|
| 310 |   { | 
|---|
| 311 |     return ""; | 
|---|
| 312 |   } | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | std::string MultiplayerTeamDeathmatch::getPlayableModelTextureFileName( int userId, int team, const ClassID& classId ) | 
|---|
| 316 | { | 
|---|
| 317 |   if ( team == 0 || team == 1 ) | 
|---|
| 318 |   { | 
|---|
| 319 |     if ( team == 0 ) | 
|---|
| 320 |       return "doom_guy.png"; | 
|---|
| 321 |     else | 
|---|
| 322 |       return "male_fiend.pcx"; | 
|---|
| 323 |   } | 
|---|
| 324 |  | 
|---|
| 325 |   return ""; | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | float MultiplayerTeamDeathmatch::getPlayableScale( int userId, int team, const ClassID& classId ) | 
|---|
| 329 | { | 
|---|
| 330 |   if ( classId == FPSPlayer::staticClassID() ) | 
|---|
| 331 |   { | 
|---|
| 332 |     return 10.0f; | 
|---|
| 333 |   } | 
|---|
| 334 |  | 
|---|
| 335 |   return 1.0f; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | /** | 
|---|
| 339 |  * calculate team score | 
|---|
| 340 |  */ | 
|---|
| 341 | void MultiplayerTeamDeathmatch::calculateTeamScore( ) | 
|---|
| 342 | { | 
|---|
| 343 |   teamScore.clear(); | 
|---|
| 344 |  | 
|---|
| 345 |   for ( int i = 0; i<numTeams; i++ ) | 
|---|
| 346 |     teamScore[i] = 0; | 
|---|
| 347 |  | 
|---|
| 348 |   for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin(); | 
|---|
| 349 |        it != PlayerStats::objectList().end(); | 
|---|
| 350 |        ++it) | 
|---|
| 351 |   { | 
|---|
| 352 |     PlayerStats & stats = *(*it); | 
|---|
| 353 |  | 
|---|
| 354 |     if ( stats.getTeamId() >= 0 ) | 
|---|
| 355 |     { | 
|---|
| 356 |       teamScore[stats.getTeamId()] += stats.getScore(); | 
|---|
| 357 |     } | 
|---|
| 358 |   } | 
|---|
| 359 | } | 
|---|
| 360 |  | 
|---|
| 361 | /** | 
|---|
| 362 |  * get team for player who choose to join random team | 
|---|
| 363 |  * @return smallest team | 
|---|
| 364 |  */ | 
|---|
| 365 | int MultiplayerTeamDeathmatch::getRandomTeam( ) | 
|---|
| 366 | { | 
|---|
| 367 |   std::map<int,int> playersInTeam; | 
|---|
| 368 |  | 
|---|
| 369 |   for ( int i = 0; i<numTeams; i++ ) | 
|---|
| 370 |     playersInTeam[i] = 0; | 
|---|
| 371 |  | 
|---|
| 372 |   for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin(); | 
|---|
| 373 |        it != PlayerStats::objectList().end(); | 
|---|
| 374 |        ++it) | 
|---|
| 375 |   { | 
|---|
| 376 |     PlayerStats & stats = *(*it); | 
|---|
| 377 |  | 
|---|
| 378 |     if ( stats.getTeamId() >= 0 ) | 
|---|
| 379 |     { | 
|---|
| 380 |       playersInTeam[stats.getTeamId()]++; | 
|---|
| 381 |     } | 
|---|
| 382 |   } | 
|---|
| 383 |  | 
|---|
| 384 |  | 
|---|
| 385 |   int minPlayers = 0xFFFF; | 
|---|
| 386 |   int minTeam = -1; | 
|---|
| 387 |  | 
|---|
| 388 |   for ( int i = 0; i<numTeams; i++ ) | 
|---|
| 389 |   { | 
|---|
| 390 |     if ( playersInTeam[i] < minPlayers ) | 
|---|
| 391 |     { | 
|---|
| 392 |       minTeam = i; | 
|---|
| 393 |       minPlayers = playersInTeam[i]; | 
|---|
| 394 |     } | 
|---|
| 395 |   } | 
|---|
| 396 |  | 
|---|
| 397 |   assert( minTeam != -1 ); | 
|---|
| 398 |  | 
|---|
| 399 |   return minTeam; | 
|---|
| 400 | } | 
|---|
| 401 |  | 
|---|
| 402 | void MultiplayerTeamDeathmatch::nextGameState( ) | 
|---|
| 403 | { | 
|---|
| 404 |   if ( currentGameState == GAMESTATE_PRE_GAME ) | 
|---|
| 405 |   { | 
|---|
| 406 |     NetworkGameManager::getInstance()->setGameState( GAMESTATE_GAME ); | 
|---|
| 407 |  | 
|---|
| 408 |     return; | 
|---|
| 409 |   } | 
|---|
| 410 |  | 
|---|
| 411 |   if ( currentGameState == GAMESTATE_GAME ) | 
|---|
| 412 |   { | 
|---|
| 413 |     NetworkGameManager::getInstance()->setGameState( GAMESTATE_POST_GAME ); | 
|---|
| 414 |  | 
|---|
| 415 |     return; | 
|---|
| 416 |   } | 
|---|
| 417 |  | 
|---|
| 418 |   if ( currentGameState == GAMESTATE_POST_GAME ) | 
|---|
| 419 |   { | 
|---|
| 420 |     //State::getCurrentStoryEntity()->stop(); | 
|---|
| 421 |     this->bShowTeamChange = false; | 
|---|
| 422 |  | 
|---|
| 423 |     return; | 
|---|
| 424 |   } | 
|---|
| 425 | } | 
|---|
| 426 |  | 
|---|
| 427 | /** | 
|---|
| 428 |  *  this handles team changes but only on the master server | 
|---|
| 429 |  */ | 
|---|
| 430 | void MultiplayerTeamDeathmatch::handleTeamChanges( ) | 
|---|
| 431 | { | 
|---|
| 432 |   for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin(); | 
|---|
| 433 |        it != PlayerStats::objectList().end(); | 
|---|
| 434 |        ++it) | 
|---|
| 435 |   { | 
|---|
| 436 |     PlayerStats & stats = *(*it); | 
|---|
| 437 |  | 
|---|
| 438 |     if ( stats.getTeamId() != stats.getPreferedTeamId() ) | 
|---|
| 439 |     { | 
|---|
| 440 |       if ( stats.getPreferedTeamId() == TEAM_SPECTATOR || ( stats.getPreferedTeamId() >= 0 && stats.getPreferedTeamId() < numTeams ) ) | 
|---|
| 441 |       { | 
|---|
| 442 |         teamChange( stats.getAssignedUserId() ); | 
|---|
| 443 |       } | 
|---|
| 444 |     } | 
|---|
| 445 |   } | 
|---|
| 446 |  | 
|---|
| 447 |   //now serve player who want join a random team | 
|---|
| 448 |   for (ObjectList<PlayerStats>::const_iterator it = PlayerStats::objectList().begin(); | 
|---|
| 449 |        it != PlayerStats::objectList().end(); | 
|---|
| 450 |        ++it) | 
|---|
| 451 |   { | 
|---|
| 452 |     PlayerStats & stats = *(*it); | 
|---|
| 453 |  | 
|---|
| 454 |     if ( stats.getTeamId() != stats.getPreferedTeamId() ) | 
|---|
| 455 |     { | 
|---|
| 456 |       if ( stats.getPreferedTeamId() == TEAM_RANDOM ) | 
|---|
| 457 |       { | 
|---|
| 458 |         stats.setPreferedTeamId( getRandomTeam() ); | 
|---|
| 459 |         teamChange( stats.getAssignedUserId() ); | 
|---|
| 460 |       } | 
|---|
| 461 |     } | 
|---|
| 462 |   } | 
|---|
| 463 | } | 
|---|
| 464 |  | 
|---|
| 465 |  | 
|---|
| 466 |  | 
|---|
| 467 | /** | 
|---|
| 468 |  * changes the team | 
|---|
| 469 |  * @param userId the user changing team (userId) | 
|---|
| 470 |  */ | 
|---|
| 471 | void MultiplayerTeamDeathmatch::teamChange( int userId ) | 
|---|
| 472 | { | 
|---|
| 473 |   assert( PlayerStats::getStats( userId ) ); | 
|---|
| 474 |   PlayerStats & stats = *(PlayerStats::getStats( userId )); | 
|---|
| 475 |  | 
|---|
| 476 |   stats.setTeamId( stats.getPreferedTeamId() ); | 
|---|
| 477 |  | 
|---|
| 478 |   Playable * oldPlayable = stats.getPlayable(); | 
|---|
| 479 |  | 
|---|
| 480 |  | 
|---|
| 481 |   ClassID       playableClassId  = getPlayableClassId( userId, stats.getPreferedTeamId() ); | 
|---|
| 482 |   std::string   playableModel    = getPlayableModelFileName( userId, stats.getPreferedTeamId(), playableClassId ); | 
|---|
| 483 |   std::string   playableTexture  = getPlayableModelTextureFileName( userId, stats.getPreferedTeamId(), playableClassId ); | 
|---|
| 484 |   float         playableScale    = getPlayableScale( userId, stats.getPreferedTeamId(), playableClassId ); | 
|---|
| 485 |  | 
|---|
| 486 |   BaseObject * bo = Factory::fabricate( playableClassId ); | 
|---|
| 487 |  | 
|---|
| 488 |   assert( bo != NULL ); | 
|---|
| 489 |   assert( bo->isA( Playable::staticClassID() ) ); | 
|---|
| 490 |  | 
|---|
| 491 |   Playable & playable = *(dynamic_cast<Playable*>(bo)); | 
|---|
| 492 |  | 
|---|
| 493 |   PRINTF(0)("%s %s %f\n", playableModel.c_str(), playableTexture.c_str(), playableScale ); | 
|---|
| 494 |    | 
|---|
| 495 |   playable.loadMD2Texture( playableTexture ); | 
|---|
| 496 |   playable.loadModel( playableModel, playableScale ); | 
|---|
| 497 |   playable.setOwner( userId ); | 
|---|
| 498 |   playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); | 
|---|
| 499 |   playable.setSynchronized( true ); | 
|---|
| 500 |  | 
|---|
| 501 |   stats.setPlayableClassId( playableClassId ); | 
|---|
| 502 |   stats.setPlayableUniqueId( playable.getUniqueID() ); | 
|---|
| 503 |   stats.setModelFileName( playableModel ); | 
|---|
| 504 |   stats.setTeamId( stats.getPreferedTeamId() ); | 
|---|
| 505 |  | 
|---|
| 506 |   playable.setTeam(stats.getPreferedTeamId()); | 
|---|
| 507 |  | 
|---|
| 508 |  | 
|---|
| 509 |   this->respawnPlayable( &playable, stats.getPreferedTeamId(), 0.0f ); | 
|---|
| 510 |  | 
|---|
| 511 |   if ( oldPlayable ) | 
|---|
| 512 |   { | 
|---|
| 513 |     delete oldPlayable; | 
|---|
| 514 |   } | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 |  | 
|---|
| 518 | void MultiplayerTeamDeathmatch::onButtonExit( ) | 
|---|
| 519 | { | 
|---|
| 520 |   State::getCurrentStoryEntity()->stop(); | 
|---|
| 521 |   this->bShowTeamChange = false; | 
|---|
| 522 | } | 
|---|
| 523 |  | 
|---|
| 524 | void MultiplayerTeamDeathmatch::onButtonRandom( ) | 
|---|
| 525 | { | 
|---|
| 526 |   NetworkGameManager::getInstance()->prefereTeam( TEAM_RANDOM ); | 
|---|
| 527 |   this->bShowTeamChange = false; | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 | void MultiplayerTeamDeathmatch::onButtonTeam0( ) | 
|---|
| 531 | { | 
|---|
| 532 |   NetworkGameManager::getInstance()->prefereTeam( 0 ); | 
|---|
| 533 |   this->bShowTeamChange = false; | 
|---|
| 534 | } | 
|---|
| 535 |  | 
|---|
| 536 | void MultiplayerTeamDeathmatch::onButtonTeam1( ) | 
|---|
| 537 | { | 
|---|
| 538 |   NetworkGameManager::getInstance()->prefereTeam( 1 ); | 
|---|
| 539 |   this->bShowTeamChange = false; | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | void MultiplayerTeamDeathmatch::onButtonSpectator( ) | 
|---|
| 543 | { | 
|---|
| 544 |   NetworkGameManager::getInstance()->prefereTeam( TEAM_SPECTATOR ); | 
|---|
| 545 |   this->bShowTeamChange = false; | 
|---|
| 546 | } | 
|---|
| 547 |  | 
|---|
| 548 | void MultiplayerTeamDeathmatch::assignPlayable( ) | 
|---|
| 549 | { | 
|---|
| 550 |   if ( PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) ) | 
|---|
| 551 |     PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPlayable(); | 
|---|
| 552 | } | 
|---|
| 553 |  | 
|---|
| 554 |  | 
|---|
| 555 | /** | 
|---|
| 556 |  * function that processes events from the handler | 
|---|
| 557 |  * @param event: the event | 
|---|
| 558 |  * @todo replace SDLK_o with something from KeyMapper | 
|---|
| 559 |  */ | 
|---|
| 560 | void MultiplayerTeamDeathmatch::process( const Event & event ) | 
|---|
| 561 | { | 
|---|
| 562 |   if ( event.type == SDLK_o ) | 
|---|
| 563 |   { | 
|---|
| 564 |     if ( event.bPressed ) | 
|---|
| 565 |       this->bShowTeamChange = true; | 
|---|
| 566 |   } | 
|---|
| 567 |   else if ( event.type == SDLK_F1 ) | 
|---|
| 568 |   { | 
|---|
| 569 |     if ( this->statsBox && !this->bLocalPlayerDead && event.bPressed ) | 
|---|
| 570 |     { | 
|---|
| 571 |       PRINTF(5)("hide stats\n"); | 
|---|
| 572 |       this->hideStats(); | 
|---|
| 573 |     } | 
|---|
| 574 |     else if ( !this->statsBox && event.bPressed ) | 
|---|
| 575 |     { | 
|---|
| 576 |       PRINTF(5)("show stats\n"); | 
|---|
| 577 |       this->showStats(); | 
|---|
| 578 |     } | 
|---|
| 579 |   } | 
|---|
| 580 |   else if ( event.type == SDLK_TAB ) | 
|---|
| 581 |   { | 
|---|
| 582 |     if ( currentGameState == GAMESTATE_GAME && event.bPressed && !EventHandler::getInstance()->isPressed( SDLK_RALT ) && !EventHandler::getInstance()->isPressed( SDLK_LALT ) ) | 
|---|
| 583 |     { | 
|---|
| 584 |       EventHandler::getInstance()->pushState( ES_MENU ); | 
|---|
| 585 |       OrxGui::GLGuiHandler::getInstance()->activateCursor(); | 
|---|
| 586 |       OrxGui::GLGuiHandler::getInstance()->deactivateCursor(); | 
|---|
| 587 |       input->show(); | 
|---|
| 588 |       input->giveMouseFocus(); | 
|---|
| 589 |       input->setText("say "); | 
|---|
| 590 |     } | 
|---|
| 591 |   } | 
|---|
| 592 |   else if ( this->bLocalPlayerDead && statsBox && event.type == KeyMapper::PEV_FIRE1 ) | 
|---|
| 593 |   { | 
|---|
| 594 |     this->hideStats(); | 
|---|
| 595 |   } | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | void MultiplayerTeamDeathmatch::onButtonCancel( ) | 
|---|
| 599 | { | 
|---|
| 600 |   this->bShowTeamChange = false; | 
|---|
| 601 | } | 
|---|
| 602 |  | 
|---|
| 603 |  | 
|---|
| 604 |  | 
|---|
| 605 | /** | 
|---|
| 606 |  * this method is called by NetworkGameManger when he recieved a chat message | 
|---|
| 607 |  * @param userId senders user id | 
|---|
| 608 |  * @param message message string | 
|---|
| 609 |  * @param messageType some int | 
|---|
| 610 |  */ | 
|---|
| 611 | void MultiplayerTeamDeathmatch::handleChatMessage( int userId, const std::string & message, int messageType ) | 
|---|
| 612 | { | 
|---|
| 613 |   std::string name = "unknown"; | 
|---|
| 614 |  | 
|---|
| 615 |   if ( PlayerStats::getStats( userId ) ) | 
|---|
| 616 |   { | 
|---|
| 617 |     name = PlayerStats::getStats( userId )->getNickName(); | 
|---|
| 618 |   } | 
|---|
| 619 |  | 
|---|
| 620 |   PRINTF(0)("CHATMESSAGE %s (%d): %s\n", name.c_str(), userId, message.c_str() ); | 
|---|
| 621 |   State::getPlayer()->hud().notifyUser(name + ": " + message); | 
|---|
| 622 | } | 
|---|
| 623 |  | 
|---|
| 624 | void MultiplayerTeamDeathmatch::onInputEnter( const std::string & text ) | 
|---|
| 625 | { | 
|---|
| 626 |   EventHandler::getInstance()->popState(); | 
|---|
| 627 |   input->breakMouseFocus(); | 
|---|
| 628 |   input->hide(); | 
|---|
| 629 |   input->setText(""); | 
|---|
| 630 |  | 
|---|
| 631 |   std::string command = text; | 
|---|
| 632 |  | 
|---|
| 633 |   //HACK insert " in say commands so user doesn't have to type them | 
|---|
| 634 |   if ( command.length() >= 4 && command[0] == 's' && command[1] == 'a' && command[2] == 'y' && command[3] == ' ' ) | 
|---|
| 635 |   { | 
|---|
| 636 |     command.insert( 4, "\"" ); | 
|---|
| 637 |     command = command + "\""; | 
|---|
| 638 |   } | 
|---|
| 639 |  | 
|---|
| 640 |   OrxShell::ShellCommand::execute( command ); | 
|---|
| 641 | } | 
|---|
| 642 |  | 
|---|
| 643 | /** | 
|---|
| 644 |  * show table with frags | 
|---|
| 645 |  */ | 
|---|
| 646 | void MultiplayerTeamDeathmatch::showStats( ) | 
|---|
| 647 | { | 
|---|
| 648 |   statsBox = new OrxGui::GLGuiBox(); | 
|---|
| 649 |   statsBox->setAbsCoor2D( 100, 100 ); | 
|---|
| 650 |  | 
|---|
| 651 |   this->table = new OrxGui::GLGuiTable(10,5); | 
|---|
| 652 |  | 
|---|
| 653 |   statsBox->pack( this->table ); | 
|---|
| 654 |  | 
|---|
| 655 |   statsBox->showAll(); | 
|---|
| 656 | } | 
|---|
| 657 |  | 
|---|
| 658 | /** | 
|---|
| 659 |  * hide table with frags | 
|---|
| 660 |  */ | 
|---|
| 661 | void MultiplayerTeamDeathmatch::hideStats( ) | 
|---|
| 662 | { | 
|---|
| 663 |   if ( statsBox ) | 
|---|
| 664 |   { | 
|---|
| 665 |     delete statsBox; | 
|---|
| 666 |     statsBox = NULL; | 
|---|
| 667 |   } | 
|---|
| 668 | } | 
|---|
| 669 |  | 
|---|
| 670 | /** | 
|---|
| 671 |  * fill stats table with values | 
|---|
| 672 |  */ | 
|---|
| 673 | void MultiplayerTeamDeathmatch::tickStatsTable( ) | 
|---|
| 674 | { | 
|---|
| 675 |   if ( !this->statsBox ) | 
|---|
| 676 |     return; | 
|---|
| 677 |  | 
|---|
| 678 |   std::vector<std::string> headers; | 
|---|
| 679 |   headers.push_back("Blue Team"); | 
|---|
| 680 |   headers.push_back(""); | 
|---|
| 681 |   headers.push_back(""); | 
|---|
| 682 |   headers.push_back("Red Team"); | 
|---|
| 683 |   headers.push_back(""); | 
|---|
| 684 |   this->table->setHeader(headers); | 
|---|
| 685 |  | 
|---|
| 686 |   ScoreList scoreList = PlayerStats::getScoreList(); | 
|---|
| 687 |  | 
|---|
| 688 |   char st[10]; | 
|---|
| 689 |   int i = 0; | 
|---|
| 690 |  | 
|---|
| 691 |   i = 2; | 
|---|
| 692 |   for ( TeamScoreList::const_iterator it = scoreList[0].begin(); it != scoreList[0].end(); it++ ) | 
|---|
| 693 |   { | 
|---|
| 694 |     this->table->setEntry( i, 0, it->name ); | 
|---|
| 695 |     snprintf( st, 10, "%d", it->score ); | 
|---|
| 696 |     this->table->setEntry( i, 1, st ); | 
|---|
| 697 |     this->table->setEntry( i, 2, "" ); | 
|---|
| 698 |     i++; | 
|---|
| 699 |   } | 
|---|
| 700 |  | 
|---|
| 701 |   i = 2; | 
|---|
| 702 |   for ( TeamScoreList::const_iterator it = scoreList[1].begin(); it != scoreList[1].end(); it++ ) | 
|---|
| 703 |   { | 
|---|
| 704 |     this->table->setEntry( i, 3, it->name ); | 
|---|
| 705 |     snprintf( st, 10, "%d", it->score ); | 
|---|
| 706 |     this->table->setEntry( i, 4, st ); | 
|---|
| 707 |     i++; | 
|---|
| 708 |   } | 
|---|
| 709 |  | 
|---|
| 710 | } | 
|---|
| 711 |  | 
|---|
| 712 | /** | 
|---|
| 713 |  * this function is called when a player kills another one or himself | 
|---|
| 714 |  * @param killedUserId | 
|---|
| 715 |  * @param userId | 
|---|
| 716 |  */ | 
|---|
| 717 | void MultiplayerTeamDeathmatch::onKill( WorldEntity * victim, WorldEntity * killer ) | 
|---|
| 718 | { | 
|---|
| 719 |   if ( !victim ) | 
|---|
| 720 |   { | 
|---|
| 721 |     PRINTF(0)("victim == NULL\n"); | 
|---|
| 722 |     return; | 
|---|
| 723 |   } | 
|---|
| 724 |   if ( !killer ) | 
|---|
| 725 |   { | 
|---|
| 726 |     PRINTF(0)("killer == NULL\n"); | 
|---|
| 727 |     return; | 
|---|
| 728 |   } | 
|---|
| 729 |  | 
|---|
| 730 |   int killerUserId = killer->getOwner(); | 
|---|
| 731 |   int victimUserId = victim->getOwner(); | 
|---|
| 732 |  | 
|---|
| 733 |   PRINTF(0)("%d %d %x %x %s %s\n", killerUserId, victimUserId, killer, victim, killer->getClassCName(), victim->getClassCName()); | 
|---|
| 734 |  | 
|---|
| 735 |   PlayerStats & victimStats = *PlayerStats::getStats( victimUserId ); | 
|---|
| 736 |   PlayerStats & killerStats = *PlayerStats::getStats( killerUserId ); | 
|---|
| 737 |  | 
|---|
| 738 |   if ( killerStats.getPlayable() == NULL || victimStats.getPlayable() == NULL) | 
|---|
| 739 |   { | 
|---|
| 740 |     PRINTF(0)("killerStats.getPlayable() != NULL || victimStats.getPlayable() != NULL\n"); | 
|---|
| 741 |     PRINTF(0)("%x %x %x %x\n", killerStats.getPlayable(), killer, victimStats.getPlayable(), victim ); | 
|---|
| 742 |     PRINTF(0)("%d %d %d %d\n", killerStats.getPlayable()->getUniqueID(), killer->getUniqueID(), victimStats.getPlayable()->getUniqueID(), victim->getUniqueID() ); | 
|---|
| 743 |     return; | 
|---|
| 744 |   } | 
|---|
| 745 |  | 
|---|
| 746 |   //check for suicide | 
|---|
| 747 |   if ( killerUserId != victimUserId ) | 
|---|
| 748 |   { | 
|---|
| 749 |     //check for teamkill | 
|---|
| 750 |     if ( victimStats.getTeamId() != killerStats.getTeamId() ) | 
|---|
| 751 |     { | 
|---|
| 752 |       killerStats.setScore( killerStats.getScore() + 1 ); | 
|---|
| 753 |     } | 
|---|
| 754 |     else | 
|---|
| 755 |     { | 
|---|
| 756 |       killerStats.setScore( killerStats.getScore() - 1 ); | 
|---|
| 757 |     } | 
|---|
| 758 |   } | 
|---|
| 759 |   else | 
|---|
| 760 |     killerStats.setScore( killerStats.getScore() - 1 ); | 
|---|
| 761 |  | 
|---|
| 762 |   if ( victimUserId == SharedNetworkData::getInstance()->getHostID() ) | 
|---|
| 763 |   { | 
|---|
| 764 |     this->bLocalPlayerDead = true; | 
|---|
| 765 |     this->showStats(); | 
|---|
| 766 |   } | 
|---|
| 767 |  | 
|---|
| 768 |   this->respawnPlayable( victimStats.getPlayable(), victimStats.getTeamId(), 3.0f ); | 
|---|
| 769 | } | 
|---|
| 770 |  | 
|---|
| 771 | /** | 
|---|
| 772 |  * this function is called on player respawn | 
|---|
| 773 |  * @param userId | 
|---|
| 774 |  */ | 
|---|
| 775 | void MultiplayerTeamDeathmatch::onRespawn( int userId ) | 
|---|
| 776 | { | 
|---|
| 777 |   if ( userId == SharedNetworkData::getInstance()->getHostID() ) | 
|---|
| 778 |   { | 
|---|
| 779 |     this->bLocalPlayerDead = false; | 
|---|
| 780 |     this->hideStats(); | 
|---|
| 781 |   } | 
|---|
| 782 | } | 
|---|
| 783 |  | 
|---|
| 784 | /** | 
|---|
| 785 |  * this function is called on player respawn | 
|---|
| 786 |  * @param we | 
|---|
| 787 |  */ | 
|---|
| 788 | void MultiplayerTeamDeathmatch::registerSpawn( WorldEntity * we ) | 
|---|
| 789 | { | 
|---|
| 790 |   onRespawn( we->getOwner() ); | 
|---|
| 791 | } | 
|---|
| 792 |  | 
|---|
| 793 |  | 
|---|
| 794 | /** | 
|---|
| 795 |  * respawns a playable in the world via spawning points | 
|---|
| 796 |  * @param playable the playable to respawn | 
|---|
| 797 |  * @param teamId the teamId to use | 
|---|
| 798 |  * @param delay time delay for delayed spawning | 
|---|
| 799 |  */ | 
|---|
| 800 | void MultiplayerTeamDeathmatch::respawnPlayable( Playable * playable, int teamId, float delay ) | 
|---|
| 801 | { | 
|---|
| 802 |  | 
|---|
| 803 |   std::vector<SpawningPoint*> spList; | 
|---|
| 804 |  | 
|---|
| 805 |   for (ObjectList<SpawningPoint>::const_iterator it = SpawningPoint::objectList().begin(); | 
|---|
| 806 |        it != SpawningPoint::objectList().end(); | 
|---|
| 807 |        ++it) | 
|---|
| 808 |   { | 
|---|
| 809 |     SpawningPoint * sp = (*it); | 
|---|
| 810 |  | 
|---|
| 811 |     if ( sp->getTeamId() == teamId ) | 
|---|
| 812 |       spList.push_back( sp ); | 
|---|
| 813 |   } | 
|---|
| 814 |  | 
|---|
| 815 |   if ( spList.size() == 0 ) | 
|---|
| 816 |   { | 
|---|
| 817 |     for (ObjectList<SpawningPoint>::const_iterator it = SpawningPoint::objectList().begin(); | 
|---|
| 818 |          it != SpawningPoint::objectList().end(); | 
|---|
| 819 |          ++it) | 
|---|
| 820 |     { | 
|---|
| 821 |       SpawningPoint * sp = (*it); | 
|---|
| 822 |  | 
|---|
| 823 |       if ( sp->getTeamId() < 0 ) | 
|---|
| 824 |         spList.push_back( sp ); | 
|---|
| 825 |     } | 
|---|
| 826 |   } | 
|---|
| 827 |  | 
|---|
| 828 |   assert( spList.size() != 0 ); | 
|---|
| 829 |  | 
|---|
| 830 |   int n = (int)((float)spList.size() * (float)rand()/(float)RAND_MAX); | 
|---|
| 831 |  | 
|---|
| 832 |   spList[n]->pushEntity( playable, delay ); | 
|---|
| 833 | } | 
|---|
| 834 |  | 
|---|
| 835 |  | 
|---|