// FILENAME:	SS_Chess.cs
//
// SCRIPT BY:     Com. Sentinal [M.I.B.]
//
// TERRAIN BY:  	Gen. Powio™ [MIB]
//------------------------------------------------------------------------------

$missionName = "Starsiege Chess";

exec("multiplayerStdLib.cs");
exec("DMstdLib.cs");

function setDefaultMissionOptions()
{
	$server::TeamPlay = true;
	$server::AllowDeathmatch = false;
	$server::AllowTeamPlay = true;	

   // what can the client choose for a team
	$server::AllowTeamRed = false;
	$server::AllowTeamBlue = false;
	$server::AllowTeamYellow = true;
	$server::AllowTeamPurple = true;

   // what can the server admin choose for available teams
   $server::disableTeamRed = true;
   $server::disableTeamBlue = true;
   $server::disableTeamYellow = false;
   $server::disableTeamPurple = false;
}

$bluePlayer = "";
$redPlayer = "";
$currentTurn = "";
$cursorOn = false;
$selectedPiece = "";
$controlLock = false;
$matchCalled = false;
$gameStarted = false;
$promotion = false;

//Sounds
$cursorPopUp = "button_down.wav";
$cursorMove = "button_up.wav";
$movePiece = "sfx_lgb.wav";
$yourTurn = "bptslct.wav";

function onMissionStart()
{
   marsSounds();

   $highlight = getObjectId("Missiongroup\\highlight");
   $blueCursor = getObjectId("Missiongroup\\blueCursor");
   $redCursor = getObjectId("Missiongroup\\redCursor");
   $blueMatch = getObjectId("Missiongroup\\BlueButtons\\bluematch");
   $blueMove = getObjectId("Missiongroup\\BlueButtons\\bluemove");
   $blueUp = getObjectId("Missiongroup\\BlueButtons\\BlueUp");
   $blueDown = getObjectId("Missiongroup\\BlueButtons\\BlueDown");
   $blueLeft = getObjectId("Missiongroup\\BlueButtons\\BlueLeft");
   $blueRight = getObjectId("Missiongroup\\BlueButtons\\BlueRight");
   $redMatch = getObjectId("Missiongroup\\RedButtons\\redmatch");
   $redMove = getObjectId("Missiongroup\\RedButtons\\redmove");
   $redUp = getObjectId("Missiongroup\\RedButtons\\RedUp");
   $redDown = getObjectId("Missiongroup\\RedButtons\\RedDown");
   $redLeft = getObjectId("Missiongroup\\RedButtons\\RedLeft");
   $redRight = getObjectId("Missiongroup\\RedButtons\\RedRight");
   $cybridRook = getObjectId("Missiongroup\\cybridRook");
   $cybridKnight = getObjectId("Missiongroup\\cybridKnight");
   $cybridBishop = getObjectId("Missiongroup\\cybridBishop");
   $cybridQueen = getObjectId("Missiongroup\\cybridQueen");
   $cybridKing = getObjectId("Missiongroup\\cybridKing");
   $cybridPawn = getObjectId("Missiongroup\\cybridPawn");
   $terranRook = getObjectId("Missiongroup\\terranRook");
   $terranKnight = getObjectId("Missiongroup\\terranKnight");
   $terranBishop = getObjectId("Missiongroup\\terranBishop");
   $terranQueen = getObjectId("Missiongroup\\terranQueen");
   $terranKing = getObjectId("Missiongroup\\terranKing");
   $terranPawn = getObjectId("Missiongroup\\terranPawn");
   $square11 = getObjectId("Missiongroup\\point11");
   $square21 = getObjectId("Missiongroup\\point21");
   $square31 = getObjectId("Missiongroup\\point31");
   $square41 = getObjectId("Missiongroup\\point41");
   $square51 = getObjectId("Missiongroup\\point51");
   $square61 = getObjectId("Missiongroup\\point61");
   $square71 = getObjectId("Missiongroup\\point71");
   $square81 = getObjectId("Missiongroup\\point81");   
   $square12 = getObjectId("Missiongroup\\point12");
   $square22 = getObjectId("Missiongroup\\point22");
   $square32 = getObjectId("Missiongroup\\point32");
   $square42 = getObjectId("Missiongroup\\point42");
   $square52 = getObjectId("Missiongroup\\point52");
   $square62 = getObjectId("Missiongroup\\point62");
   $square72 = getObjectId("Missiongroup\\point72");
   $square82 = getObjectId("Missiongroup\\point82");
   $square13 = getObjectId("Missiongroup\\point13");
   $square23 = getObjectId("Missiongroup\\point23");
   $square33 = getObjectId("Missiongroup\\point33");
   $square43 = getObjectId("Missiongroup\\point43");
   $square53 = getObjectId("Missiongroup\\point53");
   $square63 = getObjectId("Missiongroup\\point63");
   $square73 = getObjectId("Missiongroup\\point73");
   $square83 = getObjectId("Missiongroup\\point83");   
   $square14 = getObjectId("Missiongroup\\point14");
   $square24 = getObjectId("Missiongroup\\point24");
   $square34 = getObjectId("Missiongroup\\point34");
   $square44 = getObjectId("Missiongroup\\point44");
   $square54 = getObjectId("Missiongroup\\point54");
   $square64 = getObjectId("Missiongroup\\point64");
   $square74 = getObjectId("Missiongroup\\point74");
   $square84 = getObjectId("Missiongroup\\point84");   
   $square15 = getObjectId("Missiongroup\\point15");
   $square25 = getObjectId("Missiongroup\\point25");
   $square35 = getObjectId("Missiongroup\\point35");
   $square45 = getObjectId("Missiongroup\\point45");
   $square55 = getObjectId("Missiongroup\\point55");
   $square65 = getObjectId("Missiongroup\\point65");
   $square75 = getObjectId("Missiongroup\\point75");
   $square85 = getObjectId("Missiongroup\\point85");   
   $square16 = getObjectId("Missiongroup\\point16");
   $square26 = getObjectId("Missiongroup\\point26");
   $square36 = getObjectId("Missiongroup\\point36");
   $square46 = getObjectId("Missiongroup\\point46");
   $square56 = getObjectId("Missiongroup\\point56");
   $square66 = getObjectId("Missiongroup\\point66");
   $square76 = getObjectId("Missiongroup\\point76");
   $square86 = getObjectId("Missiongroup\\point86");   
   $square17 = getObjectId("Missiongroup\\point17");
   $square27 = getObjectId("Missiongroup\\point27");
   $square37 = getObjectId("Missiongroup\\point37");
   $square47 = getObjectId("Missiongroup\\point47");
   $square57 = getObjectId("Missiongroup\\point57");
   $square67 = getObjectId("Missiongroup\\point67");
   $square77 = getObjectId("Missiongroup\\point77");
   $square87 = getObjectId("Missiongroup\\point87");   
   $square18 = getObjectId("Missiongroup\\point18");
   $square28 = getObjectId("Missiongroup\\point28");
   $square38 = getObjectId("Missiongroup\\point38");
   $square48 = getObjectId("Missiongroup\\point48");
   $square58 = getObjectId("Missiongroup\\point58");
   $square68 = getObjectId("Missiongroup\\point68");
   $square78 = getObjectId("Missiongroup\\point78");
   $square88 = getObjectId("Missiongroup\\point88");   
}

function onMissionLoad()
{
   cdAudioCycle("Watching", "Purge", "Cyberntx");
   %txt1 = "<F2>GAME TYPE:<F0>  Starsiege Chess\n\n<F2>MISSION:<F0>  SS_Chess\n\n<F2>SCRIPT BY:<F0>  Com. Sentinal [M.I.B.]\n\n<F2>TERRAIN BY:<F0>  Gen. Powio [MIB]\n\n";
   %txt2 = "<F2>BASIC CONTROLS<F0>\n\nTo call a match, scan the \"Call Match\" ball. Another player can join in by scanning the \"Join In\" ball. Once the game begins, select your piece by scanning it. A cursor will appear over the selected piece. Then scan the arrow controls to move the cursor. Once you've selected the destination square, scan the \"Move Selected Piece\" ball to complete the move. At any point in the game, a player may surrender by scanning the \"Surrender\" ball. Possible surrender scenarios: Checkmate or Stalemate\n\n";
   %txt3 = "<F2>HERCBOOST KEYMAP CONTROLS<F0>\n\nCTRL + W = Move Cursor Up\nCTRL + S = Move Cursor Down\nCTRL + A = Move Cursor Left\nCTRL + D = Move Cursor Right\n\n";
   %txt4 = "<F2>UNIVERSAL KEYMAP CONTROLS<F0>\n\nCTRL + Q = Select Targeted Piece\nCTRL + W = Move Cursor Up\nCTRL + S = Move Cursor Down\nCTRL + A = Move Cursor Left\nCTRL + D = Move Cursor Right\nCTRL + E = Move Selected Piece\n\n";
   %msg = %txt1 @ "" @ %txt2 @ "" @ %txt3 @ "" @ %txt4;
   setGameInfo(%msg);
   %count = playerManager::getPlayerCount();
   for(%i = 0; %i < %count; %i++)
   {
      %player = playerManager::getPlayerNum(%i);
      %player.delay = false;
      %player.attackDelay = false;
      %player.promotion = "Queen";
   }
}

function player::onAdd(%player)
{
   say(%player, 0, "Welcome to Starsiege Chess! Check the Game Info tab for information on how to play. You can download this & other missions made by Com. Sentinal [M.I.B.] and Gen. Powio [MIB] in the \"MIB Map Packs\" on the MIB Website at www.starsiegemeninblack.cjb.net.");
   messageBox(%player, "This map requires that you download a new terrain to play. Starsiege Chess is available at www.starsiegemeninblack.cjb.net");
   %player.delay = false;
   %player.attackDelay = false;
   %player.promotion = "Queen";
}

function player::onRemove(%player)
{
   if((%player == $bluePlayer)||(%player == $redPlayer))
   {
      say("Everybody", 13, "<F4>The current chess match has been forfeited because " @ getName(%player) @ " has left the game!");
      playSound(0, "sfx_thunder2.wav", IDPRF_2D);
      $controlLock = true;
      setHudTimer(0, -1, "", 1);
      setHudTimer(0, -1, "", 2);
      setHudTimer(0, -1, "", 3);
      flushConsoleScheduler();      
      deleteObject($pieces);
      $bluePlayer = "";
      $redPlayer = "";
      $gameStarted = false;
      $matchCalled = false;
      $currentTurn = "";
      $selectedPiece = "";
      $cursorOn = false;
      $promotion = false;
      setStaticShapeShortName($blueMatch, "Call Match");
      setStaticShapeShortName($redMatch, "Call Match");
      setPosition($blueCursor, 10000, 10000, -5000);
      setPosition($redCursor, 10000, 10000, -5000);
      setPosition($highlight, 10000, 10000, -5000);
      $controlLock = false;
   }
}

function vehicle::onAdd(%vehicleId)
{
   %player = playerManager::vehicleIdToPlayerNum(%vehicleId);
   setTeam(%vehicleId, "Yellow Team");
   %player.delay = false;
   %player.attackDelay = false;
   %player.promotion = "Queen";
   if(($gameStarted == true)&&((%player == $bluePlayer)||(%player == $redPlayer)))
   {
      setHudTimer(0, -1, "", 1);
   }
}

function vehicle::onAttacked(%attacked, %attacker)
{
   %player = playerManager::vehicleIdToPlayerNum(%attacker);
   healObject(%attacked, 99999);
   healObject(%attacked, 99999);
   healObject(%attacked, 99999);
   healObject(%attacked, 99999);
   healObject(%attacked, 99999);
   if(%player.attackDelay == false)
   {
      %player.attackDelay = true;
      say(%player, %player, "<F5>You cannot damage other players in this map!");
      schedule("resetAttackDelay(" @ %player @ ");", 2);
   }
}

function vehicle::onDestroyed(%destroyed, %destroyer)
{
   %player = playerManager::vehicleIdToPlayerNum(%destroyed);
   if(($gameStarted == true)&&((%player == $bluePlayer)||(%player == $redPlayer)))
   {
      say("Everybody", 17, "<F5>" @ getName(%player) @ " has went to the wait room. The current chess match will auto-forfeit in 60 seconds, unless he respawns in time!", "alarm2.wav");
      messageBox(%player, "If you do not respawn within 60 seconds, you will automatically forfeit the chess match!");
      setHudTimer(60, -1, "Auto-Forfeit In:", 1);
      schedule("inActiveForfeit(" @ %player @ ");", 60);
   }

   // left over from missionStdLib.cs
   vehicle::onDestroyedLog(%destroyed, %destroyer);
   
   // this is weird but %destroyer isn't necessarily a vehicle
   %message = getFancyDeathMessage(getHUDName(%destroyed), getHUDName(%destroyer));
   if(%message != "")
   {
      say( 0, 0, %message);
   }
   
   // enforce the rules
   if($server::TeamPlay == true)
   {
      if((getTeam(%destroyed) == getTeam(%destroyer))&&(%destroyed != %destroyer))
      {
         antiTeamKill(%destroyer);
      }
   }   
}

