
#include "RobboLito.h"

static int HOP[8] = { 6, 10, 15, 17, -6, -10, -15, -17 };

void InitArrays ()
{
  int i, sq = 0, j, co, tr, king, dir;
  for (i = A1; i <= H8; i++)
    {
      SqSet[i] = 0;
      BitSet (i, SqSet[i]);
      SqClear[i] = ~SqSet[i];
    }
  for (i = A1; i <= H8; i++)
    {
      AttN[i] = 0;
      for (j = 0; j < 8; j++)
	{
	  sq = i + HOP[j];
	  if ((sq < A1) || (sq > H8))
	    continue;
	  if ((FileDistance (i, sq) > 2) || (RankDistance (i, sq) > 2))
	    continue;
	  BitSet (sq, AttN[i]);
	}
    }

  for (i = A1; i <= H8; i++)
    {
      AttK[i] = 0;
      for (j = A1; j <= H8; j++)
	{
	  if (MAX (FileDistance (i, j), RankDistance (i, j)) == 1)
	    BitSet (j, AttK[i]);
	}
    }

  for (i = A1; i <= H1; i++) /* correct for previous, yet read SqSet[-1] */
    {
      AttPw[i] = 0;
      AttPb[i] = SqSet[i + 7] | SqSet[i + 9];
    }
  for (i = B2; i <= H7; i++)
    AttPw[i] = SqSet[i - 7] | SqSet[i - 9];
  for (i = A2; i <= G7; i++)
    AttPb[i] = SqSet[i + 7] | SqSet[i + 9];
  for (i = A8; i <= H8; i++)
    {
      AttPb[i] = 0;
      AttPw[i] = SqSet[i - 7] | SqSet[i - 9];
    }
  for (i = A1; i <= A7; i += 8)
    AttPb[i] = SqSet[i + 9];
  for (i = A2; i <= A8; i += 8)
    AttPw[i] = SqSet[i - 7];
  for (i = H2; i <= H8; i += 8)
    AttPw[i] = SqSet[i - 9];
  for (i = H1; i <= H7; i += 8)
    AttPb[i] = SqSet[i + 7];
  AttPw[A1] = 0;
  AttPw[A2] = SqSet[B1];
  AttPb[A7] = SqSet[B8];
  AttPb[A8] = 0;
  AttPw[H1] = 0;
  AttPw[H2] = SqSet[G1];
  AttPb[H7] = SqSet[G8];
  AttPb[H8] = 0;

  for (sq = A1; sq <= H8; sq++)
    for (king = A1; king <= H8; king++)
      {
	Evade (king, sq) = AttK[king];
	if (RANK (king) == RANK (sq))
	  {
	    if (FILE (king) != FA)
	      Evade (king, sq) ^= SqSet[king - 1];
	    if (FILE (king) != FH)
	      Evade (king, sq) ^= SqSet[king + 1];
	  }
	if (FILE (king) == FILE (sq))
	  {
	    if (RANK (king) != R1)
	      Evade (king, sq) ^= SqSet[king - 8];
	    if (RANK (king) != R8)
	      Evade (king, sq) ^= SqSet[king + 8];
	  }
	if ((RANK (king) - RANK (sq)) == (FILE (king) - FILE (sq)))
	  {
	    if (RANK (king) != R8 && FILE (king) != FH)
	      Evade (king, sq) ^= SqSet[king + 9];
	    if (RANK (king) != R1 && FILE (king) != FA)
	      Evade (king, sq) ^= SqSet[king - 9];
	  }
	if ((RANK (king) - RANK (sq)) == (FILE (sq) - FILE (king)))
	  {
	    if (RANK (king) != R8 && FILE (king) != FA)
	      Evade (king, sq) ^= SqSet[king + 7];
	    if (RANK (king) != R1 && FILE (king) != FH)
	      Evade (king, sq) ^= SqSet[king - 7];
	  }
	if (AttK[king] & SqSet[sq])
	  Evade (king, sq) |= SqSet[sq];
      }

  for (sq = A1; sq <= H8; sq++)
    for (king = A1; king <= H8; king++)
      {
	InterPose (king, sq) = SqSet[sq];
	dir = 0;
	if (RANK (king) == RANK (sq))
	  {
	    if (king > sq)
	      dir = 1;
	    else
	      dir = -1;
	  }
	if (FILE (king) == FILE (sq))
	  {
	    if (king > sq)
	      dir = 8;
	    else
	      dir = -8;
	  }
	if ((RANK (king) - RANK (sq)) == (FILE (king) - FILE (sq)))
	  {
	    if (king > sq)
	      dir = 9;
	    else
	      dir = -9;
	  }
	if ((RANK (king) - RANK (sq)) == (FILE (sq) - FILE (king)))
	  {
	    if (king > sq)
	      dir = 7;
	    else
	      dir = -7;
	  }
	if (dir)
	  for (i = sq; i != king; i += dir)
	    BitSet (i, InterPose (king, sq));
      }

  for (sq = A1; sq <= H8; sq++)
    {
      ORTHO[sq] = RankArray[RANK (sq)] | FileArray[FILE (sq)];
      DIAG[sq] = 0;
      for (co = FILE (sq), tr = RANK (sq); co <= FH && tr <= R8; co++, tr++)
	BitSet (8 * tr + co, DIAG[sq]);
      for (co = FILE (sq), tr = RANK (sq); co <= FH && tr >= R1; co++, tr--)
	BitSet (8 * tr + co, DIAG[sq]);
      for (co = FILE (sq), tr = RANK (sq); co >= FA && tr <= R8; co--, tr++)
	BitSet (8 * tr + co, DIAG[sq]);
      for (co = FILE (sq), tr = RANK (sq); co >= FA && tr >= R1; co--, tr--)
	BitSet (8 * tr + co, DIAG[sq]);
      ORTHO[sq] &= SqClear[sq];
      DIAG[sq] &= SqClear[sq];
    }
  Magic_mult_init ();
}
