Index: trunk/src/util/multiplayer_team_deathmatch.cc
===================================================================
--- trunk/src/util/multiplayer_team_deathmatch.cc	(revision 8802)
+++ trunk/src/util/multiplayer_team_deathmatch.cc	(revision 9008)
@@ -68,5 +68,5 @@
   this->numTeams = 2;
   this->currentGameState = GAMESTATE_PRE_GAME;
-  this->gameStateTimer = 10.0f;
+  this->gameStateTimer = 3.0f;
   this->bShowTeamChange = false;
 
@@ -85,5 +85,5 @@
   subscribeEvent( ES_MENU, SDLK_F1 );
   subscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 );
-  
+
   this->notifier = new OrxGui::GLGuiNotifier();
   this->notifier->show();
@@ -106,5 +106,5 @@
   unsubscribeEvent( ES_MENU, SDLK_F1 );
   unsubscribeEvent( ES_MENU, KeyMapper::PEV_FIRE1 );
-  
+
   if ( this->notifier )
   {
@@ -219,16 +219,14 @@
   if ( !SharedNetworkData::getInstance()->isGameServer() )
     return;
-    
+
   //handle kills
-  for ( std::vector<Kill>::iterator it = this->killList.begin(); it != this->killList.end();  )
-  {
-    std::vector<Kill>::iterator delit = it;
-    
-    onKill( it->getKiller()->getOwner(), it->getVictim()->getOwner() );
-    
-    it++;
-    killList.erase( delit );
+  while ( this->killList.begin() != this->killList.end() )
+  {
+    onKill( this->killList.begin()->getVictim(), this->killList.begin()->getKiller() );
+    this->killList.erase( this->killList.begin() );
   }
   
+
+
   gameStateTimer -= dt;
   //PRINTF(0)("TICK %f\n", gameStateTimer);
@@ -488,4 +486,6 @@
   stats.setModelFileName( playableModel );
 
+  this->respawnPlayable( &playable, stats.getPreferedTeamId(), 0.0f );
+
   if ( oldPlayable )
   {
@@ -558,5 +558,5 @@
   else if ( event.type == SDLK_TAB )
   {
-    if ( !event.bPressed )
+    if ( currentGameState == GAMESTATE_GAME && event.bPressed && !EventHandler::getInstance()->isPressed( SDLK_RALT ) && !EventHandler::getInstance()->isPressed( SDLK_LALT ) )
     {
       EventHandler::getInstance()->pushState( ES_MENU );
@@ -627,5 +627,5 @@
   statsBox = new OrxGui::GLGuiBox();
   statsBox->setAbsCoor2D( 300, 100 );
-  
+
   this->table = new OrxGui::GLGuiTable(0,0);
 
@@ -645,5 +645,5 @@
       statsBox = NULL;
     }
-      
+
     EventHandler::getInstance()->popState();
 }
@@ -664,17 +664,17 @@
   headers.push_back("");
   this->table->setHeader(headers);
-  
+
   std::map<int,std::string> fragsTeam0;
   std::map<int,std::string> fragsTeam1;
-  
+
   const std::list<BaseObject*> * list = ClassList::getList( CL_PLAYER_STATS );
-  
+
   if ( !list )
     return;
-  
+
   for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
   {
     PlayerStats & stats = *dynamic_cast<PlayerStats*>(*it);
-    
+
     if ( stats.getTeamId() == 0 || stats.getTeamId() == 1 )
     {
@@ -685,9 +685,9 @@
     }
   }
-  
+
   char st[10];
   int i = 0;
-  
-  
+
+
   i = 2;
   for ( std::map<int,std::string>::const_iterator it = fragsTeam0.begin(); it != fragsTeam0.end(); it++ )
@@ -699,5 +699,5 @@
     i++;
   }
-  
+
   i = 2;
   for ( std::map<int,std::string>::const_iterator it = fragsTeam1.begin(); it != fragsTeam1.end(); it++ )
@@ -712,48 +712,51 @@
 /**
  * this function is called when a player kills another one or himself
- * @param killedUserId 
- * @param userId 
- */
-void MultiplayerTeamDeathmatch::onKill( int killedUserId, int userId )
-{
-  assert( PlayerStats::getStats( killedUserId ) );
-  assert( PlayerStats::getStats( userId ) );
+ * @param killedUserId
+ * @param userId
+ */
+void MultiplayerTeamDeathmatch::onKill( WorldEntity * victim, WorldEntity * killer )
+{
+  if ( !victim )
+    return;
+  if ( !killer )
+    return;
   
-  PlayerStats & killedStats = *PlayerStats::getStats( killedUserId );
-  PlayerStats & stats = *PlayerStats::getStats( userId );
+  int killerUserId = killer->getOwner();
+  int victimUserId = victim->getOwner();
+
+  PlayerStats & victimStats = *PlayerStats::getStats( victimUserId );
+  PlayerStats & killerStats = *PlayerStats::getStats( killerUserId );
   
-  if ( killedUserId != userId )
-    stats.setScore( stats.getScore() + 1 );
+  if ( killerStats.getPlayable() != killer || victimStats.getPlayable() != victim )
+    return;
+
+  //check for suicide
+  if ( killerUserId != victimUserId )
+  {
+    //check for teamkill
+    if ( victimStats.getTeamId() != killerStats.getTeamId() )
+    {
+      killerStats.setScore( killerStats.getScore() + 1 );
+    }
+    else
+    {
+      killerStats.setScore( killerStats.getScore() - 1 );
+    }
+  }
   else
-    stats.setScore( stats.getScore() - 1 );
-  
-  if ( killedUserId == SharedNetworkData::getInstance()->getHostID() )
+    killerStats.setScore( killerStats.getScore() - 1 );
+
+  if ( victimUserId == SharedNetworkData::getInstance()->getHostID() )
   {
     this->bLocalPlayerDead = true;
     this->showStats();
   }
-  
-  const std::list<BaseObject*> * list = ClassList::getList( CL_SPAWNING_POINT );
-  
-  assert( list );
-  
-  std::vector<SpawningPoint*> spList;
-  
-  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
-  {
-    SpawningPoint * sp = dynamic_cast<SpawningPoint*>(*it);
-    
-    if ( sp->getTeamId() < 0 || sp->getTeamId() == killedStats.getTeamId() )
-      spList.push_back( sp );
-  }
- 
-  int n = spList.size()*rand();
-  
-  spList[n]->pushEntity( killedStats.getPlayable(), 3 );
+
+  this->respawnPlayable( victimStats.getPlayable(), victimStats.getTeamId(), 3.0f );
 }
 
 /**
  * this function is called on player respawn
- * @param userId 
+ * @param userId
  */
 void MultiplayerTeamDeathmatch::onRespawn( int userId )
@@ -768,5 +771,5 @@
 /**
  * this function is called on player respawn
- * @param we 
+ * @param we
  */
 void MultiplayerTeamDeathmatch::registerSpawn( WorldEntity * we )
@@ -775,2 +778,37 @@
 }
 
+
+void MultiplayerTeamDeathmatch::respawnPlayable( Playable * playable, int teamId, float delay )
+{
+  const std::list<BaseObject*> * list = ClassList::getList( CL_SPAWNING_POINT );
+
+  assert( list );
+
+  std::vector<SpawningPoint*> spList;
+
+  for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
+  {
+    SpawningPoint * sp = dynamic_cast<SpawningPoint*>(*it);
+
+    if ( sp->getTeamId() == teamId )
+      spList.push_back( sp );
+  }
+
+  if ( spList.size() == 0 )
+  {
+    for ( std::list<BaseObject*>::const_iterator it = list->begin(); it != list->end(); it++ )
+    {
+      SpawningPoint * sp = dynamic_cast<SpawningPoint*>(*it);
+
+      if ( sp->getTeamId() < 0 )
+        spList.push_back( sp );
+    }
+  }
+
+  assert( spList.size() != 0 );
+
+  int n = (int)((float)spList.size() * (float)rand()/(float)RAND_MAX);
+
+  spList[n]->pushEntity( playable, delay );
+}
+
