unsigned opening_count = 0;
unsigned char longest_opening = 0;
struct opening *openings;
//unsigned char book_openings = 1;

int ai_opening(struct board *brd, struct move *move, unsigned id, signed char *dontcommit)
{
  unsigned n;
  char *movestr;
  struct opening *opening = 0;
#ifdef DEBUG
  unsigned char piece;
  struct coord *moves;
  unsigned char move_count;
  struct coord king_pos = find_king(brd, !(turn_count & 1));

#endif

  if(!setup.book_openings) return 0;

  move->resign = 0;

  if(id == -1)
  {
    if(turn_count > longest_opening)
      return 0;

    for(n=0;n<opening_count;n++)
    {
      if(!(openings[n].moves_left & 0x80) && (!opening || !(rand()&0x1F)))
      {
        opening = &openings[n];
      }
    }
  }
  else
  {
    opening = &openings[id];
  }

  if(opening)
  {
    movestr = opening->moves[turn_count-1];
    if(*movestr == '0')
    {
      move->src.col = 4;
      move->dst.row = move->src.row = ((turn_count & 1) ? 7 : 0);
      if(movestr[3] == '-')
        move->dst.col = 2;
      else
        move->dst.col = 6;
    }
    else
    {
      move->src.col = movestr[0] - 'a';
      move->src.row = 7 - (movestr[1] - '1');
      move->dst.col = movestr[3] - 'a';
      move->dst.row = 7 - (movestr[4] - '1');
    }
#ifdef DEBUG
    if(!(piece = piece_in_tile(brd, move->src.row, move->src.col)))
    {
      errorinopening:printf("Error in opening %s!\n", opening->name);
      exit(1);
    }
    if((!!(piece & PC_BLACK)) == (turn_count&1))
      goto errorinopening;

    moves = legal_moves(brd, move->src, &move_count, &king_pos);
    if(!move_in_list(moves, move_count, move->dst.row, move->dst.col))
      goto errorinopening;

#endif
    if(!dontcommit)
      move_piece(brd, move->src, move->dst);
    else
      dontcommit = 0;
    return 1;
  }

  return 0;
}

void prntjo(unsigned s, unsigned l)
{
  printf("Combining opening %s to %s to save memory.\n", openings[s].name, openings[l].name);
}

void join_openings(unsigned s, unsigned l)
{
  unsigned n;
  for(n=0;n<opening_count;n++)
  {
    if(openings[n].moves == openings[s].moves)
    {
      if(openings[n].move_count > openings[s].move_count)
        return;
    }
  }

  free(openings[s].moves);

  for(n=0;n<opening_count;n++)
  {
    if(n == l || n == s) continue;
    if(openings[n].moves == openings[s].moves)
    {
      openings[n].moves = openings[l].moves;
      prntjo(n, l);
    }
  }

  openings[s].moves = openings[l].moves;
  prntjo(s, l);
}

void free_duplicate_openings(void)
{
  unsigned n, i, l, m;

  for(n=0;n<opening_count;n++)
  {
    m = openings[n].move_count;
    for(i=0;i<opening_count;i++)
    {
      if(i == n) continue;
      if(openings[i].moves == openings[n].moves) continue;

      l = openings[i].move_count;
      if(l < m && !memcmp(openings[n].moves, openings[i].moves, l*sizeof(openings[0].moves[0])))
        join_openings(i, n);
      else if(l >= m && !memcmp(openings[n].moves, openings[i].moves, m*sizeof(openings[0].moves[0])))
        join_openings(n, i);
    }
  }
}

void play_opening(unsigned opening)
{
  struct board *brd = &openings[opening].pos;
  struct move move;
  unsigned moves = openings[opening].move_count;
#ifdef DEBUG
  unsigned n;
#endif

  init_board(brd);

  do
  {
    turn_count++;
    ai_opening(brd, &move, opening, 0);
  }
  while(turn_count < moves);

  turn_count = 0;

#ifdef DEBUG
  for(n=0;n<opening_count;n++)
  {
    if(n == opening) continue;
    if(!memcmp(&openings[n].pos, &openings[opening].pos, sizeof(struct board)))
      printf("Warning: Openings %s and %s lead to identical positions!\n", openings[n].name, openings[opening].name);
  }
#endif
}

int add_opening(struct opening *opening)
{
  void *ptr;

  if(!opening_count)
  {
    openings = malloc(sizeof(struct opening));
    if(!openings)
      return 1;
  }
  else
  {
    ptr = realloc(openings, (opening_count+1)*sizeof(struct opening));
    if(!ptr) return 1;
    openings = ptr;
  }
  openings[opening_count] = *opening;

  if(longest_opening < opening->move_count)
    longest_opening = opening->move_count;
  printf("Initialized opening: %s\n", opening->name);

  play_opening(opening_count);

  opening_count++;

  free_duplicate_openings();

  return 0;
}

int add_move_to_opening(struct opening *opening, char *move)
{
  void *ptr;
  unsigned l;
  char *ptr2;

  if(!opening->move_count)
  {
    opening->moves = malloc(sizeof(opening->moves[0]));
    if(!opening->moves) return 1;
  }
  else
  {
    ptr = realloc(opening->moves, (opening->move_count+1)*sizeof(opening->moves[0]));
    if(!ptr) return 1;
    opening->moves = ptr;
  }
  l = strlen(move);
  ptr2 = opening->moves[opening->move_count];
  memcpy(ptr2, move, l);
  memset(ptr2+l, 0, sizeof(opening->moves[0])-l);
  opening->move_count++;

  return 0;
}

int init_openings(void)
{
  struct opening opening = { 0 };
  char *ptr;
  char buf[64];
  FILE *fp = fopen("openings.dat", "rt");

  if(!fp)
  {
    puts("Could not find opening book.");
    return 1;
  }

  while(fgets(buf, sizeof(buf), fp))
  {
    ptr = strchr(buf, '\n');
    if(ptr) *ptr = 0;

    if(buf[0] == '>')
    {
      strcpy(opening.name, buf+1);
      if(add_opening(&opening))
        outofmemory();
      memset(&opening, 0, sizeof(opening));
    }
    else
    {
      if(add_move_to_opening(&opening, buf))
        outofmemory();
    }
  }

  fclose(fp);

  printf("Initialized %u openings.\n", opening_count);

  return 0;
}

void reset_openings(void)
{
  unsigned n;

  for(n=0;n<opening_count;n++)
    openings[n].moves_left = openings[n].move_count;
}

#define MAX_OPENING_TURNS 64

void check_opening(char *move)
{
  unsigned n;
  unsigned opening = -1;

  if(turn_count > MAX_OPENING_TURNS)
    for(n=0;n<opening_count;n++)
      if(!memcmp(&board, &openings[n].pos, sizeof(board)))
      {
        // the player found an alternate route to this opening...
        opening = n;
        break;
      }

  if(opening == -1 && turn_count > longest_opening)
    return;

  for(n=0;n<opening_count;n++)
  {
    if(turn_count <= openings[n].move_count
       && !strcmp(openings[n].moves[turn_count-1], move))
    {
      if(!--openings[n].moves_left)
      {
        show_dialog_nokey(openings[n].name);
        opening = -1;
        goto setff;
      }
    }
    else
      setff:openings[n].moves_left = -1;
  }

  if(opening != -1)
    show_dialog_nokey(openings[opening].name);
}
