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