File: annotate.c

package info (click to toggle)
crafty 23.4-6
  • links: PTS
  • area: non-free
  • in suites: jessie-kfreebsd, wheezy
  • size: 3,276 kB
  • sloc: ansic: 30,650; cpp: 5,829; makefile: 604; sh: 178; perl: 30
file content (797 lines) | stat: -rw-r--r-- 32,775 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#include "chess.h"
#include "data.h"
/* last modified 01/18/09 */
/*
 *******************************************************************************
 *                                                                             *
 *  "annotate" command is used to search through the game in a pgn file, and   *
 *  provide a qualitative analysis of each move played and then creating a new *
 *  output file (xxx.can) containing the original game + new commentary.       *
 *                                                                             *
 *  The normal output of this command is a file, in PGN format, that contains  *
 *  the moves of the game, along with analysis when Crafty does not think that *
 *  move was the best choice.  The definition of "best choice" is somewhat     *
 *  vague, because if the move played is "close" to the best move available,   *
 *  Crafty will not comment on the move.  "Close" is defined by the <margin>   *
 *  option explained below.  This basic type of annotation works by first      *
 *  using the normal tree search algorithm to find the best move.  If this     *
 *  move was the move played, no output is produced.  If a different move is   *
 *  considered best, then the actual move played is searched to the same depth *
 *  and if the best move and actual move scores are within <margin> of each    *
 *  other, no comment is produced, otherwise Crafty inserts the evaluation for *
 *  the move played, followed by the eval and PV for the best continuation it  *
 *  found.  You can enter suggested moves for Crafty to analyze at any point   *
 *  by simply entering a move as an analysis-type comment using (move) or      *
 *  {move}.  Crafty will search that move in addition to the move actually     *
 *  played and the move it thinks is best.                                     *
 *                                                                             *
 *  The format of the command is as follows:                                   *
 *                                                                             *
 *      annotate filename b|w|bw|name moves margin time [n]                    *
 *                                                                             *
 *  Filename is the input file where Crafty will obtain the moves to annotate, *
 *  and output will be written to file "filename.can".                         *
 *                                                                             *
 *      annotateh filename b|w|bw|name moves margin time [n]                   *
 *                                                                             *
 *  Can be used to produce an HTML-compatible file that includes bitmapped     *
 *  diagrams of the positions where Crafty provides analysis.  This file can be*
 *  opened by a browser to provide much easier 'reading'.                      *
 *                                                                             *
 *      annotatet filename b|w|bw|name moves margin time [n]                   *
 *                                                                             *
 *  Can be used to produce a LaTeX-compatible file that includes LaTeX chess   *
 *  fonts.  This file can be read/printed by any program that can handle LaTeX *
 *  input.                                                                     *
 *                                                                             *
 *  Where b/w/bw indicates whether to annotate only the white side (w), the    *
 *  black side (b) or both (bw).  You can also specify a name (or part of a    *
 *  name, just be sure it is unique in the name tags for clarity in who you    *
 *  mean).                                                                     *
 *                                                                             *
 *  Moves indicates the move or moves to annotate.  It can be a single move,   *
 *  which indicates the starting move number to annotate, or it can be a range,*
 *  which indicates a range of move (1-999 gets the whole game.)               *
 *                                                                             *
 *  Margin is the difference between Crafty's evaluation for the move actually *
 *  played and for the move Crafty thinks is best, before Crafty will generate *
 *  a comment in the annotation file.  1.0 is a pawn, and will only generate   *
 *  comments if the move played is 1.000 (1 pawn) worse than the best move     *
 *  found by doing a complete search.                                          *
 *                                                                             *
 *  Time is time per move to search, in seconds.                               *
 *                                                                             *
 *  [n] is optional and tells Crafty to produce the PV/score for the "n" best  *
 *  moves.  Crafty stops when the best move reaches the move played in the game*
 *  or after displaying n moves, whichever comes first.  If you use -n, then it*
 *  will display n moves regardless of where the game move ranks.              *
 *                                                                             *
 *******************************************************************************
 */
