// team dm game mode

if (!$gameModeAdded["TeamDeathmatch"])
{
$gameModeAdded["TeamDeathmatch"] = true;
$gameModeName[$nGameMode] = "TeamDeathmatch";
$gameModePrintName[$nGameMode] = "Team Deathmatch";
$gameModePrintInServerList[$nGameMode] = true;
$nGameMode ++;
}


////////////////////////////////////////////////////////////////////////////////
// initialisation stuff

// called when the server loads the map
function TeamDeathmatch::ServerStarted(%this)
{

   if ($debugMissionLoad)
      echo("GMServerStarted TeamDeathmatch");

   $GMMissionArea = MissionArea;


   // artefact stuff

   $artefact = createWarpArtefact(getRandomArtefactSpawnPoint());
   $artefact.setScopeAlways();
   setArtefactOccasionalMove($artefact);

   setTimedRoundsOn($Pref::Server::teamDMDefaultRoundTime, "None 2 5 10 20 30");
   setWeaponsListOn($Pref::Server::teamDMDefaultWeaponList, $Pref::Server::teamDMWeaponLists);

   teamGameServerStarted();

   $gameModePrintText = "Playing Team Deathmatch.  Kill the opposing team!.";
}


///////////////////////////////////////////////////////////////////////////////
// player/client management stuff

// new client entering the game
function TeamDeathmatch::ClientEnterGame(%this, %client)
{

   if ($debugMissionLoad)
      echo("GMClientEnterGame TeamDeathmatch");
}

// client fully loaded
function TeamDeathmatch::ClientFullyLoaded(%this, %client)
{
   if ($debugMissionLoad)
      echo("GMClientFullyLoaded TeamDeathmatch");

}

// returns true if this client is allowed to join the match
function TeamDeathmatch::CanJoinMatch(%this, %client)
{

   return (!%client.inFight && $nPlayers < $nSlots);
}

// called on a player joining the match.  Newclient indicates whether the client has been in the match before
function TeamDeathmatch::JoinMatch(%this, %client, %character, %newClient, %team)
{
   if (%client.player.isABot)
   {
      %client.player.setAiState("ChaseFacingOff");
   }

   %pos = getWords($artefact.getTransform(), 0, 2);

   %pos = VectorAdd(%pos, (getRandom() * 2 - 1) * 1000 SPC (getRandom() * 2 - 1) * 1000 SPC 0);

   %pos = setWord(%pos, 2, getTerrainOrWaterHeight(getWords(%pos, 0, 1)) + 10);
   
   %client.player.setTransform(%pos @ " 1 0 0 0");

   teamGamePlayerJoinMatch(%client, %newClient, %team);
}



// called on a client dropping for any reason
function TeamDeathmatch::ClientDropped(%this, %client, %reason)
{

}


// called on a client leaving the match
function TeamDeathmatch::ClientLeavingMatch(%this, %client)
{
   commandToClient(%client, 'SetBottomPrintText', "Left click to continue.", -1);
}


// called on player death or client leaving the match
function TeamDeathmatch::RemovePlayer(%this, %player)
{

}

// call the client and tell him what colours we want the IFFs to be
function TeamDeathmatch::SendClientIFFInfo(%this, %client)
{
   commandToClient(%client, 'setIFFColours', "0 1 0", "1 0 0", "0 0 1", "0 1 1", false);
}

function TeamDeathmatch::playerHasNewTarget(%this, %player, %target)
{
   //error(%player SPC "has new target" SPC %target);
}


//////////////////////////////////////////////////////////////////
// action stuff

// called on a player-player collision
function TeamDeathmatch::OnHit(%this, %hiter, %hitee, %myHitbox, %hisHitbox, %myForce, %hisForce, %myMult, %hisMult, %winner, %myV2, %hisV2)
{
   return standardHitDamage(%hiter, %hitee, %myHitbox, %hisHitbox, %myForce, %hisForce, %myMult, %hisMult, %winner, %myV2, %hisV2);
}





// called when a player is killed by an opponent
function TeamDeathmatch::OnKilled(%this, %killee, %killer, %reason)
{
   echo("TeamDeathmatch::OnKilled" SPC %killee SPC %killer SPC %reason SPC %killer.team SPC %killee.team);

   if (%reason $= "swordBlow")
   {
      if (%killer.client.team != %killee.client.team)
      {
         incTeamScore(%killer.client.team, 1);
      
         %killer.setDamageLevel(%killer.getDamageLevel() - 25);
      }
   }
   else if (%reason $= "bleeding")
   {
      if (%killer.client.team != %killee.client.team)
      {
         incTeamScore(%killer.client.team, 1);
      
         %killer.setDamageLevel(%killer.getDamageLevel() - 25);
      }
   }
   else if (%reason $= "curse")
   {
      if (%killer.client.team != %killee.client.team)
      {
         incTeamScore(%killer.client.team, 1);
      
         %killer.setDamageLevel(%killer.getDamageLevel() - 25);
      }
   }
   else if (%reason $= "gridRupture")
   {
      if (%killer.client.team != %killee.client.team)
      {
         incTeamScore(%killer.client.team, 1);
      
         %killer.setDamageLevel(%killer.getDamageLevel() - 25);
      }
   }
   else if (%reason $= "warpExplosion")
   {
      if (%killer.client.team != %killee.client.team)
      {
         incTeamScore(%killer.client.team, 1);
      }
   }

   // we schedule it a bit later to ensure other messages have got there
   schedule(1000, 0, "standardKillMessage", %killee, %killer, %reason);   
}




// called on a player's death
function TeamDeathmatch::OnDeath(%this, %player)
{

}



function TeamDeathmatch::playerHitItem(%this, %player, %item)
{

}

/////////////////////////////////////////////////////////////////////
// mission area stuff

// called when the player enters the MissionArea mission area struct
function TeamDeathmatch::OnEnterMissionArea(%this, %player)
{
   
}

// called when the player leaves the MissionArea mission area struct
function TeamDeathmatch::OnLeaveMissionArea(%this, %player)
{
  
}



////////////////////////////////////////////////////////////////////
// mission end cleanup


function TeamDeathmatch::OnMissionEnded(%this)
{
   disactiveWarpStorms();

   finishWarpArtefact($artefact);
}

////////////////////////////////////////////////////////////////////
// ball stuff

// player collides with the ball
function TeamDeathmatch::PlayerHitBall(%this, %player, %hitboxID, %ball)
{

}




function TeamDeathmatch::getGameModeCommands(%this, %client)
{
   commandToClient(%client, 'MessageBoxListEntry', 'Team DM Options', 'Here are the commands for Team DM.', 'Do nothing\tSwitch teams', 'Do nothing', 'teamDMCommandReturn');
}

function serverCmdteamDMCommandReturn(%client, %command)
{
   if (%command $= "Switch Teams")
   {
   
      %team = 3 - %client.team;
      
      
      setTeam(%client, %team);
      
      messageAll('playSound', '%2 has switched to team %3', "SwordBallConcede", %client.name, $teamName[%team]);
   
      if (%client.player && %client.player.isAlive())
         %client.player.applyDamage(120);
   }
}