Subversion Repositories Games.Chess Giants

Rev

Rev 108 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 pmbaty 1
#include <ctype.h>
2
#include <signal.h>
3
#include "chess.h"
4
#include "data.h"
5
#if defined(UNIX)
6
#  include <unistd.h>
7
#  include <signal.h>
8
#endif
9
#include "epdglue.h"
154 pmbaty 10
#include "tbprobe.h"
11
/* last modified 08/03/16 */
33 pmbaty 12
/*
13
 *******************************************************************************
14
 *                                                                             *
15
 *   Option() is used to handle user input necessary to control/customize the  *
16
 *   program.  It performs all functions excepting chess move input which is   *
17
 *   handled by main().                                                        *
18
 *                                                                             *
19
 *******************************************************************************
20
 */
21
int Option(TREE * RESTRICT tree) {
108 pmbaty 22
  int v;
23
 
33 pmbaty 24
/*
25
 ************************************************************
26
 *                                                          *
27
 *  parse the input.  If it looks like a FEN string, don't  *
28
 *  parse using "/" as a separator, otherwise do.           *
29
 *                                                          *
30
 ************************************************************
31
 */
32
  if (StrCnt(buffer, '/') >= 7)
33
    nargs = ReadParse(buffer, args, " \t;=");
34
  else
35
    nargs = ReadParse(buffer, args, " \t;=/");
36
  if (!nargs)
37
    return 1;
38
  if (args[0][0] == '#')
39
    return 1;
40
/*
41
 ************************************************************
42
 *                                                          *
43
 *  EPD implementation interface code.  EPD commands can    *
44
 *  not be handled if the program is actually searching in  *
45
 *  a real game, and if Crafty is "pondering" this has to   *
46
 *  be stopped.                                             *
47
 *                                                          *
48
 ************************************************************
49
 */
50
#if defined(EPD)
51
  if (initialized) {
52
    if (EGCommandCheck(buffer)) {
53
      if (thinking || pondering)
54
        return 2;
55
      else {
108 pmbaty 56
        EGCommand(buffer);
33 pmbaty 57
        return 1;
58
      }
59
    }
60
  }
61
#endif
62
/*
63
 ************************************************************
64
 *                                                          *
65
 *  "!" character is a 'shell escape' that passes the rest  *
66
 *  of the command to a shell for execution.  Note that it  *
67
 *  is ignored if in xboard mode because one could use your *
68
 *  zippy2password to execute commands on your local        *
69
 *  machine, probably something that is not wanted.         *
70
 *                                                          *
71
 ************************************************************
72
 */
73
  if (buffer[0] == '!') {
108 pmbaty 74
    if (!xboard) {
75
      v = system(strchr(buffer, '!') + 1);
76
      if (v != 0)
77
        perror("Option() system() error: ");
78
    }
33 pmbaty 79
  }
80
/*
81
 ************************************************************
82
 *                                                          *
83
 *  "." ignores "." if it happens to get to this point, if  *
84
 *  xboard is running.                                      *
85
 *                                                          *
86
 ************************************************************
87
 */
88
  else if (OptionMatch(".", *args)) {
89
    if (xboard) {
90
      printf("stat01: 0 0 0 0 0\n");
91
      fflush(stdout);
92
      return 1;
93
    } else
94
      return 0;
95
  }
96
/*
97
 ************************************************************
98
 *                                                          *
99
 *  "accepted" handles the new xboard protocol version 2    *
100
 *  accepted command.                                       *
101
 *                                                          *
102
 ************************************************************
103
 */
104
  else if (OptionMatch("accepted", *args)) {
105
  }
106
/*
107
 ************************************************************
108
 *                                                          *
109
 *  "adaptive" sets the new adaptive hash algorithm         *
110
 *  parameters.  It requires five parameters.  The first is *
111
 *  an estimated NPS, the second is the minimum hash size,  *
112
 *  and the third is the maximum hash size.  The adaptive   *
113
 *  algorithm will look at the time control, and try to     *
114
 *  adjust the hash sizes to an optimal value without       *
115
 *  dropping below the minimum or exceeding the maximum     *
116
 *  memory size given.  The min/max sizes can be given      *
117
 *  using the same syntax as the hash= command, ie xxx,     *
118
 *  xxxK or xxxM will all work. The fourth and fifth        *
119
 *  parameters are used to limit hashp in the same way.     *
120
 *                                                          *
121
 ************************************************************
122
 */
123
  else if (OptionMatch("adaptive", *args)) {
124
    if (nargs != 6) {
125
      printf("usage:  adaptive NPS hmin hmax pmin pmax\n");
126
      return 1;
127
    }
128
    if (nargs > 1) {
154 pmbaty 129
      adaptive_hash = atoiKMB(args[1]);
130
      adaptive_hash_min = atoiKMB(args[2]);
131
      adaptive_hash_max = atoiKMB(args[3]);
132
      adaptive_hashp_min = atoiKMB(args[4]);
133
      adaptive_hashp_max = atoiKMB(args[5]);
33 pmbaty 134
    }
108 pmbaty 135
    Print(32, "adaptive estimated NPS =  %s\n", DisplayKMB(adaptive_hash, 1));
136
    Print(32, "adaptive minimum hsize =  %s\n", DisplayKMB(adaptive_hash_min,
33 pmbaty 137
            1));
108 pmbaty 138
    Print(32, "adaptive maximum hsize =  %s\n", DisplayKMB(adaptive_hash_max,
33 pmbaty 139
            1));
108 pmbaty 140
    Print(32, "adaptive minimum psize =  %s\n", DisplayKMB(adaptive_hashp_min,
33 pmbaty 141
            1));
108 pmbaty 142
    Print(32, "adaptive maximum psize =  %s\n", DisplayKMB(adaptive_hashp_max,
33 pmbaty 143
            1));
144
  }
145
/*
146
 ************************************************************
147
 *                                                          *
148
 *  "alarm" command turns audible move warning on/off.      *
149
 *                                                          *
150
 ************************************************************
151
 */
152
  else if (OptionMatch("alarm", *args)) {
153
    if (!strcmp(args[1], "on"))
154
      audible_alarm = 0x07;
155
    else if (!strcmp(args[1], "off"))
156
      audible_alarm = 0x00;
157
    else
158
      printf("usage:  alarm on|off\n");
159
  }
160
/*
161
 ************************************************************
162
 *                                                          *
163
 *  "analyze" puts Crafty in analyze mode, where it reads   *
164
 *  moves in and between moves, computes as though it is    *
165
 *  trying to find the best move to make.  When another     *
166
 *  move is entered, it switches sides and continues.  It   *
167
 *  will never make a move on its own, rather, it will      *
168
 *  continue to analyze until an "exit" command is given.   *
169
 *                                                          *
170
 ************************************************************
171
 */
172
  else if (OptionMatch("analyze", *args)) {
173
    if (thinking || pondering)
174
      return 2;
175
    Analyze();
176
  }
177
/*
178
 ************************************************************
179
 *                                                          *
180
 *  "annotate" command is used to read a series of moves    *
181
 *  and analyze the resulting game, producing comments as   *
182
 *  requested by the user.  This also handles the annotateh *
183
 *  (html) and annotatet (LaTex) output forms of the        *
184
 *  command.                                                *
185
 *                                                          *
186
 ************************************************************
187
 */
188
  else if (OptionMatch("annotate", *args) || OptionMatch("annotateh", *args)
189
      || OptionMatch("annotatet", *args)) {
190
    if (thinking || pondering)
191
      return 2;
192
    Annotate();
193
  }
194
/*
195
 ************************************************************
196
 *                                                          *
108 pmbaty 197
 *  "autotune" command is used to automatically tune the    *
198
 *  SMP search parameters that affect search efficiency.    *
199
 *                                                          *
200
 ************************************************************
201
 */
202
  else if (OptionMatch("autotune", *args)) {
203
    if (thinking || pondering)
204
      return 2;
205
    AutoTune(nargs, args);
206
  }
207
/*
208
 ************************************************************
209
 *                                                          *
33 pmbaty 210
 *  "batch" command disables asynchronous I/O so that a     *
211
 *  stream of commands can be put into a file and they are  *
212
 *  not executed instantly.                                 *
213
 *                                                          *
214
 ************************************************************
215
 */
216
  else if (OptionMatch("batch", *args)) {
217
    if (!strcmp(args[1], "on"))
218
      batch_mode = 1;
219
    else if (!strcmp(args[1], "off"))
220
      batch_mode = 0;
221
    else
222
      printf("usage:  batch on|off\n");
223
  }
224
/*
225
 ************************************************************
226
 *                                                          *
227
 *  "beep" command is ignored. [xboard compatibility]       *
228
 *                                                          *
229
 ************************************************************
230
 */
231
  else if (OptionMatch("beep", *args)) {
232
    return xboard;
233
  }
234
/*
235
 ************************************************************
236
 *                                                          *
108 pmbaty 237
 *  "bench" runs internal performance benchmark.  An        *
238
 *  optional second argument can increase or decrease the   *
239
 *  time it takes.  "bench 1" increases the default depth   *
240
 *  by one ply, and "bench -1" reduces the depth to speed   *
241
 *  it up.                                                  *
33 pmbaty 242
 *                                                          *
243
 ************************************************************
244
 */
245
  else if (OptionMatch("bench", *args)) {
154 pmbaty 246
    int mod = 0;
108 pmbaty 247
 
248
    if (nargs > 1)
249
      mod = atoi(args[1]);
154 pmbaty 250
    (void) Bench(mod, 0);
33 pmbaty 251
  }
252
/*
253
 ************************************************************
254
 *                                                          *
255
 *  "bk"  book command from xboard sends the suggested book *
256
 *  moves.                                                  *
257
 *                                                          *
258
 ************************************************************
259
 */
260
  else if (OptionMatch("bk", *args)) {
261
    printf("\t%s\n\n", book_hint);
262
    fflush(stdout);
263
    return xboard;
264
  }
265
/*
266
 ************************************************************
267
 *                                                          *
268
 *  "black" command sets black to move (Flip(wtm)).         *
269
 *                                                          *
270
 ************************************************************
271
 */
108 pmbaty 272
  else if (OptionMatch("white", *args)) {
33 pmbaty 273
    if (thinking || pondering)
274
      return 2;
108 pmbaty 275
    game_wtm = 1;
33 pmbaty 276
    ponder_move = 0;
277
    last_pv.pathd = 0;
278
    last_pv.pathl = 0;
279
    if (!game_wtm)
280
      Pass();
281
    force = 0;
108 pmbaty 282
  } else if (OptionMatch("black", *args)) {
33 pmbaty 283
    if (thinking || pondering)
284
      return 2;
108 pmbaty 285
    game_wtm = 0;
33 pmbaty 286
    ponder_move = 0;
287
    last_pv.pathd = 0;
288
    last_pv.pathl = 0;
289
    if (game_wtm)
290
      Pass();
291
    force = 0;
292
  }
293
/*
294
 ************************************************************
295
 *                                                          *
296
 *  "bogus" command is ignored. [xboard compatibility]      *
297
 *                                                          *
298
 ************************************************************
299
 */
300
  else if (OptionMatch("bogus", *args)) {
301
    return xboard;
302
  }
303
/*
304
 ************************************************************
305
 *                                                          *
108 pmbaty 306
 *  "book" command updates/creates the opening book file.   *
307
 *                                                          *
308
 ************************************************************
309
 */
310
  else if (OptionMatch("book", *args)) {
311
    nargs = ReadParse(buffer, args, " \t;");
312
    Bookup(tree, nargs, args);
313
  } else if (!strcmp("create", *(args + 1))) {
314
    nargs = ReadParse(buffer, args, " \t;");
315
    Bookup(tree, nargs, args);
316
  }
317
/*
318
 ************************************************************
319
 *                                                          *
33 pmbaty 320
 *  "bookw" command updates the book selection weights.     *
321
 *                                                          *
322
 ************************************************************
323
 */
108 pmbaty 324
  else if (OptionMatch("bookw", *args)) {
33 pmbaty 325
    if (nargs > 1) {
108 pmbaty 326
      if (OptionMatch("frequency", args[1]))
154 pmbaty 327
        book_weight_freq = atof(args[2]);
108 pmbaty 328
      else if (OptionMatch("evaluation", args[1]))
154 pmbaty 329
        book_weight_eval = atof(args[2]);
108 pmbaty 330
      else if (OptionMatch("learning", args[1]))
154 pmbaty 331
        book_weight_learn = atof(args[2]);
33 pmbaty 332
    } else {
108 pmbaty 333
      Print(32, "frequency (freq)..............%4.2f\n", book_weight_freq);
334
      Print(32, "static evaluation (eval)......%4.2f\n", book_weight_eval);
335
      Print(32, "learning (learn)..............%4.2f\n", book_weight_learn);
33 pmbaty 336
    }
337
  }
338
/*
339
 ************************************************************
340
 *                                                          *
341
 *  "clock" command displays chess clock.                   *
342
 *                                                          *
343
 ************************************************************
344
 */
345
  else if (OptionMatch("clock", *args)) {
346
    int side;
347
 
348
    for (side = white; side >= black; side--) {
108 pmbaty 349
      Print(32, "time remaining (%s): %s", (side) ? "white" : "black",
33 pmbaty 350
          DisplayHHMMSS(tc_time_remaining[side]));
351
      if (tc_sudden_death != 1)
108 pmbaty 352
        Print(32, "  (%d more moves)", tc_moves_remaining[side]);
353
      Print(32, "\n");
33 pmbaty 354
    }
108 pmbaty 355
    Print(32, "\n");
33 pmbaty 356
    if (tc_sudden_death == 1)
108 pmbaty 357
      Print(32, "Sudden-death time control in effect\n");
33 pmbaty 358
  }
359
/*
360
 ************************************************************
361
 *                                                          *
362
 *  "computer" lets Crafty know it is playing a computer.   *
363
 *                                                          *
364
 ************************************************************
365
 */
366
  else if (OptionMatch("computer", *args)) {
108 pmbaty 367
    Print(32, "playing a computer!\n");
33 pmbaty 368
    accept_draws = 1;
108 pmbaty 369
    if (resign)
370
      resign = 10;
371
    resign_count = 4;
33 pmbaty 372
    usage_level = 0;
373
    books_file = (computer_bs_file) ? computer_bs_file : normal_bs_file;
374
  }
375
/*
376
 ************************************************************
377
 *                                                          *
378
 *  "display" command displays the chess board.             *
379
 *                                                          *
380
 *  "display" command sets specific display options which   *
381
 *  control how "chatty" the program is.  In the variable   *
382
 *  display_options, the following bits are set/cleared     *
383
 *  based on the option chosen.                             *
384
 *                                                          *
108 pmbaty 385
 *    1 -> display move/time/results/etc.                   *
386
 *    2 -> display PV.                                      *
387
 *    4 -> display fail high / fail low moves               *
388
 *    8 -> display search statistics.                       *
389
 *   16 -> display root moves as they are searched.         *
390
 *   32 -> display general informational messages.          *
391
 *   64 -> display ply-1 move list / flags after each       *
33 pmbaty 392
 *         iteration.                                       *
108 pmbaty 393
 *  128 -> display root moves and scores before search      *
394
 *         begins.                                          *
395
 * 2048 -> error messages (can not be disabled).            *
33 pmbaty 396
 *                                                          *
397
 ************************************************************
398
 */
399
  else if (OptionMatch("display", *args)) {
108 pmbaty 400
    int i, set, old_display_options = display_options;
401
    char *doptions[8] = { "moveinfo", "pv", "fail", "stats", "moves", "info",
402
      "ply1", "movelist"
403
    };
404
    char *descriptions[8] = { "display move time/results/etc",
405
      "principal variation", "fail highs/lows", "search statistics",
406
      "root moves as they are searched", "general information",
407
      "ply1 move list after each iteration",
408
      "root move list and scores prior to search"
409
    };
410
 
411
    if (nargs > 1) {
412
      if (!strcmp(args[1], "all"))
413
        old_display_options = ~display_options;
414
      for (i = 0; i < 8; i++) {
415
        if (strstr(args[1], doptions[i])) {
416
          if (strstr(args[1], "no"))
417
            set = 0;
418
          else
419
            set = 1;
420
          display_options &= ~(1 << i);
421
          display_options |= set << i;
33 pmbaty 422
          break;
108 pmbaty 423
        }
424
      }
425
      for (i = 0; i < 8; i++) {
426
        if ((old_display_options & (1 << i)) != (display_options & (1 << i))) {
427
          Print(32, "display ");
428
          if (!(display_options & (1 << i)))
429
            Print(32, "no");
430
          Print(32, "%s (%s)\n", doptions[i], descriptions[i]);
431
        }
432
      }
433
    } else
33 pmbaty 434
      DisplayChessBoard(stdout, display);
435
  }
436
/*
437
 ************************************************************
438
 *                                                          *
439
 *  "debug" handles the new debug command that is often     *
440
 *  modified to test some modified code function.           *
441
 *                                                          *
442
 ************************************************************
443
 */
444
  else if (OptionMatch("debug", *args)) {
108 pmbaty 445
    Print(32, "ERROR:  no debug code included\n");
33 pmbaty 446
  }
447
/*
448
 ************************************************************
449
 *                                                          *
450
 *  "depth" command sets a specific search depth to         *
451
 *  control the tree search depth. [xboard compatibility].  *
452
 *                                                          *
453
 ************************************************************
454
 */
108 pmbaty 455
  else if (OptionMatch("depth", *args)) {
33 pmbaty 456
    if (nargs < 2) {
457
      printf("usage:  depth <n>\n");
458
      return 1;
459
    }
460
    search_depth = atoi(args[1]);
108 pmbaty 461
    Print(32, "search depth set to %d.\n", search_depth);
33 pmbaty 462
  }
463
/*
464
 ************************************************************
465
 *                                                          *
466
 *  "draw" is used to offer Crafty a draw, or to control    *
467
 *  whether Crafty will offer and/or accept draw offers.    *
468
 *                                                          *
469
 ************************************************************
470
 */
471
  else if (OptionMatch("draw", *args)) {
472
    if (nargs == 1) {
473
      draw_offer_pending = 1;
474
      if (draw_offered) {
475
        Print(4095, "1/2-1/2 {Draw agreed}\n");
108 pmbaty 476
        strcpy(pgn_result, "1/2-1/2");
33 pmbaty 477
      }
478
    } else {
479
      if (!strcmp(args[1], "accept")) {
480
        accept_draws = 1;
108 pmbaty 481
        Print(32, "accept draw offers\n");
33 pmbaty 482
      } else if (!strcmp(args[1], "decline")) {
483
        accept_draws = 0;
108 pmbaty 484
        Print(32, "decline draw offers\n");
485
      } else if (!strcmp(args[1], "dynamic")) {
486
        if (nargs > 2)
487
          dynamic_draw_score = atoi(args[2]);
488
        Print(32, "dynamic draw scores %s\n",
489
            (dynamic_draw_score) ? "enabled" : "disabled");
33 pmbaty 490
      } else if (!strcmp(args[1], "offer")) {
491
        offer_draws = 1;
108 pmbaty 492
        Print(32, "offer draws\n");
33 pmbaty 493
      } else if (!strcmp(args[1], "nooffer")) {
494
        offer_draws = 0;
108 pmbaty 495
        Print(32, "do not offer draws\n");
33 pmbaty 496
      } else
108 pmbaty 497
        Print(32, "usage: draw accept|decline|offer|nooffer\n");
33 pmbaty 498
    }
499
  }
500
/*
501
 ************************************************************
502
 *                                                          *
503
 *  "easy" command disables thinking on opponent's time.    *
504
 *                                                          *
505
 ************************************************************
506
 */
507
  else if (OptionMatch("easy", *args)) {
508
    if (thinking || pondering)
509
      return 2;
510
    ponder = 0;
108 pmbaty 511
    Print(32, "pondering disabled.\n");
33 pmbaty 512
  }
513
/*
514
 ************************************************************
515
 *                                                          *
516
 *  "echo" command displays messages from command file.     *
517
 *                                                          *
518
 ************************************************************
519
 */
520
  else if (OptionMatch("echo", *args) || OptionMatch("title", *args)) {
521
  }
522
/*
523
 ************************************************************
524
 *                                                          *
525
 *  "edit" command modifies the board position.             *
526
 *                                                          *
527
 ************************************************************
528
 */
108 pmbaty 529
  else if (OptionMatch("edit", *args)) {
33 pmbaty 530
    if (thinking || pondering)
531
      return 2;
532
    Edit();
108 pmbaty 533
    move_number = 1; /* discard history */
33 pmbaty 534
    if (!game_wtm) {
535
      game_wtm = 1;
536
      Pass();
537
    }
538
    ponder_move = 0;
539
    last_pv.pathd = 0;
540
    last_pv.pathl = 0;
108 pmbaty 541
    strcpy(buffer, "savepos *");
542
    Option(tree);
33 pmbaty 543
  }
544
/*
545
 ************************************************************
546
 *                                                          *
154 pmbaty 547
 *  "egtb" command enables syzygy endgame database tables.  *
33 pmbaty 548
 *                                                          *
549
 ************************************************************
550
 */
551
  else if (OptionMatch("egtb", *args)) {
154 pmbaty 552
#if defined(SYZYGY)
553
    if (!strcmp(args[1], "off"))
554
      EGTBlimit = 0;
555
    else {
556
      if (!EGTB_setup) {
557
        tb_init(tb_path);
33 pmbaty 558
        EGTB_setup = 1;
559
      }
154 pmbaty 560
      EGTBlimit = TB_LARGEST;
33 pmbaty 561
    }
154 pmbaty 562
    if (EGTBlimit)
563
      Print(32, "SYZYGY EGTB access enabled, %d piece TBs found\n",
564
          TB_LARGEST);
565
    else
566
      Print(32, "SYZYGY EGTB access disabled.\n");
567
#else
568
    Print(32, "SYZYGY support not included (no -DSYZYGY)\n");
569
#endif
33 pmbaty 570
  }
108 pmbaty 571
/*
572
 ************************************************************
573
 *                                                          *
574
 *  "egtbd" command sets the probe depth limit.  If the     *
575
 *  remaining depth is < this limit, probes are not done to *
576
 *  avoid slowing the search unnecessarily.                 *
577
 *                                                          *
578
 ************************************************************
579
 */
154 pmbaty 580
#if defined(SYZYGY)
108 pmbaty 581
  else if (OptionMatch("egtbd", *args)) {
582
    if (nargs > 1)
583
      EGTB_depth = atoi(args[1]);
584
    Print(32, "EGTB probe depth set to %d\n", EGTB_depth);
585
  }
33 pmbaty 586
#endif
587
/*
588
 ************************************************************
589
 *                                                          *
590
 *  "end" (or "quit") command terminates the program.       *
591
 *                                                          *
592
 ************************************************************
593
 */
594
  else if (OptionMatch("end", *args) || OptionMatch("quit", *args)) {
595
    abort_search = 1;
596
    quit = 1;
597
    last_search_value =
598
        (crafty_is_white) ? last_search_value : -last_search_value;
599
    if (moves_out_of_book)
600
      LearnBook();
601
    if (thinking || pondering)
602
      return 1;
603
    CraftyExit(0);
604
  }
605
/*
606
 ************************************************************
607
 *                                                          *
608
 *  "evtest" command runs a test suite of problems and      *
609
 *  prints evaluations only.                                *
610
 *                                                          *
611
 ************************************************************
612
 */
613
  else if (OptionMatch("evtest", *args)) {
614
    if (thinking || pondering)
615
      return 2;
616
    if (nargs < 2) {
617
      printf("usage:  evtest <filename>\n");
618
      return 1;
619
    }
620
    EVTest(args[1]);
621
    ponder_move = 0;
622
    last_pv.pathd = 0;
623
    last_pv.pathl = 0;
624
  }
625
/*
626
 ************************************************************
627
 *                                                          *
628
 *  "exit" command resets input device to STDIN.            *
629
 *                                                          *
630
 ************************************************************
631
 */
632
  else if (OptionMatch("exit", *args)) {
633
    if (analyze_mode)
634
      return 0;
635
    if (input_stream != stdin)
636
      fclose(input_stream);
637
    input_stream = stdin;
638
    ReadClear();
108 pmbaty 639
    Print(32, "\n");
33 pmbaty 640
  }
641
/*
642
 ************************************************************
643
 *                                                          *
644
 *  "flag" command controls whether Crafty will call the    *
645
 *  flag in xboard/winboard games (to end the game.)        *
646
 *                                                          *
647
 ************************************************************
648
 */
108 pmbaty 649
  else if (OptionMatch("flag", *args)) {
33 pmbaty 650
    if (nargs < 2) {
651
      printf("usage:  flag on|off\n");
652
      return 1;
653
    }
654
    if (!strcmp(args[1], "on"))
655
      call_flag = 1;
656
    else if (!strcmp(args[1], "off"))
657
      call_flag = 0;
658
    if (call_flag)
108 pmbaty 659
      Print(32, "end game on time forfeits\n");
33 pmbaty 660
    else
108 pmbaty 661
      Print(32, "ignore time forfeits\n");
33 pmbaty 662
  }
663
/*
664
 ************************************************************
665
 *                                                          *
666
 *  "flip" command flips the board, interchanging each      *
667
 *  rank with the corresponding rank on the other half of   *
668
 *  the board, and also reverses the color of all pieces.   *
669
 *                                                          *
670
 ************************************************************
671
 */
672
  else if (OptionMatch("flip", *args)) {
673
    int file, rank, piece, temp;
674
 
675
    if (thinking || pondering)
676
      return 2;
677
    for (rank = 0; rank < 4; rank++) {
678
      for (file = 0; file < 8; file++) {
679
        piece = -PcOnSq((rank << 3) + file);
680
        PcOnSq((rank << 3) + file) = -PcOnSq(((7 - rank) << 3) + file);
681
        PcOnSq(((7 - rank) << 3) + file) = piece;
682
      }
683
    }
684
    game_wtm = Flip(game_wtm);
685
    temp = Castle(0, white);
686
    Castle(0, white) = Castle(0, black);
687
    Castle(0, black) = temp;
688
    SetChessBitBoards(tree);
689
#if defined(DEBUG)
690
    ValidatePosition(tree, 0, game_wtm, "Option().flip");
691
#endif
692
  }
693
/*
694
 ************************************************************
695
 *                                                          *
696
 *  "flop" command flops the board, interchanging each      *
697
 *  file with the corresponding file on the other half of   *
698
 *  the board.                                              *
699
 *                                                          *
700
 ************************************************************
701
 */
702
  else if (OptionMatch("flop", *args)) {
703
    int file, rank, piece;
704
 
705
    if (thinking || pondering)
706
      return 2;
707
    for (rank = 0; rank < 8; rank++) {
708
      for (file = 0; file < 4; file++) {
709
        piece = PcOnSq((rank << 3) + file);
710
        PcOnSq((rank << 3) + file) = PcOnSq((rank << 3) + 7 - file);
711
        PcOnSq((rank << 3) + 7 - file) = piece;
712
      }
713
    }
714
    SetChessBitBoards(tree);
715
#if defined(DEBUG)
716
    ValidatePosition(tree, 0, game_wtm, "Option().flop");
717
#endif
718
  }
719
/*
720
 ************************************************************
721
 *                                                          *
722
 *  "force" command forces the program to make a specific   *
723
 *  move instead of its last chosen move.                   *
724
 *                                                          *
725
 ************************************************************
726
 */
727
  else if (OptionMatch("force", *args)) {
728
    int move, movenum, save_move_number;
729
    char text[16];
730
 
731
    if (thinking || pondering)
732
      return 3;
733
    if (xboard) {
734
      force = 1;
735
      return 3;
736
    }
737
    if (nargs < 2) {
738
      printf("usage:  force <move>\n");
739
      return 1;
740
    }
741
    ponder_move = 0;
742
    presult = 0;
743
    last_pv.pathd = 0;
744
    last_pv.pathl = 0;
745
    save_move_number = move_number;
746
    movenum = move_number;
747
    if (game_wtm)
748
      movenum--;
108 pmbaty 749
    strcpy(text, args[1]);
750
    sprintf(buffer, "reset %d", movenum);
33 pmbaty 751
    game_wtm = Flip(game_wtm);
108 pmbaty 752
    Option(tree);
753
    move = InputMove(tree, 0, game_wtm, 0, 0, text);
33 pmbaty 754
    if (move) {
755
      if (input_stream != stdin)
108 pmbaty 756
        printf("%s\n", OutputMove(tree, 0, game_wtm, move));
33 pmbaty 757
      if (history_file) {
758
        fseek(history_file, ((movenum - 1) * 2 + 1 - game_wtm) * 10,
759
            SEEK_SET);
108 pmbaty 760
        fprintf(history_file, "%9s\n", OutputMove(tree, 0, game_wtm, move));
33 pmbaty 761
      }
108 pmbaty 762
      MakeMoveRoot(tree, game_wtm, move);
33 pmbaty 763
      last_pv.pathd = 0;
764
      last_pv.pathl = 0;
765
    } else if (input_stream == stdin)
766
      printf("illegal move.\n");
767
    game_wtm = Flip(game_wtm);
768
    move_number = save_move_number;
108 pmbaty 769
    strcpy(ponder_text, "none");
33 pmbaty 770
  }
771
/*
772
 ************************************************************
773
 *                                                          *
774
 *  "go" command does nothing, except force main() to start *
775
 *  a search.  ("move" is an alias for go).                 *
776
 *                                                          *
777
 ************************************************************
778
 */
779
  else if (OptionMatch("go", *args) || OptionMatch("move", *args)) {
108 pmbaty 780
    int t;
33 pmbaty 781
    char temp[128];
782
 
783
    if (thinking || pondering)
784
      return 2;
785
    if (game_wtm) {
786
      if (strncmp(pgn_white, "Crafty", 6)) {
108 pmbaty 787
        strcpy(temp, pgn_white);
788
        strcpy(pgn_white, pgn_black);
789
        strcpy(pgn_black, temp);
33 pmbaty 790
      }
791
    } else {
792
      if (strncmp(pgn_black, "Crafty", 6)) {
108 pmbaty 793
        strcpy(temp, pgn_white);
794
        strcpy(pgn_white, pgn_black);
795
        strcpy(pgn_black, temp);
33 pmbaty 796
      }
797
    }
108 pmbaty 798
    t = tc_time_remaining[white];
799
    tc_time_remaining[white] = tc_time_remaining[black];
800
    tc_time_remaining[black] = t;
801
    t = tc_moves_remaining[white];
802
    tc_moves_remaining[white] = tc_moves_remaining[black];
803
    tc_moves_remaining[black] = t;
33 pmbaty 804
    force = 0;
805
    return -1;
806
  }
807
/*
808
 ************************************************************
809
 *                                                          *
810
 *  "history" command displays game history (moves).        *
811
 *                                                          *
812
 ************************************************************
813
 */
814
  else if (OptionMatch("history", *args)) {
815
    int i;
816
    char buffer[128];
817
 
818
    if (history_file) {
819
      printf("    white       black\n");
820
      for (i = 0; i < (move_number - 1) * 2 - game_wtm + 1; i++) {
821
        fseek(history_file, i * 10, SEEK_SET);
108 pmbaty 822
        v = fscanf(history_file, "%s", buffer);
823
        if (v <= 0)
824
          perror("Option() fscanf error: ");
33 pmbaty 825
        if (!(i % 2))
826
          printf("%3d", i / 2 + 1);
827
        printf("  %-10s", buffer);
828
        if (i % 2 == 1)
829
          printf("\n");
830
      }
831
      if (Flip(game_wtm))
832
        printf("  ...\n");
833
    }
834
  }
835
/*
836
 ************************************************************
837
 *                                                          *
838
 *  "hard" command enables thinking on opponent's time.     *
839
 *                                                          *
840
 ************************************************************
841
 */
842
  else if (OptionMatch("hard", *args)) {
843
    ponder = 1;
108 pmbaty 844
    Print(32, "pondering enabled.\n");
33 pmbaty 845
  }
846
/*
847
 ************************************************************
848
 *                                                          *
849
 *  "hash" command controls the transposition table size.   *
108 pmbaty 850
 *  The size can be entered in one of four ways:            *
33 pmbaty 851
 *                                                          *
852
 *     hash=nnn  where nnn is in bytes.                     *
853
 *     hash=nnnK where nnn is in K bytes.                   *
854
 *     hash=nnnM where nnn is in M bytes.                   *
108 pmbaty 855
 *     hash=nnnG where nnn is in G bytes.                   *
33 pmbaty 856
 *                                                          *
857
 *  the only restriction is that the hash table is computed *
858
 *  as a perfect power of 2.  Any value that is not a       *
859
 *  perfect power of 2 is rounded down so that it is, in    *
860
 *  order to avoid breaking the addressing scheme.          *
861
 *                                                          *
862
 ************************************************************
863
 */
864
  else if (OptionMatch("hash", *args)) {
108 pmbaty 865
    size_t old_hash_size = hash_table_size, new_hash_size;
33 pmbaty 866
 
867
    if (thinking || pondering)
868
      return 2;
869
    if (nargs > 1) {
870
      allow_memory = 0;
108 pmbaty 871
      if (xboard)
872
        Print(4095, "Warning--  xboard 'memory' option disabled\n");
154 pmbaty 873
      new_hash_size = atoiKMB(args[1]);
33 pmbaty 874
      if (new_hash_size < 64 * 1024) {
875
        printf("ERROR.  Minimum hash table size is 64K bytes.\n");
876
        return 1;
877
      }
108 pmbaty 878
      hash_table_size = ((1ull) << MSB(new_hash_size)) / 16;
154 pmbaty 879
      AlignedRemalloc((void *) &hash_table, 64,
108 pmbaty 880
          hash_table_size * sizeof(HASH_ENTRY));
881
      if (!hash_table) {
33 pmbaty 882
        printf("AlignedRemalloc() failed, not enough memory.\n");
883
        exit(1);
884
      }
108 pmbaty 885
      hash_mask = (hash_table_size - 1) & ~3;
33 pmbaty 886
    }
108 pmbaty 887
    Print(32, "hash table memory = %s bytes",
888
        DisplayKMB(hash_table_size * sizeof(HASH_ENTRY), 1));
889
    Print(32, " (%s entries).\n", DisplayKMB(hash_table_size, 1));
890
    InitializeHashTables(old_hash_size != hash_table_size);
33 pmbaty 891
  }
892
/*
893
 ************************************************************
894
 *                                                          *
895
 *  "phash" command controls the path hash table size. The  *
108 pmbaty 896
 *  size can be entered in one of four ways:                *
33 pmbaty 897
 *                                                          *
898
 *     phash=nnn  where nnn is in bytes.                    *
899
 *     phash=nnnK where nnn is in K bytes.                  *
900
 *     phash=nnnM where nnn is in M bytes.                  *
108 pmbaty 901
 *     phash=nnnG where nnn is in G bytes.                  *
33 pmbaty 902
 *                                                          *
903
 *  the only restriction is that the path hash table must   *
904
 *  have a perfect power of 2 entries.  The value entered   *
905
 *  will be rounded down to meet that requirement.          *
906
 *                                                          *
907
 ************************************************************
908
 */
909
  else if (OptionMatch("phash", *args)) {
108 pmbaty 910
    size_t old_hash_size = hash_path_size, new_hash_size;
33 pmbaty 911
 
912
    if (thinking || pondering)
913
      return 2;
914
    if (nargs > 1) {
154 pmbaty 915
      new_hash_size = atoiKMB(args[1]);
33 pmbaty 916
      if (new_hash_size < 64 * 1024) {
917
        printf("ERROR.  Minimum phash table size is 64K bytes.\n");
918
        return 1;
919
      }
920
      hash_path_size = ((1ull) << MSB(new_hash_size / sizeof(HPATH_ENTRY)));
154 pmbaty 921
      AlignedRemalloc((void *) &hash_path, 64,
33 pmbaty 922
          sizeof(HPATH_ENTRY) * hash_path_size);
923
      if (!hash_path) {
924
        printf("AlignedRemalloc() failed, not enough memory.\n");
925
        hash_path_size = 0;
926
        hash_path = 0;
927
      }
928
      hash_path_mask = (hash_path_size - 1) & ~15;
929
    }
108 pmbaty 930
    Print(32, "hash path table memory = %s bytes",
931
        DisplayKMB(hash_path_size * sizeof(HPATH_ENTRY), 1));
932
    Print(32, " (%s entries).\n", DisplayKMB(hash_path_size, 1));
933
    InitializeHashTables(old_hash_size != hash_path_size);
33 pmbaty 934
  }
935
/*
936
 ************************************************************
937
 *                                                          *
938
 *  "hashp" command controls the pawn hash table size.      *
939
 *                                                          *
940
 ************************************************************
941
 */
942
  else if (OptionMatch("hashp", *args)) {
108 pmbaty 943
    size_t old_hash_size = pawn_hash_table_size, new_hash_size;
33 pmbaty 944
 
945
    if (thinking || pondering)
946
      return 2;
947
    if (nargs > 1) {
948
      allow_memory = 0;
108 pmbaty 949
      if (xboard)
950
        Print(4095, "Warning--  xboard 'memory' option disabled\n");
154 pmbaty 951
      new_hash_size = atoiKMB(args[1]);
33 pmbaty 952
      if (new_hash_size < 16 * 1024) {
953
        printf("ERROR.  Minimum pawn hash table size is 16K bytes.\n");
954
        return 1;
955
      }
956
      pawn_hash_table_size =
108 pmbaty 957
          1ull << MSB(new_hash_size / sizeof(PAWN_HASH_ENTRY));
154 pmbaty 958
      AlignedRemalloc((void *) &pawn_hash_table, 64,
33 pmbaty 959
          sizeof(PAWN_HASH_ENTRY) * pawn_hash_table_size);
960
      if (!pawn_hash_table) {
961
        printf("AlignedRemalloc() failed, not enough memory.\n");
962
        exit(1);
963
      }
108 pmbaty 964
      pawn_hash_mask = pawn_hash_table_size - 1;
965
    }
966
    Print(32, "pawn hash table memory = %s bytes",
967
        DisplayKMB(pawn_hash_table_size * sizeof(PAWN_HASH_ENTRY), 1));
968
    Print(32, " (%s entries).\n", DisplayKMB(pawn_hash_table_size, 1));
969
    InitializeHashTables(old_hash_size != pawn_hash_table_size);
970
  }
971
/*
972
 ************************************************************
973
 *                                                          *
33 pmbaty 974
 *  "help" command lists commands/options.                  *
975
 *                                                          *
976
 ************************************************************
977
 */
978
  else if (OptionMatch("help", *args)) {
979
    FILE *helpfile;
980
    char *readstat = (char *) -1;
981
    int lines = 0;
982
 
108 pmbaty 983
    helpfile = fopen("crafty.hlp", "r");
33 pmbaty 984
    if (!helpfile) {
985
      printf("ERROR.  Unable to open \"crafty.hlp\" -- help unavailable\n");
986
      return 1;
987
    }
988
    if (nargs > 1) {
989
      while (1) {
990
        readstat = fgets(buffer, 128, helpfile);
991
        if (!readstat) {
992
          printf("Sorry, no help available for \"%s\"\n", args[1]);
993
          fclose(helpfile);
994
          return 1;
995
        }
996
        if (buffer[0] == '<') {
997
          if (strstr(buffer, args[1]))
998
            break;
999
        }
1000
      }
1001
    }
1002
    while (1) {
1003
      readstat = fgets(buffer, 128, helpfile);
1004
      if (!readstat)
1005
        break;
1006
      if (strchr(buffer, '\n'))
1007
        *strchr(buffer, '\n') = 0;
1008
      if (!strcmp(buffer, "<end>"))
1009
        break;
1010
      printf("%s\n", buffer);
1011
      lines++;
1012
      if (lines > 22) {
1013
        lines = 0;
1014
        printf("<return> for more...");
1015
        fflush(stdout);
108 pmbaty 1016
        Read(1, buffer);
33 pmbaty 1017
      }
1018
    }
1019
    fclose(helpfile);
1020
  }
1021
/*
1022
 ************************************************************
1023
 *                                                          *
1024
 *  "hint" displays the expected move based on the last     *
1025
 *  search done. [xboard compatibility]                     *
1026
 *                                                          *
1027
 ************************************************************
1028
 */
108 pmbaty 1029
  else if (OptionMatch("hint", *args)) {
1030
    if (strlen(ponder_text)) {
1031
      printf("Hint: %s\n", ponder_text);
33 pmbaty 1032
      fflush(stdout);
1033
    }
1034
  }
1035
/*
1036
 ************************************************************
1037
 *                                                          *
1038
 *  "input" command directs the program to read input from  *
1039
 *  a file until eof is reached or an "exit" command is     *
1040
 *  encountered while reading the file.                     *
1041
 *                                                          *
1042
 ************************************************************
1043
 */
1044
  else if (OptionMatch("input", *args)) {
1045
    if (thinking || pondering)
1046
      return 2;
1047
    nargs = ReadParse(buffer, args, " \t=");
1048
    if (nargs < 2) {
1049
      printf("usage:  input <filename>\n");
1050
      return 1;
1051
    }
108 pmbaty 1052
    if (!(input_stream = fopen(args[1], "r"))) {
33 pmbaty 1053
      printf("file does not exist.\n");
1054
      input_stream = stdin;
1055
    }
1056
  }
1057
/*
1058
 ************************************************************
1059
 *                                                          *
1060
 *  "info" command gives some information about Crafty.     *
1061
 *                                                          *
1062
 ************************************************************
1063
 */
1064
  else if (OptionMatch("info", *args)) {
108 pmbaty 1065
    Print(32, "Crafty version %s\n", version);
1066
    Print(32, "number of threads =         %2d\n", smp_max_threads);
1067
    Print(32, "hash table memory = %s bytes", DisplayKMB(hash_table_size * 64,
1068
            1));
1069
    Print(32, " (%s entries).\n", DisplayKMB(hash_table_size * 5, 0));
1070
    Print(32, "pawn hash table memory = %5s\n",
1071
        DisplayKMB(pawn_hash_table_size * sizeof(PAWN_HASH_ENTRY), 1));
33 pmbaty 1072
    if (!tc_sudden_death) {
108 pmbaty 1073
      Print(32, "%d moves/%d minutes %d seconds primary time control\n",
33 pmbaty 1074
          tc_moves, tc_time / 6000, (tc_time / 100) % 60);
108 pmbaty 1075
      Print(32, "%d moves/%d minutes %d seconds secondary time control\n",
33 pmbaty 1076
          tc_secondary_moves, tc_secondary_time / 6000,
1077
          (tc_secondary_time / 100) % 60);
1078
      if (tc_increment)
108 pmbaty 1079
        Print(32, "increment %d seconds.\n", tc_increment / 100);
33 pmbaty 1080
    } else if (tc_sudden_death == 1) {
108 pmbaty 1081
      Print(32, " game/%d minutes primary time control\n", tc_time / 6000);
33 pmbaty 1082
      if (tc_increment)
108 pmbaty 1083
        Print(32, "increment %d seconds.\n", (tc_increment / 100) % 60);
33 pmbaty 1084
    } else if (tc_sudden_death == 2) {
108 pmbaty 1085
      Print(32, "%d moves/%d minutes primary time control\n", tc_moves,
33 pmbaty 1086
          tc_time / 6000);
108 pmbaty 1087
      Print(32, "game/%d minutes secondary time control\n",
33 pmbaty 1088
          tc_secondary_time / 6000);
1089
      if (tc_increment)
108 pmbaty 1090
        Print(32, "increment %d seconds.\n", tc_increment / 100);
33 pmbaty 1091
    }
108 pmbaty 1092
    Print(32, "book frequency (freq)..............%4.2f\n", book_weight_freq);
1093
    Print(32, "book static evaluation (eval)......%4.2f\n", book_weight_eval);
1094
    Print(32, "book learning (learn)..............%4.2f\n",
33 pmbaty 1095
        book_weight_learn);
1096
  }
1097
/*
1098
 ************************************************************
1099
 *                                                          *
1100
 *  "kibitz" command sets kibitz mode for ICS.  =1 will     *
1101
 *  kibitz mate announcements, =2 will kibitz scores and    *
1102
 *  other info, =3 will kibitz scores and PV, =4 adds the   *
1103
 *  list of book moves, =5 displays the PV after each       *
108 pmbaty 1104
 *  iteration completes.                                    *
33 pmbaty 1105
 *                                                          *
1106
 ************************************************************
1107
 */
1108
  else if (OptionMatch("kibitz", *args)) {
1109
    if (nargs < 2) {
1110
      printf("usage:  kibitz <level>\n");
1111
      return 1;
1112
    }
108 pmbaty 1113
    kibitz = Min(5, atoi(args[1]));
33 pmbaty 1114
  }
1115
/*
1116
 ************************************************************
1117
 *                                                          *
1118
 *  "learn" command enables/disables the learning           *
1119
 *  algorithm used in Crafty.  this is controlled by a      *
1120
 *  single variable that is either 0 or 1 (disabled or      *
1121
 *  enabled).                                               *
1122
 *                                                          *
1123
 *  "learn clear" clears all learning data in the opening   *
1124
 *  book, returning it to a state identical to when the     *
1125
 *  book was originally created.                            *
1126
 *                                                          *
1127
 ************************************************************
1128
 */
1129
  else if (OptionMatch("learn", *args)) {
1130
    if (nargs == 2) {
1131
      if (OptionMatch("clear", *(args + 1))) {
1132
        int index[32768], i, j, cluster;
1133
        unsigned char buf32[4];
1134
 
1135
        fseek(book_file, 0, SEEK_SET);
1136
        for (i = 0; i < 32768; i++) {
108 pmbaty 1137
          v = fread(buf32, 4, 1, book_file);
1138
          if (v <= 0)
1139
            perror("Option() fread error: ");
33 pmbaty 1140
          index[i] = BookIn32(buf32);
1141
        }
1142
        for (i = 0; i < 32768; i++)
1143
          if (index[i] > 0) {
1144
            fseek(book_file, index[i], SEEK_SET);
108 pmbaty 1145
            v = fread(buf32, 4, 1, book_file);
1146
            if (v <= 0)
1147
              perror("Option() fread error: ");
33 pmbaty 1148
            cluster = BookIn32(buf32);
108 pmbaty 1149
            if (cluster)
1150
              BookClusterIn(book_file, cluster, book_buffer);
33 pmbaty 1151
            for (j = 0; j < cluster; j++)
1152
              book_buffer[j].learn = 0.0;
1153
            fseek(book_file, index[i] + sizeof(int), SEEK_SET);
108 pmbaty 1154
            if (cluster)
1155
              BookClusterOut(book_file, cluster, book_buffer);
33 pmbaty 1156
          }
1157
      } else {
1158
        learning = atoi(args[1]);
1159
        learn = (learning > 0) ? 1 : 0;
1160
        if (learning)
108 pmbaty 1161
          Print(32, "book learning enabled {-%d,+%d}\n", learning, learning);
33 pmbaty 1162
        else
108 pmbaty 1163
          Print(32, "book learning disabled\n");
33 pmbaty 1164
      }
1165
    }
1166
  }
1167
/*
1168
 ************************************************************
1169
 *                                                          *
1170
 *  "level" command sets time controls [xboard compati-     *
1171
 *  bility.]                                                *
1172
 *                                                          *
1173
 ************************************************************
1174
 */
1175
  else if (OptionMatch("level", *args)) {
1176
    if (nargs < 4) {
1177
      printf("usage:  level <nmoves> <stime> <inc>\n");
1178
      return 1;
1179
    }
1180
    tc_moves = atoi(args[1]);
1181
    tc_time = atoi(args[2]) * 60;
1182
    if (strchr(args[2], ':'))
1183
      tc_time = tc_time + atoi(strchr(args[2], ':') + 1);
1184
    tc_time *= 100;
1185
    tc_increment = atoi(args[3]) * 100;
1186
    tc_time_remaining[white] = tc_time;
1187
    tc_time_remaining[black] = tc_time;
1188
    if (!tc_moves) {
1189
      tc_sudden_death = 1;
1190
      tc_moves = 1000;
1191
      tc_moves_remaining[white] = 1000;
1192
      tc_moves_remaining[black] = 1000;
1193
    } else
1194
      tc_sudden_death = 0;
1195
    if (tc_moves) {
1196
      tc_secondary_moves = tc_moves;
1197
      tc_secondary_time = tc_time;
1198
      tc_moves_remaining[white] = tc_moves;
1199
      tc_moves_remaining[black] = tc_moves;
1200
    }
1201
    if (!tc_sudden_death) {
108 pmbaty 1202
      Print(32, "%d moves/%d seconds primary time control\n", tc_moves,
33 pmbaty 1203
          tc_time / 100);
108 pmbaty 1204
      Print(32, "%d moves/%d seconds secondary time control\n",
33 pmbaty 1205
          tc_secondary_moves, tc_secondary_time / 100);
1206
      if (tc_increment)
108 pmbaty 1207
        Print(32, "increment %d seconds.\n", tc_increment / 100);
33 pmbaty 1208
    } else if (tc_sudden_death == 1) {
108 pmbaty 1209
      Print(32, " game/%d seconds primary time control\n", tc_time / 100);
33 pmbaty 1210
      if (tc_increment)
108 pmbaty 1211
        Print(32, "increment %d seconds.\n", tc_increment / 100);
33 pmbaty 1212
    }
1213
    if (adaptive_hash) {
108 pmbaty 1214
      uint64_t positions_per_move;
33 pmbaty 1215
      float percent;
1216
      int optimal_hash_size;
1217
 
1218
      TimeSet(think);
1219
      time_limit /= 100;
1220
      positions_per_move = time_limit * adaptive_hash / 16;
154 pmbaty 1221
      optimal_hash_size = positions_per_move * 16;
33 pmbaty 1222
      printf("optimal=%d\n", optimal_hash_size);
154 pmbaty 1223
      optimal_hash_size = Max(optimal_hash_size, adaptive_hash_min);
1224
      optimal_hash_size = Min(optimal_hash_size, adaptive_hash_max);
108 pmbaty 1225
      sprintf(buffer, "hash=%d\n", optimal_hash_size);
1226
      Option(tree);
33 pmbaty 1227
      percent =
1228
          (float) (optimal_hash_size -
1229
          adaptive_hash_min) / (float) (adaptive_hash_max -
1230
          adaptive_hash_min);
154 pmbaty 1231
      optimal_hash_size =
1232
          adaptive_hashp_min + percent * (adaptive_hashp_max -
1233
          adaptive_hashp_min);
1234
      optimal_hash_size = Max(optimal_hash_size, adaptive_hashp_min);
108 pmbaty 1235
      sprintf(buffer, "hashp=%d\n", optimal_hash_size);
1236
      Option(tree);
33 pmbaty 1237
    }
1238
  }
1239
/*
1240
 ************************************************************
1241
 *                                                          *
1242
 *  "linelength" sets the default line length to something  *
1243
 *  other than 80, if desired.  Setting this to a huge      *
1244
 *  number makes a PV print on one line for easier parsing  *
1245
 *  by automated scripts.                                   *
1246
 *                                                          *
1247
 ************************************************************
1248
 */
1249
  else if (OptionMatch("linelength", *args)) {
1250
    if (nargs > 2) {
1251
      printf("usage:  linelength <n>\n");
1252
      return 1;
1253
    }
1254
    if (nargs == 2)
1255
      line_length = atoi(args[1]);
1256
    printf("line length set to %d.\n", line_length);
1257
  }
1258
/*
1259
 ************************************************************
1260
 *                                                          *
1261
 *  "list" command allows the operator to add or remove     *
1262
 *  names from the various lists Crafty uses to recognize   *
1263
 *  and adapt to particular opponents.                      *
1264
 *                                                          *
1265
 *  list <listname> <player>                                *
1266
 *                                                          *
1267
 *  <listname> is one of AK, B, C, GM, IM, SP.              *
1268
 *                                                          *
1269
 *  The final parameter is a name to add  or remove.  If    *
1270
 *  you prepend a + to the name, that asks that the name be *
1271
 *  added to the list.  If you prepend a - to the name,     *
1272
 *  that asks that the name be removed from the list.  If   *
1273
 *  no name is given, the list is displayed.                *
1274
 *                                                          *
1275
 *  AK is the "auto-kibitz" list.  Crafty will kibitz info  *
1276
 *  on a chess server when playing any opponent in this     *
1277
 *  list.  This should only have computer names as humans   *
1278
 *  don't approve of kibitzes while they are playing.       *
1279
 *                                                          *
1280
 *  B identifies "blocker" players, those that try to       *
1281
 *  block the position and go for easy draws.  This makes   *
1282
 *  Crafty try much harder to prevent this from happening,  *
1283
 *  even at the expense of positional compensation.         *
1284
 *                                                          *
1285
 *  GM and IM identify titled players.  This affects how    *
1286
 *  and when Crafty resigns or offers/accepts draws.  For   *
1287
 *  GM players it will do so fairly early after the right   *
1288
 *  circumstances have been seen, for IM it delays a bit    *
1289
 *  longer as they are more prone to making a small error   *
1290
 *  that avoids the loss or draw.                           *
1291
 *                                                          *
1292
 *  SP is the "special player" option.  This is an extended *
1293
 *  version of the "list" command that allows you to        *
1294
 *  specify a special "start book" for a particular         *
1295
 *  opponent to make Crafty play specific openings against  *
1296
 *  that opponent, as well as allowing you to specify a     *
1297
 *  personality file to use against that specific opponent  *
1298
 *  when he is identified by the correct "name" command.    *
1299
 *                                                          *
1300
 *  For the SP list, the command is extended to use         *
1301
 *                                                          *
1302
 *  "list SP +player book=filename  personality=filename"   *
1303
 *                                                          *
1304
 *  For the SP list, the files specified must exist in the  *
1305
 *  current directory unless the bookpath and perspath      *
1306
 *  commands direct Crafty to look elsewhere.               *
1307
 *                                                          *
1308
 ************************************************************
1309
 */
1310
  else if (OptionMatch("list", *args)) {
1311
    char **targs;
1312
    char listname[5][3] = { "AK", "B", "GM", "IM", "SP" };
1313
    char **listaddr[] = { AK_list, B_list, GM_list,
1314
      IM_list, SP_list
1315
    };
108 pmbaty 1316
    int i, list, lastent = -1;
1317
 
33 pmbaty 1318
    targs = args;
1319
    for (list = 0; list < 5; list++) {
1320
      if (!strcmp(listname[list], args[1]))
1321
        break;
1322
    }
1323
    if (list > 4) {
1324
      printf("usage:  list AK|B|GM|IM|P|SP +name1 -name2 etc\n");
1325
      return 1;
1326
    }
1327
    nargs -= 2;
1328
    targs += 2;
1329
    if (nargs) {
1330
      while (nargs) {
1331
        if (targs[0][0] == '-') {
1332
          for (i = 0; i < 128; i++)
1333
            if (listaddr[list][i]) {
1334
              if (!strcmp(listaddr[list][i], targs[0] + 1)) {
1335
                free(listaddr[list][i]);
1336
                listaddr[list][i] = NULL;
108 pmbaty 1337
                Print(32, "%s removed from %s list.\n", targs[0] + 1,
33 pmbaty 1338
                    listname[list]);
1339
                break;
1340
              }
1341
            }
1342
        } else if (targs[0][0] == '+') {
1343
          for (i = 0; i < 128; i++)
1344
            if (listaddr[list][i]) {
1345
              if (!strcmp(listaddr[list][i], targs[0] + 1)) {
108 pmbaty 1346
                Print(32, "Warning: %s is already in %s list.\n",
33 pmbaty 1347
                    targs[0] + 1, listname[list]);
1348
                break;
1349
              }
1350
            }
1351
          for (i = 0; i < 128; i++)
1352
            if (listaddr[list][i] == NULL)
1353
              break;
1354
          if (i >= 128)
108 pmbaty 1355
            Print(32, "ERROR!  %s list is full at 128 entries\n",
33 pmbaty 1356
                listname[list]);
1357
          else {
1358
            listaddr[list][i] = malloc(strlen(targs[0]));
1359
            strcpy(listaddr[list][i], targs[0] + 1);
108 pmbaty 1360
            Print(32, "%s added to %s list.\n", targs[0] + 1, listname[list]);
33 pmbaty 1361
            if (list == 5)
1362
              lastent = i;
1363
          }
1364
        } else if (!strcmp(targs[0], "clear")) {
1365
          for (i = 0; i < 128; i++) {
1366
            free(listaddr[list][i]);
1367
            listaddr[list][i] = NULL;
1368
          }
1369
        } else if (!strcmp(targs[0], "book") && lastent != -1) {
1370
          char filename[256];
1371
          FILE *file;
1372
 
108 pmbaty 1373
          strcpy(filename, book_path);
1374
          strcat(filename, "/");
1375
          strcat(filename, targs[1]);
33 pmbaty 1376
          if (!strstr(args[2], ".bin"))
108 pmbaty 1377
            strcat(filename, ".bin");
1378
          file = fopen(filename, "r");
33 pmbaty 1379
          if (!file) {
1380
            Print(4095, "ERROR  book file %s can not be opened\n", filename);
1381
            break;
1382
          }
1383
          fclose(file);
1384
          SP_opening_filename[lastent] = malloc(strlen(filename) + 1);
1385
          strcpy(SP_opening_filename[lastent], filename);
1386
          nargs--;
1387
          targs++;
1388
        } else if (!strcmp(targs[0], "personality") && lastent != -1) {
1389
          char filename[256];
1390
          FILE *file;
1391
 
1392
          strcat(filename, targs[1]);
1393
          if (!strstr(args[2], ".cpf"))
1394
            strcat(filename, ".cpf");
1395
          file = fopen(filename, "r");
1396
          if (!file) {
1397
            Print(4095, "ERROR  personality file %s can not be opened\n",
1398
                filename);
1399
            break;
1400
          }
1401
          fclose(file);
1402
          SP_personality_filename[lastent] = malloc(strlen(filename) + 1);
1403
          strcpy(SP_personality_filename[lastent], filename);
1404
          nargs--;
1405
          targs++;
1406
        } else
1407
          printf("error, name must be preceeded by +/- flag.\n");
1408
        nargs--;
1409
        targs++;
1410
      }
1411
    } else {
108 pmbaty 1412
      Print(32, "%s List:\n", listname[list]);
33 pmbaty 1413
      for (i = 0; i < 128; i++) {
1414
        if (listaddr[list][i]) {
108 pmbaty 1415
          Print(32, "%s", listaddr[list][i]);
33 pmbaty 1416
          if (list == 5) {
1417
            if (SP_opening_filename[i])
108 pmbaty 1418
              Print(32, "  book=%s", SP_opening_filename[i]);
33 pmbaty 1419
            if (SP_personality_filename[i])
108 pmbaty 1420
              Print(32, "  personality=%s", SP_personality_filename[i]);
33 pmbaty 1421
          }
108 pmbaty 1422
          Print(32, "\n");
33 pmbaty 1423
        }
1424
      }
1425
    }
1426
  }
1427
/*
1428
 ************************************************************
1429
 *                                                          *
154 pmbaty 1430
 *  "lmp" command sets the formla parameters that produce   *
1431
 *  LMP pruning bounds array.                               *
1432
 *                                                          *
1433
 *     lmp <maxdepth> <base> <scale>                        *
1434
 *                                                          *
1435
 *  <maxdepth> is the max depth at which LMP is done.       *
1436
 *                                                          *
1437
 *  <base> is the base pruning move count.  The function is *
1438
 *  an exponential of the form x = base + f(y).  The        *
1439
 *  default is currently 3.                                 *
1440
 *                                                          *
1441
 *  <scale> is the exponent of the exponential function.    *
1442
 *  larger numbers produce more conservative (larger) move  *
1443
 *  counts.  Smaller values are more aggressive.  The       *
1444
 *  default is currently 1.9.                               *
1445
 *                                                          *
1446
 ************************************************************
1447
 */
1448
  else if (OptionMatch("lmp", *args)) {
1449
    int i;
1450
 
1451
    if ((nargs > 1 && nargs < 4) || nargs > 4) {
1452
      printf("usage:  lmp <maxdepth> <base> <scale>\n");
1453
      return 1;
1454
    }
1455
    if (nargs > 1) {
1456
      LMP_depth = atoi(args[1]);
1457
      LMP_base = atoi(args[2]);
1458
      LMP_scale = atof(args[3]);
1459
      InitializeLMP();
1460
    }
1461
    Print(32, "LMP depth=%d  base=%d  scale=%f\n", LMP_depth, LMP_base,
1462
        LMP_scale);
1463
    Print(32, "depth:  ");
1464
    for (i = 1; i < 16; i++)
1465
      Print(32, "%4d", i);
1466
    Print(32, "\n");
1467
    Print(32, "movcnt: ");
1468
    for (i = 1; i < 16; i++)
1469
      Print(32, "%4d", LMP[i]);
1470
    Print(32, "\n");
1471
  }
1472
/*
1473
 ************************************************************
1474
 *                                                          *
108 pmbaty 1475
 *  "lmr" command sets the formla parameters that produce   *
1476
 *  LMR reduction matrix.  The format is:                   *
1477
 *                                                          *
1478
 *     lmr <min> <max> <depth bias> <moves bias> <scale>    *
1479
 *                                                          *
1480
 *  <min> is the minimum LMR reduction.  This probably      *
1481
 *  should not be changed from 1, the default.              *
1482
 *                                                          *
1483
 *  <max> is the maximum LMR reduction.  If you adjust the  *
1484
 *  following values, you might need to increase this as it *
1485
 *  is an absolute clamp and no value can exceed this no    *
1486
 *  matter what the formula produces.                       *
1487
 *                                                          *
1488
 *  <depth_bias> is simply a multiplier that causes depth   *
1489
 *  to influence the reduction amount more or less (as the  *
1490
 *  value drops below the value used for <moves bias> below *
1491
 *  or as it is increased above <moves bias>.  The default  *
1492
 *  is 2.0.                                                 *
1493
 *                                                          *
1494
 *  <moves bias> is simply a multiplier that causes the     *
1495
 *  number of moves already searched to become more or less *
1496
 *  important than the remaining depth as above.  The       *
1497
 *  default is 1.0.                                         *
1498
 *                                                          *
1499
 *  <scale> is used to scale the formula back since it uses *
1500
 *  a logarithmic expression.  The basic idea is to adjust  *
1501
 *  the above two values to produce the desired "shape" of  *
1502
 *  the reduction matrix, then adjust this to change the    *
1503
 *  reduction amounts overall.  The default is 2.9.         *
1504
 *                                                          *
1505
 ************************************************************
1506
 */
1507
  else if (OptionMatch("lmr", *args)) {
1508
    int i, j;
1509
 
1510
    if ((nargs > 1 && nargs < 6) || nargs > 7) {
1511
      printf("usage:  lmr <min> <max> <depth bias> <move bias> <scale>\n");
1512
      return 1;
1513
    }
1514
    if (nargs > 1) {
1515
      LMR_min = atoi(args[1]);
1516
      LMR_max = Min(atoi(args[2]), 15);
1517
      LMR_db = atof(args[3]);
1518
      LMR_mb = atof(args[4]);
1519
      LMR_s = atof(args[5]);
154 pmbaty 1520
      InitializeLMR();
108 pmbaty 1521
    }
1522
    if (nargs > 6) {
1523
      char *axis = "|||||||||||depth left|||||||||||";
1524
 
1525
      Print(32,
1526
          "LMR values:  %d(min) %d(max) %.2f(depth) %.2f(moves) %.2f(scale).\n",
1527
          LMR_min, LMR_max, LMR_db, LMR_mb, LMR_s);
1528
      Print(32, "\n                 LMR reductions[depth][moves]\n");
1529
      Print(32, "  ----------------------moves searched-----------------\n");
1530
      Print(32, " |      ");
1531
      for (i = 0; i < 64; i += 1)
1532
        Print(32, "%3d", i);
1533
      Print(32, "\n");
1534
      for (i = 0; i < 32; i += 1) {
1535
        Print(32, " %c %3d: ", axis[i], i);
1536
        for (j = 0; j < 64; j += 1)
1537
          Print(32, " %2d", LMR[i][j]);
1538
        Print(32, "\n");
1539
      }
1540
    } else {
154 pmbaty 1541
      char *axis = "||depth left|||";
108 pmbaty 1542
 
1543
      Print(32,
1544
          "LMR values:  %d(min) %d(max) %.2f(depth) %.2f(moves) %.2f(scale).\n",
1545
          LMR_min, LMR_max, LMR_db, LMR_mb, LMR_s);
1546
      Print(32, "\n                 LMR reductions[depth][moves]\n");
1547
      Print(32, "  ----------------------moves searched------------------\n");
1548
      Print(32, " |      ");
1549
      for (i = 2; i < 64; i += 4)
1550
        Print(32, "%3d", i);
1551
      Print(32, "\n");
1552
      for (i = 3; i < 32; i += 2) {
1553
        Print(32, " %c %3d: ", axis[(i - 3) / 2], i);
1554
        for (j = 2; j < 64; j += 4)
1555
          Print(32, " %2d", LMR[i][j]);
1556
        Print(32, "\n");
1557
      }
1558
      Print(32, "    note:  table is shown compressed, each index is in\n");
1559
      Print(32, "    units of 1, all rows/columns are not shown above\n");
1560
    }
1561
  }
1562
/*
1563
 ************************************************************
1564
 *                                                          *
33 pmbaty 1565
 *   "load" command directs the program to read input from  *
1566
 *   a file until a "setboard" command is found  this       *
1567
 *   command is then executed, setting up the position for  *
1568
 *   a search.                                              *
1569
 *                                                          *
1570
 ************************************************************
1571
 */
1572
  else if (OptionMatch("load", *args)) {
1573
    char title[64];
1574
    char *readstat;
1575
    FILE *prob_file;
1576
 
1577
    if (thinking || pondering)
1578
      return 2;
1579
    nargs = ReadParse(buffer, args, " \t=");
1580
    if (nargs < 3) {
1581
      printf("usage:  input <filename> title\n");
1582
      return 1;
1583
    }
1584
    if (!(prob_file = fopen(args[1], "r"))) {
1585
      printf("file does not exist.\n");
1586
      return 1;
1587
    }
108 pmbaty 1588
    strcpy(title, args[2]);
33 pmbaty 1589
    while (!feof(prob_file)) {
1590
      readstat = fgets(buffer, 128, prob_file);
1591
      if (readstat) {
1592
        char *delim;
1593
 
1594
        delim = strchr(buffer, '\n');
1595
        if (delim)
1596
          *delim = 0;
1597
        delim = strchr(buffer, '\r');
1598
        if (delim)
1599
          *delim = ' ';
1600
      }
1601
      if (readstat == NULL)
1602
        break;
1603
      nargs = ReadParse(buffer, args, " \t;\n");
1604
      if (!strcmp(args[0], "title") && strstr(buffer, title))
1605
        break;
1606
    }
1607
    while (!feof(prob_file)) {
1608
      readstat = fgets(buffer, 128, prob_file);
1609
      if (readstat) {
1610
        char *delim;
1611
 
1612
        delim = strchr(buffer, '\n');
1613
        if (delim)
1614
          *delim = 0;
1615
        delim = strchr(buffer, '\r');
1616
        if (delim)
1617
          *delim = ' ';
1618
      }
1619
      if (readstat == NULL)
1620
        break;
1621
      nargs = ReadParse(buffer, args, " \t;\n");
1622
      if (!strcmp(args[0], "setboard")) {
108 pmbaty 1623
        Option(tree);
33 pmbaty 1624
        break;
1625
      }
1626
    }
154 pmbaty 1627
    fclose(prob_file);
33 pmbaty 1628
  }
1629
/*
1630
 ************************************************************
1631
 *                                                          *
1632
 *   "log" command turns log on/off, and also lets you view *
1633
 *   the end of the log or copy it to disk as needed.  To   *
1634
 *   view the end, simply type "log <n>" where n is the #   *
1635
 *   of lines you'd like to see (the last <n> lines).  You  *
1636
 *   can add a filename to the end and the output will go   *
1637
 *   to this file instead.                                  *
1638
 *                                                          *
1639
 ************************************************************
1640
 */
1641
  else if (OptionMatch("log", *args)) {
154 pmbaty 1642
    char filename[64];
33 pmbaty 1643
 
1644
    if (nargs < 2) {
1645
      printf("usage:  log on|off|n [filename]\n");
1646
      return 1;
1647
    }
1648
    if (!strcmp(args[1], "on")) {
1649
      int id;
1650
 
1651
      id = InitializeGetLogID();
108 pmbaty 1652
      sprintf(log_filename, "%s/log.%03d", log_path, id);
1653
      sprintf(history_filename, "%s/game.%03d", log_path, id);
1654
      log_file = fopen(log_filename, "w");
1655
      history_file = fopen(history_filename, "w+");
33 pmbaty 1656
    } else if (!strcmp(args[1], "off")) {
1657
      if (log_file)
1658
        fclose(log_file);
1659
      log_file = 0;
108 pmbaty 1660
      sprintf(filename, "%s/log.%03d", log_path, log_id - 1);
33 pmbaty 1661
      remove(filename);
108 pmbaty 1662
      sprintf(filename, "%s/game.%03d", log_path, log_id - 1);
33 pmbaty 1663
      remove(filename);
154 pmbaty 1664
    } else if (args[1][0] >= '0' && args[1][0] <= '9')
1665
      log_id = atoi(args[1]);
33 pmbaty 1666
  }
1667
/*
1668
 ************************************************************
1669
 *                                                          *
1670
 *   "memory" command is used to set the max memory to use  *
1671
 *   for hash and hashp combined.  This is an xboard        *
1672
 *   compatibility command, not normally used by players.   *
1673
 *                                                          *
1674
 ************************************************************
1675
 */
1676
  else if (OptionMatch("memory", *args)) {
1677
    uint64_t size;
1678
    size_t hmemory, pmemory;
1679
    if (nargs < 2) {
1680
      printf("usage:  memory <size>\n");
1681
      return 1;
1682
    }
108 pmbaty 1683
    if (allow_memory) {
1684
      size = (uint64_t) atoi(args[1]);
1685
      if (size == 0) {
1686
        Print(4095, "ERROR - memory size can not be zero\n");
1687
        return 1;
1688
      }
1689
      hmemory = (1ull) << MSB(size);
1690
      size &= ~hmemory;
1691
      pmemory = (1ull) << MSB(size);
1692
      sprintf(buffer, "hash %" PRIu64 "M\n", (uint64_t) hmemory);
1693
      Option(tree);
1694
      if (pmemory) {
1695
        sprintf(buffer, "hashp %" PRIu64 "M\n", (uint64_t) pmemory);
1696
        Option(tree);
1697
      }
1698
    } else
1699
      Print(4095, "WARNING - memory command ignored.\n");
33 pmbaty 1700
  }
1701
/*
1702
 ************************************************************
1703
 *                                                          *
1704
 *   "mode" command sets tournament mode or normal mode.    *
1705
 *   Tournament mode is used when Crafty is in a "real"     *
1706
 *   tournament.  It forces draw_score to 0, and makes      *
1707
 *   Crafty display the chess clock after each move.        *
1708
 *                                                          *
1709
 ************************************************************
1710
 */
1711
  else if (OptionMatch("mode", *args)) {
1712
    if (nargs > 1) {
1713
      if (!strcmp(args[1], "tournament")) {
1714
        mode = tournament_mode;
1715
        printf("use 'settc' command if a game is restarted after Crafty\n");
1716
        printf("has been terminated for any reason.\n");
1717
      } else if (!strcmp(args[1], "normal")) {
1718
        mode = normal_mode;
1719
        book_weight_learn = 1.0;
1720
        book_weight_freq = 1.0;
1721
        book_weight_eval = 0.5;
1722
      } else if (!strcmp(args[1], "match")) {
1723
        mode = normal_mode;
108 pmbaty 1724
        book_weight_learn = 1.0;
154 pmbaty 1725
        book_weight_freq = 0.2;
1726
        book_weight_eval = 0.1;
33 pmbaty 1727
      } else {
1728
        printf("usage:  mode normal|tournament|match\n");
1729
        mode = normal_mode;
108 pmbaty 1730
        book_weight_learn = 1.0;
1731
        book_weight_freq = 1.0;
1732
        book_weight_eval = 0.5;
33 pmbaty 1733
      }
1734
    }
1735
    if (mode == tournament_mode)
1736
      printf("tournament mode.\n");
1737
    else if (mode == normal_mode)
1738
      printf("normal mode.\n");
1739
  }
1740
/*
1741
 ************************************************************
1742
 *                                                          *
1743
 *   "name" command saves opponents name and writes it into *
1744
 *   logfile along with the date/time.  It also scans the   *
1745
 *   list of known computers and adjusts its opening book   *
1746
 *   to play less "risky" if it matches.  If the opponent   *
1747
 *   is in the GM list, it tunes the resignation controls   *
1748
 *   to resign earlier.  Ditto for other lists that are     *
1749
 *   used to recognize specific opponents and adjust things *
1750
 *   accordingly.                                           *
1751
 *                                                          *
1752
 ************************************************************
1753
 */
1754
  else if (OptionMatch("name", *args)) {
108 pmbaty 1755
    char *next;
33 pmbaty 1756
    int i;
1757
 
1758
    if (nargs < 2) {
1759
      printf("usage:  name <name>\n");
1760
      return 1;
1761
    }
1762
    if (game_wtm) {
108 pmbaty 1763
      strcpy(pgn_white, args[1]);
1764
      sprintf(pgn_black, "Crafty %s", version);
33 pmbaty 1765
    } else {
108 pmbaty 1766
      strcpy(pgn_black, args[1]);
1767
      sprintf(pgn_white, "Crafty %s", version);
33 pmbaty 1768
    }
108 pmbaty 1769
    Print(32, "Crafty %s vs %s\n", version, args[1]);
33 pmbaty 1770
    next = args[1];
1771
    while (*next) {
1772
      *next = tolower(*next);
1773
      next++;
1774
    }
1775
    if (mode != tournament_mode) {
1776
      for (i = 0; i < 128; i++)
1777
        if (AK_list[i] && !strcmp(AK_list[i], args[1])) {
1778
          kibitz = 4;
1779
          break;
1780
        }
1781
      for (i = 0; i < 128; i++)
1782
        if (GM_list[i] && !strcmp(GM_list[i], args[1])) {
108 pmbaty 1783
          Print(32, "playing a GM!\n");
33 pmbaty 1784
          book_selection_width = 3;
1785
          resign = Min(6, resign);
1786
          resign_count = 4;
1787
          draw_count = 4;
1788
          accept_draws = 1;
1789
          kibitz = 0;
1790
          break;
1791
        }
1792
      for (i = 0; i < 128; i++)
1793
        if (IM_list[i] && !strcmp(IM_list[i], args[1])) {
108 pmbaty 1794
          Print(32, "playing an IM!\n");
33 pmbaty 1795
          book_selection_width = 4;
1796
          resign = Min(9, resign);
1797
          resign_count = 5;
1798
          draw_count = 4;
1799
          accept_draws = 1;
1800
          kibitz = 0;
1801
          break;
1802
        }
1803
      for (i = 0; i < 128; i++)
1804
        if (SP_list[i] && !strcmp(SP_list[i], args[1])) {
1805
          FILE *normal_bs_file = books_file;
1806
 
108 pmbaty 1807
          Print(32, "playing a special player!\n");
33 pmbaty 1808
          if (SP_opening_filename[i]) {
1809
            books_file = fopen(SP_opening_filename[i], "rb");
1810
            if (!books_file) {
1811
              Print(4095, "Error!  unable to open %s for player %s.\n",
1812
                  SP_opening_filename[i], SP_list[i]);
1813
              books_file = normal_bs_file;
1814
            }
1815
          }
1816
          if (SP_personality_filename[i]) {
108 pmbaty 1817
            sprintf(buffer, "personality load %s\n",
33 pmbaty 1818
                SP_personality_filename[i]);
108 pmbaty 1819
            Option(tree);
33 pmbaty 1820
          }
1821
          break;
1822
        }
1823
    }
1824
    printf("tellicsnoalias kibitz Hello from Crafty v%s! (%d cpus)\n",
1825
        version, Max(1, smp_max_threads));
1826
  }
1827
/*
1828
 ************************************************************
1829
 *                                                          *
1830
 *  "new" command initializes for a new game.               *
1831
 *                                                          *
1832
 ************************************************************
1833
 */
1834
  else if (OptionMatch("new", *args)) {
154 pmbaty 1835
    Print(4095, "NOTICE:  ""new"" command not implemented, please exit and\n");
1836
    Print(4095, "restart crafty to re-initialize everything for a new game\n");
1837
    return 1;
33 pmbaty 1838
  }
1839
/*
1840
 ************************************************************
1841
 *                                                          *
108 pmbaty 1842
 *  "noise" command sets a minimum limit on time searched   *
1843
 *  before we start to display the normal search output.    *
1844
 *  With today's hardware and deep searches, it is easy to  *
1845
 *  get "swamped" with output.  Using "noise" you can say   *
1846
 *  "hold the output until you have searched for <x> time   *
1847
 *  (where time can be x.xx seconds or just x seconds.)     *
33 pmbaty 1848
 *                                                          *
1849
 ************************************************************
1850
 */
1851
  else if (OptionMatch("noise", *args)) {
1852
    if (nargs < 2) {
1853
      printf("usage:  noise <n>\n");
1854
      return 1;
1855
    }
154 pmbaty 1856
    noise_level = atof(args[1]) * 100;
108 pmbaty 1857
    Print(32, "noise level set to %.2f seconds.\n",
1858
        (float) noise_level / 100.0);
33 pmbaty 1859
  }
1860
/*
1861
 ************************************************************
1862
 *                                                          *
108 pmbaty 1863
 *  "null" command sets the minimum null-move reduction and *
1864
 *  a value that is used to compute the max reduction.      *
33 pmbaty 1865
 *                                                          *
108 pmbaty 1866
 *     null <min> <divisor>                                 *
1867
 *                                                          *
1868
 *  <min> is the minimum null move reduction.  The default  *
1869
 *  is 3 which is pretty reliable.                          *
1870
 *                                                          *
1871
 *  <divisor> increases the null move by the following      *
1872
 *  simple formula:                                         *
1873
 *                                                          *
1874
 *    null_reduction = min + depth / divisor                *
1875
 *                                                          *
1876
 *  The default value is currently 10, which will increase  *
1877
 *  R (null-move reduction) by one at any position where    *
1878
 *  depth >= 10 and < 20.  Or by two when depth > 20.  Etc. *
1879
 *                                                          *
33 pmbaty 1880
 ************************************************************
1881
 */
108 pmbaty 1882
  else if (OptionMatch("null", *args)) {
1883
 
1884
    if (nargs > 3) {
1885
      printf("usage:  null <min> <divisor>\n");
33 pmbaty 1886
      return 1;
1887
    }
108 pmbaty 1888
    if (nargs > 1) {
1889
      null_depth = atoi(args[1]);
1890
      null_divisor = atoi(args[2]);
1891
    }
1892
    Print(32, "null move:  R = %d + depth / %d\n", null_depth, null_divisor);
33 pmbaty 1893
  }
1894
/*
1895
 ************************************************************
1896
 *                                                          *
108 pmbaty 1897
 *  "otim" command sets the opponent's time remaining.      *
33 pmbaty 1898
 *  This is used to determine if the opponent is in time    *
1899
 *  trouble, and is factored into the draw score if he is.  *
1900
 *                                                          *
1901
 ************************************************************
1902
 */
108 pmbaty 1903
  else if (OptionMatch("otim", *args)) {
33 pmbaty 1904
    if (nargs < 2) {
1905
      printf("usage:  otime <time(unit=.01 secs))>\n");
1906
      return 1;
1907
    }
108 pmbaty 1908
    tc_time_remaining[game_wtm] = atoi(args[1]);
33 pmbaty 1909
    if (log_file && time_limit > 99)
1910
      fprintf(log_file, "time remaining: %s (opponent).\n",
108 pmbaty 1911
          DisplayTime(tc_time_remaining[game_wtm]));
1912
    if (call_flag && xboard && tc_time_remaining[game_wtm] < 1) {
33 pmbaty 1913
      if (crafty_is_white)
108 pmbaty 1914
        Print(32, "1-0 {Black ran out of time}\n");
33 pmbaty 1915
      else
108 pmbaty 1916
        Print(32, "0-1 {White ran out of time}\n");
33 pmbaty 1917
    }
1918
  }
1919
/*
1920
 ************************************************************
1921
 *                                                          *
1922
 *  "output" command sets long or short algebraic output.   *
1923
 *  Long is Ng1f3, while short is simply Nf3.               *
1924
 *                                                          *
1925
 ************************************************************
1926
 */
1927
  else if (OptionMatch("output", *args)) {
1928
    if (nargs < 2) {
1929
      printf("usage:  output long|short\n");
1930
      return 1;
1931
    }
1932
    if (!strcmp(args[1], "long"))
1933
      output_format = 1;
1934
    else if (!strcmp(args[1], "short"))
1935
      output_format = 0;
1936
    else
1937
      printf("usage:  output long|short\n");
1938
    if (output_format == 1)
108 pmbaty 1939
      Print(32, "output moves in long algebraic format\n");
33 pmbaty 1940
    else if (output_format == 0)
108 pmbaty 1941
      Print(32, "output moves in short algebraic format\n");
33 pmbaty 1942
  }
1943
/*
1944
 ************************************************************
1945
 *                                                          *
1946
 *  "personality" command is used to adjust the eval terms  *
1947
 *  and search options to modify the way Crafty plays.      *
1948
 *                                                          *
1949
 ************************************************************
1950
 */
1951
  else if (OptionMatch("personality", *args)) {
1952
    int i, j, param, index, value;
1953
 
1954
/*
1955
 ************************************************************
1956
 *                                                          *
1957
 *  Handle the "personality list" command and dump every-   *
1958
 *  thing the user can modify.                              *
1959
 *                                                          *
1960
 ************************************************************
1961
 */
1962
    if (nargs == 2 && !strcmp(args[1], "list")) {
1963
      printf("\n");
1964
      for (i = 0; i < 256; i++) {
1965
        if (!personality_packet[i].description)
1966
          continue;
1967
        if (personality_packet[i].value) {
1968
          switch (personality_packet[i].type) {
1969
            case 1:
1970
              printf("%3d  %s %7d\n", i, personality_packet[i].description,
108 pmbaty 1971
                  *(int *) personality_packet[i].value);
33 pmbaty 1972
              break;
1973
            case 2:
1974
              printf("%3d  %s %7d (mg) %7d (eg)\n", i,
1975
                  personality_packet[i].description,
108 pmbaty 1976
                  ((int *) personality_packet[i].value)[mg],
1977
                  ((int *) personality_packet[i].value)[eg]);
33 pmbaty 1978
              break;
1979
            case 3:
108 pmbaty 1980
              printf("%3d  %s %7.2f\n", i, personality_packet[i].description,
1981
                  *(double *) personality_packet[i].value);
33 pmbaty 1982
              break;
1983
            case 4:
108 pmbaty 1984
              printf("%3d  %s    ", i, personality_packet[i].description);
1985
              for (j = 0; j < personality_packet[i].size; j++)
1986
                printf("%4d", ((int *) personality_packet[i].value)[j]);
1987
              printf("\n");
33 pmbaty 1988
              break;
1989
          }
1990
        } else {
1991
          printf("==================================================\n");
1992
          printf("=         %s  =\n", personality_packet[i].description);
1993
          printf("==================================================\n");
1994
        }
1995
      }
1996
      printf("\n");
1997
      return 1;
1998
    }
1999
/*
2000
 ************************************************************
2001
 *                                                          *
2002
 *  Handle the "personality load" command and read in the   *
2003
 *  specified *.cpf file.                                   *
2004
 *                                                          *
2005
 ************************************************************
2006
 */
2007
    if (!strcmp(args[1], "load")) {
2008
      FILE *file;
2009
      char filename[256];
2010
 
108 pmbaty 2011
      strcpy(filename, args[2]);
33 pmbaty 2012
      if (!strstr(filename, ".cpf"))
108 pmbaty 2013
        strcat(filename, ".cpf");
2014
      Print(32, "Loading personality file %s\n", filename);
2015
      if ((file = fopen(filename, "r+"))) {
33 pmbaty 2016
        while (fgets(buffer, 4096, file)) {
2017
          char *delim;
2018
 
2019
          delim = strchr(buffer, '\n');
2020
          if (delim)
2021
            *delim = 0;
2022
          delim = strstr(buffer, "->");
2023
          if (delim)
2024
            *delim = 0;
2025
          delim = strchr(buffer, '\r');
2026
          if (delim)
2027
            *delim = ' ';
108 pmbaty 2028
          Option(tree);
33 pmbaty 2029
        }
2030
        fclose(file);
2031
      }
2032
      return 1;
2033
    }
2034
/*
2035
 ************************************************************
2036
 *                                                          *
2037
 *  Handle the "personality save" command and dump every-   *
2038
 *  thing that can be modified to a file.                   *
2039
 *                                                          *
2040
 ************************************************************
2041
 */
2042
    if (nargs == 3 && !strcmp(args[1], "save")) {
2043
      char filename[256];
2044
      FILE *file;
2045
 
108 pmbaty 2046
      strcpy(filename, args[2]);
33 pmbaty 2047
      if (!strstr(filename, ".cpf"))
108 pmbaty 2048
        strcat(filename, ".cpf");
2049
      file = fopen(filename, "w");
33 pmbaty 2050
      if (!file) {
2051
        printf("ERROR.  Unable to open %s for writing\n", args[2]);
2052
        return 1;
2053
      }
2054
      printf("saving to file \"%s\"\n", filename);
2055
      fprintf(file, "# Crafty v%s personality file\n", version);
2056
      for (i = 0; i < 256; i++) {
2057
        if (!personality_packet[i].description)
2058
          continue;
2059
        if (personality_packet[i].value) {
2060
          if (personality_packet[i].size <= 1)
2061
            fprintf(file, "personality %3d %7d\n", i,
108 pmbaty 2062
                *((int *) personality_packet[i].value));
33 pmbaty 2063
          else if (personality_packet[i].size > 1) {
2064
            fprintf(file, "personality %3d ", i);
2065
            for (j = 0; j < personality_packet[i].size; j++)
108 pmbaty 2066
              fprintf(file, "%d ", ((int *) personality_packet[i].value)[j]);
33 pmbaty 2067
            fprintf(file, "\n");
2068
          }
2069
        }
2070
      }
2071
      fprintf(file, "exit\n");
2072
      fclose(file);
2073
      return 1;
2074
    }
2075
/*
2076
 ************************************************************
2077
 *                                                          *
2078
 *  Handle the "personality index val" command that changes *
2079
 *  only those personality terms that are scalars.          *
2080
 *                                                          *
2081
 ************************************************************
2082
 */
2083
    param = atoi(args[1]);
2084
    value = atoi(args[2]);
2085
    if (!personality_packet[param].value) {
2086
      Print(4095, "ERROR.  evaluation term %d is not defined\n", param);
2087
      return 1;
2088
    }
2089
    if (personality_packet[param].size == 0) {
2090
      if (nargs > 3) {
2091
        printf("this eval term requires exactly 1 value.\n");
2092
        return 1;
2093
      }
108 pmbaty 2094
      *(int *) personality_packet[param].value = value;
33 pmbaty 2095
    }
2096
/*
2097
 ************************************************************
2098
 *                                                          *
2099
 *  Handle the "personality index v1 v2 .. vn" command that *
2100
 *  changes eval terms that are vectors.                    *
2101
 *                                                          *
2102
 ************************************************************
2103
 */
2104
    else {
2105
      index = nargs - 2;
2106
      if (index != personality_packet[param].size) {
2107
        printf
2108
            ("this eval term (%s [%d]) requires exactly %d values, found %d.\n",
2109
            personality_packet[param].description, param,
2110
            Abs(personality_packet[param].size), index);
2111
        return 1;
2112
      }
2113
      for (i = 0; i < index; i++)
108 pmbaty 2114
        ((int *) personality_packet[param].value)[i] = atoi(args[i + 2]);
33 pmbaty 2115
    }
2116
    InitializeKingSafety();
2117
  }
2118
/*
2119
 ************************************************************
2120
 *                                                          *
2121
 *  "bookpath", "logpath" and "tbpath" set the default      *
2122
 *  paths to locate or save these files.                    *
2123
 *                                                          *
2124
 ************************************************************
2125
 */
2126
  else if (OptionMatch("logpath", *args) || OptionMatch("bookpath", *args)
2127
      || OptionMatch("tbpath", *args)) {
2128
    if (OptionMatch("logpath", *args) || OptionMatch("bookpath", *args)) {
2129
      if (log_file)
2130
        Print(4095, "ERROR -- this must be used on command line only\n");
2131
    }
2132
    nargs = ReadParse(buffer, args, " \t=");
2133
    if (nargs < 2) {
2134
      printf("usage:  bookpath|perspath|logpath|tbpath <path>\n");
2135
      return 1;
2136
    }
2137
    if (!strchr(args[1], '(')) {
2138
      if (strstr(args[0], "bookpath"))
108 pmbaty 2139
        strcpy(book_path, args[1]);
33 pmbaty 2140
      else if (strstr(args[0], "logpath"))
108 pmbaty 2141
        strcpy(log_path, args[1]);
154 pmbaty 2142
#if defined(SYZYGY)
33 pmbaty 2143
      else if (strstr(args[0], "tbpath"))
108 pmbaty 2144
        strcpy(tb_path, args[1]);
33 pmbaty 2145
#endif
154 pmbaty 2146
 
33 pmbaty 2147
    } else {
2148
      if (strchr(args[1], ')')) {
2149
        *strchr(args[1], ')') = 0;
2150
        if (strstr(args[0], "bookpath"))
108 pmbaty 2151
          strcpy(book_path, args[1] + 1);
33 pmbaty 2152
        else if (strstr(args[0], "logpath"))
108 pmbaty 2153
          strcpy(log_path, args[1] + 1);
154 pmbaty 2154
#if defined(SYZYGY)
33 pmbaty 2155
        else if (strstr(args[0], "tbpath"))
108 pmbaty 2156
          strcpy(tb_path, args[1] + 1);
33 pmbaty 2157
#endif
2158
      } else
2159
        Print(4095, "ERROR multiple paths must be enclosed in ( and )\n");
2160
    }
2161
  }
2162
/*
2163
 ************************************************************
2164
 *                                                          *
2165
 *  "perf" command turns times move generator/make_move.    *
2166
 *                                                          *
2167
 ************************************************************
2168
 */
2169
#define PERF_CYCLES 4000000
2170
  else if (OptionMatch("perf", *args)) {
108 pmbaty 2171
    int i, clock_before, clock_after;
2172
    unsigned *mv;
33 pmbaty 2173
    float time_used;
2174
 
2175
    if (thinking || pondering)
2176
      return 2;
2177
    clock_before = clock();
2178
    while (clock() == clock_before);
2179
    clock_before = clock();
2180
    for (i = 0; i < PERF_CYCLES; i++) {
2181
      tree->last[1] = GenerateCaptures(tree, 0, game_wtm, tree->last[0]);
2182
      tree->last[1] = GenerateNoncaptures(tree, 0, game_wtm, tree->last[1]);
2183
    }
2184
    clock_after = clock();
2185
    time_used =
2186
        ((float) clock_after - (float) clock_before) / (float) CLOCKS_PER_SEC;
2187
    printf("generated %d moves, time=%.2f seconds\n",
2188
        (int) (tree->last[1] - tree->last[0]) * PERF_CYCLES, time_used);
2189
    printf("generated %d moves per second\n",
2190
        (int) (((float) (PERF_CYCLES * (tree->last[1] -
2191
                        tree->last[0]))) / time_used));
2192
    clock_before = clock();
2193
    while (clock() == clock_before);
2194
    clock_before = clock();
2195
    for (i = 0; i < PERF_CYCLES; i++) {
2196
      tree->last[1] = GenerateCaptures(tree, 0, game_wtm, tree->last[0]);
2197
      tree->last[1] = GenerateNoncaptures(tree, 0, game_wtm, tree->last[1]);
2198
      for (mv = tree->last[0]; mv < tree->last[1]; mv++) {
108 pmbaty 2199
        MakeMove(tree, 0, game_wtm, *mv);
2200
        UnmakeMove(tree, 0, game_wtm, *mv);
33 pmbaty 2201
      }
2202
    }
2203
    clock_after = clock();
2204
    time_used =
2205
        ((float) clock_after - (float) clock_before) / (float) CLOCKS_PER_SEC;
2206
    printf("generated/made/unmade %d moves, time=%.2f seconds\n",
2207
        (int) (tree->last[1] - tree->last[0]) * PERF_CYCLES, time_used);
2208
    printf("generated/made/unmade %d moves per second\n",
2209
        (int) (((float) (PERF_CYCLES * (tree->last[1] -
2210
                        tree->last[0]))) / time_used));
2211
  }
2212
/*
2213
 ************************************************************
2214
 *                                                          *
2215
 *  "perft" command turns tests move generator/make_move.   *
2216
 *                                                          *
2217
 ************************************************************
2218
 */
2219
  else if (OptionMatch("perft", *args)) {
108 pmbaty 2220
    float time_used;
33 pmbaty 2221
    int i, clock_before, clock_after;
2222
 
2223
    if (thinking || pondering)
2224
      return 2;
2225
    clock_before = clock();
2226
    while (clock() == clock_before);
2227
    clock_before = clock();
2228
    if (nargs < 2) {
108 pmbaty 2229
      printf("usage:  perft <depth>\n");
33 pmbaty 2230
      return 1;
2231
    }
2232
    tree->status[1] = tree->status[0];
2233
    tree->last[0] = tree->move_list;
2234
    i = atoi(args[1]);
2235
    if (i <= 0) {
108 pmbaty 2236
      Print(32, "usage:  perft <maxply>\n");
33 pmbaty 2237
      return 1;
2238
    }
2239
    total_moves = 0;
2240
    OptionPerft(tree, 1, i, game_wtm);
2241
    clock_after = clock();
2242
    time_used =
2243
        ((float) clock_after - (float) clock_before) / (float) CLOCKS_PER_SEC;
2244
    printf("total moves=%" PRIu64 "  time=%.2f\n", total_moves, time_used);
2245
  }
2246
/*
2247
 ************************************************************
2248
 *                                                          *
2249
 *  "pgn" command sets the various PGN header files.        *
2250
 *                                                          *
2251
 ************************************************************
2252
 */
2253
  else if (OptionMatch("pgn", *args)) {
2254
    int i;
2255
 
2256
    if (nargs < 3) {
2257
      printf("usage:  pgn <tag> <value>\n");
2258
      return 1;
2259
    }
2260
    if (!strcmp(args[1], "Event")) {
2261
      pgn_event[0] = 0;
2262
      for (i = 2; i < nargs; i++) {
108 pmbaty 2263
        strcpy(pgn_event + strlen(pgn_event), args[i]);
2264
        strcpy(pgn_event + strlen(pgn_event), " ");
33 pmbaty 2265
      }
2266
    } else if (!strcmp(args[1], "Site")) {
2267
      pgn_site[0] = 0;
2268
      for (i = 2; i < nargs; i++) {
108 pmbaty 2269
        strcpy(pgn_site + strlen(pgn_site), args[i]);
2270
        strcpy(pgn_site + strlen(pgn_site), " ");
33 pmbaty 2271
      }
2272
    } else if (!strcmp(args[1], "Round")) {
2273
      pgn_round[0] = 0;
108 pmbaty 2274
      strcpy(pgn_round, args[2]);
33 pmbaty 2275
    } else if (!strcmp(args[1], "White")) {
2276
      pgn_white[0] = 0;
2277
      for (i = 2; i < nargs; i++) {
108 pmbaty 2278
        strcpy(pgn_white + strlen(pgn_white), args[i]);
2279
        strcpy(pgn_white + strlen(pgn_white), " ");
33 pmbaty 2280
      }
2281
    } else if (!strcmp(args[1], "WhiteElo")) {
2282
      pgn_white_elo[0] = 0;
108 pmbaty 2283
      strcpy(pgn_white_elo, args[2]);
33 pmbaty 2284
    } else if (!strcmp(args[1], "Black")) {
2285
      pgn_black[0] = 0;
2286
      for (i = 2; i < nargs; i++) {
108 pmbaty 2287
        strcpy(pgn_black + strlen(pgn_black), args[i]);
2288
        strcpy(pgn_black + strlen(pgn_black), " ");
33 pmbaty 2289
      }
2290
    } else if (!strcmp(args[1], "BlackElo")) {
2291
      pgn_black_elo[0] = 0;
108 pmbaty 2292
      strcpy(pgn_black_elo, args[2]);
33 pmbaty 2293
    }
2294
  }
2295
/*
2296
 ************************************************************
2297
 *                                                          *
2298
 *  "ping" command simply echos the argument back to xboard *
2299
 *  to let it know all previous commands have been executed *
2300
 *  and we are ready for whatever is next.                  *
2301
 *                                                          *
2302
 ************************************************************
2303
 */
108 pmbaty 2304
  else if (OptionMatch("ping", *args)) {
2305
    if (pondering)
2306
      Print(-1, "pong %s\n", args[1]);
2307
    else
33 pmbaty 2308
      pong = atoi(args[1]);
2309
  }
2310
/*
2311
 ************************************************************
2312
 *                                                          *
2313
 *  "playother" command says "position is set up, we are    *
2314
 *  waiting on the opponent to move, ponder if you want to  *
2315
 *  do so.                                                  *
2316
 *                                                          *
2317
 ************************************************************
2318
 */
108 pmbaty 2319
  else if (OptionMatch("playother", *args)) {
33 pmbaty 2320
    force = 0;
2321
  }
2322
/*
2323
 ************************************************************
2324
 *                                                          *
2325
 *  "ponder" command toggles pondering off/on or sets a     *
2326
 *  move to ponder.                                         *
2327
 *                                                          *
2328
 ************************************************************
2329
 */
2330
  else if (OptionMatch("ponder", *args)) {
2331
    if (thinking || pondering)
2332
      return 2;
2333
    if (nargs < 2) {
2334
      printf("usage:  ponder off|on|<move>\n");
2335
      return 1;
2336
    }
2337
    if (!strcmp(args[1], "on")) {
2338
      ponder = 1;
108 pmbaty 2339
      Print(32, "pondering enabled.\n");
33 pmbaty 2340
    } else if (!strcmp(args[1], "off")) {
2341
      ponder = 0;
108 pmbaty 2342
      Print(32, "pondering disabled.\n");
33 pmbaty 2343
    } else {
108 pmbaty 2344
      ponder_move = InputMove(tree, 0, game_wtm, 0, 0, args[1]);
33 pmbaty 2345
      last_pv.pathd = 0;
2346
      last_pv.pathl = 0;
2347
    }
2348
  }
2349
/*
2350
 ************************************************************
2351
 *                                                          *
2352
 *  "post/nopost" command sets/resets "show thinking" mode  *
2353
 *  for xboard compatibility.                               *
2354
 *                                                          *
2355
 ************************************************************
2356
 */
108 pmbaty 2357
  else if (OptionMatch("post", *args)) {
33 pmbaty 2358
    post = 1;
108 pmbaty 2359
  } else if (OptionMatch("nopost", *args)) {
33 pmbaty 2360
    post = 0;
2361
  }
2362
/*
2363
 ************************************************************
2364
 *                                                          *
2365
 *  "protover" command is sent by xboard to identify the    *
2366
 *  xboard protocol version and discover what the engine    *
2367
 *  can handle.                                             *
2368
 *                                                          *
2369
 ************************************************************
2370
 */
108 pmbaty 2371
  else if (OptionMatch("protover", *args)) {
33 pmbaty 2372
    int pversion = atoi(args[1]);
2373
 
2374
    if (pversion >= 1 && pversion <= 3) {
2375
      if (pversion >= 2) {
108 pmbaty 2376
        Print(-1, "feature ping=1 setboard=1 san=1 time=1 draw=1\n");
154 pmbaty 2377
        Print(-1, "feature sigint=0 sigterm=0 reuse=0 analyze=1\n");
108 pmbaty 2378
        Print(-1, "feature myname=\"Crafty-%s\" name=1\n", version);
2379
        Print(-1, "feature playother=1 colors=0 memory=%d\n", allow_memory);
33 pmbaty 2380
#if (CPUS > 1)
108 pmbaty 2381
        Print(-1, "feature smp=%d\n", allow_cores);
33 pmbaty 2382
#endif
108 pmbaty 2383
        Print(-1, "feature variants=\"normal,nocastle\"\n");
2384
        Print(-1, "feature done=1\n");
2385
        xboard_done = 1;
33 pmbaty 2386
      }
2387
    } else
2388
      Print(4095, "ERROR, bogus xboard protocol version received.\n");
2389
  }
2390
/*
2391
 ************************************************************
2392
 *                                                          *
2393
 *  "random" command is ignored. [xboard compatibility]     *
2394
 *                                                          *
2395
 ************************************************************
2396
 */
2397
  else if (OptionMatch("random", *args)) {
2398
    return xboard;
2399
  }
2400
/*
2401
 ************************************************************
2402
 *                                                          *
2403
 *  "rating" is used by xboard to set Crafty's rating and   *
2404
 *  the opponent's rating, which is used by the learning    *
2405
 *  functions.                                              *
2406
 *                                                          *
2407
 ************************************************************
2408
 */
2409
  else if (OptionMatch("rating", *args)) {
2410
    int rd;
108 pmbaty 2411
 
33 pmbaty 2412
    if (nargs < 3) {
2413
      printf("usage:  rating <Crafty> <opponent>\n");
2414
      return 1;
2415
    }
2416
    crafty_rating = atoi(args[1]);
2417
    opponent_rating = atoi(args[2]);
2418
    if (crafty_rating == 0 && opponent_rating == 0) {
2419
      crafty_rating = 2500;
2420
      opponent_rating = 2300;
2421
    }
108 pmbaty 2422
    if (dynamic_draw_score) {
2423
      rd = opponent_rating - crafty_rating;
2424
      rd = Max(Min(rd, 300), -300);
2425
      abs_draw_score = rd / 8;
2426
      if (log_file) {
2427
        fprintf(log_file, "Crafty's rating: %d.\n", crafty_rating);
2428
        fprintf(log_file, "opponent's rating: %d.\n", opponent_rating);
2429
        fprintf(log_file, "draw score: %d.\n", abs_draw_score);
2430
      }
33 pmbaty 2431
    }
2432
  }
2433
/*
2434
 ************************************************************
2435
 *                                                          *
2436
 *  "remove" command backs up the game one whole move,      *
2437
 *  leaving the opponent still on move.  It's intended for  *
2438
 *  xboard compatibility, but works in any mode.            *
2439
 *                                                          *
2440
 ************************************************************
2441
 */
108 pmbaty 2442
  else if (OptionMatch("remove", *args)) {
33 pmbaty 2443
    if (thinking || pondering)
2444
      return 2;
2445
    move_number--;
108 pmbaty 2446
    sprintf(buffer, "reset %d", move_number);
2447
    Option(tree);
33 pmbaty 2448
  }
2449
/*
2450
 ************************************************************
2451
 *                                                          *
2452
 *  "read" reads game moves in and makes them.  This can    *
2453
 *  be used in two ways:  (1) type "read" and then start    *
2454
 *  entering moves;  type "exit" when done;  (2) type       *
2455
 *  "read <filename>" to read moves in from <filename>.     *
2456
 *  Note that read will attempt to skip over "non-move"     *
2457
 *  text and try to extract moves if it can.                *
2458
 *                                                          *
2459
 *  Note that "reada" appends to the existing position,     *
2460
 *  while "read" resets the board to the start position     *
2461
 *  before reading moves.                                   *
2462
 *                                                          *
2463
 ************************************************************
2464
 */
2465
  else if (OptionMatch("read", *args) || OptionMatch("reada", *args)) {
108 pmbaty 2466
    FILE *read_input = 0;
33 pmbaty 2467
    int append, move, readstat;
2468
 
2469
    if (thinking || pondering)
2470
      return 2;
2471
    nargs = ReadParse(buffer, args, " \t=");
2472
    if (!strcmp("reada", *args))
2473
      append = 1;
2474
    else
2475
      append = 0;
2476
    ponder_move = 0;
2477
    last_pv.pathd = 0;
2478
    last_pv.pathl = 0;
2479
    if (nargs > 1) {
108 pmbaty 2480
      if (!(read_input = fopen(args[1], "r"))) {
33 pmbaty 2481
        printf("file %s does not exist.\n", args[1]);
2482
        return 1;
2483
      }
2484
    } else {
2485
      printf("type \"exit\" to terminate.\n");
2486
      read_input = stdin;
2487
    }
2488
    if (!append) {
2489
      InitializeChessBoard(tree);
2490
      game_wtm = 1;
2491
      move_number = 1;
2492
      tc_moves_remaining[white] = tc_moves;
2493
      tc_moves_remaining[black] = tc_moves;
2494
    }
2495
/*
2496
 step 1:  read in the PGN tags.
2497
 */
2498
    readstat = ReadPGN(0, 0);
2499
    do {
2500
      if (read_input == stdin) {
2501
        if (game_wtm)
2502
          printf("read.White(%d): ", move_number);
2503
        else
2504
          printf("read.Black(%d): ", move_number);
2505
        fflush(stdout);
2506
      }
2507
      readstat = ReadPGN(read_input, 0);
2508
    } while (readstat == 1);
2509
    if (readstat < 0)
2510
      return 1;
2511
/*
2512
 step 2:  read in the moves.
2513
 */
2514
    do {
2515
      move = 0;
2516
      move = ReadNextMove(tree, buffer, 0, game_wtm);
2517
      if (move) {
2518
        if (read_input != stdin) {
108 pmbaty 2519
          printf("%s ", OutputMove(tree, 0, game_wtm, move));
33 pmbaty 2520
          if (!(move_number % 8) && Flip(game_wtm))
2521
            printf("\n");
2522
        }
2523
        fseek(history_file, ((move_number - 1) * 2 + 1 - game_wtm) * 10,
2524
            SEEK_SET);
108 pmbaty 2525
        fprintf(history_file, "%9s\n", OutputMove(tree, 0, game_wtm, move));
2526
        MakeMoveRoot(tree, game_wtm, move);
2527
        TimeAdjust(game_wtm, 0);
33 pmbaty 2528
#if defined(DEBUG)
2529
        ValidatePosition(tree, 1, move, "Option()");
2530
#endif
2531
      } else if (!read_input)
2532
        printf("illegal move.\n");
2533
      if (move) {
2534
        game_wtm = Flip(game_wtm);
2535
        if (game_wtm)
2536
          move_number++;
2537
      }
2538
      if (read_input == stdin) {
2539
        if (game_wtm)
2540
          printf("read.White(%d): ", move_number);
2541
        else
2542
          printf("read.Black(%d): ", move_number);
2543
        fflush(stdout);
2544
      }
2545
      readstat = ReadPGN(read_input, 0);
2546
      if (readstat < 0)
2547
        break;
2548
      if (!strcmp(buffer, "exit"))
2549
        break;
2550
    } while (1);
2551
    moves_out_of_book = 0;
2552
    printf("NOTICE: %d moves to next time control\n",
2553
        tc_moves_remaining[root_wtm]);
2554
    root_wtm = !game_wtm;
2555
    if (read_input != stdin) {
2556
      printf("\n");
2557
      fclose(read_input);
2558
    }
2559
  }
2560
/*
2561
 ************************************************************
2562
 *                                                          *
2563
 *  "rejected" handles the new xboard protocol version 2    *
2564
 *  rejected command.                                       *
2565
 *                                                          *
2566
 ************************************************************
2567
 */
2568
  else if (OptionMatch("rejected", *args)) {
2569
    Print(4095, "ERROR.  feature %s rejected by xboard\n", args[1]);
2570
  }
2571
/*
2572
 ************************************************************
2573
 *                                                          *
108 pmbaty 2574
 *  "reset" restores (backs up) a game to a prior position  *
2575
 *  with the same side on move.  Reset 17 would reset the   *
2576
 *  position to what it was at move 17 with the current     *
2577
 *  still on move (you can use white/black commands to      *
2578
 *  change the side to move first, if needed.)              *
2579
 *                                                          *
2580
 ************************************************************
2581
 */
2582
  else if (OptionMatch("reset", *args)) {
2583
    int i, move, nmoves;
2584
 
2585
    if (!history_file)
2586
      return 1;
2587
    if (thinking || pondering)
2588
      return 2;
2589
    if (nargs < 2) {
2590
      printf("usage:  reset <movenumber>\n");
2591
      return 1;
2592
    }
2593
    ponder_move = 0;
2594
    last_mate_score = 0;
2595
    last_pv.pathd = 0;
2596
    last_pv.pathl = 0;
2597
    if (thinking || pondering)
2598
      return 2;
2599
    over = 0;
2600
    move_number = atoi(args[1]);
2601
    if (!move_number) {
2602
      move_number = 1;
2603
      return 1;
2604
    }
2605
    nmoves = (move_number - 1) * 2 + 1 - game_wtm;
2606
    root_wtm = Flip(game_wtm);
2607
    InitializeChessBoard(tree);
2608
    game_wtm = 1;
2609
    move_number = 1;
2610
    tc_moves_remaining[white] = tc_moves;
2611
    tc_moves_remaining[black] = tc_moves;
2612
    for (i = 0; i < nmoves; i++) {
2613
      fseek(history_file, i * 10, SEEK_SET);
2614
      v = fscanf(history_file, "%s", buffer);
2615
      if (v <= 0)
2616
        perror("Option() fscanf error: ");
2617
/*
2618
 If the move is "pass", that means that the side on move passed.
2619
 This includes the case where the game started from a black-to-move
2620
 position; then white's first move is recorded as a pass.
2621
 */
2622
      if (strcmp(buffer, "pass") == 0) {
2623
        game_wtm = Flip(game_wtm);
2624
        if (game_wtm)
2625
          move_number++;
2626
        continue;
2627
      }
2628
      move = InputMove(tree, 0, game_wtm, 0, 0, buffer);
2629
      if (move)
2630
        MakeMoveRoot(tree, game_wtm, move);
2631
      else {
2632
        printf("ERROR!  move %s is illegal\n", buffer);
2633
        break;
2634
      }
2635
      TimeAdjust(game_wtm, 0);
2636
      game_wtm = Flip(game_wtm);
2637
      if (game_wtm)
2638
        move_number++;
2639
    }
2640
    moves_out_of_book = 0;
2641
    printf("NOTICE: %d moves to next time control\n",
2642
        tc_moves_remaining[root_wtm]);
2643
  }
2644
/*
2645
 ************************************************************
2646
 *                                                          *
33 pmbaty 2647
 *  "resign" command sets the resignation threshold to the  *
2648
 *  number of pawns the program must be behind before       *
2649
 *  resigning (0 -> disable resignations).  Resign with no  *
2650
 *  arguments will mark the pgn result as lost by the       *
2651
 *  opponent.                                               *
2652
 *                                                          *
2653
 ************************************************************
2654
 */
2655
  else if (OptionMatch("resign", *args)) {
2656
    if (nargs < 2) {
2657
      if (crafty_is_white) {
2658
        Print(4095, "result 1-0\n");
108 pmbaty 2659
        strcpy(pgn_result, "1-0");
33 pmbaty 2660
      } else {
2661
        Print(4095, "result 0-1\n");
108 pmbaty 2662
        strcpy(pgn_result, "0-1");
33 pmbaty 2663
      }
2664
      learn_value = 300;
2665
      LearnBook();
2666
      return 1;
2667
    }
2668
    resign = atoi(args[1]);
2669
    if (nargs == 3)
2670
      resign_count = atoi(args[2]);
2671
    if (resign)
108 pmbaty 2672
      Print(32, "resign after %d consecutive moves with score < %d.\n",
33 pmbaty 2673
          resign_count, -resign);
2674
    else
108 pmbaty 2675
      Print(32, "disabled resignations.\n");
33 pmbaty 2676
  }
2677
/*
2678
 ************************************************************
2679
 *                                                          *
2680
 *  "result" command comes from xboard/winboard and gives   *
2681
 *  the result of the current game.  If learning routines   *
2682
 *  have not yet been activated, this will do it.           *
2683
 *                                                          *
2684
 ************************************************************
2685
 */
2686
  else if (OptionMatch("result", *args)) {
2687
    if (nargs > 1) {
2688
      if (!strcmp(args[1], "1-0")) {
108 pmbaty 2689
        strcpy(pgn_result, "1-0");
33 pmbaty 2690
        if (crafty_is_white)
2691
          learn_value = 300;
2692
        else
2693
          learn_value = -300;
2694
      } else if (!strcmp(args[1], "0-1")) {
108 pmbaty 2695
        strcpy(pgn_result, "0-1");
33 pmbaty 2696
        if (crafty_is_white)
2697
          learn_value = -300;
2698
        else
2699
          learn_value = 300;
2700
      } else if (!strcmp(args[1], "1/2-1/2")) {
108 pmbaty 2701
        strcpy(pgn_result, "1/2-1/2");
33 pmbaty 2702
        learn_value = 1;
2703
      }
2704
      LearnBook();
2705
      return 1;
2706
    }
2707
  }
2708
/*
2709
 ************************************************************
2710
 *                                                          *
108 pmbaty 2711
 *  "safety" command sets a specific time safety margin     *
2712
 *  target for normal timed games.  This can generally be   *
2713
 *  left at the default value unless Crafty is being        *
2714
 *  manually operated.                                      *
2715
 *                                                          *
2716
 ************************************************************
2717
 */
2718
  else if (OptionMatch("safety", *args)) {
2719
    if (nargs == 2)
2720
      tc_safety_margin = atoi(args[1]) * 100;
2721
    Print(32, "safety margin set to %s.\n", DisplayTime(tc_safety_margin));
2722
  }
2723
/*
2724
 ************************************************************
2725
 *                                                          *
33 pmbaty 2726
 *  "savegame" command saves the game in a file in PGN      *
2727
 *  format.  Command has an optional filename.              *
2728
 *                                                          *
2729
 ************************************************************
2730
 */
2731
  else if (OptionMatch("savegame", *args)) {
2732
    struct tm *timestruct;
108 pmbaty 2733
    FILE *output_file;
2734
    time_t secs;
33 pmbaty 2735
    int i, more, swtm;
2736
    char input[128], text[128], *next;
2737
 
2738
    output_file = stdout;
2739
    secs = time(0);
2740
    timestruct = localtime((time_t *) & secs);
2741
    if (nargs > 1) {
108 pmbaty 2742
      if (!(output_file = fopen(args[1], "w"))) {
33 pmbaty 2743
        printf("unable to open %s for write.\n", args[1]);
2744
        return 1;
2745
      }
2746
    }
2747
    fprintf(output_file, "[Event \"%s\"]\n", pgn_event);
2748
    fprintf(output_file, "[Site \"%s\"]\n", pgn_site);
2749
    fprintf(output_file, "[Date \"%4d.%02d.%02d\"]\n",
2750
        timestruct->tm_year + 1900, timestruct->tm_mon + 1,
2751
        timestruct->tm_mday);
2752
    fprintf(output_file, "[Round \"%s\"]\n", pgn_round);
2753
    fprintf(output_file, "[White \"%s\"]\n", pgn_white);
2754
    fprintf(output_file, "[WhiteElo \"%s\"]\n", pgn_white_elo);
2755
    fprintf(output_file, "[Black \"%s\"]\n", pgn_black);
2756
    fprintf(output_file, "[BlackElo \"%s\"]\n", pgn_black_elo);
2757
    fprintf(output_file, "[Result \"%s\"]\n", pgn_result);
2758
/* Handle setup positions and initial pass by white */
2759
    swtm = 1;
2760
    if (move_number > 1 || !game_wtm) {
2761
      fseek(history_file, 0, SEEK_SET);
108 pmbaty 2762
      if (fscanf(history_file, "%s", input) == 1 &&
33 pmbaty 2763
          strcmp(input, "pass") == 0)
2764
        swtm = 0;
2765
    }
2766
    if (initial_position[0])
2767
      fprintf(output_file, "[FEN \"%s\"]\n[SetUp \"1\"]\n", initial_position);
2768
    else if (!swtm) {
2769
      fprintf(output_file,
2770
          "[FEN \"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1\"\n"
2771
          "[SetUp \"1\"]\n");
2772
    }
2773
    fprintf(output_file, "\n");
2774
    next = text;
2775
    if (!swtm) {
108 pmbaty 2776
      strcpy(next, "1... ");
33 pmbaty 2777
      next = text + strlen(text);
2778
    }
2779
/* Output the moves */
2780
    more = 0;
2781
    for (i = (swtm ? 0 : 1); i < (move_number - 1) * 2 - game_wtm + 1; i++) {
2782
      fseek(history_file, i * 10, SEEK_SET);
108 pmbaty 2783
      v = fscanf(history_file, "%s", input);
2784
      if (v <= 0)
2785
        perror("Option() fscanf error: ");
33 pmbaty 2786
      if (!(i % 2)) {
2787
        sprintf(next, "%d. ", i / 2 + 1);
2788
        next = text + strlen(text);
2789
      }
2790
      sprintf(next, "%s ", input);
2791
      next = text + strlen(text);
2792
      more = 1;
2793
      if (next - text >= 60) {
2794
        fprintf(output_file, "%s\n", text);
2795
        more = 0;
2796
        next = text;
2797
      }
2798
    }
2799
    if (more)
2800
      fprintf(output_file, "%s", text);
2801
    fprintf(output_file, "%s\n", pgn_result);
2802
    if (output_file != stdout)
2803
      fclose(output_file);
2804
    printf("PGN save complete.\n");
2805
  }
2806
/*
2807
 ************************************************************
2808
 *                                                          *
2809
 *  "savepos" command saves the current position in a FEN   *
2810
 *  (Forsythe-Edwards Notation) string that can be later    *
2811
 *  used to recreate this exact position.                   *
2812
 *                                                          *
2813
 ************************************************************
2814
 */
2815
  else if (OptionMatch("savepos", *args)) {
108 pmbaty 2816
    FILE *output_file;
33 pmbaty 2817
    int rank, file, nempty;
2818
 
2819
    output_file = stdout;
2820
    if (nargs > 1) {
2821
      if (!strcmp(args[1], "*")) {
2822
        output_file = 0;
108 pmbaty 2823
        strcpy(initial_position, "");
33 pmbaty 2824
      } else if (!(output_file = fopen(args[1], "w"))) {
2825
        printf("unable to open %s for write.\n", args[1]);
2826
        return 1;
2827
      }
2828
    }
2829
    if (output_file)
2830
      fprintf(output_file, "setboard ");
2831
    for (rank = RANK8; rank >= RANK1; rank--) {
2832
      nempty = 0;
2833
      for (file = FILEA; file <= FILEH; file++) {
2834
        if (PcOnSq((rank << 3) + file)) {
2835
          if (nempty) {
2836
            if (output_file)
2837
              fprintf(output_file, "%c", empty_sqs[nempty]);
2838
            else
2839
              sprintf(initial_position + strlen(initial_position), "%c",
2840
                  empty_sqs[nempty]);
2841
            nempty = 0;
2842
          }
2843
          if (output_file)
2844
            fprintf(output_file, "%c",
2845
                translate[PcOnSq((rank << 3) + file) + 6]);
2846
          else
2847
            sprintf(initial_position + strlen(initial_position), "%c",
2848
                translate[PcOnSq((rank << 3) + file) + 6]);
2849
        } else
2850
          nempty++;
2851
      }
2852
      if (empty_sqs[nempty]) {
2853
        if (output_file)
2854
          fprintf(output_file, "%c", empty_sqs[nempty]);
2855
        else
2856
          sprintf(initial_position + strlen(initial_position), "%c",
2857
              empty_sqs[nempty]);
2858
      }
2859
      if (rank != RANK1) {
2860
        if (output_file)
2861
          fprintf(output_file, "/");
2862
        else
108 pmbaty 2863
          sprintf(initial_position + strlen(initial_position), "/");
33 pmbaty 2864
      }
2865
    }
2866
    if (output_file)
2867
      fprintf(output_file, " %c ", (game_wtm) ? 'w' : 'b');
2868
    else
108 pmbaty 2869
      sprintf(initial_position + strlen(initial_position), " %c ",
2870
          (game_wtm) ? 'w' : 'b');
33 pmbaty 2871
    if (Castle(0, white) & 1) {
2872
      if (output_file)
2873
        fprintf(output_file, "K");
2874
      else
108 pmbaty 2875
        sprintf(initial_position + strlen(initial_position), "K");
33 pmbaty 2876
    }
2877
    if (Castle(0, white) & 2) {
2878
      if (output_file)
2879
        fprintf(output_file, "Q");
2880
      else
108 pmbaty 2881
        sprintf(initial_position + strlen(initial_position), "Q");
33 pmbaty 2882
    }
2883
    if (Castle(0, black) & 1) {
2884
      if (output_file)
2885
        fprintf(output_file, "k");
2886
      else
108 pmbaty 2887
        sprintf(initial_position + strlen(initial_position), "k");
33 pmbaty 2888
    }
2889
    if (Castle(0, black) & 2) {
2890
      if (output_file)
2891
        fprintf(output_file, "q");
2892
      else
108 pmbaty 2893
        sprintf(initial_position + strlen(initial_position), "q");
33 pmbaty 2894
    }
2895
    if (!Castle(0, white) && !Castle(0, black)) {
2896
      if (output_file)
2897
        fprintf(output_file, " -");
2898
      else
108 pmbaty 2899
        sprintf(initial_position + strlen(initial_position), " -");
33 pmbaty 2900
    }
2901
    if (EnPassant(0)) {
2902
      if (output_file)
2903
        fprintf(output_file, " %c%c", File(EnPassant(0)) + 'a',
2904
            Rank(EnPassant(0)) + '1');
2905
      else
2906
        sprintf(initial_position + strlen(initial_position), " %c%c",
2907
            File(EnPassant(0)) + 'a', Rank(EnPassant(0)) + '1');
2908
    } else {
2909
      if (output_file)
2910
        fprintf(output_file, " -");
2911
      else
108 pmbaty 2912
        sprintf(initial_position + strlen(initial_position), " -");
33 pmbaty 2913
    }
2914
    if (output_file)
2915
      fprintf(output_file, "\n");
2916
    if (output_file && output_file != stdout) {
2917
      fprintf(output_file, "exit\n");
2918
      fclose(output_file);
2919
    }
2920
    if (output_file)
2921
      printf("FEN save complete.\n");
2922
  }
2923
/*
2924
 ************************************************************
2925
 *                                                          *
2926
 *  "scale" command is used for tuning.  We modify this to  *
2927
 *  scale some scoring value(s) by a percentage that can    *
2928
 *  be either positive or negative.                         *
2929
 *                                                          *
2930
 ************************************************************
2931
 */
2932
  else if (!strcmp("scale", *args)) {
2933
    scale = atoi(args[1]);
2934
  }
2935
/*
2936
 ************************************************************
2937
 *                                                          *
2938
 *  "score" command displays static evaluation of the       *
2939
 *  current board position.                                 *
2940
 *                                                          *
2941
 ************************************************************
2942
 */
2943
  else if (OptionMatch("score", *args)) {
108 pmbaty 2944
    int phase, s, tw, tb, mgb, mgw, egb, egw, trop[2];
33 pmbaty 2945
 
2946
    if (thinking || pondering)
2947
      return 2;
108 pmbaty 2948
    memset((void *) &(tree->pawn_score), 0, sizeof(tree->pawn_score));
2949
    Print(32, "note: scores are for the white side\n");
2950
    Print(32, "                       ");
2951
    Print(32, " +-----------white----------+");
2952
    Print(32, "-----------black----------+\n");
33 pmbaty 2953
    tree->score_mg = 0;
2954
    tree->score_eg = 0;
2955
    mgb = tree->score_mg;
2956
    EvaluateMaterial(tree, game_wtm);
2957
    mgb = tree->score_mg - mgb;
108 pmbaty 2958
    Print(32, "material.......%s", DisplayEvaluation(mgb, 1));
2959
    Print(32, "  |    comp     mg      eg   |");
2960
    Print(32, "    comp     mg      eg   |\n");
33 pmbaty 2961
    root_wtm = Flip(game_wtm);
2962
    tree->status[1] = tree->status[0];
2963
    s = Evaluate(tree, 1, game_wtm, -99999, 99999);
108 pmbaty 2964
    trop[black] = tree->tropism[black];
2965
    trop[white] = tree->tropism[white];
33 pmbaty 2966
    if (!game_wtm)
2967
      s = -s;
2968
    tree->score_mg = 0;
2969
    tree->score_eg = 0;
2970
    phase =
2971
        Min(62, TotalPieces(white, occupied) + TotalPieces(black, occupied));
2972
    tree->pawn_score.score_mg = 0;
2973
    tree->pawn_score.score_eg = 0;
2974
    mgb = tree->pawn_score.score_mg;
2975
    egb = tree->pawn_score.score_eg;
2976
    EvaluatePawns(tree, black);
2977
    mgb = tree->pawn_score.score_mg - mgb;
2978
    egb = tree->pawn_score.score_eg - egb;
2979
    mgw = tree->pawn_score.score_mg;
2980
    egw = tree->pawn_score.score_eg;
2981
    EvaluatePawns(tree, white);
2982
    mgw = tree->pawn_score.score_mg - mgw;
2983
    egw = tree->pawn_score.score_eg - egw;
2984
    tb = (mgb * phase + egb * (62 - phase)) / 62;
2985
    tw = (mgw * phase + egw * (62 - phase)) / 62;
108 pmbaty 2986
    Print(32, "pawns..........%s  |", DisplayEvaluation(tb + tw, 1));
2987
    Print(32, " %s", DisplayEvaluation(tw, 1));
2988
    Print(32, " %s", DisplayEvaluation(mgw, 1));
2989
    Print(32, " %s  |", DisplayEvaluation(egw, 1));
2990
    Print(32, " %s", DisplayEvaluation(tb, 1));
2991
    Print(32, " %s", DisplayEvaluation(mgb, 1));
2992
    Print(32, " %s  |\n", DisplayEvaluation(egb, 1));
33 pmbaty 2993
    mgb = tree->score_mg;
2994
    egb = tree->score_eg;
2995
    EvaluatePassedPawns(tree, black, game_wtm);
2996
    mgb = tree->score_mg - mgb;
2997
    egb = tree->score_eg - egb;
2998
    mgw = tree->score_mg;
2999
    egw = tree->score_eg;
3000
    EvaluatePassedPawns(tree, white, game_wtm);
3001
    mgw = tree->score_mg - mgw;
3002
    egw = tree->score_eg - egw;
3003
    tb = (mgb * phase + egb * (62 - phase)) / 62;
3004
    tw = (mgw * phase + egw * (62 - phase)) / 62;
108 pmbaty 3005
    Print(32, "passed pawns...%s  |", DisplayEvaluation(tb + tw, 1));
3006
    Print(32, " %s", DisplayEvaluation(tw, 1));
3007
    Print(32, " %s", DisplayEvaluation(mgw, 1));
3008
    Print(32, " %s  |", DisplayEvaluation(egw, 1));
3009
    Print(32, " %s", DisplayEvaluation(tb, 1));
3010
    Print(32, " %s", DisplayEvaluation(mgb, 1));
3011
    Print(32, " %s  |\n", DisplayEvaluation(egb, 1));
33 pmbaty 3012
    mgb = tree->score_mg;
3013
    egb = tree->score_eg;
3014
    EvaluateKnights(tree, black);
3015
    mgb = tree->score_mg - mgb;
3016
    egb = tree->score_eg - egb;
3017
    mgw = tree->score_mg;
3018
    egw = tree->score_eg;
3019
    EvaluateKnights(tree, white);
3020
    mgw = tree->score_mg - mgw;
3021
    egw = tree->score_eg - egw;
3022
    tb = (mgb * phase + egb * (62 - phase)) / 62;
3023
    tw = (mgw * phase + egw * (62 - phase)) / 62;
108 pmbaty 3024
    Print(32, "knights........%s  |", DisplayEvaluation(tb + tw, 1));
3025
    Print(32, " %s", DisplayEvaluation(tw, 1));
3026
    Print(32, " %s", DisplayEvaluation(mgw, 1));
3027
    Print(32, " %s  |", DisplayEvaluation(egw, 1));
3028
    Print(32, " %s", DisplayEvaluation(tb, 1));
3029
    Print(32, " %s", DisplayEvaluation(mgb, 1));
3030
    Print(32, " %s  |\n", DisplayEvaluation(egb, 1));
33 pmbaty 3031
    mgb = tree->score_mg;
3032
    egb = tree->score_eg;
3033
    EvaluateBishops(tree, black);
3034
    mgb = tree->score_mg - mgb;
3035
    egb = tree->score_eg - egb;
3036
    mgw = tree->score_mg;
3037
    egw = tree->score_eg;
3038
    EvaluateBishops(tree, white);
3039
    mgw = tree->score_mg - mgw;
3040
    egw = tree->score_eg - egw;
3041
    tb = (mgb * phase + egb * (62 - phase)) / 62;
3042
    tw = (mgw * phase + egw * (62 - phase)) / 62;
108 pmbaty 3043
    Print(32, "bishops........%s  |", DisplayEvaluation(tb + tw, 1));
3044
    Print(32, " %s", DisplayEvaluation(tw, 1));
3045
    Print(32, " %s", DisplayEvaluation(mgw, 1));
3046
    Print(32, " %s  |", DisplayEvaluation(egw, 1));
3047
    Print(32, " %s", DisplayEvaluation(tb, 1));
3048
    Print(32, " %s", DisplayEvaluation(mgb, 1));
3049
    Print(32, " %s  |\n", DisplayEvaluation(egb, 1));
33 pmbaty 3050
    mgb = tree->score_mg;
3051
    egb = tree->score_eg;
3052
    EvaluateRooks(tree, black);
3053
    mgb = tree->score_mg - mgb;
3054
    egb = tree->score_eg - egb;
3055
    mgw = tree->score_mg;
3056
    egw = tree->score_eg;
3057
    EvaluateRooks(tree, white);
3058
    mgw = tree->score_mg - mgw;
3059
    egw = tree->score_eg - egw;
3060
    tb = (mgb * phase + egb * (62 - phase)) / 62;
3061
    tw = (mgw * phase + egw * (62 - phase)) / 62;
108 pmbaty 3062
    Print(32, "rooks..........%s  |", DisplayEvaluation(tb + tw, 1));
3063
    Print(32, " %s", DisplayEvaluation(tw, 1));
3064
    Print(32, " %s", DisplayEvaluation(mgw, 1));
3065
    Print(32, " %s  |", DisplayEvaluation(egw, 1));
3066
    Print(32, " %s", DisplayEvaluation(tb, 1));
3067
    Print(32, " %s", DisplayEvaluation(mgb, 1));
3068
    Print(32, " %s  |\n", DisplayEvaluation(egb, 1));
33 pmbaty 3069
    mgb = tree->score_mg;
3070
    egb = tree->score_eg;
3071
    EvaluateQueens(tree, black);
3072
    mgb = tree->score_mg - mgb;
3073
    egb = tree->score_eg - egb;
3074
    mgw = tree->score_mg;
3075
    egw = tree->score_eg;
3076
    EvaluateQueens(tree, white);
3077
    mgw = tree->score_mg - mgw;
3078
    egw = tree->score_eg - egw;
3079
    tb = (mgb * phase + egb * (62 - phase)) / 62;
3080
    tw = (mgw * phase + egw * (62 - phase)) / 62;
108 pmbaty 3081
    Print(32, "queens.........%s  |", DisplayEvaluation(tb + tw, 1));
3082
    Print(32, " %s", DisplayEvaluation(tw, 1));
3083
    Print(32, " %s", DisplayEvaluation(mgw, 1));
3084
    Print(32, " %s  |", DisplayEvaluation(egw, 1));
3085
    Print(32, " %s", DisplayEvaluation(tb, 1));
3086
    Print(32, " %s", DisplayEvaluation(mgb, 1));
3087
    Print(32, " %s  |\n", DisplayEvaluation(egb, 1));
3088
    tree->tropism[black] = trop[black];
3089
    tree->tropism[white] = trop[white];
33 pmbaty 3090
    mgb = tree->score_mg;
3091
    egb = tree->score_eg;
108 pmbaty 3092
    EvaluateKing(tree, 1, black);
33 pmbaty 3093
    mgb = tree->score_mg - mgb;
3094
    egb = tree->score_eg - egb;
3095
    mgw = tree->score_mg;
3096
    egw = tree->score_eg;
108 pmbaty 3097
    EvaluateKing(tree, 1, white);
33 pmbaty 3098
    mgw = tree->score_mg - mgw;
3099
    egw = tree->score_eg - egw;
3100
    tb = (mgb * phase + egb * (62 - phase)) / 62;
3101
    tw = (mgw * phase + egw * (62 - phase)) / 62;
108 pmbaty 3102
    Print(32, "kings..........%s  |", DisplayEvaluation(tb + tw, 1));
3103
    Print(32, " %s", DisplayEvaluation(tw, 1));
3104
    Print(32, " %s", DisplayEvaluation(mgw, 1));
3105
    Print(32, " %s  |", DisplayEvaluation(egw, 1));
3106
    Print(32, " %s", DisplayEvaluation(tb, 1));
3107
    Print(32, " %s", DisplayEvaluation(mgb, 1));
3108
    Print(32, " %s  |\n", DisplayEvaluation(egb, 1));
33 pmbaty 3109
    egb = tree->score_eg;
3110
    if ((TotalPieces(white, occupied) == 0 && tree->pawn_score.passed[black])
3111
        || (TotalPieces(black, occupied) == 0 &&
3112
            tree->pawn_score.passed[white]))
3113
      EvaluatePassedPawnRaces(tree, game_wtm);
3114
    egb = tree->score_eg - egb;
108 pmbaty 3115
    Print(32, "pawn races.....%s", DisplayEvaluation(egb, 1));
3116
    Print(32, "  +--------------------------+--------------------------+\n");
3117
    Print(32, "total..........%s\n", DisplayEvaluation(s, 1));
33 pmbaty 3118
  }
3119
/*
3120
 ************************************************************
3121
 *                                                          *
108 pmbaty 3122
 *  "screen" command runs runs through a test suite of      *
3123
 *  positions and culls any where a search returns a value  *
3124
 *  outside the margin given to the screen command.         *
3125
 *                                                          *
3126
 ************************************************************
3127
 */
3128
  else if (OptionMatch("screen", *args)) {
3129
    int margin = 9999999, save_noise, save_display;
3130
 
3131
    nargs = ReadParse(buffer, args, " \t;=");
3132
    if (thinking || pondering)
3133
      return 2;
3134
    if (nargs < 3) {
3135
      printf("usage:  screen <filename> score-margin\n");
3136
      return 1;
3137
    }
3138
    save_noise = noise_level;
3139
    save_display = display_options;
3140
    early_exit = 99;
3141
    margin = atoi(args[2]);
3142
    noise_level = 99999999;
3143
    display_options = 2048;
3144
    Test(args[1], 0, 1, margin);
3145
    noise_level = save_noise;
3146
    display_options = save_display;
3147
    ponder_move = 0;
3148
    last_pv.pathd = 0;
3149
    last_pv.pathl = 0;
3150
  }
3151
/*
3152
 ************************************************************
3153
 *                                                          *
33 pmbaty 3154
 *  "sd" command sets a specific search depth to control    *
3155
 *  the tree search depth.                                  *
3156
 *                                                          *
3157
 ************************************************************
3158
 */
3159
  else if (OptionMatch("sd", *args)) {
3160
    if (nargs < 2) {
3161
      printf("usage:  sd <depth>\n");
3162
      return 1;
3163
    }
3164
    search_depth = atoi(args[1]);
108 pmbaty 3165
    Print(32, "search depth set to %d.\n", search_depth);
33 pmbaty 3166
  }
3167
/*
3168
 ************************************************************
3169
 *                                                          *
108 pmbaty 3170
 *  "search" command sets a specific move for the search    *
3171
 *  to analyze, ignoring all others completely.             *
3172
 *                                                          *
3173
 ************************************************************
3174
 */
3175
  else if (OptionMatch("search", *args)) {
3176
    if (thinking || pondering)
3177
      return 2;
3178
    if (nargs < 2) {
3179
      printf("usage:  search <move>\n");
3180
      return 1;
3181
    }
3182
    search_move = InputMove(tree, 0, game_wtm, 0, 0, args[1]);
3183
    if (!search_move)
3184
      search_move = InputMove(tree, 0, Flip(game_wtm), 0, 0, args[1]);
3185
    if (!search_move)
3186
      printf("illegal move.\n");
3187
  }
3188
/*
3189
 ************************************************************
3190
 *                                                          *
3191
 *  "setboard" command sets the board to a specific         *
3192
 *  position for analysis by the program.                   *
3193
 *                                                          *
3194
 ************************************************************
3195
 */
3196
  else if (OptionMatch("setboard", *args)) {
3197
    if (thinking || pondering)
3198
      return 2;
3199
    nargs = ReadParse(buffer, args, " \t;=");
3200
    if (nargs < 3) {
3201
      printf("usage:  setboard <fen>\n");
3202
      return 1;
3203
    }
3204
    SetBoard(tree, nargs - 1, args + 1, 0);
3205
    move_number = 1;
3206
    if (!game_wtm) {
3207
      game_wtm = 1;
3208
      Pass();
3209
    }
3210
    ponder_move = 0;
3211
    last_pv.pathd = 0;
3212
    last_pv.pathl = 0;
3213
    over = 0;
3214
    strcpy(buffer, "savepos *");
3215
    Option(tree);
3216
  } else if (StrCnt(*args, '/') > 3) {
3217
    if (thinking || pondering)
3218
      return 2;
3219
    nargs = ReadParse(buffer, args, " \t;=");
3220
    SetBoard(tree, nargs, args, 0);
3221
    move_number = 1;
3222
    if (!game_wtm) {
3223
      game_wtm = 1;
3224
      Pass();
3225
    }
3226
    ponder_move = 0;
3227
    last_pv.pathd = 0;
3228
    last_pv.pathl = 0;
3229
    over = 0;
3230
    strcpy(buffer, "savepos *");
3231
    Option(tree);
3232
  }
3233
/*
3234
 ************************************************************
3235
 *                                                          *
3236
 *  "settc" command is used to reset the time controls      *
3237
 *  after a complete restart.                               *
3238
 *                                                          *
3239
 ************************************************************
3240
 */
3241
  else if (OptionMatch("settc", *args)) {
3242
    if (thinking || pondering)
3243
      return 2;
3244
    if (nargs < 4) {
3245
      printf("usage:  settc <wmoves> <wtime> <bmoves> <btime>\n");
3246
      return 1;
3247
    }
3248
    tc_moves_remaining[white] = atoi(args[1]);
3249
    tc_time_remaining[white] = ParseTime(args[2]) * 6000;
3250
    tc_moves_remaining[black] = atoi(args[3]);
3251
    tc_time_remaining[black] = ParseTime(args[4]) * 6000;
3252
    Print(32, "time remaining: %s (white).\n",
3253
        DisplayTime(tc_time_remaining[white]));
3254
    Print(32, "time remaining: %s (black).\n",
3255
        DisplayTime(tc_time_remaining[black]));
3256
    if (tc_sudden_death != 1) {
3257
      Print(32, "%d moves to next time control (white)\n",
3258
          tc_moves_remaining[white]);
3259
      Print(32, "%d moves to next time control (black)\n",
3260
          tc_moves_remaining[black]);
3261
    } else
3262
      Print(32, "Sudden-death time control in effect\n");
3263
    TimeSet(999);
3264
  }
3265
/*
3266
 ************************************************************
3267
 *                                                          *
33 pmbaty 3268
 *  "show" command enables/disables whether or not we want  *
3269
 *  show book information as the game is played.            *
3270
 *                                                          *
3271
 ************************************************************
3272
 */
3273
  else if (OptionMatch("show", *args)) {
3274
    if (nargs < 2) {
3275
      printf("usage:  show book\n");
3276
      return 1;
3277
    }
3278
    if (OptionMatch("book", args[1])) {
3279
      show_book = !show_book;
3280
      if (show_book)
108 pmbaty 3281
        Print(32, "show book statistics\n");
33 pmbaty 3282
      else
108 pmbaty 3283
        Print(32, "don't show book statistics\n");
33 pmbaty 3284
    }
3285
  }
3286
/*
3287
 ************************************************************
3288
 *                                                          *
3289
 *  "skill" command sets a value from 1-100 that affects    *
3290
 *  Crafty's playing skill level.  100 => max skill, 1 =>   *
154 pmbaty 3291
 *  minimal skill.  This is used to slow the search speed   *
3292
 *  (and depth) significantly.                              *
33 pmbaty 3293
 *                                                          *
3294
 ************************************************************
3295
 */
3296
#if defined(SKILL)
3297
  else if (OptionMatch("skill", *args)) {
3298
    if (nargs < 2) {
3299
      printf("usage:  skill <1-100>\n");
3300
      return 1;
3301
    }
108 pmbaty 3302
    if (skill != 100)
33 pmbaty 3303
      printf("ERROR:  skill can only be changed one time in a game\n");
108 pmbaty 3304
    else {
33 pmbaty 3305
      skill = atoi(args[1]);
3306
      if (skill < 1 || skill > 100) {
3307
        printf("ERROR: skill range is 1-100 only\n");
3308
        skill = 100;
3309
      }
108 pmbaty 3310
      Print(32, "skill level set to %d%%\n", skill);
33 pmbaty 3311
    }
3312
  }
3313
#endif
3314
/*
3315
 ************************************************************
3316
 *                                                          *
3317
 *   "smp" command is used to tune the various SMP search   *
3318
 *   parameters.                                            *
3319
 *                                                          *
108 pmbaty 3320
 *   "smpaffinity" command is used to enable (>= 0) and to  *
3321
 *   disable smp processor affinity (off).  If you try to   *
3322
 *   run two instances of Crafty on the same machine, ONE   *
3323
 *   them (if not both) need to have processor affinity     *
3324
 *   disabled or else you can use the smpaffinity=<n> to    *
3325
 *   prevent processor conflicts.  If you use a 32 core     *
3326
 *   machine, and you want to run two instances of Crafty,  *
3327
 *   use smpaffinity=0 on one, and smpaffinity=16 on the    *
3328
 *   other.  The first will bind to processors 0-15, and    *
3329
 *   the second will bind to processors 16-31.              *
3330
 *                                                          *
33 pmbaty 3331
 *   "smpgroup" command is used to control how many threads *
3332
 *   may work together at any point in the tree.  The       *
3333
 *   usual default is 6, but this might be reduced on a     *
3334
 *   machine with a large number of processors.  It should  *
3335
 *   be tested, of course.                                  *
3336
 *                                                          *
3337
 *   "smpmin" sets the minimum depth the search can split   *
3338
 *   at to keep it from splitting too near the leaves.      *
3339
 *                                                          *
3340
 *   "smpmt" command is used to set the maximum number of   *
3341
 *   parallel threads to use, assuming that Crafty was      *
3342
 *   compiled with -DSMP.  This value can not be set        *
3343
 *   larger than the compiled-in -DCPUS=n value.            *
3344
 *                                                          *
3345
 *   "smpnice" command turns on "nice" mode where idle      *
3346
 *   processors are terminated between searches to avoid    *
3347
 *   burning CPU time in the idle loop.                     *
3348
 *                                                          *
108 pmbaty 3349
 *   "smpnuma" command enables NUMA mode which distributes  *
3350
 *   hash tables across all NUMA nodes evenly.  If your     *
3351
 *   machine is not NUMA, or only has one socket (node) you *
3352
 *   should set this to zero as it will be slightly more    *
3353
 *   efficient when you change hash sizes.                  *
3354
 *                                                          *
33 pmbaty 3355
 *   "smproot" command is used to enable (1) or disable (0) *
3356
 *   splitting the tree at the root (ply=1).  Splitting at  *
3357
 *   the root is more efficient, but might slow finding the *
3358
 *   move in some test positions.                           *
3359
 *                                                          *
108 pmbaty 3360
 *   "smpgsd" sets the minimum depth remaining at which a   *
3361
 *   gratuitous split can be done.                          *
33 pmbaty 3362
 *                                                          *
108 pmbaty 3363
 *   "smpgsl" sets the maximum number of gratuitous splits  *
3364
 *   per thread.  This only counts splits that have not yet *
3365
 *   been joined.                                           *
3366
 *                                                          *
33 pmbaty 3367
 ************************************************************
3368
 */
108 pmbaty 3369
  else if (OptionMatch("smpaffinity", *args)) {
33 pmbaty 3370
    if (nargs < 2) {
108 pmbaty 3371
      printf("usage:  smpaffinity <0/1>\n");
3372
      return 1;
3373
    }
3374
    if (!strcmp(args[1], "off"))
3375
      smp_affinity = -1;
3376
    else
3377
      smp_affinity = atoi(args[1]);
3378
    if (smp_affinity >= 0)
3379
      Print(32, "smp processor affinity enabled.\n");
3380
    else
3381
      Print(32, "smp processor affinity disabled.\n");
3382
  } else if (OptionMatch("smpmin", *args)) {
3383
    if (nargs < 2) {
33 pmbaty 3384
      printf("usage:  smpmin <depth>\n");
3385
      return 1;
3386
    }
3387
    smp_min_split_depth = atoi(args[1]);
108 pmbaty 3388
    Print(32, "minimum thread depth set to %d.\n", smp_min_split_depth);
33 pmbaty 3389
  } else if (OptionMatch("smpgroup", *args)) {
3390
    if (nargs < 2) {
3391
      printf("usage:  smpgroup <threads>\n");
3392
      return 1;
3393
    }
3394
    smp_split_group = atoi(args[1]);
108 pmbaty 3395
    Print(32, "maximum thread group size set to %d.\n", smp_split_group);
33 pmbaty 3396
  } else if (OptionMatch("smpmt", *args) || OptionMatch("mt", *args)
3397
      || OptionMatch("cores", *args)) {
3398
    int proc;
3399
 
3400
    if (nargs < 2) {
3401
      printf("usage:  smpmt=<threads>\n");
3402
      return 1;
3403
    }
3404
    if (thinking || pondering)
3405
      return 3;
3406
    allow_cores = 0;
108 pmbaty 3407
    if (xboard)
3408
      Print(4095, "Warning--  xboard 'cores' option disabled\n");
33 pmbaty 3409
    smp_max_threads = atoi(args[1]);
154 pmbaty 3410
    if (smp_max_threads > hardware_processors) {
3411
      Print(4095, "ERROR - machine has %d processors.\n",
3412
          hardware_processors);
3413
      Print(4095, "ERROR - max threads can not exceed this limit.\n");
3414
      smp_max_threads = hardware_processors;
3415
    }
33 pmbaty 3416
    if (smp_max_threads > CPUS) {
3417
      Print(4095, "ERROR - Crafty was compiled with CPUS=%d.", CPUS);
3418
      Print(4095, "  mt can not exceed this value.\n");
3419
      smp_max_threads = CPUS;
3420
    }
108 pmbaty 3421
    if (smp_max_threads == 1) {
3422
      Print(4095, "ERROR - max threads can be set to zero (0) to");
3423
      Print(4095, " disable parallel search, otherwise it must be > 1.\n");
3424
      smp_max_threads = 0;
3425
    }
33 pmbaty 3426
    if (smp_max_threads)
108 pmbaty 3427
      Print(32, "max threads set to %d.\n", smp_max_threads);
33 pmbaty 3428
    else
108 pmbaty 3429
      Print(32, "parallel threads disabled.\n");
33 pmbaty 3430
    for (proc = 1; proc < CPUS; proc++)
154 pmbaty 3431
      if (proc >= smp_max_threads)
108 pmbaty 3432
        thread[proc].terminate = 1;
33 pmbaty 3433
  } else if (OptionMatch("smpnice", *args)) {
3434
    if (nargs < 2) {
3435
      printf("usage:  smpnice 0|1\n");
3436
      return 1;
3437
    }
3438
    smp_nice = atoi(args[1]);
3439
    if (smp_nice)
108 pmbaty 3440
      Print(32, "SMP terminate extra threads when idle.\n");
33 pmbaty 3441
    else
108 pmbaty 3442
      Print(32, "SMP keep extra threads spinning when idle.\n");
3443
  } else if (OptionMatch("smpnuma", *args)) {
3444
    if (nargs < 2) {
3445
      printf("usage:  smpnuma 0|1\n");
3446
      return 1;
3447
    }
3448
    smp_numa = atoi(args[1]);
3449
    if (smp_numa)
3450
      Print(32, "SMP NUMA mode enabled.\n");
3451
    else
3452
      Print(32, "SMP NUMA mode disabled.\n");
33 pmbaty 3453
  } else if (OptionMatch("smproot", *args)) {
3454
    if (nargs < 2) {
3455
      printf("usage:  smproot 0|1\n");
3456
      return 1;
3457
    }
3458
    smp_split_at_root = atoi(args[1]);
3459
    if (smp_split_at_root)
108 pmbaty 3460
      Print(32, "SMP search split at ply >= 1.\n");
33 pmbaty 3461
    else
108 pmbaty 3462
      Print(32, "SMP search split at ply > 1.\n");
3463
  } else if (OptionMatch("smpgsl", *args)) {
33 pmbaty 3464
    if (nargs < 2) {
108 pmbaty 3465
      printf("usage:  smpgsl <n>\n");
33 pmbaty 3466
      return 1;
3467
    }
108 pmbaty 3468
    smp_gratuitous_limit = atoi(args[1]);
3469
    Print(32, "maximum gratuitous splits allowed %d.\n",
3470
        smp_gratuitous_limit);
3471
  } else if (OptionMatch("smpgsd", *args)) {
3472
    if (nargs < 2) {
3473
      printf("usage:  smpgsd <nodes>\n");
3474
      return 1;
3475
    }
3476
    smp_gratuitous_depth = atoi(args[1]);
3477
    Print(32, "gratuitous split min depth %d.\n", smp_gratuitous_depth);
33 pmbaty 3478
  }
3479
/*
3480
 ************************************************************
3481
 *                                                          *
3482
 *  "sn" command sets a specific number of nodes to search  *
3483
 *  before stopping.  Note:  this requires -DNODES as an    *
3484
 *  option when building Crafty.                            *
3485
 *                                                          *
3486
 ************************************************************
3487
 */
3488
  else if (OptionMatch("sn", *args)) {
3489
    if (nargs < 2) {
3490
      printf("usage:  sn <nodes>\n");
3491
      return 1;
3492
    }
3493
    search_nodes = atoi(args[1]);
108 pmbaty 3494
    Print(32, "search nodes set to %" PRIu64 ".\n", search_nodes);
33 pmbaty 3495
    ponder = 0;
3496
  }
3497
/*
3498
 ************************************************************
3499
 *                                                          *
3500
 *  "speech" command turns speech on/off.                   *
3501
 *                                                          *
3502
 ************************************************************
3503
 */
3504
  else if (OptionMatch("speech", *args)) {
3505
    if (nargs < 2) {
3506
      printf("usage:  speech on|off\n");
3507
      return 1;
3508
    }
3509
    if (!strcmp(args[1], "on"))
3510
      speech = 1;
3511
    else if (!strcmp(args[1], "off"))
3512
      speech = 0;
3513
    if (speech)
3514
      Print(4095, "Audio output enabled\n");
3515
    else
3516
      Print(4095, "Audio output disabled\n");
3517
  }
3518
/*
3519
 ************************************************************
3520
 *                                                          *
3521
 *  "st" command sets a specific search time to control the *
3522
 *  tree search time.                                       *
3523
 *                                                          *
3524
 ************************************************************
3525
 */
108 pmbaty 3526
  else if (OptionMatch("st", *args)) {
33 pmbaty 3527
    if (nargs < 2) {
3528
      printf("usage:  st <time>\n");
3529
      return 1;
3530
    }
154 pmbaty 3531
    search_time_limit = atof(args[1]) * 100;
108 pmbaty 3532
    Print(32, "search time set to %.2f.\n",
33 pmbaty 3533
        (float) search_time_limit / 100.0);
3534
  }
3535
/*
3536
 ************************************************************
3537
 *                                                          *
3538
 *  "swindle" command turns swindle mode off/on.            *
3539
 *                                                          *
3540
 ************************************************************
3541
 */
3542
  else if (OptionMatch("swindle", *args)) {
3543
    if (!strcmp(args[1], "on"))
3544
      swindle_mode = 1;
3545
    else if (!strcmp(args[1], "off"))
3546
      swindle_mode = 0;
3547
    else
3548
      printf("usage:  swindle on|off\n");
3549
  }
3550
/*
3551
 ************************************************************
3552
 *                                                          *
3553
 *  "tags" command lists the current PGN header tags.       *
3554
 *                                                          *
3555
 ************************************************************
3556
 */
3557
  else if (OptionMatch("tags", *args)) {
3558
    struct tm *timestruct;
3559
    uint64_t secs;
3560
 
3561
    secs = time(0);
3562
    timestruct = localtime((time_t *) & secs);
3563
    printf("[Event \"%s\"]\n", pgn_event);
3564
    printf("[Site \"%s\"]\n", pgn_site);
3565
    printf("[Date \"%4d.%02d.%02d\"]\n", timestruct->tm_year + 1900,
3566
        timestruct->tm_mon + 1, timestruct->tm_mday);
3567
    printf("[Round \"%s\"]\n", pgn_round);
3568
    printf("[White \"%s\"]\n", pgn_white);
3569
    printf("[WhiteElo \"%s\"]\n", pgn_white_elo);
3570
    printf("[Black \"%s\"]\n", pgn_black);
3571
    printf("[BlackElo \"%s\"]\n", pgn_black_elo);
3572
    printf("[Result \"%s\"]\n", pgn_result);
3573
  }
3574
/*
3575
 ************************************************************
3576
 *                                                          *
3577
 *  "test" command runs a test suite of problems and        *
3578
 *  displays results.                                       *
3579
 *                                                          *
3580
 ************************************************************
3581
 */
3582
  else if (OptionMatch("test", *args)) {
108 pmbaty 3583
    FILE *unsolved = NULL;
3584
    int save_noise, save_display;
3585
 
33 pmbaty 3586
    if (thinking || pondering)
3587
      return 2;
108 pmbaty 3588
    nargs = ReadParse(buffer, args, " \t;=");
33 pmbaty 3589
    if (nargs < 2) {
3590
      printf("usage:  test <filename> [exitcnt]\n");
3591
      return 1;
3592
    }
108 pmbaty 3593
    save_noise = noise_level;
3594
    save_display = display_options;
33 pmbaty 3595
    if (nargs > 2)
3596
      early_exit = atoi(args[2]);
108 pmbaty 3597
    if (nargs > 3)
3598
      unsolved = fopen(args[3], "w+");
3599
    Test(args[1], unsolved, 0, 0);
3600
    noise_level = save_noise;
3601
    display_options = save_display;
33 pmbaty 3602
    ponder_move = 0;
3603
    last_pv.pathd = 0;
3604
    last_pv.pathl = 0;
108 pmbaty 3605
    if (unsolved)
3606
      fclose(unsolved);
33 pmbaty 3607
  }
3608
/*
3609
 ************************************************************
3610
 *                                                          *
3611
 *  "time" is used to set the basic search timing controls. *
3612
 *  The general form of the command is as follows:          *
3613
 *                                                          *
3614
 *    time nmoves/ntime/[nmoves/ntime]/[increment]          *
3615
 *                                                          *
3616
 *  nmoves/ntime represents a traditional first time        *
3617
 *  control when nmoves is an integer representing the      *
3618
 *  number of moves and ntime is the total time allowed for *
3619
 *  these moves.  The [optional] nmoves/ntime is a          *
3620
 *  traditional secondary time control.  Increment is a     *
3621
 *  feature related to ics play and emulates the fischer    *
3622
 *  clock where "increment" is added to the time left after *
3623
 *  each move is made.                                      *
3624
 *                                                          *
3625
 *  As an alternative, nmoves can be "sd" which represents  *
3626
 *  a "sudden death" time control of the remainder of the   *
3627
 *  game played in ntime.  The optional secondary time      *
3628
 *  control can be a sudden-death time control, as in the   *
3629
 *  following example:                                      *
3630
 *                                                          *
3631
 *    time 60/30/sd/30                                      *
3632
 *                                                          *
3633
 *  This sets 60 moves in 30 minutes, then game in 30       *
3634
 *  additional minutes.  An increment can be added if       *
3635
 *  desired.                                                *
3636
 *                                                          *
3637
 ************************************************************
3638
 */
3639
  else if (OptionMatch("time", *args)) {
3640
    if (xboard) {
108 pmbaty 3641
      tc_time_remaining[Flip(game_wtm)] = atoi(args[1]);
33 pmbaty 3642
      if (log_file && time_limit > 99)
3643
        fprintf(log_file, "time remaining: %s (Crafty).\n",
108 pmbaty 3644
            DisplayTime(tc_time_remaining[Flip(game_wtm)]));
33 pmbaty 3645
    } else {
3646
      if (thinking || pondering)
3647
        return 2;
3648
      tc_moves = 60;
3649
      tc_time = 180000;
3650
      tc_moves_remaining[white] = 60;
3651
      tc_moves_remaining[black] = 60;
3652
      tc_time_remaining[white] = 180000;
3653
      tc_time_remaining[black] = 180000;
3654
      tc_secondary_moves = 60;
3655
      tc_secondary_time = 180000;
3656
      tc_increment = 0;
3657
      tc_sudden_death = 0;
3658
/*
3659
 first let's pick off the basic time control (moves/minutes)
3660
 */
3661
      if (nargs > 1)
3662
        if (!strcmp(args[1], "sd")) {
3663
          tc_sudden_death = 1;
3664
          tc_moves = 1000;
3665
        }
3666
      if (nargs > 2) {
3667
        tc_moves = atoi(args[1]);
3668
        tc_time = atoi(args[2]) * 100;
3669
      }
3670
/*
3671
 now let's pick off the secondary time control (moves/minutes)
3672
 */
3673
      tc_secondary_time = tc_time;
3674
      tc_secondary_moves = tc_moves;
3675
      if (nargs > 4) {
3676
        if (!strcmp(args[3], "sd")) {
3677
          tc_sudden_death = 2;
3678
          tc_secondary_moves = 1000;
3679
        } else
3680
          tc_secondary_moves = atoi(args[3]);
3681
        tc_secondary_time = atoi(args[4]) * 100;
3682
      }
3683
      if (nargs > 5)
3684
        tc_increment = atoi(args[5]) * 100;
3685
      tc_time_remaining[white] = tc_time;
3686
      tc_time_remaining[black] = tc_time;
3687
      tc_moves_remaining[white] = tc_moves;
3688
      tc_moves_remaining[black] = tc_moves;
3689
      if (!tc_sudden_death) {
108 pmbaty 3690
        Print(32, "%d moves/%d minutes primary time control\n", tc_moves,
33 pmbaty 3691
            tc_time / 100);
108 pmbaty 3692
        Print(32, "%d moves/%d minutes secondary time control\n",
33 pmbaty 3693
            tc_secondary_moves, tc_secondary_time / 100);
3694
        if (tc_increment)
108 pmbaty 3695
          Print(32, "increment %d seconds.\n", tc_increment / 100);
33 pmbaty 3696
      } else if (tc_sudden_death == 1) {
108 pmbaty 3697
        Print(32, " game/%d minutes primary time control\n", tc_time / 100);
33 pmbaty 3698
        if (tc_increment)
108 pmbaty 3699
          Print(32, "increment %d seconds.\n", tc_increment / 100);
33 pmbaty 3700
      } else if (tc_sudden_death == 2) {
108 pmbaty 3701
        Print(32, "%d moves/%d minutes primary time control\n", tc_moves,
33 pmbaty 3702
            tc_time / 100);
108 pmbaty 3703
        Print(32, "game/%d minutes secondary time control\n",
33 pmbaty 3704
            tc_secondary_time / 100);
3705
        if (tc_increment)
108 pmbaty 3706
          Print(32, "increment %d seconds.\n", tc_increment / 100);
33 pmbaty 3707
      }
3708
      tc_time *= 60;
3709
      tc_time_remaining[white] *= 60;
3710
      tc_time_remaining[black] *= 60;
3711
      tc_secondary_time *= 60;
3712
      tc_safety_margin = tc_time / 6;
3713
    }
3714
  }
3715
/*
3716
 ************************************************************
3717
 *                                                          *
3718
 *  "timebook" command is used to adjust Crafty's time      *
3719
 *  usage after it leaves the opening book.  The first      *
3720
 *  value specifies the multiplier for the time added to    *
3721
 *  the first move out of book expressed as a percentage    *
3722
 *  (100 is 100% for example).  The second value specifies  *
3723
 *  the "span" (number of moves) that this multiplier       *
3724
 *  decays over.  For example, "timebook 100 10" says to    *
3725
 *  add 100% of the normal search time for the first move   *
3726
 *  out of book, then 90% for the next, until after 10      *
3727
 *  non-book moves have been played, the percentage has     *
3728
 *  dropped back to 0 where it will stay for the rest of    *
3729
 *  the game.                                               *
3730
 *                                                          *
3731
 ************************************************************
3732
 */
3733
  else if (OptionMatch("timebook", *args)) {
3734
    if (nargs < 3) {
3735
      printf("usage:  timebook <percentage> <move span>\n");
3736
      return 1;
3737
    }
3738
    first_nonbook_factor = atoi(args[1]);
3739
    first_nonbook_span = atoi(args[2]);
3740
    if (first_nonbook_factor < 0 || first_nonbook_factor > 500) {
3741
      Print(4095, "ERROR, factor must be >= 0 and <= 500\n");
3742
      first_nonbook_factor = 0;
3743
    }
3744
    if (first_nonbook_span < 0 || first_nonbook_span > 30) {
3745
      Print(4095, "ERROR, span must be >= 0 and <= 30\n");
3746
      first_nonbook_span = 0;
3747
    }
3748
  }
3749
/*
3750
 ************************************************************
3751
 *                                                          *
3752
 *  "trace" command sets the search trace level which will  *
3753
 *  dump the tree as it is searched.                        *
3754
 *                                                          *
3755
 ************************************************************
3756
 */
3757
  else if (OptionMatch("trace", *args)) {
3758
#if !defined(TRACE)
3759
    printf
3760
        ("Sorry, but I can't display traces unless compiled with -DTRACE\n");
3761
#endif
3762
    if (nargs < 2) {
3763
      printf("usage:  trace <depth>\n");
3764
      return 1;
3765
    }
3766
    trace_level = atoi(args[1]);
3767
    printf("trace=%d\n", trace_level);
3768
  }
3769
/*
3770
 ************************************************************
3771
 *                                                          *
3772
 *  "undo" command backs up 1/2 move, which leaves the      *
3773
 *  opposite side on move. [xboard compatibility]           *
3774
 *                                                          *
3775
 ************************************************************
3776
 */
108 pmbaty 3777
  else if (OptionMatch("undo", *args)) {
33 pmbaty 3778
    if (thinking || pondering)
3779
      return 2;
3780
    if (!game_wtm || move_number != 1) {
3781
      game_wtm = Flip(game_wtm);
3782
      if (Flip(game_wtm))
3783
        move_number--;
108 pmbaty 3784
      sprintf(buffer, "reset %d", move_number);
3785
      Option(tree);
33 pmbaty 3786
    }
3787
  }
3788
/*
108 pmbaty 3789
 ************************************************************
3790
 *                                                          *
3791
 *  "usage" command controls the time usage multiplier      *
3792
 *  factors used in the game  - percentage increase or      *
3793
 *  decrease in time used up front.  Enter a number between *
3794
 *  1 to 100 for the % decrease (negative value) or to      *
3795
 *  increase (positive value) although other time           *
3796
 *  limitation controls may kick in.  This more commonly    *
3797
 *  used in the .craftyrc/crafty.rc file.                   *
3798
 *                                                          *
3799
 ************************************************************
33 pmbaty 3800
 */
3801
  else if (OptionMatch("usage", *args)) {
3802
    if (nargs < 2) {
3803
      printf("usage:  usage <percentage>\n");
3804
      return 1;
3805
    }
3806
    usage_level = atoi(args[1]);
3807
    if (usage_level > 50)
3808
      usage_level = 50;
3809
    else if (usage_level < -50)
3810
      usage_level = -50;
108 pmbaty 3811
    Print(32, "time usage up front set to %d percent increase/(-)decrease.\n",
33 pmbaty 3812
        usage_level);
3813
  }
3814
/*
3815
 ************************************************************
3816
 *                                                          *
3817
 *  "variant" command sets the wild variant being played    *
3818
 *  on a chess server.  [xboard compatibility].             *
3819
 *                                                          *
3820
 ************************************************************
3821
 */
3822
  else if (OptionMatch("variant", *args)) {
3823
    if (thinking || pondering)
3824
      return 2;
3825
    printf("command=[%s]\n", buffer);
3826
    return -1;
3827
  }
3828
/*
3829
 ************************************************************
3830
 *                                                          *
3831
 *  "whisper" command sets whisper mode for ICS.  =1 will   *
3832
 *  whisper mate announcements, =2 will whisper scores and  *
3833
 *  other info, =3 will whisper scores and PV, =4 adds the  *
3834
 *  list of book moves, =5 displays the PV after each       *
3835
 *  iteration completes, and =6 displays the PV each time   *
3836
 *  it changes in an iteration.                             *
3837
 *                                                          *
3838
 ************************************************************
3839
 */
3840
  else if (OptionMatch("whisper", *args)) {
3841
    if (nargs < 2) {
3842
      printf("usage:  whisper <level>\n");
3843
      return 1;
3844
    }
108 pmbaty 3845
    kibitz = 16 + Min(0, atoi(args[1]));
33 pmbaty 3846
  }
3847
/*
3848
 ************************************************************
3849
 *                                                          *
108 pmbaty 3850
 *  "white" command sets white to move (wtm).               *
3851
 *                                                          *
3852
 ************************************************************
3853
 */
3854
  else if (OptionMatch("white", *args)) {
3855
    if (thinking || pondering)
3856
      return 2;
3857
    ponder_move = 0;
3858
    last_pv.pathd = 0;
3859
    last_pv.pathl = 0;
3860
    if (!game_wtm)
3861
      Pass();
3862
    force = 0;
3863
  }
3864
/*
3865
 ************************************************************
3866
 *                                                          *
33 pmbaty 3867
 *  "wild" command sets up an ICS wild position (only 7 at  *
3868
 *  present, but any can be added easily, except for those  *
3869
 *  that Crafty simply can't play (two kings, invisible     *
3870
 *  pieces, etc.)                                           *
3871
 *                                                          *
3872
 ************************************************************
3873
 */
3874
  else if (OptionMatch("wild", *args)) {
3875
    int i;
3876
 
3877
    if (nargs < 2) {
3878
      printf("usage:  wild <value>\n");
3879
      return 1;
3880
    }
3881
    i = atoi(args[1]);
3882
    switch (i) {
3883
      case 7:
108 pmbaty 3884
        strcpy(buffer, "setboard 4k/5ppp/////PPP/3K/ w");
3885
        Option(tree);
33 pmbaty 3886
        break;
3887
      default:
3888
        printf("sorry, only wild7 implemented at present\n");
3889
        break;
3890
    }
3891
  }
3892
/*
3893
 ************************************************************
3894
 *                                                          *
3895
 *  "xboard" command is normally invoked from main() via    *
3896
 *  the xboard command-line option.  It sets proper         *
3897
 *  defaults for Xboard interface requirements.             *
3898
 *                                                          *
3899
 ************************************************************
3900
 */
3901
  else if (OptionMatch("xboard", *args) || OptionMatch("winboard", *args)) {
3902
    if (!xboard) {
3903
      signal(SIGINT, SIG_IGN);
3904
      xboard = 1;
108 pmbaty 3905
      display_options = 2048;
3906
      Print(-1, "\n");
3907
      Print(-1, "tellicsnoalias set 1 Crafty v%s (%d cpus)\n", version, Max(1,
33 pmbaty 3908
              smp_max_threads));
108 pmbaty 3909
      Print(-1, "tellicsnoalias kibitz Hello from Crafty v%s! (%d cpus)\n",
33 pmbaty 3910
          version, Max(1, smp_max_threads));
3911
    }
3912
  }
3913
/*
3914
 ************************************************************
3915
 *                                                          *
3916
 *  "?" command does nothing, but since this is the "move   *
3917
 *  now" keystroke, if Crafty is not searching, this will   *
3918
 *  simply "wave it off" rather than produce an error.      *
3919
 *                                                          *
3920
 ************************************************************
3921
 */
3922
  else if (OptionMatch("?", *args)) {
3923
  }
3924
/*
3925
 ************************************************************
3926
 *                                                          *
3927
 *  unknown command, it must be a move.                     *
3928
 *                                                          *
3929
 ************************************************************
3930
 */
3931
  else
3932
    return 0;
3933
/*
3934
 ************************************************************
3935
 *                                                          *
3936
 *  command executed, return for another.                   *
3937
 *                                                          *
3938
 ************************************************************
3939
 */
3940
  return 1;
3941
}
3942
 