function inActiveForfeit(%player)
{
   %vehicleId = playerManager::playerNumToVehicleId(%player);
   // Are they still in the wait room?
   if((%vehicleId == 0)||(isGroupDestroyed(%vehicleId) == true))
   {
      setHudTimer(0, -1, "", 1);
      playSound(0, "sfx_thunder2.wav", IDPRF_2D);
      $controlLock = true;
      flushConsoleScheduler();
      say("Everybody", 18, "<F4>" @ getName(%player) @ " has forfeited the chess match!");
      deleteObject($pieces);
      $bluePlayer = "";
      $redPlayer = "";
      $gameStarted = false;
      $matchCalled = false;
      $currentTurn = "";
      $selectedPiece = "";
      $cursorOn = false;
      $promotion = false;
      setStaticShapeShortName($blueMatch, "Call Match");
      setStaticShapeShortName($redMatch, "Call Match");
      setPosition($blueCursor, 10000, 10000, -5000);
      setPosition($redCursor, 10000, 10000, -5000);
      setPosition($highlight, 10000, 10000, -5000);
      $controlLock = false;
   }
}

function resetAttackDelay(%player)
{
   %player.attackDelay = false;
}

function copyObject(%object, %name)
{
   storeObject(%object, "copyObject.tmp");
   if(%name == "")
   {
      %copy = loadObject(getObjectName(%object), "copyObject.tmp");
   }
   else
   {
      %copy = loadObject(%name, "copyObject.tmp");
   }
   return %copy;
}

function getSquare(%piece)
{
   return %piece.square;
}

function getPiece(%square)
{
   return %square.piece;
}

function getType(%piece)
{
   return %piece.type;
}

function getGridPosition(%piece, %coord)
{
   %square = %piece.square;
   if(%coord == x)
   {
      return %square.x;
   }
   else if(%coord == y)
   {
      return %square.y;
   }
}

function getFutureGridPosition(%piece, %coord)
{
   %square = %piece.newSquare;
   if(%coord == x)
   {
      return %square.x;
   }
   else if(%coord == y)
   {
      return %square.y;
   }
}

function setBoard()
{  
   $controlLock = true;
   setPosition($blueCursor, 10000, 10000, -5000);
   setPosition($redCursor, 10000, 10000, -5000);
   setPosition($highlight, 10000, 10000, -5000);
   setStaticShapeShortName($blueMatch, "Surrender");
   setStaticShapeShortName($redMatch, "Surrender");
   $gameStarted = true;
   deleteObject($pieces);
   $pieces = newObject("pieces", simGroup);
   addToSet("Missiongroup", $pieces);
   $blueRook1 = copyObject($terranRook, "piece");
   addToSet($pieces, $blueRook1);
   $blueRook2 = copyObject($terranRook, "piece");
   addToSet($pieces, $blueRook2);
   $blueKnight1 = copyObject($terranKnight, "piece");
   addToSet($pieces, $blueKnight1);
   $blueKnight2 = copyObject($terranKnight, "piece");
   addToSet($pieces, $blueKnight2);
   $blueBishop1 = copyObject($terranBishop, "piece");
   addToSet($pieces, $blueBishop1);
   $blueBishop2 = copyObject($terranBishop, "piece");
   addToSet($pieces, $blueBishop2);
   $blueQueen = copyObject($terranQueen, "piece");
   addToSet($pieces, $blueQueen);
   $blueKing = copyObject($terranKing, "piece");
   addToSet($pieces, $blueKing);
   $bluePawn1 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn1);
   $bluePawn2 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn2);
   $bluePawn3 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn3);
   $bluePawn4 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn4);
   $bluePawn5 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn5);
   $bluePawn6 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn6);
   $bluePawn7 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn7);
   $bluePawn8 = copyObject($terranPawn, "piece");
   addToSet($pieces, $bluePawn8);
   $redRook1 = copyObject($cybridRook, "piece");
   addToSet($pieces, $redRook1);
   $redRook2 = copyObject($cybridRook, "piece");
   addToSet($pieces, $redRook2);
   $redKnight1 = copyObject($cybridKnight, "piece");
   addToSet($pieces, $redKnight1);
   $redKnight2 = copyObject($cybridKnight, "piece");
   addToSet($pieces, $redKnight2);
   $redBishop1 = copyObject($cybridBishop, "piece");
   addToSet($pieces, $redBishop1);
   $redBishop2 = copyObject($cybridBishop, "piece");
   addToSet($pieces, $redBishop2);
   $redQueen = copyObject($cybridQueen, "piece");
   addToSet($pieces, $redQueen);
   $redKing = copyObject($cybridKing, "piece");
   addToSet($pieces, $redKing);
   $redPawn1 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn1);
   $redPawn2 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn2);
   $redPawn3 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn3);
   $redPawn4 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn4);
   $redPawn5 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn5);
   $redPawn6 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn6);
   $redPawn7 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn7);
   $redPawn8 = copyObject($cybridPawn, "piece");
   addToSet($pieces, $redPawn8);
   $square11.piece = $blueRook1;
   $square11.x = 1;
   $square11.y = 1;
   $square21.piece = $blueKnight1;
   $square21.x = 2;
   $square21.y = 1;   
   $square31.piece = $blueBishop1;
   $square31.x = 3;
   $square31.y = 1;
   $square41.piece = $blueKing;
   $square41.x = 4;
   $square41.y = 1;
   $square51.piece = $blueQueen;
   $square51.x = 5;
   $square51.y = 1;   
   $square61.piece = $blueBishop2;
   $square61.x = 6;
   $square61.y = 1;
   $square71.piece = $blueKnight2;
   $square71.x = 7;
   $square71.y = 1;
   $square81.piece = $blueRook2;
   $square81.x = 8;
   $square81.y = 1;
   $square12.piece = $bluePawn1;
   $square12.x = 1;
   $square12.y = 2;
   $square22.piece = $bluePawn2;
   $square22.x = 2;
   $square22.y = 2;
   $square32.piece = $bluePawn3;
   $square32.x = 3;
   $square32.y = 2;
   $square42.piece = $bluePawn4;
   $square42.x = 4;
   $square42.y = 2;
   $square52.piece = $bluePawn5;
   $square52.x = 5;
   $square52.y = 2;
   $square62.piece = $bluePawn6;
   $square62.x = 6;
   $square62.y = 2;
   $square72.piece = $bluePawn7;
   $square72.x = 7;
   $square72.y = 2;
   $square82.piece = $bluePawn8;
   $square82.x = 8;
   $square82.y = 2;
   $square13.piece = "";
   $square13.x = 1;
   $square13.y = 3;
   $square23.piece = "";
   $square23.x = 2;
   $square23.y = 3;
   $square33.piece = "";
   $square33.x = 3;
   $square33.y = 3;
   $square43.piece = "";
   $square43.x = 4;
   $square43.y = 3;
   $square53.piece = "";
   $square53.x = 5;
   $square53.y = 3;
   $square63.piece = "";
   $square63.x = 6;
   $square63.y = 3;
   $square73.piece = "";
   $square73.x = 7;
   $square73.y = 3;
   $square83.piece = "";
   $square83.x = 8;
   $square83.y = 3;
   $square14.piece = "";
   $square14.x = 1;
   $square14.y = 4;
   $square24.piece = "";
   $square24.x = 2;
   $square24.y = 4;
   $square34.piece = "";
   $square34.x = 3;
   $square34.y = 4;
   $square44.piece = "";
   $square44.x = 4;
   $square44.y = 4;
   $square54.piece = "";
   $square54.x = 5;
   $square54.y = 4;
   $square64.piece = "";
   $square64.x = 6;
   $square64.y = 4;
   $square74.piece = "";
   $square74.x = 7;
   $square74.y = 4;
   $square84.piece = "";
   $square84.x = 8;
   $square84.y = 4;
   $square15.piece = "";
   $square15.x = 1;
   $square15.y = 5;
   $square25.piece = "";
   $square25.x = 2;
   $square25.y = 5;
   $square35.piece = "";
   $square35.x = 3;
   $square35.y = 5;
   $square45.piece = "";
   $square45.x = 4;
   $square45.y = 5;
   $square55.piece = "";
   $square55.x = 5;
   $square55.y = 5;
   $square65.piece = "";
   $square65.x = 6;
   $square65.y = 5;
   $square75.piece = "";
   $square75.x = 7;
   $square75.y = 5;
   $square85.piece = "";
   $square85.x = 8;
   $square85.y = 5;
   $square16.piece = "";
   $square16.x = 1;
   $square16.y = 6;
   $square26.piece = "";
   $square26.x = 2;
   $square26.y = 6;
   $square36.piece = "";
   $square36.x = 3;
   $square36.y = 6;
   $square46.piece = "";
   $square46.x = 4;
   $square46.y = 6;
   $square56.piece = "";
   $square56.x = 5;
   $square56.y = 6;
   $square66.piece = "";
   $square66.x = 6;
   $square66.y = 6;
   $square76.piece = "";
   $square76.x = 7;
   $square76.y = 6;
   $square86.piece = "";
   $square86.x = 8;
   $square86.y = 6;
   $square17.piece = $redPawn1;
   $square17.x = 1;
   $square17.y = 7;
   $square27.piece = $redPawn2;
   $square27.x = 2;
   $square27.y = 7;
   $square37.piece = $redPawn3;
   $square37.x = 3;
   $square37.y = 7;
   $square47.piece = $redPawn4;
   $square47.x = 4;
   $square47.y = 7;
   $square57.piece = $redPawn5;
   $square57.x = 5;
   $square57.y = 7;
   $square67.piece = $redPawn6;
   $square67.x = 6;
   $square67.y = 7;
   $square77.piece = $redPawn7;
   $square77.x = 7;
   $square77.y = 7;
   $square87.piece = $redPawn8;
   $square87.x = 8;
   $square87.y = 7;
   $square18.piece = $redRook1;
   $square18.x = 1;
   $square18.y = 8;
   $square28.piece = $redKnight1;
   $square28.x = 2;
   $square28.y = 8;
   $square38.piece = $redBishop1;
   $square38.x = 3;
   $square38.y = 8;
   $square48.piece = $redKing;
   $square48.x = 4;
   $square48.y = 8;
   $square58.piece = $redQueen;
   $square58.x = 5;
   $square58.y = 8;
   $square68.piece = $redBishop2;
   $square68.x = 6;
   $square68.y = 8;
   $square78.piece = $redKnight2;
   $square78.x = 7;
   $square78.y = 8;
   $square88.piece = $redRook2;
   $square88.x = 8;
   $square88.y = 8;
   $blueRook1.square = $square11;
   $blueRook2.square = $square81;
   $blueKnight1.square = $square21;
   $blueKnight2.square = $square71;
   $blueBishop1.square = $square31;
   $blueBishop2.square = $square61;
   $blueQueen.square = $square51;
   $blueKing.square = $square41;
   $bluePawn1.square = $square12;
   $bluePawn2.square = $square22;
   $bluePawn3.square = $square32;
   $bluePawn4.square = $square42;
   $bluePawn5.square = $square52;
   $bluePawn6.square = $square62;
   $bluePawn7.square = $square72;
   $bluePawn8.square = $square82;
   $redRook1.square = $square18;
   $redRook2.square = $square88;
   $redKnight1.square = $square28;
   $redKnight2.square = $square78;
   $redBishop1.square = $square38;
   $redBishop2.square = $square68;
   $redQueen.square = $square58;
   $redKing.square = $square48;
   $redPawn1.square = $square17;
   $redPawn2.square = $square27;
   $redPawn3.square = $square37;
   $redPawn4.square = $square47;
   $redPawn5.square = $square57;
   $redPawn6.square = $square67;
   $redPawn7.square = $square77;
   $redPawn8.square = $square87;
   $blueRook1.type = "Rook";
   $blueRook2.type = "Rook";
   $blueKnight1.type = "Knight";
   $blueKnight2.type = "Knight";
   $blueBishop1.type = "Bishop";
   $blueBishop2.type = "Bishop";
   $blueQueen.type = "Queen";
   $blueKing.type = "King";
   $bluePawn1.type = "Pawn";
   $bluePawn2.type = "Pawn";
   $bluePawn3.type = "Pawn";
   $bluePawn4.type = "Pawn";
   $bluePawn5.type = "Pawn";
   $bluePawn6.type = "Pawn";
   $bluePawn7.type = "Pawn";
   $bluePawn8.type = "Pawn";
   $redRook1.type = "Rook";
   $redRook2.type = "Rook";
   $redKnight1.type = "Knight";
   $redKnight2.type = "Knight";
   $redBishop1.type = "Bishop";
   $redBishop2.type = "Bishop";
   $redQueen.type = "Queen";
   $redKing.type = "King";
   $redPawn1.type = "Pawn";
   $redPawn2.type = "Pawn";
   $redPawn3.type = "Pawn";
   $redPawn4.type = "Pawn";
   $redPawn5.type = "Pawn";
   $redPawn6.type = "Pawn";
   $redPawn7.type = "Pawn";
   $redPawn8.type = "Pawn";
   $blueKing.firstmove = true;
   $blueRook1.firstmove = true;
   $blueRook2.firstmove = true;
   $bluePawn1.firstmove = true;
   $bluePawn2.firstmove = true;
   $bluePawn3.firstmove = true;
   $bluePawn4.firstmove = true;
   $bluePawn5.firstmove = true;
   $bluePawn6.firstmove = true;
   $bluePawn7.firstmove = true;
   $bluePawn8.firstmove = true;
   $redKing.firstmove = true;
   $redRook1.firstmove = true;
   $redRook2.firstmove = true;
   $redPawn1.firstmove = true;
   $redPawn2.firstmove = true;
   $redPawn3.firstmove = true;
   $redPawn4.firstmove = true;
   $redPawn5.firstmove = true;
   $redPawn6.firstmove = true;
   $redPawn7.firstmove = true;
   $redPawn8.firstmove = true;
   $square13.enpassant = "";
   $square23.enpassant = "";
   $square33.enpassant = "";
   $square43.enpassant = "";
   $square53.enpassant = "";
   $square63.enpassant = "";
   $square73.enpassant = "";
   $square83.enpassant = "";
   $square16.enpassant = "";
   $square26.enpassant = "";
   $square36.enpassant = "";
   $square46.enpassant = "";
   $square56.enpassant = "";
   $square66.enpassant = "";
   $square76.enpassant = "";
   $square86.enpassant = "";
   $blueRook1.newSquare = $square11;
   $blueRook2.newSquare = $square81;
   $blueKnight1.newSquare = $square21;
   $blueKnight2.newSquare = $square71;
   $blueBishop1.newSquare = $square31;
   $blueBishop2.newSquare = $square61;
   $blueQueen.newSquare = $square51;
   $blueKing.newSquare = $square41;
   $bluePawn1.newSquare = $square12;
   $bluePawn2.newSquare = $square22;
   $bluePawn3.newSquare = $square32;
   $bluePawn4.newSquare = $square42;
   $bluePawn5.newSquare = $square52;
   $bluePawn6.newSquare = $square62;
   $bluePawn7.newSquare = $square72;
   $bluePawn8.newSquare = $square82;
   $redRook1.newSquare = $square18;
   $redRook2.newSquare = $square88;
   $redKnight1.newSquare = $square28;
   $redKnight2.newSquare = $square78;
   $redBishop1.newSquare = $square38;
   $redBishop2.newSquare = $square68;
   $redQueen.newSquare = $square58;
   $redKing.newSquare = $square48;
   $redPawn1.newSquare = $square17;
   $redPawn2.newSquare = $square27;
   $redPawn3.newSquare = $square37;
   $redPawn4.newSquare = $square47;
   $redPawn5.newSquare = $square57;
   $redPawn6.newSquare = $square67;
   $redPawn7.newSquare = $square77;
   $redPawn8.newSquare = $square87;
   $square11.newPiece = $blueRook1;
   $square21.newPiece = $blueKnight1;
   $square31.newPiece = $blueBishop1;
   $square41.newPiece = $blueKing;
   $square51.newPiece = $blueQueen;
   $square61.newPiece = $blueBishop2;
   $square71.newPiece = $blueKnight2;
   $square81.newPiece = $blueRook2;
   $square12.newPiece = $bluePawn1;
   $square22.newPiece = $bluePawn2;
   $square32.newPiece = $bluePawn3;
   $square42.newPiece = $bluePawn4;
   $square52.newPiece = $bluePawn5;
   $square62.newPiece = $bluePawn6;
   $square72.newPiece = $bluePawn7;
   $square82.newPiece = $bluePawn8;
   $square13.newPiece = "";
   $square23.newPiece = "";
   $square33.newPiece = "";
   $square43.newPiece = "";
   $square53.newPiece = "";
   $square63.newPiece = "";
   $square73.newPiece = "";
   $square83.newPiece = "";
   $square14.newPiece = "";
   $square24.newPiece = "";
   $square34.newPiece = "";
   $square44.newPiece = "";
   $square54.newPiece = "";
   $square64.newPiece = "";
   $square74.newPiece = "";
   $square84.newPiece = "";
   $square15.newPiece = "";
   $square25.newPiece = "";
   $square35.newPiece = "";
   $square45.newPiece = "";
   $square55.newPiece = "";
   $square65.newPiece = "";
   $square75.newPiece = "";
   $square85.newPiece = "";
   $square16.newPiece = "";
   $square26.newPiece = "";
   $square36.newPiece = "";
   $square46.newPiece = "";
   $square56.newPiece = "";
   $square66.newPiece = "";
   $square76.newPiece = "";
   $square86.newPiece = "";
   $square17.newPiece = $redPawn1;
   $square27.newPiece = $redPawn2;
   $square37.newPiece = $redPawn3;
   $square47.newPiece = $redPawn4;
   $square57.newPiece = $redPawn5;
   $square67.newPiece = $redPawn6;
   $square77.newPiece = $redPawn7;
   $square87.newPiece = $redPawn8;
   $square18.newPiece = $redRook1;
   $square28.newPiece = $redKnight1;
   $square38.newPiece = $redBishop1;
   $square48.newPiece = $redKing;
   $square58.newPiece = $redQueen;
   $square68.newPiece = $redBishop2;
   $square78.newPiece = $redKnight2;
   $square88.newPiece = $redRook2;
   for(%numY = 1; %numY <= 8; %numY++)
   {
      for(%numX = 1; %numX <= 8; %numX++)
      {
         %x = getPosition($square[%numX @ "" @ %numY], x);
         %y = getPosition($square[%numX @ "" @ %numY], y);
         %z = getTerrainHeight(%x, %y);
         if(getTeam($square[%numX @ "" @ %numY].piece) == "Blue Team")
         {
            %rot = 180;
         }
         else
         {
            %rot = 0;
         }
         setPosition($square[%numX @ "" @ %numY].piece, %x, %y, %z, %rot);
      }
   }
   $blueCursor.x = 1;
   $blueCursor.y = 1;
   $redCursor.x = 8;
   $redCursor.y = 8;
   changeTurn();
}

