
#include "RobboBaseLibUsage.h"
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h> /* dynamic load */
#include "GaviotaDebug.h" /* in too */

#define FALSE 0
#define TRUE 1
#define uint64 unsigned long long int
#define uint8 unsigned char

void TestRobboBaseLib ();

#define GAIN(x) dlerror(); /* clear */ \
  x = dlsym (so_file, #x); /* load */ \
  err = dlerror (); /* again */ \
  if (err) { printf ("%s\n", err); return -8; } 
int main ()
{
  void * so_file;
  char * err;
  GAVIOTA_DEBUG_SET_UP ();
  so_file = dlopen ("./RobboBaseLib.so", RTLD_LAZY); /* local */
  if (!so_file)
    {
      printf ("No RobboBaseLib.so detected\n");
      return -16;
    }

  GAIN (RobboTotalLibScore);
  GAIN (RobboTripleLibScore);

  GAIN (RegisterTotalBases);
  GAIN (DeRegisterTotalBases);
  GAIN (RegisterRobboTotalBasesInDirectory);
  GAIN (SetCacheTotalBase);
  GAIN (HAS_TotalBase);

  GAIN (SetCacheTripleBase);
  GAIN (SetLoadOnWeakProbeBackGround);
  GAIN (AllowBackGroundLoader);
  GAIN (PauseBackGroundLoader);

  GAIN (LoadTripleBases);
  GAIN (UnLoadTripleBases);
  GAIN (LoadRobboTripleBasesInDirectory);
  GAIN (HAS_TripleBase);
  
  GAIN (RobboLibBulkLoadFile);
  GAIN (RobboLibBulkDetachFile);
  GAIN (RobboLibBulkLoadDirectory);
  GAIN (RobboLibBulkDetachDirectory);

  GAIN (RobboBaseLibStartUp);
  GAIN (RobboBaseLibExit);

  TestRobboBaseLib ();

  printf ("TestRobboBaseLib complete\n");
  GAVIOTA_DEBUG_FINALE (); /* debug */
  return 0; /* comply */
}

void Tester (int);
void TestRobboBaseLib ()
{
  printf ("TestRobboBaseLib enact\n");
  (*RobboBaseLibStartUp) (FALSE);
  (*RegisterTotalBases) ("./RobboTotalBase");
  (*LoadTripleBases) ("./RobboTripleBase");  
  (*SetCacheTotalBase) (0x800); /* en courage, value */
  (*SetCacheTripleBase) (0x400);
  Tester (0x1 | 0xe | 0x00000000);
  //  Tester (0x1 | 0xe | 0x40000000);
  (*RobboBaseLibExit) ();
}

boolean REY[0100][0100];
void SetUpKings ()
{
  int a, b, a1, a2, b1, b2;
  for (a = 0; a < 0100; a++)
    for (b = 0; b < 0100; b++)
      {
	a1 = a & 07;
	a2 = a >> 3;
	b1 = b & 07;
	b2 = b >> 3;
	if (((a1 - b1) * (a1 - b1) <= 1) && ((a2 - b2) * (a2 - b2) <= 1))
	  REY[a][b] = FALSE;
	else
	  REY[a][b] = 1;
      }
}

void TesterBase (int, Type_PiSq*);
void TesterBaseEnPassAnt (int, Type_PiSq*);
#define SHUTTLE(X) /* */ /* now */

void Tester (int x)
{
  int i, E[4], O[12] = { 0, 1, 2, 4, 6, 7, 8, 9, 10, 12, 14, 15};
  Type_PiSq PiSq[1];
  SetUpKings (); /* demand */
  PiSq->ep = 0;
  PiSq->wtm = 0;
  for (E[3] = 0; E[3] < 12; E[3]++)
    for (E[2] = E[3]; E[2] < 12; E[2]++)
      for (E[1] = E[2]; E[1] < 12; E[1]++)
	for (E[0] = E[1]; E[0] < 12; E[0]++)
	  {
	    if (E[0] ==0 && E[1] == 0)
	      {	    E[0] = 3; E[1] = 8; E[2] = 8;  E[3] = 6; }
	    for (i = 0; i < 4; i++)
	      PiSq->pi[i] = O[E[i]];
	    PiSq->n = 0;
	    for (i = 0; i < 4; i++)
	      if (PiSq->pi[i])
		PiSq->n++;
	    SHUTTLE (PiSq);
	    if (HAS_TripleBase (PiSq))
	      {
		if (x & 0x40000000)
		  TesterBaseEnPassAnt (x, PiSq);
		else
		  TesterBase (x, PiSq);
	      }
	    return;
	    if (E[3] == 11)
	      return;
	  }
}

uint64 INCUR (Type_PiSq*, int);
#define KINGS_OK(x, y) REY[x][y]
static boolean VALID (Type_PiSq* PiSq)
{
  int i, j;
  for (i = 0; i < 4; i++)
    {
      if (!PiSq->pi[i])
	continue;
      if (PiSq->sq[i] == PiSq->wK || PiSq->sq[i] == PiSq->bK)
	return FALSE;
      for (j = 0; j < 4; j++)
	if (j != i && PiSq->pi[j] && PiSq->sq[i] == PiSq->sq[j])
	  return FALSE;
    }
  for (i = 0; i < 4; i++)
    {
      if (PiSq->pi[i] != 8) /* Blocked Pawn */
	continue;
      if ((PiSq->sq[i] + 8) == PiSq->wK || (PiSq->sq[i] + 8) == PiSq->bK)
	return FALSE;
      for (j = 0; j < 4; j++)
	if (j != i && PiSq->pi[j] && (PiSq->sq[i] + 8) == PiSq->sq[j])
	  return FALSE;
      for (j = 0; j < 4; j++)
	if (j != i && PiSq->pi[j] == 8 &&
	    (PiSq->sq[i] + 1) == PiSq->sq[j] && (PiSq->sq[i] & 07) != 07)
	  return FALSE;
    }  
  if (PiSq->ep & 077)
    {
      if (PiSq->wK == PiSq->ep || PiSq->bK == PiSq->ep)
	return FALSE;
      for (i = 0; i < 4; i++)
	if (PiSq->sq[i] == PiSq->ep)
	  return FALSE;
      if (PiSq->wtm)
	{
	  if ((PiSq->wK == PiSq->ep + 8) || (PiSq->bK == PiSq->ep + 8))
	    return FALSE;
	  for (i = 0; i < 4; i++)
	    if (PiSq->sq[i] == PiSq->ep + 8)
	      return FALSE;
	}
      else
	{
	  if ((PiSq->wK == PiSq->ep - 8) || (PiSq->bK == PiSq->ep - 8))
	    return FALSE;
	  for (i = 0; i < 4; i++)
	    if (PiSq->sq[i] == PiSq->ep - 8)
	      return FALSE;
	}
    }
  return TRUE;
}

static void OUTPUT_RESULTS (Type_PiSq* PiSq, int r, int s);

/* x & 1, all / parcel, x & 6, Triple / Total / compare, x & 8 OUTPUT */
void TesterBase (int x, Type_PiSq* PiSq)
{
  int i, sq[4], L[4], S[4], wK, bK;
  char C[64] = "0PN3B5RQZpn#b%rq";
  uint64 result;
  printf ("Testing this, %c%c%c%c Mode %d Count %d\n",
	  C[PiSq->pi[0]], C[PiSq->pi[1]], C[PiSq->pi[2]], C[PiSq->pi[3]],
	  x & 15, x >> 4);
  fflush (stdout); /* prefer */
#if 0
  PiSq->wK = 18;
  PiSq->bK = 56;
  PiSq->wtm = 0;
  PiSq->pi[0] = 9; PiSq->pi[1] = 6; PiSq->pi[2] = 1; PiSq->pi[3] = 1;
  PiSq->sq[0] = 35; PiSq->sq[1] = 49; PiSq->sq[2] = 19; PiSq->sq[3] = 42;
  PiSq->ep = 0;
  INCUR (PiSq, x & 0xe);
  return;
#endif
  for (i = 0; i < 4; i++)
    {
      S[i] = 0;
      L[i] = 0100;
      if (!PiSq->pi[i])
	L[i] = 1;
      if ((PiSq->pi[i] & 07) == 01) /* Pawn, the either */
	{
	  S[i] = 010;
	  L[i] = 070;
	}
      if (PiSq->pi[i] == 8) /* BlockedPawn */
	{
	  S[i] = 010;
	  L[i] = 060;
	}
    }
  if (x & 1) /* all */
    {
      for (wK = 0; wK < 0100; wK++)
	for (bK = 0; bK < 0100; bK++)
	  {
	    if (PiSq->pi[2] && bK == 0)
	      {
		printf ("%c%c\n", 'a' + (wK & 07), '1' + (wK >> 3));
		fflush(stdout);
	      }
	    if (!KINGS_OK (wK, bK))
	      continue;
	    PiSq->wK = wK;
	    PiSq->bK = bK;
	    for (sq[0] = S[0]; sq[0] < L[0]; sq[0]++)
	      for (sq[1] = S[1]; sq[1] < L[1]; sq[1]++)
		for (sq[2] = S[2]; sq[2] < L[2]; sq[2]++)
		  for (sq[3] = S[3]; sq[3] < L[3]; sq[3]++)
		    {
		      for (i = 0; i < 4; i++)
			PiSq->sq[i] = sq[i];
		      if (!VALID (PiSq))
			continue;
		      result ^= INCUR (PiSq, x & 0xe);
		    }		    
	  }
    }
  else /* parcel */
    {
      int u, a = x >> 4;
      uint64 s = 0x123456789fedcba0;
      uint64 m = 0x8765432109abcdef;
      uint64 p = 0x86420abcdef97531;
      for (u = 0; u < 4; u++)
	{
	  s ^= ((uint64) PiSq->pi[u]) << (5 * u + 3);
	  m ^= ((uint64) PiSq->pi[u]) << (9 * u + 2);
	  p ^= ((uint64) PiSq->pi[u]) << (13 * u + 1);
	  s ^= ((uint64) PiSq->pi[u]) << (60 - 7 * u);
	  m ^= ((uint64) PiSq->pi[u]) << (50 - 11 * u);
	  p ^= ((uint64) PiSq->pi[u]) << (40 - 13 * u);
	}
      for (u = 0; u < a; u++)
	{
	  s = s * m + p;
	  wK = (s >> 51) & 077;
	  bK = (s >> 42) & 077;
	  if (!KINGS_OK (wK, bK))
	    continue;
	  PiSq->wK = wK;
	  PiSq->bK = bK;
	  PiSq->wtm ^= 1;
	  for (i = 0; i < 4; i++)
	    if (PiSq->pi[i] != 0)
		PiSq->sq[i] = S[i] + (s >> (9 * i + 1)) % (L[i] - S[i]);
	  if (!VALID (PiSq))
	    continue;
	  result ^= INCUR (PiSq, x & 0xe);
	  if (u && !(u & 0xffff))
	       printf ("%x\n",u);
	}
      /* Verify (result); */
    }    
}

static uint64 HASH_RESULT (Type_PiSq* PiSq, int b)
{
  int i;
  uint64 result = 0;
  for (i = 0; i < 4; i++)
    result ^= (((uint64) (PiSq->sq[i])) << (3 * i + (b & 07)))
      ^ (((uint64) (PiSq->sq[i])) << (50 - 5 * i - (b & 037)));
  for (i = 0; i < 4; i++)
    result ^= (((uint64) (PiSq->pi[i])) << (5 * i + (b & 07)))
      ^ (((uint64) (PiSq->pi[i])) << (50 - 7 * i - (b & 037)));
  return result;
}

static void OUTPUT_RESULTS (Type_PiSq* PiSq, int r, int s)
{
  int i;
  char F[16][3] = { "00", "wP", "wN", "wK", "wB", "05", "wR", "wQ",
		    "Pp", "bP", "bN", "bK", "bB", "0d", "bR", "bQ" };
  printf ("wK %c%c ", 'a' + (PiSq->wK & 07), '1' + (PiSq->wK >> 3));
  printf ("bK %c%c | ", 'a' + (PiSq->bK & 07), '1' + (PiSq->bK >> 3));
  for (i = 0; i < 4; i++)
    if (PiSq->pi[i])
      printf ("%s %c%c ", F[PiSq->pi[i]],
	      'a' + (PiSq->sq[i] & 07), '1' + (PiSq->sq[i] >> 3));
  printf (" | wtm %d ep %d | results %d %d\n", PiSq->wtm, PiSq->ep, r, s);
  fflush (stdout); /* confer */
  if (r || s)
    exit (-1); /* HACK */
}

uint64 INCUR (Type_PiSq* PiSq, int x)
{
  uint8 TotalV;
  int TripleV;
  uint64 result = 0;
  int y = x & 6;
  if (y == 2) /* Total */
    {
      PiSq->wtm = TRUE;
      if (RobboTotalLibScore (PiSq, &TotalV, 1) && TotalV)
	{
	  result ^= HASH_RESULT (PiSq, TotalV);
	  if (x & 8)
	    OUTPUT_RESULTS (PiSq, TotalV, 0);
	}
      PiSq->wtm = FALSE;
      if (RobboTotalLibScore (PiSq, &TotalV, 1) && TotalV)
	{
	  result ^= HASH_RESULT (PiSq, TotalV);
	  if (x & 8)
	    OUTPUT_RESULTS (PiSq, TotalV, 0);
	}
    }
  if (y == 4) /* Triple */
    {
      PiSq->wtm = TRUE;
      if (RobboTripleLibScore (PiSq, &TripleV, 1))
	{	
	  result ^= HASH_RESULT (PiSq, TripleV);
	  if (x & 8)
	    OUTPUT_RESULTS (PiSq, TripleV, 0);
	}
      PiSq->wtm = FALSE;
      if (RobboTripleLibScore (PiSq, &TripleV, 1))
	{
	  result ^= HASH_RESULT (PiSq, TripleV);
	  if (x & 8)
	    OUTPUT_RESULTS (PiSq, TripleV, 0);
	}
    }
  if (y == 6) /* Total + Triple */
    {
      PiSq->wtm = TRUE;
      if (RobboTotalLibScore (PiSq, &TotalV, 1) && TotalV)
	{
	  if (!RobboTripleLibScore (PiSq, &TripleV, 1))
	    OUTPUT_RESULTS (PiSq, TotalV, -100);
	  if (TotalV == 1 && TripleV != 1)
	    OUTPUT_RESULTS (PiSq, TotalV, TripleV);
	  if (TotalV == 2 && TripleV != 0)
	    OUTPUT_RESULTS (PiSq, TotalV, TripleV);
	  if (TotalV >= 3 && TripleV != -1)
	    OUTPUT_RESULTS (PiSq, TotalV, TripleV);
	}
      PiSq->wtm = FALSE;
      if (RobboTotalLibScore (PiSq, &TotalV, 1) && TotalV)
	{
	  if (!RobboTripleLibScore (PiSq, &TripleV, 1))
	    OUTPUT_RESULTS (PiSq, TotalV, -100);
	  if (TotalV == 1 && TripleV != 1)
	    OUTPUT_RESULTS (PiSq, TotalV, TripleV);
	  if (TotalV == 2 && TripleV != 0)
	    OUTPUT_RESULTS (PiSq, TotalV, TripleV);
	  if (TotalV >= 3 && TripleV != -1)
	    OUTPUT_RESULTS (PiSq, TotalV, TripleV);
	}
    }
  return result;
}

static int BSF (uint64 w)
{
  uint64 x;
  asm ("bsfq %1,%0\n": "=&r" (x):"r" (w));
  return x;
}

void ValueEnPassAntTarGet (Type_PiSq* PiSq, int *ep1, int *ep2)
{
  uint8 T = 0, U = 0, V;
  int i;
  *ep1 = *ep2 = 0;
  if (PiSq->wtm)
    { 
      for (i = 0; i < 4; i++)
	{
	  uint8 t = PiSq->sq[i] >> 3;
	  uint8 c = PiSq->sq[i] & 07;
	  if (t == 4 && PiSq->pi[i] == 9)
	    T |= (1 << c);
	  if (t == 3 && PiSq->pi[i] == 8)
	    T |= (1 << c);
	  if (t == 5 || t == 6)
	    T &= ~(1ULL << c);
	  if (t == 4 && (PiSq->pi[i] == 1 || PiSq->pi[i] == 8))
	    {
	      if (c != 0)
		U |= (1 << (c - 1));
	      if (c != 07)
		U |= (1 << (c + 1));
	    }
	}	  
      V = T & U;
      if (V)
	*ep1 = BSF (V) + 0x28;
      V &= (V - 1);
      if (V)
	*ep2 = BSF (V) + 0x28;
      V &= (V - 1);
      if (V)
	OUTPUT_RESULTS (PiSq, -123, -321);
    }
  else
    { 
      for (i = 0; i < 4; i++)
	{
	  uint8 t = PiSq->sq[i] >> 3;
	  uint8 c = PiSq->sq[i] & 07;
	  if (t == 3 && (PiSq->pi[i] == 1 || PiSq->pi[i] == 8))
	    T |= (1 << c);
	  if (t == 1 || t == 2)
	    T &= ~(1ULL << c);
	  if ((t == 3 && PiSq->pi[i] == 9) || (t == 2 && PiSq->pi[i] == 8))
	    {
	      if (c != 0)
		U |= (1 << (c - 1));
	      if (c != 07)
		U |= (1 << (c + 1));
	    }
	}	  
      V = T & U;
      if (V)
	*ep1 = BSF (V) + 0x10;
      V &= (V - 1);
      if (V)
	*ep2 = BSF (V) + 0x10;
      V &= (V - 1);
      if (V)
	OUTPUT_RESULTS (PiSq, -123, -321);
    }
}

static void INCUR_EP (Type_PiSq*); /* simple, minus */
static void QualmBlack (Type_PiSq*, int, int);
static void QualmWhite (Type_PiSq*, int, int);

void TesterBaseEnPassAnt (int x, Type_PiSq* PiSq)
{
  int i, j;
#if 0
  PiSq->wK = 0;
  PiSq->bK = 2;
  PiSq->wtm = 1;
  PiSq->pi[0] = 8; PiSq->pi[1] = 8; PiSq->pi[2] = 1; PiSq->pi[3] = 1;
  PiSq->sq[0] = 25; PiSq->sq[1] = 34; PiSq->sq[2] = 8; PiSq->sq[3] = 32;
  PiSq->ep = 41;
  INCUR_EP (PiSq);
  return;
#endif

  for (i = 0; i < 4; i++)
    {
      if (PiSq->pi[i] == 1 || PiSq->pi[i] == 8)
	for (j = 0; j < 4; j++)
	  if (j != i && (PiSq->pi[j] == 9 || PiSq->pi[j] == 8))
	    if (!PiSq->pi[3] || PiSq->pi[i] != 8 || PiSq->pi[j] != 8)
	      if ((i == 0 || PiSq->pi[i] != PiSq->pi[i - 1]) &&
		  (j == 0 || PiSq->pi[j] != PiSq->pi[j - 1]))
		QualmWhite (PiSq, i, j);
    }
  for (i = 0; i < 4; i++)
    {
      if (PiSq->pi[i] == 9 || PiSq->pi[i] == 8)
	for (j = 0; j < 4; j++)
	  if (j != i && (PiSq->pi[j] == 1 || PiSq->pi[j] == 8))
	    if (!PiSq->pi[3] || PiSq->pi[i] != 8 || PiSq->pi[j] != 8)
	      if ((i == 0 || PiSq->pi[i] != PiSq->pi[i - 1]) &&
		  (j == 0 || PiSq->pi[j] != PiSq->pi[j - 1]))
		QualmBlack (PiSq, i, j);
    }
}

void QualmWhite (Type_PiSq* PiSq, int v1, int v2)
{
  int i, sq[4], L[4], S[4], wK, bK;
  char C[64] = "0PN3B5RQZpn#b%rq";
  printf ("Testing this, ep %c%c%c%c ",
	  C[PiSq->pi[0]], C[PiSq->pi[1]], C[PiSq->pi[2]], C[PiSq->pi[3]]);
  printf ("Qualm White %d %d\n", v1, v2);
  fflush (stdout); /* prefer */
  for (i = 0; i < 4; i++)
    {
      S[i] = 0;
      L[i] = 0100;
      if (!PiSq->pi[i])
	L[i] = 1;
      if ((PiSq->pi[i] & 07) == 01) /* Pawn, the either */
	{
	  S[i] = 010;
	  L[i] = 070;
	}
      if (PiSq->pi[i] == 8) /* BlockedPawn */
	{
	  S[i] = 010;
	  L[i] = 060;
	}
    }
  S[v1] = 040;
  L[v1] = 050;
  S[v2] = 0;
  L[v2] = 1;

  for (wK = 0; wK < 0100; wK++)
    for (bK = 0; bK < 0100; bK++)
      {
	if (PiSq->pi[2] && bK == 0)
	  {
	    printf ("%c%c ", 'a' + (wK & 07), '1' + (wK >> 3));
	    fflush(stdout);
	  }
	if (!KINGS_OK (wK, bK))
	  continue;
	PiSq->wK = wK;
	PiSq->bK = bK;
	for (sq[0] = S[0]; sq[0] < L[0]; sq[0]++)
	  for (sq[1] = S[1]; sq[1] < L[1]; sq[1]++)
	    for (sq[2] = S[2]; sq[2] < L[2]; sq[2]++)
	      for (sq[3] = S[3]; sq[3] < L[3]; sq[3]++)
		{
		  for (i = 0; i < 4; i++)
		    PiSq->sq[i] = sq[i];
		  if (PiSq->sq[v1] != 047)
		    {
		      PiSq->sq[v2] = PiSq->sq[v1] + 1;
		      PiSq->ep = PiSq->sq[v2] + 8;
		      PiSq->wtm = TRUE;
		      if (PiSq->pi[v2] == 8)
			PiSq->sq[v2] -= 8;
		      if (!VALID (PiSq))
			continue;
		      INCUR_EP (PiSq);
		    }
		  if (PiSq->sq[v1] != 040)
		    {
		      PiSq->sq[v2] = PiSq->sq[v1] - 1;
		      PiSq->ep = PiSq->sq[v2] + 8;
		      PiSq->wtm = TRUE;
		      if (PiSq->pi[v2] == 8)
			PiSq->sq[v2] -= 8;
		      PiSq->wtm = TRUE;
		      if (!VALID (PiSq))
			continue;
		      INCUR_EP (PiSq);
		    }
		}
      }
  printf ("\n");
}


void QualmBlack (Type_PiSq* PiSq, int v1, int v2)
{
  int i, sq[4], L[4], S[4], wK, bK;
  char C[64] = "0PN3B5RQZpn#b%rq";
  printf ("Testing this, ep %c%c%c%c ",
	  C[PiSq->pi[0]], C[PiSq->pi[1]], C[PiSq->pi[2]], C[PiSq->pi[3]]);
  printf ("Qualm Black %d %d\n", v1, v2);
  fflush (stdout); /* prefer */
  for (i = 0; i < 4; i++)
    {
      S[i] = 0;
      L[i] = 0100;
      if (!PiSq->pi[i])
	L[i] = 1;
      if ((PiSq->pi[i] & 07) == 01) /* Pawn, the either */
	{
	  S[i] = 010;
	  L[i] = 070;
	}
      if (PiSq->pi[i] == 8) /* BlockedPawn */
	{
	  S[i] = 010;
	  L[i] = 060;
	}
    }
  S[v2] = 030;
  L[v2] = 040;
  S[v1] = 0;
  L[v1] = 1;

  for (wK = 0; wK < 0100; wK++)
    for (bK = 0; bK < 0100; bK++)
      {
	if (PiSq->pi[2] && bK == 0)
	  {
	    printf ("%c%c ", 'a' + (wK & 07), '1' + (wK >> 3));
	    fflush(stdout);
	  }
	if (!KINGS_OK (wK, bK))
	  continue;
	PiSq->wK = wK;
	PiSq->bK = bK;
	for (sq[0] = S[0]; sq[0] < L[0]; sq[0]++)
	  for (sq[1] = S[1]; sq[1] < L[1]; sq[1]++)
	    for (sq[2] = S[2]; sq[2] < L[2]; sq[2]++)
	      for (sq[3] = S[3]; sq[3] < L[3]; sq[3]++)
		{
		  for (i = 0; i < 4; i++)
		    PiSq->sq[i] = sq[i];
		  if (PiSq->sq[v2] != 037)
		    {
		      PiSq->sq[v1] = PiSq->sq[v2] + 1;
		      PiSq->ep = PiSq->sq[v2] - 8;
		      PiSq->wtm = FALSE;
		      if (PiSq->pi[v1] == 8)
			PiSq->sq[v1] -= 8;
		      if (!VALID (PiSq))
			continue;
		      INCUR_EP (PiSq);
		    }
		  if (PiSq->sq[v2] != 030)
		    {
		      PiSq->sq[v1] = PiSq->sq[v2] - 1;
		      PiSq->ep = PiSq->sq[v2] - 8;
		      PiSq->wtm = FALSE;
		      if (PiSq->pi[v1] == 8)
			PiSq->sq[v1] -= 8;
		      if (!VALID (PiSq))
			continue;
		      INCUR_EP (PiSq);
		    }
		}
      }
  printf ("\n");
}

static void INCUR_EP (Type_PiSq* PiSq)
{
  uint8 TotalV;
  int TripleV;
  if (RobboTotalLibScore (PiSq, &TotalV, 1) && TotalV)
    {
      if (!RobboTripleLibScore (PiSq, &TripleV, 1))
	OUTPUT_RESULTS (PiSq, TotalV, -100);
      if (TotalV == 1 && TripleV != 1)
	OUTPUT_RESULTS (PiSq, TotalV, TripleV);
      if (TotalV == 2 && TripleV != 0)
	OUTPUT_RESULTS (PiSq, TotalV, TripleV);
      if (TotalV >= 3 && TripleV != -1)
	OUTPUT_RESULTS (PiSq, TotalV, TripleV);
    }
}
