// free for all game mode

if (!$gameModeAdded["GoldenSword"])
{
   $gameModeAdded["GoldenSword"] = true;
   $gameModeName[$nGameMode] = "GoldenSword";
   $gameModePrintName[$nGameMode] = "Golden Sword";
   $gameModePrintInServerList[$nGameMode] = true;
   $nGameMode ++;
}

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

// called when the server loads the map
function GoldenSword::ServerStarted(%this)
{
   if ($debugMissionLoad)
      echo("GMServerStarted GoldenSword");

   $GMMissionArea = MissionArea;


   // artefact stuff

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


   $gameModePrintText = "Playing Golden Sword.  Kill the man with the golden sword, and then you will become him!";

   setCurrentWeaponList("sword");

   setTimedRoundsOn($Pref::Server::gsDefaultRoundTime, "None 2 5 10 20 30");

   $weaponLimit = 2;
   
   $fightInfo.teamGame = true;
   
   $golden = 0;
}


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

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


   if ($debugMissionLoad)
      echo("GMClientEnterGame GoldenSword");
      
   setTeam(%client, 1);
}

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

}

// returns true if this client is allowed to join the match
function GoldenSword::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 GoldenSword::JoinMatch(%this, %client, %character, %newClient, %team)
{
   if (%client.player.isABot)
   {
      %client.player.setAiState("ChaseFacingOff");
   }

   if ($golden)
      %pos = getWords($golden.player.getTransform(), 0, 2);
   else
      %pos = getWords($artefact.getTransform(), 0, 2);

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

   if (!$golden)
   {
      // if we're about to be made Golden, spawn us high
      %pos = setWord(%pos, 2, getTerrainOrWaterHeight(getWords(%pos, 0, 1)) + 50);
   }
   else
   {
      %pos = setWord(%pos, 2, getTerrainOrWaterHeight(getWords(%pos, 0, 1)) + 10);
   }
   
   %client.player.setTransform(%pos @ " 1 0 0 0");
   
   %client.player.setBaseThrustForce(1);
   %client.player.setGroundAddition(0.5);
   
   if (!$golden)
      makePlayerGolden(%client);
}





function getRandomWeaponFromList(%list)
{
   return getWord(%list, getRandom(0, getWordCount(%list) - 1));
}

// called on a client dropping for any reason
function GoldenSword::ClientDropped(%this, %client, %reason)
{
   if (%client == $golden)
   {
      findNewGolden(%client);
   }
}


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


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

}

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

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


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

// called on a player-player collision
function GoldenSword::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 GoldenSword::OnKilled(%this, %killee, %killer, %reason)
{
   echo("GoldenSword::OnKilled" SPC %killee SPC %killer SPC %reason);


   if (%reason $= "swordBlow")
   {
      if (%killer.client == $golden)
      {
         increaseScore(%killer.client, 1);
         
         reportFFAResult(%killer.client, "kill", %killee.client);
      }
      else if (%killee.client == $golden)
      {
         increaseScore(%killer.client, 2);
         
         reportFFAResult(%killer.client, "kill", %killee.client);
         
         makePlayerGolden(%killer.client);
      }
      else
      {
         increaseScore(%killer.client, -2);
         
         messageClient(%killer.client, 'playSound', "You killed a team-mate!", "badbeep");
      }
   }
   
   schedule(1000, 0, "standardKillMessage", %killee, %killer, %reason); 
}


function makePlayerGolden(%client)
{
   messageAllExcept(%client, -20, 'playSound', '%2 has the Golden Sword!', "badSound", %client.name);
   
   messageClient(%client, 'playSound', 'You have the Golden Sword!', "WinWaveSound");

   %client.player.setDamageLevel(0);
   
   //%client.player.setSpecialMode(1);
   
   $golden = %client;
   
   $golden.player.setSpecialFilterColour(5);
   
   giveWeaponTo($golden.player, UberGoldenSword.index);
   
   %client.player.invincible = true;
   
   %client.player.setBaseThrustForce(0.8);
   %client.player.setGroundAddition(0.5);
   
   schedule(2000, 0, "makePlayerVincible", %client.player);
   
   setTeam($golden, 0);
   
   for (%i = 0; %i < ClientGroup.getCount(); %i ++)
   {
      %c = ClientGroup.getObject(%i);
      
      if (%c != $golden)
      {
         setTeam(%c, 1);
      }
   }
}

function makePlayerVincible(%player)
{
   %player.invincible = false;
}



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

}


function GoldenSword::onSuicide(%this, %client)
{
   reportFFAResult(%client, "suicide", 0);

   if (%client == $golden)
   {
      findNewGolden(%client);
   }
}


function findNewGolden(%client)
{
   $golden = 0;
      
   %new = 0;
   
   for (%i = 0; %i < $nPlayers; %i ++)
   {
      if ($client[%i] != %client)
      {
         %new = $client[%i];
         break;
      }
   }
   
   if (%new)
   {
      makePlayerGolden(%new);
   }
}



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

}



function GoldenSword::Trigger(%this, %client, %trigger, %val)
{
   //error("client trigger: " @ %trigger SPC %val);
}



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

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

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

}



//////////////////////////////////////////////////////////////////////

function GoldenSword::onTimedRoundEndRound(%this)
{
   $golden = 0;

   for (%i = 0; %i < ClientGroup.getCount(); %i ++)
   {
      %c = ClientGroup.getObject(%i);

      
   }
}




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


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

   finishWarpArtefact($artefact);
}

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

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

}