#define MIN_DECISIVE_ADV 150
#define MIN_MODERATE_ADV  70
#define MIN_SLIGHT_ADV    30
void Annotate() {
  FILE *annotate_in, *annotate_out;
  char text[128], tbuffer[4096], colors[32] = { "" }, pname[128] = {
  ""};
  int annotate_margin, annotate_score[100], player_score, best_moves,
      annotate_wtm;
  int annotate_search_time_limit, search_player;
  int twtm, path_len, analysis_printed = 0;
  int wtm, move_num, line1, line2, move, suggested, i;
  int searches_done, read_status;
  PATH temp[100], player_pv;
  int temp_search_depth;
  TREE *const tree = block[0];
  char html_br[5] = { "" };
  int save_swindle_mode;
  int html_mode = 0;
  int latex = 0;

/*
 ************************************************************
 *                                                          *
 *   First, extract the options from the command line to    *
 *   determine what the user wanted us to do.               *
 *                                                          *
 ************************************************************
 */
  save_swindle_mode = swindle_mode;
  if (!strcmp(args[0], "annotateh")) {
    html_mode = 1;
    strcpy(html_br, "<br>");
  }
  if (!strcmp(args[0], "annotatet")) {
    latex = 1;
    strcpy(html_br, "\\\\");
  }
  strcpy(tbuffer, buffer);
  nargs = ReadParse(tbuffer, args, " 	;");
  if (nargs < 6) {
    printf
        ("usage: annotate <file> <color> <moves> <margin> <time> [nmoves]\n");
    return;
  }
  annotate_in = fopen(args[1], "r");
  if (annotate_in == NULL) {
    Print(4095, "unable to open %s for input\n", args[1]);
    return;
  }
  nargs = ReadParse(tbuffer, args, " 	;");
  strcpy(text, args[1]);
  if (html_mode == 1)
    strcpy(text + strlen(text), ".html");
  else if (latex == 1)
    strcpy(text + strlen(text), ".tex");
  else
    strcpy(text + strlen(text), ".can");
  annotate_out = fopen(text, "w");
  if (annotate_out == NULL) {
    Print(4095, "unable to open %s for output\n", text);
    return;
  }
  if (html_mode == 1)
    AnnotateHeaderHTML(text, annotate_out);
  if (latex == 1)
    AnnotateHeaderTeX(text, annotate_out);
  if (strlen(args[2]) <= 2)
    strcpy(colors, args[2]);
  else
    strcpy(pname, args[2]);
  line1 = 1;
  line2 = 999;
  if (strchr(args[3], 'b'))
    line2 = -1;
  if (strchr(args[3], '-'))
    sscanf(args[3], "%d-%d", &line1, &line2);
  else {
    sscanf(args[3], "%d", &line1);
    line2 = 999;
  }
  annotate_margin = atof(args[4]) * PieceValues(white, pawn);
  annotate_search_time_limit = atof(args[5]) * 100;
  if (nargs > 6)
    best_moves = atoi(args[6]);
  else
    best_moves = 1;
/*
 ************************************************************
 *                                                          *
 *   Reset the game to "square 0" to start the annotation   *
 *   procedure.  Then we read moves from the input file,    *
 *   make them on the game board, and annotate if the move  *
 *   is for the correct side.  If we haven't yet reached    *
 *   the starting move to annotate, we skip the Search()    *
 *   stuff and read another move.                           *
 *                                                          *
 ************************************************************
 */
  annotate_mode = 1;
  swindle_mode = 0;
  ponder = 0;
  temp_search_depth = search_depth;
  read_status = ReadPGN(0, 0);
  read_status = ReadPGN(annotate_in, 0);
  player_pv.path[1] = 0;
  while (read_status != -1) {
    ponder_move = 0;
    last_pv.pathd = 0;
    last_pv.pathl = 0;
    player_pv.pathd = 0;
    player_pv.pathl = 0;
    tree->pv[0].pathl = 0;
    tree->pv[0].pathd = 0;
    analysis_printed = 0;
    InitializeChessBoard(tree);
    tree->position[1] = tree->position[0];
    wtm = 1;
    move_number = 1;
/*
 ************************************************************
 *                                                          *
 *   Now grab the PGN tag values so they can be copied to   *
 *   the .can file for reference.                           *
 *                                                          *
 ************************************************************
 */
    do
      read_status = ReadPGN(annotate_in, 0);
    while (read_status == 1);
    if (read_status == -1)
      break;
    if (latex == 0) {
      fprintf(annotate_out, "[Event \"%s\"]%s\n", pgn_event, html_br);
      fprintf(annotate_out, "[Site \"%s\"]%s\n", pgn_site, html_br);
      fprintf(annotate_out, "[Date \"%s\"]%s\n", pgn_date, html_br);
      fprintf(annotate_out, "[Round \"%s\"]%s\n", pgn_round, html_br);
      fprintf(annotate_out, "[White \"%s\"]%s\n", pgn_white, html_br);
      fprintf(annotate_out, "[WhiteElo \"%s\"]%s\n", pgn_white_elo, html_br);
      fprintf(annotate_out, "[Black \"%s\"]%s\n", pgn_black, html_br);
      fprintf(annotate_out, "[BlackElo \"%s\"]%s\n", pgn_black_elo, html_br);
      fprintf(annotate_out, "[Result \"%s\"]%s\n", pgn_result, html_br);
      fprintf(annotate_out, "[Annotator \"Crafty v%s\"]%s\n", version,
          html_br);
      if (strlen(colors) != 0) {
        if (!strcmp(colors, "bw") || !strcmp(colors, "wb"))
          fprintf(annotate_out,
              "{annotating both black and white moves.}%s\n", html_br);
        else if (strchr(colors, 'b'))
          fprintf(annotate_out, "{annotating only black moves.}%s\n",
              html_br);
        else if (strchr(colors, 'w'))
          fprintf(annotate_out, "{annotating only white moves.}%s\n",
              html_br);
      } else
        fprintf(annotate_out, "{annotating for player %s}%s\n", pname,
            html_br);
      fprintf(annotate_out, "{using a scoring margin of %s pawns.}%s\n",
          DisplayEvaluationKibitz(annotate_margin, wtm), html_br);
      fprintf(annotate_out, "{search time limit is %s}%s\n%s\n",
          DisplayTimeKibitz(annotate_search_time_limit), html_br, html_br);
    } else {
      fprintf(annotate_out, "\\textbf{\\sc %s %s -- %s %s}%s\n", pgn_white,
          pgn_white_elo, pgn_black, pgn_black_elo, html_br);
      fprintf(annotate_out, "{\\em %s, %s}%s\n", pgn_site, pgn_date, html_br);
      fprintf(annotate_out, "{\\small %s, Round: %s}%s\n", pgn_event,
          pgn_round, html_br);
      fprintf(annotate_out, "\\begin{mainline}{%s}{Crafty v%s}\n", pgn_result,
          version);
    }
    if (strlen(colors)) {
      if (!strcmp(colors, "w"))
        annotate_wtm = 1;
      else if (!strcmp(colors, "b"))
        annotate_wtm = 0;
      else if (!strcmp(colors, "wb"))
        annotate_wtm = 2;
      else if (!strcmp(colors, "bw"))
        annotate_wtm = 2;
      else {
        Print(4095, "invalid color specification, retry\n");
        fclose(annotate_out);
        return;
      }
    } else {
      if (strstr(pgn_white, pname))
        annotate_wtm = 1;
      else if (strstr(pgn_black, pname))
        annotate_wtm = 0;
      else {
        Print(4095, "Player name doesn't match any PGN name tag, retry\n");
        fclose(annotate_out);
        return;
      }
    }
    do {
      fflush(annotate_out);
      move = ReadNextMove(tree, buffer, 0, wtm);
      if (move <= 0)
        break;
      strcpy(text, OutputMove(tree, move, 0, wtm));
      if (history_file) {
        fseek(history_file, ((move_number - 1) * 2 + 1 - wtm) * 10, SEEK_SET);
        fprintf(history_file, "%9s\n", text);
      }
      if (wtm)
        Print(4095, "White(%d): %s\n", move_number, text);
      else
        Print(4095, "Black(%d): %s\n", move_number, text);
      if (analysis_printed)
        fprintf(annotate_out, "%3d.%s%8s\n", move_number,
            (wtm ? "" : "     ..."), text);
      else {
        if (wtm)
          fprintf(annotate_out, "%3d.%8s", move_number, text);
        else
          fprintf(annotate_out, "%8s\n", text);
      }
      analysis_printed = 0;
      if (move_number >= line1 && move_number <= line2) {
        if (annotate_wtm == 2 || annotate_wtm == wtm) {
          last_pv.pathd = 0;
          last_pv.pathl = 0;
          thinking = 1;
          RootMoveList(wtm);
/*
 ************************************************************
 *                                                          *
 *   Search the position to see if the move played is the   *
 *   best move possible.  If not, then search just the move *
 *   played to get a score for it as well, so we can        *
 *   determine if annotated output is appropriate.          *
 *                                                          *
 ************************************************************
 */
          search_time_limit = annotate_search_time_limit;
          search_depth = temp_search_depth;
          player_score = -999999;
          search_player = 1;
          for (searches_done = 0; searches_done < Abs(best_moves);
              searches_done++) {
            if (searches_done > 0) {
              search_time_limit = 3 * annotate_search_time_limit;
              search_depth = temp[0].pathd;
            }
            Print(4095, "\n              Searching all legal moves.");
            Print(4095, "----------------------------------\n");
            tree->position[1] = tree->position[0];
            InitializeHashTables();
            annotate_score[searches_done] = Iterate(wtm, annotate, 1);
            if (tree->pv[0].path[1] == move) {
              player_score = annotate_score[searches_done];
              player_pv = tree->pv[0];
              search_player = 0;
            }
            temp[searches_done] = tree->pv[0];
            for (i = 0; i < n_root_moves; i++) {
              if (root_moves[i].move == tree->pv[0].path[1]) {
                for (; i < n_root_moves; i++)
                  root_moves[i] = root_moves[i + 1];
                n_root_moves--;
                break;
              }
            }
            if (n_root_moves == 0 || (annotate_margin >= 0 &&
                    player_score + annotate_margin >
                    annotate_score[searches_done]
                    && best_moves > 0)) {
              if (n_root_moves == 0)
                searches_done++;
              break;
            }
          }
          if (search_player) {
            Print(4095,
                "\n              Searching only the move played in game.");
            Print(4095, "--------------------\n");
            tree->position[1] = tree->position[0];
            search_move = move;
            root_moves[0].move = move;
            root_moves[0].nodes = 0;
            root_moves[0].status = 0;
            n_root_moves = 1;
            search_time_limit = 3 * annotate_search_time_limit;
            search_depth = temp[0].pathd;
            if (search_depth == temp_search_depth)
              search_time_limit = annotate_search_time_limit;
            InitializeHashTables();
            player_score = Iterate(wtm, annotate, 1);
            player_pv = tree->pv[0];
            search_depth = temp_search_depth;
            search_time_limit = annotate_search_time_limit;
            search_move = 0;
          }
/*
 ************************************************************
 *                                                          *
 *   Output the score/pv for the move played unless it      *
 *   matches what Crafty would have played.  If it doesn't  *
 *   then output the pv for what Crafty thinks is best.     *
 *                                                          *
 ************************************************************
 */
          thinking = 0;
          if (player_pv.pathd > 1 && player_pv.pathl >= 1 &&
              player_score + annotate_margin < annotate_score[0] &&
              (temp[0].path[1] != player_pv.path[1] || annotate_margin < 0.0
                  || best_moves != 1)) {
            if (wtm) {
              analysis_printed = 1;
              fprintf(annotate_out, "%s\n", html_br);
            }
            if (html_mode == 1)
              AnnotatePositionHTML(tree, wtm, annotate_out);
            if (latex == 1) {
              AnnotatePositionTeX(tree, wtm, annotate_out);
              fprintf(annotate_out, "   \\begin{variation}\\{%d:%s\\}",
                  player_pv.pathd, DisplayEvaluationKibitz(player_score,
                      wtm));
            } else
              fprintf(annotate_out, "                ({%d:%s}",
                  player_pv.pathd, DisplayEvaluationKibitz(player_score,
                      wtm));
            path_len = player_pv.pathl;
            fprintf(annotate_out, " %s", FormatPV(tree, wtm, player_pv));
            if (latex == 1)
              fprintf(annotate_out, " %s\n   \\end{variation}\n",
                  AnnotateVtoNAG(player_score, wtm, html_mode, latex));
            else
              fprintf(annotate_out, " %s)%s\n", AnnotateVtoNAG(player_score,
                      wtm, html_mode, latex), html_br);
            for (move_num = 0; move_num < searches_done; move_num++) {
              if (move != temp[move_num].path[1]) {
                if (latex == 1)
                  fprintf(annotate_out, "   \\begin{variation}\\{%d:%s\\}",
                      temp[move_num].pathd,
                      DisplayEvaluationKibitz(annotate_score[move_num], wtm));
                else
                  fprintf(annotate_out, "                ({%d:%s}",
                      temp[move_num].pathd,
                      DisplayEvaluationKibitz(annotate_score[move_num], wtm));
                path_len = temp[move_num].pathl;
                fprintf(annotate_out, " %s", FormatPV(tree, wtm,
                        temp[move_num]));
                if (latex == 1)
                  fprintf(annotate_out, " %s\n   \\end{variation}\n",
                      AnnotateVtoNAG(annotate_score[move_num], wtm, html_mode,
                          latex));
                else
                  fprintf(annotate_out, " %s)%s\n",
                      AnnotateVtoNAG(annotate_score[move_num], wtm, html_mode,
                          latex), html_br);
              }
            }
            if (html_mode == 1)
              fprintf(annotate_out, "<br>\n");
            if (line2 < 0)
              line2--;
          }
        }
      }
/*
 ************************************************************
 *                                                          *
 *   Before going on to the next move, see if the user has  *
 *   included a set of other moves that require a search.   *
 *   If so, search them one at a time and produce the ana-  *
 *   lysis for each one.                                    *
 *                                                          *
 ************************************************************
 */
      read_status = ReadPGN(annotate_in, 1);
      while (read_status == 2) {
        suggested = InputMove(tree, buffer, 0, wtm, 1, 0);
        if (suggested > 0) {
          thinking = 1;
          Print(4095, "\n              Searching only the move suggested.");
          Print(4095, "--------------------\n");
          tree->position[1] = tree->position[0];
          search_move = suggested;
          search_time_limit = 3 * annotate_search_time_limit;
          search_depth = temp[0].pathd;
          InitializeHashTables();
          annotate_score[0] = Iterate(wtm, annotate, 0);
          search_depth = temp_search_depth;
          search_time_limit = annotate_search_time_limit;
          search_move = 0;
          thinking = 0;
          twtm = wtm;
          path_len = tree->pv[0].pathl;
          if (tree->pv[0].pathd > 1 && path_len >= 1) {
            if (wtm && !analysis_printed) {
              analysis_printed = 1;
              fprintf(annotate_out, "%s\n", html_br);
            }
            fprintf(annotate_out, "                ({suggested %d:%s}",
                tree->pv[0].pathd, DisplayEvaluationKibitz(annotate_score[0],
                    wtm));
            for (i = 1; i <= path_len; i++) {
              fprintf(annotate_out, " %s", OutputMove(tree,
                      tree->pv[0].path[i], i, twtm));
              MakeMove(tree, i, tree->pv[0].path[i], twtm);
              twtm = Flip(twtm);
            }
            for (i = path_len; i > 0; i--) {
              twtm = Flip(twtm);
              UnmakeMove(tree, i, tree->pv[0].path[i], twtm);
            }
            fprintf(annotate_out, " %s)%s\n",
                AnnotateVtoNAG(annotate_score[0], wtm, html_mode, latex),
                html_br);
          }
        }
        read_status = ReadPGN(annotate_in, 1);
        if (read_status != 2)
          break;
      }
      if ((analysis_printed) && (latex == 0))
        fprintf(annotate_out, "%s\n", html_br);
      MakeMoveRoot(tree, move, wtm);
      wtm = Flip(wtm);
      if (wtm)
        move_number++;
      if (read_status != 0)
        break;
      if (line2 < -1)
        break;
    } while (1);
    fprintf(annotate_out, "\n       %s %s\n", pgn_result, html_br);
    if (html_mode == 1) {
      fprintf(annotate_out, "%s\n", html_br);
      AnnotateFooterHTML(annotate_out);
    }
    if (latex == 1) {
      AnnotatePositionTeX(tree, wtm, annotate_out);
      fprintf(annotate_out, "\\end{mainline}\n");
      if (strlen(colors) != 0) {
        fprintf(annotate_out, "\\begin{flushright}{\\small ");
        if (!strcmp(colors, "bw") || !strcmp(colors, "wb"))
          fprintf(annotate_out, "annotating both black and white moves.%s\n",
              html_br);
        else if (strchr(colors, 'b'))
          fprintf(annotate_out, "annotating only black moves.%s\n", html_br);
        else if (strchr(colors, 'w'))
          fprintf(annotate_out, "annotating only white moves.%s\n", html_br);
      } else
        fprintf(annotate_out, "annotating for player %s%s\n", pname, html_br);
      fprintf(annotate_out, "using a scoring margin of %s pawns.%s\n",
          DisplayEvaluationKibitz(annotate_margin, wtm), html_br);
      fprintf(annotate_out, "search time limit is %s%s\n",
          DisplayTimeKibitz(annotate_search_time_limit), html_br);
      fprintf(annotate_out, " } \\end{flushright}");
      AnnotateFooterTeX(annotate_out);
    }
  }
  if (annotate_out)
    fclose(annotate_out);
  if (annotate_in)
    fclose(annotate_in);
  search_time_limit = 0;
  annotate_mode = 0;
  swindle_mode = save_swindle_mode;
}