function changeTurn()
{
   if($currentTurn == $bluePlayer)
   {
      $currentTurn = $redPlayer;
      $square16.enpassant = "";
      $square26.enpassant = "";
      $square36.enpassant = "";
      $square46.enpassant = "";
      $square56.enpassant = "";
      $square66.enpassant = "";
      $square76.enpassant = "";
      $square86.enpassant = "";
      %check = isKingInCheck($currentTurn);
      if(%check == true)
      {
         say("Everybody", 83, "<F4>" @ getName($currentTurn) @ "'s King is now in check!", "warn_missl_lock.wav");
         schedule("flushChannel(83);", 0.65);
      }
      schedule("say(\"Everybody\", 24, \"<F4>It is now " @ getName($currentTurn) @ "'s turn.\");", 1);
      schedule("playSound(0, $yourTurn, IDPRF_2D);", 1);
      schedule("$controlLock = false;", 1);
   }
   else if($currentTurn == $redPlayer)
   {
      $currentTurn = $bluePlayer;
      $square13.enpassant = "";
      $square23.enpassant = "";
      $square33.enpassant = "";
      $square43.enpassant = "";
      $square53.enpassant = "";
      $square63.enpassant = "";
      $square73.enpassant = "";
      $square83.enpassant = "";
      %check = isKingInCheck($currentTurn);
      if(%check == true)
      {
         say("Everybody", 83, "<F4>" @ getName($currentTurn) @ "'s King is now in check!", "warn_missl_lock.wav");
         schedule("flushChannel(83);", 0.65);
      }
      schedule("say(\"Everybody\", 24, \"<F4>It is now " @ getName($currentTurn) @ "'s turn.\");", 1);
      schedule("playSound(0, $yourTurn, IDPRF_2D);", 1);
      schedule("$controlLock = false;", 1);
   }
   else if($currentTurn == "")
   {
      %random = randomInt(0, 1);
      if(%random == 0)
      {         
         $currentTurn = $bluePlayer;
         $square13.enpassant = "";
         $square23.enpassant = "";
         $square33.enpassant = "";
         $square43.enpassant = "";
         $square53.enpassant = "";
         $square63.enpassant = "";
         $square73.enpassant = "";
         $square83.enpassant = "";
      }
      else
      {         
         $currentTurn = $redPlayer;
         $square16.enpassant = "";
         $square26.enpassant = "";
         $square36.enpassant = "";
         $square46.enpassant = "";
         $square56.enpassant = "";
         $square66.enpassant = "";
         $square76.enpassant = "";
         $square86.enpassant = "";
      }
      schedule("say(\"Everybody\", 23, \"<F4>" @ getName($currentTurn) @ " has been chosen to go first.\", \"sfx_thunder2.wav\");", 4);
      schedule("$controlLock = false;", 4);
   }
   updateFutureVariables();
   $promotion = false;
}

function updateFutureVariables()
{
   $blueRook1.newSquare = $blueRook1.square;
   $blueRook2.newSquare = $blueRook2.square;
   $blueKnight1.newSquare = $blueKnight1.square;
   $blueKnight2.newSquare = $blueKnight2.square;
   $blueBishop1.newSquare = $blueBishop1.square;
   $blueBishop2.newSquare = $blueBishop2.square;
   $blueQueen.newSquare = $blueQueen.square;
   $blueKing.newSquare = $blueKing.square;
   $bluePawn1.newSquare = $bluePawn1.square;
   $bluePawn2.newSquare = $bluePawn2.square;
   $bluePawn3.newSquare = $bluePawn3.square;
   $bluePawn4.newSquare = $bluePawn4.square;
   $bluePawn5.newSquare = $bluePawn5.square;
   $bluePawn6.newSquare = $bluePawn6.square;
   $bluePawn7.newSquare = $bluePawn7.square;
   $bluePawn8.newSquare = $bluePawn8.square;
   $redRook1.newSquare = $redRook1.square;
   $redRook2.newSquare = $redRook2.square;
   $redKnight1.newSquare = $redKnight1.square;
   $redKnight2.newSquare = $redKnight2.square;
   $redBishop1.newSquare = $redBishop1.square;
   $redBishop2.newSquare = $redBishop2.square;
   $redQueen.newSquare = $redQueen.square;
   $redKing.newSquare = $redKing.square;
   $redPawn1.newSquare = $redPawn1.square;
   $redPawn2.newSquare = $redPawn2.square;
   $redPawn3.newSquare = $redPawn3.square;
   $redPawn4.newSquare = $redPawn4.square;
   $redPawn5.newSquare = $redPawn5.square;
   $redPawn6.newSquare = $redPawn6.square;
   $redPawn7.newSquare = $redPawn7.square;
   $redPawn8.newSquare = $redPawn8.square;
   $square11.newPiece = $square11.piece;
   $square21.newPiece = $square21.piece;
   $square31.newPiece = $square31.piece;
   $square41.newPiece = $square41.piece;
   $square51.newPiece = $square51.piece;
   $square61.newPiece = $square61.piece;
   $square71.newPiece = $square71.piece;
   $square81.newPiece = $square81.piece;
   $square12.newPiece = $square12.piece;
   $square22.newPiece = $square22.piece;
   $square32.newPiece = $square32.piece;
   $square42.newPiece = $square42.piece;
   $square52.newPiece = $square52.piece;
   $square62.newPiece = $square62.piece;
   $square72.newPiece = $square72.piece;
   $square82.newPiece = $square82.piece;
   $square13.newPiece = $square13.piece;
   $square23.newPiece = $square23.piece;
   $square33.newPiece = $square33.piece;
   $square43.newPiece = $square43.piece;
   $square53.newPiece = $square53.piece;
   $square63.newPiece = $square63.piece;
   $square73.newPiece = $square73.piece;
   $square83.newPiece = $square83.piece;
   $square14.newPiece = $square14.piece;
   $square24.newPiece = $square24.piece;
   $square34.newPiece = $square34.piece;
   $square44.newPiece = $square44.piece;
   $square54.newPiece = $square54.piece;
   $square64.newPiece = $square64.piece;
   $square74.newPiece = $square74.piece;
   $square84.newPiece = $square84.piece;
   $square15.newPiece = $square15.piece;
   $square25.newPiece = $square25.piece;
   $square35.newPiece = $square35.piece;
   $square45.newPiece = $square45.piece;
   $square55.newPiece = $square55.piece;
   $square65.newPiece = $square65.piece;
   $square75.newPiece = $square75.piece;
   $square85.newPiece = $square85.piece;
   $square16.newPiece = $square16.piece;
   $square26.newPiece = $square26.piece;
   $square36.newPiece = $square36.piece;
   $square46.newPiece = $square46.piece;
   $square56.newPiece = $square56.piece;
   $square66.newPiece = $square66.piece;
   $square76.newPiece = $square76.piece;
   $square86.newPiece = $square86.piece;
   $square17.newPiece = $square17.piece;
   $square27.newPiece = $square27.piece;
   $square37.newPiece = $square37.piece;
   $square47.newPiece = $square47.piece;
   $square57.newPiece = $square57.piece;
   $square67.newPiece = $square67.piece;
   $square77.newPiece = $square77.piece;
   $square87.newPiece = $square78.piece;
   $square18.newPiece = $square18.piece;
   $square28.newPiece = $square28.piece;
   $square38.newPiece = $square38.piece;
   $square48.newPiece = $square48.piece;
   $square58.newPiece = $square58.piece;
   $square68.newPiece = $square68.piece;
   $square78.newPiece = $square78.piece;
   $square88.newPiece = $square88.piece;
}