3943
/*
3944
 *******************************************************************************
3945
 *                                                                             *
3946
 *   OptionMatch() is used to recognize user commands.  It requires that the   *
3947
 *   command (text input which is the *2nd parameter* conform to the following *
3948
 *   simple rules:                                                             *
3949
 *                                                                             *
3950
 *     1.  The input must match the command, starting at the left-most         *
3951
 *         character.                                                          *
3952
 *     2.  If the command starts with a sequence of characters that could      *
3953
 *         be interpreted as a chess move as well (re for reset and/or rook    *
3954
 *         to the e-file) then the input must match enough of the command      *
3955
 *         to get past the ambiguity (res would be minimum we will accept      *
3956
 *         for the reset command.)                                             *
3957
 *                                                                             *
3958
 *******************************************************************************
3959
 */
3960
int OptionMatch(char *command, char *input) {
3961
/*
3962
 ************************************************************
3963
 *                                                          *
3964
 *  check for the obvious exact match first.                *
3965
 *                                                          *
3966
 ************************************************************
3967
 */
3968
  if (!strcmp(command, input))
3969
    return 1;
3970
/*
3971
 ************************************************************
3972
 *                                                          *
3973
 *  now use strstr() to see if "input" is in "command." the *
3974
 *  first requirement is that input matches command         *
3975
 *  starting at the very left-most character.               *
3976
 *                                                          *
3977
 ************************************************************
3978
 */
3979
  if (strstr(command, input) == command)
3980
    return 1;
3981
  return 0;
3982
}
108 pmbaty 3983
 
