void main()
{

object oPC = GetClickingObject();
if (!GetIsPC(oPC)) return;

if (GetLocalInt(oPC, "DisarmChessboard") == 1) return;

// Jump to center of square
object oWP = GetNearestObjectByTag("WP_Chess");
AssignCommand(oPC, ClearAllActions());
AssignCommand(oPC, ActionJumpToLocation(GetLocation(oWP)));

// Assign global variables
int i;
int nSquares = 8;
int nRowNow;
int nColumnNow;
int nRowValid;
int nColumnValid;
string sRowValid;
string sColumnValid;
string sValidTag;
effect eDamage = EffectDamage(5, DAMAGE_TYPE_MAGICAL, DAMAGE_POWER_ENERGY);
object oBeam = GetObjectByTag("InvisBeam");
effect eBeam = EffectBeam(VFX_BEAM_LIGHTNING, oBeam, BODY_NODE_CHEST, FALSE);

// Set default case to "FALSE"
int nValidMove = FALSE;

// Establish which square the PC now occupies
string sSelf = GetTag(OBJECT_SELF);
// Row 1
if (sSelf == "R1C1") ((nRowNow = 1)&&(nColumnNow = 1));
if (sSelf == "R1C2") ((nRowNow = 1)&&(nColumnNow = 2));
if (sSelf == "R1C3") ((nRowNow = 1)&&(nColumnNow = 3));
if (sSelf == "R1C4") ((nRowNow = 1)&&(nColumnNow = 4));
if (sSelf == "R1C5") ((nRowNow = 1)&&(nColumnNow = 5));
if (sSelf == "R1C6") ((nRowNow = 1)&&(nColumnNow = 6));
if (sSelf == "R1C7") ((nRowNow = 1)&&(nColumnNow = 7));
if (sSelf == "R1C8") ((nRowNow = 1)&&(nColumnNow = 8));
// Row 2
if (sSelf == "R2C1") ((nRowNow = 2)&&(nColumnNow = 1));
if (sSelf == "R2C2") ((nRowNow = 2)&&(nColumnNow = 2));
if (sSelf == "R2C3") ((nRowNow = 2)&&(nColumnNow = 3));
if (sSelf == "R2C4") ((nRowNow = 2)&&(nColumnNow = 4));
if (sSelf == "R2C5") ((nRowNow = 2)&&(nColumnNow = 5));
if (sSelf == "R2C6") ((nRowNow = 2)&&(nColumnNow = 6));
if (sSelf == "R2C7") ((nRowNow = 2)&&(nColumnNow = 7));
if (sSelf == "R2C8") ((nRowNow = 2)&&(nColumnNow = 8));
// Row 3
if (sSelf == "R3C1") ((nRowNow = 3)&&(nColumnNow = 1));
if (sSelf == "R3C2") ((nRowNow = 3)&&(nColumnNow = 2));
if (sSelf == "R3C3") ((nRowNow = 3)&&(nColumnNow = 3));
if (sSelf == "R3C4") ((nRowNow = 3)&&(nColumnNow = 4));
if (sSelf == "R3C5") ((nRowNow = 3)&&(nColumnNow = 5));
if (sSelf == "R3C6") ((nRowNow = 3)&&(nColumnNow = 6));
if (sSelf == "R3C7") ((nRowNow = 3)&&(nColumnNow = 7));
if (sSelf == "R3C8") ((nRowNow = 3)&&(nColumnNow = 8));
// Row 4
if (sSelf == "R4C1") ((nRowNow = 4)&&(nColumnNow = 1));
if (sSelf == "R4C2") ((nRowNow = 4)&&(nColumnNow = 2));
if (sSelf == "R4C3") ((nRowNow = 4)&&(nColumnNow = 3));
if (sSelf == "R4C4") ((nRowNow = 4)&&(nColumnNow = 4));
if (sSelf == "R4C5") ((nRowNow = 4)&&(nColumnNow = 5));
if (sSelf == "R4C6") ((nRowNow = 4)&&(nColumnNow = 6));
if (sSelf == "R4C7") ((nRowNow = 4)&&(nColumnNow = 7));
if (sSelf == "R4C8") ((nRowNow = 4)&&(nColumnNow = 8));
// Row 5
if (sSelf == "R5C1") ((nRowNow = 5)&&(nColumnNow = 1));
if (sSelf == "R5C2") ((nRowNow = 5)&&(nColumnNow = 2));
if (sSelf == "R5C3") ((nRowNow = 5)&&(nColumnNow = 3));
if (sSelf == "R5C4") ((nRowNow = 5)&&(nColumnNow = 4));
if (sSelf == "R5C5") ((nRowNow = 5)&&(nColumnNow = 5));
if (sSelf == "R5C6") ((nRowNow = 5)&&(nColumnNow = 6));
if (sSelf == "R5C7") ((nRowNow = 5)&&(nColumnNow = 7));
if (sSelf == "R5C8") ((nRowNow = 5)&&(nColumnNow = 8));
// Row 6
if (sSelf == "R6C1") ((nRowNow = 6)&&(nColumnNow = 1));
if (sSelf == "R6C2") ((nRowNow = 6)&&(nColumnNow = 2));
if (sSelf == "R6C3") ((nRowNow = 6)&&(nColumnNow = 3));
if (sSelf == "R6C4") ((nRowNow = 6)&&(nColumnNow = 4));
if (sSelf == "R6C5") ((nRowNow = 6)&&(nColumnNow = 5));
if (sSelf == "R6C6") ((nRowNow = 6)&&(nColumnNow = 6));
if (sSelf == "R6C7") ((nRowNow = 6)&&(nColumnNow = 7));
if (sSelf == "R6C8") ((nRowNow = 6)&&(nColumnNow = 8));
// Row 7
if (sSelf == "R7C1") ((nRowNow = 7)&&(nColumnNow = 1));
if (sSelf == "R7C2") ((nRowNow = 7)&&(nColumnNow = 2));
if (sSelf == "R7C3") ((nRowNow = 7)&&(nColumnNow = 3));
if (sSelf == "R7C4") ((nRowNow = 7)&&(nColumnNow = 4));
if (sSelf == "R7C5") ((nRowNow = 7)&&(nColumnNow = 5));
if (sSelf == "R7C6") ((nRowNow = 7)&&(nColumnNow = 6));
if (sSelf == "R7C7") ((nRowNow = 7)&&(nColumnNow = 7));
if (sSelf == "R7C8") ((nRowNow = 7)&&(nColumnNow = 8));
// Row 8
if (sSelf == "R8C1") ((nRowNow = 8)&&(nColumnNow = 1));
if (sSelf == "R8C2") ((nRowNow = 8)&&(nColumnNow = 2));
if (sSelf == "R8C3") ((nRowNow = 8)&&(nColumnNow = 3));
if (sSelf == "R8C4") ((nRowNow = 8)&&(nColumnNow = 4));
if (sSelf == "R8C5") ((nRowNow = 8)&&(nColumnNow = 5));
if (sSelf == "R8C6") ((nRowNow = 8)&&(nColumnNow = 6));
if (sSelf == "R8C7") ((nRowNow = 8)&&(nColumnNow = 7));
if (sSelf == "R8C8") ((nRowNow = 8)&&(nColumnNow = 8));
//FloatingTextStringOnCreature("Clicked "+sSelf+" square", oPC);

// Check to see if present square is a valid move from the PC's previous square
//
// Get the tag (sPreviousSquare) of the square previously occupied by PC
int nRowPrevious = GetLocalInt(oPC, "RowPC");
int nColumnPrevious = GetLocalInt(oPC, "ColumnPC");
string sRowPrevious = IntToString(nRowPrevious);
string sColumnPrevious = IntToString(nColumnPrevious);
string sPreviousSquare = "R"+sRowPrevious+"C"+sColumnPrevious+"";

// Compare tag of previous (sPreviousSquare) and present (sSelf) to see
// if it was a valid move (of course, it is different depending upon
// what "piece" the PC happens to be)...

// First, call variables to see which of the different chess pieces
// the PC happens to be
int nRook = GetLocalInt(oPC, "PCisROOK");
int nKnight = GetLocalInt(oPC, "PCisKNIGHT");
int nBishop = GetLocalInt(oPC, "PCisBISHOP");
int nKing = GetLocalInt(oPC, "PCisKING");

// If PC is the Rook, establish moves which were valid from the previous square
if (nRook == 1)
 {
  //Valid Moves within same row, forward columns
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = nRowPrevious;
  nColumnValid = (nColumnNow + i);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
  //Valid Moves within same row, backward columns
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = nRowPrevious;
  nColumnValid = (nColumnNow - i);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
  //Valid Moves within same column, forward rows
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = (nRowNow + i);
  nColumnValid = nColumnPrevious;
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
  //Valid Moves within same column, backward rows
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = (nRowNow - i);
  nColumnValid = nColumnPrevious;
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
 // End conditions for "Rook"
 }

// If PC is the Bishop, establish moves which were valid from the previous square
if (nBishop == 1)
 {
  //Valid Moves within negative rows, positive columns (diagonal from PC)
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = (nRowPrevious - i);
  nColumnValid = (nColumnPrevious + i);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
  //Valid Moves within negative rows, negative columns (diagonal from PC)
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = (nRowPrevious - i);
  nColumnValid = (nColumnPrevious - i);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
  //Valid Moves within positive rows, positive columns (diagonal from PC)
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = (nRowPrevious + i);
  nColumnValid = (nColumnPrevious + i);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
  //Valid Moves within positive rows, negative columns (diagonal from PC)
  for (i = 0; i < (nSquares+1); i++)
  {
  nRowValid = (nRowPrevious + i);
  nColumnValid = (nColumnPrevious - i);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  }
 // End conditions for "Bishop"
 }

// If PC is the King, establish moves which were valid from the previous square
if (nKing == 1)
 {
  //Valid Moves for the king are one square (any direction) from previous square
  //Valid Move 1
  nRowValid = nRowPrevious;
  nColumnValid = (nColumnPrevious + 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 2
  nRowValid = nRowPrevious;
  nColumnValid = (nColumnPrevious - 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 3
  nRowValid = (nRowPrevious + 1);
  nColumnValid = nColumnPrevious;
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 4
  nRowValid = (nRowPrevious - 1);
  nColumnValid = nColumnPrevious;
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 5
  nRowValid = (nRowPrevious + 1);
  nColumnValid = (nColumnPrevious + 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 6
  nRowValid = (nRowPrevious + 1);
  nColumnValid = (nColumnPrevious - 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 7
  nRowValid = (nRowPrevious - 1);
  nColumnValid = (nColumnPrevious + 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 8
  nRowValid = (nRowPrevious - 1);
  nColumnValid = (nColumnPrevious - 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
 // End conditions for "King"
 }

// If PC is the Knight, establish moves were valid from the previous square
if (nKnight == 1)
 {
  //Valid Moves for the knight are "L-shaped" from previous square
  //Valid Move 1
  nRowValid = (nRowPrevious - 2);
  nColumnValid = (nColumnPrevious + 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 2
  nRowValid = (nRowPrevious - 2);
  nColumnValid = (nColumnPrevious - 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 3
  nRowValid = (nRowPrevious - 1);
  nColumnValid = (nColumnPrevious + 2);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 4
  nRowValid = (nRowPrevious - 1);
  nColumnValid = (nColumnPrevious - 2);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 5
  nRowValid = (nRowPrevious + 1);
  nColumnValid = (nColumnPrevious + 2);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 6
  nRowValid = (nRowPrevious + 1);
  nColumnValid = (nColumnPrevious - 2);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 7
  nRowValid = (nRowPrevious + 2);
  nColumnValid = (nColumnPrevious + 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
  //Valid Move 8
  nRowValid = (nRowPrevious + 2);
  nColumnValid = (nColumnPrevious - 1);
  sRowValid = IntToString(nRowValid);
  sColumnValid = IntToString(nColumnValid);
  sValidTag = "R"+sRowValid+"C"+sColumnValid+"";
  if (sValidTag == sSelf) nValidMove = TRUE;
 // End conditions for "Knight"
 }

// If move was INVALID, apply damage and vfx, knock pc back to previous square
 if (nValidMove == FALSE)
  {
  //FloatingTextStringOnCreature("INVALID move", oPC);
  DelayCommand(0.5, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eBeam, oPC, 3.5f));
  DelayCommand(0.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDamage, oPC));
  DelayCommand(0.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_HIT_ELECTRICAL), oPC));
  DelayCommand(0.5, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectVisualEffect(VFX_DUR_GLOW_RED), oPC, 5.5f));
  DelayCommand(0.5, AssignCommand(oPC, ActionJumpToLocation(GetLocation(GetObjectByTag(""+sPreviousSquare+"")))));
  DelayCommand(1.0, SetCutsceneMode(oPC, TRUE));
  DelayCommand(5.5, AssignCommand(oPC, ActionJumpToLocation(GetLocation(GetNearestObjectByTag("WP_Chess")))));
  DelayCommand(6.0, ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectVisualEffect(VFX_DUR_GLOW_WHITE), oPC, 2.0f));
  DelayCommand(6.5, SetLocalInt(oPC, "RowPC", nRowPrevious));
  DelayCommand(6.5, SetLocalInt(oPC, "ColumnPC", nColumnPrevious));
  DelayCommand(7.0, SetCutsceneMode(oPC, FALSE));
  }

// If move was VALID, assign present square as the new "previous" square
// and apply "safe move" VFX
 else
  {
  //FloatingTextStringOnCreature("Move was VALID", oPC);
  ApplyEffectToObject(DURATION_TYPE_TEMPORARY, EffectVisualEffect(VFX_DUR_GLOW_WHITE), oPC, 2.0f);
  SetLocalInt(oPC, "RowPC", nRowNow);
  SetLocalInt(oPC, "ColumnPC", nColumnNow);
  }
// End script
}