function willKingBeInCheck(%player, %piece, %destination)
{
   %oldSquare = %piece.square;
   %oldSquare.newPiece = "";
   %destination.newPiece = %piece;
   %piece.newSquare = %destination; 
   %check = isKingInCheckFuture(%player);
   return %check;
}

function willCastledKingBeInCheck(%player, %king, %rook, %kingDest, %rookDest)
{
   %oldSquare1 = %king.square;
   %oldSquare1.newPiece = "";
   %kingDest.newPiece = %king;
   %king.newSquare = %kingDest; 
   %oldSquare2 = %rook.square;
   %oldSquare2.newPiece = "";
   %rookDest.newPiece = %rook;
   %rook.newSquare = %rookDest;
   %check = isKingInCheckFuture(%player);
   return %check;
}

function moveRook(%player, %piece, %destination)
{
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = %destination.x;            
   %y2 = %destination.y;         
   %castle = false;       
   if(%destination == %piece.square)
   {
      say(%player, %player, "<F5>That is not a valid move! The selected piece is already occupying that square.", "alarm2.wav");
      return;
   }
   else if(%destination.piece != "")
   {
      %type = getType(%destination.piece);
      if(getTeam(%piece) == getTeam(%destination.piece))
      {
         %destPiece = %destination.piece;
         if(%type == "King")
         {
            if((%piece.firstmove == true)&&(%destPiece.firstmove == true))
            {
               %castle = true;
            }
            else
            {
               say(%player, %player, "<F5>That is not a valid move! You cannot castle because you have already moved one or more of the pieces involved.", "alarm2.wav");
               return;
            }
         }
         else
         {
            say(%player, %player, "<F5>That is not a valid move! The destination square is already occupied by one of your pieces.", "alarm2.wav");
            return;
         }
      }
      else if(%type == "King")
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot take the enemy King, you must put him in checkmate to win.", "alarm2.wav");
         return;
      }
   }
   if((%x1 == %x2)||(%y1 == %y2))
   {
      if(%x1 == %x2)
      {
         %max = abs(%y2 - %y1);
         %access = true;
         if(%y2 < %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 - %i;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%y2 > %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 + %i;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      else if(%y1 == %y2)
      {
         %max = abs(%x2 - %x1);
         %access = true;
         if(%x2 < %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 - %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%x2 > %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 + %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      if(%access == true)
      {
         if(%castle == true)
         {
            if(%player == $bluePlayer)
            {
               if(%piece == $blueRook1)
               {
                  %king = $blueKing;
                  %kingDest = $square21;
                  %rook = %piece;
                  %rookDest = $square31;
               }
               else if(%piece == $blueRook2)
               {                  
                  %king = $blueKing;
                  %kingDest = $square61;
                  %rook = %piece;
                  %rookDest = $square51;
               }
            }
            else if(%player == $redPlayer)
            {
               if(%piece == $redRook1)
               {
                  %king = $redKing;
                  %kingDest = $square28;
                  %rook = %piece;
                  %rookDest = $square38;
               }
               else if(%piece == $redRook2)
               {
                  %king = $redKing;
                  %kingDest = $square68;
                  %rook = %piece;
                  %rookDest = $square58;                  
               }
            }
            if(isKingInCheck(%player) == true)
            {               
               say(%player, %player, "<F5>That is not a valid move! You cannot castle out of check.", "alarm2.wav");
               return;
            }
            else if(willCastledKingBeInCheck(%player, %king, %rook, %kingDest, %rookDest) == true)
            {
               say(%player, %player, "<F5>That is not a valid move! You cannot castle because it would put your King in check.", "alarm2.wav");
               updateFutureVariables();
               return;
            }
            else if(willKingBeInCheck(%player, %king, %rookDest) == true)
            {
               say(%player, %player, "<F5>That is not a valid move! You cannot castle because it would mean moving your King through check.", "alarm2.wav");
               updateFutureVariables();
               return;
            }
            else
            {         
               %king.firstmove = false;
               %rook.firstmove = false;
               $controlLock = true;
               castle(%player, %king, %rook, %kingDest, %rookDest);
            }
         }
         else
         {
            if(willKingBeInCheck(%player, %piece, %destination) == true)
            {
               say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
               updateFutureVariables();
               return;
            }
            $controlLock = true;
            %piece.firstmove = false;
            move(%player, %piece, %destination);
         }
      }
      else
      {
         if(%castle == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot castle because the path is obstructed.", "alarm2.wav");
         }
         else
         {
            say(%player, %player, "<F5>That is not a valid move! The path is obstructed by another piece.", "alarm2.wav");
         }
      }
   }
   else
   {
      say(%player, %player, "<F5>That is not a valid move! Rooks can only move crossways.", "alarm2.wav");
   }
}

function moveBishop(%player, %piece, %destination)
{
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = %destination.x;            
   %y2 = %destination.y;                   
   if(%destination == %piece.square)
   {
      say(%player, %player, "<F5>That is not a valid move! The selected piece is already occupying that square.", "alarm2.wav");
      return;
   }
   else if(%destination.piece != "")
   {
      %type = getType(%destination.piece);
      if(getTeam(%piece) == getTeam(%destination.piece))
      {
         say(%player, %player, "<F5>That is not a valid move! The destination square is already occupied by one of your pieces.", "alarm2.wav");
         return;
      }
      else if(%type == "King")
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot take the enemy King, you must put him in checkmate to win.", "alarm2.wav");
         return;
      }
   }
   %deltaX = %x2 - %x1;
   %deltaY = %y2 - %y1;
   if(abs(%deltaX) == abs(%deltaY))
   {
      %access = true;
      %max = abs(%deltaX);
      if((%deltaX > 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }      
      }
      else if((%deltaX < 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX > 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX < 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      if(%access == true)
      {
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         move(%player, %piece, %destination);
      }
      else
      {
         say(%player, %player, "<F5>That is not a valid move! The path is obstructed by another piece.", "alarm2.wav");
      }
   }
   else
   {
      say(%player, %player, "<F5>That is not a valid move! Bishops can only move diagonally.", "alarm2.wav");
   }
}

function moveKnight(%player, %piece, %destination)
{
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = %destination.x;            
   %y2 = %destination.y;                   
   if(%destination == %piece.square)
   {
      say(%player, %player, "<F5>That is not a valid move! The selected piece is already occupying that square.", "alarm2.wav");
      return;
   }
   else if(%destination.piece != "")
   {
      %type = getType(%destination.piece);
      if(getTeam(%piece) == getTeam(%destination.piece))
      {
         say(%player, %player, "<F5>That is not a valid move! The destination square is already occupied by one of your pieces.", "alarm2.wav");
         return;
      }
      else if(%type == "King")
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot take the enemy King, you must put him in checkmate to win.", "alarm2.wav");
         return;
      }
   }
   %deltaX = %x2 - %x1;
   %deltaY = %y2 - %y1;
   if(((abs(%deltaX) == 2)&&(abs(%deltaY) == 1))||((abs(%deltaX) == 1)&&(abs(%deltaY) == 2)))
   {
      if(willKingBeInCheck(%player, %piece, %destination) == true)
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
         updateFutureVariables();
         return;
      }
      $controlLock = true;
      move(%player, %piece, %destination);
   }
   else
   {
      say(%player, %player, "<F5>That is not a valid move! Knights can only move in a 1x2 L-shaped pattern.", "alarm2.wav");
   }
}

function moveQueen(%player, %piece, %destination)
{
   %team = getTeam(%piece);
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = %destination.x;            
   %y2 = %destination.y;
   %deltaX = %x2 - %x1;
   %deltaY = %y2 - %y1;         
   if(%destination == %piece.square)
   {
      say(%player, %player, "<F5>That is not a valid move! The selected piece is already occupying that square.", "alarm2.wav");
      return;
   }
   else if(%destination.piece != "")
   {
      %type = getType(%destination.piece);
      if(getTeam(%piece) == getTeam(%destination.piece))
      {
         say(%player, %player, "<F5>That is not a valid move! The destination square is already occupied by one of your pieces.", "alarm2.wav");
         return;
      }
      else if(%type == "King")
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot take the enemy King, you must put him in checkmate to win.", "alarm2.wav");
         return;
      }
   }
   if((%x1 == %x2)||(%y1 == %y2))
   {
      if(%x1 == %x2)
      {
         %max = abs(%y2 - %y1);
         %access = true;
         if(%y2 < %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 - %i;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%y2 > %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 + %i;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      else if(%y1 == %y2)
      {
         %max = abs(%x2 - %x1);
         %access = true;
         if(%x2 < %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 - %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%x2 > %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 + %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      if(%access == true)
      {   
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         move(%player, %piece, %destination);         
      }
      else
      {
         say(%player, %player, "<F5>That is not a valid move! The path is obstructed by another piece.", "alarm2.wav");
      }
   }
   else if(abs(%deltaX) == abs(%deltaY))
   {
      %access = true;
      %max = abs(%deltaX);
      if((%deltaX > 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }      
      }
      else if((%deltaX < 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX > 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX < 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      if(%access == true)
      {
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         move(%player, %piece, %destination);
      }
      else
      {
         say(%player, %player, "<F5>That is not a valid move! The path is obstructed by another piece.", "alarm2.wav");
      }
   }
   else
   {
      say(%player, %player, "<F5>That is not a valid move! Queens can only move crossways and diagonal.", "alarm2.wav");
   }
}

function moveKing(%player, %piece, %destination)
{
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = %destination.x;            
   %y2 = %destination.y; 
   %deltaX = %x2 - %x1; 
   %deltaY = %y2 - %y1;     
   %castle = false;             
   if(%destination == %piece.square)
   {
      say(%player, %player, "<F5>That is not a valid move! The selected piece is already occupying that square.", "alarm2.wav");
      return;
   }
   else if(%destination.piece != "")
   {
      %type = getType(%destination.piece);
      if(getTeam(%piece) == getTeam(%destination.piece))
      {
         %destPiece = %destination.piece;
         if(%type == "Rook")
         {            
            if((%piece.firstmove == true)&&(%destPiece.firstmove == true))
            {
               if(%y1 == %y2)
               {
                  %max = abs(%x2 - %x1);
                  %access = true;
                  if(%x2 < %x1)
                  {
                     for(%i = 1; %i < %max; %i++)
                     {
                        %curX = %x1 - %i;
                        %curY = %y1;
                        %curpiece = $square[%curX @ "" @ %curY].piece;
                        if(%curpiece != "")
                        {
                           %access = false;
                        }
                     }
                  }
                  else if(%x2 > %x1)
                  {
                     for(%i = 1; %i < %max; %i++)
                     {
                        %curX = %x1 + %i;
                        %curY = %y1;
                        %curpiece = $square[%curX @ "" @ %curY].piece;
                        if(%curpiece != "")
                        {
                           %access = false;
                        }
                     }
                  }
               }
               else
               {
                  say(%player, %player, "<F5>That is not a valid move! The destination square is already occupied by one of your pieces.", "alarm2.wav");
                  return;
               }
               if(%access == true)
               {
                  if(%player == $bluePlayer)
                  {
                     if(%destination.piece == $blueRook1)
                     {
                        %king = %piece;
                        %kingDest = $square21;
                        %rook = $blueRook1;
                        %rookDest = $square31;
                     }
                     else if(%destination.piece == $blueRook2)
                     {                  
                        %king = %piece;
                        %kingDest = $square61;
                        %rook = $blueRook2;
                        %rookDest = $square51;
                     }
                  }
                  else if(%player == $redPlayer)
                  {
                     if(%destination.piece == $redRook1)
                     {
                        %king = %piece;
                        %kingDest = $square28;
                        %rook = $redRook1;
                        %rookDest = $square38;
                     }
                     else if(%destination.piece == $redRook2)
                     {
                        %king = %piece;
                        %kingDest = $square68;
                        %rook = $redRook2;
                        %rookDest = $square58;                  
                     }
                  }
                  if(isKingInCheck(%player) == true)
                  {               
                     say(%player, %player, "<F5>That is not a valid move! You cannot castle out of check.", "alarm2.wav");
                     return;
                  }
                  else if(willCastledKingBeInCheck(%player, %king, %rook, %kingDest, %rookDest) == true)
                  {
                     say(%player, %player, "<F5>That is not a valid move! You cannot castle because it would put your King in check.", "alarm2.wav");
                     updateFutureVariables();
                     return;
                  }
                  else if(willKingBeInCheck(%player, %king, %rookDest) == true)
                  {
                     say(%player, %player, "<F5>That is not a valid move! You cannot castle because it would mean moving your King through check.", "alarm2.wav");
                     updateFutureVariables();
                     return;
                  }
                  else
                  {         
                     %king.firstmove = false;
                     %rook.firstmove = false;
                     $controlLock = true;
                     castle(%player, %king, %rook, %kingDest, %rookDest);
                     return;
                  }
               }
               else
               {                  
                  say(%player, %player, "<F5>That is not a valid move! You cannot castle because the path is obstructed.", "alarm2.wav");
                  return;
               }                  
            }
            else
            {
               say(%player, %player, "<F5>That is not a valid move! You cannot castle because you have already moved one or more of the pieces involved.", "alarm2.wav");
               return;
            }
         }
         else
         {
            say(%player, %player, "<F5>That is not a valid move! The destination square is already occupied by one of your pieces.", "alarm2.wav");
            return;
         }
      }
      else if(%type == "King")
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot take the enemy King, you must put him in checkmate to win.", "alarm2.wav");
         return;
      }
   }
   if((abs(%deltaX) == 1)&&(abs(%deltaY) == 1))
   {
      if(willKingBeInCheck(%player, %piece, %destination) == true)
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
         updateFutureVariables();
         return;
      }
      $controlLock = true;
      %piece.firstmove = false;
      move(%player, %piece, %destination);
   }
   else if((abs(%deltaX) == 1)&&(abs(%deltaY) == 0))
   {
      if(willKingBeInCheck(%player, %piece, %destination) == true)
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
         updateFutureVariables();
         return;
      }
      $controlLock = true;
      %piece.firstmove = false;
      move(%player, %piece, %destination);
   }
   else if((abs(%deltaX) == 0)&&(abs(%deltaY) == 1))
   {
      if(willKingBeInCheck(%player, %piece, %destination) == true)
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
         updateFutureVariables();
         return;
      }
      $controlLock = true;
      %piece.firstmove = false;
      move(%player, %piece, %destination);
   }
   else
   {
      say(%player, %player, "<F5>That is not a valid move! Kings can only move 1 space in any direction.", "alarm2.wav");
   }
}

function movePawn(%player, %piece, %destination)
{
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = %destination.x;            
   %y2 = %destination.y; 
   %deltaX = %x2 - %x1; 
   %deltaY = %y2 - %y1;               
   if(%destination == %piece.square)
   {
      say(%player, %player, "<F5>That is not a valid move! The selected piece is already occupying that square.", "alarm2.wav");
      return;
   }
   else if(%destination.piece != "")
   {
      %type = getType(%destination.piece);
      if(getTeam(%piece) == getTeam(%destination.piece))
      {
         say(%player, %player, "<F5>That is not a valid move! The destination square is already occupied by one of your pieces.", "alarm2.wav");
         return;
      }
      else if(%type == "King")
      {
         say(%player, %player, "<F5>That is not a valid move! You cannot take the enemy King, you must put him in checkmate to win.", "alarm2.wav");
         return;
      }
      else
      {
         if((getTeam(%piece) == "Blue Team")&&(%deltaY == 1)&&(abs(%deltaX) == 1))
         {   
            if(willKingBeInCheck(%player, %piece, %destination) == true)
            {
               say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
               updateFutureVariables();
               return;
            }   
            $controlLock = true;
            %piece.firstmove = false;
            move(%player, %piece, %destination);         
         }
         else if((getTeam(%piece) == "Red Team")&&(%deltaY == -1)&&(abs(%deltaX) == 1))
         {   
            if(willKingBeInCheck(%player, %piece, %destination) == true)
            {
               say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
               updateFutureVariables();
               return;
            }
            $controlLock = true;
            %piece.firstmove = false;
            move(%player, %piece, %destination);                 
         }
         else
         {
            say(%player, %player, "<F5>That is not a valid move! Pawns can only attack diagonally.", "alarm2.wav");
         }
      }
   }
   else if((%piece.firstmove == true)&&(%deltaX == 0)&&(abs(%deltaY) == 2))
   {
      if((getTeam(%piece) == "Blue Team")&&(%deltaY == 2))
      {      
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         %piece.firstmove = false;
         %square = $square[%x1 @ "" @ %y1 + 1];
         %square.enpassant = %piece;
         move(%player, %piece, %destination);         
      }
      else if((getTeam(%piece) == "Red Team")&&(%deltaY == -2))
      {       
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         %piece.firstmove = false;
         %square = $square[%x1 @ "" @ %y1 - 1];
         %square.enpassant = %piece;
         move(%player, %piece, %destination);         
      }
      else
      {
         say(%player, %player, "<F5>That is not a valid move! Pawns can only move 1 space forward or attack 1 space diagonal.", "alarm2.wav");
      }
   }
   else if(%destination.enpassant != "")
   {
      if((getTeam(%piece) == "Blue Team")&&(abs(%deltaX) == 1)&&(%deltaY == 1))
      {       
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         %piece.firstmove = false;
         move(%player, %piece, %destination);
      }
      else if((getTeam(%piece) == "Red Team")&&(abs(%deltaX) == 1)&&(%deltaY == -1))
      {       
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }     
         $controlLock = true;
         %piece.firstmove = false;
         move(%player, %piece, %destination);
      }       
      else
      {
         say(%player, %player, "<F5>That is not a valid move! Pawns can only move 1 space forward or attack 1 space diagonal.", "alarm2.wav");
      }
   } 
   else if((%deltaX == 0)&&(abs(%deltaY) == 1))
   {
      if((getTeam(%piece) == "Blue Team")&&(%deltaY == 1))
      {       
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         %piece.firstmove = false;
         move(%player, %piece, %destination);         
      }
      else if((getTeam(%piece) == "Red Team")&&(%deltaY == -1))
      {       
         if(willKingBeInCheck(%player, %piece, %destination) == true)
         {
            say(%player, %player, "<F5>That is not a valid move! You cannot make that move because it would put your King in check.", "alarm2.wav");
            updateFutureVariables();
            return;
         }
         $controlLock = true;
         %piece.firstmove = false;
         move(%player, %piece, %destination);         
      }
      else
      {
         say(%player, %player, "<F5>That is not a valid move! Pawns can only move 1 space forward or attack 1 space diagonal.", "alarm2.wav");
      }
   }
   else
   {
      say(%player, %player, "<F5>That is not a valid move! Pawns can only move 1 space forward or attack 1 space diagonal.", "alarm2.wav");
   }
}

function castle(%player, %king, %rook, %kingDest, %rookDest)
{
   playSound(0, $movePiece, IDPRF_2D);
   %x1 = getPosition(%rook, x);
   %y1 = getPosition(%rook, y);
   %x2 = getPosition(%rookDest, x);
   %y2 = getPosition(%rookDest, y);
   %distance = getDistance(%rook, %rookDest);
   %increments = %distance / 3;
   %xDist = %x2 - %x1;
   %yDist = %y2 - %y1;
   %xOffset = %xDist / %increments;
   %yOffset = %yDist / %increments;
   castleRook(%player, %king, %rook, %kingDest, %rookDest, %xOffset, %yOffset, %increments);
}

function castleRook(%player, %king, %rook, %kingDest, %rookDest, %xOffset, %yOffset, %increments)
{
   if(%player == $bluePlayer)
   {
      %rot = 180;
   }
   else if(%player == $redPlayer)
   {
      %rot = 0;
   }   
   if(%increments > 1)
   {
      %increments--;
      %x = getPosition(%rook, x) + %xOffset;
      %y = getPosition(%rook, y) + %yOffset;
      %z = getTerrainHeight(%x, %y);
      setPosition(%rook, %x, %y, %z, %rot);
      schedule("castleRook(" @ %player @ ", " @ %king @ ", " @ %rook @ ", " @ %kingDest @ ", " @ %rookDest @ ", " @ %xOffset @ ", " @ %yOffset @ ", " @ %increments @ ");", 0.1);
   }
   else
   {
      %x = getPosition(%rookDest, x);
      %y = getPosition(%rookDest, y);
      %z = getTerrainHeight(%x, %y);
      setPosition(%rook, %x, %y, %z, %rot);

      //Update the variables      
      %oldSquare = %rook.square;
      %oldSquare.piece = "";
      %rookDest.piece = %rook;
      %rook.square = %rookDest;

      castleKing(%player, %king, %kingDest);
   }
}

function castleKing(%player, %king, %kingDest)
{
   
   %x1 = getPosition(%king, x);
   %y1 = getPosition(%king, y);
   %x2 = getPosition(%kingDest, x);
   %y2 = getPosition(%kingDest, y);
   %distance = getDistance(%king, %kingDest);
   %increments = %distance / 3;
   %xDist = %x2 - %x1;
   %yDist = %y2 - %y1;
   %xOffset = %xDist / %increments;
   %yOffset = %yDist / %increments;
   if(%player == $bluePlayer)
   {
      %rot = 180;
   }
   else if(%player == $redPlayer)
   {
      %rot = 0;
   }   
   jumpCastle(%player, %king, %kingDest, %xOffset, %yOffset, %increments, %rot);
}

function jump(%piece, %rot)
{   
   %x = getPosition(%piece, x);
   %y = getPosition(%piece, y);
   %z = getPosition(%piece, z) + 3;
   setPosition(%piece, %x, %y, %z, %rot);
}

function fall(%piece, %rot)
{   
   %x = getPosition(%piece, x);
   %y = getPosition(%piece, y);
   %z = getPosition(%piece, z) - 3;
   setPosition(%piece, %x, %y, %z, %rot);
}

function jumpCastle(%player, %king, %kingDest, %xOffset, %yOffset, %increments, %rot)
{
   jump(%king, %rot);
   schedule("jump(" @ %king @ ", " @ %rot @ ");", 0.1);
   schedule("jump(" @ %king @ ", " @ %rot @ ");", 0.2);
   schedule("jump(" @ %king @ ", " @ %rot @ ");", 0.3);
   schedule("jump(" @ %king @ ", " @ %rot @ ");", 0.4);
   schedule("jump(" @ %king @ ", " @ %rot @ ");", 0.5);
   schedule("jump(" @ %king @ ", " @ %rot @ ");", 0.6);
   schedule("jumpCastle2(" @ %player @ ", " @ %king @ ", " @ %kingDest @ ", " @ %xOffset @ ", " @ %yOffset @ ", " @ %increments @ ", " @ %rot @ ");", 0.7);
}

function jumpCastle2(%player, %king, %kingDest, %xOffset, %yOffset, %increments, %rot)
{
   if(%increments > 1)
   {
      %increments--;
      %x = getPosition(%king, x) + %xOffset;
      %y = getPosition(%king, y) + %yOffset;
      %z = getPosition(%king, z);
      setPosition(%king, %x, %y, %z, %rot);
      schedule("jumpCastle2(" @ %player @ ", " @ %king @ ", " @ %kingDest @ ", " @ %xOffset @ ", " @ %yOffset @ ", " @ %increments @ ", " @ %rot @ ");", 0.1);
   }
   else
   {
      fall(%king, %rot);
      schedule("fall(" @ %king @ ", " @ %rot @ ");", 0.1);
      schedule("fall(" @ %king @ ", " @ %rot @ ");", 0.2);
      schedule("fall(" @ %king @ ", " @ %rot @ ");", 0.3);
      schedule("fall(" @ %king @ ", " @ %rot @ ");", 0.4);
      schedule("fall(" @ %king @ ", " @ %rot @ ");", 0.5);
      schedule("fall(" @ %king @ ", " @ %rot @ ");", 0.6);
      schedule("finishCastle(" @ %player @ ", " @ %king @ ", " @ %kingDest @ ", " @ %rot @ ");", 0.7);
   }
}

function finishCastle(%player, %king, %kingDest, %rot)
{   
   %x = getPosition(%kingDest, x);
   %y = getPosition(%kingDest, y);
   %z = getPosition(%x, %y);
   setPosition(%king, %x, %y, %z, %rot);

   say("Everybody", 14, "<F4>" @ getName(%player) @ " has castled his King.");

   //Update the variables      
   %oldSquare = %king.square;
   %oldSquare.piece = "";
   %kingDest.piece = %king;
   %king.square = %kingDest;
   changeTurn();
}

function move(%player, %piece, %destination)
{
   playSound(0, $movePiece, IDPRF_2D);
   %x1 = getPosition(%piece, x);
   %y1 = getPosition(%piece, y);
   %x2 = getPosition(%destination, x);
   %y2 = getPosition(%destination, y);
   %distance = getDistance(%piece, %destination);
   %increments = %distance / 3;
   %xDist = %x2 - %x1;
   %yDist = %y2 - %y1;
   %xOffset = %xDist / %increments;
   %yOffset = %yDist / %increments;
   if(getTeam(%piece) == "Blue Team")
   {
      %rot = 180;
   }
   else
   {
      %rot = 0;
   }
   slide(%player, %piece, %destination, %xOffset, %yOffset, %increments, %rot);
}

function slide(%player, %piece, %destination, %xOffset, %yOffset, %increments, %rot)
{
   if(%increments > 1)
   {
      %increments--;
      %x = getPosition(%piece, x) + %xOffset;
      %y = getPosition(%piece, y) + %yOffset;
      %z = getTerrainHeight(%x, %y);
      setPosition(%piece, %x, %y, %z, %rot);
      schedule("slide(" @ %player @ ", " @ %piece @ ", " @ %destination @ ", " @ %xOffset @ ", " @ %yOffset @ ", " @ %increments @ ", " @ %rot @ ");", 0.1);
   }
   else
   {
      %x = getPosition(%destination, x);
      %y = getPosition(%destination, y);
      %z = getTerrainHeight(%x, %y);
      setPosition(%piece, %x, %y, %z, %rot);
      if(%destination.piece != "")
      {
         take(%destination.piece, %destination);
      }
      else if((%piece.type == "Pawn")&&(%destination.enpassant != ""))
      {
         take(%destination.enpassant, %destination);
      }
      //Update the variables      
      %oldSquare = %piece.square;
      %oldSquare.piece = "";
      %destination.piece = %piece;
      %piece.square = %destination;
      if((%piece.type == "Pawn")&&(getTeam(%piece) == "Blue Team")&&(%destination.y == 8))
      {         
         schedule("promotePawn(" @ %player @ ");", 1);
      }
      else if((%piece.type == "Pawn")&&(getTeam(%piece) == "Red Team")&&(%destination.y == 1))
      {
         schedule("promotePawn(" @ %player @ ");", 1);
      }
      else
      {
         changeTurn();
      }
   }
}

function take(%piece, %destination)
{
   if(getTeam(%piece) == "Blue Team")
   {
      %attacker = $redPlayer;
      %victim = $bluePlayer;
   }
   else
   {      
      %attacker = $bluePlayer;
      %victim = $redPlayer;
   }
   if((%piece.type == "Pawn")&&(%destination.enpassant != ""))
   {
      %destination.enpassant = "";
      say("Everybody", 14, "<F4>" @ getName(%attacker) @ " has taken " @ getName(%victim) @ "'s " @ getType(%piece) @ " using the En Passant maneuver.");      
   }
   else
   {
      say("Everybody", 14, "<F4>" @ getName(%attacker) @ " has taken " @ getName(%victim) @ "'s " @ getType(%piece) @ ".");
   }
   deleteObject(%piece);
}

function promotePawn(%player)
{
   say("Everybody", 15, "<F4>" @ getName(%player) @ "'s Pawn has penetrated the enemy lines! It will now be promoted to a high-ranking piece.");   
   schedule("promoteControls(" @ %player @ ");", 4);
}

function promoteControls(%player)
{
   say("Everybody", 18, "<F4>" @ getName(%player) @ ", select the piece that you wish to promote your pawn to. Use the arrows to scroll through the selection list, then scan the \"Promote Pawn\" ball to make your selection.");
   say(%player, %player, "<F4>Current Selection: " @ %player.promotion); 
   if(%player == $bluePlayer)
   {
      setStaticShapeShortName($blueUp, "Scroll Forward");
      setStaticShapeShortName($blueDown, "Scroll Backward");
      setStaticShapeShortName($blueRight, "Scroll Forward");
      setStaticShapeShortName($blueLeft, "Scroll Backward");
      setStaticShapeShortName($blueMove, "Promote Pawn");
   }
   else if(%player == $redPlayer)
   {
      setStaticShapeShortName($redUp, "Scroll Forward");
      setStaticShapeShortName($redDown, "Scroll Backward");
      setStaticShapeShortName($redRight, "Scroll Forward");
      setStaticShapeShortName($redLeft, "Scroll Backward");
      setStaticShapeShortName($redMove, "Promote Pawn");
   }
   $promotion = true;
}

function scrollRight(%player)
{
   if(%player.promotion == "Queen")
   {   
      %player.promotion = "Knight";
   }
   else if(%player.promotion == "Knight")
   {   
      %player.promotion = "Rook";
   }
   else if(%player.promotion == "Rook")
   {   
      %player.promotion = "Bishop";
   }
   else if(%player.promotion == "Bishop")
   {   
      %player.promotion = "Queen";
   }
   say(%player, %player, "<F4>Current Selection: " @ %player.promotion);
}

function scrollLeft(%player)
{
   if(%player.promotion == "Queen")
   {   
      %player.promotion = "Bishop";
   }
   else if(%player.promotion == "Bishop")
   {   
      %player.promotion = "Rook";
   }
   else if(%player.promotion == "Rook")
   {   
      %player.promotion = "Knight";
   }
   else if(%player.promotion == "Knight")
   {   
      %player.promotion = "Queen";
   }
   say(%player, %player, "<F4>Current Selection: " @ %player.promotion);
}

function promote(%player)
{
   $promotion = false;
   if(%player == $bluePlayer)
   {
      if(%player.promotion == "Queen")
      {
         %promotion = $terranQueen;
      }
      else if(%player.promotion == "Knight")
      {
         %promotion = $terranKnight;
      }
      else if(%player.promotion == "Rook")
      {
         %promotion = $terranRook;
      }
      else if(%player.promotion == "Bishop")
      {
         %promotion = $terranBishop;
      }
   }
   else if(%player == $redPlayer)
   {
      if(%player.promotion == "Queen")
      {
         %promotion = $cybridQueen;
      }
      else if(%player.promotion == "Knight")
      {
         %promotion = $cybridKnight;
      }
      else if(%player.promotion == "Rook")
      {
         %promotion = $cybridRook;
      }
      else if(%player.promotion == "Bishop")
      {
         %promotion = $cybridBishop;
      }
   }
   %oldPiece = $selectedPiece;
   %pieceX = getGridPosition(%oldPiece, x);
   %pieceY = getGridPosition(%oldPiece, y);
   %square = $square[%pieceX @ "" @ %pieceY];
   setPosition(%oldPiece, 10000, 10000, -5000);
   %x = getPosition(%square, x);
   %y = getPosition(%square, y);
   %z = getTerrainHeight(%x, %y);
   %newPiece = copyObject(%promotion, "piece");
   addToSet($pieces, %newPiece);
   //Update the variables     
   %newPiece.type = %player.promotion;
   %newPiece.firstMove = false;
   %square.piece = %newPiece;
   %newPiece.square = %square;
   if(%oldPiece == $bluePawn1)
   {
      $bluePawn1 = %newPiece;
   }
   else if(%oldPiece == $bluePawn2)
   {
      $bluePawn2 = %newPiece;
   }
   else if(%oldPiece == $bluePawn3)
   {
      $bluePawn3 = %newPiece;
   }
   else if(%oldPiece == $bluePawn4)
   {
      $bluePawn4 = %newPiece;
   }
   else if(%oldPiece == $bluePawn5)
   {
      $bluePawn5 = %newPiece;
   }
   else if(%oldPiece == $bluePawn6)
   {
      $bluePawn6 = %newPiece;
   }
   else if(%oldPiece == $bluePawn7)
   {
      $bluePawn7 = %newPiece;
   }
   else if(%oldPiece == $bluePawn8)
   {
      $bluePawn8 = %newPiece;
   }
   else if(%oldPiece == $redPawn1)
   {
      $redPawn1 = %newPiece;
   }
   else if(%oldPiece == $redPawn2)
   {
      $redPawn2 = %newPiece;
   }
   else if(%oldPiece == $redPawn3)
   {
      $redPawn3 = %newPiece;
   }
   else if(%oldPiece == $redPawn4)
   {
      $redPawn4 = %newPiece;
   }
   else if(%oldPiece == $redPawn5)
   {
      $redPawn5 = %newPiece;
   }
   else if(%oldPiece == $redPawn6)
   {
      $redPawn6 = %newPiece;
   }
   else if(%oldPiece == $redPawn7)
   {
      $redPawn7 = %newPiece;
   }
   else if(%oldPiece == $redPawn8)
   {
      $redPawn8 = %newPiece;
   }
   if(getTeam(%newPiece) == "Blue Team")
   {
      %rot = 180;
   }
   else
   {
      %rot = 0;
   }
   setPosition(%newPiece, %x, %y, %z, %rot);
   deleteObject(%oldPiece);
   say("Everybody", 16, "<F4>" @ getName(%player) @ " has promoted his Pawn to a " @ %player.promotion @ ".");
   if(%player == $bluePlayer)
   {
      setStaticShapeShortName($blueUp, "Cursor Up");
      setStaticShapeShortName($blueDown, "Cursor Down");
      setStaticShapeShortName($blueRight, "Cursor Right");
      setStaticShapeShortName($blueLeft, "Cursor Left");
      setStaticShapeShortName($blueMove, "Move Selected Piece");
   }
   else if(%player == $redPlayer)
   {
      setStaticShapeShortName($redUp, "Cursor Up");
      setStaticShapeShortName($redDown, "Cursor Down");
      setStaticShapeShortName($redRight, "Cursor Right");
      setStaticShapeShortName($redLeft, "Cursor Left");
      setStaticShapeShortName($redMove, "Move Selected Piece");
   }
   changeTurn();
}

function abs(%number)
{
   if(%number >= 0)
   {
      %answer = %number;
   }
   else if(%number < 0)
   {
      %answer = -%number;
   }
}

function remoteMakeW(%player)
{
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorUp(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorUp(%player);
      }
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         scrollRight(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         scrollRight(%player);
      }
   }
}

function remoteBreakW(%player)
{
   //Blank function
}

function remoteMakeA(%player)
{
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorLeft(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorLeft(%player);
      }
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         scrollLeft(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         scrollLeft(%player);
      }
   }
}

