int select_tile(struct coord *legal_moves, unsigned char legal_move_count, struct coord *selcoords, struct coord *selpiece, struct aimvstruct *aimvstate);

int king_in_adjacent_tile(struct board *brd, struct coord pc_coords, unsigned char side);

int check_stale(struct board *brd, struct coord king_pos, unsigned char depth, unsigned char *reps);

int edit_board()
{
  int s;
  struct coord seltile;
  int c;
  struct coord king_pos;
  unsigned char *ptr;
  struct coord coords;

  do
  {
    s = select_tile(0, 0, &seltile, 0, 0);
    if(s) break;

    while((c = show_dialog("Select: [P]awn, K[n]ight, [B]ishop, [R]ook, [Q]ueen, [K]ing, [E]npassable or [space] == empty"))
           == SCANCODE_UNKNOWN);

    if((piece_in_tile(&board, seltile.row, seltile.col) & 0x7) != PC_KING)
    {
      if(c == 'K')
      {
        if(!king_in_adjacent_tile(&board, seltile, 0))
        {
          king_pos = find_king(&board, 0);
          *ptr_to_tile(&board, king_pos.row, king_pos.col) = 0;
          *ptr_to_tile(&board, seltile.row, seltile.col) = PC_KING;
        }
      }
      else if(c == 'k')
      {
        if(!king_in_adjacent_tile(&board, seltile, 1))
        {
          king_pos = find_king(&board, 1);
          *ptr_to_tile(&board, king_pos.row, king_pos.col) = 0;
          *ptr_to_tile(&board, seltile.row, seltile.col) = PC_KING | PC_BLACK;
        }
      }
      else if(c == ' ')
      {
        *ptr_to_tile(&board, seltile.row, seltile.col) = 0;
      }
      else if((c & 0x1F) == ('P' & 0x1F))
      {
        if(seltile.row == 1 && (c & 0x20))
          *ptr_to_tile(&board, seltile.row, seltile.col) = PC_PAWN_UNMOVED | PC_BLACK;
        else if(seltile.row == 6 && !(c & 0x20))
          *ptr_to_tile(&board, seltile.row, seltile.col) = PC_PAWN_UNMOVED;
        else
          *ptr_to_tile(&board, seltile.row, seltile.col) = (c & 0x20) ? PC_PAWN_MOVED | PC_BLACK : PC_PAWN_MOVED;
      }
      else if((c & 0x1F) == ('N' & 0x1F))
      {
        *ptr_to_tile(&board, seltile.row, seltile.col) = (c & 0x20) ? PC_KNIGHT | PC_BLACK : PC_KNIGHT;
      }
      else if((c & 0x1F) == ('B' & 0x1F))
      {
        *ptr_to_tile(&board, seltile.row, seltile.col) = (c & 0x20) ? PC_BISHOP | PC_BLACK : PC_BISHOP;
      }
      else if((c & 0x1F) == ('R' & 0x1F))
      {
        *ptr_to_tile(&board, seltile.row, seltile.col) = (c & 0x20) ? PC_ROOK | PC_BLACK : PC_ROOK;
      }
      else if((c & 0x1F) == ('Q' & 0x1F))
      {
        *ptr_to_tile(&board, seltile.row, seltile.col) = (c & 0x20) ? PC_QUEEN | PC_BLACK : PC_QUEEN;
      }
      else if((c & 0x1F) == ('E' & 0x1F))
      {
        ptr = ptr_to_tile(&board, seltile.row, seltile.col);
        if((*ptr & 0x7) == PC_PAWN_MOVED)
          *ptr |= PC_PAWN_ENPASS;
        for(coords.row=0;coords.row<8;coords.row++)
          for(coords.col=0;coords.col<8;coords.col++)
            *ptr_to_tile(&board, coords.row, coords.col) &= ~PC_PAWN_ENPASS;
      }
    }
    if(game_status->turn != !(c & 0x20))
    {
      king_pos = find_king(&board, !(c & 0x20));
      if(king_threatened(&board, &king_pos, king_pos))
        *ptr_to_tile(&board, seltile.row, seltile.col) = 0;
    }
  }
  while(1);

  return 2;
}
