public void generate() {
        final int maxHeight = HEIGHT / 2;
        final int stepHeight = maxHeight / 75 * blockSize;
        
        int maxBlockHeight = 0;
        blockHeights[0] = Util.rand(maxHeight);
        for (int i = 1; i < blocksCount; i++) {
            int value = blockHeights[i - 1] + Util.rand(-stepHeight, stepHeight);
            if (value > maxHeight) value = maxHeight;
            else if (value < 0) value = 0;
            blockHeights[i] = value;
            // Detect max block height
            if (maxBlockHeight < value) maxBlockHeight = value;
        }
        // Flatten out left and right blocks for turrets.
        for (int i = 0; i < PLAYERS_BLOCK_COUNT; i++) {
            blockHeights[i] = getFirstBlockHeight();
            blockHeights[blocksCount - i - 1] = getLastBlockHeight();
        }
        // Smooth blocks
        final int halfCount = PLAYERS_BLOCK_COUNT / 2;
        for (int start = 1; start <= SMOOTH_ITERATIONS; start++) {
            for (int i = start + halfCount; i < blocksCount - halfCount; i += 2) {
                blockHeights[i] = (blockHeights[i - 1] + blockHeights[i + 1]) / 2;
            }
        }
        
        gradientPaint = new LinearGradientPaint(
                0, HEIGHT - maxBlockHeight,
                0, HEIGHT,
                new float[] {0f, 1f}, TERRAIN_COLOR);
    }