Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/util/multiplayer_team_deathmatch.cc @ 9135

Last change on this file since 9135 was 9119, checked in by rennerc, 19 years ago

added scling for playables

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