[7034] | 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 | |
---|
[8068] | 17 | #include <map> |
---|
| 18 | |
---|
[7034] | 19 | #include "multiplayer_team_deathmatch.h" |
---|
| 20 | |
---|
[7193] | 21 | #include "util/loading/load_param.h" |
---|
| 22 | #include "util/loading/factory.h" |
---|
[7034] | 23 | |
---|
[7810] | 24 | #include "render2D/image_plane.h" |
---|
[7039] | 25 | #include "state.h" |
---|
[7101] | 26 | #include "class_list.h" |
---|
[7034] | 27 | |
---|
[7044] | 28 | #include "player.h" |
---|
| 29 | #include "playable.h" |
---|
[7101] | 30 | #include "space_ships/space_ship.h" |
---|
[7039] | 31 | |
---|
[7116] | 32 | |
---|
[7101] | 33 | #include "shared_network_data.h" |
---|
[7116] | 34 | #include "terrain.h" |
---|
| 35 | #include "class_list.h" |
---|
| 36 | #include "space_ships/space_ship.h" |
---|
[7044] | 37 | |
---|
[8068] | 38 | #include "network_game_manager.h" |
---|
[7101] | 39 | |
---|
[8147] | 40 | #include "event_handler.h" |
---|
[8068] | 41 | |
---|
[8147] | 42 | #include "glgui.h" |
---|
| 43 | |
---|
| 44 | #include "story_entity.h" |
---|
| 45 | |
---|
[8708] | 46 | #include "shell_command.h" |
---|
[8147] | 47 | |
---|
[8708] | 48 | |
---|
[7034] | 49 | using namespace std; |
---|
| 50 | |
---|
| 51 | |
---|
[7035] | 52 | CREATE_FACTORY(MultiplayerTeamDeathmatch, CL_MULTIPLAYER_TEAM_DEATHMATCH); |
---|
| 53 | |
---|
| 54 | |
---|
[7034] | 55 | /** |
---|
| 56 | * constructor |
---|
| 57 | */ |
---|
[7035] | 58 | MultiplayerTeamDeathmatch::MultiplayerTeamDeathmatch(const TiXmlElement* root) |
---|
[8068] | 59 | : NetworkGameRules(root) |
---|
[7035] | 60 | { |
---|
| 61 | this->setClassID(CL_MULTIPLAYER_TEAM_DEATHMATCH, "MultiplayerTeamDeathmatch"); |
---|
[7034] | 62 | |
---|
[7037] | 63 | this->bLocalPlayerDead = false; |
---|
| 64 | this->deathTimeout = 10.0f; // 5 seconds |
---|
[7082] | 65 | this->timeout = 0.0f; |
---|
[8068] | 66 | this->numTeams = 2; |
---|
| 67 | this->currentGameState = GAMESTATE_PRE_GAME; |
---|
| 68 | this->gameStateTimer = 10.0f; |
---|
[8623] | 69 | this->bShowTeamChange = false; |
---|
[8147] | 70 | |
---|
| 71 | this->box = NULL; |
---|
[8739] | 72 | this->table = NULL; |
---|
| 73 | this->statsBox = NULL; |
---|
[7040] | 74 | |
---|
[7044] | 75 | this->localPlayer = State::getPlayer(); |
---|
| 76 | |
---|
[7035] | 77 | if( root != NULL) |
---|
| 78 | this->loadParams(root); |
---|
[8623] | 79 | |
---|
| 80 | subscribeEvent( ES_GAME, SDLK_o ); |
---|
[8708] | 81 | subscribeEvent( ES_GAME, SDLK_TAB ); |
---|
[8738] | 82 | subscribeEvent( ES_GAME, SDLK_F1 ); |
---|
[8774] | 83 | subscribeEvent( ES_MENU, SDLK_F1 ); |
---|
[8739] | 84 | subscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 ); |
---|
[8708] | 85 | |
---|
| 86 | this->notifier = new OrxGui::GLGuiNotifier(); |
---|
| 87 | this->notifier->show(); |
---|
| 88 | this->notifier->setAbsCoor2D(5, 30); |
---|
| 89 | this->notifier->setFadeAge( 6.0 ); |
---|
| 90 | this->notifier->setHideAge( 8.0 ); |
---|
| 91 | this->input = new OrxGui::GLGuiInputLine(); |
---|
| 92 | this->input->setAbsCoor2D(180, 5); |
---|
| 93 | this->input->connect(SIGNAL(input, enterPushed), this, SLOT(MultiplayerTeamDeathmatch, onInputEnter)); |
---|
[7035] | 94 | } |
---|
| 95 | |
---|
[7034] | 96 | /** |
---|
| 97 | * decontsructor |
---|
| 98 | */ |
---|
| 99 | MultiplayerTeamDeathmatch::~MultiplayerTeamDeathmatch() |
---|
[7044] | 100 | { |
---|
[8623] | 101 | unsubscribeEvent( ES_GAME, SDLK_o ); |
---|
[8708] | 102 | unsubscribeEvent( ES_GAME, SDLK_TAB ); |
---|
[8738] | 103 | unsubscribeEvent( ES_GAME, SDLK_F1 ); |
---|
[8774] | 104 | unsubscribeEvent( ES_MENU, SDLK_F1 ); |
---|
[8739] | 105 | unsubscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 ); |
---|
[8708] | 106 | |
---|
| 107 | if ( this->notifier ) |
---|
| 108 | { |
---|
| 109 | delete this->notifier; |
---|
| 110 | this->notifier = NULL; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | if ( this->input ) |
---|
| 114 | { |
---|
| 115 | delete this->input; |
---|
| 116 | this->input = NULL; |
---|
| 117 | } |
---|
[7044] | 118 | } |
---|
[7034] | 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | void MultiplayerTeamDeathmatch::loadParams(const TiXmlElement* root) |
---|
[7035] | 123 | { |
---|
[7040] | 124 | GameRules::loadParams(root) ; |
---|
[7037] | 125 | |
---|
| 126 | LoadParam(root, "death-penalty-timeout", this, MultiplayerTeamDeathmatch, setDeathPenaltyTimeout) |
---|
| 127 | .describe("sets the time in seconds a player has to wait for respawn"); |
---|
| 128 | |
---|
| 129 | LoadParam(root, "max-kills", this, MultiplayerTeamDeathmatch, setMaxKills) |
---|
| 130 | .describe("sets the maximal kills for winning condition"); |
---|
[7039] | 131 | |
---|
[8068] | 132 | LoadParam(root, "num-teams", this, MultiplayerTeamDeathmatch, setNumTeams) |
---|
| 133 | .describe("sets number of teams"); |
---|
[7039] | 134 | |
---|
[7035] | 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * called when the player enters the game |
---|
| 140 | * @param player the spawned player |
---|
| 141 | */ |
---|
[7044] | 142 | void MultiplayerTeamDeathmatch::onPlayerSpawn() |
---|
[7039] | 143 | { |
---|
[7044] | 144 | this->bLocalPlayerDead = false; |
---|
[7039] | 145 | } |
---|
[7034] | 146 | |
---|
[7035] | 147 | |
---|
| 148 | /** |
---|
| 149 | * when the player is killed |
---|
| 150 | * @param player the killed player |
---|
| 151 | */ |
---|
[7044] | 152 | void MultiplayerTeamDeathmatch::onPlayerDeath() |
---|
[7039] | 153 | { |
---|
| 154 | this->bLocalPlayerDead = true; |
---|
| 155 | } |
---|
[7035] | 156 | |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * time tick |
---|
| 160 | * @param dt time |
---|
| 161 | */ |
---|
| 162 | void MultiplayerTeamDeathmatch::tick(float dt) |
---|
[7037] | 163 | { |
---|
[8739] | 164 | tickStatsTable(); |
---|
[8147] | 165 | //on client side hostId is -1 until hanshake finished |
---|
| 166 | if ( SharedNetworkData::getInstance()->getHostID() < 0 ) |
---|
| 167 | return; |
---|
| 168 | |
---|
[8623] | 169 | if ( currentGameState == GAMESTATE_PRE_GAME || currentGameState == GAMESTATE_GAME ) |
---|
[8147] | 170 | { |
---|
| 171 | if ( PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) |
---|
| 172 | && box == NULL |
---|
[8623] | 173 | && (PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPreferedTeamId() == TEAM_NOTEAM |
---|
| 174 | || bShowTeamChange ) |
---|
| 175 | |
---|
[8147] | 176 | ) |
---|
| 177 | { |
---|
| 178 | EventHandler::getInstance()->pushState( ES_MENU ); |
---|
| 179 | |
---|
| 180 | OrxGui::GLGuiHandler::getInstance()->activateCursor(); |
---|
| 181 | |
---|
| 182 | box = new OrxGui::GLGuiBox(); |
---|
| 183 | box->setAbsCoor2D( 300, 100 ); |
---|
| 184 | |
---|
| 185 | OrxGui::GLGuiPushButton * buttonSpectator = new OrxGui::GLGuiPushButton("Spectator"); |
---|
| 186 | box->pack( buttonSpectator ); |
---|
| 187 | buttonSpectator->connect(SIGNAL(buttonSpectator, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonSpectator)); |
---|
| 188 | |
---|
| 189 | OrxGui::GLGuiPushButton * buttonRandom = new OrxGui::GLGuiPushButton("Random"); |
---|
| 190 | box->pack( buttonRandom ); |
---|
| 191 | buttonRandom->connect(SIGNAL(buttonRandom, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonRandom)); |
---|
| 192 | |
---|
| 193 | OrxGui::GLGuiPushButton * buttonTeam0 = new OrxGui::GLGuiPushButton("Blue Team"); |
---|
| 194 | box->pack( buttonTeam0 ); |
---|
| 195 | buttonTeam0->connect(SIGNAL(buttonTeam0, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonTeam0)); |
---|
| 196 | |
---|
| 197 | OrxGui::GLGuiPushButton * buttonTeam1 = new OrxGui::GLGuiPushButton("Red Team"); |
---|
| 198 | box->pack( buttonTeam1 ); |
---|
| 199 | buttonTeam1->connect(SIGNAL(buttonTeam1, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonTeam1)); |
---|
| 200 | |
---|
[8623] | 201 | if ( bShowTeamChange ) |
---|
| 202 | { |
---|
| 203 | OrxGui::GLGuiPushButton * buttonCancel = new OrxGui::GLGuiPushButton("Cancel"); |
---|
| 204 | box->pack( buttonCancel ); |
---|
| 205 | buttonCancel->connect(SIGNAL(buttonCancel, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonCancel)); |
---|
| 206 | } |
---|
| 207 | |
---|
[8147] | 208 | OrxGui::GLGuiPushButton * buttonExit = new OrxGui::GLGuiPushButton("Exit"); |
---|
| 209 | box->pack( buttonExit ); |
---|
| 210 | buttonExit->connect(SIGNAL(buttonExit, released), this, SLOT(MultiplayerTeamDeathmatch, onButtonExit)); |
---|
| 211 | |
---|
| 212 | box->showAll(); |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if ( box != NULL |
---|
| 217 | && PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) |
---|
| 218 | && PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPreferedTeamId() != TEAM_NOTEAM |
---|
[8623] | 219 | && !bShowTeamChange |
---|
[8147] | 220 | ) |
---|
| 221 | { |
---|
| 222 | delete box; |
---|
| 223 | box = NULL; |
---|
| 224 | |
---|
| 225 | OrxGui::GLGuiHandler::getInstance()->deactivateCursor( true ); |
---|
| 226 | |
---|
| 227 | EventHandler::getInstance()->popState(); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | if ( box != NULL ) |
---|
| 231 | { |
---|
| 232 | OrxGui::GLGuiHandler::getInstance()->tick( dt ); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | assignPlayable(); |
---|
| 236 | |
---|
[8068] | 237 | if ( !SharedNetworkData::getInstance()->isGameServer() ) |
---|
| 238 | return; |
---|
| 239 | |
---|
| 240 | gameStateTimer -= dt; |
---|
[8147] | 241 | //PRINTF(0)("TICK %f\n", gameStateTimer); |
---|
[8068] | 242 | |
---|
| 243 | if ( currentGameState != GAMESTATE_GAME && gameStateTimer < 0 ) |
---|
| 244 | nextGameState(); |
---|
| 245 | |
---|
| 246 | this->currentGameState = NetworkGameManager::getInstance()->getGameState(); |
---|
| 247 | |
---|
| 248 | if ( currentGameState == GAMESTATE_GAME ) |
---|
| 249 | { |
---|
| 250 | handleTeamChanges(); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | this->calculateTeamScore(); |
---|
| 254 | |
---|
[7039] | 255 | this->checkGameRules(); |
---|
[7035] | 256 | |
---|
[7039] | 257 | // is the local player dead and inactive |
---|
| 258 | if( unlikely(this->bLocalPlayerDead)) |
---|
| 259 | { |
---|
| 260 | this->timeout += dt; |
---|
[7088] | 261 | PRINTF(0)("TICK DEATH: %f of %f\n", this->timeout, this->deathTimeout); |
---|
[7039] | 262 | // long enough dead? |
---|
[7044] | 263 | if( this->timeout >= this->deathTimeout) |
---|
[7039] | 264 | { |
---|
| 265 | this->timeout = 0.0f; |
---|
| 266 | // respawn |
---|
[7079] | 267 | PRINTF(0)("RESPAWN\n"); |
---|
[7044] | 268 | (State::getPlayer())->getPlayable()->respawn(); |
---|
[7039] | 269 | } |
---|
| 270 | } |
---|
[7037] | 271 | } |
---|
[7035] | 272 | |
---|
[7037] | 273 | |
---|
[7035] | 274 | /** |
---|
| 275 | * draws the stuff |
---|
| 276 | */ |
---|
| 277 | void MultiplayerTeamDeathmatch::draw() |
---|
[7039] | 278 | { |
---|
| 279 | if( unlikely( this->bLocalPlayerDead)) |
---|
| 280 | { |
---|
[7035] | 281 | |
---|
[7039] | 282 | } |
---|
| 283 | } |
---|
[7035] | 284 | |
---|
[7039] | 285 | |
---|
[7035] | 286 | /** |
---|
| 287 | * check the game rules for consistency |
---|
| 288 | */ |
---|
| 289 | void MultiplayerTeamDeathmatch::checkGameRules() |
---|
[7039] | 290 | { |
---|
[8068] | 291 | if ( !SharedNetworkData::getInstance()->isGameServer() ) |
---|
| 292 | return; |
---|
| 293 | |
---|
| 294 | // check for max killing count |
---|
| 295 | for ( int i = 0; i<numTeams; i++ ) |
---|
| 296 | { |
---|
| 297 | if ( teamScore[i] >= maxKills ) |
---|
| 298 | { |
---|
| 299 | //team i wins |
---|
| 300 | //TODO |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | } |
---|
[7101] | 304 | |
---|
[8068] | 305 | /** |
---|
| 306 | * find group for new player |
---|
| 307 | * @return group id |
---|
| 308 | */ |
---|
[8147] | 309 | int MultiplayerTeamDeathmatch::getTeamForNewUser() |
---|
[8068] | 310 | { |
---|
| 311 | return TEAM_NOTEAM; |
---|
| 312 | } |
---|
[7101] | 313 | |
---|
[8147] | 314 | ClassID MultiplayerTeamDeathmatch::getPlayableClassId( int userId, int team ) |
---|
[8068] | 315 | { |
---|
[8147] | 316 | if ( team == TEAM_NOTEAM || team == TEAM_SPECTATOR ) |
---|
| 317 | return CL_SPECTATOR; |
---|
| 318 | |
---|
| 319 | if ( team == 0 || team == 1 ) |
---|
| 320 | return CL_SPACE_SHIP; |
---|
| 321 | |
---|
| 322 | assert( false ); |
---|
[8068] | 323 | } |
---|
[7101] | 324 | |
---|
[8147] | 325 | std::string MultiplayerTeamDeathmatch::getPlayableModelFileName( int userId, int team, ClassID classId ) |
---|
[8068] | 326 | { |
---|
[8147] | 327 | if ( team == 0 ) |
---|
| 328 | return "models/ships/reap_#.obj"; |
---|
| 329 | else if ( team == 1 ) |
---|
| 330 | return "models/ships/fighter.obj"; |
---|
| 331 | else |
---|
| 332 | return ""; |
---|
[8068] | 333 | } |
---|
| 334 | |
---|
| 335 | /** |
---|
| 336 | * calculate team score |
---|
| 337 | */ |
---|
| 338 | void MultiplayerTeamDeathmatch::calculateTeamScore( ) |
---|
| 339 | { |
---|
| 340 | teamScore.clear(); |
---|
| 341 | |
---|
| 342 | for ( int i = 0; i<numTeams; i++ ) |
---|
| 343 | teamScore[i] = 0; |
---|
| 344 | |
---|
| 345 | |
---|
| 346 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
| 347 | |
---|
| 348 | if ( !list ) |
---|
| 349 | return; |
---|
| 350 | |
---|
| 351 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
[7039] | 352 | { |
---|
[8068] | 353 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
| 354 | |
---|
| 355 | if ( stats.getTeamId() >= 0 ) |
---|
| 356 | { |
---|
| 357 | teamScore[stats.getTeamId()] += stats.getScore(); |
---|
| 358 | } |
---|
[7039] | 359 | } |
---|
[8068] | 360 | } |
---|
| 361 | |
---|
| 362 | /** |
---|
| 363 | * get team for player who choose to join random team |
---|
| 364 | * @return smallest team |
---|
| 365 | */ |
---|
| 366 | int MultiplayerTeamDeathmatch::getRandomTeam( ) |
---|
| 367 | { |
---|
| 368 | std::map<int,int> playersInTeam; |
---|
| 369 | |
---|
| 370 | for ( int i = 0; i<numTeams; i++ ) |
---|
| 371 | playersInTeam[i] = 0; |
---|
| 372 | |
---|
| 373 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
| 374 | |
---|
| 375 | if ( !list ) |
---|
| 376 | return 0; |
---|
| 377 | |
---|
| 378 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
[7039] | 379 | { |
---|
[8068] | 380 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
| 381 | |
---|
| 382 | if ( stats.getTeamId() >= 0 ) |
---|
| 383 | { |
---|
| 384 | playersInTeam[stats.getTeamId()]++; |
---|
| 385 | } |
---|
[7039] | 386 | } |
---|
[8068] | 387 | |
---|
| 388 | |
---|
| 389 | int minPlayers = 0xFFFF; |
---|
| 390 | int minTeam = -1; |
---|
| 391 | |
---|
| 392 | for ( int i = 0; i<numTeams; i++ ) |
---|
| 393 | { |
---|
| 394 | if ( playersInTeam[i] < minPlayers ) |
---|
| 395 | { |
---|
| 396 | minTeam = i; |
---|
| 397 | minPlayers = playersInTeam[i]; |
---|
| 398 | } |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | assert( minTeam != -1 ); |
---|
| 402 | |
---|
| 403 | return minTeam; |
---|
| 404 | } |
---|
[7101] | 405 | |
---|
[8068] | 406 | void MultiplayerTeamDeathmatch::nextGameState( ) |
---|
| 407 | { |
---|
| 408 | if ( currentGameState == GAMESTATE_PRE_GAME ) |
---|
| 409 | { |
---|
| 410 | NetworkGameManager::getInstance()->setGameState( GAMESTATE_GAME ); |
---|
| 411 | |
---|
| 412 | return; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | if ( currentGameState == GAMESTATE_GAME ) |
---|
| 416 | { |
---|
| 417 | NetworkGameManager::getInstance()->setGameState( GAMESTATE_POST_GAME ); |
---|
| 418 | |
---|
| 419 | return; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | if ( currentGameState == GAMESTATE_POST_GAME ) |
---|
| 423 | { |
---|
| 424 | //TODO end game |
---|
| 425 | |
---|
| 426 | return; |
---|
| 427 | } |
---|
| 428 | } |
---|
[7118] | 429 | |
---|
[8068] | 430 | void MultiplayerTeamDeathmatch::handleTeamChanges( ) |
---|
| 431 | { |
---|
| 432 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
| 433 | |
---|
| 434 | if ( !list ) |
---|
| 435 | return; |
---|
| 436 | |
---|
| 437 | //first server players with choices |
---|
| 438 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
[7116] | 439 | { |
---|
[8068] | 440 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
[7118] | 441 | |
---|
[8068] | 442 | if ( stats.getTeamId() != stats.getPreferedTeamId() ) |
---|
[7116] | 443 | { |
---|
[8147] | 444 | if ( stats.getPreferedTeamId() == TEAM_SPECTATOR || ( stats.getPreferedTeamId() >= 0 && stats.getPreferedTeamId() < numTeams ) ) |
---|
[7116] | 445 | { |
---|
[8068] | 446 | teamChange( stats.getUserId() ); |
---|
[7116] | 447 | } |
---|
| 448 | } |
---|
| 449 | } |
---|
[8068] | 450 | |
---|
| 451 | //now serve player who want join a random team |
---|
| 452 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
| 453 | { |
---|
| 454 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
[7101] | 455 | |
---|
[8068] | 456 | if ( stats.getTeamId() != stats.getPreferedTeamId() ) |
---|
[7101] | 457 | { |
---|
[8068] | 458 | if ( stats.getPreferedTeamId() == TEAM_RANDOM ) |
---|
[7101] | 459 | { |
---|
[8147] | 460 | stats.setPreferedTeamId( getRandomTeam() ); |
---|
[8068] | 461 | teamChange( stats.getUserId() ); |
---|
[7101] | 462 | } |
---|
| 463 | } |
---|
| 464 | } |
---|
[8068] | 465 | } |
---|
[7101] | 466 | |
---|
[8068] | 467 | void MultiplayerTeamDeathmatch::teamChange( int userId ) |
---|
| 468 | { |
---|
| 469 | assert( PlayerStats::getStats( userId ) ); |
---|
| 470 | PlayerStats & stats = *(PlayerStats::getStats( userId )); |
---|
| 471 | |
---|
[8147] | 472 | stats.setTeamId( stats.getPreferedTeamId() ); |
---|
| 473 | |
---|
| 474 | Playable * oldPlayable = stats.getPlayable(); |
---|
| 475 | |
---|
| 476 | |
---|
| 477 | ClassID playableClassId = getPlayableClassId( userId, stats.getPreferedTeamId() ); |
---|
| 478 | std::string playableModel = getPlayableModelFileName( userId, stats.getPreferedTeamId(), playableClassId ); |
---|
| 479 | |
---|
| 480 | BaseObject * bo = Factory::fabricate( playableClassId ); |
---|
| 481 | |
---|
| 482 | assert( bo != NULL ); |
---|
| 483 | assert( bo->isA( CL_PLAYABLE ) ); |
---|
| 484 | |
---|
| 485 | Playable & playable = *(dynamic_cast<Playable*>(bo)); |
---|
| 486 | |
---|
| 487 | playable.loadModel( playableModel ); |
---|
| 488 | playable.setOwner( userId ); |
---|
| 489 | playable.setUniqueID( SharedNetworkData::getInstance()->getNewUniqueID() ); |
---|
| 490 | playable.setSynchronized( true ); |
---|
| 491 | |
---|
| 492 | stats.setTeamId( stats.getPreferedTeamId() ); |
---|
| 493 | stats.setPlayableClassId( playableClassId ); |
---|
| 494 | stats.setPlayableUniqueId( playable.getUniqueID() ); |
---|
| 495 | stats.setModelFileName( playableModel ); |
---|
| 496 | |
---|
| 497 | if ( oldPlayable ) |
---|
| 498 | { |
---|
| 499 | //if ( userId == SharedNetworkData::getInstance()->getHostID() ) |
---|
| 500 | // State::getPlayer()->setPlayable( NULL ); |
---|
| 501 | delete oldPlayable; |
---|
| 502 | } |
---|
[7039] | 503 | } |
---|
[7035] | 504 | |
---|
[8147] | 505 | void MultiplayerTeamDeathmatch::onButtonExit( ) |
---|
| 506 | { |
---|
| 507 | State::getCurrentStoryEntity()->stop(); |
---|
[8623] | 508 | this->bShowTeamChange = false; |
---|
[8147] | 509 | } |
---|
[7035] | 510 | |
---|
[8147] | 511 | void MultiplayerTeamDeathmatch::onButtonRandom( ) |
---|
| 512 | { |
---|
| 513 | NetworkGameManager::getInstance()->prefereTeam( TEAM_RANDOM ); |
---|
[8623] | 514 | this->bShowTeamChange = false; |
---|
[8147] | 515 | } |
---|
[7035] | 516 | |
---|
[8147] | 517 | void MultiplayerTeamDeathmatch::onButtonTeam0( ) |
---|
| 518 | { |
---|
| 519 | NetworkGameManager::getInstance()->prefereTeam( 0 ); |
---|
[8623] | 520 | this->bShowTeamChange = false; |
---|
[8147] | 521 | } |
---|
[7035] | 522 | |
---|
[8147] | 523 | void MultiplayerTeamDeathmatch::onButtonTeam1( ) |
---|
| 524 | { |
---|
| 525 | NetworkGameManager::getInstance()->prefereTeam( 1 ); |
---|
[8623] | 526 | this->bShowTeamChange = false; |
---|
[8147] | 527 | } |
---|
[7035] | 528 | |
---|
[8147] | 529 | void MultiplayerTeamDeathmatch::onButtonSpectator( ) |
---|
| 530 | { |
---|
| 531 | NetworkGameManager::getInstance()->prefereTeam( TEAM_SPECTATOR ); |
---|
[8623] | 532 | this->bShowTeamChange = false; |
---|
[8147] | 533 | } |
---|
[7035] | 534 | |
---|
[8147] | 535 | void MultiplayerTeamDeathmatch::assignPlayable( ) |
---|
| 536 | { |
---|
| 537 | if ( PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() ) ) |
---|
| 538 | PlayerStats::getStats( SharedNetworkData::getInstance()->getHostID() )->getPlayable(); |
---|
| 539 | } |
---|
| 540 | |
---|
[8623] | 541 | /** |
---|
| 542 | * function that processes events from the handler |
---|
| 543 | * @param event: the event |
---|
| 544 | * @todo replace SDLK_o with something from KeyMapper |
---|
| 545 | */ |
---|
| 546 | void MultiplayerTeamDeathmatch::process( const Event & event ) |
---|
| 547 | { |
---|
| 548 | if ( event.type == SDLK_o ) |
---|
| 549 | { |
---|
| 550 | if ( event.bPressed ) |
---|
| 551 | this->bShowTeamChange = true; |
---|
[8738] | 552 | } else if ( event.type == SDLK_F1 ) |
---|
| 553 | { |
---|
[8774] | 554 | if ( !this->statsBox && event.bPressed ) |
---|
| 555 | { |
---|
| 556 | PRINTF(0)("show stats\n"); |
---|
[8738] | 557 | this->showStats(); |
---|
[8774] | 558 | } |
---|
| 559 | if ( this->statsBox && !this->bLocalPlayerDead && event.bPressed && this->table->rowCount() != 0 && this->table->columnCount() != 0 ) |
---|
| 560 | { |
---|
| 561 | PRINTF(0)("hide stats\n"); |
---|
| 562 | this->hideStats(); |
---|
| 563 | } |
---|
[8623] | 564 | } |
---|
[8708] | 565 | else if ( event.type == SDLK_TAB ) |
---|
| 566 | { |
---|
| 567 | if ( !event.bPressed ) |
---|
| 568 | { |
---|
| 569 | EventHandler::getInstance()->pushState( ES_MENU ); |
---|
| 570 | OrxGui::GLGuiHandler::getInstance()->activateCursor(); |
---|
| 571 | OrxGui::GLGuiHandler::getInstance()->deactivateCursor(); |
---|
| 572 | input->show(); |
---|
| 573 | input->giveFocus(); |
---|
| 574 | input->setText("say "); |
---|
| 575 | } |
---|
| 576 | } |
---|
[8774] | 577 | else if ( this->bLocalPlayerDead && statsBox && event.type == KeyMapper::PEV_FIRE1 ) |
---|
[8739] | 578 | { |
---|
| 579 | this->hideStats(); |
---|
| 580 | } |
---|
[8623] | 581 | } |
---|
[8147] | 582 | |
---|
[8623] | 583 | void MultiplayerTeamDeathmatch::onButtonCancel( ) |
---|
| 584 | { |
---|
| 585 | this->bShowTeamChange = false; |
---|
| 586 | } |
---|
[8147] | 587 | |
---|
| 588 | |
---|
| 589 | |
---|
[8623] | 590 | /** |
---|
| 591 | * this method is called by NetworkGameManger when he recieved a chat message |
---|
| 592 | * @param userId senders user id |
---|
| 593 | * @param message message string |
---|
| 594 | * @param messageType some int |
---|
| 595 | */ |
---|
| 596 | void MultiplayerTeamDeathmatch::handleChatMessage( int userId, const std::string & message, int messageType ) |
---|
| 597 | { |
---|
| 598 | std::string name = "unknown"; |
---|
| 599 | |
---|
| 600 | if ( PlayerStats::getStats( userId ) ) |
---|
| 601 | { |
---|
| 602 | name = PlayerStats::getStats( userId )->getNickName(); |
---|
| 603 | } |
---|
| 604 | |
---|
[8708] | 605 | PRINTF(0)("CHATMESSAGE %s (%d): %s\n", name.c_str(), userId, message.c_str() ); |
---|
| 606 | notifier->pushNotifyMessage(name + ": " + message); |
---|
[8623] | 607 | } |
---|
[8147] | 608 | |
---|
[8708] | 609 | void MultiplayerTeamDeathmatch::onInputEnter( const std::string & text ) |
---|
| 610 | { |
---|
| 611 | EventHandler::getInstance()->popState(); |
---|
| 612 | input->breakFocus(); |
---|
| 613 | input->hide(); |
---|
| 614 | input->setText(""); |
---|
[8623] | 615 | |
---|
[8708] | 616 | std::string command = text; |
---|
| 617 | |
---|
| 618 | //HACK insert " in say commands so user doesn't have to type them |
---|
| 619 | if ( command.length() >= 4 && command[0] == 's' && command[1] == 'a' && command[2] == 'y' && command[3] == ' ' ) |
---|
| 620 | { |
---|
| 621 | command.insert( 4, "\"" ); |
---|
| 622 | command = command + "\""; |
---|
| 623 | } |
---|
[8623] | 624 | |
---|
[8708] | 625 | OrxShell::ShellCommand::execute( command ); |
---|
| 626 | } |
---|
[8623] | 627 | |
---|
[8738] | 628 | /** |
---|
| 629 | * show table with frags |
---|
| 630 | */ |
---|
| 631 | void MultiplayerTeamDeathmatch::showStats( ) |
---|
| 632 | { |
---|
| 633 | EventHandler::getInstance()->pushState( ES_MENU ); |
---|
| 634 | statsBox = new OrxGui::GLGuiBox(); |
---|
| 635 | statsBox->setAbsCoor2D( 300, 100 ); |
---|
| 636 | |
---|
[8774] | 637 | this->table = new OrxGui::GLGuiTable(0,0); |
---|
[8739] | 638 | |
---|
| 639 | statsBox->pack( this->table ); |
---|
[8774] | 640 | |
---|
[8738] | 641 | statsBox->showAll(); |
---|
| 642 | } |
---|
[8708] | 643 | |
---|
[8738] | 644 | /** |
---|
| 645 | * hide table with frags |
---|
| 646 | */ |
---|
| 647 | void MultiplayerTeamDeathmatch::hideStats( ) |
---|
| 648 | { |
---|
[8739] | 649 | if ( statsBox ) |
---|
| 650 | { |
---|
| 651 | delete statsBox; |
---|
| 652 | statsBox = NULL; |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | EventHandler::getInstance()->popState(); |
---|
[8738] | 656 | } |
---|
[8708] | 657 | |
---|
[8739] | 658 | /** |
---|
| 659 | * fill stats table with values |
---|
| 660 | */ |
---|
| 661 | void MultiplayerTeamDeathmatch::tickStatsTable( ) |
---|
| 662 | { |
---|
| 663 | if ( !this->statsBox ) |
---|
| 664 | return; |
---|
[8708] | 665 | |
---|
[8739] | 666 | std::vector<std::string> headers; |
---|
| 667 | headers.push_back("Red Team"); |
---|
| 668 | headers.push_back(""); |
---|
[8773] | 669 | headers.push_back(""); |
---|
[8739] | 670 | headers.push_back("Blue Team"); |
---|
| 671 | headers.push_back(""); |
---|
| 672 | this->table->setHeader(headers); |
---|
| 673 | |
---|
[8773] | 674 | std::map<int,std::string> fragsTeam0; |
---|
| 675 | std::map<int,std::string> fragsTeam1; |
---|
| 676 | |
---|
| 677 | const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS ); |
---|
| 678 | |
---|
| 679 | if ( !list ) |
---|
| 680 | return; |
---|
| 681 | |
---|
| 682 | for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ ) |
---|
| 683 | { |
---|
| 684 | PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it); |
---|
| 685 | |
---|
[8774] | 686 | if ( stats.getTeamId() == 0 || stats.getTeamId() == 1 ) |
---|
| 687 | { |
---|
| 688 | if ( stats.getTeamId() == 0 ) |
---|
| 689 | fragsTeam0[stats.getScore()] = stats.getNickName(); |
---|
| 690 | else |
---|
| 691 | fragsTeam1[stats.getScore()] = stats.getNickName(); |
---|
| 692 | } |
---|
[8773] | 693 | } |
---|
| 694 | |
---|
| 695 | char st[10]; |
---|
| 696 | int i = 0; |
---|
| 697 | |
---|
| 698 | |
---|
| 699 | i = 2; |
---|
| 700 | for ( std::map<int,std::string>::const_iterator it = fragsTeam0.begin(); it != fragsTeam0.end(); it++ ) |
---|
| 701 | { |
---|
| 702 | this->table->setEntry( 0, i, it->second ); |
---|
| 703 | snprintf( st, 10, "%d", it->first ); |
---|
| 704 | this->table->setEntry( 1, i, st ); |
---|
| 705 | this->table->setEntry( 2, i, "" ); |
---|
| 706 | i++; |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | i = 2; |
---|
| 710 | for ( std::map<int,std::string>::const_iterator it = fragsTeam1.begin(); it != fragsTeam1.end(); it++ ) |
---|
| 711 | { |
---|
| 712 | this->table->setEntry( 3, i, it->second ); |
---|
| 713 | snprintf( st, 10, "%d", it->first ); |
---|
| 714 | this->table->setEntry( 4, i, st ); |
---|
| 715 | i++; |
---|
| 716 | } |
---|
[8739] | 717 | } |
---|
[8738] | 718 | |
---|
[8782] | 719 | /** |
---|
| 720 | * this function is called when a player kills another one or himself |
---|
| 721 | * @param killedUserId |
---|
| 722 | * @param userId |
---|
| 723 | */ |
---|
| 724 | void MultiplayerTeamDeathmatch::onKill( int killedUserId, int userId ) |
---|
| 725 | { |
---|
| 726 | assert( PlayerStats::getStats( killedUserId ) ); |
---|
| 727 | assert( PlayerStats::getStats( userId ) ); |
---|
| 728 | |
---|
| 729 | PlayerStats & killedStats = *PlayerStats::getStats( killedUserId ); |
---|
| 730 | PlayerStats & stats = *PlayerStats::getStats( userId ); |
---|
| 731 | |
---|
| 732 | if ( killedUserId != userId ) |
---|
| 733 | stats.setScore( stats.getScore() + 1 ); |
---|
| 734 | else |
---|
| 735 | stats.setScore( stats.getScore() - 1 ); |
---|
| 736 | |
---|
| 737 | killedStats.getPlayable()->toList( OM_DEAD ); |
---|
| 738 | |
---|
| 739 | if ( killedUserId == SharedNetworkData::getInstance()->getHostID() ) |
---|
| 740 | { |
---|
| 741 | this->bLocalPlayerDead = true; |
---|
| 742 | this->showStats(); |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | //TODO respawn killed palyer |
---|
| 746 | |
---|
| 747 | } |
---|
[8738] | 748 | |
---|
[8782] | 749 | /** |
---|
| 750 | * this function is called on player respawn |
---|
| 751 | * @param userId |
---|
| 752 | */ |
---|
| 753 | void MultiplayerTeamDeathmatch::onRespawn( int userId ) |
---|
| 754 | { |
---|
| 755 | if ( userId == SharedNetworkData::getInstance()->getHostID() ) |
---|
| 756 | { |
---|
| 757 | this->bLocalPlayerDead = false; |
---|
| 758 | this->hideStats(); |
---|
| 759 | } |
---|
| 760 | } |
---|
[8739] | 761 | |
---|