function remoteBreakA(%player)
{
   //Blank function
}

function remoteMakeS(%player)
{
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorDown(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorDown(%player);
      }
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         scrollLeft(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         scrollLeft(%player);
      }
   }
}

function remoteBreakS(%player)
{
   //Blank function
}

function remoteMakeD(%player)
{
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorRight(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorRight(%player);
      }
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         scrollRight(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         scrollRight(%player);
      }
   }
}

function remoteBreakD(%player)
{
   //Blank function
}

function remoteMakeQ(%player)
{
   %vehicleId = playerManager::playerNumToVehicleId(%player);
   %target = getTargetId(%vehicleId);
   if(getObjectName(%target) == "piece")
   {
      piece::structure::onScan(%target, %vehicleId);
   }
}

function remoteBreakQ(%player)
{
   //Blank function
}

function remoteMakeE(%player)
{
   if($controlLock == false)
   {
      if(($cursorOn == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         $cursorOn = false;
         %destination = $square[$blueCursor.x @ "" @ $blueCursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition($blueCursor, 10000, 10000, -5000, 0, -90);
         setPosition($highlight, 10000, 10000, -5000);
         if($selectedPiece.type == "Pawn")
         {
            movePawn(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Rook")
         {
            moveRook(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Knight")
         {
            moveKnight(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Bishop")
         {
            moveBishop(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Queen")
         {
            moveQueen(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "King")
         {
            moveKing(%player, $selectedPiece, %destination);
         }
      }
      else if(($cursorOn == true)&&(%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         $cursorOn = false;
         %destination = $square[$redCursor.x @ "" @ $redCursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition($redCursor, 10000, 10000, -5000, 0, -90);
         setPosition($highlight, 10000, 10000, -5000);
         if($selectedPiece.type == "Pawn")
         {
            movePawn(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Rook")
         {
            moveRook(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Knight")
         {
            moveKnight(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Bishop")
         {
            moveBishop(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Queen")
         {
            moveQueen(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "King")
         {
            moveKing(%player, $selectedPiece, %destination);
         }
      }    
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         promote(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         promote(%player);
      }
   }
}

function remoteBreakE(%player)
{
   //Blank function
}

function remoteMakeF(%player)
{
   //Blank function
}

function remoteBreakF(%player)
{
   //Blank function
}

function remoteMakeR(%player)
{
   //Blank function
}

function remoteBreakR(%player)
{
   //Blank function
}

function remoteForwardSwitch(%player)
{
   %player.delay = true;
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorUp(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorUp(%player);
      }
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         scrollRight(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         scrollRight(%player);
      }
   }
}

function remoteBoostSwitch(%player)
{
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorDown(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorDown(%player);
      }
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         scrollLeft(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         scrollLeft(%player);
      }
   }
}

function remoteCancelSwitch(%player)
{
   if(%player.delay == false)
   {
      if($controlLock == false)
      {
         if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
         {
            moveCursorLeft(%player);
         }
         else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
         {
            moveCursorLeft(%player);
         }
      }
      else if($promotion == true)
      {
         if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
         {
            scrollLeft(%player);
         }
         else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
         {
            scrollLeft(%player);
         }
      }
   }
   %player.delay = false;
}

function remoteHoverSwitch(%player)
{
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorRight(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorRight(%player);
      }
   }
   else if($promotion == true)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         scrollRight(%player);
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         scrollRight(%player);
      }
   }
}

function piece::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer))
      {
         if(getTeam(%scanned) == "Blue Team")
         {
            $cursorOn = true;
            $selectedPiece = %scanned;
            $blueCursor.x = getGridPosition(%scanned, x);
            $blueCursor.y = getGridPosition(%scanned, y);
            %square = $square[$blueCursor.x @ "" @ $blueCursor.y];
            %x = getPosition(%square, x);
            %y = getPosition(%square, y);
            %z = getTerrainHeight(%x, %y);
            setPosition($blueCursor, %x, %y, %z + 15, 180, -90);
            setPosition($highlight, %x, %y, %z + 0.1);
            playSound(0, $cursorPopUp, IDPRF_2D);
         }
         else
         {
            say(%player, %player, "<F5>That is not your piece!", "alarm2.wav");
         }
      }
      else if((%player == $redPlayer)&&($currentTurn == $redPlayer))
      {
         if(getTeam(%scanned) == "Red Team")
         {
            $cursorOn = true;
            $selectedPiece = %scanned;
            $redCursor.x = getGridPosition(%scanned, x);
            $redCursor.y = getGridPosition(%scanned, y);
            %square = $square[$redCursor.x @ "" @ $redCursor.y];
            %x = getPosition(%square, x);
            %y = getPosition(%square, y);
            %z = getTerrainHeight(%x, %y);
            setPosition($redCursor, %x, %y, %z + 15, 0, -90);
            setPosition($highlight, %x, %y, %z + 0.1);
            playSound(0, $cursorPopUp, IDPRF_2D);
         }
         else
         {
            say(%player, %player, "<F5>That is not your piece!", "alarm2.wav");
         }
      }
   }
}

function bluematch::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if($gameStarted == true)
      {
         if(%player == $bluePlayer)
         {            
            surrender(%player);
         }
      }
      else 
      {
         if($matchCalled == false)
         {
            $matchCalled = true;
            $bluePlayer = %player;
            setHudTimer(0, -1, "", 2);
            setHudTimer(60, -1, "Match Called:", 2);
            schedule("resetMatch();", 60);
            setStaticShapeShortName($blueMatch, "Waiting...");
            setStaticShapeShortName($redMatch, "Join In");
            say("Everybody", 12, "<F4>" @ getName(%player) @ " has called a Chess match! Join in at the red controls to accept the challenge.");
         }
         else
         {
            if(%player != $redPlayer)
            {
               if($bluePlayer == "")
               {
                  $matchCalled = false;
                  $bluePlayer = %player;
                  setHudTimer(0, -1, "", 2);
                  say("Everybody", 12, "<F4>" @ getName(%player) @ " has accepted " @ getName($redPlayer) @ "'s challenge!");
                  setBoard();
               }
               else
               {
                  say(%player, %player, "<F5>The blue controls have already been taken! Join in at the red controls!");
               }
            }
         }
      }
   }
}

function blueUp::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorUp(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollRight(%player);
   }
}

function blueDown::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorDown(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollLeft(%player);
   }
}

function blueLeft::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorLeft(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollLeft(%player);
   }
}

function blueRight::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         moveCursorRight(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollRight(%player);
   }
}

function bluemove::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $bluePlayer)&&($currentTurn == $bluePlayer)&&($cursorOn == true))
      {
         $cursorOn = false;
         %destination = $square[$blueCursor.x @ "" @ $blueCursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition($blueCursor, 10000, 10000, -5000, 0, -90);
         setPosition($highlight, 10000, 10000, -5000);
         if($selectedPiece.type == "Pawn")
         {
            movePawn(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Rook")
         {
            moveRook(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Knight")
         {
            moveKnight(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Bishop")
         {
            moveBishop(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Queen")
         {
            moveQueen(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "King")
         {
            moveKing(%player, $selectedPiece, %destination);
         }
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      promote(%player);
   }
}

function redmatch::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if($gameStarted == true)
      {
         if(%player == $redPlayer)
         {
            surrender(%player);
         }
      }
      else 
      {
         if($matchCalled == false)
         {
            $matchCalled = true;
            $redPlayer = %player;
            setHudTimer(0, -1, "", 2);
            setHudTimer(60, -1, "Match Called:", 2);
            schedule("resetMatch();", 60);
            setStaticShapeShortName($redMatch, "Waiting...");
            setStaticShapeShortName($blueMatch, "Join In");
            say("Everybody", 12, "<F4>" @ getName(%player) @ " has called a Chess match! Join in at the blue controls to accept the challenge.");
         }
         else
         {
            if(%player != $bluePlayer)
            {
               if($redPlayer == "")
               {
                  $matchCalled = false;
                  $redPlayer = %player;
                  setHudTimer(0, -1, "", 2);
                  say("Everybody", 12, "<F4>" @ getName(%player) @ " has accepted " @ getName($bluePlayer) @ "'s challenge!");
                  setBoard();
               }
               else
               {
                  say(%player, %player, "<F5>The red controls have already been taken! Join in at the blue controls!");
               }
            }
         }
      }
   }
}

function redUp::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorUp(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollRight(%player);
   }
}

function redDown::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorDown(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollLeft(%player);
   }
}

function redLeft::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorLeft(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollLeft(%player);
   }
}

function redRight::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         moveCursorRight(%player);
      }
   }
   else if(($promotion == true)&&(%player == $bluePlayer)&&($currentTurn == $bluePlayer))
   {
      scrollRight(%player);
   }
}

function redmove::structure::onScan(%scanned, %scanner)
{
   %player = playerManager::vehicleIdToPlayerNum(%scanner);
   if($controlLock == false)
   {
      if((%player == $redPlayer)&&($currentTurn == $redPlayer)&&($cursorOn == true))
      {
         $cursorOn = false;
         %destination = $square[$redCursor.x @ "" @ $redCursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition($redCursor, 10000, 10000, -5000, 180, -90);
         setPosition($highlight, 10000, 10000, -5000);
         if($selectedPiece.type == "Pawn")
         {
            movePawn(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Rook")
         {
            moveRook(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Knight")
         {
            moveKnight(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Bishop")
         {
            moveBishop(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "Queen")
         {
            moveQueen(%player, $selectedPiece, %destination);
         }
         else if($selectedPiece.type == "King")
         {
            moveKing(%player, $selectedPiece, %destination);
         }
      }
   }
   else if(($promotion == true)&&(%player == $redPlayer)&&($currentTurn == $redPlayer))
   {
      promote(%player);
   }
}

function moveCursorUp(%player)
{
   if(%player == $bluePlayer)
   {
      %cursor = $blueCursor;
      if(%cursor.y < 8)
      {
         %cursor.y++;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 180, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }      
      playSound(0, $cursorMove, IDPRF_2D);
   }
   else if(%player == $redPlayer)
   {
      %cursor = $redCursor;
      if(%cursor.y > 1)
      {
         %cursor.y--;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 0, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }
      playSound(0, $cursorMove, IDPRF_2D);
   }
}

function moveCursorDown(%player)
{
   if(%player == $bluePlayer)
   {
      %cursor = $blueCursor;
      if(%cursor.y > 1)
      {
         %cursor.y--;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 180, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }
      playSound(0, $cursorMove, IDPRF_2D);
   }
   else if(%player == $redPlayer)
   {
      %cursor = $redCursor;
      if(%cursor.y < 8)
      {
         %cursor.y++;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 0, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }
      playSound(0, $cursorMove, IDPRF_2D);
   }
}

function moveCursorLeft(%player)
{
   if(%player == $bluePlayer)
   {
      %cursor = $blueCursor;
      if(%cursor.x > 1)
      {
         %cursor.x--;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 180, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }
      playSound(0, $cursorMove, IDPRF_2D);
   }
   else if(%player == $redPlayer)
   {
      %cursor = $redCursor;
      if(%cursor.x < 8)
      {
         %cursor.x++;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 0, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }
      playSound(0, $cursorMove, IDPRF_2D);
   }
}

function moveCursorRight(%player)
{
   if(%player == $bluePlayer)
   {
      %cursor = $blueCursor;
      if(%cursor.x < 8)
      {
         %cursor.x++;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 180, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }
      playSound(0, $cursorMove, IDPRF_2D);
   }
   else if(%player == $redPlayer)
   {
      %cursor = $redCursor;
      if(%cursor.x > 1)
      {
         %cursor.x--;
         %square = $square[%cursor.x @ "" @ %cursor.y];
         %x = getPosition(%square, x);
         %y = getPosition(%square, y);
         %z = getTerrainHeight(%x, %y);
         setPosition(%cursor, %x, %y, %z + 15, 0, -90);
         setPosition($highlight, %x, %y, %z + 0.1);
      }
      playSound(0, $cursorMove, IDPRF_2D);
   }
}

function resetMatch()
{   
   if($matchCalled == true)
   {
      $matchCalled = false;
      setStaticShapeShortName($blueMatch, "Call Match");
      setStaticShapeShortName($redMatch, "Call Match");
      setHudTimer(0, -1, "", 2);
      say("Everybody", 13, "<F4>The match has been aborted because the challenge was not accepted.");
   }
}

function isKingInCheck(%player)
{
   if(%player == $bluePlayer)
   {
      %team = *IDSTR_TEAM_BLUE;
      %king = $blueKing;
   }
   else if(%player == $redPlayer)
   {
      %team = *IDSTR_TEAM_RED;
      %king = $redKing;
   }
   %check = false;
   for(%numY = 1; %numY <= 8; %numY++)
   {
      for(%numX = 1; %numX <= 8; %numX++)
      {
         %curPiece = $square[%numX @ "" @ %numY].piece;
         if((%curPiece != "")&&(getTeam(%curPiece) != %team))
         {
            %test = testForCheck(%king, %curPiece);
            if(%test == true)
            {
               %check = true;
            }
         }
      }
   }
   return %check;
}

function testForCheck(%king, %piece)
{
   if(%piece.type == "Rook")
   {
      %test = testRook(%king, %piece);
   }
   else if(%piece.type == "Bishop")
   {
      %test = testBishop(%king, %piece);
   }
   else if(%piece.type == "Knight")
   {
      %test = testKnight(%king, %piece);
   }
   else if(%piece.type == "Queen")
   {
      %test = testQueen(%king, %piece);
   }
   else if(%piece.type == "King")
   {
      %test = testKing(%king, %piece);
   }
   else if(%piece.type == "Pawn")
   {
      %test = testPawn(%king, %piece);
   }
   return %test;
}

function testRook(%king, %piece)
{
   %test = false;
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = getGridPosition(%king, x);
   %y2 = getGridPosition(%king, y);
   if((%x1 == %x2)||(%y1 == %y2))
   {
      if(%x1 == %x2)
      {
         %max = abs(%y2 - %y1);
         %access = true;
         if(%y2 < %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 - %i;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%y2 > %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 + %i;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      else if(%y1 == %y2)
      {
         %max = abs(%x2 - %x1);
         %access = true;
         if(%x2 < %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 - %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%x2 > %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 + %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].piece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      if(%access == true)
      {
         %test = true;
      }
   }
   return %test;
}

function testBishop(%king, %piece)
{
   %test = false;
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = getGridPosition(%king, x);
   %y2 = getGridPosition(%king, y);       
   %deltaX = %x2 - %x1;
   %deltaY = %y2 - %y1;
   if(abs(%deltaX) == abs(%deltaY))
   {
      %access = true;
      %max = abs(%deltaX);
      if((%deltaX > 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }      
      }
      else if((%deltaX < 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX > 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX < 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].piece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      if(%access == true)
      {
         %test = true;
      }
   }
   return %test;
}

function testKnight(%king, %piece)
{
   %test = false;
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = getGridPosition(%king, x);
   %y2 = getGridPosition(%king, y);       
   %deltaX = %x2 - %x1;
   %deltaY = %y2 - %y1;
   if(((abs(%deltaX) == 2)&&(abs(%deltaY) == 1))||((abs(%deltaX) == 1)&&(abs(%deltaY) == 2)))
   {
      %test = true;
   }
   return %test;
}

function testQueen(%king, %piece)
{
   %test = false;
   %test1 = testRook(%king, %piece);
   if(%test1 == false)
   {
      %test2 = testBishop(%king, %piece);
      if(%test2 == true)
      {
         %test = true;
      }
   }
   else
   {
      %test = true;
   }
   return %test;
}

function testKing(%king, %piece)
{
   %test = false;
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = getGridPosition(%king, x);
   %y2 = getGridPosition(%king, y);
   %deltaX = %x2 - %x1; 
   %deltaY = %y2 - %y1;         
   if((abs(%deltaX) == 1)&&(abs(%deltaY) == 1))
   {
      %test = true;
   }
   else if((abs(%deltaX) == 1)&&(abs(%deltaY) == 0))
   {
      %test = true;
   }
   else if((abs(%deltaX) == 0)&&(abs(%deltaY) == 1))
   {
      %test = true;
   }
   return %test;
}

function testPawn(%king, %piece)
{
   %test = false;
   %x1 = getGridPosition(%piece, x);
   %y1 = getGridPosition(%piece, y);
   %x2 = getGridPosition(%king, x);
   %y2 = getGridPosition(%king, y);
   %deltaX = %x2 - %x1; 
   %deltaY = %y2 - %y1;               
   if((getTeam(%piece) == "Blue Team")&&(%deltaY == 1)&&(abs(%deltaX) == 1))
   {            
      %test = true;     
   }
   else if((getTeam(%piece) == "Red Team")&&(%deltaY == -1)&&(abs(%deltaX) == 1))
   {
      %test = true;                 
   }
   return %test;
}

function isKingInCheckFuture(%player)
{
   if(%player == $bluePlayer)
   {
      %team = *IDSTR_TEAM_BLUE;
      %king = $blueKing;
   }
   else if(%player == $redPlayer)
   {
      %team = *IDSTR_TEAM_RED;
      %king = $redKing;
   }
   %check = false;
   for(%numY = 1; %numY <= 8; %numY++)
   {
      for(%numX = 1; %numX <= 8; %numX++)
      {
         %curPiece = $square[%numX @ "" @ %numY].newpiece;
         if((%curPiece != "")&&(getTeam(%curPiece) != %team))
         {
            %test = testForCheckFuture(%king, %curPiece);
            if(%test == true)
            {
               %check = true;
            }
         }
      }
   }
   return %check;
}

function testForCheckFuture(%king, %piece)
{
   if(%piece.type == "Rook")
   {
      %test = testRookFuture(%king, %piece);
   }
   else if(%piece.type == "Bishop")
   {
      %test = testBishopFuture(%king, %piece);
   }
   else if(%piece.type == "Knight")
   {
      %test = testKnightFuture(%king, %piece);
   }
   else if(%piece.type == "Queen")
   {
      %test = testQueenFuture(%king, %piece);
   }
   else if(%piece.type == "King")
   {
      %test = testKingFuture(%king, %piece);
   }
   else if(%piece.type == "Pawn")
   {
      %test = testPawnFuture(%king, %piece);
   }
   return %test;
}

function testRookFuture(%king, %piece)
{
   %test = false;
   %x1 = getFutureGridPosition(%piece, x);
   %y1 = getFutureGridPosition(%piece, y);
   %x2 = getFutureGridPosition(%king, x);
   %y2 = getFutureGridPosition(%king, y);
   if((%x1 == %x2)||(%y1 == %y2))
   {
      if(%x1 == %x2)
      {
         %max = abs(%y2 - %y1);
         %access = true;
         if(%y2 < %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 - %i;
               %curpiece = $square[%curX @ "" @ %curY].newpiece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%y2 > %y1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1;
               %curY = %y1 + %i;
               %curpiece = $square[%curX @ "" @ %curY].newpiece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      else if(%y1 == %y2)
      {
         %max = abs(%x2 - %x1);
         %access = true;
         if(%x2 < %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 - %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].newpiece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
         else if(%x2 > %x1)
         {
            for(%i = 1; %i < %max; %i++)
            {
               %curX = %x1 + %i;
               %curY = %y1;
               %curpiece = $square[%curX @ "" @ %curY].newpiece;
               if(%curpiece != "")
               {
                  %access = false;
               }
            }
         }
      }
      if(%access == true)
      {
         %test = true;
      }
   }
   return %test;
}

function testBishopFuture(%king, %piece)
{
   %test = false;
   %x1 = getFutureGridPosition(%piece, x);
   %y1 = getFutureGridPosition(%piece, y);
   %x2 = getFutureGridPosition(%king, x);
   %y2 = getFutureGridPosition(%king, y);       
   %deltaX = %x2 - %x1;
   %deltaY = %y2 - %y1;
   if(abs(%deltaX) == abs(%deltaY))
   {
      %access = true;
      %max = abs(%deltaX);
      if((%deltaX > 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].newpiece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }      
      }
      else if((%deltaX < 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].newpiece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX > 0)&&(%deltaY < 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 + %i;
            %curY = %y1 - %i;
            %curpiece = $square[%curX @ "" @ %curY].newpiece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      else if((%deltaX < 0)&&(%deltaY > 0))
      {
         for(%i = 1; %i < %max; %i++)
         {
            %curX = %x1 - %i;
            %curY = %y1 + %i;
            %curpiece = $square[%curX @ "" @ %curY].newpiece;
            if(%curpiece != "")
            {
               %access = false;
            }
         }         
      }
      if(%access == true)
      {
         %test = true;
      }
   }
   return %test;
}

function testKnightFuture(%king, %piece)
{
   %test = false;
   %x1 = getFutureGridPosition(%piece, x);
   %y1 = getFutureGridPosition(%piece, y);
   %x2 = getFutureGridPosition(%king, x);
   %y2 = getFutureGridPosition(%king, y);       
   %deltaX = %x2 - %x1;
   %deltaY = %y2 - %y1;
   if(((abs(%deltaX) == 2)&&(abs(%deltaY) == 1))||((abs(%deltaX) == 1)&&(abs(%deltaY) == 2)))
   {
      %test = true;
   }
   return %test;
}

function testQueenFuture(%king, %piece)
{
   %test = false;
   %test1 = testRookFuture(%king, %piece);
   if(%test1 == false)
   {
      %test2 = testBishopFuture(%king, %piece);
      if(%test2 == true)
      {
         %test = true;
      }
   }
   else
   {
      %test = true;
   }
   return %test;
}

function testKingFuture(%king, %piece)
{
   %test = false;
   %x1 = getFutureGridPosition(%piece, x);
   %y1 = getFutureGridPosition(%piece, y);
   %x2 = getFutureGridPosition(%king, x);
   %y2 = getFutureGridPosition(%king, y);
   %deltaX = %x2 - %x1; 
   %deltaY = %y2 - %y1;         
   if((abs(%deltaX) == 1)&&(abs(%deltaY) == 1))
   {
      %test = true;
   }
   else if((abs(%deltaX) == 1)&&(abs(%deltaY) == 0))
   {
      %test = true;
   }
   else if((abs(%deltaX) == 0)&&(abs(%deltaY) == 1))
   {
      %test = true;
   }
   return %test;
}

function testPawnFuture(%king, %piece)
{
   %test = false;
   %x1 = getFutureGridPosition(%piece, x);
   %y1 = getFutureGridPosition(%piece, y);
   %x2 = getFutureGridPosition(%king, x);
   %y2 = getFutureGridPosition(%king, y);
   %deltaX = %x2 - %x1; 
   %deltaY = %y2 - %y1;               
   if((getTeam(%piece) == "Blue Team")&&(%deltaY == 1)&&(abs(%deltaX) == 1))
   {            
      %test = true;     
   }
   else if((getTeam(%piece) == "Red Team")&&(%deltaY == -1)&&(abs(%deltaX) == 1))
   {
      %test = true;                 
   }
   return %test;
}

function surrender(%player)
{
   playSound(0, "sfx_thunder2.wav", IDPRF_2D);
   $controlLock = true;
   flushConsoleScheduler();
   if(%player == $bluePlayer)
   {
      say("Everybody", 18, "<F4>" @ getName($bluePlayer) @ " has surrendered to " @ getName($redPlayer) @ "!");
   }
   else if(%player == $redPlayer)
   {
      say("Everybody", 18, "<F4>" @ getName($redPlayer) @ " has surrendered to " @ getName($bluePlayer) @ "!");
   }
   deleteObject($pieces);
   $bluePlayer = "";
   $redPlayer = "";
   $gameStarted = false;
   $matchCalled = false;
   $currentTurn = "";
   $selectedPiece = "";
   $cursorOn = false;
   $promotion = false;
   setStaticShapeShortName($blueMatch, "Call Match");
   setStaticShapeShortName($redMatch, "Call Match");
   setPosition($blueCursor, 10000, 10000, -5000);
   setPosition($redCursor, 10000, 10000, -5000);
   setPosition($highlight, 10000, 10000, -5000);
   $controlLock = false;
}

function onMissionEnd()
{
   $controlLock = true;
   flushConsoleScheduler();
   deleteObject($pieces);
   $bluePlayer = "";
   $redPlayer = "";
   $gameStarted = false;
   $matchCalled = false;
   $currentTurn = "";
   $selectedPiece = "";
   $cursorOn = false;
   $promotion = false;
   setStaticShapeShortName($blueMatch, "Call Match");
   setStaticShapeShortName($redMatch, "Call Match");
   setPosition($blueCursor, 10000, 10000, -5000);
   setPosition($redCursor, 10000, 10000, -5000);
   setPosition($highlight, 10000, 10000, -5000);
   $controlLock = false;
}

function initScoreBoard()
{
   deleteVariables("$ScoreBoard::PlayerColumn*");
   deleteVariables("$ScoreBoard::TeamColumn*");

   if($server::TeamPlay == "True")	
   {
	   // Player ScoreBoard column headings
	   $ScoreBoard::PlayerColumnHeader1 = "Squad";

	   // Player ScoreBoard column functions
	   $ScoreBoard::PlayerColumnFunction1 = "getSquad";
   }
   else
   {
       // Player ScoreBoard column headings
	   $ScoreBoard::PlayerColumnHeader1 = "Squad";

	   // Player ScoreBoard column functions
	   $ScoreBoard::PlayerColumnFunction1 = "getSquad";
   }

   // Team ScoreBoard column headings
   $ScoreBoard::TeamColumnHeader1 = "Players";

   // Team ScoreBoard column functions
   $ScoreBoard::TeamColumnFunction1 = "getNumberOfPlayersOnTeam";

   // tell server to process all the scoreboard definitions defined above
   serverInitScoreBoard();
}