/*
 *******************************************************************************
 *                                                                             *
 *   These functions provide HTML output support interfaces.                   *
 *                                                                             *
 *******************************************************************************
 */
void AnnotateHeaderHTML(char *title_text, FILE * annotate_out) {
  fprintf(annotate_out,
      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
  fprintf(annotate_out,
      "          \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
  fprintf(annotate_out, "<HTML>\n");
  fprintf(annotate_out, "<HEAD><TITLE>%s</TITLE>\n", title_text);
  fprintf(annotate_out,
      "<LINK rev=\"made\" href=\"hyatt@cis.uab.edu\"></HEAD>\n");
  fprintf(annotate_out,
      "<BODY BGColor=\"#ffffff\" text=\"#000000\" link=\"#0000ee\""
      " vlink=\"#551a8b\">\n");
}

void AnnotateFooterHTML(FILE * annotate_out) {
  fprintf(annotate_out, "</BODY>\n");
  fprintf(annotate_out, "</HTML>\n");
}
void AnnotatePositionHTML(TREE * RESTRICT tree, int wtm, FILE * annotate_out) {
  char filename[32], html_piece;
  char alt[32];
  int rank, file;

/*  Display the board in HTML using table of images.          */
  fprintf(annotate_out, "<br>\n");
  fprintf(annotate_out, "<TABLE Border=1 CellSpacing=0 CellPadding=0>\n\n");
  for (rank = RANK8; rank >= RANK1; rank--) {
    fprintf(annotate_out, "<TR>\n");
    for (file = FILEA; file <= FILEH; file++) {
      (void) strcpy(filename, "/usr/share/crafty-bitmaps/");
      if ((rank + file) % 2)
        (void) strcat(filename, "w");
      else
        (void) strcat(filename, "b");
      html_piece = translate[PcOnSq((rank << 3) + file) + 6];
      switch (html_piece) {
        case 'p':
          strcat(filename, "bp");
          strcpy(alt, "*P");
          break;
        case 'r':
          strcat(filename, "br");
          strcpy(alt, "*R");
          break;
        case 'n':
          strcat(filename, "bn");
          strcpy(alt, "*N");
          break;
        case 'b':
          strcat(filename, "bb");
          strcpy(alt, "*B");
          break;
        case 'q':
          strcat(filename, "bq");
          strcpy(alt, "*Q");
          break;
        case 'k':
          strcat(filename, "bk");
          strcpy(alt, "*K");
          break;
        case 'P':
          strcat(filename, "wp");
          strcpy(alt, "P");
          break;
        case 'R':
          strcat(filename, "wr");
          strcpy(alt, "R");
          break;
        case 'N':
          strcat(filename, "wn");
          strcpy(alt, "N");
          break;
        case 'B':
          strcat(filename, "wb");
          strcpy(alt, "B");
          break;
        case 'Q':
          strcat(filename, "wq");
          strcpy(alt, "Q");
          break;
        case 'K':
          strcat(filename, "wk");
          strcpy(alt, "K");
          break;
        default:
          strcat(filename, "sq");
          strcpy(alt, " ");
          break;
      }
      strcat(filename, ".png");
      fprintf(annotate_out, "<TD><IMG ALT=\"%s\" SRC=\"%s\"></TD>\n", alt,
          filename);
    }
    fprintf(annotate_out, "</TR>\n\n");
  }
  fprintf(annotate_out, "</TABLE>\n");
  if (wtm)
    fprintf(annotate_out, "<H2>White to move.</H2>\n");
  else
    fprintf(annotate_out, "<H2>Black to move.</H2>\n");
  fprintf(annotate_out, "<BR>\n");
}

/*
 *******************************************************************************
 * Author         : Alexander Wagner                                           *
 *                                      University of Michigan                 *
 * Date           : 03.01.04                                                   *
 *                                                                             *
 * Last Modified  : 03.01.04                                                   *
 *                                                                             *
 * Based upon the HTML-Code above                                              *
 *                                                                             *
 * These functions provide LaTeX output capability to Crafty.                  *
 *                                                                             *
 *******************************************************************************
 */
void AnnotateHeaderTeX(char *title_text, FILE * annotate_out) {
  fprintf(annotate_out, "\\documentclass[12pt,twocolumn]{article}\n");
  fprintf(annotate_out, "%% This is a LaTeX file generated by Crafty \n");
  fprintf(annotate_out,
      "%% You must have the \"chess12\" package to typeset this file.\n");
  fprintf(annotate_out, "\n");
  fprintf(annotate_out, "\\usepackage{times}\n");
  fprintf(annotate_out, "\\usepackage{a4wide}\n");
  fprintf(annotate_out, "\\usepackage{chess}\n");
  fprintf(annotate_out, "\\usepackage{bdfchess}\n");
  fprintf(annotate_out, "\\usepackage[T1]{fontenc}\n");
  fprintf(annotate_out, "\n");
  fprintf(annotate_out, "\\setlength{\\columnsep}{7mm}\n");
  fprintf(annotate_out, "\\setlength{\\parindent}{0pt}\n");
  fprintf(annotate_out, "\n");
  fprintf(annotate_out, "%% Macros for variations and diagrams:\n");
  fprintf(annotate_out,
      "\\newenvironment{mainline}[2]{\\bf\\newcommand{\\result}{#1}%%\n");
  fprintf(annotate_out, "\\newcommand{\\commentator}{#2}\\begin{chess}}%%\n");
  fprintf(annotate_out, "{\\end{chess}\\finito{\\result}{\\commentator}}\n");
  fprintf(annotate_out,
      "\\newenvironment{variation}{[\\begingroup\\rm\\ignorespaces}%%\n");
  fprintf(annotate_out, "{\\endgroup]\\ignorespaces\\newline}\n");
  fprintf(annotate_out,
      "\\newcommand{\\finito}[2]{{\\bf\\hfill#1\\hfill[#2]\\par}}\n");
  fprintf(annotate_out, "\\setlength{\\parindent}{0pt}\n");
  fprintf(annotate_out,
      "\\newenvironment{diagram}{\\begin{nochess}}"
      "{$$\\showboard$$\\end{nochess}}\n");
  fprintf(annotate_out, "\n\n\\begin{document}\n\n");
}

void AnnotateFooterTeX(FILE * annotate_out) {
  fprintf(annotate_out, "\n\n\\end{document}\n");
}
void AnnotatePositionTeX(TREE * tree, int wtm, FILE * annotate_out) {
  char filename[32], html_piece;
  int rank, file;

/*  Display the board in LaTeX using picture notation, similar to html */
  fprintf(annotate_out, "\\begin{diagram}\n\\board\n");
  for (rank = RANK8; rank >= RANK1; rank--) {
    fprintf(annotate_out, "   {");
    for (file = FILEA; file <= FILEH; file++) {
      if ((rank + file) % 2)
        (void) strcpy(filename, " ");
      else
        (void) strcpy(filename, "*");
      html_piece = translate[PcOnSq((rank << 3) + file) + 6];
      switch (html_piece) {
        case 'p':
          strcpy(filename, "p");
          break;
        case 'r':
          strcpy(filename, "r");
          break;
        case 'n':
          strcpy(filename, "n");
          break;
        case 'b':
          strcpy(filename, "b");
          break;
        case 'q':
          strcpy(filename, "q");
          break;
        case 'k':
          strcpy(filename, "k");
          break;
        case 'P':
          strcpy(filename, "P");
          break;
        case 'R':
          strcpy(filename, "R");
          break;
        case 'N':
          strcpy(filename, "N");
          break;
        case 'B':
          strcpy(filename, "B");
          break;
        case 'Q':
          strcpy(filename, "Q");
          break;
        case 'K':
          strcpy(filename, "K");
          break;
        default:
          break;
      }
      fprintf(annotate_out, "%s", filename);
    }
    fprintf(annotate_out, "}\n");
  }
  fprintf(annotate_out, "\\end{diagram}\n");
  fprintf(annotate_out, "\\begin{center} \\begin{nochess}\n  {\\small ");
  if (wtm)
    fprintf(annotate_out, "White to move.\n");
  else
    fprintf(annotate_out, "Black to move.\n");
  fprintf(annotate_out, "}\n \\end{nochess}\\end{center} \n\n");
  fprintf(annotate_out, "\n");
}
char *AnnotateVtoNAG(int value, int wtm, int html_mode, int latex) {
  static char buf[64];

  if (!wtm)
    value = -value;
  if (value > MIN_DECISIVE_ADV)
    strcpy(buf, html_mode ? "+-" : "$18");
  else if (value > MIN_MODERATE_ADV)
    strcpy(buf, html_mode ? "+/-" : "$16");
  else if (value > MIN_SLIGHT_ADV)
    strcpy(buf, html_mode ? "+=" : "$14");
  else if (value < -MIN_DECISIVE_ADV)
    strcpy(buf, html_mode ? "-+" : "$19");
  else if (value < -MIN_MODERATE_ADV)
    strcpy(buf, html_mode ? "-/+" : "$17");
  else if (value < -MIN_SLIGHT_ADV)
    strcpy(buf, html_mode ? "=+" : "$15");
  else
    strcpy(buf, html_mode ? "=" : "$10");
  if (latex == 1) {
    if (value > MIN_DECISIVE_ADV)
      strcpy(buf, "\\wdecisive");
    else if (value > MIN_MODERATE_ADV)
      strcpy(buf, "\\wupperhand");
    else if (value > MIN_SLIGHT_ADV)
      strcpy(buf, "\\wbetter");
    else if (value < -MIN_DECISIVE_ADV)
      strcpy(buf, "\\bdecisive");
    else if (value < -MIN_MODERATE_ADV)
      strcpy(buf, "\\bupperhand");
    else if (value < -MIN_SLIGHT_ADV)
      strcpy(buf, "\\bbetter");
    else
      strcpy(buf, "\\equal");
  }
  return buf;
}