33 pmbaty 3984
void OptionPerft(TREE * RESTRICT tree, int ply, int depth, int wtm) {
108 pmbaty 3985
  unsigned *mv;
33 pmbaty 3986
#if defined(TRACE)
3987
  static char line[256];
3988
  static char move[16], *p[64];
3989
#endif
108 pmbaty 3990
 
33 pmbaty 3991
  tree->last[ply] = GenerateCaptures(tree, ply, wtm, tree->last[ply - 1]);
3992
  for (mv = tree->last[ply - 1]; mv < tree->last[ply]; mv++)
3993
    if (Captured(*mv) == king)
3994
      return;
3995
  tree->last[ply] = GenerateNoncaptures(tree, ply, wtm, tree->last[ply]);
3996
#if defined(TRACE)
3997
  p[1] = line;
3998
#endif
3999
  for (mv = tree->last[ply - 1]; mv < tree->last[ply]; mv++) {
4000
#if defined(TRACE)
108 pmbaty 4001
    strcpy(move, OutputMove(tree, ply, wtm, *mv));
33 pmbaty 4002
#endif
108 pmbaty 4003
    MakeMove(tree, ply, wtm, *mv);
33 pmbaty 4004
#if defined(TRACE)
4005
    if (ply <= trace_level) {
4006
      strcpy(p[ply], move);
108 pmbaty 4007
      strcpy(line + strlen(line), " ");
33 pmbaty 4008
      p[ply + 1] = line + strlen(line);
4009
      if (ply == trace_level)
4010
        printf("%s\n", line);
4011
    }
4012
#endif
4013
    if (depth - 1)
4014
      OptionPerft(tree, ply + 1, depth - 1, Flip(wtm));
4015
    else if (!Check(wtm))
4016
      total_moves++;
108 pmbaty 4017
    UnmakeMove(tree, ply, wtm, *mv);
33 pmbaty 4018
  }
4019
}