Subversion Repositories Games.Chess Giants

Rev

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

Rev Author Line No. Line
33 pmbaty 1
#include "chess.h"
2
#include "data.h"
3
#if defined(UNIX)
4
#  include <unistd.h>
5
#  include <pwd.h>
6
#  include <sys/types.h>
7
#endif
8
#include <signal.h>
9
/* last modified 02/24/14 */
10
/*
11
 *******************************************************************************
12
 *                                                                             *
108 pmbaty 13
 *  Crafty, copyright 1996-2015 by Robert M. Hyatt, Ph.D., Associate Professor *
33 pmbaty 14
 *  of Computer and Information Sciences, University of Alabama at Birmingham. *
15
 *                                                                             *
16
 *  Crafty is a team project consisting of the following members.  These are   *
17
 *  the people involved in the continuing development of this program, there   *
18
 *  are no particular members responsible for any specific aspect of Crafty,   *
19
 *  although R. Hyatt wrote 99%+ of the existing code, excepting the Magic .   *
108 pmbaty 20
 *  move stuff by Pradu Kaanan, egtb.cpp written by Eugene Nalimov, and the    *
21
 *  epd stuff written by S. Edwards.                                           *
33 pmbaty 22
 *                                                                             *
23
 *     Robert Hyatt, University of Alabama at Birmingham.                      *
108 pmbaty 24
 *     Mike Byrne, Pen Argyl, PA.                                              *
33 pmbaty 25
 *     Tracy Riegle, Hershey, PA.                                              *
26
 *     Peter Skinner, Edmonton, AB  Canada.                                    *
27
 *                                                                             *
28
 *  All rights reserved.  No part of this program may be reproduced in any     *
29
 *  form or by any means, for other than your personal use, without the        *
30
 *  express written permission of the authors.  This program may not be used   *
31
 *  in whole, nor in part, to enter any computer chess competition without     *
32
 *  written permission from the authors.  Such permission will include the     *
33
 *  requirement that the program be entered under the name "Crafty" so that    *
34
 *  the program's ancestry will be known.                                      *
35
 *                                                                             *
36
 *  Copies of the source must contain the original copyright notice intact.    *
37
 *                                                                             *
38
 *  Any changes made to this software must also be made public to comply with  *
39
 *  the original intent of this software distribution project.  These          *
40
 *  restrictions apply whether the distribution is being done for free or as   *
41
 *  part or all of a commercial product.  The authors retain sole ownership    *
42
 *  and copyright on this program except for 'personal use' explained below.   *
43
 *                                                                             *
44
 *  Personal use includes any use you make of the program yourself, either by  *
45
 *  playing games with it yourself, or allowing others to play it on your      *
46
 *  machine,  and requires that if others use the program, it must be clearly  *
47
 *  identified as "Crafty" to anyone playing it (on a chess server as one      *
48
 *  example).  Personal use does not allow anyone to enter this into a chess   *
49
 *  tournament where other program authors are invited to participate.  IE you *
50
 *  can do your own local tournament, with Crafty + other programs, since this *
51
 *  is for your personal enjoyment.  But you may not enter Crafty into an      *
52
 *  event where it will be in competition with other programs/programmers      *
53
 *  without permission as stated previously.                                   *
54
 *                                                                             *
55
 *  Crafty is the "son" (direct descendent) of Cray Blitz.  it is designed     *
56
 *  totally around the bit-board data structure for reasons of speed of ex-    *
57
 *  ecution, ease of adding new knowledge, and a *significantly* cleaner       *
58
 *  overall design.  it is written totally in ANSI C with some few UNIX system *
59
 *  calls required for I/O, etc.                                               *
60
 *                                                                             *
61
 *  main() is the driver for the chess program.  Its primary function is to    *
62
 *  cycle between asking the user for a move and calling for a tree search     *
63
 *  to produce a move for the program.  After accepting an input string from   *
64
 *  the user, this string is first passed to Option() which checks to see if   *
65
 *  it is a command to the program.  If Option() returns a non-zero result,    *
66
 *  the input was a command and was executed, otherwise the input should be    *
67
 *  passed to input() for conversion to the internal move format.              *
68
 *                                                                             *
69
 *  The following diagram shows how bit numbers / squares match up in Crafty's *
70
 *  bitboard arrays.  Note that bit zero (LSB) corresponds to square A, which  *
71
 *  makes this a mental challenge to take a 64 bit value and visualize it as   *
72
 *  chess board, since the files are reversed.  This was done for the          *
73
 *  following reasons:  (1) bit 0 needs to be the LSB, and bit 63 needs to be  *
74
 *  the MSB so that the Intel BSF/BSR instructions return the proper square    *
75
 *  number without any remapping.  (2) the lower 3 bits (file number) are now  *
76
 *  0 for A, 1 for B, ..., 7 for H.  The upper 3 bits (rank number) are then   *
77
 *  0 for rank 1, 1 for rank 2, ..., 7 for rank 8.                             *
78
 *                                                                             *
79
 *  A8 B8 C8 D8 E8 F8 G8 H8                                                    *
80
 *  A7 B7 C7 D7 E7 F7 G7 H7                                                    *
81
 *  A6 B6 C6 D6 E6 F6 G6 H6                                                    *
82
 *  A5 B5 C5 D5 E5 F5 G5 H5                                                    *
83
 *  A4 B4 C4 D4 E4 F4 G4 H4                                                    *
84
 *  A3 B3 C3 D3 E3 F3 G3 H3                                                    *
85
 *  A2 B2 C2 D2 E2 F2 G2 H2                                                    *
86
 *  A1 B1 C1 D1 E1 F1 G1 H1                                                    *
87
 *                                                                             *
88
 *  56 57 58 59 60 61 62 63                                                    *
89
 *  48 49 50 51 52 53 54 55                                                    *
90
 *  40 41 42 43 44 45 46 47                                                    *
91
 *  32 33 34 35 36 37 38 39                                                    *
92
 *  24 25 26 27 28 29 30 31                                                    *
93
 *  16 17 18 19 20 21 22 23                                                    *
94
 *   8  9 10 11 12 13 14 15                                                    *
95
 *   0  1  2  3  4  5  6  7                                                    *
96
 *                                                                             *
97
 *  version  description                                                       *
98
 *                                                                             *
99
 *    1.0    First version of the bit-board program to play a legitimate game  *
100
 *           even though significant features are missing:  transposition      *
101
 *           table, sophisticated scoring, etc.                                *
102
 *                                                                             *
103
 *    1.1    Added the minimal window search.  At each ply in the tree, the    *
104
 *           first branch is searched with the normal alpha/beta window, while *
105
 *           remaining nodes are searched with value/value+1 which was         *
106
 *           returned from searching the first branch.  If such a search       *
107
 *           returns the upper bound, this position is once again searched     *
108
 *           with the normal window.                                           *
109
 *                                                                             *
110
 *    1.2    Added the classic null-move search.  The program searches the     *
111
 *           sub-tree below the null move (typically) one play shallower than  *
112
 *           the normal search, saving a lot of time.                          *
113
 *                                                                             *
114
 *    1.3    Added the transposition table support.  This also includes the    *
115
 *           code in Iterate() necessary to propagate the principal variation  *
116
 *           from one iteration to the next to improve move ordering.          *
117
 *                                                                             *
118
 *    1.4    Modified the transposition table to use three tables, one for 64  *
119
 *           bits of white entry, one for 64 bits of black entry, and one with *
120
 *           the remaining 32 bits of white and black entry combined into one  *
121
 *           64 bit word.  Eliminated the "bit fields" since they seemed to be *
122
 *           erratic on most compilers and made portability nearly impossible. *
123
 *                                                                             *
124
 *    1.5    Pawn scoring added.  This required three additions to the code:   *
125
 *           (1) InitializePawnMasks() which produces the necessary masks to   *
126
 *           let us efficiently detect isolated, backward, passed, etc. pawns; *
127
 *           (2) a pawn hash table to store recently computed pawn scores so   *
128
 *           it won't be too expensive to do complex analysis;  (3) Evaluate() *
129
 *           now evaluates pawns if the current pawn position is not in the    *
130
 *           pawn hash table.                                                  *
131
 *                                                                             *
132
 *    1.6    Piece scoring added, although it is not yet complete.  Also, the  *
133
 *           search now uses the familiar zero-width window search for all but *
134
 *           the first move at the root, in order to reduce the size of the    *
135
 *           tree to a nearly optimal size.  This means that a move that is    *
136
 *           only one point better than the first move will "fail high" and    *
137
 *           have to be re-searched a second time to get the true value.  If   *
138
 *           the new best move is significantly better, it may have to be      *
139
 *           searched a third time to increase the window enough to return the *
140
 *           score.                                                            *
141
 *                                                                             *
142
 *    1.7    Replaced the old "killer" move ordering heuristic with the newer  *
143
 *           "history" ordering heuristic.   This version uses the 12-bit key  *
144
 *           formed by <from><to> to index into the history table.             *
145
 *                                                                             *
146
 *    1.8    Added pondering for both PC and UNIX-based machines.  Also other  *
147
 *           improvements include the old Cray Blitz algorithm that takes the  *
148
 *           P.V. returned by the tree search, and "extends" it by looking up  *
149
 *           positions in the transposition table, which improves move         *
150
 *           ordering significantly.  Repetitions are now handled correctly.   *
151
 *                                                                             *
152
 *    1.9    Added an opening book with flags to control which moves are       *
153
 *           selected for play.  The book maintenance code is stubbed directly *
154
 *           into the program, so that no additional executables are needed.   *
155
 *                                                                             *
156
 *    1.10   Added king safety + hashing (as done in the old Cray Blitz).      *
157
 *           Nothing novel other than the use of bit-masks to speed up some of *
158
 *           the tests.                                                        *
159
 *                                                                             *
160
 *    1.11   Additional evaluation knowledge plus log_file so that program     *
161
 *           saves a record of the game statistics for later analysis.  Added  *
162
 *           the "annotate" command to make the program analyze a game or part *
163
 *           of a game for testing/debugging.                                  *
164
 *                                                                             *
165
 *    1.12   Added ICS (Internet Chess Server) support for Xboard support so   *
166
 *           that Crafty can play on ICS in an automated manner.  Added new    *
167
 *           commands to be compatible with Xboard.  Crafty also has a new     *
168
 *           command-line interface that allows options to be entered on the   *
169
 *           command-line directly, ie:  "crafty alarm=off verbose=2" will     *
170
 *           start the program, set the move alarm off, and set verbose to     *
171
 *           2.  This allows an "alias" to easily customize the program.       *
172
 *                                                                             *
173
 *    1.13   Added time-control logic to the program.  It now monitors how     *
174
 *           much time both it and its opponent uses when thinking about a     *
175
 *           move to make.  When pondering, it only times itself from the      *
176
 *           point that a move is actually entered.                            *
177
 *                                                                             *
178
 *    1.14   Added the piece/square tables to the program to move static       *
179
 *           scoring to the root of the tree since it never changes (things    *
180
 *           like centralization of pieces, etc.)  also some minor tuning of   *
181
 *           numbers to bring positional score "swings" down some.             *
182
 *                                                                             *
183
 *    1.15   Added edit so that positions can be altered (and so ICS games     *
184
 *           can be resumed after they are adjourned).  Also added a "force"   *
185
 *           command to force the program to play a different move than the    *
186
 *           search selected.  Search timing allocation slightly altered to    *
187
 *           improve ICS performance.                                          *
188
 *                                                                             *
189
 *    1.16   Significant adjustments to evaluation weights to bring positional *
190
 *           scores down to below a pawn for "normal" positions.  Some small   *
191
 *           "tweaks" to king safety as well to keep the king safer.           *
192
 *                                                                             *
193
 *    1.17   Added "development" evaluation routine to encourage development   *
194
 *           and castling before starting on any tactical "sorties."  Also     *
195
 *           repaired many "bugs" in evaluation routines.                      *
196
 *                                                                             *
197
 *    1.18   Added the famous Cray Blitz "square of the king" passed pawn      *
198
 *           evaluation routine.  This will evaluate positions where one       *
199
 *           side has passed pawns and the other side has no pieces, to see    *
200
 *           if the pawn can outrun the defending king and promote.  Note that *
201
 *           this is ridiculously fast because it only requires one AND        *
202
 *           to declare that a pawn can't be caught!                           *
203
 *                                                                             *
204
 *    2.0    initial version preparing for search extension additions.  This   *
205
 *           version has a new "next_evasion()" routine that is called when    *
206
 *           the king is in check.  It generates sensible (nearly all are      *
207
 *           legal) moves which include capturing the checking piece (if there *
208
 *           is only one), moving the king (but not along the checking ray),   *
209
 *           and interpositions that block the checking ray (if there is only  *
210
 *           one checking piece.                                               *
211
 *                                                                             *
212
 *    2.1    Search is now broken down into three cleanly-defined phases:      *
213
 *           (1) basic full-width search [Search()]; (2) extended tactical     *
214
 *           search [extend()] which includes winning/even captures and then   *
215
 *           passed pawn pushes to the 6th or 7th rank (if the pawn pushes     *
216
 *           are safe);  (3) normal quiescence search [Quiesce()] which only   *
217
 *           includes captures that appear to gain material.                   *
218
 *                                                                             *
219
 *    2.2    King safety code re-written and cleaned up.  Crafty was giving    *
220
 *           multiple penalties which was causing the king safety score to be  *
221
 *           extremely large (and unstable.)  Now, if it finds something wrong *
222
 *           with king safety, it only penalizes a weakness once.              *
223
 *                                                                             *
224
 *    2.3    King safety code modified further, penalties were significant     *
225
 *           enough to cause search anomolies.  Modified rook scoring to avoid *
226
 *           having the rook trapped in the corner when the king is forced to  *
227
 *           move rather than castle, and to keep the rook in a position to be *
228
 *           able to reach open files in one move, which avoids getting them   *
229
 *           into awkward positions where they become almost worthless.        *
230
 *                                                                             *
231
 *    2.4    King safety code completely rewritten.  It now analyzes "defects" *
232
 *           in the king safety field and counts them as appropriate.  These   *
233
 *           defects are then summed up and used as an index into a vector of  *
234
 *           of values that turns them into a score in a non-linear fashion.   *
235
 *           Increasing defects quickly "ramp up" the score to about 1/3+ of   *
236
 *           a pawn, then additional faults slowly increase the penalty.       *
237
 *                                                                             *
238
 *    2.5    First check extensions added.  In the extension search (stage     *
239
 *           between full-width and captures-only, up to two checking moves    *
240
 *           can be included, if there are checks in the full-width part of    *
241
 *           the search.  If only one check occurs in the full-width, then     *
242
 *           only one check will be included in the extension phase of the     *
243
 *           selective search.                                                 *
244
 *                                                                             *
245
 *    2.6    Evaluation modifications attempting to cure Crafty's frantic      *
246
 *           effort to develop all its pieces without giving a lot of regard   *
247
 *           to the resulting position until after the pieces are out.         *
248
 *                                                                             *
249
 *    2.7    New evaluation code to handle the "outside passed pawn" concept.  *
250
 *           Crafty now understands that a passed pawn on the side away from   *
251
 *           the rest of the pawns is a winning advantage due to decoying the  *
252
 *           king away from the pawns to prevent the passer from promoting.    *
253
 *           The advantage increases as material is removed from the board.    *
254
 *                                                                             *
255
 *    3.0    The 3.* series of versions will primarily be performance en-      *
256
 *           hancements.  The first version (3.0) has a highly modified        *
257
 *           version of MakeMove() that tries to do no unnecessary work.  It   *
258
 *           is about 25% faster than old version of MakeMove() which makes    *
259
 *           Crafty roughly 10% faster.  Also calls to Mask() have been        *
260
 *           replaced by constants (which are replaced by calls to Mask() on   *
261
 *           the Crays for speed) to eliminate function calls.                 *
262
 *                                                                             *
263
 *    3.1    Significantly modified king safety again, to better detect and    *
264
 *           react to king-side attacks.  Crafty now uses the recursive null-  *
265
 *           move search to depth-2 instead of depth-1, which results in       *
266
 *           slightly improved speed.                                          *
267
 *                                                                             *
268
 *    3.2    Null-move restored to depth-1.  Depth-2 proved unsafe as Crafty   *
269
 *           was overlooking tactical moves, particularly against itself,      *
270
 *           which lost several "won" games.                                   *
271
 *                                                                             *
272
 *    3.3    Additional king-safety work.  Crafty now uses the king-safety     *
273
 *           evaluation routines to compare king safety for both sides.  If    *
274
 *           one side is "in distress" pieces are attracted to that king in    *
275
 *           "big hurry" to either attack or defend.                           *
276
 *                                                                             *
277
 *    3.4    "Threat extensions" added.  Simply, this is a null-move search    *
278
 *           used to determine if a move is good only because it is a horizon  *
108 pmbaty 279
 *           effect type move.  We do a null move search after a move fails    *
33 pmbaty 280
 *           high anywhere in the tree.  The window is normally lowered by 1.5 *
281
 *           pawns, the idea being that if the fail-high move happens in a     *
282
 *           position that fails "really low" with a null move, then this move *
283
 *           might be a horizon move.  To test this, re-search this move with  *
284
 *           the depth increased by one.                                       *
285
 *                                                                             *
286
 *    3.5    50-move rule implemented.  A count of moves since the last pawn   *
287
 *           move or capture is kept as part of the position[] structure.  It  *
288
 *           is updated by MakeMove().  When this number reaches 100 (which    *
289
 *           in plies is 50 moves) Repeat() will return the draw indication    *
290
 *           immediately, just as though the position was a repetition draw.   *
291
 *                                                                             *
292
 *    3.6    Search extensions cleaned up to avoid excessive extensions which  *
293
 *           produced some wild variations, but which was also slowing things  *
294
 *           down excessively.                                                 *
295
 *                                                                             *
296
 *    3.7    Endgame strategy added.  Two specifics: KBN vs K has a piece/sq   *
297
 *           table that will drive the losing king to the correct corner.  For *
298
 *           positions with no pawns, scoring is altered to drive the losing   *
299
 *           king to the edge and corner for mating purposes.                  *
300
 *                                                                             *
301
 *    3.8    Hashing strategy modified.  Crafty now stores search value or     *
302
 *           bound, *and* positional evaluation in the transposition table.    *
303
 *           This avoids about 10-15% of the "long" evaluations during the     *
304
 *           middlegame, and avoids >75% of them in endgames.                  *
305
 *                                                                             *
306
 *    4.0    Evaluation units changed to "millipawns" where a pawn is now      *
307
 *           1000 rather than the prior 100 units.  This provides more         *
308
 *           "resolution" in the evaluation and will let Crafty have large     *
309
 *           positional bonuses for significant things, without having the     *
310
 *           small positional advantages add up and cause problems.  V3.8      *
311
 *           exhibited a propensity to "sac the exchange" frequently because   *
312
 *           of this.  It is hoped that this is a thing of the past now.       *
313
 *           Also, the "one legal reply to check" algorithm is now being used. *
314
 *           In short, if one side has only one legal reply to a checking move *
315
 *           then the other side is free to check again on the next ply. this  *
316
 *           only applies in the "extension" search, and allows some checks    *
317
 *           that extend() would normally not follow.                          *
318
 *                                                                             *
319
 *    4.1    Extension search modified.  It now "tracks" the basic iteration   *
320
 *           search depth up to some user-set limit (default=6).  For shallow  *
321
 *           depths, this speeds things up, while at deeper depths it lets the *
322
 *           program analyze forcing moves somewhat deeper.                    *
323
 *                                                                             *
324
 *    4.2    Extension search modified.  Fixed a problem where successive      *
325
 *           passed-pawn pushes were not extended correctly by extend() code.  *
326
 *           Threat-extension margin was wrong after changing the value of a   *
327
 *           pawn to 1000, it is now back to 1.5 pawns.                        *
328
 *                                                                             *
329
 *    4.3    Hash scoring "repaired."  As the piece/square tables changed, the *
330
 *           transposition table (positional evaluation component) along with  *
331
 *           the pawn and king-safety hash tables prevented the changes from   *
332
 *           affecting the score, since the hashed scores were based on the    *
333
 *           old piece/square table values.  Now, when any piece/square table  *
334
 *           is modified, the affected hash tables are cleared of evaluation   *
335
 *           information.                                                      *
336
 *                                                                             *
337
 *    4.4    Piece/square tables simplified, king tropism scoring moved to     *
338
 *           Evaluate() where it is computed dynamically now as it should be.  *
339
 *                                                                             *
340
 *    4.5    Book move selection algorithm replaced.  Crafty now counts the    *
341
 *           number of times each book move was played as the book file is     *
342
 *           created.  Since these moves come (typically) from GM games, the   *
343
 *           more frequently a move appears, the more likely it is to lead to  *
344
 *           a sound position.  Crafty then enumerates all possible book moves *
345
 *           for the current position, computes a probability distribution for *
346
 *           each move so that Crafty will select a move proportional to the   *
347
 *           number of times it was played in GM games (for example, if e4 was *
348
 *           played 55% of the time, then Crafty will play e4 55% of the time) *
349
 *           although the special case of moves played too infrequently is     *
350
 *           handled by letting the operator set a minimum threshold (say 5)   *
351
 *           Crafty won't play moves that are not popular (or are outright     *
352
 *           blunders as the case may be.)  Pushing a passed pawn to the 7th   *
353
 *           rank in the basic search [Search() module] now extends the search *
354
 *           by two plies unless we have extended this ply already (usually a  *
355
 *           check evasion) or unless we have extended the whole line to more  *
356
 *           than twice the nominal iteration depth.                           *
357
 *                                                                             *
358
 *    5.0    Selective search extensions removed.  With the extensions now in  *
359
 *           the basic full-width search, these became more a waste of time    *
360
 *           than anything useful, and removing them simplifies things quite   *
361
 *           a bit.                                                            *
362
 *                                                                             *
363
 *    5.1    Pondering logic now has a "puzzling" feature that is used when    *
364
 *           the search has no move to ponder.  It does a short search to find *
365
 *           a move and then ponders that move, permanently eliminating the    *
366
 *           "idle" time when it has nothing to ponder.                        *
367
 *                                                                             *
368
 *    5.2    Evaluation terms scaled down to prevent positional scores from    *
369
 *           reaching the point that sacrificing material to avoid simple      *
370
 *           positional difficulties begins to look attractive.                *
371
 *                                                                             *
372
 *    5.3    Performance improvement produced by avoiding calls to MakeMove()  *
373
 *           when the attack information is not needed.  Since the first test  *
374
 *           done in Quiesce() is to see if the material score is so bad that  *
375
 *           a normal evaluation and/or further search is futile, this test    *
376
 *           has been moved to inside the loop *before* Quiesce() is           *
377
 *           recursively called, avoiding the MakeMove() work only to          *
378
 *           discover that the resulting position won't be searched further.   *
379
 *                                                                             *
108 pmbaty 380
 *    5.4    New SEE() function that now understands indirect attacks through  *
33 pmbaty 381
 *           the primary attacking piece(s).  This corrects a lot of sloppy    *
382
 *           move ordering as well as make the "futility" cutoffs added in     *
383
 *           in version 5.3 much more reliable.                                *
384
 *                                                                             *
385
 *    5.5    Checks are now back in the quiescence search.  Crafty now stores  *
386
 *           a negative "draft" in the transposition table rather than forcing *
387
 *           any depth < 0 to be zero.  The null-move search now searches the  *
388
 *           null-move to depth-1 (R=1) rather than depth-2 (R=2) which seemed *
389
 *           to cause some tactical oversights in the search.                  *
390
 *                                                                             *
391
 *    5.6    Improved move ordering by using the old "killer move" idea.  An   *
392
 *           additional advantage is that the killers can be tried before      *
393
 *           generating any moves (after the captures.)  Quiescence now only   *
108 pmbaty 394
 *           includes "safe" checks (using SEE() to determine if it is a safe  *
33 pmbaty 395
 *           checking move.                                                    *
396
 *                                                                             *
397
 *    5.7    King safety now "hates" a pawn at b3/g3 (white) or b6/g6 (black)  *
398
 *           to try and avoid the resulting mate threats.                      *
399
 *                                                                             *
400
 *    5.8    EvaluateOutsidePassedPawns() fixed to correctly evaluate those    *
401
 *           positions where both sides have outside passed pawns.  Before     *
402
 *           this fix, it only evaluated positions where one side had a passed *
403
 *           pawn, which overlooked those won/lost positions where both sides  *
404
 *           have passed pawns but one is "distant" or "outside" the other.    *
405
 *                                                                             *
406
 *    5.9    Removed "futility" forward pruning.  Exhaustive testing has       *
407
 *           proved that this causes significant tactical oversights.          *
408
 *                                                                             *
409
 *    5.10   Added code to handle king and pawn vs king, which understands     *
410
 *           the cases where the pawn can't outrun the king, but has to be     *
411
 *           assisted along by the king.                                       *
412
 *                                                                             *
413
 *    5.11   Two king-safety changes.  (1) The program's king safety score is  *
414
 *           now "doubled" to make it much less likely that it will try to win *
415
 *           a pawn but destroy it's king-side.  (2) The attack threshold has  *
416
 *           been lowered which is used to key the program that it is now      *
417
 *           necessary to move pieces to defend the king-side, in an effort to *
418
 *           protect a king-side that is deemed somewhat unsafe.               *
419
 *                                                                             *
420
 *    5.12   Improved null-move code by computing the Knuth/Moore node type    *
421
 *           and then only trying null-moves at type 2 nodes.  This has        *
422
 *           resulted in a 2x speedup in test positions.                       *
423
 *                                                                             *
424
 *    5.13   Optimization to avoid doing a call to MakeMove() if it can be     *
425
 *           avoided by noting that the move is not good enough to bring the   *
426
 *           score up to an acceptable level.                                  *
427
 *                                                                             *
428
 *    5.14   Artificially isolated pawns recognized now, so that Crafty won't  *
429
 *           push a pawn so far it can't be defended.  Also, the bonus for a   *
430
 *           outside passed pawn was nearly doubled.                           *
431
 *                                                                             *
432
 *    5.15   Passed pawns supported by a king, or connected passed pawns now   *
433
 *           get a large bonus as they advance.  Minor fix to the ICC resume   *
434
 *           feature to try to avoid losing games on time.                     *
435
 *                                                                             *
436
 *    6.0    Converted to rotated bitboards to make attack generation *much*   *
437
 *           faster.  MakeMove() now can look up the attack bit vectors        *
438
 *           rather than computing them which is significantly faster.         *
439
 *                                                                             *
440
 *    6.1    Added a "hung piece" term to Evaluate() which penalizes the side  *
441
 *           to move if a piece is attacked by a lesser piece, or a piece is   *
442
 *           attacked and not defended at all.  Additionally, a new scoring    *
443
 *           term was added to detect a weak back rank (where the king does    *
444
 *           not attack an empty square on the 2nd rank, and there are no      *
445
 *           horizontally sliding pieces [rook or queen] on the first rank to  *
446
 *           guard against back-rank mates.                                    *
447
 *                                                                             *
448
 *    6.2    Modified the "futility" cutoff to (a) emulate the way the program *
449
 *           normally searches, but avoiding MakeMove() calls when possible,   *
450
 *           and (2) not searching a move near the horizon if the material     *
451
 *           score is hopeless.                                                *
452
 *                                                                             *
453
 *    6.3    Null-move code moved from NextMove() directly into Search().      *
454
 *           this results is a "cleaner" implementation, as well as fixing an  *
455
 *           error in the node type, caused by Search() thinking that after    *
456
 *           the first move tried fails to cause a cutoff, that suddenly this  *
457
 *           node is a type=3 node (Knuth and Moore).  This bug would then     *
458
 *           introduce null-moves into later nodes that are a waste of time.   *
459
 *                                                                             *
460
 *    6.4    Pawn scoring modified.  Passed pawns were scored low enough that  *
461
 *           Crafty would let the opponent create one in the endgame (as long  *
462
 *           as it wasn't an "outside" passed pawn).  This required adjusting  *
463
 *           isolated pawns as well.  All "weak" pawns (pawns on open files    *
464
 *           that aren't defended by a pawn) are not penalized so much if      *
465
 *           there aren't any rooks/queens to attack on the file.              *
466
 *                                                                             *
467
 *    7.0    Removed the to.attack and from.attack bitboards.  These are now   *
468
 *           computed as needed, using the rotated bitboards.  Hung piece      *
469
 *           stuff removed due to lack of any verified benefit.                *
470
 *                                                                             *
471
 *    7.1    Modified next.c and nextc.c so that they check "mvv_lva_ordering" *
472
 *           to determine which capture ordering strategy to use.  Note that   *
473
 *           setting this to "1" (default at present) disables the forward     *
474
 *           pruning in quiescence search that wants to cull losing captures.  *
475
 *                                                                             *
476
 *    7.2    Major clean-up of MakeMove() using macros to make it easier to    *
477
 *           read and understand.  Ditto for other modules as well.  Fixed a   *
478
 *           bug in Evaluate() where bishop scoring failed in endgame          *
479
 *           situations, and discouraged king centralization.                  *
480
 *                                                                             *
481
 *    7.3    Evaluate() is no longer called if material is too far outside the *
482
 *           current alpha/beta window.  The time-consuming part of Evaluate() *
483
 *           is no longer done if the major scoring contributors in Evaluate() *
484
 *           haven't pulled the score within the alpha/beta window, and the    *
485
 *           remainder of Evaluate() can't possible accomplish this either.    *
486
 *           Gross error in EvaluatePawns() fixed, which would "forget" all    *
487
 *           of the pawn scoring done up to the point where doubled white      *
108 pmbaty 488
 *           pawns were found.  Error was xx=+ rather than xx+=, which had     *
33 pmbaty 489
 *           an extremely harmful effect on pawn structure evaluation.         *
490
 *                                                                             *
491
 *    7.4    Performance improvements produced by elimination of bit-fields.   *
492
 *           This was accomplished by hand-coding the necessary ANDs, ORs, and *
493
 *           SHIFTs necessary to accomplish the same thing, only faster.       *
494
 *                                                                             *
495
 *    7.5    Repetition code modified.  It could store at replist[-1] which    *
496
 *           clobbered the long long word just in front of this array.         *
497
 *                                                                             *
498
 *    7.6    Null move search now searches with R=2, unless within 3 plies     *
499
 *           of the quiescence search, then it searches nulls with R=1.        *
500
 *                                                                             *
501
 *    8.0    Re-vamp of evaluation, bringing scores back down so that Crafty   *
502
 *           will stop sacrificing the exchange, or a piece for two pawns just *
503
 *           it thinks it has lots of positional compensation.  The next few   *
504
 *           versions are going to be tuning or coding modifications to eval.  *
505
 *                                                                             *
506
 *    8.1    Futility() re-written to be more efficient, as well as to stop    *
507
 *           tossing moves out based on the +/- 2 pawn window, which was too   *
508
 *           speculative.  It still saves roughly 20% in speed over the same   *
509
 *           searches without futility, but seems to have *no* harmful effects *
510
 *           in tests run to date.                                             *
511
 *                                                                             *
512
 *    8.2    Futility() removed once and for all.  Since MakeMove() is so      *
513
 *           fast after the new attack generation algorithm, this was actually *
514
 *           slower than not using it.                                         *
515
 *                                                                             *
516
 *    8.3    EvaluatePawns() weak pawn analysis bug fixed.  Other minor        *
517
 *           performance enhancements and cleanup.                             *
518
 *                                                                             *
519
 *    8.4    Dynamic "king tropism" evaluation term now used, which varies     *
520
 *           based on how exposed the target king is.  The more exposed, the   *
521
 *           larger the bonus for placing pieces near the king.  New "analyze" *
522
 *           feature that complements the "annotate" feature that Crafty has   *
523
 *           had for a while.  Annotate is used to play over moves that are    *
524
 *           either in the current game history, or have been read in using    *
525
 *           the "read <filename>" command.  Analyze, on the other hand, will  *
526
 *           immediately begin searching the current position.  Each time the  *
527
 *           operator enters a move, it will make that move on the board, and  *
528
 *           then search the resulting position for the other side.  In effect *
529
 *           this gives a running commentary on a "game in progress".  These   *
530
 *           moves can be pumped into Crafty in many ways so that "on the fly" *
531
 *           analysis of a game-in-progress is possible.                       *
532
 *                                                                             *
533
 *    8.5    More cleanup.  Pawn promotions were searched twice, thanks to the *
534
 *           killer moves, although often they resulted in no search overhead  *
535
 *           due to transposition table hits the second time around.  Other    *
536
 *           minor fixes, including one serious "undefined variable" in        *
537
 *           Evaluate() that caused grief in endings.                          *
538
 *                                                                             *
539
 *    8.6    New book file structure.  It is no longer necessary to enter the  *
540
 *           "number of records" as Crafty now uses a new compressed hashing   *
541
 *           technique so that the book has no empty space in it, and the size *
542
 *           is computed "on the fly" producing a smaller book without the     *
543
 *           user having to compute the size.  Tweaks to Evaluate() to cure    *
544
 *           minor irritating habits.                                          *
545
 *                                                                             *
546
 *    8.7    Repaired optimization made in null-move search, that forgot to    *
547
 *           clear "EnPassant_Target".  A double pawn push, followed by a null *
548
 *           left the side on move "very confused" and would let it play an    *
549
 *           illegal enpassant capture in the tree. Sort.<n> files are now     *
550
 *           removed after book.bin is created.  Also, minor change to book    *
551
 *           format makes it incompatible with older book versions, but also   *
552
 *           allows book lines to be as long as desired, and the book size to  *
553
 *           reach any reasonable size.  Dynamic king tropism replaced by a    *
554
 *           dynamic computation, but with a static king tropism score, rather *
555
 *           than a score based on king exposure.  There was simply too much   *
556
 *           interaction, although this may prove workable later.              *
557
 *                                                                             *
558
 *    8.8    Tweaks to passed pawn scoring.  Scores were too low, making       *
559
 *           Crafty "ignore" them too much.                                    *
560
 *                                                                             *
561
 *    8.9    Internal iterative deepening is now used in those rare positions  *
562
 *           where a PV node has no hash move to search.  This does a shallow  *
563
 *           search to depth-2 to find a good move for ordering.               *
564
 *                                                                             *
565
 *    8.10   Internal interative deepening modified to handle cases where lots *
566
 *           of tactics make the deepening search fail high or low, and        *
567
 *           occasionally end up with no move to try.  This produced a "bad    *
568
 *           move from hash table" error.                                      *
569
 *                                                                             *
570
 *    8.11   Minor bug in internal iterative deepening fixed.  Also, macros    *
571
 *           for accessing the "position" data structure are now used to make  *
572
 *           things a little more readable.                                    *
573
 *                                                                             *
574
 *    8.12   Fixed minor bugs in quiescence checks, which let Crafty include   *
575
 *           more checks than intended (default quiescence_checks=2, but the   *
576
 *           comparison was <= before including a check, which made it include *
577
 *           three.  Additionally, a hashed check was *always* tried, even if  *
578
 *           quiescence_checks had already been satisfied.  Pawn scoring also  *
579
 *           had a bug, in that there was a "crack" between opening and middle *
580
 *           game, where Crafty would conclude that it was in an endgame, and  *
581
 *           adjust the pawn advance scores accordingly, sometimes causing an  *
582
 *           odd a4/a5/etc move "out of the blue."  Minor bug in next_capture  *
583
 *           was pruning even exchanges very early in quiescence search.  That *
584
 *           has now been removed, so only losing exchanges are pruned.        *
585
 *                                                                             *
586
 *    8.13   NextCapture() now *always* tries checking moves if the move at    *
587
 *           the previous ply was a null-move.  This avoids letting the null   *
588
 *           move hide some serious mating threats.  A minor bug where Crafty  *
589
 *           could draw by repetition after announcing a mate.  This was a     *
590
 *           result of finding a mate score in hash table, and, after the      *
591
 *           search iteration completed, the mate score would terminate the    *
592
 *           search completely.  Now, the search won't terminate until it      *
593
 *           finds a mate shorter than the previous search did.  Minor eval    *
594
 *           tweaks and bugfixes as well.                                      *
595
 *                                                                             *
596
 *    8.14   Checks after null moves discontinued.  Worked in some cases, but, *
597
 *           in general made the tree larger for nothing.  Eval tweaks to stop *
598
 *           sacs that resulted in connected passed pawns, often at the        *
599
 *           expense of a minor piece for a pawn or two, which usually lost.   *
600
 *           Mobility coeffecient increased for all pieces.  Asymmetric king   *
601
 *           safety discontinued.  King safety for both sides is now equally   *
602
 *           important.  King safety is now also proportional to the number of *
603
 *           pieces the other side has, so that a disrupted king-side will     *
604
 *           encourage trading pieces to reduce attacking chances.             *
605
 *                                                                             *
606
 *    8.15   Weak pawn scoring modified further.  The changes are designed to  *
607
 *           cause Crafty to keep pawns "mobile" where they can advance,       *
608
 *           rather than letting them become blocked or locked.                *
609
 *                                                                             *
610
 *    8.16   Still more weak pawn modifications.  In addition, there is no     *
611
 *           "rook on half-open file" any longer.  If there are only enemy     *
612
 *           pawns on the file, and the most advanced one is weak, then the    *
613
 *           file is treated as though it were open.  Technical error in the   *
614
 *           EvaluateDevelopment() code that caused screwey evaluations is     *
615
 *           fixed, making Crafty more likely to castle.  :)  Book() now       *
616
 *           verifies that the current position is in book, before trying any  *
617
 *           moves to see if the resulting positions are in book.  This avoids *
618
 *           something like e4 e5 Bb5 a6 Nf3 from causing Crafty to play Nc6   *
619
 *           which takes it back into book, rather than axb5 winning a piece.  *
620
 *           This will occasionally backfire and prevent Crafty from trans-    *
621
 *           posing back into book, but seems safer at present.                *
622
 *                                                                             *
623
 *    8.17   Piece values changed somewhat, to avoid Crafty's propensity to    *
624
 *           make unfavorable trades.  An example:  Crafty is faced with the   *
625
 *           loss of a pawn.  Instead, it trades the queen for a rook and      *
626
 *           bishop, which used to be the same as losing a pawn.  This is not  *
627
 *           so good, and often would result in a slow loss.  This version     *
628
 *           also implements several "user-supplied" patches to make Crafty    *
629
 *           compile cleanly on various machines.  In addition, you no longer  *
630
 *           have to continually modify types.h for different configurations.  *
631
 *           the Makefile now supplies a -D<target> to the compiler.  You need *
632
 *           to edit Makefile and set "target" to the appropriate value from   *
633
 *           the list provided.  Then, when getting a new version, save your   *
634
 *           Makefile, extract the new source, copy in your makefile and you   *
635
 *           will be ready, except for those rare occasions where I add a new  *
636
 *           source module.  Other changes are performance tweaks.  One is a   *
108 pmbaty 637
 *           simple trick to order captures while avoiding SEE() if possible.  *
33 pmbaty 638
 *           If the captured piece is more valuable than the capturing piece,  *
639
 *           we can simply use the difference (pessimistic value) rather than  *
108 pmbaty 640
 *           calling SEE() since this pessimistic value is still > 0.  Other   *
33 pmbaty 641
 *           tweaks to the various Next_*() routines to avoid a little un-     *
642
 *           necessary work.                                                   *
643
 *                                                                             *
644
 *    8.18   Book move selection algorithm changed.  Note that this is highly  *
645
 *           speculative, but when best book play is selected, Crafty will     *
646
 *           use the books.bin file as always.  If there is no move in books,  *
647
 *           Crafty reverts to book.bin, but now finds all known book moves    *
648
 *           and puts them into the root move list.  It then does a fairly     *
649
 *           short search, only considering these moves, and it will play the  *
650
 *           best one so long as the evaluation is acceptable.  If not, it     *
651
 *           simply returns as though there was no book move, and executes a   *
652
 *           search as it normally would.  The book hashing algorithm was also *
653
 *           modified so that the 64-bit hash key is slightly different from   *
654
 *           the real hash key.  In effect, the upper 16 bits are only set as  *
655
 *           a result of the side-not-to-move's pieces.  This means that for a *
656
 *           given position, all the book moves will be in the same "cluster"  *
657
 *           which makes the book dramatically faster, since one seek and read *
658
 *           produces all the possible book moves (plus some others too, since *
659
 *           the upper 16 bits are not unique enough.)  A significant speed    *
660
 *           improvement is noticable, particularly with a 60mb opening book.  *
661
 *           Crafty now understands (if that's the right word) that endings    *
662
 *           with bishops of opposite colors are to be avoided.  When such an  *
663
 *           ending is encountered, the total score is simply divided by two   *
664
 *           to make the ending look more drawish.                             *
665
 *                                                                             *
666
 *    8.19   Book selection algorithm further modified, so that the result of  *
667
 *           each game in the GM database is known.  Now Crafty will not play  *
668
 *           a move from book, if all games were lost by the side on move,     *
669
 *           hopefully avoiding many blunders.  Crafty now understands how to  *
670
 *           attack if both players castle on opposite sides, by encouraging   *
671
 *           pawn advances against the kings.  Other minor modifications to    *
672
 *           the eval values.                                                  *
673
 *                                                                             *
674
 *    8.20   PVS search finally implemented fully.  Problems several months    *
675
 *           ago prevented doing the PVS search along the PV itself.  This is  *
676
 *           therefore quite a bit faster, now that it's fully implemented and *
677
 *           working properly.  Elapsed time code modified so that Crafty now  *
678
 *           uses gettimeofday() to get fractions of a second elapsed time.    *
679
 *           Slight modification time allocation algorithm to avoid using too  *
680
 *           much time early in the game.                                      *
681
 *                                                                             *
682
 *    8.21   Courtesy of Mark Bromley, Crafty may run significantly faster on  *
683
 *           your machine.  There are some options in the Makefile that will   *
684
 *           eliminate many of the large attack computation long longs, which  *
685
 *           helps prevent cache thrashing.  On some machines this will be     *
686
 *           slower, on others 20% (or maybe more) faster.  You'll have to     *
687
 *           try -DCOMPACT_ATTACKS, and if that's faster, then it's time to    *
688
 *           try -DUSE_SPLIT_SHIFTS which may help even more.  Finally, if you *
689
 *           are running on a supersparc processor, you can use the fastattack *
690
 *           assembly module fastattack.s and get another big boost.  (Attack  *
691
 *           function is largest compute user in Crafty at present.) Serious   *
692
 *           search problem fixed.  It turns out that Crafty uses the hash     *
693
 *           table to pass PV moves from one iteration to the next, but for    *
694
 *           moves at the root of the tree the hash table has no effect, so a  *
695
 *           special case was added to RootMoveList to check the list of moves *
696
 *           and if one matches the PV move, move it to the front of the list. *
697
 *           This turned out to be critical because after completing a search, *
698
 *           (say to 9 plies) Crafty makes its move, then chops the first two  *
699
 *           moves off of the PV and then passes that to iterate to start a    *
700
 *           search.  This search starts at lastdepth-1 (8 in this case) since *
701
 *           the n-2 search was already done.  The "bug" showed up however, in *
702
 *           that RootMoveList() was not checking for the PV move correctly,   *
703
 *           which meant the root moves were ordered by static eval and static *
704
 *           exchange evaluation.  Normally not a serious problem, just that   *
705
 *           move ordering is not so good.  However, suppose that the opponent *
706
 *           takes a real long time to make a move (say 30 seconds) so that    *
707
 *           Crafty completes a 10 ply search.  It then starts the next search *
708
 *           at depth=9, but with the wrong move first.  If the target time is *
709
 *           not large enough to let it resolve this wrong move and get to the *
710
 *           other moves on the list, Crafty is "confused" into playing this   *
711
 *           move knowing absolutely nothing about it.  The result is that it  *
712
 *           can play a blunder easily.  The circumstances leading to this are *
713
 *           not common, but in a 60 move game, once is enough.  PV was pretty *
714
 *           well mis-handled in carrying moves from one search to another     *
715
 *           (not from one iteration to another, but from one complete search  *
716
 *           to another) and has been fixed.  A cute glitch concerning storing *
717
 *           PV from one iteration to another was also fixed, where the score  *
718
 *           stored was confusing the following search.                        *
719
 *                                                                             *
720
 *    8.22   EPD support (courtesy of Steven Edwards [thanks]) is now standard *
721
 *           in Crafty.  For porting, I'll provide a small test run which can  *
722
 *           be used to validate Crafty once it's been compiled.               *
723
 *                                                                             *
108 pmbaty 724
 *    8.23   Cleanup/speedup in hashing.  ProbeTransRef() and StoreTransRef()  *
725
 *           now carefully cast the boolean operations to the most efficient   *
726
 *           size to avoid 64bit operations when only the right 32 bits are    *
33 pmbaty 727
 *           significant. Repeat() code completely re-written to maintain two  *
728
 *           repetition lists, one for each side.  Quiesce() now handles the   *
729
 *           repetition check a little different, being careful to not call it *
730
 *           when it's unimportant, but calling it when repetitions are        *
731
 *           possible.                                                         *
732
 *                                                                             *
733
 *    8.24   Tweaks for king tropism to encourage pieces to collect near the   *
734
 *           king, or to encourage driving them away when being attacked.  A   *
735
 *           modification to Evaluate() to address the problem where Crafty    *
736
 *           is forced to play Kf1 or Kf8, blocking the rook in and getting    *
737
 *           into tactical difficulties as a result.  Book problem fixed where *
108 pmbaty 738
 *           Bookup was attempting to group moves with a common ancestor       *
33 pmbaty 739
 *           position together.  Unfortunately, captures were separated from   *
740
 *           this group, meaning capture moves in the book could never be      *
741
 *           played.  If a capture was the only move in book, it sort of       *
742
 *           worked because Crafty would drop out of book (not finding the     *
743
 *           capture) and then the search would "save" it.  However, if there  *
744
 *           was a non-capture alternative, it would be forced to play it,     *
745
 *           making it play gambits, and, often, bad ones.  Tablebase support  *
746
 *           is now in.  The tablebase files can be downloaded from the ftp    *
747
 *           machine at chess.onenet.net, pub/chess/TB.  Currently, Steven     *
748
 *           Edwards has all interesting 4 piece endings done.  To make this   *
749
 *           work, compile with -DTABLEBASES, and create a TB directory where  *
750
 *           Crafty is run, and locate the tablebase files in that directory.  *
751
 *           If you are running under UNIX, TB can be a symbolic or hard link  *
752
 *           to a directory anywhere you want.                                 *
753
 *                                                                             *
754
 *    8.25   Minor repair on draw by repetition.  Modified how books.bin was   *
755
 *           being used.  Now, a move in books.bin has its flags merged with   *
756
 *           the corresponding move from book.bin, but if the move does not    *
757
 *           exist in book.bin, it is still kept as a book move.  Repetitions  *
758
 *           are now counted as a draw if they occur two times, period.  The   *
759
 *           last approach was causing problems due to hashing, since the hash *
760
 *           approach used in Crafty is fairly efficient and frequently will   *
761
 *           carry scores across several searches.  This resulted in Crafty    *
762
 *           stumbling into a draw by repetition without knowing.  The con-    *
763
 *           figuration switch HAS-64BITS has been cleaned up and should be    *
764
 *           set for any 64bit architecture now, not just for Cray machines.   *
765
 *                                                                             *
766
 *    8.26   New search extension added, the well-known "one legal response to *
767
 *           check" idea.  If there's only one legal move when in check, we    *
768
 *           extend two plies rather than one, since this is a very forcing    *
769
 *           move.  Also, when one side has less than a rook, and the other    *
770
 *           has passed pawns, pushing these pawns to the 6th or 7th rank      *
771
 *           will extend the search one ply, where in normal positions, we     *
772
 *           only extend when a pawn reaches the 7th rank.                     *
773
 *                                                                             *
774
 *    9.0    Minor constraint added to most extensions:  we no longer extend   *
775
 *           if the side on move is either significantly ahead or behind,      *
776
 *           depending on the extension.  For example, getting out of check    *
777
 *           won't extend if the side on move is a rook behind, since it's     *
778
 *           already lost anyway.  We don't extend on passed pawn pushes if    *
779
 *           the side on move is ahead a rook, since he's already winning.     *
108 pmbaty 780
 *           minor adjustments for efficiency as well.  The next few versions  *
781
 *           in this series will have module names in these comments           *
33 pmbaty 782
 *           indicating which modules have been "cleaned" up.  This is an      *
783
 *           effort at optimizing, although it is currently directed at a line *
784
 *           by line analysis within modules, rather than major changes that   *
785
 *           effect more global ideas.  This type of optimization will come at *
786
 *           a later point in time.                                            *
787
 *                                                                             *
788
 *    9.1    NextMove(), NextCapture() and NextEvasion() were re-structured    *
789
 *           to eliminate unnecessary testing and speed them up significantly. *
790
 *           EvaluateTrades() was completely removed, since Crafty already has *
791
 *           positional scores that encourage/discourage trading based on the  *
792
 *           status of the game.  EvaluateTempo() was evaluated to be a        *
793
 *           failure and was removed completely.                               *
794
 *                                                                             *
795
 *    9.2    Quiesce()) and Search() were modified to be more efficient.  In   *
796
 *           addition, the null-move search was relaxed so that it always does *
797
 *           a search to depth-R, even if close to the end of the tree.        *
798
 *                                                                             *
799
 *    9.3    Check() is no longer used to make sure a position is legal after  *
800
 *           MakeMove() called, but before Search() is called recursively.  We *
801
 *           now use the same approach as Cray Blitz, we make a move and then  *
802
 *           capture the king at the next ply to prove the position is not a   *
803
 *           valid move.  If the side-on-move is already in check, we use      *
804
 *           NextEvasion() which only generates legal moves anyway, so this    *
805
 *           basically eliminates 1/2 of the calls to Check(), a big win.      *
806
 *           This resulted in modifications to Search(), Quiesce(), and        *
807
 *           QuiesceFull().  Connected passed pawns on 6th-7th no longer       *
808
 *           trigger search extensions.  Solved some problems like Win at      *
809
 *           Chess #2, but overall was a loser.                                *
810
 *                                                                             *
811
 *    9.4    Lookup now checks the transposition table entry and then informs  *
812
 *           Search() when it appears that a null move is useless.  This is    *
813
 *           found when the draft is too low to use the position, but it is at *
814
 *           least as deep as a null-move search would go, and the position    *
815
 *           did not cause a fail-high when it was stored.  King-safety was    *
816
 *           modified again.  The issue here is which king should attract the  *
817
 *           pieces, and for several months the answer has been the king that  *
818
 *           is most exposed attracts *all* pieces.  This has been changed to  *
819
 *           always attract one side's pieces to the other king.  Crafty's     *
820
 *           asymmetric king-safety was making it overly-defensive, even when  *
821
 *           the opponent's king was more exposed.  This should cure that and  *
822
 *           result in more aggressive play.                                   *
823
 *                                                                             *
824
 *    9.5    "Vestigial code" (left over from who-knows-when) removed from     *
825
 *           Evaluate().  This code used an undefined variable when there was  *
826
 *           no bishop or knight left on the board, and could add in a penalty *
827
 *           on random occasions.  Quiescence checks removed completely to see *
828
 *           how this works, since checks extend the normal search significant *
829
 *           amounts in any case.  QuiesceFull() removed and merged into       *
830
 *           Quiesce() which is faster and smaller.  First impression: faster, *
831
 *           simpler, better.  The benefit of no quiescence checks is that the *
832
 *           exhaustive search goes deeper to find positional gains, rather    *
833
 *           than following checks excessively.  No noticable loss in tactical *
834
 *           strength (so far).                                                *
835
 *                                                                             *
836
 *    9.6    legal move test re-inserted in Search() since null-moves could    *
837
 *           make it overlook the fact that a move was illegal.  New assembly  *
838
 *           code for X86 improves performance about 1/3, very similarly to    *
839
 *           the results obtained with the sparc-20 assembly code.  This was   *
840
 *           contributed by Eugene Nalimov and is a welcome addition.          *
841
 *                                                                             *
842
 *    9.7    Optimizations continuing.  Minor bug in pawn evaluation repaired. *
843
 *           Crafty looked at most advanced white pawn, but least-advanced     *
844
 *           black pawn on a file when doing its weak pawn evaluation.  The    *
845
 *           history heuristic was slightly broken, in that it was supposed to *
846
 *           be incrementing the history count by depth*depth, but this was    *
847
 *           somehow changed to 7*depth, which was not as good.  Assembly      *
848
 *           language interface fixed to cleanly make Crafty on all target     *
849
 *           architectures.                                                    *
850
 *                                                                             *
851
 *    9.8    The first change was to borrow a time-allocation idea from Cray   *
852
 *           Blitz.  Essentially, when Crafty notices it has used the normal   *
853
 *           time allotment, rather than exiting the search immediately, it    *
854
 *           will wait until it selects the next move at the root of the tree. *
855
 *           The intent of this is that if it has started searching a move     *
856
 *           that is going to be better than the best found so far, this will  *
857
 *           take more time, while a worse move will fail low quickly.  Crafty *
858
 *           simply spends more time to give this move a chance to fail high   *
859
 *           or else quickly fail low.  A fairly serious bug, that's been      *
860
 *           around for a long time, has finally been found/fixed.  In simple  *
861
 *           terms, if the "noise" level was set too high, it could make       *
862
 *           Crafty actually play a bad move.  The more likely effect was to   *
863
 *           see "bad move hashed" messages and the first few lines of output  *
864
 *           from the search often looked funny.  The bug was "noise" was used *
865
 *           as a limit, until that many nodes had been searched, no output    *
866
 *           would be produced.  Unfortunately, on a fail-high condition, the  *
867
 *           same code that produced the "++  Nh5" message also placed the     *
868
 *           fail-high move in the PV.  Failing to do this meant that the      *
869
 *           search could fail high, but not play the move it found.  Most     *
870
 *           often this happened in very fast games, or near the end of long   *
871
 *           zero-increment games.  It now always saves this move as it should *
872
 *           regardless of whether it prints the message or not.               *
873
 *                                                                             *
874
 *    9.9    Interface to Xboard changed from -ics to -xboard, so that -ics    *
875
 *           can be used with the custom interface.  Additional code to        *
876
 *           communicate with custom interface added.  Search() extensions are *
877
 *           restricted so that there can not be more than one extension at a  *
878
 *           node, rather than the two or (on occasion) more of the last       *
879
 *           version.                                                          *
880
 *                                                                             *
881
 *    9.10   Converted to Ken Thompson's "Belle" hash table algorithm.  Simply *
882
 *           put, each side has two tables, one that has entries replaced on a *
883
 *           depth-priority only, the other is an "always replace" table, but  *
884
 *           is twice as big.  This allows "deep" entries to be kept along     *
885
 *           with entries close to search point.  Basically, when the depth-   *
886
 *           priority table gets a position stored in it, the displaced        *
887
 *           position is moved to the always-replace table.  If the position   *
888
 *           can not replace the depth-priority entry, it always overwrites    *
889
 *           the other always-replace entry.  However, a position is never put *
890
 *           in both tables at the same time.                                  *
891
 *                                                                             *
892
 *    9.11   Bug in Search() that could result in the "draft" being recorded   *
893
 *           incorrectly in the hash table.  Caused by passing depth, which    *
894
 *           could be one or two plies more than it should be due to the last  *
895
 *           move extending the search.  Repeat() completely removed from      *
896
 *           Quiesce() since only capture moves are included, and it's         *
897
 *           impossible to repeat a position once a capture has been made.     *
898
 *                                                                             *
899
 *    9.12   optimizations: history.c, other minor modifications.              *
900
 *                                                                             *
901
 *    9.13   EvaluatePawns() modified significantly to simplify and be more    *
902
 *           accurate as well.  Pawns on open files are now not directly       *
903
 *           penalized if they are weak, rather the penalty is added when the  *
904
 *           rooks are evaluated.  Pawn hashing modified to add more info to   *
905
 *           the data that is hashed.  King safety scores toned down almost    *
906
 *           50% although it is still asymmetric.                              *
907
 *                                                                             *
908
 *    9.14   EvaluatePawns() modified further so that it now does the same     *
909
 *           computations for king safety as the old EvaluateKingSafety*()     *
910
 *           modules did, only it produces four values, two for each king,     *
911
 *           for each side of the board (king or queen-side).  This is now     *
912
 *           hashed with the rest of the pawn scoring, resulting in less       *
913
 *           hashing and computation, and more hits for king-safety too.  King *
914
 *           safety modified further.  Asymmetric scoring was accidentally     *
915
 *           disabled several versions ago, this is now enabled again.         *
916
 *                                                                             *
917
 *    9.15   Evaluate() now attempts to recognize the case where a knight is   *
918
 *           trapped on one of the four corner squares, much like a bishop     *
919
 *           trapped at a2/a7/h2/h7.  If a knight is on a corner square, and   *
920
 *           neither of the two potential flight squares are safe (checked by  *
108 pmbaty 921
 *           SEE()) then a large penalty is given.  This was done to avoid     *
33 pmbaty 922
 *           positions where Crafty would give up a piece to fork the king and *
923
 *           rook at (say) c7, and pick up the rook at a8 and think it was an  *
924
 *           exchange ahead, when often the knight was trapped and it was      *
925
 *           two pieces for a rook and pawn (or worse.)  Two timing bugs fixed *
926
 *           in this version:  (1) nodes_per_second was getting clobbered at   *
927
 *           the end of iterate, which would then corrupt the value stored in  *
928
 *           nodes_between_time_checks and occasionally make Crafty not check  *
929
 *           the time very often and use more time than intended; (2) the      *
930
 *           "don't stop searching after time is out, until the current move   *
931
 *           at the root of the tree has been searched" also would let Crafty  *
932
 *           use time unnecessarily.                                           *
933
 *                                                                             *
934
 *    9.16   Significant changes to the way MakeMove() operates.  Rather than  *
935
 *           copying the large position[ply] structure around, the piece       *
936
 *           location bitmaps and the array of which piece is on which square  *
937
 *           are now simple global variables.  This means that there is now an *
938
 *           UnmakeMove() function that must be used to restore these after a  *
939
 *           a move has been made.  The benefit is that we avoid copying 10    *
940
 *           bitmaps for piece locations when typically only one is changed,   *
941
 *           Ditto for the which piece is on which square array which is 64    *
942
 *           bytes long.  The result is much less memory traffic, with the     *
943
 *           probability that the bitmaps now stay in cache all the time. The  *
944
 *           EvaluatePawns() code now has an exponential-type penalty for      *
945
 *           isolated pawns, but it penalizes the difference between white and *
946
 *           black isolated pawns in this manner.  King evaluation now         *
947
 *           evaluates cases where the king moves toward one of the rooks and  *
948
 *           traps it in either corner.                                        *
949
 *                                                                             *
950
 *    9.17   Xboard compatibility version!  Now supports, so far as I know,    *
951
 *           the complete xboard interface, including hint, show thinking,     *
952
 *           taking back moves, "move now" and so forth.  Additionally, Crafty *
953
 *           now works with standard xboard.  Notice that this means that a    *
954
 *           ^C (interrupt) will no long terminate Crafty, because xboard uses *
955
 *           this to implement the "move now" facility.  This should also      *
956
 *           work with winboard and allow pondering, since there is now a      *
957
 *           special windows version of CheckInput() that knows how to check   *
958
 *           Win95 or WinNT pipes when talking with winboard.                  *
959
 *                                                                             *
960
 *    9.18   Xboard compatibility update.  "force" (gnu) mode was broken, so   *
961
 *           that reading in a PGN file (via xboard) would break.  Now, Crafty *
962
 *           can track the game perfectly as xboard reads the moves in.  King  *
963
 *           safety modified to discourage g3/g6 type moves without the bishop *
964
 *           to defend the weak squares.  Significant other changes to the     *
965
 *           Evaluate() procedure and its derivatives.  Very nasty trans/ref   *
966
 *           bug fixed.  Been there since transposition tables added.  When    *
967
 *           storing a mate score, it has to be adjusted since it's backed up  *
968
 *           as "mate in n plies from root" but when storing, it must be saved *
969
 *           as "mate in n plies from current position".  Trivial, really, but *
970
 *           I overlooked one important fact:  mate scores can also be upper   *
971
 *           or lower search bounds, and I was correcting them in the same way *
972
 *           which is an error.  The effect was that Crafty would often find a *
973
 *           mate in 2, fail high on a move that should not fail high, and end *
974
 *           up making a move that would mate in 3.  In particularly bad cases *
975
 *           this would make the search oscillate back and forth and draw a    *
976
 *           won position.  The fix was to only adjust the score if it's a     *
977
 *           score, not if it's a bound.  The "age" field of the trans/ref     *
978
 *           table was expanded to 3 bits.  Iterate now increments a counter   *
979
 *           modulo 8, and this counter is stored in the 3 ID bits of the      *
980
 *           trans/ref table.  If they are == the transposition_id counter,    *
981
 *           we know that this is a position stored during the current search. *
982
 *           If not, it's an "old" position.  This avoids the necessity of     *
983
 *           stepping through each entry in the trans/ref table (at the begin- *
984
 *           ning of a search) to set the age bit.  Much faster in blitz games *
985
 *           as stepping through the trans/ref table, entry by entry, would    *
986
 *           blow out cache.  This idea was borrowed from Cray Blitz, which    *
987
 *           used this same technique for many years.                          *
988
 *                                                                             *
989
 *    9.19   New evaluation code for passed pawns.  Crafty now gives a large   *
990
 *           bonus for two or more connected passed pawns, once they reach the *
991
 *           sixth rank, when the opponent has less than a queen remaining.    *
992
 *           this will probably be tuned as experience produces the special    *
993
 *           cases that "break" it.  Such things as the king too far away or   *
994
 *           the amount of enemy material left might be used to further im-    *
995
 *           prove the evaluation.                                             *
996
 *                                                                             *
997
 *    9.20   Bug in SetBoard() fixed.  Validity checks for castling status and *
998
 *           en passant status was broken for castle status.  It was checking  *
999
 *           the wrong rook to match castling status, and would therefore not  *
1000
 *           accept some valid positions, including #8 in Win At Chess suite.  *
1001
 *           Minor repetition bug fixed, where Crafty would recognize that a   *
1002
 *           three-fold repetition had occurred, but would not claim it when   *
1003
 *           playing on a chess server.  Crafty now does not immediately re-   *
1004
 *           search the first move when it fails low.  Rather, it behaves like *
1005
 *           Cray Blitz, and searches all moves.  If all fail low, Crafty will *
1006
 *           then relax (lower) alpha and search the complete list again.      *
1007
 *           This is a simple gamble that there is another move that will      *
1008
 *           "save the day" so that we avoid the overhead of finding out just  *
1009
 *           how bad the first move is, only to have more overhead when a good *
1010
 *           move fails high.  Minor repetition bug fixed.  When running test  *
1011
 *           suites of problems, SetBoard() neglected to put the starting      *
1012
 *           position in the repetition list, which would occasionally waste   *
1013
 *           time.  One notable case was Win At Chess #8, where Crafty would   *
1014
 *           find Nf7+ Kg8 Nh6+ Kh8 (repeating the original position) followed *
1015
 *           by Rf7 (the correct move, but 4 tempi behind.)  Display() bug     *
1016
 *           fixed.  Crafty now displays the current board position, rather    *
1017
 *           than the position that has been modified by a search in progress. *
1018
 *           Castling evaluation modified including a bug in the black side    *
1019
 *           that would tend to make Crafty not castle queen-side as black if  *
1020
 *           the king-side was shredded.  Minor bug in Book(). If the book was *
1021
 *           turned off (which is done automatically when using the test com-  *
1022
 *           mand since several of the Win At Chess positions are from games   *
1023
 *           in Crafty's large opening book) Book() would still try to read    *
1024
 *           the file and then use the data from the uninitialized buffer.     *
1025
 *           This would, on occasion, cause Crafty to crash in the middle of   *
1026
 *           a test suite run.                                                 *
1027
 *                                                                             *
1028
 *    9.21   Castling evaluation has been significantly modified to handle the *
1029
 *           following three cases better:                                     *
1030
 *           (a) One side can castle at the root of the tree, but can not      *
1031
 *           castle at a tip position.  If the move that gave up the right to  *
1032
 *           castle was *not* a castle move, penalize the score.               *
1033
 *           (b) One side can castle short or long at the root, but has al-    *
1034
 *           ready castled by the time the search reaches a tip position.      *
1035
 *           Here the goal is to make sure the player castled to the "better"  *
1036
 *           side by comparing king safety where the king really is, to king   *
1037
 *           safety had the king castled to the other side.  If the latter is  *
1038
 *           "safer" then penalize the current position by the "difference"    *
1039
 *           in king safety to encourage that side to delay castling.  This is *
1040
 *           a problem when Crafty can castle kingside *now*, but the kingside *
1041
 *           has been disrupted somehow.  If the search is not deep enough to  *
1042
 *           see clearing out the queenside and castling long, then it will    *
1043
 *           often castle short rather than not castle at all.  This fix will  *
1044
 *           make it delay, develop the queenside, and castle to a safer king  *
1045
 *           shelter, unless tactics force it to castle short.                 *
1046
 *           (c) One side can castle short or long at the root, but can only   *
1047
 *           castle one way at a tip position, because a rook has been moved.  *
1048
 *           If the remaining castle option is worse than castling to the side *
1049
 *           where the rook was moved, then we penalize the rook move to keep  *
1050
 *           castling to that side open as an option.                          *
1051
 *                                                                             *
1052
 *    9.22   More castling evaluation changes, to tune how/when Crafty chooses *
1053
 *           to castle.  Bugs in Option() where certain commands could be ex-  *
1054
 *           ecuted while a search was in progress.  If these commands did     *
1055
 *           anything to the board position, It would break Crafty badly.      *
1056
 *           All that was needed was to check for an active search before some *
1057
 *           commands were attempted, and if a search was in progress, return  *
1058
 *           to abort the search then re-try the command.  When playing games  *
1059
 *           with computers (primarily a chess-server consideration) Crafty is *
1060
 *           now defaulting to "book random 0" to use a short search to help   *
1061
 *           avoid some ugly book lines on occasion.  In this version, *all*   *
1062
 *           positional scores were divided by 2 to reduce the liklihood that  *
1063
 *           Crafty would sacrifice material and make up the loss with some    *
1064
 *           sort of positional gain that was often only temporary.  Note that *
1065
 *           king safety scores were not reduced while doing this.  New book   *
1066
 *           random options:  0=search, 1=most popular move, 2=move that       *
1067
 *           produces best positional value, 3=choose from most frequently     *
1068
 *           played moves, 4=emulate GM frequency of move selection, 5=use     *
1069
 *           sqrt() on frequencies to give more randomness, 6=random.  For     *
1070
 *           now, computers get book_random=1, while GM's now get 4 (same as   *
1071
 *           before), everyone else gets 5.  Until now, there has been no      *
1072
 *           penalty for doubled/tripled pawns.  It now exists.  Also, if the  *
1073
 *           king stands on E1/E8, then the king-side king safety term is used *
1074
 *           to discourage disrupting the king-side pawns before castling.     *
1075
 *           Crafty now has a start-up initialization file (as most Unix       *
1076
 *           programs do).  This file is named ".craftyrc" if the macro UNIX   *
1077
 *           is defined, otherwise a "dossy" filename "crafty.rc" is used.     *
1078
 *           Crafty opens this file and executes every command in it as though *
1079
 *           they were typed by the operator.  The last command *must* be      *
1080
 *           "exit" to revert input back to STDIN.                             *
1081
 *                                                                             *
1082
 *    9.23   More evaluation tuning, including the complete removal of the     *
1083
 *           mobility term for rooks/queens.  Since rooks already have lots    *
1084
 *           of scoring terms like open files, connected, etc, mobility is     *
1085
 *           redundant at times and often misleading.  The queen's mobility    *
1086
 *           is so variable it is nearly like introducing a few random points  *
1087
 *           at various places in the search.  Minor Xboard compatibility bug  *
1088
 *           fixed where you could not load a game file, then have Crafty play *
1089
 *           by clicking the "machine white" or "machine black" options.       *
1090
 *                                                                             *
1091
 *    9.24   Minor bug in Quiesce() repaired.  When several mate-in-1 moves    *
1092
 *           were available at the root, Crafty always took the last one,      *
1093
 *           which was often an under-promotion to a rook rather than to a     *
1094
 *           queen.  It looked silly and has been fixed so that it will play   *
1095
 *           the first mate-in-1, not the last.  RootMoveList() went to great  *
1096
 *           pains to make sure that promotion to queen occurs before rook.    *
1097
 *           Bug in EvaluateOutsidePassedPawns() would fail to recognize that  *
1098
 *           one side had an outside passer, unless both sides had at least    *
1099
 *           one passer, which failed on most cases seen in real games.        *
1100
 *           Minor "tweak" to move ordering.  GenerateMoves() now uses the     *
1101
 *           LSB() function to enumerate white moves, while still using MSB()  *
1102
 *           for black moves.  This has the effect of moving pieces toward     *
1103
 *           your opponent before moving them away.  A severe oversight        *
1104
 *           regarding the use of "!" (as in wtm=!wtm) was found and corrected *
1105
 *           to speed things up some.  !wtm was replaced by wtm^1 (actually,   *
1106
 *           a macro Flip(wtm) is used to make it more readable) which         *
1107
 *           resulted in significant speedup on the sparc, but a more modest   *
1108
 *           improvement on the pentium.                                       *
1109
 *                                                                             *
1110
 *    9.25   Minor bug in Book() fixed where it was possible for Crafty to     *
1111
 *           play a book move that was hardly ever played.  Minor change to    *
1112
 *           time utilization to use a little more time "up front".  Tuned     *
1113
 *           piece/square tables to improve development some.  Saw Crafty lose *
1114
 *           an occasional game because (for example) the bishop at c1 or c8   *
1115
 *           had not moved, but the king had already castled, which turned the *
1116
 *           EvaluateDevelopment() module "off".  first[ply] removed, since it *
1117
 *           was redundant with last[ply-1].  The original intent was to save  *
1118
 *           a subscript math operation, but the compilers are quite good with *
1119
 *           the "strength-reduction" optimization, making referencing a value *
1120
 *           by first[ply] or last[ply-1] exactly the same number of cycles.   *
1121
 *           Passed pawn scores increased.  The reduction done to reduce all   *
1122
 *           scores seemed to make Crafty less attentive to passed pawns and   *
1123
 *           the threats they incur.  Minor bug in Book() fixed, the variable  *
1124
 *           min_percent_played was inoperative due to incorrect test on the   *
1125
 *           book_status[n] value.  Pawn rams now scored a little differently, *
1126
 *           so that pawn rams on Crafty's side of the board are much worse    *
1127
 *           than rams elsewhere.  This avoids positional binds where, for ex- *
1128
 *           ample Crafty would like black with pawns at e6 d5 c6, white with  *
1129
 *           pawns at e5 and c5.  Black has a protected passed pawn at d5 for  *
1130
 *           sure, but the cramp makes it easy for white to attack black, and  *
1131
 *           makes it difficult for black to defend because the black pawns    *
1132
 *           block the position badly.  King safety tweaked up about 30%, due  *
1133
 *           to using nearly symmetrical scoring, the values had gotten too    *
1134
 *           small and were not overriding minor positional things.            *
1135
 *                                                                             *
1136
 *    9.26   Minor bug fixes in time allocation.  This mainly affects Crafty   *
1137
 *           as it plays on a chess server.  The problem is, simply, that the  *
1138
 *           current internal timing resolution is 1/10 of a second, kept in   *
1139
 *           an integer variable.  .5 seconds is stored as 5, for example.     *
1140
 *           This became a problem when the target time for a search dropped   *
1141
 *           below .3 seconds, because the "easy move" code would terminate    *
1142
 *           the search after 1/3 of the target time elapsed if there were no  *
1143
 *           fail lows or PV changes.  Unfortunately, 2 divided by 3 (.2 secs  *
1144
 *           /3) is "zero" which would let the search quickly exit, possibly   *
1145
 *           without producing a PV with a move for Crafty to play.  Crafty    *
1146
 *           would play the first PV move anyway, which was likely the PV move *
1147
 *           from the last search.  This would corrupt the board information,  *
1148
 *           often produce the error "illegal move" when the opponent tried to *
1149
 *           move, or, on occasion, blow Crafty up.  On a chess server, Crafty *
1150
 *           would simply flag and lose.  After this fix, I played several     *
1151
 *           hundred games with a time control of 100 moves in 1 second and    *
1152
 *           there were no failures.  Before the fix, this time control could  *
1153
 *           not result in a single completed game.  If you don't run Crafty   *
1154
 *           on a chess server, this version is not important, probably. a     *
1155
 *           minor fix for move input, so that moves like e2e4 will always be  *
1156
 *           accepted, where before they would sometimes be flagged as errors. *
1157
 *                                                                             *
1158
 *    9.27   This version has an interesting modification to NextCapture().    *
1159
 *           Quiesce() now passes alpha to NextCapture() and if a capture      *
1160
 *           appears to win material, but doesn't bring the score up to the    *
1161
 *           value of alpha, the capture is ignored.  In effect, if we are so  *
1162
 *           far behind that a capture still leaves us below alpha, then there *
1163
 *           is nothing to gain by searching the capture because our opponent  *
1164
 *           can simply stand pat at the next ply leaving the score very bad.  *
1165
 *           One exception is if the previous ply was in check, we simply look *
1166
 *           for any safe-looking capture just in case it leads to mate.  Bad  *
1167
 *           book bug with book random=1 or 2 fixed (this is set when playing  *
1168
 *           a computer).  This bug would cause Crafty to play an illegal move *
1169
 *           and bust wide open with illegal move errors, bad move hashed, and *
1170
 *           other things.  Book randomness also quite a bit better now.       *
1171
 *                                                                             *
1172
 *    9.28   Piece/square table added for rooks.  Primary purpose is to dis-   *
1173
 *           courage rooks moving to the edge of the board but in front of     *
1174
 *           pawns, where it is easy to trap it.  Crafty now understands the   *
1175
 *           concept of blockading a passed pawn to prevent it from advancing. *
1176
 *           it always understood that advancing one was good, which would     *
1177
 *           tend to encourage blockading, but it would also often ignore the  *
1178
 *           pawn and let it advance a little here, a little there, until it   *
1179
 *           suddenly realized that it was a real problem.  The command to set *
1180
 *           the size of the hash table has been modified.  This command is    *
1181
 *           now "hash n", "hash nK" or "hash nM" to set the hash table to one *
1182
 *           of bytes, Kbytes, or Mbytes.  Note that if the size is not an     *
1183
 *           exact multiple of what Crafty needs, it silently reduces the size *
1184
 *           to an optimum value as close to the suggested size as possible.   *
1185
 *           Trade bonus/penalty is now back in, after having been removed     *
1186
 *           when the new UnmakeMove() code was added.  Crafty tries to trade  *
1187
 *           pawns but not pieces when behind, and tries to trade pieces but   *
1188
 *           not pawns when ahead.  EvaluateDraws() now understands that rook  *
1189
 *           pawns + the wrong bishop is a draw.  Minor adjustment to the      *
1190
 *           quiescence-search pruning introduced in 9.27, to avoid excluding  *
1191
 *           captures that didn't lose material, but looked bad because the    *
1192
 *           positional score was low.  Slightly increased the values of the   *
1193
 *           pieces relative to that of a pawn to discourage the tendency to   *
1194
 *           trade a piece for two or three pawns plus some positional plusses *
1195
 *           that often would slowly vanish.  Null-move search modified to try *
1196
 *           null-moves, even after captures.  This seems to hurt some prob-   *
1197
 *           lem positions, but speeds up others significantly.  Seems better  *
1198
 *           but needs close watching.  EvaluateDraws() call moved to the top  *
1199
 *           of Evaluate() to check for absolute draws first.  Quiesce() now   *
1200
 *           always calls Evaluate() which checks for draws before trying the  *
1201
 *           early exit tests to make sure that draws are picked up first.     *
1202
 *           EvaluateDraws() substantially re-written to get rid of lots of    *
1203
 *           unnecessary instructions, and to also detect RP+B of wrong color  *
1204
 *           even if the losing side has a pawn of its own.  Crafty would      *
1205
 *           often refuse to capture that last pawn to avoid reaching an end-  *
1206
 *           game it knew was drawn.                                           *
1207
 *                                                                             *
1208
 *    9.29   New time.c as contributed by Mike Byrne.  Basically makes Crafty  *
1209
 *           use more time up front, which is a method of anticipating that    *
1210
 *           Crafty will predict/ponder pretty well and save time later on     *
1211
 *           anyway.  InCheck() is never called in Quiesce() now.  As a        *
1212
 *           result, Crafty won't recognize mates in the q-search.  This has   *
1213
 *           speeded the code up with no visible penalty, other than it will   *
1214
 *           occasionally find a win of material rather than a mate score, but *
1215
 *           this has not affected the problem suite results in a measurable   *
1216
 *           way, other than Crafty is now about 10% faster in average type    *
1217
 *           position, and much faster in others.  The latest epd code from    *
1218
 *           Steven Edwards is a part of this version, which includes updated  *
1219
 *           tablebase access code.                                            *
1220
 *                                                                             *
1221
 *   10.0    New time.c with a "monitoring feature" that has two distinct      *
1222
 *           functions:  (1) monitor Crafty's time remaining and if it is too  *
1223
 *           far behind the opponent, force it to speed up, or if it is well   *
1224
 *           ahead on time, slow down some;  (2) if opponent starts moving     *
1225
 *           quickly, Crafty will speed up as well to avoid getting too far    *
1226
 *           behind.  EvaluateDraws() modified to detect that if one side has  *
1227
 *           insufficient material to win, the score can never favor that side *
1228
 *           which will make Crafty avoid trading into an ending where it has  *
1229
 *           KNN vs KB for example, where KNN seems to be ahead.               *
1230
 *                                                                             *
1231
 *   10.1    New book.c.  Crafty now counts wins, losses and draws for each    *
1232
 *           possible book move and can use these to play a move that had a    *
1233
 *           high percentage of wins, vs the old approach of playing a move    *
1234
 *           just because it was played a lot in the past.                     *
1235
 *                                                                             *
1236
 *   10.2    Evaluation tuning.  Rook on 7th, connected rooks on 7th, etc.,    *
1237
 *           are now more valuable.  Crafty was mistakenly evaluating this     *
1238
 *           too weakly.  Book() changed so that the list of moves is first    *
1239
 *           sorted based on winning percentages, but then, after selecting    *
1240
 *           the sub-set of this group, the list is re-sorted based on the     *
1241
 *           raw total games won so that the most popular opening is still the *
1242
 *           most likely to be played.                                         *
1243
 *                                                                             *
1244
 *   10.3    Timer resolution changed to 1/100th of a second units, with all   *
1245
 *           timing variables conforming to this, so that it is unnecessary    *
1246
 *           to multiply by some constant to convert one type of timing info   *
1247
 *           to the standard resolution.  Bug in evaluate.c fixed.  Trapped    *
1248
 *           rook (rook at h1, king at g1 for example) code had a bad sub-     *
1249
 *           script that caused erroneous detection of a trapped rook when     *
1250
 *           there was none.                                                   *
1251
 *                                                                             *
1252
 *   10.4    Quiesce() no longer checks for time exceeded, nor does it do any  *
1253
 *           hashing whatsoever, which makes it significantly faster, while    *
1254
 *           making the trees only marginally bigger.                          *
1255
 *                                                                             *
1256
 *   10.5    Quiesce() now calls GenerateCaptures() to generate capture moves. *
1257
 *           This new move generator module is significantly faster, which     *
1258
 *           speeds the quiescence search up somewhat.                         *
1259
 *                                                                             *
1260
 *   10.6    CastleStatus is now handled slightly differently.  The right two  *
1261
 *           bits still indicate whether or not that side can castle to either *
1262
 *           side, and 0 -> no castling at all, but now, <0 means that no      *
1263
 *           castling can be done, and that castling rights were lost by       *
1264
 *           making a move that was not castling.  This helps in Evaluate()    *
1265
 *           by eliminating a loop to see if one side castled normally or had  *
1266
 *           to give up the right to castle for another (bad) reason.  This is *
1267
 *           simply an efficiency issue, and doesn't affect play or anything   *
1268
 *           at all other than to make the search faster.                      *
1269
 *                                                                             *
1270
 *   10.7    NextMove() now doesn't try history moves forever, rather it will  *
1271
 *           try the five most popular history moves and if it hasn't gotten a *
1272
 *           cutoff by then, simply tries the rest of the moves without the    *
1273
 *           history testing, which is much faster.                            *
1274
 *                                                                             *
1275
 *   10.8    Book() now "puzzles" differently.  In puzzling, it is trying to   *
1276
 *           find a move for the opponent.  Since a book move by the opponent  *
1277
 *           will produce an instant move, puzzle now enumerates the set of    *
1278
 *           legal opponent moves, and from this set, removes the set of known *
1279
 *           book moves and then does a quick search to choose the best.  We   *
1280
 *           then ponder this move just in case the opponent drops out of book *
1281
 *           and uses a normal search to find a move.  Crafty now penalizes    *
1282
 *           a pawn that advances, leaving its neighbors behind where both     *
1283
 *           the advancing pawn, and the ones being left behind become weaker. *
1284
 *           the opening phase of the game has been modified to "stay active"  *
1285
 *           until all minor pieces are developed and Crafty has castled, to   *
1286
 *           keep EvaluateDevelopment() active monitoring development.  Minor  *
1287
 *           change to DrawScore() so that when playing a computer, it will    *
1288
 *           return "default_draw_score" under all circumstances rather than   *
1289
 *           adjusting this downward, which makes Crafty accept positional     *
1290
 *           weaknesses rather than repeat a position.                         *
1291
 *                                                                             *
1292
 *   10.9    Evaluate() now handles king safety slightly differently, and      *
1293
 *           increases the penalty for an exposed king quite a bit when there  *
1294
 *           is lots of material present, but scales this increase down as the *
1295
 *           material comes off.  This seems to help when attacking or getting *
1296
 *           attacked.  Since Quiesce() no longer probes the hash table, the   *
1297
 *           idea of storing static evaluations there became worthless and was *
1298
 *           burning cycles that were wasted.  This has been removed. Check    *
1299
 *           extensions relaxed slightly so that if ply is < iteration+10 it   *
1300
 *           will allow the extension.  Old limit was 2*iteration.  Connected  *
1301
 *           passed pawns on 6th now evaluated differently.  They get a 1 pawn *
1302
 *           bonus, then if material is less than a queen, another 1 pawn      *
1303
 *           bonus, and if not greater than a rook is left, and the king can't *
1304
 *           reach the queening square of either of the two pawns, 3 more      *
1305
 *           more pawns are added in for the equivalent of + a rook.           *
1306
 *                                                                             *
1307
 *   10.10   Evaluate() modified.  King safety was slightly broken, in that    *
1308
 *           an uncastled king did not get a penalty for open center files.    *
1309
 *           new evaluation term for controlling the "absolute 7th rank", a    *
1310
 *           concept from "My System".                                         *
1311
 *                                                                             *
1312
 *   10.11   Lazy evaluation implemented.  Now as the Evaluate() code is ex-   *
1313
 *           ecuted, Crafty will "bail out" when it becomes obvious that the   *
1314
 *           remainder of the evaluation can't bring the score back inside the *
1315
 *           alpha/beta window, saving time.                                   *
1316
 *                                                                             *
1317
 *   10.12   More Jakarta time control commands added so operator can set the  *
1318
 *           time accurately if something crashes, and so the operator can set *
1319
 *           a "cushion" to compensate for operator inattention.  "Easy" is    *
1320
 *           more carefully qualified to be a recapture, or else a move that   *
1321
 *           preserves the score, rather than one that appears to win material *
1322
 *           as was done in the past.  King safety tweaked a little so that if *
1323
 *           one side gives up the right to castle, and could castle to a safe *
1324
 *           position, the penalty for giving up the right is substantially    *
1325
 *           larger than before.  A minor bug also made it more likely for     *
1326
 *           black to give up the right to castle than for white.              *
1327
 *                                                                             *
1328
 *   10.13   Modifications to Book() so that when using "book random 0" mode   *
1329
 *           (planned for Jakarta) and the search value indicates that we are  *
1330
 *           losing a pawn, we return "out of book" to search *all* the moves, *
1331
 *           Not just the moves that are "in book."  This hopefully avoids     *
1332
 *           some book lines that lose (gambit) material unsoundly.            *
1333
 *                                                                             *
1334
 *   10.14   Final timing modifications.  Puzzling/booking searches now take   *
1335
 *           1/30th of the normal time, rather than 1/10th.  New command       *
1336
 *           "mode=tournament" makes Crafty assess DRAW as "0" and also makes  *
1337
 *           it prompt the operator for time corrections as required by WMCCC  *
1338
 *           rules.                                                            *
1339
 *                                                                             *
1340
 *   10.15   Evaluate() tuning to reduce development bonuses, which were able  *
1341
 *           to overwhelm other scores and lead to poor positions.             *
1342
 *                                                                             *
1343
 *   10.16   "Lazy" (early exit) caused some problems in Evaluate() since so   *
1344
 *           many scores are very large.  Scaled this back significantly and   *
1345
 *           also EvaluateOutsidePassedPawns() could double the score if one   *
1346
 *           side had passers on both sides.  This was fixed.                  *
1347
 *                                                                             *
1348
 *   10.17   Crafty's Book() facility has been significantly modified to be    *
1349
 *           more understandable.  book random <n> has the following options:  *
1350
 *           (0) do a search on set of book moves; (1) choose from moves that  *
1351
 *           are played most frequently; (2) choose from moves with best win   *
1352
 *           ratio; (3) choose from moves with best static evaluation; and     *
1353
 *           (4) choose completely randomly.  book width <n> can be used to    *
1354
 *           control how large the "set" of moves will be, with the moves in   *
1355
 *           the "set" being the best (according to the criteria chosen) from  *
1356
 *           the known book moves.  Minor tweak to Search() so that it extends *
1357
 *           passed pawn pushes to the 6th or 7th rank if the move is safe,    *
1358
 *           and this is an endgame.                                           *
1359
 *                                                                             *
1360
 *   10.18   New annotate command produces a much nicer analysis file and also *
1361
 *           lets you exert control over what has to happen to trigger a com-  *
1362
 *           ment among other things.  AKA Crafty/Jakarta version.             *
1363
 *                                                                             *
1364
 *   11.1    Fractional depth allows fractional ply increments.  The reso-     *
1365
 *           lution is 1/4 of a ply, so all of the older "depth++" lines now   *
1366
 *           read depth+=PLY, where #define PLY 4 is used everywhere.  This    *
1367
 *           gives added flexibility in that some things can increment the     *
1368
 *           depth by < 1 ply, so that it takes two such things before the     *
1369
 *           search actually extends one ply deeper.  Crafty now has full PGN  *
1370
 *           support.  A series of pgn commands (pgn Event 14th WMCCC) allow   *
1371
 *           the PGN tags to be defined, when Crafty reads or annotates a PGN  *
1372
 *           file it reads and parses the headers and will display the info    *
1373
 *           in the annotation (.can) file or in the file you specify when you *
1374
 *           execute a "savegame <filename>" command.                          *
1375
 *                                                                             *
1376
 *   11.2    Fractional ply increments now implemented.  11.1 added the basic  *
1377
 *           fractional ply extension capabilities, but this version now uses  *
1378
 *           them in Search().  There are several fractional ply extensions    *
1379
 *           being used, all are #defined in types.h to make playing with them *
1380
 *           somewhat easier.  One advantage is that now all extensions are    *
1381
 *           3/4 of a ply, which means the first time any extension is hit, it *
1382
 *           has no effect, but the next three extensions will result in an    *
1383
 *           additional ply, followed by another ply where the extension does  *
1384
 *           nothing, followed by ...  This allowed me to remove the limit on  *
1385
 *           how deep in the tree the extensions were allowed, and also seems  *
1386
 *           to not extend as much unless there's "something" going on.  When  *
1387
 *           the position gets interesting, these kick in.  Asymmetric king    *
1388
 *           safety is back in.  Crafty's king safety is a little more im-     *
1389
 *           portant than the opponent's now to help avoid getting its king-   *
1390
 *           side shredded to win a pawn.  A new term, ROOK_ON_7TH_WITH_PAWN   *
1391
 *           to help Crafty understand how dangerous a rook on the 7th is if   *
1392
 *           it is supported by a pawn.  It is difficult to drive off and the  *
1393
 *           pawn provides additional threats as well.                         *
1394
 *                                                                             *
1395
 *   11.3    King safety modifications.  One fairly serious bug is that Crafty *
1396
 *           considered that king safety became unimportant when the total     *
1397
 *           pieces dropped below 16 material points.  Note that this was      *
1398
 *           pieces only, not pieces and pawns, which left plenty of material  *
1399
 *           on the board to attack with.  As a result, Crafty would often     *
1400
 *           allow its king-side to be weakened because it was either in an    *
1401
 *           endgame, or close to an endgame.  *Incorrectly*, it turns out. :) *
1402
 *           Going back to a very old Crafty idea, king tropism is now tied to *
1403
 *           how exposed each king is, with pieces being attracted to the most *
1404
 *           exposed king for attack/defense.                                  *
1405
 *                                                                             *
1406
 *   11.4    Tuning on outside passed pawn code.  Values were not large enough *
1407
 *           and didn't emphasize how strong such a pawn is.  General passed   *
1408
 *           pawn value increased as well.  Minor bug in edit that would let   *
1409
 *           Crafty flag in some wild games was fixed.  Basically, edit has no *
1410
 *           way to know if castling is legal, and has to assume if a rook and *
1411
 *           king are on the original squares, castling is o.k.  For wild2     *
1412
 *           games in particular, this can fail because castling is illegal    *
1413
 *           (by the rules for wild 2) while it might be that rooks and king   *
1414
 *           are randomly configured to make it look possible.  Crafty would   *
1415
 *           then try o-o or o-o-o and flag when the move was rejected as      *
1416
 *           illegal.  richard Lloyd suggested a speed-up for the annotate     *
1417
 *           command which I'm using temporarily.  Crafty now searches all     *
1418
 *           legal moves in a position, and if the best move matches the game  *
1419
 *           move, the second search of just the move played is not needed.  I *
1420
 *           was searching just the game move first, which was a waste of time *
1421
 *           in many positions.  I'll resume this later, because I want to see *
1422
 *           these values:  (1) score for move actually played; (2) score for  *
1423
 *           the rest of the moves only, not including the move actually       *
1424
 *           played, to see how often a player finds a move that is the *only* *
1425
 *           good move in a position.                                          *
1426
 *                                                                             *
1427
 *   11.5    More fractional ply extension tuning.  A few positions could blow *
1428
 *           up the search and go well beyond 63 plies, the max that Crafty    *
1429
 *           can handle.  Now, extensions are limited to no more than one ply  *
1430
 *           of extensions for every ply of search.  Note that "in check" is   *
1431
 *           extended on the checking move ply, not on the out of check ply,   *
1432
 *           so that one-reply-to-check still extends further.  Minor changes  *
1433
 *           to time.c to further help on long fast games.  Another important  *
1434
 *           timing bug repaired.  If Crafty started an iteration, and failed  *
1435
 *           high on the first move, it would not terminate the search until   *
1436
 *           this move was resolved and a score returned.  This could waste a  *
1437
 *           lot of time in already won positions.  Interesting bug in the     *
1438
 *           capture search pruning fixed.  At times, a piece would be hung,   *
1439
 *           but the qsearch would refuse to try the capture because it would  *
1440
 *           not bring the score back to above alpha.  Unfortunately, taking   *
1441
 *           the piece could let passed pawns race in undisturbed which would  *
1442
 *           affect the score.  This pruning is turned off when there are not  *
1443
 *           at least two pieces left on the board.  Another bug in the move   *
1444
 *           generator produced pawn promotions twice, once when captures were *
1445
 *           generated, then again when the rest of the moves are produced.    *
108 pmbaty 1446
 *           this was also repaired.  Small book now will work.  The Bookup()  *
33 pmbaty 1447
 *           code now assumes white *and* black won in the absence of a valid  *
1448
 *           PGN Result tag, so that the small book will work correctly.       *
1449
 *                                                                             *
1450
 *   11.6    Modifications to time allocation in a further attempt to make     *
1451
 *           Crafty speed up if time gets short, to avoid flagging.  It is now *
1452
 *           *very* difficult to flag this thing.  :)  Minor evaluation tweaks *
1453
 *           and a fix to Quiesce() to speed it up again after a couple of bad *
1454
 *           fixes slowed things down.                                         *
1455
 *                                                                             *
1456
 *   11.7    Minor modification to Evaluate().  Rooks behind a passed pawn are *
1457
 *           good, but two rooks behind the pawn don't help if the pawn is     *
1458
 *           blockaded and can't move, unless the two rooks are not the same   *
1459
 *           color as the pawn.                                                *
1460
 *                                                                             *
1461
 *   11.8    First stage of "book learning" implemented.  Crafty monitors the  *
1462
 *           search evaluation for the first 10 moves out of book.  It then    *
1463
 *           computes a "learn value" if it thinks this set of values shows    *
1464
 *           that the book line played was very good or very bad.  This value  *
1465
 *           is added to a "learn count" for each move in the set of book      *
1466
 *           moves it played, with the last move getting the full learn value, *
1467
 *           and moves closer to the start of the game getting a smaller       *
1468
 *           percentage.  (See learn.c for more details).  These values are    *
1469
 *           used by Book() to help in selecting/avoiding book lines.  Crafty  *
1470
 *           produces a "book.lrn" file that synthesizes this information into *
1471
 *           a portable format that will be used by other Crafty programs,     *
1472
 *           once the necessary code is added later on.                        *
1473
 *                                                                             *
1474
 *   11.9    An age-old problem caused by null-move searching was eliminated   *
1475
 *           in this version.  Often the null-move can cause a move to fail    *
1476
 *           high when it should not, but then the move will fail low on the   *
1477
 *           re-search when beta is relaxed.  This would, on occasion, result  *
1478
 *           in Crafty playing a bad move for no reason.  Now, if a fail high  *
1479
 *           is followed by a fail-low, the fail high condition is ignored.    *
1480
 *           This can be wrong, but with null-move pruning, it can also be     *
1481
 *           even "more wrong" to accept a fail high move after it fails low   *
1482
 *           due to a null-move failure, than it would be right to accept the  *
1483
 *           fail high/fail low that is caused by trans/ref table anomalies.   *
1484
 *                                                                             *
1485
 *   11.10   Crafty is now using the Kent, et. al, "razoring" algorithm, which *
1486
 *           simply eliminates the last ply of full-width searching and goes   *
1487
 *           directly to the capture search if the move before the last full-  *
1488
 *           width ply is uninteresting, and the Material eval is 1.5 pawns    *
1489
 *           below alpha.  It's a "modest futility" idea that doesn't give up  *
1490
 *           on the uninteresting move, but only tries captures after the move *
1491
 *           is played in the tree.  Net result is 20-30% average faster times *
1492
 *           to reach the same depth.  Minor mod to book.lrn to include Black, *
1493
 *           White and Date PGN tags just for reference.  Next learning mod is *
1494
 *           another "book random n" option, n=3.  This will use the "learned" *
1495
 *           score to order book moves, *if* any of them are > 0.  What this   *
1496
 *           does is to encourage Crafty to follow opening lines that have     *
1497
 *           been good, even if the learn count hasn't reached the current     *
1498
 *           threshold of 1,000.  This makes learning "activate" faster.  This *
1499
 *           has one hole in it, in that once Crafty learns that one move has  *
1500
 *           produced a positive learn value, it will keep playing that move   *
1501
 *           (since no others will yet have a positive value) until it finally *
1502
 *           loses enough to take the learn value below zero.  This will be    *
1503
 *           taken care of in the next version, hopefully.                     *
1504
 *                                                                             *
1505
 *   11.11   New "book random 4" mode that simply takes *all* learning scores  *
1506
 *           for the set of legal book moves, translates them so that the      *
1507
 *           worst value is +1 (by adding -Min(all_scores)+1 to every learn    *
1508
 *           value) and then squaring the result.  This encourages Crafty to   *
1509
 *           follow book lines that it has learned to be good, while still     *
1510
 *           letting it try openings that it has not yet tried (learn value    *
1511
 *           =0) and even lets it try (albeit rarely) moves that it has        *
1512
 *           learned to be bad.  Minor evaluation tweaking for king placement  *
1513
 *           in endgames to encourage penetration in the center rather than on *
1514
 *           a wing where the king might get too far from the "action."        *
1515
 *                                                                             *
1516
 *   11.12   LearnFunction() now keeps positional score, rather than using a   *
1517
 *           constant value for scores < PAWN_VALUE, to improve learning       *
1518
 *           accuracy.  Book learn <filename> [clear] command implemented to   *
1519
 *           import learning files from other Crafty programs.                 *
1520
 *                                                                             *
1521
 *   11.13   Endgame threshold lowered to the other side having a queen or     *
1522
 *           less (9) rather than (13) points.  Queen+bishop can be very       *
1523
 *           dangerous to the king for example, or two rooks and a bishop.     *
1524
 *           Learning "curve" modified.  It was accidentally left in a sort    *
1525
 *           of "geometric" shape, with the last move getting the full learn   *
108 pmbaty 1526
 *           value, the next one back getting 1/2, the next 1/3, etc.  Now     *
33 pmbaty 1527
 *           it is uniformly distributed from front to back.  If there are 20  *
1528
 *           moves, the last gets the full value, the next gets 19/20, etc..   *
1529
 *           Also the "percentage played" was hosed and is fixed.              *
1530
 *                                                                             *
1531
 *   11.14   Minor modification to book learn 4 code so that when you start    *
1532
 *           off with *no* learned data, Book() won't exclude moves from the   *
1533
 *           possible set of opening moves just because one was not played     *
1534
 *           very often.  Due to the lack of "ordering" info in this case, it  *
1535
 *           would often discover the second or third move in the list had not *
1536
 *           been played very often and cull the rest of the list thinking it  *
1537
 *           was ordered by the number of times it was played.  This code      *
1538
 *           contains all of Frank's xboard patches so that analyze mode and   *
1539
 *           so forth works cleanly.  A new book random option <5> modifies    *
1540
 *           the probability of playing a move by letting the popularity of a  *
1541
 *           move affect the learned-value sorting algorithm somewhat. Minor   *
1542
 *           adjustment in pawn ram asymmetric term to simply count rams and   *
1543
 *           not be too concerned about which half of the board they are on.   *
1544
 *           adjustment to weak pawn code.  Also increased bonus for king      *
1545
 *           tropism to attract pieces toward enemy king to improve attacking. *
1546
 *           code that detects drawn K+RP+wrong bishop had a bug in that on    *
1547
 *           occasion, the defending king might not be able to reach the       *
1548
 *           queening square before the opposing king blocks it out.  This     *
1549
 *           would let some positions look like draws when they were not. King *
1550
 *           safety now uses square(7-distance(piece,king)) as one multiplier  *
1551
 *           to really encourage pieces to get closer to the king.  In cases   *
1552
 *           whre one king is exposed, all pieces are attracted to that king   *
1553
 *           to attack or defend.   This is turned off during the opening.     *
1554
 *           minor 50-move rule draw change to fix a bug, in that the game is  *
1555
 *           not necessarily a draw after 50 moves by both sides, because one  *
1556
 *           side might be mated by the 50th move by the other side.  This     *
1557
 *           occurred in a game Crafty vs Ferret, and now works correctly.     *
1558
 *                                                                             *
1559
 *   11.15   Modified LearnBook() so that when Crafty terminates, or resigns,  *
1560
 *           or sees a mate, it will force LearnBook() to execute the learning *
1561
 *           analysis even if 10 moves have not been made sine leaving the     *
1562
 *           book.  Crafty now trusts large positive evals less when learning  *
1563
 *           about an opening, since such evals are often the result of the    *
1564
 *           opponent's blunder, rather than a really  good opening line.      *
1565
 *           second learning stage implemented.  Crafty maintains a permanent  *
1566
 *           hash file that it uses to store positions where the search value  *
1567
 *           (at the root of the tree) suddenly drop, and then it "seeds" the  *
1568
 *           real transposition table with these values at the start of each   *
1569
 *           of each search so that information is available earlier the next  *
1570
 *           time this game is played.  Position.bin has the raw binary data   *
1571
 *           this uses, while position.lrn has the "importable" data.  New     *
1572
 *           import <filename> [clear] command imports all learning data now,  *
1573
 *           eliminating the old book learn command.  LearnFunction() modified *
1574
 *           to handle "trusted" values (negative scores, because we are sure  *
1575
 *           that Crafty won't make an outright blunder very often) and        *
1576
 *           "untrusted" values (positive values often caused by the opponent  *
1577
 *           hanging a piece.  Trusted values are scaled up if the opponent is *
1578
 *           weaker than Crafty since a weaker opponent forcing a negative     *
1579
 *           eval means the position must really be bad, and are scaled down   *
1580
 *           somewhat if the opponent is stronger than Crafty (from the rating *
1581
 *           information from ICC or the operator using the rating command.)   *
1582
 *           Untrusted values are scaled down harshly, unless the opponent is  *
1583
 *           much stronger than Crafty, since it is unlikely such an opponent  *
1584
 *           would really blunder, while weaker opponents drastically scale    *
1585
 *           untrusted value (which is positive, remember) down.  minor depth  *
1586
 *           problem fixed in learning code.  The depth used in LearnFunction  *
1587
 *           was not the depth for the search that produced the "learn value"  *
1588
 *           the LearnBook() chooses, it was the depth for the search last     *
1589
 *           done.  Now, in addition to the 10 last search values, I store the *
1590
 *           depth for each as well.  When a particular search value is used   *
1591
 *           to "learn", the corresponding (correct) depth is also now used.   *
1592
 *           These learn values are now limited to +6000/-6000 because in one  *
1593
 *           book line, Crafty discovered it was getting mated in 2 moves at   *
1594
 *           the end of the book line which was 30 moves deep.  If you try to  *
1595
 *           distribute a "mate score" as the learned value, it will instantly *
1596
 *           make every move for the last 26 moves look too bad to play.  This *
1597
 *           is not desirable, rather the last few moves should be unplayable, *
1598
 *           but other moves further back should be o.k.  Another bug that let *
1599
 *           Crafty overlook the above mate also fixed.  The search value was  *
1600
 *           not correct if Crafty got mated, which would avoid learning in    *
1601
 *           that case.  This has been fixed, although it was obviously a rare *
1602
 *           problem anyway.  Minor draw bug fixed, where Crafty would offer   *
1603
 *           a draw when time was way low, caused by the resolution change a   *
1604
 *           few months back, and also it would offer a draw after the         *
1605
 *           opponent moved, even if he made a blunder.  It now only offers a  *
1606
 *           draw after moving to make sure it's reasonable.  Crafty now won't *
1607
 *           resign if there is a deep mate, such as a mate in 10+, because we *
1608
 *           want to be sure that the opponent can demonstrate making progress *
1609
 *           before we toss in the towel.  LearnBook() now learns on all games *
1610
 *           played.  The old algorithm had a serious problem in that negative *
1611
 *           scores were trusted fully, positive scores were treated with low  *
1612
 *           confidence, and scores near zero were ignored totally.  The net   *
1613
 *           result was that learning values became more and more negative as  *
1614
 *           games were played.  Now equal positions are also factored in to   *
1615
 *           the learning equation to help pull some of these negative values  *
1616
 *           back up.  Book() now excludes moves that have not been played     *
1617
 *           many times (if there is at least one move that has) for book      *
1618
 *           random = 2, 3 and 4.  This stops some of the silly moves.         *
1619
 *           position learning is turned off after 15 moves have passed since  *
1620
 *           leaving book, to avoid learning things that won't be useful at a  *
1621
 *           later time.  Hashing changed slightly to include "permanent"      *
1622
 *           entries so that the learned stuff can be copied into the hash     *
1623
 *           table with some confidence that it will stay there long enough to *
1624
 *           be used.                                                          *
1625
 *                                                                             *
1626
 *   11.16   King tropism toned down a little to not be quite so intent on     *
1627
 *           closeness to the kings, so that other positional ideas don't get  *
1628
 *           "lost" in the eval.  Back to normal piece values (1,3,3,5,9) for  *
1629
 *           a while.  Optimizations in Quiesce() speeded things up about 2%.  *
1630
 *           Pawns pushed toward the king are not so valuable any more so that *
1631
 *           Crafty won't prefer to hold on to them rather than trading to     *
1632
 *           open a file(s) on the enemy king.  BookLearn() call was moved in  *
1633
 *           Resign() so that it first learns, then resigns, so that it won't  *
1634
 *           get zapped by a SIGTERM right in the middle of learning when      *
1635
 *           xboard realizes the game is over.  Edit/setboard logic modified   *
1636
 *           so that it is now possible to back up, and move forward in a game *
1637
 *           that was set up via these commands.                               *
1638
 *                                                                             *
1639
 *   11.17   EvaluatePawns() modified to eliminate "loose pawn" penalty which  *
1640
 *           was not particularly effective and produced pawn scores that were *
1641
 *           sometimes misleading.  Several optimizations dealing with cache   *
1642
 *           efficiency, primarily changing int's to signed char's so that     *
1643
 *           multiple values fit into a cache line together, to eliminate some *
1644
 *           cache thrashing.  PopCnt() no longer used to recognize that we    *
1645
 *           have reached an endgame database, there's now a counter for the   *
1646
 *           total number of pieces on the board, as it's much faster to inc   *
1647
 *           and dec that value rather than count 1 bits.  Tweaks to MakeMove  *
1648
 *           and UnmakeMove to make things a couple of percent faster, but a   *
108 pmbaty 1649
 *           whole lot cleaner.  Ditto for SEE and Quiesce.  More speed, and   *
33 pmbaty 1650
 *           cleaner code.  Outside passed pawn scored scaled down except when *
1651
 *           the opponent has no pieces at all.  "Lazy eval" cleaned up.  We   *
1652
 *           now have two early exits, one before king safety and one after    *
1653
 *           king safety.  There are two saved values so we have an idea of    *
1654
 *           how large the positional scores have gotten to make this work.    *
1655
 *           Bug in passed pawn code was evaluating black passed pawns as less *
1656
 *           valuable than white.  Fixed and passed pawn scores increased      *
1657
 *           slightly.  Isolated pawn scoring modified so the penalty is pro-  *
1658
 *           portional to the number of isolani each side has, rather than the *
1659
 *           old approach of penalizing the "excess" isolani one side has over *
1660
 *           the other side.  Seems to be more accurate, but we'll see.  Fixed *
1661
 *           a rather gross bug in ValidateMove() related to the way castling  *
1662
 *           status not only indicates whether castling is legal or not, but   *
1663
 *           if it's not, whether that side castled or move the king/rooks.    *
1664
 *           this let ValidateMove() fail to correctly detect that a castling  *
1665
 *           move from the hash table might be invalid, which could wreck the  *
1666
 *           search easily.  This was an oversight from the new status code.   *
108 pmbaty 1667
 *           Serious book bug fixed.  Bookup() was not counting wins and       *
33 pmbaty 1668
 *           losses correctly, a bug introduced to make small.pgn work. Minor  *
1669
 *           bug in the "trapped bishop" (a2/h2/a7/h7) code fixed.  Minor bug  *
108 pmbaty 1670
 *           in SEE() also fixed (early exit that was not safe in rare cases.) *
33 pmbaty 1671
 *                                                                             *
1672
 *   11.18   EvaluatePawns() modified to recognize that if there are two white *
1673
 *           pawns "rammed" by black pawns at (say) c4/c5 and e4/e5, then any  *
1674
 *           pawns on the d-file are also effectively rammed as well since     *
1675
 *           there is no hope for advancing it.  Auto232 automatic play code   *
1676
 *           seems to be working, finally.  The problem was Crafty delayed     *
1677
 *           echoing the move back to auto232, if the predicted move was the   *
1678
 *           move played by the opponent.  Auto232 gave Crafty 3 seconds and   *
1679
 *           then said "too late, you must be hung."  Outpost knight scoring   *
1680
 *           modified to produce scores between 0 and .2 pawns.  A knight was  *
1681
 *           getting too valuable, which caused some confusion in the scoring. *
1682
 *                                                                             *
1683
 *   11.19   Slight reduction in the value of passed pawns, as they were       *
1684
 *           getting pretty large.  Cleaned up annotate.c to get rid of code   *
1685
 *           that didn't work out.  Annotate now gives the PV for the move     *
1686
 *           actually played as well as the move Crafty suggests is best.      *
1687
 *           Crafty now has a reasonable "trade" bonus that kicks in when it   *
1688
 *           has 20 points of material (or less) and is ahead or behind at     *
1689
 *           least 2 pawns.  This trade bonus ranges from 0 to 1000 (one pawn) *
1690
 *           and encourages the side that is winning to trade, while the side  *
1691
 *           that is losing is encouraged to avoid trades.  More minor tweaks  *
1692
 *           for auto232 including trying a delay of 100ms before sending      *
1693
 *           a move.  New annotate command gives the option to get more than   *
1694
 *           one suggested best move displayed, as well as fixes a few bugs    *
1695
 *           in the older code.  Fix to bishop + wrong rook pawn code to also  *
1696
 *           recognize the RP + no pieces is just as bad if the king can reach *
1697
 *           the queening square first.                                        *
1698
 *                                                                             *
1699
 *   11.20   Cleanup to annotate command.  It now won't predict that one side  *
1700
 *           will follow a book line that only resulted in losses.  Also a     *
1701
 *           couple of glitches in annotate where it was possible for a move   *
1702
 *           to take an exhorbitant amount of time if the best move was a      *
1703
 *           book move, but there were no other possible book moves.  The      *
1704
 *           trade bonus now includes trade pawns if behind, but not if ahead  *
1705
 *           as well as now considering being ahead or behind only one pawn    *
1706
 *           rather than the two pawns used previously in 11.19.               *
1707
 *                                                                             *
1708
 *   11.21   Additional bug fix in annotate command that would fail if there   *
1709
 *           was only one legal move to search.  Passed pawn values now have   *
1710
 *           an added evaluation component based on material on the board, so  *
1711
 *           that they become more valuable as material is traded.             *
1712
 *                                                                             *
1713
 *   11.22   Test command modified.  Test <filename> [N] (where N is optional  *
1714
 *           and defaults to 99) will terminate a test position after N con-   *
1715
 *           secutive iterations have had the correct solution.  Bug in        *
1716
 *           Quiesce() fixed, which would incorrectly prune (or fail to prune) *
1717
 *           some captures, due to using "Material" which is computed with     *
1718
 *           respect to white-to-move.  This was fixed many versions ago, but  *
1719
 *           the fix was somehow lost.  Fix to EvaluateDraws that mistakenly   *
1720
 *           declared some endings drawn when there were rookpawns on both     *
1721
 *           sides of the board, and the king could get in front of one. Minor *
1722
 *           fix to Repeat() that was checking one too many entries.  No       *
1723
 *           harmful effect other than one extra iteration in the loop that    *
1724
 *           would make "purify/purity" fail as well as slow things down 1% or *
1725
 *           so.                                                               *
1726
 *                                                                             *
1727
 *   11.23   logpath=, bookpath= and tbpath= command line options added to     *
1728
 *           direct logs, books and tb files to the indicated path.  These     *
1729
 *           only work as command line options, and can't be used in normal    *
1730
 *           places since the files are already opened and in use.  A bug in   *
1731
 *           Evaluate() would call EvaluatePawns() when there were no pawns,   *
1732
 *           breaking things on many systems.                                  *
1733
 *                                                                             *
1734
 *   12.0    Rewrite of Input/Output routines.  Crafty no longer relies on the *
1735
 *           C library buffered I/O routine scanf() for input.  This made it   *
1736
 *           basically impossible to determine when to read data from the key- *
1737
 *           board, which made xboard/winboard difficult to interface with.    *
1738
 *           Now Crafty does its own I/O using read(), which is actually much  *
1739
 *           cleaner and easier.  Minor bug where a fail high in very short    *
1740
 *           time controls might not get played...                             *
1741
 *                                                                             *
1742
 *   12.1    Clean-up of Main().  The code had gotten so obtuse with the       *
1743
 *           pondering call, plus handling Option() plus move input, that it   *
1744
 *           was nearly impossible to follow.  It is now *much* cleaner and    *
1745
 *           easier to understand/follow.                                      *
1746
 *                                                                             *
1747
 *   12.2    Continued cleanup of I/O, particularly the way main() is          *
1748
 *           structured, to make it more understandable.  Rewritten time.c     *
1749
 *           to make it more readable and understandable, not to mention       *
1750
 *           easier to modify.  Fixed a timing problem where Crafty could be   *
1751
 *           pondering, and the predicted move is made followed immediately by *
1752
 *           "force", caused when the opponent makes a move and loses on time, *
1753
 *           or makes a move and resigns.  This would leave the engine state   *
1754
 *           somewhat confused, and would result in the "new" command not      *
1755
 *           reinitializing things at all, which would hang Crafty on xboard   *
1756
 *           or winboard.                                                      *
1757
 *                                                                             *
1758
 *   12.3    Modified piece values somewhat to increase their worth relative   *
1759
 *           to a pawn, to try to stop the tendency to sac a piece for 3 pawns *
1760
 *           and get into trouble for doing so.  Minor fixes in NewGame() that *
1761
 *           caused odd problems, such as learning positions after 1. e4.  The *
1762
 *           position.lrn/.bin files were growing faster than they should.     *
1763
 *           Other minor tweaks to fix a few broken commands in Option().      *
1764
 *           Slight reduction in trade bonus to match slightly less valuable   *
1765
 *           pawns.                                                            *
1766
 *                                                                             *
1767
 *   12.4    Added ABSearch() macro, which makes Search() much easier to read  *
1768
 *           by doing the test to call Search() or Quiesce() in the macro      *
1769
 *           where it is hidden from sight.  Bugs in the way the log files     *
1770
 *           were closed and then used caused crashed with log=off.            *
1771
 *                                                                             *
1772
 *   12.5    Development bonus for central pawns added to combat the h4/a3     *
1773
 *           sort of openings that would get Crafty into trouble.  It now will *
1774
 *           try to seize the center if the opponent dallies around.           *
1775
 *                                                                             *
1776
 *   12.6    Bug in EvaluateDraws() that would erroneously decide that a KNP   *
1777
 *           vs K was drawn if the pawn was a rook pawn and the king could get *
1778
 *           in front of it, which was wrong.  Another minor bug would use     *
1779
 *           EvaluateMate() when only a simple exchange ahead, which would     *
1780
 *           result in sometimes giving up a pawn to reach such a position.    *
1781
 *           Minor bug in SetBoard() would incorrectly reset many important    *
1782
 *           game state variables if the initial position was set to anything  *
1783
 *           other than the normal starting position, such as wild 7.  This    *
1784
 *           would make learning add odd PV's to the .lrn file.  Development   *
1785
 *           bonus modified to penalize moving the queen before minor pieces,  *
1786
 *           and not moving minor pieces at all.  It is now possible to build  *
1787
 *           a "wild 7" book, by first typing "wild 7", and then typing the    *
1788
 *           normal book create command.  Crafty will remember to reset to the *
1789
 *           "wild 7" position at the start of each book line.  Note that it   *
1790
 *           is not possible to have both wild 7 book lines *and* regular book *
1791
 *           lines in the same book.                                           *
1792
 *                                                                             *
1793
 *   12.7    Failsoft added, so that scores can now be returned outside the    *
1794
 *           alpha/beta window.  Minor null-move threat detection added where  *
1795
 *           a mate in 1 score, returned by a null-move search, causes a one   *
1796
 *           ply search extension, since a mate in one is a serious threat.    *
1797
 *           razoring is now disabled if there is *any* sort of extension in   *
1798
 *           the search preceeding the point where razoring would be applied,  *
1799
 *           to avoid razoring taking away one ply from a search that was ex-  *
1800
 *           tended (for good reasons) earlier.  Fail-soft discarded, due to   *
1801
 *           no advantages at all, and a slight slowdown in speed.  Rewrite of *
1802
 *           pawn hashing, plus a rewrite of the weak pawn scoring code to     *
1803
 *           make it significantly more accurate, while going a little slower  *
1804
 *           (but since hashing hits 99% of the time, the slower speed is not  *
1805
 *           noticable at all.)  Evaluation tweaking as well.                  *
1806
 *                                                                             *
1807
 *   12.8    Continued work on weak pawn analysis, as well as a few minor      *
1808
 *           changes to clean the code up.                                     *
1809
 *                                                                             *
1810
 *   13.0    Preparation for the Paris WMCCC version starts here.  First new   *
1811
 *           thing is the blocked pawn evaluation code.  This recognizes when  *
1812
 *           a pawn is immobolized and when a pawn has lever potential, but    *
1813
 *           is disabled if playing a computer opponent.  Passed pawn scoring  *
1814
 *           modified to eliminate duplicate bonuses that were added in        *
1815
 *           EvaluatePawns as well as EvaluatePassedPawns.  Trade bonus code   *
1816
 *           rewritten to reflect trades made based on the material when the   *
1817
 *           search begins.  This means that some things have to be cleared in *
1818
 *           the hash table after a capture is really made on the board, but   *
1819
 *           prevents the eval from looking so odd near the end of a game.     *
1820
 *           minor bug with book random 0 fixed.  If there was only one book   *
1821
 *           move, the search could blow up.                                   *
1822
 *                                                                             *
1823
 *   13.1    All positional scores reduced by 1/4, in an attempt to balance    *
1824
 *           things a little better.  Also more modifications to the weak and  *
1825
 *           blocked pawn code.  Added evaluation code to help with KRP vs KR, *
1826
 *           so that the pawn won't be advanced too far before the king comes  *
1827
 *           to its aid.  Ditto for defending, it knows that once the king is  *
1828
 *           in front of the pawn, it is a draw.                               *
1829
 *                                                                             *
1830
 *   13.2    Blocked pawn scoring changed slightly to not check too far to see *
1831
 *           if a pawn can advance.  Also pawns on center squares are weighted *
1832
 *           more heavily to encourage keeping them there, or trying to get    *
1833
 *           rid of them if the opponent has some there.  Discovered that      *
1834
 *           somehow, the two lines of code that add in bonuses for passed     *
1835
 *           pawns were deleted as I cleaned up and moved code around.  These  *
1836
 *           were reinserted (missing from 13.0 on).                           *
1837
 *                                                                             *
1838
 *   13.3    Modified tablebase code to support 5 piece endings.  There is now *
1839
 *           a configuration option (see Makefile) to specify 3, 4 or 5 piece  *
1840
 *           endgame databases.  More evaluation tuning.                       *
1841
 *                                                                             *
1842
 *   13.4    Modified null-move search.  If there is more than a rook left for *
1843
 *           the side on move, null is always tried, as before.  If there is   *
1844
 *           a rook or less left, the null move is only tried within 5 plies   *
1845
 *           of the leaf positions, so that as the search progresses deeper,   *
1846
 *           null-move errors are at least moved away from the root.  Many     *
1847
 *           evaluation changes, particularly to king safety and evaluating    *
1848
 *           castling to one side while the other side is safer.  Also it now  *
1849
 *           handles pre-castled positions better.                             *
1850
 *                                                                             *
1851
 *   13.5    "From-to" display patch from Jason added.  This shows the from    *
1852
 *           and to squares on a small diagram, oriented to whether Crafty is  *
1853
 *           playing black or white.  The intent is to minimize operator       *
1854
 *           errors when playing black and the board coordinates are reversed. *
1855
 *           More eval changes.                                                *
1856
 *                                                                             *
1857
 *   13.6    Weak pawns on open files now add value to the opponent's rooks    *
1858
 *           since they are the pieces that will primarily use an open file to *
1859
 *           attack a weak pawn.                                               *
1860
 *                                                                             *
1861
 *   13.7    Minor eval tweaks, plus fixes for the CR/LF file reading problems *
1862
 *           in windows NT.                                                    *
1863
 *                                                                             *
1864
 *   13.8    Adjustments to book.c, primarily preventing Crafty from playing   *
1865
 *           lines that had very few games in the file, because those are      *
1866
 *           often quite weak players.  Minor fix to InputMoves() so that a    *
1867
 *           move like bxc3 can *never* be interpreted as a bishop move.  A    *
1868
 *           bishop move must use an uppercase B, as in Bxc3.  Bug in the      *
1869
 *           king safety for white only was not checking for open opponent     *
1870
 *           files on the king correctly.                                      *
1871
 *                                                                             *
1872
 *   13.9    Minor adjustment to time allocation to allow "operator time" to   *
1873
 *           affect time per move.  This is set by "operator <n>" where <n> is *
1874
 *           time in seconds per move that should be "reserved" to allow the   *
1875
 *           operator some typing/bookkeeping time.  New command "book         *
1876
 *           trigger <n>" added so that in tournament mode, when choosing a    *
1877
 *           book line, Crafty will revert to book random 0 if the least       *
1878
 *           popular move was played fewer times than <n>.  This is an attempt *
1879
 *           to avoid trusting narrow book lines too much while still allowing *
1880
 *           adequate randomness.  If the least frequently played move in the  *
1881
 *           set of book moves was played fewer times than this, then a search *
1882
 *           over all the book moves is done.  If the best one doesn't look    *
1883
 *           so hot, Crafty drops out of book and searches *all* legal moves   *
1884
 *           instead.                                                          *
1885
 *                                                                             *
1886
 *   13.10   Minor adjustments.  This is the gold Paris version that is going  *
1887
 *           with Jason.  Any changes made during the event will be included   *
1888
 *           in the next version.                                              *
1889
 *                                                                             *
1890
 *   14.0    Major eval modification to return to centipawn evaluation, rather *
1891
 *           then millipawns.  This reduces the resolution, but makes it a lot *
1892
 *           easier to understand evaluation changes.  Significant eval mods   *
1893
 *           regarding king safety and large positional bonuses, where the ad- *
1894
 *           vantage is not pretty solid.  Note that old book learning will    *
1895
 *           not work now, since the evals are wrong.  Old position learning   *
1896
 *           must be deleted completely.  See the read.me file for details on  *
1897
 *           what must be done about book learning results.  New egtb=n option *
1898
 *           to replace the older -DTABLEBASES=n Makefile option, so that the  *
1899
 *           tablebases can be enabled or disabled without recompiling.        *
1900
 *                                                                             *
1901
 *   14.1    Glitch in EvaluateDevelopment() was producing some grossly large  *
1902
 *           positional scores.  Also there were problems with where the       *
1903
 *           PreEvaluate() procedure were called, so that it could be called   *
1904
 *           *after* doing RootMoveslist(), but before starting a search.  This*
1905
 *           caused minor problems with hashing scores.  One case was that it  *
1906
 *           was possible for a "test suite" to produce slightly different     *
1907
 *           scores than the same position run in normal mode.  This has been  *
1908
 *           corrected.  St=n command now supports fractional times to .01     *
1909
 *           second resolution.                                                *
1910
 *                                                                             *
1911
 *   14.2    MATE reduced to 32768, which makes all scores now fit into 16     *
1912
 *           bits.  Added code to EvaluatePawns() to detect the Stonewall      *
1913
 *           pawn formation and not like it as black.  Also it detects a       *
1914
 *           reversed Stonewall formation (for black) and doesn't like it as   *
1915
 *           white.  EvaluateOutsidePassedPawns() removed.  Functionality is   *
1916
 *           incorporated in EvaluatePawns() and is hashed, which speeds the   *
1917
 *           evaluation up a bit.  King safety is now fully symmetric and is   *
1918
 *           folded into individual piece scoring to encourage pieces to       *
1919
 *           attack an unsafe king.                                            *
1920
 *                                                                             *
1921
 *   14.3    Minor adjustments to the king safety code for detecting attacks   *
1922
 *           initiated by pushing kingside pawns.  Flip/flop commands added to *
1923
 *           mirror the board either horizontally or vertically to check for   *
1924
 *           symmetry bugs in the evaluation.  Other eval tuning changes.      *
1925
 *                                                                             *
1926
 *   14.4    Queen value increased to stop trading queen for two rooks.  Book  *
1927
 *           learning slightly modified to react quicker.  Also scores are now *
1928
 *           stored in the book file as floats to get rid of an annoying trun- *
1929
 *           cation problem that was dragging scores toward zero.  The book    *
1930
 *           file now has a "version" so that old book versions won't cause    *
1931
 *           problems due to incorrect formats.  Null-move mated threat ex-    *
1932
 *           tension was modified.  If null-move search fails high, I fail     *
1933
 *           high as always, if it fails low, and it says I get mated in N     *
1934
 *           moves, I extend all moves at this ply, since something is going   *
1935
 *           on.  I also carry this threat extension into the hash table since *
1936
 *           a hash entry can say "don't do null move, it will fail low."  I   *
1937
 *           now have a flag that says not only will null-move fail low, it    *
1938
 *           fails to "mated in N" so this ply needs extending even without a  *
1939
 *           null-move search to tell it so.  Book learning greatly modified   *
1940
 *           in the way it "distributes" the learned values, in an effort to   *
1941
 *           make learning happen faster.  I now construct a sort of "tree"    *
1942
 *           so I know how many book alternatives Crafty had at each move it   *
1943
 *           at each book move it played.  I then assign the whole learn value *
1944
 *           to all moves below the last book move where there was a choice,   *
1945
 *           and also assign the whole learn value to the move where there     *
1946
 *           was a choice.  I then divide the learn value by the number of     *
1947
 *           choices and assign this to each book move (working backward once  *
1948
 *           again) until the next point where there were choices.  This makes *
1949
 *           sure that if there is a choice, and it is bad, it won't be played *
1950
 *           again, even if this choice was at move 2, and the remainder of    *
1951
 *           the book line was completely forced.  All in an effort to learn   *
1952
 *           more quickly.                                                     *
1953
 *                                                                             *
1954
 *   14.5    annotate bug fixed where if there was only one legal move, the    *
108 pmbaty 1955
 *           annotation would stop with no warning.  Bookup() now produces a   *
33 pmbaty 1956
 *           more useful error message, giving the exact line number in the    *
1957
 *           input file where the error occurred, to make fixing them easier.  *
1958
 *           If you are playing without a book.bin, 14.4 would crash in the    *
108 pmbaty 1959
 *           book learning code.  This is now caught and avoided.  Bookup()    *
33 pmbaty 1960
 *           now knows how to skip analysis in PGN files, so you can use such  *
1961
 *           files to create your opening book, without having problems.  It   *
1962
 *           understands that nesting () {} characters "hides" the stuff in-   *
1963
 *           side them, as well as accepting non-standard PGN comments that    *
1964
 *           are inside [].  Annotate() now will annotate a PGN file with      *
1965
 *           multiple games, although it will use the same options for all of  *
1966
 *           the games.  PGN headers are preserved in the .can file.  Annotate *
1967
 *           now will recognize the first move in a comment (move) or {move}   *
1968
 *           and will search that move and give analysis for it in addition to *
1969
 *           the normal output.                                                *
1970
 *                                                                             *
1971
 *   14.6    Minor change to book random 4 (use learned value.)  if all the    *
1972
 *           learned values are 0, then use the book random 3 option instead.  *
1973
 *           Bug in 50-move counter fixed.  Learn() could cause this to be set *
1974
 *           to a value that didn't really reflect the 50-move status, and     *
1975
 *           cause games to be drawn when they should not be.  Modified the    *
1976
 *           "Mercilous" attack code to recognize a new variation he worked    *
1977
 *           out that sacrifices a N, R and B for a mating attack.  No more.   *
1978
 *                                                                             *
1979
 *   14.7    "Verbose" command removed and replaced by a new "display" command *
1980
 *           that sets display options.  Use "help display" for more info on   *
1981
 *           how this works.  The "mercilous" attack scoring is only turned on *
1982
 *           when playing "mercilous".  But there is a new "special" list that *
1983
 *           you can add players too if you want, and this special scoring is  *
1984
 *           turned on for them as well.                                       *
1985
 *                                                                             *
1986
 *   14.8    New scoring term to penalize unbalanced trades.  IE if Crafty has *
1987
 *           to lose a pawn, it would often trade a knight for two pawns in-   *
1988
 *           stead, which is actually worse.  This new term prevents this sort *
1989
 *           of trades.                                                        *
1990
 *                                                                             *
1991
 *   14.9    "Mercilous" attack code modified to handle a new wrinkle he (and  *
1992
 *           one other player) found.                                          *
1993
 *                                                                             *
1994
 *   14.10   New "bench" command runs a test and gives a benchmark comparison  *
1995
 *           for Crafty and compares this to a P6/200 base machine.  Minor bug *
1996
 *           disabled the "dynamic draw score" and fixed it at zero, even if   *
1997
 *           the opponent was much weaker (rating) or behind on time.  Also    *
1998
 *           draw_score was not reset to 0 at start of each new game, possibly *
1999
 *           leaving it at +.20 after playing a higher-rated opponent.         *
2000
 *                                                                             *
2001
 *   14.11   Release to make mercilous attack detection code the default, to   *
2002
 *           prevent the rash of mercilous attacks on the chess servers.       *
2003
 *                                                                             *
2004
 *   14.12   Modified tuning of evaluation.  Scores were reduced in the past,  *
2005
 *           to discourage the piece for two pawns sort of trades, but since   *
2006
 *           this was basically solved in 14.8, scores are going "back up".    *
2007
 *           Particularly passed pawn scores.  Crafty now accepts draw offers  *
2008
 *           if the last search result was <= the current result returned by   *
2009
 *           DrawScore().  It also honors draw requests via xboard when        *
2010
 *           against a human opponent as well, and will flag the game as over. *
2011
 *           Minor modification to annotate.c to display move numbers in the   *
2012
 *           PV's to match the PV output format used everywhere else in        *
2013
 *           Crafty.  A new type of learning, based on results, has been added *
2014
 *           and is optional (learn=7 now enables every type of learning.)     *
2015
 *           this type of learning uses the win/lose game result to flag an    *
2016
 *           opening that leads to a lost game as "never" play again.  The     *
2017
 *           book move selection logic has been modified.  There are four com- *
2018
 *           ponents used in choosing book moves:  frequency of play; win/lose *
2019
 *           ratio; static evaluation; and learned score.  There are four      *
2020
 *           weights that can be modified with the bookw command, that control *
2021
 *           how these 4 values are used to choose book moves.  Each weight is *
2022
 *           a floating point value between 0 and 1, and controls how much the *
2023
 *           corresponding term factors in to move selection.                  *
2024
 *                                                                             *
2025
 *   14.13   Fixed a book parsing bug that now requires a [Site "xxx"] tag in  *
2026
 *           any book file between lines for the "minplay" option of book      *
2027
 *           create to work properly.                                          *
2028
 *                                                                             *
2029
 *   15.0    This is the first version to support a parallel search using      *
2030
 *           multiple processors on a shared-memory machinel.  This version    *
2031
 *           uses the "PVS" algorithm (Principal Variation Search) that is an  *
2032
 *           early form of "Young Brothers Wait".  This is not the final       *
2033
 *           search algorithm that will be used, but it is relatively easy to  *
2034
 *           implement, so that the massive data structure changes can be de-  *
2035
 *           bugged before getting too fancy.  The major performance problem   *
2036
 *           with this approach is that all processors work together at a node *
2037
 *           and when one finishes, it has to wait until all others finish     *
2038
 *           before it can start doing anything more.  This adds up to a       *
2039
 *           significant amount of time and really prevents good speedups on   *
2040
 *           multiprocessor machines.  This restriction will be eliminanted in *
2041
 *           future versions, but it makes debugging much simpler for the      *
2042
 *           first version.  After much agonizing, I finally chose to write my *
2043
 *           own "spinlock" macros to avoid using the pthread_mutex_lock stuff *
2044
 *           that is terribly inefficient due to its trying to be cute and     *
2045
 *           yield the processor rather than spinning, when the lock is al-    *
2046
 *           ready set.  i'd rather spin than context-switch.                  *
2047
 *                                                                             *
2048
 *   15.1    This version actually unleashes the full design of the parallel   *
2049
 *           search, which lets threads split away from the "pack" when        *
2050
 *           they become idle, and they may then jump in and help another      *
2051
 *           busy thread.  This eliminates the significant delays that         *
2052
 *           occur near the end of a search at a given ply.  Now as those pro- *
2053
 *           cessors drop out of the search at that ply, they are reused at    *
2054
 *           other locations in the tree without any significant delays.       *
2055
 *                                                                             *
2056
 *   15.2    WinNT support added.  Also fixed a small bug in Interrupt() that  *
2057
 *           could let it exit with "busy" set so that it would ignore all     *
2058
 *           input from that point forward, hanging the game up.  Added a new  *
2059
 *           "draw accept" and "draw decline" option to enable/disable Crafty  *
2060
 *           accepting draw offers if it is even or behind.  Turned on by      *
2061
 *           an IM/GM opponent automatically.  Thread pools now being used to  *
2062
 *           avoid the overhead of creating/destroying threads at a high rate  *
2063
 *           rate of speed.  It is now quite easy to keep N processors busy.   *
2064
 *                                                                             *
2065
 *   15.3    CPU time accounting modified to correctly measure the actual time *
2066
 *           spent in a parallel search, as well as to modify the way Crafty   *
2067
 *           handles time management in an SMP environment.  A couple of bugs  *
2068
 *           fixed in book.c, affecting which moves were played.  Also veri-   *
2069
 *           fied that this version will play any ! move in books.bin, even if *
2070
 *           it was not played in the games used to build the main book.bin    *
2071
 *           file.                                                             *
2072
 *                                                                             *
2073
 *   15.4    This version will terminate threads once the root position is in  *
2074
 *           a database, to avoid burning cpu cycles that are not useful.  A   *
2075
 *           flag "done" is part of each thread block now.  When a thread goes *
2076
 *           to select another move to search, and there are no more left, it  *
2077
 *           sets "done" in the parent task thread block so that no other pro- *
2078
 *           cessors will attempt to "join the party" at that block since no   *
2079
 *           work remains to be done.  Book selection logic modified so that   *
2080
 *           frequency data is "squared" to improve probabilities.  Also, the  *
2081
 *           frequency data is used to cull moves, in that if a move was not   *
2082
 *           played 1/10th of the number of times that the "most popular" move *
2083
 *           was played, then that move is eliminated from consideration.      *
2084
 *                                                                             *
2085
 *   15.5    Changes to "volatile" variables to enhance compiler optimization  *
2086
 *           in places where it is safe.  Added a function ThreadStop() that   *
2087
 *           works better at stopping threads when a fail high condition       *
2088
 *           occurs at any nodes.  It always stopped all sibling threads at    *
2089
 *           the node in question, but failed to stop threads that might be    *
2090
 *           working somewhere below the fail-high node.  ThreadStop() will    *
2091
 *           recursively call itself to stop all of those threads. Significant *
2092
 *           increase in king centralization (endgame) scoring to encourage    *
2093
 *           the king to come out and fight rather than stay back and get      *
2094
 *           squeezed to death.  Minor change to queen/king tropism to only    *
2095
 *           conside the file distance, not ranks also.  Bug in interrupt.c    *
2096
 *           fixed.  This bug would, on very rare occasion, let Crafty read in *
2097
 *           a move, but another thread could read on top of this and lose the *
2098
 *           move.  Xboard would then watch the game end with a <flag>.        *
2099
 *                                                                             *
2100
 *   15.6    Ugly repetition draw bug (SMP only) fixed.  I carefully copied    *
2101
 *           the repetition list from parent to child, but also copied the     *
2102
 *           repetition list pointer...  which remained pointing at the parent *
2103
 *           repetition list.  Which failed, of course.                        *
2104
 *                                                                             *
2105
 *   15.7    Problem in passing PV's back up the search fixed.  The old bug of *
2106
 *           a fail high/fail low was not completely handled, so that if the   *
2107
 *           *last* move on a search failed high, then low, the PV could be    *
2108
 *           broken or wrong, causing Crafty to make the wrong move, or else   *
2109
 *           break something when the bogus PV was used.  Piece/square tables  *
2110
 *           now only have the _w version initialized in data.c, to eliminate  *
2111
 *           the second copy which often caused errors.  Initialize() now will *
2112
 *           copy the _w tables to the _b tables (reflected of course.)        *
2113
 *                                                                             *
2114
 *   15.8    trade bonus modified to not kick in until one side is two pawns   *
2115
 *           ahead or behind, so that trading toward a draw is not so common.  *
2116
 *           Fixed a whisper bug that would cause Crafty to whisper nonsense   *
2117
 *           if it left book, then got back in book.  It also now whispers the *
2118
 *           book move it played, rather than the move it would play if the    *
2119
 *           opponent played the expected book move, which was confusing.      *
2120
 *                                                                             *
2121
 *   15.9    Bug in positions with one legal move would abort the search after *
2122
 *           a 4 ply search, as it should, but it was possible that the result *
2123
 *           from the aborted search could be "kept" which would often make    *
2124
 *           Crafty just "sit and wait" and flag on a chess server.            *
2125
 *                                                                             *
2126
 *   15.10   Fixed "draw" command to reject draw offers if opponent has < ten  *
2127
 *           seconds left and no increment is being used.  Fixed glitch in     *
2128
 *           LearnResult() that would crash Crafty if no book was being used,  *
2129
 *           and it decides to resign.  Modified trade bonus code to eliminate *
2130
 *           a hashing inconsistency.  Minor glitch in "show thinking" would   *
2131
 *           show bad value if score was mate.  Added Tim Mann's new xboard/   *
2132
 *           winboard "result" command and modified the output from Crafty to  *
2133
 *           tell xboard when a game is over, using the new result format that *
2134
 *           Tim now expects.  Note that this is xboard 3.6.9, and earlier     *
2135
 *           versions will *not* work correctly (ok on servers, but not when   *
2136
 *           you play yourself) as the game won't end correctly due to the     *
2137
 *           change in game-end message format between the engine and xboard.  *
2138
 *                                                                             *
2139
 *   15.11   Modified SMP code to not start a parallel search at a node until  *
2140
 *           "N" (N=2 at present) moves have been search.  N was 1, and when   *
2141
 *           move ordering breaks down the size of the tree gets larger.  This *
2142
 *           controls that much better.  Fixed an ugly learning bug.  If the   *
2143
 *           last move actually in the game history was legal for the other    *
2144
 *           side, after playing it for the correct side, it could be played   *
2145
 *           a second time, which would break things after learning was done.  *
2146
 *           An example was Rxd4 for white, where black can respond with Rxd4. *
2147
 *           Changes for the new 3.6.10 xboard/winboard are also included as   *
2148
 *           well as a fix to get book moves displayed in analysis window as   *
2149
 *           it used to be done.                                               *
2150
 *                                                                             *
2151
 *   15.12   "The" SMP repetition bug was finally found and fixed.  CopyToSMP  *
2152
 *           was setting the thread-specific rephead_* variable *wrong* which  *
2153
 *           broke so many things it was not funny.  Evaluation tuning in this *
2154
 *           version as well to try to "center" values in a range of -X to +X  *
2155
 *           rather than 0 to 2X.  Trade bonus had a bug in the pawn part that *
2156
 *           not only encouraged trading pawns when it should be doing the     *
2157
 *           opposite, but the way it was written actually made it look bad to *
2158
 *           win a pawn.  More minor xboard/winboard compatibility bugs fixed  *
2159
 *           so that games end when they should.  DrawScore() had a quirk that *
2160
 *           caused some problems, in that it didn't know what to do with non- *
2161
 *           zero draw scores, and could return a value with the wrong sign at *
2162
 *           times.  It is now passed a flag, "Crafty_is_white" to fix this.   *
2163
 *           Modification so that Crafty can handle the "FEN" PGN tag and set  *
2164
 *           the board to the correct position when it is found.  Xboard       *
2165
 *           options "hard" and "easy" now enable and disable pondering.       *
2166
 *                                                                             *
2167
 *   15.13   Modification to "bad trade" code to avoid any sort of unbalanced  *
2168
 *           trades, like knight/bishop for 2-3 pawns, or two pieces for a     *
2169
 *           rook and pawn.  More new xboard/winboard fixes for Illegal move   *
2170
 *           messages and other engine interface changes made recently.  Minor *
2171
 *           change to null move search, to always search with the alpha/beta  *
2172
 *           window of (beta-1,beta) rather than (alpha,beta), which is very   *
2173
 *           slightly more efficient.                                          *
2174
 *                                                                             *
2175
 *   15.14   Rewrite of ReadPGN() to correctly handle nesting of various sorts *
2176
 *           of comments.  This will now correctly parse wall.pgn, except for  *
2177
 *           promotion moves with no promotion piece specified.  The book      *
2178
 *           create code also now correctly reports when you run out of disk   *
2179
 *           space and this should work under any system to let you know when  *
2180
 *           you run out of space.                                             *
2181
 *                                                                             *
108 pmbaty 2182
 *   15.15   Major modifications to Book() and Bookup().  Bookup() now needs   *
33 pmbaty 2183
 *           1/2 the temp space to build a book that it used to need (even     *
2184
 *           less under windows).  Also, a book position is now 16 bytes in-   *
2185
 *           stead of 20 (or 24 under windows) further reducing the space      *
2186
 *           requirements.  The "win/lose/draw" counts are no longer absolute, *
2187
 *           rather they are relative so they can be stored in one byte. A new *
2188
 *           method for setting "!" moves in books.bin is included in this     *
2189
 *           version.  For any move in the input file used to make books.bin,  *
2190
 *           you can add a {play nn%} string, which says to play this move nn% *
2191
 *           of the time, regardless of how frequently it was played in the    *
2192
 *           big book file.  For example, e4 {play 50%} says to play e4 50% of *
2193
 *           the time.  Note two things here:  (1) If the percentages for all  *
2194
 *           of white's first moves add up to a number > 100, then the         *
2195
 *           percentage played is taken as is, but all will be treated as if   *
2196
 *           they sum to 100 (ie if there are three first moves, each with a   *
2197
 *           percentage of 50%, they will behave as though they are all 33%    *
2198
 *           moves).  (2) For any moves not marked with a % (assuming at least *
2199
 *           one move *is* marked with a percent) then such moves will use the *
2200
 *           remaining percentage left over applied to them equally. Note that *
2201
 *           using the % *and* the ! flags together slightly changes things,   *
2202
 *           because normally only the set of ! moves will be considered, and  *
2203
 *           within that set, any with % specified will have that percent used *
2204
 *           as expected.  But if you have some moves with !, then a percent   *
2205
 *           on any of the *other* moves won't do a thing, because only the !  *
2206
 *           moves will be included in the set to choose from.                 *
2207
 *                                                                             *
2208
 *   15.16   Bug in the king tropism code, particularly for white pieces that  *
2209
 *           want to be close to the black king, fixed in this version.  New   *
2210
 *           History() function that handles all history/killer cases, rather  *
2211
 *           than the old 2-function approach.  Ugly Repeat() bug fixed in     *
2212
 *           CopyToChild().  This was not copying the complete replist due to  *
2213
 *           a ply>>2 rather than ply>>1 counter.                              *
2214
 *                                                                             *
2215
 *   15.17   Adjustments to "aggressiveness" to slightly tone it down.  Minor  *
2216
 *           fix to avoid the odd case of whispering one move and playing an-  *
2217
 *           other, caused by a fail-high/fail-low overwriting the pv[1] stuff *
2218
 *           and then Iterate() printing that rather than pv[0] which is the   *
2219
 *           correct one.  New LegalMove() function that strengthens the test  *
2220
 *           for legality at the root, so that it is impossible to ponder a    *
2221
 *           move that looks legal, but isn't.  Modifications to king safety   *
2222
 *           to better handle half-open files around the king as well as open  *
2223
 *           files.  EGTB "swindler" added.  If the position is drawn at the   *
2224
 *           root of the tree, then all losing moves are excluded from the     *
2225
 *           move list and the remainder are searched normally with the EGTB   *
2226
 *           databases disabled, in an effort to give the opponent a chance to *
2227
 *           go wrong and lose.  Fixed "mode tournament" and "book random 0"   *
2228
 *           so that they will work correctly with xboard/winboard, and can    *
2229
 *           even be used to play bullet games on a server if desired. Minor   *
2230
 *           fix to attempt to work around a non-standard I/O problem on the   *
2231
 *           Macintosh platform dealing with '\r' as a record terminator       *
2232
 *           rather than the industry/POSIX-standard \n terminator character.  *
2233
 *                                                                             *
2234
 *   15.18   Fix to the "material balance" evaluation term so that it will not *
2235
 *           like two pieces for a rook (plus pawn) nor will it like trading a *
2236
 *           piece for multiple pawns.  A rather gross bug in the repetition   *
2237
 *           code (dealing with 50 move rule) could overwrite random places in *
2238
 *           memory when the 50 move rule approached, because the arrays were  *
2239
 *           not long enough to hold 50 moves, plus allow the search to go     *
2240
 *           beyond the 50-move rule (which is legal and optional) and then    *
2241
 *           output moves (which uses ply=65 data structures).  This let the   *
2242
 *           repetition (50-move-rule part) overwrite things by storing well   *
2243
 *           beyond the end of the repetition list for either side.  Minor fix *
2244
 *           to EGTB "swindle" code.  It now only tries the searches if it is  *
2245
 *           the side with swindling chances.  Ie it is pointless to try for   *
2246
 *           a swindle in KRP vs KR if you don't have the P.  :)  Some very    *
2247
 *           substantial changes to evaluate.c to get things back into some    *
2248
 *           form of synch with each other.                                    *
2249
 *                                                                             *
2250
 *   15.19   More evaluation changes.  Slight change to the Lock() facility    *
2251
 *           as it is used to lock the hash table.  Modifications to the book  *
2252
 *           selection logic so that any move in books.bin that is not in the  *
2253
 *           regular book.bin file still can be played, and will be played if  *
2254
 *           it has a {play xxx%} or ! option.  New option added to the book   *
2255
 *           create command to allow the creation of a better book.  This      *
2256
 *           option lets you specify a percentage that says "exclude a book    *
2257
 *           move it if didn't win xx% as many games as it lost.  IE, if the   *
2258
 *           book move has 100 losses, and you use 50% for the new option, the *
2259
 *           move will only be included if there are at least 50 wins.  This   *
2260
 *           allows culling lines that only lost, for example.  New bad trade  *
2261
 *           scoring that is simpler.  If one side has one extra minor piece   *
2262
 *           and major pieces are equal, the other side has traded a minor for *
2263
 *           pawns and made a mistake.  If major pieces are not matched, and   *
2264
 *           one side has two or more extra minors, we have seen either a rook *
2265
 *           for two minor pieces (bad) or a queen for three minor pieces (bad *
2266
 *           also).  Horrible bug in initializing min_thread_depth, which is   *
2267
 *           supposed to control thrashing.  The SMP search won't split the    *
2268
 *           tree within "min_thread_depth" of the leaf positions.  Due to a   *
2269
 *           gross bug, however, this was initialized to "2", and would have   *
2270
 *           been the right value except that a ply in Crafty is 4.  The       *
2271
 *           mtmin command that adjusts this correctly multiplied it by 4,     *
2272
 *           but in data.c, it was left at "2" which lets the search split way *
2273
 *           too deeply and can cause thrashing.  It now correctly defaults to *
2274
 *           120 (2*PLY) as planned.  Crafty now *only* supports               *
2275
 *           winboard/xboard 4.0 or higher, by sending the string "move xxx"   *
2276
 *           to indicate its move.  This was done to eliminate older xboard    *
2277
 *           versions that had some incompatibilities with Crafty that were    *
2278
 *           causing lots of questions/problems.  Xboard 4.0 works perfectly   *
2279
 *           and is the only recommended GUI now.                              *
2280
 *                                                                             *
2281
 *   15.20   New evaluation term to ensure that pawns are put on squares of    *
2282
 *           the opposite color as the bishop in endings where one side only   *
2283
 *           has a single bishop.  Another new evaluation term to favor        *
2284
 *           bishops over knights in endings with pawns on both sides of the   *
2285
 *           board.  Search extensions now constrained so that when doing an   *
2286
 *           N-ply search, the first 2N plies can extend normally, but beyond  *
2287
 *           that the extensions are reduced by one-half.  This eliminates a   *
2288
 *           few tree explosions that wasted lots of time but didn't produce   *
2289
 *           any useful results.  This version solves 299/300 of the Win at    *
2290
 *           Chess positions on a single-cpu pentium pro 200mhz machine now.   *
2291
 *           New evaluation term to reward "pawn duos" (two pawns side by side *
2292
 *           which gives them the most flexibility in advancing.  A change to  *
2293
 *           the bishops of opposite color to not just consider B vs B drawish *
2294
 *           when the B's are on opposite colors, but to also consider other   *
2295
 *           positons like RB vs RB drawish with opposite B's.                 *
2296
 *                                                                             *
108 pmbaty 2297
 *   16.0    Hash functions renamed to ProbeTransRef() and StoreTransRef().    *
2298
 *           The store functions (2) were combined to eliminate some duplicate *
2299
 *           code and shrink the cache footprint a bit.  Adjustment to "losing *
2300
 *           the right to castle" so that the penalty is much less if this is  *
2301
 *           done when trading queens, since a king in the center becomes less *
2302
 *           of a problem there. Several eval tweaks.  Support for Eugene      *
2303
 *           Nalimov's new endgame databases that reduce the size by 50%.      *
2304
 *           Added a new cache=xxx command to set the tablebase cache (more    *
2305
 *           memory means less I/O of course).  The "egtb" command now has two *
2306
 *           uses.  If you enter egtb=0, you can disable tablebases for        *
2307
 *           testing.  Otherwise, you just add "egtb" to your command line or  *
2308
 *           .craftyrc file and Crafty automatically recognizes the files that *
2309
 *           are present and sets up to probe them properly.  Added an eval    *
2310
 *           term to catch a closed position with pawns at e4/d3/c4 and then   *
2311
 *           avoiding the temptation to castle into an attack.  Most of the    *
2312
 *           evaluation discontinuities have been removed, using the two new   *
2313
 *           macros ScaleToMaterial() and InverseScaleToMaterial().  Scale()   *
2314
 *           is used to reduce scores as material is removed from the board,   *
2315
 *           while InverseScale() is used to increase the score as material    *
2316
 *           is removed.  This removed a few ugly effects of things happening  *
33 pmbaty 2317
 *           right around the EG_MAT threshold.                                *
2318
 *                                                                             *
2319
 *   16.1    Bug in EGTBProbe() which used the wrong variable to access the    *
2320
 *           enpassant target square was fixed.  The cache=x command now       *
2321
 *           checks for a failed malloc() and reverts to the default cache     *
2322
 *           size to avoid crashes.  Major eval tuning changes.  Cache bug     *
2323
 *           would do an extra malloc() and if it is done after the "egtb"     *
2324
 *           command it would crash.  Analyze mode now turns off the "swindle" *
2325
 *           mode.                                                             *
2326
 *                                                                             *
2327
 *   16.2    More evaluation changes, specifically to bring king safety down   *
2328
 *           to reasonable levels.  Support for the DGT electronic chess board *
2329
 *           is now included (but only works in text mode, not in conjunction  *
108 pmbaty 2330
 *           with xboard/winboard yet.  Fix to StoreTransRef() that corrected  *
2331
 *           a problem with moving an entry from tablea to tableb, but to the  *
33 pmbaty 2332
 *           wrong address 50% of the time (thanks to J. Wesley Cleveland).    *
2333
 *                                                                             *
2334
 *   16.3    Performance tweaks plus a few evaluation changes.  An oversight   *
2335
 *           would let Crafty play a move like exd5 when cxd5 was playable.    *
2336
 *           IE it didn't follow the general rule "capture toward the center." *
2337
 *           this has been fixed.  King safety ramped up just a bit.           *
2338
 *                                                                             *
2339
 *   16.4    Search extension fixed.  One-reply was being triggered when there *
2340
 *           was only one capture to look at as well, which was wrong.  Minor  *
2341
 *           fix to king safety in Evaluate().  Adjustments to preeval to fix  *
2342
 *           some "space" problems caused by not wanting to advance pawns at   *
2343
 *           all, plus some piece/square pawn values that were too large near  *
2344
 *           the opponent's king position.  New swindle on*off command allows  *
2345
 *           the user to disable "swindle mode" so that Crafty will report     *
2346
 *           tablebase draws as draws.  Swindle mode is now disabled auto-     *
2347
 *           matically in analysis or annotate modes.  DrawScore() now returns *
2348
 *           a draw score that is not nearly so negative when playing humans.  *
2349
 *           The score was a bit large (-.66 and -.33) so that this could tend *
2350
 *           to force Crafty into losing rather than drawn positions at times. *
2351
 *           EGTB probe code now monitors how the probes are affecting the nps *
2352
 *           and modifies the probe depth to control this.  IE it adjusts the  *
2353
 *           max depth of probes to attempt to keep the NPS at around 50% or   *
2354
 *           thereabouts of the nominal NPS rate.  Fixed a nasty bug in the    *
2355
 *           EGTB 'throttle' code.  It was possible that the EGTB_mindepth     *
2356
 *           could get set to something more than 4 plies, then the next       *
2357
 *           search starts off already in a tablebase ending, but with this    *
2358
 *           probe limit, it wouldn't probe yet Iterate() would terminate the  *
2359
 *           search after a couple of plies.  And the move chosen could draw.  *
2360
 *           New code submitted by George Barrett, which adds a new command    *
2361
 *           "html on".  Using this will cause the annotate command to produce *
2362
 *           a game.html file (rather than game.can) which includes nice board *
108 pmbaty 2363
 *           displays (bitmapped images) everywhere Crafty suggests a          *
2364
 *           different move.  You will need to download the bitmaps directory  *
2365
 *           files to make this work.  Note that html on enables this or you   *
2366
 *           will get normal annotation output in the .can file.  Final        *
2367
 *           hashing changes made.  Now crafty has only two hash tables, the   *
2368
 *           'depth preferred' and 'always store' tables, but not one for each *
2369
 *           side.                                                             *
33 pmbaty 2370
 *                                                                             *
2371
 *   16.5    Minor glitch in internal iterative deepening code (in search.c)   *
2372
 *           could sometimes produce an invalid move.  Crafty now probes the   *
2373
 *           hash table to find the best move which works in all cases.        *
2374
 *           Interesting tablebase bug fixed.  By probing at ply=2, the        *
2375
 *           search could overlook a repetition/50 move draw, and turn a won   *
2376
 *           position into a draw.  Crafty now only probes at ply > 2, to      *
108 pmbaty 2377
 *           let the Repeat() function have a chance.  Added code by Dan       *
2378
 *           Corbitt to add environment variables for the tablebases, books,   *
33 pmbaty 2379
 *           logfiles and the .craftyrc file paths.  The environment variables *
2380
 *           CRAFTY_TB_PATH, CRAFTY_BOOK_PATH, CRAFTY_LOG_PATH and             *
2381
 *           CRAFTY_RC_PATH should point to where these files are located and  *
2382
 *           offer an alternative to specifying them on the command line.      *
2383
 *           Final version of the EGTB code is included in this version.  This *
2384
 *           allows Crafty to use either compressed (using Andrew Kadatch's    *
2385
 *           compressor, _not_ zip or gzip) or non-compressed tablebases.  It  *
2386
 *           will use non-compressed files when available, and will also use   *
2387
 *           compressed files that are available so long as there is no non-   *
2388
 *           compressed version of the same file.  It is allowable to mix and  *
2389
 *           match as you see fit, but with all of the files compressed, the   *
2390
 *           savings is tremendous, roughly a factor of 5 in space reduction.  *
2391
 *                                                                             *
2392
 *   16.6    Major rewrite of king-safety pawn shelter code to emphasize open  *
2393
 *           files (and half-open files) moreso than just pawns that have been *
2394
 *           moved.  Dynamic EGTB probe depth removed.  This caused far too    *
2395
 *           many search inconsistencies.  A fairly serious SMP bug that could *
2396
 *           produce a few hash glitches was found.  Rewrite of the 'king-     *
2397
 *           tropism' code completed.  Now the pieces that are 'crowding' the  *
2398
 *           king get a bigger bonus if there are more of them.  Ie it is more *
2399
 *           than twice as good if we have two pieces close to the king, while *
2400
 *           before this was purely 'linear'.  Minor fix to book.c to cure an  *
2401
 *           annotate command bug that would produce odd output in multi-game  *
2402
 *           PGN files.                                                        *
2403
 *                                                                             *
2404
 *   16.7    Minor bug in 'book create' command would produce wrong line num-  *
2405
 *           bers if you used two book create commands without restarting      *
2406
 *           Crafty between uses.  Replaced "ls" command with a !cmd shell     *
2407
 *           escape to allow _any_ command to be executed by a shell. Change   *
2408
 *           to bishop scoring to avoid a bonus for a bishop pair _and_ a good *
2409
 *           bishop sitting at g2/g7/etc to fill a pawn hole.  If the bishop   *
2410
 *           fills that hole, the bishop pair score is reduced by 1/2 to avoid *
2411
 *           scores getting too large.  Another ugly SMP bug fixed.  It was    *
2412
 *           for the CopyFromSMP() function to fail because Search() put the   *
2413
 *           _wrong_ value into tree->value.  The "don't castle into an unsafe *
2414
 *           king position" code was scaled up a bit as it wasn't quite up to  *
2415
 *           detecting all cases of this problem.  The parallel search has     *
2416
 *           been modified so that it can now split the tree at the root, as   *
2417
 *           well as at deeper nodes.  This has improved the parallel search   *
2418
 *           efficiency noticably while also making the code a bit more        *
2419
 *           complex.  This uses an an adaptive algorithm that pays close      *
2420
 *           attention to the node count for each root move.  If one (or more) *
2421
 *           moves below the best move have high node counts (indicating that  *
2422
 *           these moves might become new 'best' moves) then these moves are   *
2423
 *           flagged as "don't search in parallel" so that all such moves are  *
2424
 *           searched one by one, using all processors so that a new best move *
2425
 *           if found faster.  Positional scores were scaled back 50% and the  *
2426
 *           king safety code was again modified to incorporate pawn storms,   *
2427
 *           which was intentionally ignored in the last major revision.  Many *
2428
 *           other scoring terms have been modified as well to attempt to      *
2429
 *           bring the scores back closer to something reasonable.             *
2430
 *                                                                             *
2431
 *   16.8    Bug in evaluate.c (improper editing) broke the scoring for white  *
108 pmbaty 2432
 *           rooks on open files (among other things.)  New twist on null-     *
33 pmbaty 2433
 *           move search...  Crafty now uses R=3 near the root of the tree,    *
2434
 *           and R=2 near the top.  It also uses null-move so long as there is *
2435
 *           a single piece on the board, but when material drops to a rook or *
2436
 *           less, null move is turned off and only used in the last 7 plies   *
2437
 *           to push the zugzwang errors far enough off that they can be       *
2438
 *           avoided, hopefully.                                               *
2439
 *                                                                             *
2440
 *   16.9    Tuning to king safety.  King in center of board was way too high  *
2441
 *           which made the bonus for castling way too big.  More fine-tuning  *
2442
 *           on the new 'tropism' code (that tries to coordinate piece attacks *
2443
 *           on the king).  It understands closeness and (now) open files for  *
2444
 *           the rooks/queens.  Modified blocked center pawn code so that an   *
2445
 *           'outpost' in front of an unmoved D/E pawn is made even stronger   *
2446
 *           since it restrains that pawn and cramps the opponent.             *
2447
 *                                                                             *
2448
 *   16.10   All scores are back to 16.6 values except for king safety, as     *
2449
 *           these values played well.  King safety is quite a bit different   *
2450
 *           and now has two fine-tuning paramaters that can be set in the     *
2451
 *           crafty.rc file (try help ksafety for more details, or see the new *
2452
 *           crafty.doc for more details.  Suffice it to say that you can now  *
2453
 *           tune it to be aggressive or passive as you wish.                  *
2454
 *                                                                             *
2455
 *   16.11   Adjustment to king safety asymmetry to make Crafty a bit more     *
2456
 *           cautious about starting a king-side attack.  New "ksafe tropism"  *
2457
 *           command allows the tropism scores (pulling pieces toward opponent *
2458
 *           king) to be adjusted up or down (up is more aggressive).          *
2459
 *                                                                             *
2460
 *   16.12   evaluation adjustment options changed.  The new command to modify *
2461
 *           this stuff is "evaluation option value".  For example, to change  *
2462
 *           the asymmetry scoring for king safety, you now use "evaluation    *
2463
 *           asymmetry 120".  See "help evaluation" for more details.  There   *
2464
 *           is a new "bscale" option to adjust the scores for 'blocked' type  *
2465
 *           positions, and I added a new "blockers_list" so that players that *
2466
 *           continually try to lock things up to get easy draws can be better *
2467
 *           handled automatically.  The command is "list B +name".  There are *
2468
 *           other adjustable evaluation scaling parameters now (again, use    *
2469
 *           "help evaluation" to see the options.)                            *
2470
 *                                                                             *
2471
 *   16.13   Bug in Evaluate() fixed.  King safety could produce impossibly    *
2472
 *           large scores due to negative subscript values.  Minor change to   *
2473
 *           root.c to allow partial tablebase sets while avoiding the ugly    *
2474
 *           potential draw problem.  This was supplied by Tim Hoooebeek and   *
2475
 *           seems to work ok for those wanting to have files like kpk with-   *
2476
 *           out the corresponding promotion files.                            *
2477
 *                                                                             *
2478
 *   16.14   Bug in Evaluate() fixed.  King safety could produce impossibly    *
2479
 *           large scores due to a subscript two (2) larger than expected for  *
2480
 *           max values.  A couple of other minor tweaks to rook on 7th as     *
2481
 *           well.  Setboard now validates enpassant target square to be sure  *
2482
 *           that an enpassant capture is physically possible, and that the    *
2483
 *           target square is on the right rank with pawns in the right place. *
2484
 *                                                                             *
2485
 *   16.15   Bug in EGTBProbe() usage fixed.  It was possible to call this     *
2486
 *           function before tablebases were initialized, or when there were   *
2487
 *           more than 3 pieces on one side, which would break a subscript.    *
2488
 *                                                                             *
2489
 *   16.16   Changes to the way EvaluatePassedPawn() works.  It now does a     *
2490
 *           reasonable job of penalizing blockaded passed pawns.  Also a bug  *
2491
 *           in king scoring used a variable set from the previous call to the *
2492
 *           Evaluate() procedure, which could produce bizarre effects.  A few *
2493
 *           minor eval glitches that caused asymmetric scores unintentionally *
2494
 *           were removed.                                                     *
2495
 *                                                                             *
2496
 *   16.17   Minor "hole" in SMP code fixed.  It was possible that a move      *
108 pmbaty 2497
 *           displayed as the best in Output() would not get played.           *
33 pmbaty 2498
 *           Very subtle timing issue dealing with the search timing out and   *
2499
 *           forgetting to copy the best move back to the right data area.     *
2500
 *           Minor fixes to the modified -USE_ATTACK_FUNCTIONS option that     *
2501
 *           was broken when the And()/Or()/ShiftX() macros were replaced by   *
2502
 *           their direct bitwise operator replacements.                       *
2503
 *                                                                             *
2504
 *   16.18   Adjustments to king tropism scoring terms (these attract pieces   *
2505
 *           toward the opponent king in a coordinated way).  The scoring term *
2506
 *           that kept the queen from going to the opposite side of the board  *
2507
 *           was not working well which would also cause attacking problems.   *
2508
 *           Razoring code has been disabled, as it neither helps or hurts on  *
2509
 *           tactical tests, and it creates a few hashing problems that are    *
108 pmbaty 2510
 *           annoying.  Bug in StoreTransRefPV() that would store the PV into  *
2511
 *           the hash table, but possibly in the wrong table entry, which      *
2512
 *           could stuff a bad move in a hash entry, or overwrite a key entry  *
2513
 *           that could have been useful later.  Book random 0 was slightly    *
2514
 *           wrong as it could not ignore book moves if the search said that   *
2515
 *           the score was bad.  Also the search would not time-out properly   *
2516
 *           on a forced move, it would use the entire time alotted which was  *
2517
 *           a big waste of time when there was only one legal move to make.   *
2518
 *           A very interesting bug was found in storing mate bounds in the    *
2519
 *           hash table, one I had not heard of before.  Another change to     *
33 pmbaty 2520
 *           Iterate() to make it avoid exiting as soon as a mate is found, so *
2521
 *           that it can find the shortest mate possible.                      *
2522
 *                                                                             *
2523
 *   16.19   Savepos now pads each rank to 8 characters so that programs that  *
2524
 *           don't handle abbreviated FEN will be able to parse Crafty's FEN   *
2525
 *           output without problems.  Threads are no longer started and       *
2526
 *           stopped before after searches that seem to not need multiple      *
2527
 *           threads (such as when the starting position is in the databases   *
2528
 *           or whatever) because in fast games, it can start and stop them    *
2529
 *           after each search, due to puzzling for a ponder move, and this is *
2530
 *           simply to inefficient to deal with, and burning the extra cpus is *
2531
 *           only bad if the machine is used for other purposes.  And when the *
2532
 *           machine is not dedicated to chess only, the SMP search is very    *
2533
 *           poor anyway.  New lockless hashing (written by Tim Mann) added    *
2534
 *           to improve SMP performance.  New eval term that handles various   *
2535
 *           types of candidate passed pawns, most commonly the result of a    *
2536
 *           position where one side has an offside majority that later turns  *
2537
 *           into an outside passed pawn.  New EGTBPV() procedure added.  This *
2538
 *           code will print the entire PV of a tablebase mating line.  If it  *
2539
 *           given an option of ! (command is "egtb !") it will add a ! to any *
2540
 *           move where there is only one optimal choice.  Crafty will now     *
2541
 *           automatically display the EGTB PV if you give it a setboard FEN   *
2542
 *           command, or if you just give it a raw FEN string (the setboard    *
2543
 *           command is now optional except in test suites).  This version now *
2544
 *           supports the 6 piece database files.  To include this support,    *
2545
 *           you need to -DEGTB6 when compiling everything.  The majority code *
2546
 *           has been temporarily removed so that this version can be released *
2547
 *           to get the 6 piece ending stuff out.                              *
2548
 *                                                                             *
2549
 *   17.0    Connected passed pawn scoring modified so that connected passers  *
2550
 *           don't get any sort of bonus, except for those cases where the     *
2551
 *           opponent has a rook or more.  This will avoid liking positions    *
2552
 *           where Crafty has two connected passers on the d/e files, and the  *
2553
 *           opponent has passers on the b/g files.  The split passers win if  *
2554
 *           all pieces are traded, while the connected passers are better     *
2555
 *           when pieces are present.  A new scoring term evaluates the        *
2556
 *           "split" passers similar to outside passers, this at the request   *
2557
 *           (make that demand) of GM Roman Dzindzichashvili. Book() now finds *
2558
 *           the most popular move as the move to ponder.  This eliminated all *
2559
 *           'puzzling' searches which result in clearing the hash tables two  *
2560
 *           times for every book move, which can slow it down when a large    *
2561
 *           hash table is used.  New book option for playing against computer *
2562
 *           opponents was written for this version.  Crafty now supports two  *
2563
 *           "books.bin" type files, books.bin and bookc.bin (the bookc create *
2564
 *           command can be used to make bookc.bin).  Bookc will only be used  *
2565
 *           when Crafty is playing a computer.  This is supplied by xboard or *
2566
 *           winboard when playing on a server, or can be included int the     *
2567
 *           crafty.rc/.craftyrc file when playing directly.  Bookc.bin will   *
2568
 *           be used when playing a computer only, and should be used to avoid *
2569
 *           unsound lines that are fine against humans, but not against other *
2570
 *           computers.  If you don't use a bookc.bin, it will use the normal  *
2571
 *           books.bin as always.  If you use both, it will only use bookc.bin *
2572
 *           in games that have been started and then given the 'computer'     *
2573
 *           command.  Minor adjustment to EGTB probe code so that it will     *
2574
 *           always probe TBs at ply=2 (if the right number of pieces are on   *
2575
 *           the board) but won't probe beyond ply=2 unless the move at the    *
2576
 *           previous ply was a capture/promotion and the total pieces drops   *
2577
 *           into the proper range for TB probes.  This makes the 6 piece      *
2578
 *           files far more efficient as before it would continuously probe    *
2579
 *           after reaching 6 piece endings, even if it didn't have the right  *
2580
 *           file.  now after the capture that takes us to a 6 piece ending    *
2581
 *           we probe one time...  and if there is no hit we don't probe at    *
2582
 *           the next node (or any successors) unless there is another capture *
2583
 *           or promotion that might take us to a database we have.  The book  *
2584
 *           (binary) format has once again been modified, meaning that to use *
2585
 *           16.20 and beyond the book.bin/books.bin files must be re-built    *
2586
 *           from the raw PGN input.  This new format includes an entry for    *
2587
 *           the CAP project score, so that deep searches can be used to guide *
2588
 *           opening book lines.  A new weight (bookw CAP) controls how much   *
2589
 *           this affects book move ordering/selection.  A new import command  *
2590
 *           will take the raw CAPS data and merge it into the book.bin after  *
2591
 *           the file has been built.  Serious bug in SetBoard() would leave   *
2592
 *           old EP status set in test suites.  This has been fixed.  Outpost  *
2593
 *           knight code was modified so that a knight in a hole is good, a    *
2594
 *           knight in a hole supported by a pawn is better, supported by two  *
2595
 *           pawns is still better, and if the opponent has no knights or a    *
2596
 *           bishop that can attack this knight it is even better.  The out-   *
2597
 *           post bonus for a bishop was removed.  A pretty serious parallel   *
2598
 *           search bug surfaced.  In non-parallel searches, Crafty _always_   *
2599
 *           completed the current ply=1 move after the time limit is reached, *
2600
 *           just to be sure that this move isn't going to become a new best   *
2601
 *           move.  However, when parallel searching, this was broken, and at  *
2602
 *           the moment time runs out, the search would be stopped if the      *
2603
 *           parallel split was done at the root of the tree, which means that *
2604
 *           where the search would normally find a new best move, it would    *
2605
 *           not.  This has been fixed.  New "store <val>" command can be used *
2606
 *           to make "position learning" remember the current position and the *
2607
 *           score (val) you supply.  This is useful in analyze mode to move   *
2608
 *           into the game, then let Crafty know that the current position is  *
2609
 *           either lost or won (with a specific score).                       *
2610
 *                                                                             *
2611
 *   17.1    Book structure fixed, since compilers can't quite agree on how    *
2612
 *           structures ought to be aligned, for reasons not clear to me.  A   *
2613
 *           serious eval bug that could produce gross scores when one side    *
2614
 *           had two queens was fixed.                                         *
2615
 *                                                                             *
2616
 *   17.2    Isolated pawn scoring tweaked a bit, plus a couple of bugs in the *
2617
 *           way EvaluateDevelopment() was called were fixed.                  *
2618
 *                                                                             *
2619
 *   17.3    Passed pawn scores increased somewhat to improve endgame play.    *
2620
 *                                                                             *
2621
 *   17.4    Minor bug with "black" command (extra line from unknown origin)   *
2622
 *           would make "black" command fail to do anything.  Minor tweaks to  *
2623
 *           passed pawn scoring again...  And a slight performance improve-   *
2624
 *           ment in how EvaluateKingSafety() is called.  Code for "bk"        *
2625
 *           from xboard was somehow lost.  It now provides a book hint.       *
2626
 *                                                                             *
2627
 *   17.5    Rewrite of outside passed pawn/pawn majority code.  It is now     *
2628
 *           much faster by using pre-computed bitmaps to recognize the right  *
2629
 *           patterns.                                                         *
2630
 *                                                                             *
2631
 *   17.6    Minor fix in interrupt.c, which screwed up handling some commands *
2632
 *           while pondering.  Minor fix to score for weak back rank.  Score   *
2633
 *           was in units of 'defects' but should have been in units of        *
2634
 *           "centipawns".   Minor bug in "drawn.c" could mis-classify some    *
2635
 *           positions as drawn when they were not.                            *
2636
 *                                                                             *
2637
 *   17.7    Repair to DrawScore() logic to stop the occasional backward sign  *
2638
 *           that could create some funny-looking scores (-20 and +20 for      *
2639
 *           example).  Minor fix to majority code to recognize the advantage  *
2640
 *           of having a majority when the opponent has no passers or majority *
2641
 *           even if the candidate in the majority is not an 'outside' passer  *
2642
 *           candidate.  Minor change to passed pawns so that a protected      *
2643
 *           passed pawn is not considered a winning advantage if the          *
2644
 *           opponent has two or more passed pawns.  But in input_status       *
2645
 *           would cause an infinite loop if you reset a game to a position    *
2646
 *           that was in book, after searching a position that was not in      *
2647
 *           the book.  Bug in position learning fixed also.  This was caused  *
2648
 *           by the new hashing scheme Tim Mann introduced to avoid locking    *
2649
 *           the hash table.  I completed the changes he suggested, but forgot *
2650
 *           about how it might affect the position learning since it is also  *
2651
 *           hash-based.  Needless to say, I broke it quite nicely, thank you. *
2652
 *                                                                             *
2653
 *   17.8    This is the version used in the first ICC computer chess          *
2654
 *           tournament.  Crafty won with a score of 7.0/8.0 which included    *
2655
 *           only 2 draws and no losses.  Changes are minimal except for a few *
2656
 *           non-engine syntax changes to eliminate warnings and fix a bad bug *
2657
 *           in 'bench.c' that would crash if there was no books.bin file.     *
2658
 *                                                                             *
2659
 *   17.9    LearnPosition() called with wrong arguments from main() which     *
2660
 *           effectively disabled position learning.  This was broken in 17.7  *
2661
 *           but is now fixed.                                                 *
2662
 *                                                                             *
2663
 *   17.10   Minor change to "threat" extension now only extends if the null-  *
2664
 *           move search returns "mated in 1" and not "mated in N".  This      *
2665
 *           tends to shrink the trees a bit with no noticable effect in       *
2666
 *           tactical strength.  EvaluatePawns() was not doing a reasonable    *
2667
 *           job of recognizing blocked pawns and candidate passers.  It did   *
2668
 *           not do well in recognizing that the pawn supporting a candidate   *
2669
 *           could not advance far enough to help make a real passed pawn.     *
2670
 *           Minor change to Repeat() to not count two-fold repeats as draws   *
2671
 *           in the first two plies, which prevents some odd-looking repeats   *
2672
 *           at the expense of a little inefficiency.  Ugly repetition bug     *
2673
 *           fixed.  Rephead was off by one for whatever side Crafty was       *
2674
 *           playing which would screw up repetition detection in some cases.  *
2675
 *           Minor bug in main() that would report stalemate on some mates     *
2676
 *           when the ponder score was forgotten.                              *
2677
 *                                                                             *
2678
 *   17.11   Fail high/low logic changed to move the ++ output into Iterate()  *
2679
 *           where the -- output was already located.  (This was done for      *
2680
 *           consistecy only, it was not a problem.  New function to detect    *
2681
 *           draws RepetitionCheckBook() was added to count two-fold repeats   *
2682
 *           as draws while in book.  This lets Crafty search to see if it     *
2683
 *           wants tht draw or not, rather than just repeating blindly.  Minor *
2684
 *           bug in "go"/"move" command fixed.  The command did not work if    *
2685
 *           the engine was pondering.  Minor change to Evaluate() that makes  *
2686
 *           BAD_TRADE code more acurate.  Minor change to pawn majority code  *
2687
 *           to fix two bugs.  White pawns on g2/h2 with black pawn on h7 was  *
2688
 *           not seen as a majority (outside candidate passer) but moving the  *
2689
 *           black pawn to g7 made it work fine.   Also if both sides had an   *
2690
 *           equal number of pawns (black pawn at a7/b7, white pawns at b2/c2  *
2691
 *           the code would not notice black had an outside passer candidate   *
2692
 *           since pawns were 'equal' in number.                               *
2693
 *                                                                             *
108 pmbaty 2694
 *   17.12   Bookup() will now create a book with all sorts of games in it, so *
33 pmbaty 2695
 *           long as the various games have the FEN string to properly set the *
2696
 *           initial chess board position.                                     *
2697
 *                                                                             *
2698
 *   17.13   Endgame evaluation problem fixed.  King scoring now dynamically   *
2699
 *           chooses the right piece/square table depending on which side of   *
2700
 *           the board has pawns, rather than using root pre-processing which  *
2701
 *           could make some gross errors.  Glitch with majorities would make  *
2702
 *           an "inside majority candidate" offset a real outside passed pawn  *
2703
 *           even if the candidate would not be outside.  A suggestion by      *
2704
 *           Blass Uri (CCC) was added.  This adds .01 to tablebase scores if  *
2705
 *           the side on move is ahead in material, and -.01 if the side on    *
2706
 *           move is behind in material (only for drawn positions.)  the       *
2707
 *           intent is to favor positions where you are ahead in material      *
2708
 *           rather than even in material, which gives 'swindle mode' a chance *
2709
 *           to work.  Bug in EvaluateDraws() would mis-categorize some B+RP   *
2710
 *           (wrong bishop) as drawn, even if the losing king could not make   *
2711
 *           it to the corner square in time.                                  *
2712
 *                                                                             *
2713
 *   17.14   Another endgame evaluation problem fixed.  The outside passed     *
2714
 *           pawn code worked well, up until the point the pawn had to be      *
2715
 *           given up to decoy the other side's king away from the remainder   *
2716
 *           of the pawns.  Crafty now understands the king being closer to    *
2717
 *           the pawns than the enemy king, and therefore transitions from     *
2718
 *           outside passer to won king-pawn ending much cleaner.  New command *
2719
 *           "selective" as requested by S. Lim, which allows the user to      *
2720
 *           set the min/max null move R values (default=2/3).  They can be    *
2721
 *           set to 0 which disables null-move totally, or they can be set     *
2722
 *           larger than the default for testing.  Minor changes to init.c     *
2723
 *           sent by Eugene Nalimov to handle 64 bit pointer declarations for  *
2724
 *           win64 executable compilation.  NetBSD changes included along with *
2725
 *           a new Makefile that requires no editing to use for any known      *
2726
 *           configuration ("make help" will explain how to use it).  This was *
2727
 *           submitted by Johnny Lam.  Serious changes to the outside passed   *
2728
 *           pawn code.  The evaluator now understands that outside passers    *
2729
 *           on _both_ sides of the board is basically winning.  Same goes for *
2730
 *           candidate passers.                                                *
2731
 *                                                                             *
2732
 *   18.0    Bug in EvaluateDraws() could incorrectly decide that if one side  *
2733
 *           had only rook pawns in a king/pawn ending, the ending was a draw. *
2734
 *           Even if the other side had pawns all over the board.  Passed pawn *
2735
 *           scores increased a bit.  Minor change to "bad trade" code to turn *
2736
 *           off in endings.  S list removed.  The trojan horse check is now   *
2737
 *           automatically enabled when the key position is recognized at the  *
2738
 *           root of the tree.  It is disabled once the key position is no     *
108 pmbaty 2739
 *           longer seen at the root.  Bug in Bookup() would break if a FEN    *
33 pmbaty 2740
 *           string was included in a game used for input.  SetBoard() was     *
2741
 *           setting castling status incorrectly.  Internal iterative          *
2742
 *           deepening bug could (on very rare occasions) cause Crafty to try  *
2743
 *           to search an illegal move that would bomb the search.  This       *
2744
 *           version fully supports the new xboard protocol version 2.  One    *
2745
 *           other major change is that the eval is now consistently displayed *
2746
 *           as +=good for white, -=good for black.  In the log file.  In the  *
2747
 *           console output window.  And when whispering/kibitzing on a chess  *
2748
 *           server.  This should eliminate _all_ confusion about the values   *
2749
 *           that are displayed as there are now no exceptions to the above    *
2750
 *           policy of any kind.  Book() was changed so that if it notices     *
2751
 *           that the opponent has a "stonewall pattern" set up (IE Crafty     *
2752
 *           is black, has a pawn at e7 or e6, and the opponent has pawns at   *
2753
 *           d4/f4) then it won't play a castle move from book.  It will do a  *
2754
 *           normal search instead and let the evaluation guide it on whether  *
2755
 *           to castle or not (generally it won't unless it is forced).        *
2756
 *                                                                             *
2757
 *   18.1    Easy move code broken, so that no move ever appeared to be "easy" *
2758
 *           even if it was the only possible recapture move to make.  A minor *
2759
 *           problem in analysis mode dealing with pawn promotions was fixed.  *
2760
 *           It was possible for a move like h8=Q to be rejected even though   *
2761
 *           it was perfectly legal.                                           *
2762
 *                                                                             *
2763
 *   18.2    Minor bug in analyze mode would break the "h" command when black  *
2764
 *           was on move, and show one less move for either side that had      *
2765
 *           actually been played in the game.  Another bug also reversed the  *
2766
 *           sign of a score whispered in analysis mode.  Recapture extension  *
2767
 *           is disabled by default.  To turn it on, it is necessary to use    *
2768
 *           -DRECAPTURE when compiling search.c and option.c.  LearnBook()    *
2769
 *           has been modified to 'speed up' learning.  See the comments in    *
2770
 *           that module to see how it was changed.  LearnResult() has been    *
2771
 *           removed.  LearnBook() is now used to do the same thing, except    *
2772
 *           that a good (or bad) score is used.                               *
2773
 *                                                                             *
2774
 *   18.3    Minor bug in "avoid_null_move" test used R=2 for the test rather  *
2775
 *           than testing R=2/3 as the real null-move search uses.  The kibitz *
2776
 *           for "Hello from Crafty Vx.xx" has been moved so that it works     *
2777
 *           with the new xboard/winboard 4.2.2 versions.  Book learning was   *
2778
 *           badly broken in the previous version and has been fixed/tested.   *
2779
 *                                                                             *
108 pmbaty 2780
 *   18.4    Recapture extension was left in SearchParallel() by mistake.      *
2781
 *           This has now been protected by a #ifdef just like it was in       *
2782
 *           Search().  Bug in Repeat() was causing problems in SMP versions.  *
2783
 *           The entire repetition list code was modified to clean this up.    *
2784
 *           The problem was most noticable on things like fine #70.  Bug in   *
33 pmbaty 2785
 *           LearnImportBook() confused the learn value sign, due to the other *
108 pmbaty 2786
 *           changes to make +=white all the time.  Opposite bishop scoring    *
33 pmbaty 2787
 *           has been beefed up a bit to avoid these drawish endings.          *
2788
 *                                                                             *
2789
 *   18.5    Minor change to RootMove() to use Quiesce() rather than the more  *
2790
 *           complicated way it was ordering with Evaluate()/EnPrise().  This  *
2791
 *           is no faster, but it is simpler and eliminated the need for the   *
2792
 *           EnPrise() function totally, making the code a bit smaller.  Bug   *
2793
 *           in EvaluateDraws() would let it think that the bishop+wrong rook  *
2794
 *           pawn endings were winnable if both kings were very close to the   *
2795
 *           queening square, even with the wrong bishop.                      *
2796
 *                                                                             *
2797
 *   18.6    "New" no longer produces a new log.nnn/game.nnn file if no moves  *
2798
 *           have actually been played.  Minor change to rook scoring gives a  *
2799
 *           penalty when a rook has no horizontal (rank) mobility, to avoid   *
2800
 *           moves like Ra2 protecting the pawn on b2, etc.  Glitch in the     *
2801
 *           code that initializes is_outside[][] and is_outside_c[][] could   *
2802
 *           cause missed outside pawn cases to happen.  This has been there   *
2803
 *           a long time.                                                      *
2804
 *                                                                             *
2805
 *   18.7    BOOK_CLUSTER_SIZE increased to 2000 to handle making really large *
2806
 *           books.  A book made without this change could produce clusters    *
2807
 *           that would cause memory overwrites.                               *
2808
 *                                                                             *
2809
 *   18.8    Recapture extension turned back on for a while.  Changes to the   *
2810
 *           evaluation code, particularly EvaluatePawns() to make it more     *
2811
 *           efficient and accurate.  IE it was possible for an isolated pawn  *
2812
 *           to be penalized for being isolated, weak, and blocked, which made *
2813
 *           little sense.                                                     *
2814
 *                                                                             *
2815
 *   18.9    Book() modified to increase the responsiveness of book learning.  *
2816
 *           The new code, plus the default weights for the book parameters    *
2817
 *           now make Crafty learn very aggressively and repeat good opening   *
2818
 *           lines and avoid bad ones.                                         *
2819
 *                                                                             *
2820
 *   18.10   Minor bug in book.c would let Crafty play lines that were very    *
2821
 *           rarely played even though there were others that had been played  *
2822
 *           far more times and were more reliable.  King safety scores ramped *
2823
 *           up a bit and made more "responsive".                              *
2824
 *                                                                             *
2825
 *   18.11   Minor bug in EvaluateDevelopment() found by Bruce Moreland.  The  *
2826
 *           pawn ram code is now disabled when playing a computer, although   *
2827
 *           the normal 'blocked pawn' code is always active.  Bug in the code *
2828
 *           that penalizes a rook with no horizontal mobility was fixed.  If  *
2829
 *           the first rook scored had horizontal mobility, the second rook    *
2830
 *           appeared to have this mobility as well, which was wrong.  Pawn    *
2831
 *           hash statistics were wrong on longer searches due to an int       *
2832
 *           overflow on a multiply and divide calculation.  This has been     *
2833
 *           re-ordered to avoid the overflow.  For unknown reasons, epd       *
2834
 *           support was disabled.  It is now enabled as it should be.  Some   *
2835
 *           strange adjustements to root_alpha/root_beta were removed from    *
2836
 *           searchr.c.  They were probably left-over from some sort of test.  *
2837
 *                                                                             *
2838
 *   18.12   Bug in EvaluateDraws() fixed to not call KBB vs KN a draw if the  *
2839
 *           correct tablebase is not available.  Bishop pair scores now vary  *
2840
 *           depending on how many pawns are left on the board.  A pair is not *
2841
 *           worth a lot if there are 7-8 pawns left as most diagonals will be *
2842
 *           blocked by pawns.  Test() modified so that it will now run using  *
2843
 *           an EPD test suite with am/bm and id tags.  The old testfile for-  *
2844
 *           mat will continue to work for a while, but Test() now notices the *
2845
 *           EPD format and processes it correctly.  A new way of handling the *
2846
 *           search extensions is in place.  With the old approach, one ply    *
2847
 *           could not extend more than one full ply.  With the new approach,  *
2848
 *           borrowed from Deep Blue, two consecutive plies can not extend     *
2849
 *           more than two plies total.  It averages out to be the same, of    *
2850
 *           course, but the effect is a bit different.  Now it is possible    *
2851
 *           for a check and recapture to be applied at the same ply, where    *
2852
 *           they could not before (since a check was already a full one-ply   *
2853
 *           extension).  Whether this is better or not is not clear yet, but  *
2854
 *           it is worth careful analysis.  EvaluateDraws() has been replaced  *
2855
 *           by EvaluateWinner().  This function returns an indicator that     *
2856
 *           specifices whether white, black, both or neither can win in the   *
2857
 *           present position.                                                 *
2858
 *                                                                             *
2859
 *   18.13   Deep Blue extension limit removed and restored to one ply of      *
2860
 *           extension per ply of search.  Pruning in q-search fixed so that   *
2861
 *           a capture will be considered if it takes the total material on    *
2862
 *           the board down to a bishop or less for the opponent, as that can  *
2863
 *           greatly influence the evaluation with the EvaluateWinner() code.  *
2864
 *           As the 50 move rule draws near, the hash table scores are marked  *
2865
 *           as invalid every 5 moves so that hashing can't hide potential     *
2866
 *           50-move-rule draws.  Lazy evaluation code modified to be more     *
2867
 *           conservative, since in endgames the positional score can be a     *
2868
 *           large change.  EvaluatePassedPawnRaces() fixed so that if one     *
2869
 *           side has two pawns that are far enough apart, it will recognize   *
2870
 *           that even though the king is "in the square" of either, it can't  *
2871
 *           always stay in the square of one if it has to capture the other.  *
2872
 *           Pawn hash signature restored to 64 bits after testing proved that *
2873
 *           32 was producing an unacceptable number of collisions.  Search    *
2874
 *           node counter is now 64 bits as well to avoid overflows.  Bug in   *
2875
 *           the outside passed pawn code fixed (bad mask).                    *
2876
 *                                                                             *
2877
 *   18.14   Minor bug in ResignOrDraw() code caused Crafty to not offer draws *
2878
 *           although it would accept them when appropriate.  Rook vs Minor    *
2879
 *           is now evaluated as "neither side can win" an oversight in the    *
2880
 *           EvaluateWinner() code.  Minor bug in ResignOrDraw() would fail to *
2881
 *           offer draws due to the +0.01/-0.01 draw scores returned by the    *
2882
 *           EGTB probe code.                                                  *
2883
 *                                                                             *
2884
 *   18.15   Change in endgame draw recognition to handle the case where one   *
2885
 *           side appears to be in a lost ending but is stalemated.  The code  *
2886
 *           now evaluates such positions as "DrawScore()" instead.  The code  *
2887
 *           to accept/decline draws has been modified.  When a draw offer is  *
2888
 *           received, a global variable "draw_offer_pending" is set to 1.     *
2889
 *           When the search for a move for Crafty terminates, Crafty then     *
2890
 *           uses this value to decide whether to accept or decline the draw.  *
2891
 *           This means that the accept/decline won't happen until _after_ the *
2892
 *           search has a chance to see if something good is happening that    *
2893
 *           should cause the draw to be declined, closing a timing hole that  *
2894
 *           used to exist that let a few "suspects" get away with draws that  *
2895
 *           should not have happened (ie Crafty has - scores for a long time, *
2896
 *           the opponent suddenly fails low and sees he is losing and offers  *
2897
 *           a draw quickly.  Crafty would accept before doing a search and    *
2898
 *           noticing that it was suddenly winning.)  minor evaluation change  *
2899
 *           to notice that K+B+right RP vs K+B is not necessarily won if the  *
2900
 *           weaker side has a bishop of the right color.                      *
2901
 *                                                                             *
2902
 *   19.0    Significant change to the search extension limits.  First, the    *
2903
 *           limit of no more than 1 ply of extensions per ply has been tossed *
2904
 *           out.  Now, any number of extensions are allowed in the first N    *
2905
 *           plies, where N=iteration_depth of the current iteration.  After   *
2906
 *           the first N plies, extensions are reduced by 50% for the next N   *
2907
 *           plies.  They are then reduced by another 50% for the next N plies *
2908
 *           and then completely disabled after that.  IE for a 12 ply search, *
2909
 *           all extensions (even > one ply) are allowed for the first 12      *
2910
 *           plies, then the extensions are reduced by 1/2 for the next 12     *
2911
 *           plies, then by 1/4 for the next 12 plies, then turned off from    *
2912
 *           that point forward.  Minor tweak (suggested by GCP) to reduce the *
2913
 *           time limit by 1/4 for ponder=off games was done in time.c.  Fix   *
2914
 *           to EvaluateWinner() to correctly realize that KR[BN] vs KRR is a  *
2915
 *           draw for the KRR side if it doesn't have at least a pawn left.    *
2916
 *           Minor bug in RejectBookMove() would reject all moves if the op-   *
2917
 *           ponent had a stonewall-type pawn set-up.  It was supposed to only *
2918
 *           reject castling into the attack.  Minor change to code in the     *
2919
 *           EvaluateMaterial() function to lower the penalty for sacrificing  *
2920
 *           the exchange.  it was considered just as bad as trading a rook    *
2921
 *           for two pawns which was wrong.  Change to hashing code so that we *
2922
 *           can now determine that a hash entry came from the EGTB so that    *
2923
 *           PVs are displayed with <EGTB> when appropriate, not <EGTB> if it  *
2924
 *           originally came from the EGTB but later <HT> when it was picked   *
2925
 *           up from the hash table instead.  Minor changes to search/searchmp *
2926
 *           to make them identical in "look" where possible, for consistency  *
2927
 *           in source reading if nothing else.  If the first argument on the  *
2928
 *           command-line is "xboard" or if the environment variable           *
2929
 *           "CRAFTY_XBOARD" is set, Crafty sends a "feature done=0" command   *
2930
 *           to tell xboard it has not yet initialized things.  Significant    *
2931
 *           changes to EvaluateWinner() to recognize new cases of draws such  *
2932
 *           as Q+minor+nopawns vs Q+pawns as unwinnable by the Q+minor side.  *
2933
 *           Other cases were fixed/improved as well.                          *
2934
 *                                                                             *
2935
 *   19.1    Changes to the outside passed pawn and outside candidate pawn     *
2936
 *           code to more correctly recognize when one side has a simple end-  *
2937
 *           game position that is easy to win.  Futility pruning and razoring *
2938
 *           (Jeremiah Penery) was added.  To endable it, you will need to add *
2939
 *           -DFUTILITY to the Makefile options, otherwise it is disabled by   *
2940
 *           default.  EvaluateWinner() had a bug dealing with KR vs KN or     *
2941
 *           KRR vs KRN(B) that has been fixed.                                *
2942
 *                                                                             *
2943
 *   19.2    CCT-5 version 01/20/03.                                           *
2944
 *           Changes to the LimitExtensions() macro.  The extensions are now   *
2945
 *           a bit more aggressive, but after 2*iteration_depth, then they     *
2946
 *           taper off smoothly so that by the time the depth reaches          *
2947
 *           4*iteration_depth, the extensions are cut to zero.  Fixed bug in  *
2948
 *           EvaluatePawns() that missed candidate passed pawns so that the    *
2949
 *           new endgame stuff didn't work completely.  Change to hash.c to    *
2950
 *           combine the two hash tables into one so that the two probed       *
2951
 *           entries are adjacent in memory to be more cache friendly.  A bug  *
2952
 *           in moving a replaced entry from depth-preferred to always-store   *
2953
 *           caused the moved entry to go to the wrong address which would     *
2954
 *           make it impossible to match later.  New hash table layout is more *
2955
 *           cache-friendly by putting one entry from depth-preferred table    *
2956
 *           with two entries from always-store, so that they often end up in  *
2957
 *           the same cache-line.                                              *
2958
 *                                                                             *
2959
 *   19.3    Change to EvaluateMaterial to realize that a rook for five pawns  *
2960
 *           is also likely a "bad trade."  Adaptive hash table size code was  *
2961
 *           added so that the hash size is set automatically based on the     *
2962
 *           estimated NPS and time per move values for a specific "level"     *
108 pmbaty 2963
 *           command setting.  Repeat() rewritten.  The old code had an        *
33 pmbaty 2964
 *           unexplained bug that would overlook repetitions in a parallel     *
2965
 *           search in rare cases.  The old cold was complex enough that it    *
2966
 *           was time to rewrite it and simplify it significantly.             *
2967
 *                                                                             *
2968
 *   19.4    Initial depth "seed" value changed to iteration_depth + 1/2 ply,  *
2969
 *           so that fractional extensions kick in earlier rather than deeper  *
2970
 *           in the search.  This performs _much_ better in tactical tests     *
2971
 *           such as WAC and similar suites.  Minor book fix for a bug that    *
2972
 *           could cause a crash with book=off in xboard/winboard.             *
2973
 *                                                                             *
2974
 *   19.5    Default draw score set to 1, because endgame table draws can be   *
2975
 *           0, -.01 or +.01, which translates to -1, 0 or 1 in real scores    *
2976
 *           inside Crafty.  +1 means a draw where white has extra material    *
2977
 *           and that would break accepting draws with such a score.  A few    *
2978
 *           NUMA-related changes.  One global variable was made thread-       *
2979
 *           private to avoid cache thrashing.  Split blocks are now allocated *
2980
 *           by each individual processor to make them local to the specific   *
2981
 *           processor so that access is much faster.  CopyToSMP() now tries   *
2982
 *           to allocate a block for a thread based on the thread's ID so that *
2983
 *           the split block will be in that thread's local memory.  A few     *
2984
 *           other NUMA-related changes to help scaling on NUMA machines.      *
2985
 *                                                                             *
2986
 *   19.6    New egtb.cpp module that cuts decompression indices memory by 50% *
2987
 *           with no harmful side-effects.  Fixes to NUMA code to make SMP and *
2988
 *           non-SMP windows compiles go cleanly.                              *
2989
 *                                                                             *
2990
 *   19.7    Changes to draw code so that Crafty will first claim a draw and   *
2991
 *           then play the move, to avoid any confusion in whether the draw    *
2992
 *           was made according to FIDE rules or not.  Minor bug in evaluate() *
2993
 *           dealing with candidate passed pawns fixed.  A few additions to    *
108 pmbaty 2994
 *           support my AMD Opteron inline assembly for MSB(), LSB()           *
33 pmbaty 2995
 *           and PopCnt() procedures.                                          *
2996
 *                                                                             *
2997
 *   19.8    Changes to lock.h to (a) eliminate NT/alpha, since Microsoft no   *
2998
 *           longer supports the alpha, which makes lock.h much more readable. *
2999
 *           CLONE is no longer an option either, further simplyfying the .h   *
3000
 *           files.  New mode option "match" which sets an aggressive learning *
3001
 *           mode, which is now the default.  The old learning mode can be set *
3002
 *           by using the command "mode normal".  Memory leak in new windows   *
3003
 *           NUMA code fixed by Eugene Nalimov.  AMD fix to make the history   *
3004
 *           counts thread-local rather than global (to avoid excessive cache  *
3005
 *           invalidate traffic).                                              *
3006
 *                                                                             *
3007
 *   19.9    Two new pieces of code submitted by Alexander Wagner.  The first  *
3008
 *           adds speech to Crafty for unix users.  /opt/chess/sounds needs to *
3009
 *           exist and the common/sounds.tar.gz file needs to be untarred in   *
3010
 *           directory.  "Speech on*off" controls it.  A new annotatet command *
3011
 *           outputs the annotated file in LaTeX format for those that prefer  *
3012
 *           that platform.  Lots of source reformatting to make indenting and *
3013
 *           other things more consistent, using the unix "indent" utility.    *
3014
 *           Singular extension (simplistic approach) has been added and needs *
3015
 *           -DSINGULAR to activate it.  Once it has been compiled in, you can *
3016
 *           turn it on/off by setting the singular extension depth using the  *
3017
 *           ext command.  Default is on if you compile it in.  Ext/sing=0     *
3018
 *           disable it.  Changes to non-COMPACT_ATTACKS code to shrink the    *
3019
 *           array sizes.  This was done primarily for the Opteron where the   *
3020
 *           COMPACT_ATTACKS stuff was a bit slower than the direct look-up    *
3021
 *           approach.  This change reduced the table sizes by 75%.  A small   *
3022
 *           change to Evaluate() to better handle the 50-move draw problem.   *
3023
 *           What used to happen was when the counter hit 100 (100 plies since *
3024
 *           last capture or pawn push) the score just dumped to DrawScore()   *
3025
 *           instantly.  It was possible that by the time the draw was seen,   *
3026
 *           it was too late to do anything to avoid it without wrecking the   *
3027
 *           position.  what happens now is that once the 50-move counter      *
3028
 *           reaches 80 plies (40 moves by both sides with no pawn push or     *
3029
 *           capture) the score starts dropping toward DrawScore() no matter   *
3030
 *           which side is winning.  Dave Slate called this a "weariness       *
3031
 *           factor" in that after playing 40 moves with no progress, the      *
3032
 *           evaluation shows that the program is getting "weary".  The idea   *
3033
 *           is that the score starts to slip over the last 10 moves by each   *
3034
 *           side to that if either can do something to reset the counter,     *
3035
 *           they will do it sooner rather than later.                         *
3036
 *                                                                             *
3037
 *   19.10   A fix to the EvaluateWinner() code to recognize that while KRB vs *
3038
 *           KR can not normally be won by the KRB side, we require that the   *
3039
 *           KR side not be trapped on the edge of the board, where it can be  *
3040
 *           mated.  FUTILITY code put back in.  19.9 will be the SE version   *
3041
 *           for those wanting to play with it.  New "learn" command option to *
3042
 *           allow the user to set the position learning parameters that are   *
3043
 *           used to trigger position learning and disable position learning   *
3044
 *           after the game is already lost.  The command syntax is as follows *
3045
 *           "learn trigger cutoff" and are expressed as fractions of a pawn   *
3046
 *           with a decimel point.  The default is learn .33 -2.0 which says   *
3047
 *           to do position learning when the score drops 1/3 of a pawn, but   *
3048
 *           turn it off after the score reaches -2.00 as the game is already  *
3049
 *           lost and learning won't help at this point.  This is the CCT-6    *
3050
 *           version exactly as played in the CCT-6 tournament.                *
3051
 *                                                                             *
3052
 *   19.11   A fix to the Annotate() code that could, in certain cases, fail   *
3053
 *           to display N moves when asked.  This happened when there were     *
3054
 *           fewer than N moves total, and on occasion it would not display    *
3055
 *           all the moves that were possible, omitting the last one.  Fix to  *
3056
 *           egtb.cpp to correctly set 16 bit index mode for a few of the      *
3057
 *           recent 6-piece tables.                                            *
3058
 *                                                                             *
3059
 *   19.12   Fix to allocating split blocks that did not let all blocks get    *
3060
 *           used, which would produce slow-downs on large parallel boxes      *
3061
 *           (cpus > 8-16).  Change to fail-high/fail-low window adjustment.   *
3062
 *           I now use the old Cray Blitz approach ("Using time wisely -       *
3063
 *           revisited", JICCA) where a fail-high (or low) now adjusts the     *
3064
 *           root alpha or beta bound by 1 pawn on the first fail, another two *
3065
 *           pawns on the second failure, and finally relaxes the bound to     *
3066
 *           +/- infinity on the third fail for the same move.  This speeds up *
3067
 *           searching many fail-high/fail-low conditions, although it will    *
3068
 *           occasionally cause the search to take a bit longer.               *
3069
 *                                                                             *
3070
 *   19.13   Fix to evaluating split passed pawns.  Code is cleaner and is not *
3071
 *           applied if the opposing side has any pieces at all since a piece  *
3072
 *           and the king can stop both.  New egtb.cpp that opens the tables   *
3073
 *           approximately 20-25% faster is included in this version (code by  *
3074
 *           Eugene Nalimov of course).                                        *
3075
 *                                                                             *
3076
 *   19.14   New "eval" command (eval list and eval help to start) allow a     *
3077
 *           user to modify all internal evaluation values via commands that   *
3078
 *           may be entered directly or via the .craftyrc/crafty.rc init file. *
3079
 *           This was mainly done for the automated evaluation tuning project  *
3080
 *           I am working on with Anthony Cozzie, but it will be useful for    *
3081
 *           merging Mike Byrne's "personality" stuff as well.  New command    *
3082
 *           "personality load/save <filename>" which saves eval and search    *
3083
 *           parameters to create new personalities that can be loaded at any  *
3084
 *           time.  "Crafty.cpf" is the "default" personality file that is     *
3085
 *           used at start-up if it exists.  Evaluation tuning changes based   *
3086
 *           on some results from the new annealing code, although all of the  *
3087
 *           annealed values are not yet fully understood nor utilized yet.    *
3088
 *           LearnBook() is now used to learn good and bad game results.  IE   *
3089
 *           if Crafty wins with a particular opening, it will remember it as  *
3090
 *           good and try it again, in addition to the previous method that    *
3091
 *           would remember bad openings and avoid them.  It also now handles  *
3092
 *           draws as well which can make slightly bad openings appear better. *
3093
 *           Minor change to SMP code to eliminate the possibility of a shared *
3094
 *           variable getting overwritten unintentionally.  Several changes to *
3095
 *           evaluation weights and evaluation code as testing for the WCCC    *
3096
 *           continues.  New "help" command uses a file "crafty.hlp" which now *
3097
 *           contains all the help information.  This makes the help info      *
3098
 *           easier to maintain and gets rid of a bunch of printf()s in the    *
3099
 *           Option() source (option.c).  Rewrite of the "list" code.  Crafty  *
3100
 *           now supports six lists, AK (auto-kibitz), B (blocker), C (comp),  *
3101
 *           GM, IM and SP (special player).  The SP list has two extra items  *
3102
 *           that can be specified to go with a player's name, namely a book   *
3103
 *           filename (to replace the normal books.bin, but not book.bin) and  *
3104
 *           a personality file that will be loaded each time this particular  *
3105
 *           named opponent plays.  The format is available in the new help    *
3106
 *           facility.  Bug fix for outside passed pawn code that would cause  *
3107
 *           an outside passer to appear to be a winning advantage even if the *
3108
 *           opponent has a protected passer, which easily negates the outside *
3109
 *           passer's threat.                                                  *
3110
 *                                                                             *
3111
 *   19.15   Fix to outside passed pawn code that requires pawns on both sides *
3112
 *           of the board for the side with an "outside passer" or "outside    *
3113
 *           candidate" to avoid some bizarre evaluations. Sel 0/0 now works   *
3114
 *           without crashing Crafty.  This would fail in previous versions as *
3115
 *           the hash signature would be modified but not restored.  Slightly  *
3116
 *           more conservative limit on using null-move search to head off a   *
3117
 *           few notable zugzwang problems was added.  Fix to time control     *
3118
 *           code to remove a hole that could cause a divide-by-zero at a time *
3119
 *           control boundary.  Stonewall detection removed completely as it   *
3120
 *           appears to be no longer needed.  Rook scoring changed to better   *
3121
 *           evaluate "open files" by measuring mobility on them.  Complete    *
3122
 *           removal of Phase() (phase.c) and references to the opening,       *
3123
 *           middlegame and endgame phases as they were no longer referenced   *
3124
 *           anywhere in the code.                                             *
3125
 *                                                                             *
3126
 *   19.16   Fix to "Trojan code" to eliminate the time limit exclusion since  *
3127
 *           many users still have old and slow hardware, and the time limit   *
3128
 *           was not set correctly when PreEvaluate() was called anyway.  The  *
3129
 *           code to display fail-high/fail-low information was cleaned up so  *
3130
 *           that the +1 or +3 now makes sense from the black side where the   *
3131
 *           score is really going down (good for black) rather than showing   *
3132
 *           a +3 fail high (when Crafty is black) and the score is really     *
3133
 *           going to drop (get better for black).  Now the fail-high-fail-low *
3134
 *           +/- sign is also relative to +=good for white like the scores     *
3135
 *           have been for years.  Adjustments to pawn evaluation terms to     *
3136
 *           improve the scoring balance.  "New" now terminates parallel       *
3137
 *           threads (they will be re-started when needed) so that we don't    *
3138
 *           burn CPU time when not actually playing a game.                   *
3139
 *                                                                             *
3140
 *   19.17   Changes to pawn evaluation to limit positional scores that could  *
3141
 *           get a bit out of sane boundaries in some positions.               *
3142
 *                                                                             *
108 pmbaty 3143
 *   19.18   ProbeTransRef() no longer adjusts alpha/beta bounds if the entry  *
3144
 *           is not good enough to terminate the search here.  This has helped *
33 pmbaty 3145
 *           speed things up (reduced size of tree) over many test positions   *
3146
 *           so either it was buggy or not worthwhile.  Regardless, it is now  *
3147
 *           'gone'.  Connected passed pawns now scored as a simple pair of    *
3148
 *           pawns that are better as they are advanced, the old connected     *
3149
 *           passed pawns on the 6th rank special code has been removed.       *
3150
 *                                                                             *
3151
 *   19.19   Repeat3x() had a bug that would cause it to miss a draw claim on  *
3152
 *           the 50th move, often making a strange (losing) move that would    *
3153
 *           not lose if the draw is was claimed, but which would cause a loss *
3154
 *           if the draw was not claimed because the piece might be instantly  *
3155
 *           captured if the opponent can play a move.                         *
3156
 *                                                                             *
3157
 *   19.20   Bug in the EvaluateMaterial() (bad trade) code that would not     *
3158
 *           penalize a single piece vs 3 pawns properly.  Now the penalty is  *
3159
 *           added in unless the side with the piece has nothing else to go    *
3160
 *           with it at all (no pawns or other pieces).  Pawn scoring changed, *
3161
 *           doubled pawns were scored too badly because the penalties were    *
3162
 *           added twice (as expected) but they were set as if they were just  *
3163
 *           added once.  Eval command also had a bug in displaying the pawn   *
3164
 *           evaluation parameters.  Move input changed to accept pawn         *
3165
 *           promotions of the form a8Q (this is needed as ChessBase violates  *
3166
 *           the PGN/SAN standard that mandates a8=Q as the proper syntax for  *
3167
 *           pawn promotions.)  annoying glitch in epdglue.c that would        *
3168
 *           produce the wrong score for positions with exactly one legal move *
3169
 *           was fixed.  New InvalidPosition() function verifies that FEN      *
3170
 *           setup positions are reasonable, without extra pawns or too many   *
3171
 *           pieces total, etc.  Passed pawn to 7th search extension removed,  *
3172
 *           mate threat extension tuned down to 1/2 ply.  Bug in SetBoard()   *
3173
 *           fixed.  This bug made it impossible to import many epd records    *
3174
 *           due to incorrectly handling the wtm flag.  This was introduced in *
3175
 *           the recent changes to disallow certain types of illegal positions *
3176
 *           that were impossible (more than 9 queens of one color, etc.) new  *
3177
 *           eval term for space added.  This simply counts the number of      *
3178
 *           squares on each rank attacked by pawns, and then multiplies by a  *
3179
 *           constant that scales the attacks so that attacking ranks on the   *
3180
 *           opponent's side of the board is more valuable than attacking      *
3181
 *           squares on our side of the board and vice-versa.  Significant     *
3182
 *           coding changes for the unix parallel search.  POSIX threads are   *
3183
 *           gone, due to too many differences between vendor's implementation *
3184
 *           details.  Crafty now uses SYSV shared memory to share the search  *
3185
 *           data (this is exactly the same parallel search approach always    *
3186
 *           used, but with a completely different implementation) and fork()  *
3187
 *           to spawn processes for each CPU.  It simply became too much work  *
3188
 *           to figure out what each vendor was doing about CPU time, how      *
3189
 *           threads were scheduled, plus debug thread library implementations *
3190
 *           that were often buggy.  There are no performance benefits to this *
3191
 *           approach other than falling back to features found in all Unix    *
3192
 *           implementations.  New NUMA changes to make sure that the local    *
3193
 *           thread "blocks" are actually allocated on the NUMA node where     *
3194
 *           the process actually runs, rather than on a remote node that      *
3195
 *           will cause extra memory latency for often-used memory data.  Bug  *
3196
 *           in EvaluatePawns() caused the outside passer / outside            *
3197
 *           candidate bonus to be triggered when the opponent has a simple    *
3198
 *           protected passed pawn.  Since the decoy value is nil with a       *
3199
 *           protected passer (we can't take the supporing pawn or the passer  *
3200
 *           will run) the outside candidate or passer doesn't help.  Bug in   *
3201
 *           king safety caused pieces to be attracted to the opponent's king, *
3202
 *           even in simple endgames, inflating/deflating the score for a      *
3203
 *           feature that was pointless.  (2005 WCCC final version).           *
3204
 *                                                                             *
3205
 *   20.0    First in a new series.  First change is to produce an endian-     *
3206
 *           independent opening book.  The format has not changed so that old *
3207
 *           book.bin/books.bin files will work fine, but they may now be      *
3208
 *           freely between big-endian and little-endian architectures.  This  *
3209
 *           makes the binary format compatible between a PC and a Sun or HP   *
3210
 *           box, for example, so that just one book.bin file is needed.       *
3211
 *                                                                             *
3212
 *   20.1    First step toward a complete redesign of the evaluation code in   *
3213
 *           Evaluate() (evaluate.c).  This first set of changes are mainly    *
3214
 *           restructuring related, an attempt to collect the various bits of  *
3215
 *           evaluation code that are related to a common theme (i.e. All      *
3216
 *           bishop scoring, all passed pawn scoring, etc) are not contained   *
3217
 *           in one routine, with a sensible name.  This makes it much easier  *
3218
 *           to find evaluation code and modify it, without having bits and    *
3219
 *           pieces scattered all over the place.  Perhaps the most signifi-   *
3220
 *           cant is that the current evaluation has dropped the asymmetric    *
3221
 *           king safety, and later the asymmetric blocked pawn code will go   *
3222
 *           as well.  To do this, I did have to make some changes in terms    *
3223
 *           of the "tropism" code.  The basic idea is that white's "tropism"  *
3224
 *           bonus in incremented for each white piece that attacks the black  *
3225
 *           king, but it is also decrement for each black piece that attacks  *
3226
 *           the black king, so that a defender offsets an attacker.  This has *
3227
 *           certainly changed the "personality" of how it plays.  It is now   *
3228
 *           far more aggressive, and further changes planned for the future   *
3229
 *           will hopefully improve on this change.  It is playing quite a bit *
3230
 *           better, but whether this aggressiveness is "over the top" remains *
3231
 *           to be seen.  Fix to "won kp ending" logic.  If one king is closer *
3232
 *           to the opponent's "average pawn square" than the other, by at     *
3233
 *           least 2 moves, then that side is credited as having a won k+p     *
3234
 *           ending. New code for time allocation testing added.  A new        *
3235
 *           "timebook <factor> <moves>" command that says use "factor" extra  *
3236
 *           time (factor is expressed as a percent, 100 means use 100% extra  *
3237
 *           time) tapered off for "moves" out of book.  For example,          *
3238
 *           "timebook 100 10" says use 100% extra time on first non-book move *
3239
 *           followed by 90% extra on the next move, 80% on the next, until    *
3240
 *           after 10 moves we are back to the normal time average.            *
3241
 *                                                                             *
3242
 *   20.2    Significant evaluation changes to try to limit positional scores  *
3243
 *           so that new tuning can be accomplished.  In particular, the king  *
3244
 *           safety code has been greatly simplified, now having two separate  *
3245
 *           components, one for pawn structure, one for piece tropism.  A new *
3246
 *           SearchControl() procedure now handles all the search extensions   *
3247
 *           in one centralized place, shared by normal and parallel search    *
3248
 *           code.  The plan is that eventually this piece of code will also   *
3249
 *           be able to recommend search reductions as well as extensions to   *
3250
 *           speed the tree traversal process significantly with a form of     *
3251
 *           of forward pruning that is commonly called "search reductions".   *
3252
 *                                                                             *
3253
 *   20.3    Search reduction code added.  Part of this dates back to 1997     *
3254
 *           when Bruce Moreland and I played with the idea of reducing the    *
3255
 *           depth on the last part of the move list when it seemed to become  *
3256
 *           fairly certain that no fail-high was going to happen.  That was a *
3257
 *           big search speed improvement, but also risky.  Recently, several  *
3258
 *           started to do this again, except they added one fairly useful     *
3259
 *           constraint, that the "history value" for a specific move has to   *
3260
 *           be below some threshold before a reduction can happen.  The idea  *
3261
 *           is that we don't want to reduce the depth of moves that have      *
3262
 *           failed high in the past, because they might fail high again if    *
3263
 *           they are not reduced too much.  Tord Romstad suggested an         *
3264
 *           additional improvement, namely that if a reduced move does fail   *
3265
 *           high, re-search with the proper (non-reduced) depth to verify     *
3266
 *           that it will fail high there as well.  All of this is included    *
3267
 *           in version 20.3, but the values (reduction amount, history        *
3268
 *           threshold, min depth to do a reduction) need serious testing and  *
3269
 *           tuning.  The current values are simply "legal" but hardly could   *
3270
 *           be considered optimal without substantial testing.  New code to   *
3271
 *           "age" history counters added.  After each iteration, this code    *
3272
 *           divides each counter by 2, to scale the values back, so that      *
3273
 *           moves that are no longer causing fail highs will have a history   *
3274
 *           value that won't prevent reduction pruning.  The "minimum depth"  *
3275
 *           value indicates how much search to leave after doing a reduction  *
3276
 *           in the tree.  A value of 1 ply guarantees that after a reduction  *
3277
 *           is done, there will always be one full-width ply of search done   *
3278
 *           after such reductions to avoid gross tactical oversights.  Tuning *
3279
 *           to extensions to reduce the tree size.  The recapture extension   *
3280
 *           has been completely removed, while the check extension has been   *
3281
 *           left at 1.0 as in the past.  The mate threat and one-legal-reply  *
3282
 *           extensions are now both set to .75 after extensive testing.  This *
3283
 *           is the CCT8 version of Crafty.                                    *
3284
 *                                                                             *
3285
 *   20.4    History counters changed.  There is now one large history[8192]   *
3286
 *           array to simplify indexing.  More importantly, these values are   *
3287
 *           now limited to a max value of 32000.  During the update process,  *
3288
 *           if any value reaches that limit, all values are divided by 2 to   *
3289
 *           prevent deep searches from producing very large history values    *
3290
 *           which would essentially disable late move reductions based on the *
3291
 *           big history values.  The reduce/history=n command now uses values *
3292
 *           0 <= n <= 100.  A value of 100 will cause all non-tactical moves  *
3293
 *           to be reduced (most aggressive) while a value of 0 will result in *
3294
 *           no moves being reduced (most conservative).  The value, loosely   *
3295
 *           interpreted, means "reduce if a move fails high < n% of the time  *
3296
 *           when at least one move fails high in a search."  the default is   *
3297
 *           currently 50%, but further testing is needed.                     *
3298
 *                                                                             *
3299
 *   20.5    Old "mask" code removed and replaced by constants, since this is  *
3300
 *           never intended to run on a Cray specifically any longer.  Change  *
3301
 *           to "won king and pawn ending" to make it more accurate, this code *
3302
 *           is used when there are nothing but pawns left, all on one side of *
3303
 *           board, and one king is closer to a capturable pawn than the other *
3304
 *           king is.  This is intended to help in outside passed pawn games   *
3305
 *           where one side has to eventually give up the outside passer to    *
3306
 *           decoy the opposing king away from the rest of the pawns.  New     *
3307
 *           king safety now factors in king pawn shelter and opponent piece   *
3308
 *           tropism into one score, but the score is now an arithmetic        *
3309
 *           value that goes up more quickly as both indices increase, to do a *
3310
 *           more accurate evaluation of safety and attacking chances.  Change *
3311
 *           to bishop mobility, which now goes in units of 2 rather than 1,   *
3312
 *           to increase the value of mobility somewhat.  Also mobility is now *
3313
 *           'centered' in that it is computed as (squares - 6) * 2, to keep   *
3314
 *           the bishop value from going way over its normal range with these  *
3315
 *           new and somewhat larger numbers.                                  *
3316
 *                                                                             *
3317
 *   20.6    New eval code to look for bishops that are blocked in the forward *
3318
 *           direction by friendly pawns.  That basically turns a bishop into  *
3319
 *           a "tall pawn" since movement in the critical direction is not     *
3320
 *           possible.                                                         *
3321
 *                                                                             *
3322
 *   20.7    Manually tuned kinf_safety[][] array values.  Modified the new    *
3323
 *           bishop code to only consider the "long diagonal" if the bishop    *
3324
 *           is in either 9-square corner on its own side of the board, so     *
3325
 *           that the short diagonal being open won't offset the long          *
3326
 *           diagonal that is blocked.  Bishop-pair bonus is now adjusted by   *
3327
 *           how open or closed the center is.  If 3 of the four center pawns  *
3328
 *           are gone, the center is open and the bishop pair bonus is set to  *
3329
 *           the max.  If the four center pawns are locked (e4/e5/d5/d6 for    *
3330
 *           example) then the bishop pair bonus is eliminated.  if the center *
3331
 *           is somewhere in the middle of those extremes, then the bishop     *
3332
 *           pair bonus is present but reduced.  Castling now is a bit more    *
3333
 *           intelligent.  If a side has not castled, then moving either rook  *
3334
 *           receives a penalty, where moving the king or the last unmoved     *
3335
 *           rook receives a larger penalty.  If the king has not castled, its *
3336
 *           'safety' score is based on the most safe position of the possible *
3337
 *           options.  If neither rook has moved, then the score is based on   *
3338
 *           the safest of the three locations, either in the center where it  *
3339
 *           is, or on the safest wing.  If uncastled, this safaty is always   *
3340
 *           reduced a bit to encourage castling before it becomes too late to *
3341
 *           do so.  This was done to avoid a problem where the queen rook had *
3342
 *           been moved, Crafty had not castled, and it pushed the kingside    *
3343
 *           pawns making it unsafe to castle there, leaving the center as the *
3344
 *           best place, yet had it not pushed the kingside pawns, castling    *
3345
 *           would have been a better alternative.   Two new 16 word vectors,  *
3346
 *           safety_vector[]/tropism_vectorp[] are used to set the edge values *
3347
 *           for the king_safetyp[][] array.  Interior values are filled in    *
3348
 *           based on these two vectors, making it much easier to tweak the    *
3349
 *           king safety scores without pulling your hair out.                 *
3350
 *                                                                             *
3351
 *   20.8    When queens come off, the previous version cut the tropism scores *
3352
 *           and pawn shelter scores by 1/2 each, which had the effect of      *
3353
 *           dropping the overall score by 75% (the score is a geometric       *
3354
 *           computation).  The safety/tropism values are now dropped, but by  *
3355
 *           a smaller amount, since bishops + rooks + knights are still a     *
3356
 *           dangerous threat.                                                 *
3357
 *                                                                             *
3358
 *   20.9    Simple lazy eval (Mike Byrne) added.  This simply avoids the more *
3359
 *           expensive piece evaluations if the pawn scoring and material      *
3360
 *           leave the score well outside the alpha/beta search window.  Looks *
3361
 *           like a 20% speed improvement in general.                          *
3362
 *                                                                             *
3363
 *   20.10   Change to king safety pawn structure.  Open files around the king *
3364
 *           are dangerous, but the h/g (or b/a) files are more dangerous than *
3365
 *           the f-e-d-c files since they are much harder to defend against    *
3366
 *           with the castled king in the way.  Modified the way the king      *
3367
 *           safety matrix is computed so that at the tropism/pawnstructure    *
3368
 *           values are more sane.  It currently will get the same value for   *
3369
 *           array indices of 5,0, 4,1, 3,2, 2,3, 1,4, or 0,5, which is better *
3370
 *           than what it was doing.  I plan to tweak this more later to make  *
3371
 *           3,2 or 2,3 worse than 4,1 or 1,4 and 0,5 or 5,0, as the latter    *
3372
 *           mean "unsafe king but no piece pressure" or "piece pressure but   *
3373
 *           king pawn shelter is safe" whereas the middle values mean both    *
108 pmbaty 3374
 *           components of an attack are present, unsafe king position and the *
33 pmbaty 3375
 *           pieces are closing in.                                            *
3376
 *                                                                             *
3377
 *   20.11   Minor fix to late move reduction code.  Turns out it is possible  *
3378
 *           to reach the block of code if (first_move) { } with status set to *
3379
 *           REMAINING_MOVES (this means no captures, no killer, no hash move, *
3380
 *           no history move, etc).  That would let me try a reduction, and if *
3381
 *           it failed high, we would use that score as-is.  We now re-search  *
3382
 *           a fail-high here just like we did in the other block of code so   *
3383
 *           that a fail-high on a reduction search is never trusted.          *
3384
 *                                                                             *
3385
 *   20.12   Further modifications to king safety initialization code to try   *
3386
 *           scale back the values a bit to control score limits.              *
3387
 *                                                                             *
3388
 *   20.13   Fix to bench.c to remove two file closes that would wreck the     *
3389
 *           program after the bench command was finished.  Also the           *
3390
 *           SMP-time-to-ply measurement was removed since it had no meaning.  *
3391
 *           Old FUTILITY code re-inserted along with an extension limit       *
3392
 *           modification by Mike Byrne to avoid limiting the search extension *
3393
 *           on one-reply extensions only.  New lock.h submitted by Andreas    *
3394
 *           Guettinger to support spinlocks on the PPC architecture (Linux).  *
3395
 *           Some -D configuration options were changed so that none are       *
3396
 *           needed to compile the "standard" Crafty.  Futility is normally on *
3397
 *           now, and -DNOFUTILITY is required to compile it out.  The new     *
3398
 *           Makefile gives the configuration options, but the only ones that  *
3399
 *           would normally need setting are the SMP and CPUS= options if you  *
3400
 *           have a SMP platform.                                              *
3401
 *                                                                             *
3402
 *   20.14   Fix to EvaluateWinningChances() to correctly evaluate KRP vs KR   *
3403
 *           type endings.  Previously this was done in the wrong place and    *
3404
 *           the score would favor the side with the pawn significantly, where *
3405
 *           the score is now zero.  Minor bugfix to code that detects that an *
3406
 *           illegal position has been set up.  It caught every possible       *
3407
 *           problem except for positions with a king (or kings) missing.      *
3408
 *                                                                             *
3409
 *   20.15   Change to how Crafty chooses where to split.  Previously, there   *
3410
 *           was a min remaining depth limit to prevent Crafty from trying to  *
3411
 *           split too close to the frontier nodes.  Now that is specified as  *
3412
 *           a percentage of the nominal search depth.  For example, the       *
3413
 *           default value is 50%, which says that on a 16 ply search, we can  *
3414
 *           split if at least 8 plies of search are left.  This provides      *
3415
 *           better split overhead than a simple limit that doesn't take the   *
3416
 *           actual search depth into consideration.  History data arranged to *
3417
 *           be more cache friendly.  The three values for a common history    *
3418
 *           index value are now stored in adjacent memory words to take       *
3419
 *           advantage of pre-fetching within a cache line.  Additional        *
3420
 *           tuning of the king safety matrix.                                 *
3421
 *                                                                             *
3422
 *   20.16   Evaluation of knights, bishops, rooks, queens and kings is now    *
3423
 *           done on a 1000 point scale, then the sum is divided by 10.  This  *
3424
 *           allows us to use the same hashing mechanism, but with finer       *
3425
 *           tuning precision.  Minor queen eval tweak for 7th rank position.  *
3426
 *                                                                             *
3427
 *   20.17   New book learning, with a simplified book system.  The export     *
3428
 *           files (book.lrn, position.lrn) no longer are created/used, the    *
3429
 *           learn mechanism no longer touches the real chess board which      *
3430
 *           could cause problems in certain xboard/winboard timing            *
3431
 *           situations.  One major learning change is that we now only learn  *
3432
 *           things from "Crafty's perspective".  That is, if Crafty loses a   *
3433
 *           game as black, it won't think that opening is good when it plays  *
3434
 *           white since it might well not understand how to win using that    *
3435
 *           particular opening.  It will only "learn" on the actual moves it  *
3436
 *           plays in a game.  Old "history heuristic" removed.  Testing       *
3437
 *           showed that the LMR idea produced better results without the      *
3438
 *           history ordering, since now more moves are reduced, but moves     *
3439
 *           with good history values don't get reduced.                       *
3440
 *                                                                             *
3441
 *   21.0    Finally did the bit-renumbering task so that bit 0 = LSB = A1,    *
3442
 *           to eliminate the 63-n translation to the numbering scheme used    *
3443
 *           on the Cray architecture.  For all bit operations now, LSB=0,     *
3444
 *           MSB=7, 15, 31 or 63 depending on the data size.  A few minor      *
3445
 *           bugs were fixed along the way as these changes were debugged.     *
3446
 *           Minor bug in EvaluateKings() where it tested for a "won ending"   *
3447
 *           and used a test that was too aggressive.  This test did not get   *
3448
 *           things quite right when the two kings were very close together    *
3449
 *           and "opposition" prevented one king from moving closer to the     *
3450
 *           pawns.                                                            *
3451
 *                                                                             *
3452
 *   21.1    New piece scoring from Tracy, which changes the way pieces are    *
3453
 *           scored based on squares they attack.                              *
3454
 *                                                                             *
3455
 *   21.2    The scores are now scaled better for the transition from middle-  *
3456
 *           game to end-game.  Passed pawn scores climb as material is        *
3457
 *           removed from the board, to prevent them from overwhelming the     *
3458
 *           middlegame king safety scores.  We do exactly the same scaling on *
3459
 *           king-safety scores, but they are scaled downward as material is   *
3460
 *           removed so that they "phase out" as the endgame is reached.       *
3461
 *           Weak pawn code changed to more accurately identify weak pawns.    *
3462
 *           These are totaled and then used to index a scoring array that     *
3463
 *           penalizes the number of weak pawns on each side.  Pawn islands    *
3464
 *           are now recognized and each side is penalized according to the    *
3465
 *           number of islands he has, more being worse.                       *
3466
 *                                                                             *
3467
 *   21.3    "Magic" move generation is now used, which eliminates the rotated *
3468
 *           bitboard approach completely.  Not a significant speed change,    *
3469
 *           but it is far simpler overall.  Original code by Pradu Kannan as  *
3470
 *           posted on CCC/Winboard forums, modified to work with Crafty.      *
3471
 *                                                                             *
3472
 *   21.4    Misc eval changes.  More king safety changes to include a tropism *
3473
 *           distance of 1 for bishops or rooks that can slide to intercept    *
3474
 *           any square surrounding the enemy king.  Reduced rook slide score  *
3475
 *           by 40%.
3476
 *                                                                             *
3477
 *   21.5    passed pawn extension revisited.  Bad trade was giving a penalty  *
3478
 *           for being down an exchange (or a bonus for being up an exchange)  *
3479
 *           which was not expected behavior.  This has been fixed.  EGTB      *
3480
 *           initialization was being done during the first pass over the      *
3481
 *           RC file, which was too early as the rest of the engine had not    *
3482
 *           been initialized.  Now the "egtb" command is the only thing that  *
3483
 *           triggers initialization, and is only processed during the second  *
3484
 *           pass over the RC file after paths have been properly set up.      *
3485
 *           Abs() function bug fix.                                           *
3486
 *                                                                             *
3487
 *   21.6    Passed pawn evaluation changed with respect to distant passed     *
3488
 *           pawns and distant majorities.  Now, if both have a pawn majority  *
3489
 *           we check to see if one is "distant" (away from the enemy king) as *
3490
 *           this represents a significant time advantage because the other    *
3491
 *           side has to waste moves to reach and capture the pawn to prevent  *
3492
 *           promotion.  LMR no longer uses the history counters, which proved *
3493
 *           to be essentially random numbers.  LMR decisions are now based    *
3494
 *           solely on static characteristics of an individual move, rather    *
3495
 *           than trying to determine how the move affected the search at      *
3496
 *           other points in the tree.  New wtm bonus added to evaluation.     *
3497
 *           EvaluateMate() bug fixed where the score was set to a non-zero    *
3498
 *           value which broke EvaluateMate() completely.  New mobility code   *
3499
 *           for bishops/rooks encourages better piece placement.  Note that   *
3500
 *           there are many other evaluation changes too numerous to list here *
3501
 *           in detail.                                                        *
3502
 *                                                                             *
3503
 *   21.7    New reduce at root code added to do reductions at the root, but   *
3504
 *           not on any move that has been a "best move" during the current    *
3505
 *           search, unless the move drops too far in the move list.  New nice *
3506
 *           mode for parallel search terminates threads while waiting on the  *
3507
 *           opponent's move to avoid burning unnecessary CPU time (this nice  *
3508
 *           mode is NOT the default mode however.) -DNOFUTILITY removed as    *
3509
 *           futility is on by default and should not be removed.  All of the  *
3510
 *           search() code has been cleaned up and streamlined a bit for more  *
3511
 *           clarity when reading the code.  Nasty bug in EvaluatePawns() that *
3512
 *           unfortunattely used the king squares to make a decision dealing   *
3513
 *           with outside passed pawns, but the hash signature for pawns does  *
3514
 *           not include king position.  This code was deemed unnecessary and  *
3515
 *           was summarily removed (not summarily executed, it has had that    *
3516
 *           done far too many times already).                                 *
3517
 *                                                                             *
3518
 *   22.0    Redesign of the data structures completed so that Crafty will no  *
3519
 *           longer have duplicated code for wtm and btm cases.  This has      *
108 pmbaty 3520
 *           already resulted in a rewrite of movgen(), SEE(), Make() and      *
33 pmbaty 3521
 *           Unmake() and cut that code size by almost exactly 50%. The result *
3522
 *           has been slightly faster speeds, but also the future benefits     *
3523
 *           will be enormous by eliminating a lot of debugging caused by the  *
3524
 *           duplicated code.  Timing change to avoid starting an iteration    *
3525
 *           and wasting significant time.  We still search enough to get a    *
3526
 *           quick fail-low, otherwise we stop and make the move and save the  *
3527
 *           time we used to burn.  Massive cleanup in the macros used to      *
3528
 *           access much of the data since the new [wtm] approach resulted in  *
3529
 *           many old macros becoming unused.  Yet another rewrite of the code *
3530
 *           that handles repetition detection.  There is an undetected bug in *
3531
 *           the previous code, related to pondering and SMP, that was not     *
3532
 *           obvious.  The code was completely rewritten and is now much       *
3533
 *           simpler to understand, and has been verified to be bug-free with  *
3534
 *           massive cluster testing.                                          *
3535
 *                                                                             *
3536
 *   22.1    Minor fix for CPUS=1, which would cause compile errors.  Other    *
3537
 *           eval tweaks to improve scoring.  New "skill" command that can be  *
3538
 *           used to "dumb down" Crafty.  "Skill <n>" where n is a number      *
3539
 *           between 1 and 100.  100 is max (default) skill.  Skill 70 will    *
3540
 *           drop the playing Elo by about 200 points.  Skill 50 will drop it  *
3541
 *           about 400 points.  The curve is not linear, and the closer you    *
3542
 *           get to 1, the lower the rating.  To use this feature, you need to *
3543
 *           add -DSKILL to your Makefile options otherwise it is not included *
3544
 *           in the executable.                                                *
3545
 *                                                                             *
3546
 *   22.2    We are now back to using POSIX threads, since all current Linux   *
3547
 *           distributions now use the posix-conforming NTPL implementation    *
3548
 *           which should eliminate the various compatibility issues that      *
3549
 *           caused problems in the past.  This also should make the new       *
3550
 *           smpnice mode work correctly for Linux and Windows since they will *
3551
 *           now both use threads for the SMP search.  Fruit-like scoring      *
3552
 *           (interpolation between mg and eg scores) fully implemented.  AEL  *
3553
 *           pruning (Heinz 2000) also fully implemented (we had razoring and  *
3554
 *           futility, but not extended futility).  "Eval" command has been    *
3555
 *           removed and combined with the "personality" command so that eval  *
3556
 *           parameters can be modified, in addition to some search parameters *
3557
 *           that also belong in the personality data.  Mate threat extension  *
3558
 *           and one legal reply to check extensions have been removed.  Tests *
3559
 *           proved them to be absolutely useless, over 30,000 games for each  *
3560
 *           test showed no gain and sometimes a loss in playing strength with *
3561
 *           them so we followed the "simple is better" and removed them.  The *
3562
 *           fractional ply code was also removed since the only extension we  *
3563
 *           now use is the "give check" extension which is a whole ply.  A    *
3564
 *           significant number of evaluation parameters have been changed,    *
3565
 *           a few even removed as cluster testing helped us find optimal      *
3566
 *           values.  There are a few new terms, with more planned.            *
3567
 *                                                                             *
3568
 *   22.3    Corrected a bug in analysis mode where "back n" would crash.  A   *
3569
 *           bad array reference broke this during recent changes.             *
3570
 *                                                                             *
3571
 *   22.4    Corrected a bug in NextRootMove() that tries to calculate the NPS *
3572
 *           by adding up the node counters in each used split block.  However *
3573
 *           should you not use all possible threads (mt=n where n is < the    *
3574
 *           max CPUS value compiled in) then all split blocks will not be     *
3575
 *           properly initialized (not even allocated) which will cause a      *
3576
 *           segfault instantly.  Pawn island evaluation term has also been    *
3577
 *           removed as cluster testing showed that setting these values to    *
3578
 *           zero produced a +14 Elo gain.  There might be a better value for  *
3579
 *           them, but the old value was too high.  Further cluster testing is *
3580
 *           underway to test other (but smaller than original) values to see  *
3581
 *           if one of them turns out to be better than zero.  So far, no, but *
3582
 *           the test has not completed.                                       *
3583
 *                                                                             *
3584
 *   22.5    Minor eval tweaks.  Pthread_create was called without giving it   *
3585
 *           any attributes for the new thread(s) which makes them default to  *
3586
 *           "joinable".  When smpnice=1 terminates threads, the threads hang  *
3587
 *           around using memory expecting that the parent will join with them *
3588
 *           to check the exit status.  We now set "detached" as the proper    *
3589
 *           attribute so that this memory leak no longer happens.             *
3590
 *                                                                             *
3591
 *   22.6    Compile issue fixed which would cause the pthread_lib calls to    *
3592
 *           be used in a windows compile, which was incorrect.  They are now  *
3593
 *           protected by a #if defined(UNIX) && (CPUS > 1) conditional test.  *
3594
 *                                                                             *
3595
 *   22.7    Windows NUMA support fix to eliminate a memory leak caused by     *
3596
 *           re-malloc'ing the split blocks each time a new game is started.   *
3597
 *           This was not found in the previous memory leak testing as that    *
3598
 *           problem was related to stopping/re-starting threads when using    *
3599
 *           smpnice=1.  This is unrelated, but similar, and has been around   *
3600
 *           a long time.                                                      *
3601
 *                                                                             *
3602
 *   22.8    Modified material imbalance fix from Tracy included.  This        *
3603
 *           produced an immediate +7 Elo improvement, and additional tuning   *
3604
 *           is under way.  Fix to GenerateChecks().  This code had bugs a few *
3605
 *           weeks back and they were fixed, but the fixes were somehow lost   *
3606
 *           during copying files among multiple machines.  The fixes are now  *
3607
 *           back in and properly saved.                                       *
3608
 *                                                                             *
3609
 *   22.9    More eval tuning producing another +10 Elo gain.  Curmv[n] was    *
3610
 *           "overloaded" in that it was used to obtain the next move for a    *
3611
 *           parallel thread and was also used to pass back a "fail high" move *
3612
 *           to store in a "LOWER" hash table entry.  This led to some error   *
3613
 *           messages internal to Crafty that could possibly cause problems.   *
3614
 *           There is now a second variable "cutmove" used to pass the move    *
3615
 *           back to eliminate race conditions.  Hash.c modified extensively   *
3616
 *           to improve readability as well as to eliminate duplicated code.   *
3617
 *           Internal iterative deepening code removed.  No measurable benefit *
3618
 *           in real game testing led to removal since it is simpler without   *
3619
 *           the code.  EvaluateMaterialDynamic() also removed.  This was code *
3620
 *           that implemented ideas suggested by IM Larry Kaufman, but careful *
3621
 *           testing showed absolutely no effect on game results.  Again, we   *
3622
 *           chose "simpler is better" and removed it, as no tuning attempts   *
3623
 *           helped for this code.  Nasty pawn hashing bug fix, something that *
3624
 *           has been around for years.  This was essentially the same sort of *
3625
 *           bug that the "lockless hashing algorithm" addressed for the main  *
3626
 *           hash table, where out-of-order memory writes could produce a hash *
3627
 *           signature and the 64 bit word with score and best move which did  *
3628
 *           not "go together".  The same thing happens in pawn hashing,       *
3629
 *           except that an entry is 32 bytes rather than 16, giving even      *
3630
 *           higher probability of an entry that contains mismatched pieces.   *
3631
 *           In the best case, we just got a bad score.  In the worst case,    *
3632
 *           this could cause a program crash since some of the data might be  *
3633
 *           invalid, yet it can be used in ways that produce impossible array *
3634
 *           subscripts that would cause (very rarely) a segmentation fault.   *
3635
 *           The solution was to do the same lockless hashing algorithm where  *
3636
 *           the key is XORed with the other three 64 bit words when the entry *
3637
 *           is stored, then XORed with them again when we try to match.  If   *
3638
 *           the entry is mismatched, the signature will not match anything    *
3639
 *           and we simply will not use the bad data and do a normal pawn      *
3640
 *           evaluation to compute valid values.                               *
3641
 *                                                                             *
3642
 *    23.0   Essentially a cleaned up 22.9 version.  Comments have been        *
3643
 *           reviewed to make sure they are consistent with what is actually   *
3644
 *           done in the program.  Major change is that the random numbers     *
3645
 *           used to produce the Zobrist hash signature are now statically     *
3646
 *           initialized which eliminates a source of compatibility issues     *
3647
 *           where a different stream of random numbers is produced if an      *
3648
 *           architecture has some feature that changes the generator, such    *
3649
 *           as a case on an older 30/36 bit word machine.  The issue with     *
3650
 *           this change is that the old binary books are not compatible and   *
3651
 *           need to be re-created with the current random numbers.  The       *
3652
 *           "lockless hash table" idea is back in.  It was removed because    *
3653
 *           the move from the hash table is recognized as illegal when this   *
3654
 *           is appropriate, and no longer causes crashes.  However, the above *
3655
 *           pawn hash issue showed that this happens enough that it is better *
3656
 *           to avoid any error at all, including the score, for safety.  We   *
3657
 *           made a significant change to the parallel search split logic in   *
3658
 *           this version.  We now use a different test to limit how near the  *
3659
 *           tips we split.  This test measures how large the sub-tree is for  *
3660
 *           the first move at any possible split point, and requires that     *
3661
 *           this be at least some minimum number of nodes before a split can  *
3662
 *           be considered at this node.  The older approach, which based this *
3663
 *           decsion on remaining search depth at a node led to some cases     *
3664
 *           where the parallel search overhead was excessively high, or even  *
3665
 *           excessively low (when we chose to not split frequently enough).   *
3666
 *           This version appears to work well on a variety of platforms, even *
3667
 *           though NUMA architectures may well need additional tuning of this *
3668
 *           paramenter (smpsn) as well as (smpgroup) to try to contain most   *
3669
 *           splits on a single NUMA node where memory is local.  I attempted  *
3670
 *           to automate this entire process, and tune for all sorts of plat-  *
3671
 *           forms, but nothing worked for the general case, which leaves the  *
3672
 *           current approach.  When I converted back to threads from          *
3673
 *           processes, I forgot to restore the alignment for the hash/pawn-   *
3674
 *           hash tables.  The normal hash table needs to be on a 16 byte      *
3675
 *           boundary, which normally happens automatically, but pawn hash     *
3676
 *           entries should be on a 32 byte boundary to align them properly in *
3677
 *           cache to avoid splitting an entry across two cache blocks and     *
3678
 *           hurting performance.  New rook/bishop cache introduced to limit   *
3679
 *           overhead caused by mobility calculations.  If the ranks/files the *
3680
 *           rook is on are the same as the last time we evaluated a rook on   *
3681
 *           this specific square, we can reuse the mobility score with no     *
3682
 *           calculation required.  The code for "rook behind passed pawns"    *
3683
 *           was moved to EvaluatePassedPawns() so that it is only done when   *
3684
 *           there is a passed pawn on the board, not just when rooks are      *
3685
 *           present.  Book learning has been greatly cleaned up and           *
3686
 *           simplified.  The old "result learning" (which used the game       *
3687
 *           result to modify the book) and "book learning" (which used the    *
3688
 *           first N search scores to modify the book) were redundant, since   *
3689
 *           result learning would overwrite whatever book learning did.  The  *
3690
 *           new approach uses the game result (if available) to update the    *
3691
 *           book database when the program exits or starts a new game.  If    *
3692
 *           a result is not available, it will then rely on the previous      *
3693
 *           search results so that it has some idea of whether this was a     *
3694
 *           good opening or not, even if the game was not completed.  Minor   *
3695
 *           LMR bug in SearchRoot() could do a PVS fail-high research using   *
3696
 *           the wrong depth (off by -1) because of the LMR reduction that had *
3697
 *           been set.  The normal search module had this correct, but the     *
3698
 *           SearchRoot() module did not.  EvaluateDevelopment() was turned    *
3699
 *           off for a side after castling.  This caused a problem in that we  *
3700
 *           do things like checking for a knight blocking the C-pawn in queen *
3701
 *           pawn openings.  Unfortunately, the program could either block the *
3702
 *           pawn and then castle, which removed the blocked pawn penalty, or  *
3703
 *           it could castle first and then block the pawn, making it more     *
3704
 *           difficult to develop the queen-side.  We now enable this code     *
3705
 *           always and never disable it.  This disable was done years ago     *
3706
 *           when the castling evaluation of Crafty would scan the move list   *
3707
 *           to see if Crafty had castled, when it noticed castling was no     *
3708
 *           longer possible.  Once it had castled, we disabled this to avoid  *
3709
 *           the lengthy loop and the overhead it caused.  Today the test is   *
3710
 *           quite cheap anyway, and I measured no speed difference with it on *
3711
 *           or off, to speak of.  Now we realize that the knight in front of  *
3712
 *           the C-pawn is bad and needs to either not go there, or else move  *
3713
 *           out of the way.                                                   *
3714
 *                                                                             *
3715
 *    23.1   Xboard protocol support slightly modified so that draw results    *
3716
 *           are now sent after the move that causes the draw.  They were      *
3717
 *           always sent before a move if the position prior to the move was   *
3718
 *           drawn, but they were also sent just before playing a move to work *
3719
 *           around a xboard protocol race condition where I can send a move,  *
3720
 *           and then a draw claim (xboard 'result string') and the GUI sees   *
3721
 *           my move, relays it to my opponent, and gets an instant reply that *
3722
 *           it then sees before my draw claim.  After making this move and    *
3723
 *           forwarding it to me, it now thinks the draw claim is invalid.     *
3724
 *           slight tweak to queen material value and the way our bishop pair  *
3725
 *           scoring is done.  The old razoring code has been removed.  It is  *
3726
 *           redundant with LMR, and it had the unwanted side-effect of        *
3727
 *           reducing a few of the moves two plies.  The futility pruning has  *
3728
 *           been modified to work at the last N plies.  We have an array of   *
3729
 *           pruning margins (tuned on the cluster) where anytime the depth is *
3730
 *           less than "pruning_depth" we try the futility test.  If the test  *
3731
 *           succeeds, that move is discarded and not searched at all.  Move   *
108 pmbaty 3732
 *           ordering code modified slightly to call SEE() fewer times if the  *
33 pmbaty 3733
 *           goodness of the capture can still be accurately assessed without  *
3734
 *           the call.  Hash table has been changed to use a bucket of 4       *
3735
 *           entries similar to a 4-way set associative cache.  When a store   *
3736
 *           is attempted, we replace the least useful entry (see hash.c for   *
3737
 *           details).  Additionally the hash table is forced to a 64 byte     *
3738
 *           boundary alignment so that a bucket aligns on the start of a      *
3739
 *           cache line for efficiency.  The old approach used 3 entries per   *
3740
 *           bucket, the Belle two-table approach, which caused 1/2 of the     *
3741
 *           cache probes to result in two cache line reads rather than just   *
3742
 *           one.  Slight modification to "time overflow" logic.  Now, if the  *
3743
 *           score drops on the iteration where we reach the normal time       *
3744
 *           allocation, Crafty will continue to search.  Previously the score *
3745
 *           had to drop by 1/4 pawn, but cluster testing has shown that this  *
3746
 *           change is better.  Final bit of old rotated bitboard logic        *
3747
 *           removed.  We had two bitboards, RooksQueens and BishopsQueens     *
3748
 *           that were updated in MakeMove() and UnmakeMove().  It turned out  *
3749
 *           to be faster to just OR the needed piece bitboards together since *
3750
 *           they were infrequently used in the new "magic" approach, which    *
3751
 *           allowed me to remove them completely from the position structure  *
3752
 *           and make/unmake.  Net result was a percent or two faster search   *
3753
 *           overall.  Old bug fixed related to EGTB usage.  The Edwards       *
3754
 *           format did not include en passant pawn captures, so it was        *
3755
 *           necessary to avoid probing any position where both sides had a    *
3756
 *           pawn and there was any possibility of an en passant capture (one  *
3757
 *           pawn on original square, enemy pawn anywhere on either adjacent   *
3758
 *           file).  The Nalimov tables consider en passant correctly, but     *
3759
 *           somehow this old code was either left in iterate.c or else added  *
3760
 *           back in by accident.  It is now no longer present.  Minor changes *
3761
 *           (suggested by J. Cleveland on CCC) that avoids trying a null-move *
3762
 *           search when remaining depth == 1, and which saves some effort by  *
3763
 *           remembering, when a node fails low, the first move searched.      *
3764
 *           This move then gets stored in the hash table as the "best move".  *
3765
 *           While this might be wrong, it has a chance of being right and     *
3766
 *           when it is good enough to cause a cutoff, we can avoid a move     *
3767
 *           generation which saves a bit of time.  Fixed a significant bug in *
3768
 *           EvaluateWinningChances() where it was using a global variable     *
3769
 *           "wtm" which tells who is to move in the real game, not at the     *
3770
 *           current position in the treee.  This value  is used for tempo     *
3771
 *           calculations to determine if the losing king (in a K + rook-pawn  *
3772
 *           ending (with or without wrong bishop)) can reach the queening     *
3773
 *           square before the winning side's king can block acccess.  This    *
3774
 *           would randomly make the tempo calculation off by one, so that the *
3775
 *           score could bounce from draw to win and back as the game          *
3776
 *           progresses, since about 1/2 the time the wtm  value is wrong.  I  *
3777
 *           simply passed the current wtm in as a formal parameter to solve   *
3778
 *           this (a good example of why having a global variable and a formal *
3779
 *           argument paramenter using the same name is a bad idea.)  In this  *
3780
 *           same general area, EvaluateDraws() could set the score to         *
3781
 *           DrawScore, which was somewhat "iffy".  I changed this to simply   *
3782
 *           determine if the side that ahead is score has any realistic       *
3783
 *           winning chances.  If not, I divide the score by 10 rather than    *
3784
 *           setting it to the absolute drawscore.  This has the same effect,  *
3785
 *           but does let the evaluation guide the search to better moves when *
3786
 *           it is convinced all of them lead to draws.  This version is about *
3787
 *           +100 Elo stronger than 23.0 based on cluster testing results.     *
3788
 *                                                                             *
3789
 *    23.2   Two changes related to draw handling.  First, the 50-move draw    *
3790
 *           rule is tricky.  It is possible that the move on the 100th ply    *
3791
 *           after the last non-reversible move might not be a draw, even if   *
3792
 *           it is reversible itself, because it might deliver mate.  In this  *
3793
 *           specific case, the mate will end the game as a win.  This has     *
3794
 *           been fixed in Crafty to match this specific rule exception.  Also *
3795
 *           draws by "insufficient material" have been changed to match the   *
3796
 *           FIDE rules which say that mate can't be possible, even with worst *
3797
 *           possible play by the opponent.  Crafty would claim a draw in a    *
3798
 *           simple KN vs KN position, which is incorrect.  Even though        *
3799
 *           neither side can force mate, mate is possible and so the position *
3800
 *           is not a draw by FIDE rules.  Mobility scoring changed for        *
3801
 *           sliding pieces.  Now the mobility scores are pre-computed and     *
3802
 *           stored in a table that is addressed by the "magic number move     *
3803
 *           generation idea".  This completely eliminates the computational   *
3804
 *           cost of the mobility scoring since all of the scores are pre-     *
3805
 *           computed before the game starts.  This was a modest speed         *
3806
 *           improvement but really made the code simpler and smaller.  Change *
3807
 *           to lazy eval cutoff code to improve accuracy.  BookUP() had a     *
3808
 *           minor bug that caused it to not report how many moves were not    *
3809
 *           included because of the "maxply" limit.  This has been fixed also *
3810
 *           so that it now reports the correct value.  New xboard "cores" and *
3811
 *           "memory" command support added.  Code to malloc() both was also   *
3812
 *           rewritten completely to clean it up and simplify things.  In an   *
3813
 *           effort to avoid confusion, there is one caveat here.  If your     *
3814
 *           .craftyrc/crafty.rc file has an option to set the number of       *
3815
 *           threads (mt=n/smpnt=n), then the "cores" option from xboard is    *
3816
 *           automatically disabled so that the .craftyrc option effectively   *
3817
 *           overrides any potential cores setting.  If you set hash or hashp  *
3818
 *           in the .craftyrc/crafty.rc file, then the "memory" option from    *
3819
 *           xboard is automatically disabled, for the same reason.  In short, *
3820
 *           you can let xboard set the number of threads and memory usage     *
3821
 *           limit, or you can set them in your .craftyrc/crafty.rc file.  But *
3822
 *           if you use the .craftyrc/crafty.rc file to set either, then the   *
3823
 *           corresponding xboard command will be disabled.                    *
3824
 *                                                                             *
3825
 *    23.3   Null-move search restriction changed to allow null-move searches  *
3826
 *           at any node where the side on move has at least one piece of any  *
3827
 *           type.  Minor bug in ValidMove() fixed to catch "backward" pawn    *
3828
 *           moves and flag them as illegal.  This sometimes caused an error   *
3829
 *           when annotating games and annotating for both sides.  The killer  *
3830
 *           move array could have "backward" moves due to flipping sides back *
3831
 *           and forth, which would cause some odd PV displays.  Small change  *
3832
 *           to "reduce-at-root".  We no longer reduce moves that are flagged  *
3833
 *           as "do not search in parallel".  Check extension modified so that *
3834
 *           if "SEE" says the check is unsafe, the extension is not done.  We *
3835
 *           found this to be worth about +10 Elo.  We also now reduce any     *
3836
 *           capture move that appears in the "REMAINING_MOVES" phase since    *
3837
 *           they can only appear there if SEE returns a score indicating loss *
3838
 *           of material.  We now reduce a bit more aggressively, reducing by` *
3839
 *           2 plies once we have searched at least 4 moves at any ply.  I     *
3840
 *           tried going to 3 on very late moves, but could not find any case  *
3841
 *           where this was better, even limiting it to near the root or other *
3842
 *           ideas.  But reducing by 2 plies after the at least 2 moves are    *
3843
 *           searched was an improvement.  Minor change is that the first move *
3844
 *           is never reduced.  It is possible that there is no hash move, no  *
3845
 *           non-losing capture, and no killer move.  That drops us into the   *
3846
 *           REMAINING_MOVES phase which could reduce the first move searched, *
3847
 *           which was not intended.  Very minor tweak, but a tweak all the    *
3848
 *           same.                                                             *
3849
 *                                                                             *
3850
 *    23.4   Bug in hash implementation fixed.  It was possible to get a hash  *
3851
 *           hit at ply=2, and alter the ply=1 PV when it should be left with  *
3852
 *           no change.  This could cause Crafty to display one move as best   *
3853
 *           and play another move entirely.  Usually this move was OK, but it *
3854
 *           could, on occasion, be an outright blunder.  This has been fixed. *
3855
 *           The search.c code has been re-written to eliminate SearchRoot()   *
3856
 *           entirely, which simplifies the code.  The only duplication is in  *
3857
 *           SearchParallel() which would be messier to eliminate.  Ditto for  *
3858
 *           quiesce.c, which now only has Quiesce() and QuiesceEvasions().    *
3859
 *           The old QuiesceChecks() has been combined with Quiesce, again to  *
3860
 *           eliminate duplication and simplify the code.  Stupid bug in code  *
3861
 *           that handles time-out.  One place checked the wrong variable and  *
3862
 *           could cause a thread to attempt to output a PV after the search   *
3863
 *           was in the process of timing out.  That thread could back up an   *
3864
 *           incomplete/bogus search result to the root in rare occasions,     *
3865
 *           which would / could result in Crafty kibitzing one move but       *
3866
 *           playing a different move.  On occasion, the move played could be  *
108 pmbaty 3867
 *           a horrible blunder.  Minor bug caused StoreTransRef() to lose a   *
3868
 *           best move when overwriting an old position with a null-move       *
3869
 *           search result.  Minor change by Tracy to lazy evaluation cutoff   *
3870
 *           to speed up evaluation somewhat.  Old "Trojan Horse" attack       *
3871
 *           detection was removed.  At today's depths, it was no longer       *
3872
 *           needed.  New hash idea stores the PV for an EXACT hash entry in a *
3873
 *           separate hash table.  When an EXACT hit happens, this PV can be   *
3874
 *           added to the incomplete PV we have so far so that we have the     *
3875
 *           exact path that leads to the backed up score.  New "phash" (path  *
3876
 *           hash) command to set the number of entries in this path hash      *
3877
 *           table.  For longer searches, a larger table avoids path table     *
3878
 *           collisions which will produce those short paths that end in <HT>. *
3879
 *           If <HT> appears in too many PVs, phash should be increased.  The  *
3880
 *           "Trojan Horse" code has been completely removed, which also       *
3881
 *           resulted in the removal of the last bit of "pre-processing code"  *
3882
 *           in preeval.c, so it has been completely removed as well.  Current *
3883
 *           search depths are such that the Trojan Horse code is simply not   *
3884
 *           needed any longer.  A minor bug in TimeSet() fixed where on rare  *
3885
 *           occasions, near the end of a time control, time_limit could be    *
3886
 *           larger than the max time allowed (absolute_time_limit).  We never *
3887
 *           check against this limit until we exceed the nominal time limit.  *
3888
 *           Cleanup on the draw by repetition code to greatly simplify the    *
3889
 *           code as well as speed it up.                                      *
33 pmbaty 3890
 *                                                                             *
3891
 *    23.5   Several pieces of code cleanup, both for readability and speed.   *
3892
 *           We are now using stdint.h, which lets us specific the exact size  *
3893
 *           of an integer value which gets rid of the int vs long issues that *
3894
 *           exist between MSVC and the rest of the world on X86_64.  We still *
3895
 *           use things like "int" if we want to let the compiler choose the   *
3896
 *           most efficient size, but when we need a specific number of bits,  *
3897
 *           such as 64, we use a specific type (uint64_t for 64 bits).  Minor *
3898
 *           change to forward pruning that does not toss out passed pawn      *
3899
 *           moves if they are advanced enough (compared to remaining depth)   *
3900
 *           that they might be worth searching.  Cleanup to passed pawn eval  *
3901
 *           code to help efficiency and readability.  New options to the      *
3902
 *           display command.  "display nothing" turns all output off except   *
3903
 *           for the move Crafty chooses to play.  "display everything" will   *
3904
 *           produce a veritable flood of information before it makes a move.  *
3905
 *           Cleanup of personality code to fix some values that were not      *
3906
 *           included, as well as clean up the formatting for simplicity.      *
3907
 *                                                                             *
3908
 *    23.6   Minor tweak to "adaptive hash" code + a fix to the usage warning  *
3909
 *           that failed to explain how many parameters are required.  New way *
3910
 *           of timing the search, replacing the old "easy move" code that was *
3911
 *           simply too ineffective.  Now Crafty computes a "difficulty" level *
3912
 *           after each iteration.  If the best move doesn't change, this      *
3913
 *           level is reduced by 10% of the current value, until it reaches    *
3914
 *           60% where it stops dropping.  If the best move changes during an  *
3915
 *           iteration, it adjusts in one of two ways.  If the current         *
3916
 *           difficulty level is less than 100%, it reverts to 100% + the      *
3917
 *           number of different best moves minus 1 times 20%.  If the current *
3918
 *           difficulty level is already >= 100% it is set to 80% of the       *
3919
 *           current value + (again) the number of different best moves - 1    *
3920
 *           times 20%.  Repeated changes can run this up to 180% max.  As the *
3921
 *           search progresses this difficulty level represents the percentage *
3922
 *           of the nominal target time limit it should use.  It still tries   *
3923
 *           to complete the current iteration before quitting, so this limit  *
3924
 *           is a lower bound on the time it will use.  Restored an old idea   *
3925
 *           from earlier Crafty versions (and even Cray Blitz), that of       *
3926
 *           trying the killers from two plies earlier in the tree, once the   *
3927
 *           killers for the current ply have been tried.  Was a +10 Elo gain, *
3928
 *           added to about +10 for the new time control logic.  Old fail-high *
3929
 *           restriction removed.  At one point in time, a fail-high on the    *
3930
 *           null-window search at the root would cause problems.  Crafty was  *
3931
 *           modified so that the fail-high was ignored if the re-search       *
3932
 *           failed low.  Removing this produced an 8 Elo gain.                *
3933
 *                                                                             *
3934
 *    23.7   Minor fix to time check code.  Was particularly broken at very    *
3935
 *           low skill level settings, but it had a small impact everywhere.   *
3936
 *           Some simplification with the reduction code, and how moves are    *
3937
 *           counted as they are searched, which would not count any moves     *
3938
 *           that were pruned, which could delay triggering reductions at a    *
3939
 *           ply.  Fixed significant killer move bug that would only surface   *
3940
 *           during a parallel search.  NextMove (remaining moves phase) culls *
3941
 *           killer moves as it selects, because they have already been tried. *
3942
 *           Unfortunately, in a parallel search, the killers could get        *
3943
 *           modified by other threads, which could erroneously cause a move   *
3944
 *           to be excluded that had not been searched.  I discovered this bug *
3945
 *           on wac #266 where Crafty normally finds a mate at depth=11, but   *
3946
 *           a parallel search would often miss the mate completely.  Minor    *
3947
 *           fix to new "difficulty" time management.  It was possible for a   *
3948
 *           move to look "easy" (several iterations without finding a new     *
3949
 *           best move) but it could fail low at the beginning of the next     *
3950
 *           iteration.  Difficulty would be reduced due to not changing the   *
3951
 *           best move at previous iterations, so the search could time out    *
3952
 *           quicker than expected.  Simple fix was to reset difficulty to     *
3953
 *           100% on a fail low, which makes sense anyway if it was supposed   *
3954
 *           to be "easy" but the score is mysteriously dropping anyway.       *
3955
 *                                                                             *
3956
 *    23.8   MAXPLY changed to 128, which increases the max length of the PV's *
3957
 *           that can be displayed.  The hash path stuff works so well that it *
3958
 *           often STILL has PVs with <HT> on the end, after 64 moves are      *
3959
 *           displayed.  While this can still probably be reached, it should   *
3960
 *           be much less frequent.  This does mean the search can go for 128  *
3961
 *           iterations, rather than the old 64 limit, of course.  Bug in      *
3962
 *           Search() that failed to update the PV on an EGTB hit, which would *
3963
 *           cause the hashed PV to contain illegal moves.  When Crafty then   *
3964
 *           tried to display this PV, it would promptly crash.                *
3965
 *                                                                             *
3966
 *    24.0   Major changes to portability/platform specific code.  Crafty now  *
3967
 *           supports Unix and Windows platforms.  All the other spaghetti     *
3968
 *           code has been removed, with an effort to make the code readable.  *
3969
 *           Couple of bugs in EvaluateWinningChances() fixed, one in          *
3970
 *           particular that makes the krp vs kr draw detection unreachable.   *
3971
 *           strcpy() replaced with memmove() to fix problems with Apple OS X  *
3972
 *           Mavericks where strcpy() with operands that overlap now causes an *
3973
 *           application to abort on the spot.  Bug in Search() that caused an *
3974
 *           inefficiency in the parallel search.  At some point the way moves *
3975
 *           are counted within a ply was changed, which changed the split     *
3976
 *           algorithm unintentionally.  The original intent was to search     *
3977
 *           just one move and then allow splits.  The bug changed this to     *
3978
 *           search TWO moves before splitting is allowed, which slowed things *
3979
 *           down quite a bit.  thread[i] is now an array of structures so     *
3980
 *           that the thread pointers are separated from each other to fit     *
3981
 *           into separate cache blocks, this avoids excessive cache traffic   *
3982
 *           since they get changed regularly and were sitting in a single     *
3983
 *           cache block with 8 cpus on 64 bit architectures.  Bug in new      *
3984
 *           "difficulty" time usage code would let the program run up against *
3985
 *           "absolute_time_limit" before stopping the search, even if we      *
3986
 *           finished an iteration using beyond the normal target.  This       *
3987
 *           really burned a lot of unnecessary time.  I had previously fixed  *
3988
 *           the PVS window fail high (at the root) to force such a move to be *
3989
 *           played in the game.  One unfortunate oversight however was that   *
3990
 *           when splitting at the root, N different processors could fail     *
3991
 *           high on N different moves, and they were searching those in       *
3992
 *           parallel.  I've modified this so that the PVS fail high at the    *
3993
 *           root exits from Search() (after stopping other busy threads) and  *
3994
 *           then returns to Iterate() which will print the fail-high notice   *
3995
 *           and re-start the search with all threads searching just one fail  *
3996
 *           high move to get a score quicker.  Fail-high/fail-low code clean- *
3997
 *           up.  In particular, the "delta" value to increment beta or        *
3998
 *           decrement alpha is now two variables, so that an early fail-low   *
3999
 *           won't increment the delta value and then the subsequent (hope-    *
4000
 *           fully) fail-high starts off with a bad increment and jumps beta   *
4001
 *           too quickly.  All fail-highs at the root return to Iterate(),     *
4002
 *           even the PVS null-window fail-highs.  This re-starts the search   *
4003
 *           with all processors working on that one move to get it resolved   *
4004
 *           much quicker.  Other minor fixes such as when failing low on the  *
4005
 *           first move, we pop out of search and go back to Iterate() to      *
4006
 *           lower alpha and start a new search.  Unfortunately, after the     *
4007
 *           first move is searched, a parallel split at the root would occur, *
4008
 *           only to be instantly aborted so that we could back out of Search()*
4009
 *           and then re-start.  Wasted the split time unnecessarily although  *
4010
 *           it was not a huge amount of time.  Another simplification to the  *
4011
 *           repetition-detection code while chasing a parallel search bug     *
4012
 *           was missing repetitions.                                          *
4013
 *                                                                             *
108 pmbaty 4014
 *    24.1   LMR reductions changed to allow a more dynamic reduction amount   *
4015
 *           rather than just 1 or 2.  An array LMR_reductions[depth][moves]   *
4016
 *           is initialized at start-up and sets the reduction amount for a    *
4017
 *           specific depth remaining (larger reduction with more depth left)  *
4018
 *           and the total moves searched at this node (again a larger         *
4019
 *           reduction as more moves are searched.)  This can easily be        *
4020
 *           changed with the new "lmr" command (help lmr for details).  The   *
4021
 *           old "noise" stuff has been changed.  Now, rather than noise       *
4022
 *           specifying the number of nodes that have to be searched before    *
4023
 *           any output is displayed (PV, fail high, etc) it now specifies a   *
4024
 *           time that must pass before the first output is produced.  This    *
4025
 *           can still be displayed with noise=0, otherwise noise=n says no    *
4026
 *           output until n seconds have passed (n can be a fraction of a      *
4027
 *           second if wanted.)  I restored the old adaptive null-move code,   *
4028
 *           but changed the reduction (used to be 2 to 3 depending on the     *
4029
 *           depth remaining) so that it is 3 + depth / 6.  For each 6 plies   *
4030
 *           of remaining depth, we reduce by another ply, with no real bound  *
4031
 *           other than the max depth we can reasonably reach.  The "6" in     *
4032
 *           the above can be changed with the personality command or the new  *
4033
 *           null m n command.  When the changes to noise were made, Iterate() *
4034
 *           was also changed so that if the search terminates for any reason  *
4035
 *           and no PV was displayed, whether because the noise limit was not  *
4036
 *           satisfied or this search did not produce a PV because it started  *
4037
 *           at an excessively deep search depth, Crafty now always displays   *
4038
 *           at least one PV in the log file to show how the game is going to  *
4039
 *           continue.  One case where this was an issue was where Crafty did  *
4040
 *           a LONG ponder search because the opponent took an excessive       *
4041
 *           amount of time to make a move.  If it finishes (say) a 30 ply     *
4042
 *           search while pondering, when the opponent moves, Crafty will move *
4043
 *           instantly, but when it starts the next search, we start pondering *
4044
 *           at iteration 29, which might take a long time to finish.  But we  *
4045
 *           do still have the PV from the last search (the 30 ply one) and    *
4046
 *           main() thoughtfully removed the first two moves (the move played  *
4047
 *           and the move we are now pondering, so we always have something to *
4048
 *           display, and now we no longer terminate a search with no output   *
4049
 *           (PV, time, etc) at all.  Generation II of the parallel split code *
4050
 *           (see Thread() in the thread.c source file comments for specifics) *
4051
 *           that is a lighter-weight split/copy plus the individual threads   *
4052
 *           do most of the copying rather than the thread that initiates the  *
4053
 *           split.  More work on the skill command.  For each 10 units the    *
4054
 *           skill level is reduced, the search speed is reduced by 50%.       *
4055
 *           Additionally the null-move and LMR reduction values are also      *
4056
 *           ramped down so that by the time skill drops below 10, there are   *
4057
 *           no reductions left.                                               *
4058
 *                                                                             *
4059
 *    24.2   Bug in SetTime() fixed.  The absolute time limit was set way too  *
4060
 *           large in sudden death games (games with no secondary time control *
4061
 *           to add time on the clock) with an increment.  It was intended to  *
4062
 *           allow the search to use up to 5x the normal target time, unless   *
4063
 *           that was larger than 1/2 of the total time remaining, but some-   *
4064
 *           where along the way that 5x was removed and it always set the     *
4065
 *           absolute time limit to 1/2 the remaining clock, which would burn  *
4066
 *           clock time way too quickly.  Complete re-factor of Search() to    *
4067
 *           take the main loop and move that to a new procedure               *
4068
 *           SearchMoveList() to eliminate the duplicated search code between  *
4069
 *           Search() and SearchParallel() which has been replaced by          *
4070
 *           SearchMoveList().  New addition to NextMove().  It was cleaned up *
4071
 *           to eliminate the old NextEvasion() code since one was a sub-set   *
4072
 *           of the other.  A minor change to limit the NPS impact on history  *
4073
 *           ordering.  We do not do history ordering within 6 plies of a      *
4074
 *           terminal node, and we only try to order 1/2 of the moves before   *
4075
 *           giving up and assuming this is an ALL node where ordering does    *
4076
 *           not matter.                                                       *
4077
 *                                                                             *
4078
 *    25.0   Complete re-factor of pawn evaluation code.  There were too many  *
4079
 *           overlapping terms that made tuning difficult.  Now a pawn is      *
4080
 *           classified as one specific class, there is no overlap between     *
4081
 *           classes, which simplifies the code significantly.  The code is    *
4082
 *           now easier to understand and modify.  In addition, the passed     *
4083
 *           pawn evaluation was rewritten and consolidates all the passed     *
4084
 *           pawn evaluation in one place.  The evaluation used to add a bonus *
4085
 *           for rooks behind passed pawns in rook scoring, blockading some-   *
4086
 *           where else, etc.  All of this was moved to the passed pawn code   *
4087
 *           to make it easier to understand and modify.  A limited version of *
4088
 *           late move pruning (LMP) is used in the last two plies.  Once a    *
4089
 *           set number of moves have been searched with no fail high, non-    *
4090
 *           interesting moves are simply skipped in a way similar to futility *
4091
 *           pruning.  This version also contains a major rewrite of the       *
4092
 *           parallel search code, now referred to as Generation II.  It has a *
4093
 *           more lightweight split algorithm, that costs the parent MUCH less *
4094
 *           effort to split the search.  The key is that now the individual   *
4095
 *           "helper" threads do all the work, allocating a split block,       *
4096
 *           copying the data from the parent, etc., rather than the parent    *
4097
 *           doing it all.  Gen II based on the DTS "late-join" idea so that a *
4098
 *           thread will try to join existing split points before it goes to   *
4099
 *           the idle wait loop waiting for some other thread to split with    *
4100
 *           it.  In fact, this is now the basis for the new split method      *
4101
 *           where the parent simply creates a split point by allocating a     *
4102
 *           split block for itself, and then continuing the search, after the *
4103
 *           parent split block is marked as "joinable".  Now any idle threads *
4104
 *           just "jump in" without the parent doing anything else, which      *
4105
 *           means that currently idle threads will "join" this split block if *
4106
 *           they are in the "wait-for-work" spin loop, and threads that be-   *
4107
 *           come idle also join in exactly the same way.  This is MUCH more   *
4108
 *           efficient, and also significantly reduces the number of split     *
4109
 *           blocks needed during the search.  We also now pre-split the       *
4110
 *           search when we are reasonably close to the root of the tree,      *
4111
 *           which is called a "gratuitous split.  This leaves joinable split  *
4112
 *           points laying around so that whenever a thread becomes idle, it   *
4113
 *           can join in at these pre-existing split points immediately.  We   *
4114
 *           now use a much more conservative approach when dealing with fail  *
4115
 *           highs at the root.  Since LMR and such have introduced greater    *
4116
 *           levels of search instability, we no longer trust a fail-high at   *
4117
 *           the root if it fails low on the re-search.  We maintain the last  *
4118
 *           score returned for every root move, along with the PV.  Either an *
4119
 *           exact score or the bound score that was returned.  At the end of  *
4120
 *           the iteration, we sort the root move list using the backed-up     *
4121
 *           score as the sort key, and we play the move with the best score.  *
4122
 *           This solves a particularly ugly issue where we get a score for    *
4123
 *           the first move, then another move fails high, but then fails low  *
4124
 *           and the re-search produces a score that is actually WORSE than    *
4125
 *           the original best move.  We still see that, but we always play    *
4126
 *           the best move now.  One other efficiency trick is that when the   *
4127
 *           above happens, the search would tend to be less efficient since   *
4128
 *           the best score for that fail-high/fail-low move is actually worse *
4129
 *           than the best move/score found so far.  If this happens, the      *
4130
 *           score is restored to the original best move score (in Search())   *
4131
 *           so that we continue searching with a good lower bound, not having *
4132
 *           to deal with moves that would fail high with this worse value,    *
4133
 *           but not with the original best move's value.  Minor change to     *
4134
 *           history counters that now rely on a "saturating counter" idea.  I *
4135
 *           wanted to avoid the aging idea, and it seemed to not be so clear  *
4136
 *           that preferring history moves by the depth at which they were     *
4137
 *           good was the way to go.  I returned to a history counter idea I   *
4138
 *           tested around 2005 but discarded, namely using a saturating       *
4139
 *           counter.  The idea is that a center value (at present 1024)       *
4140
 *           represents a zero score.  Decrementing it makes it worse,         *
4141
 *           incrementing it makes it better.  But to make it saturate at      *
4142
 *           either end, I only reduce the counter by a fraction of its        *
4143
 *           distance from the saturation point so that once it gets to either *
4144
 *           extreme value, it will not be modified further avoiding wrap-     *
4145
 *           around.  This basic idea was originally reported by Mark Winands  *
4146
 *           in 2005.  It seems to provide better results (slightly) on very   *
4147
 *           deep searches.  One impetus for this was an intent to fold this   *
4148
 *           into a move so that I could sort the moves rather than doing the  *
4149
 *           selection approach I currently use.  However, this had a bad      *
4150
 *           effect on testing, since history information is dynamic and is    *
4151
 *           constantly changing, between two moves at the same ply in fact.   *
4152
 *           The sort fixed the history counters to the value at the start of  *
4153
 *           that ply.  This was discarded after testing, but the history      *
4154
 *           counter based on the saturating counter idea seemed to be OK and  *
4155
 *           was left in even though it produced minimal Elo gain during       *
4156
 *           testing.  Change to the way moves are counted, to add a little    *
4157
 *           more consistency to LMR.  Now Next*() returns an order number     *
4158
 *           that starts with 1 and monotonically increases, this order number *
4159
 *           is used for LMR and such decisions that vary things based on how  *
4160
 *           far down the move list something occurs.  Root move counting was  *
4161
 *           particularly problematic with parallel searching, now things are  *
4162
 *           at least "more consistent".  The only negative impact is that now *
4163
 *           the move counter gets incremented even for illegal moves, but     *
4164
 *           testing showed this was a no-change change with one thread, and   *
4165
 *           the consistency with multiple threads made it useful.  New method *
4166
 *           to automatically tune SMP parameters.  The command is autotune    *
4167
 *           and "help autotune" will explain how to run it.  Added the        *
4168
 *           "counter-move" heuristic for move ordering (Jos Uiterwijk, JICCA) *
4169
 *           which simply remembers a fail high move and the move at the       *
4170
 *           previous ply.  If the hash, captures or killer moves don't result *
4171
 *           in a fail high, this move is tried next.  No significant cost,    *
4172
 *           seems to reduce tree size noticeably.  Added a follow-up idea     *
4173
 *           based on the same idea, except we pair a move that fails high     *
4174
 *           with the move two plies back, introducing a sort of "connection"  *
4175
 *           between them.  This is a sort of "plan" idea where the first move *
4176
 *           of the pair enables the second move to fail high.  The benefit is *
4177
 *           that this introduces yet another pair of good moves that get      *
4178
 *           ordered before history moves, and is therefore not subject to     *
4179
 *           reduction.  I have been unable to come up with a reference for    *
4180
 *           this idea, but I believe I first saw it somewhere around the time *
4181
 *           Fruit showed up, I am thinking perhaps in the JICCA/JICGA.  Any   *
4182
 *           reference would be appreciated.  Minor change to the way the PV   *
4183
 *           and fail-hi/fail-low moves are displayed when pondering.  Crafty  *
4184
 *           now adds the ponder move to the front of the PV enclosed in ( )   *
4185
 *           so that it is always visible in console mode.  The depths are     *
4186
 *           reaching such extreme numbers the ponder move scrolls off the top *
4187
 *           of the screen when running in console mode or when "tail -f" is   *
4188
 *           used to watch the log file while a game is in progress.  This is  *
4189
 *           a bit trickier than you might think since Crafty displays the     *
4190
 *           game move numbers in the PV.  Penalty for pawns on same color as  *
4191
 *           bishop now only applies when there is one bishop.                 *
4192
 *                                                                             *
4193
 *    25.0.1 Cleanup of NextMove() plus a minor ordering bug fix that would    *
4194
 *           skip counter moves at ply = 2. Added NUMA code to force the hash  *
4195
 *           tables to be spread across the numa nodes as equally as possible  *
4196
 *           rather than all of the data sitting on just onenode.  This makes  *
4197
 *           one specific user policy important.  BEFORE you set the hash size *
4198
 *           for any of the four hash tables, you should ALWAYS set the max    *
4199
 *           threads limit first, so that the NUMA trick works correctly.  Of  *
4200
 *           course, if you do not use -DAFFINITY this is all irrelevant.  The *
4201
 *           -DNUMA option has been removed.  I no longer use any libnuma      *
4202
 *           routines.  A new "smpnuma" command is now used to enable/disable  *
4203
 *           NUMA mode (which really only affects how the hash tables are      *
4204
 *           cleared, all the other NUMA code works just fine no matter        *
4205
 *           whether this is enabled or disabled.  Fixed a bug with the xboard *
4206
 *           memory command that could overflow and cause preposterous malloc  *
4207
 *           requests.                                                         *
4208
 *                                                                             *
33 pmbaty 4209
 *******************************************************************************
4210
 */
4211
int main(int argc, char **argv) {
4212
  TREE *tree;
4213
  FILE *personality;
108 pmbaty 4214
  int move, readstat, value = 0, i, v, result, draw_type;
33 pmbaty 4215
  char announce[128];
4216
 
4217
#if defined(UNIX)
4218
  char path[1024];
4219
  struct passwd *pwd;
4220
#else
4221
  char crafty_rc_file_spec[FILENAME_MAX];
4222
#endif
4223
/* Collect environmental variables */
4224
  char *directory_spec = getenv("CRAFTY_BOOK_PATH");
4225
 
4226
  if (directory_spec)
108 pmbaty 4227
    strncpy(book_path, directory_spec, sizeof book_path);
33 pmbaty 4228
  directory_spec = getenv("CRAFTY_LOG_PATH");
4229
  if (directory_spec)
108 pmbaty 4230
    strncpy(log_path, directory_spec, sizeof log_path);
33 pmbaty 4231
  directory_spec = getenv("CRAFTY_TB_PATH");
4232
  if (directory_spec)
108 pmbaty 4233
    strncpy(tb_path, directory_spec, sizeof tb_path);
33 pmbaty 4234
  directory_spec = getenv("CRAFTY_RC_PATH");
4235
  if (directory_spec)
108 pmbaty 4236
    strncpy(rc_path, directory_spec, sizeof rc_path);
33 pmbaty 4237
  if (getenv("CRAFTY_XBOARD")) {
108 pmbaty 4238
    Print(-1, "feature done=0\n");
4239
    xboard_done = 0;
33 pmbaty 4240
  } else if (argc > 1 && !strcmp(argv[1], "xboard")) {
108 pmbaty 4241
    Print(-1, "feature done=0\n");
4242
    xboard_done = 0;
33 pmbaty 4243
  }
4244
/*
4245
 ************************************************************
4246
 *                                                          *
4247
 *  First, parse the command-line options and pick off the  *
4248
 *  ones that need to be handled before any initialization  *
4249
 *  is attempted (mainly path commands at present.)         *
4250
 *                                                          *
4251
 ************************************************************
4252
 */
108 pmbaty 4253
  AlignedMalloc((void *) ((void *) &tree), 2048, (size_t) sizeof(TREE));
33 pmbaty 4254
  block[0] = tree;
108 pmbaty 4255
  memset((void *) block[0], 0, sizeof(TREE));
33 pmbaty 4256
  tree->ply = 1;
4257
  input_stream = stdin;
4258
  for (i = 0; i < 512; i++)
4259
    args[i] = (char *) malloc(256);
4260
  if (argc > 1) {
4261
    for (i = 1; i < argc; i++) {
108 pmbaty 4262
      if (strstr(argv[i], "path") || strstr(argv[i], "log") ||
4263
          strstr(argv[1], "affinity")) {
4264
        strcpy(buffer, argv[i]);
33 pmbaty 4265
        result = Option(tree);
4266
        if (result == 0)
108 pmbaty 4267
          Print(2048, "ERROR \"%s\" is unknown command-line option\n",
4268
              buffer);
33 pmbaty 4269
        display = tree->position;
4270
      }
4271
    }
4272
  }
4273
/*
4274
 ************************************************************
4275
 *                                                          *
4276
 *  Now, read the crafty.rc/.craftyrc initialization file   *
4277
 *  and process the commands that need to be handled prior  *
4278
 *  to initializing things (path commands).                 *
4279
 *                                                          *
4280
 ************************************************************
4281
 */
4282
#if defined(UNIX)
4283
  input_stream = fopen(".craftyrc", "r");
4284
  if (!input_stream)
4285
    if ((pwd = getpwuid(getuid()))) {
4286
      sprintf(path, "%s/.craftyrc", pwd->pw_dir);
4287
      input_stream = fopen(path, "r");
4288
    }
4289
  if (input_stream)
4290
#else
108 pmbaty 4291
  sprintf(crafty_rc_file_spec, "%s/crafty.rc", rc_path);
33 pmbaty 4292
  if ((input_stream = fopen(crafty_rc_file_spec, "r")))
4293
#endif
4294
    while (1) {
108 pmbaty 4295
      readstat = Read(1, buffer);
33 pmbaty 4296
      if (readstat < 0)
4297
        break;
4298
      if (!strstr(buffer, "path"))
4299
        continue;
4300
      result = Option(tree);
4301
      if (result == 0)
108 pmbaty 4302
        Print(2048, "ERROR \"%s\" is unknown rc-file option\n", buffer);
33 pmbaty 4303
      if (input_stream == stdin)
4304
        break;
4305
    }
4306
/*
4307
 ************************************************************
4308
 *                                                          *
4309
 *   Initialize all data arrays and chess board.            *
4310
 *                                                          *
4311
 ************************************************************
4312
 */
4313
  Initialize();
4314
/*
4315
 ************************************************************
4316
 *                                                          *
4317
 *  Now, parse the command-line options and pick off the    *
4318
 *  ones that need to be handled after initialization is    *
4319
 *  completed.                                              *
4320
 *                                                          *
4321
 ************************************************************
4322
 */
4323
  if (argc > 1) {
4324
    for (i = 1; i < argc; i++)
4325
      if (strcmp(argv[i], "c"))
4326
        if (!strstr(argv[i], "path")) {
4327
          if (strlen(argv[i]) > 255)
108 pmbaty 4328
            Print(-1, "ERROR ignoring token %s, 255 character max\n",
33 pmbaty 4329
                argv[i]);
4330
          else {
108 pmbaty 4331
            strcpy(buffer, argv[i]);
4332
            Print(32, "(info) command line option \"%s\"\n", buffer);
33 pmbaty 4333
            result = Option(tree);
4334
            if (result == 0)
108 pmbaty 4335
              Print(2048, "ERROR \"%s\" is unknown command-line option\n",
4336
                  buffer);
33 pmbaty 4337
          }
4338
        }
4339
  }
4340
  display = tree->position;
4341
  initialized = 1;
4342
  move_actually_played = 0;
4343
/*
4344
 ************************************************************
4345
 *                                                          *
4346
 *  Next, read the crafty.rc/.craftyrc initialization file  *
4347
 *  and process the commands that need to be handled after  *
4348
 *  engine initialization has been completed.               *
4349
 *                                                          *
4350
 ************************************************************
4351
 */
4352
#if defined(UNIX)
4353
  input_stream = fopen(".craftyrc", "r");
4354
  if (!input_stream)
4355
    if ((pwd = getpwuid(getuid()))) {
4356
      sprintf(path, "%s/.craftyrc", pwd->pw_dir);
4357
      input_stream = fopen(path, "r");
4358
    }
4359
  if (input_stream)
4360
#else
108 pmbaty 4361
  sprintf(crafty_rc_file_spec, "%s/crafty.rc", rc_path);
33 pmbaty 4362
  if ((input_stream = fopen(crafty_rc_file_spec, "r")))
4363
#endif
4364
    while (1) {
108 pmbaty 4365
      readstat = Read(1, buffer);
33 pmbaty 4366
      if (readstat < 0)
4367
        break;
4368
      if (strstr(buffer, "path"))
4369
        continue;
4370
      result = Option(tree);
4371
      if (result == 0)
108 pmbaty 4372
        Print(2048, "ERROR \"%s\" is unknown rc-file option\n", buffer);
33 pmbaty 4373
      if (input_stream == stdin)
4374
        break;
4375
    }
4376
  input_stream = stdin;
4377
#if defined(UNIX)
4378
  if (xboard)
4379
    signal(SIGINT, SIG_IGN);
4380
#endif
108 pmbaty 4381
  Print(32, "\nCrafty v%s (%d cpus)\n\n", version, Max(smp_max_threads, 1));
33 pmbaty 4382
  NewGame(1);
4383
/*
4384
 ************************************************************
4385
 *                                                          *
4386
 *  Check to see if we can find a "crafty.cpf" personality  *
4387
 *  file which contains the default personality settings to *
4388
 *  be used unless overridden by the user.                  *
4389
 *                                                          *
4390
 ************************************************************
4391
 */
4392
  if ((personality = fopen("crafty.cpf", "r"))) {
4393
    fclose(personality);
108 pmbaty 4394
    Print(-1, "using default personality file \"crafty.cpf\"\n");
4395
    sprintf(buffer, "personality load crafty.cpf");
4396
    Option(tree);
33 pmbaty 4397
  }
4398
/*
4399
 ************************************************************
4400
 *                                                          *
4401
 *  This is the "main loop" that never ends unless the user *
4402
 *  tells Crafty to "quit".  We read commands/moves,        *
4403
 *  execute them, and when a move is entered we change      *
4404
 *  sides and execute a search to find a reply.  We repeat  *
4405
 *  this until the game ends or the opponent gets tired and *
4406
 *  tells us to terminate the engine.                       *
4407
 *                                                          *
4408
 *  Prompt user and read input string for processing.  As   *
4409
 *  long as Option() returns a non-zero value, continue     *
4410
 *  reading lines and calling Option().  When Option()      *
4411
 *  fails to recogize a command, then try InputMove() to    *
4412
 *  determine if this is a legal move.                      *
4413
 *                                                          *
4414
 *  While we are waiting on a move, we call Ponder() which  *
4415
 *  will find a move that it assumes the opponent will      *
4416
 *  make, and then it will call Iterate() to find a reply   *
4417
 *  for that move while we are waiting to see what is       *
4418
 *  actually played in the game.  Ponder() will return one  *
4419
 *  of the following "results" (stored in "presult"):       *
4420
 *                                                          *
4421
 *  (0) This indicates that Ponder() did not run, either    *
4422
 *      because pondering is disabled, or we are not in a   *
4423
 *      state where pondering is allowed, such as using the *
4424
 *      xboard "force" command to set up a game that will   *
4425
 *      be continued.                                       *
4426
 *                                                          *
4427
 *  (1) This indicates that we pondered a move, the         *
4428
 *      opponent actually played this move, and we were     *
4429
 *      able to complete a normal search for the proper     *
4430
 *      amount of time and have a move ready to use.        *
4431
 *                                                          *
4432
 *  (2) This indicates that we pondered a move, but that    *
4433
 *      for some reason, we did not need to continue until  *
4434
 *      time ran out.  For example, we found a forced mate  *
4435
 *      and further searching is not needed.  The search is *
4436
 *      not "in progress" but we still have a valid move    *
4437
 *      to play here.                                       *
4438
 *                                                          *
4439
 *  (3) We pondered, but the opponent either played a       *
4440
 *      different move, or else entered a command that      *
4441
 *      forces us to unwind the search so that the command  *
4442
 *      can be executed.  In this case, we will re-start    *
4443
 *      pondering, otherwise we make the correct move and   *
4444
 *      then start a normal search.                         *
4445
 *                                                          *
4446
 ************************************************************
4447
 */
4448
/*
4449
 ************************************************************
4450
 *                                                          *
4451
 *  From this point forward, we are in a state where it is  *
4452
 *                                                          *
4453
 *             O P P O N E N T ' S turn to move.            *
4454
 *                                                          *
4455
 ************************************************************
4456
 */
4457
  while (1) {
4458
    presult = 0;
4459
    do {
4460
      if (new_game)
4461
        NewGame(0);
4462
      opponent_start_time = ReadClock();
4463
      input_status = 0;
4464
      display = tree->position;
4465
      move = 0;
4466
      presult = 0;
108 pmbaty 4467
      if (xboard_done == 0 && xboard) {
4468
        xboard_done = 1;
4469
        Print(-1, "feature done=1\n");
33 pmbaty 4470
      }
4471
      do {
4472
        if (presult != 2)
4473
          presult = 0;
4474
        result = 0;
4475
        if (pong) {
108 pmbaty 4476
          Print(-1, "pong %d\n", pong);
33 pmbaty 4477
          pong = 0;
4478
        }
4479
        display = tree->position;
4480
        if (presult != 2 && (move_number != 1 || !game_wtm))
4481
          presult = Ponder(game_wtm);
4482
        if (presult == 1)
4483
          value = last_root_value;
4484
        else if (presult == 2)
4485
          value = ponder_value;
4486
        if (presult == 0 || presult == 2) {
4487
          if (!xboard) {
4488
            printf("%s(%d): ", SideToMove(game_wtm), move_number);
4489
            fflush(stdout);
4490
          }
108 pmbaty 4491
          readstat = Read(1, buffer);
33 pmbaty 4492
          if (log_file)
4493
            fprintf(log_file, "%s(%d): %s\n", SideToMove(game_wtm),
4494
                move_number, buffer);
4495
          if (readstat < 0 && input_stream == stdin) {
108 pmbaty 4496
            strcpy(buffer, "end");
4497
            Option(tree);
33 pmbaty 4498
          }
4499
        }
4500
        if (presult == 1)
4501
          break;
4502
        opponent_end_time = ReadClock();
4503
        result = Option(tree);
4504
        if (result == 0) {
108 pmbaty 4505
          nargs = ReadParse(buffer, args, " \t;");
4506
          move = InputMove(tree, 0, game_wtm, 0, 0, args[0]);
33 pmbaty 4507
          result = !move;
4508
          if (move)
4509
            last_pv.path[1] = 0;
4510
        } else {
4511
          input_status = 0;
4512
          if (result == 3)
4513
            presult = 0;
4514
        }
4515
      } while (result > 0);
4516
      if (presult == 1)
4517
        move = ponder_move;
4518
/*
4519
 ************************************************************
4520
 *                                                          *
4521
 *  We have a move.  Make the move (returned by InputMove)  *
4522
 *  and then change the side on move (wtm).                 *
4523
 *                                                          *
4524
 ************************************************************
4525
 */
4526
      if (result == 0) {
4527
        fseek(history_file, ((move_number - 1) * 2 + 1 - game_wtm) * 10,
4528
            SEEK_SET);
108 pmbaty 4529
        fprintf(history_file, "%9s\n", OutputMove(tree, 0, game_wtm, move));
4530
        MakeMoveRoot(tree, game_wtm, move);
4531
        tree->curmv[0] = move;
33 pmbaty 4532
        time_used_opponent = opponent_end_time - opponent_start_time;
4533
        if (!force)
4534
          Print(1, "              time used: %s\n",
4535
              DisplayTime(time_used_opponent));
108 pmbaty 4536
        TimeAdjust(game_wtm, time_used_opponent);
33 pmbaty 4537
        game_wtm = Flip(game_wtm);
4538
        if (game_wtm)
4539
          move_number++;
4540
        move_actually_played = 1;
4541
        last_opponent_move = move;
4542
/*
4543
 ************************************************************
4544
 *                                                          *
4545
 *  From this point forward, we are in a state where it is  *
4546
 *                                                          *
4547
 *              C R A F T Y ' S turn to move.               *
4548
 *                                                          *
4549
 ************************************************************
4550
 */
4551
/*
4552
 ************************************************************
4553
 *                                                          *
4554
 *  We have made the indicated move, before we do a search  *
4555
 *  we need to determine if the present position is a draw  *
4556
 *  by rule.  If so, Crafty allowed it to happen and must   *
4557
 *  be satisfied with a draw, so we will claim it and end   *
4558
 *  the current game.                                       *
4559
 *                                                          *
4560
 ************************************************************
4561
 */
108 pmbaty 4562
        if ((draw_type = Repeat3x(tree)) == 1) {
4563
          Print(1, "I claim a draw by 3-fold repetition.\n");
33 pmbaty 4564
          value = DrawScore(game_wtm);
4565
          if (xboard)
108 pmbaty 4566
            Print(-1, "1/2-1/2 {Drawn by 3-fold repetition}\n");
33 pmbaty 4567
        }
4568
        if (draw_type == 2) {
108 pmbaty 4569
          Print(1, "I claim a draw by the 50 move rule.\n");
33 pmbaty 4570
          value = DrawScore(game_wtm);
4571
          if (xboard)
108 pmbaty 4572
            Print(-1, "1/2-1/2 {Drawn by 50-move rule}\n");
33 pmbaty 4573
        }
4574
        if (Drawn(tree, last_search_value) == 2) {
108 pmbaty 4575
          Print(1, "I claim a draw due to insufficient material.\n");
33 pmbaty 4576
          if (xboard)
108 pmbaty 4577
            Print(-1, "1/2-1/2 {Insufficient material}\n");
33 pmbaty 4578
        }
4579
      } else {
4580
        tree->status[1] = tree->status[0];
4581
        presult = 0;
4582
      }
4583
#if defined(DEBUG)
4584
      ValidatePosition(tree, 0, move, "Main(1)");
4585
#endif
4586
    } while (force);
4587
/*
4588
 ************************************************************
4589
 *                                                          *
4590
 *  Now call Iterate() to compute a move for the current    *
4591
 *  position.  (Note this is not done if Ponder() has al-   *
4592
 *  ready computed a move.)                                 *
4593
 *                                                          *
4594
 ************************************************************
4595
 */
4596
    crafty_is_white = game_wtm;
4597
    if (presult == 2) {
4598
      if (From(ponder_move) == From(move) && To(ponder_move) == To(move)
4599
          && Piece(ponder_move) == Piece(move)
4600
          && Captured(ponder_move) == Captured(move)
4601
          && Promote(ponder_move) == Promote(move)) {
4602
        presult = 1;
4603
        if (!book_move)
4604
          predicted++;
4605
      } else
4606
        presult = 0;
4607
    }
4608
    ponder_move = 0;
4609
    thinking = 1;
4610
    if (presult != 1) {
108 pmbaty 4611
      strcpy(kibitz_text, "n/a");
33 pmbaty 4612
      last_pv.pathd = 0;
4613
      last_pv.pathl = 0;
4614
      display = tree->position;
4615
      value = Iterate(game_wtm, think, 0);
4616
    }
4617
/*
4618
 ************************************************************
4619
 *                                                          *
4620
 *  We've now completed a search and need to handle a       *
4621
 *  pending draw offer based on what the search found.      *
4622
 *                                                          *
4623
 *  When we get a draw offer, we make a note, but we do not *
4624
 *  accept it until after we have a chance to see what      *
4625
 *  happens after making the opponent's move that came with *
4626
 *  the draw offer.  If he played a blunder, we are not     *
4627
 *  going to mistakenly accept a draw when we are now       *
4628
 *  winning.  We make this decision to accept or decline    *
4629
 *  since we have completed as search and now have a real   *
4630
 *  score.                                                  *
4631
 *                                                          *
4632
 ************************************************************
4633
 */
4634
    if (draw_offer_pending) {
4635
      int drawsc = abs_draw_score;
4636
 
4637
      draw_offer_pending = 0;
4638
      if (move_number < 40 || !accept_draws)
4639
        drawsc = -300;
4640
      if (value <= drawsc && (tc_increment != 0 ||
4641
              tc_time_remaining[Flip(game_wtm)] >= 1000)) {
4642
        if (xboard)
108 pmbaty 4643
          Print(-1, "offer draw\n");
33 pmbaty 4644
        else {
108 pmbaty 4645
          Print(1, "Draw accepted.\n");
33 pmbaty 4646
          if (audible_alarm)
4647
            printf("%c", audible_alarm);
4648
          if (speech) {
108 pmbaty 4649
            strcpy(announce, "./speak ");
4650
            strcat(announce, "Drawaccept");
4651
            v = system(announce);
4652
            if (v != 0)
4653
              perror("main() system() error: ");
33 pmbaty 4654
          }
4655
        }
108 pmbaty 4656
        Print(-1, "1/2-1/2 {Draw agreed}\n");
4657
        strcpy(pgn_result, "1/2-1/2");
33 pmbaty 4658
      } else {
108 pmbaty 4659
        if (!xboard) {
4660
          Print(1, "Draw declined.\n");
4661
          if (speech) {
4662
            strcpy(announce, "./speak ");
4663
            strcat(announce, "Drawdecline");
4664
            v = system(announce);
4665
            if (v != 0)
4666
              perror("main() system() error: ");
4667
          }
4668
        }
33 pmbaty 4669
      }
4670
    }
4671
/*
4672
 ************************************************************
4673
 *                                                          *
4674
 *  If the last_pv.path is null, then we were either        *
4675
 *  checkmated or stalemated.  We need to determine which   *
4676
 *  and end the game appropriately.                         *
4677
 *                                                          *
4678
 *  If we do have a PV, we will also check to see if it     *
4679
 *  leads to mate and make the proper announcement if so.   *
4680
 *                                                          *
4681
 ************************************************************
4682
 */
4683
    last_pv = tree->pv[0];
4684
    last_value = value;
4685
    if (MateScore(last_value))
4686
      last_mate_score = last_value;
4687
    thinking = 0;
4688
    if (!last_pv.pathl) {
4689
      if (value == -MATE + 1) {
4690
        over = 1;
4691
        if (game_wtm) {
108 pmbaty 4692
          Print(-1, "0-1 {Black mates}\n");
4693
          strcpy(pgn_result, "0-1");
33 pmbaty 4694
        } else {
108 pmbaty 4695
          Print(-1, "1-0 {White mates}\n");
4696
          strcpy(pgn_result, "1-0");
33 pmbaty 4697
        }
4698
        if (speech) {
108 pmbaty 4699
          strcpy(announce, "./speak ");
4700
          strcat(announce, "Checkmate");
4701
          v = system(announce);
4702
          if (v != 0)
4703
            perror("main() system() error: ");
33 pmbaty 4704
        }
4705
      } else {
4706
        over = 1;
4707
        if (!xboard) {
108 pmbaty 4708
          Print(1, "stalemate\n");
33 pmbaty 4709
          if (speech) {
108 pmbaty 4710
            strcpy(announce, "./speak ");
4711
            strcat(announce, "Stalemate");
4712
            v = system(announce);
4713
            if (v != 0)
4714
              perror("main() system() error: ");
33 pmbaty 4715
          }
4716
        } else
108 pmbaty 4717
          Print(-1, "1/2-1/2 {stalemate}\n");
33 pmbaty 4718
      }
4719
    } else {
4720
      if (value > 32000 && value < MATE - 2) {
108 pmbaty 4721
        Print(1, "\nmate in %d moves.\n\n", (MATE - value) / 2);
33 pmbaty 4722
        Kibitz(1, game_wtm, 0, 0, (MATE - value) / 2, tree->nodes_searched, 0,
4723
            0, " ");
4724
      } else if (-value > 32000 && -value < MATE - 1) {
108 pmbaty 4725
        Print(1, "\nmated in %d moves.\n\n", (MATE + value) / 2);
33 pmbaty 4726
        Kibitz(1, game_wtm, 0, 0, -(MATE + value) / 2, tree->nodes_searched,
4727
            0, 0, " ");
4728
      }
4729
/*
4730
 ************************************************************
4731
 *                                                          *
4732
 *  See if we want to offer a draw based on the recent      *
4733
 *  scores that have been returned.  See resign.c for more  *
4734
 *  details, but this is where we offer a draw, or resign,  *
4735
 *  if appropriate.  This has nothing to do with claiming a *
4736
 *  draw by rule, which is done later.                      *
4737
 *                                                          *
4738
 ************************************************************
4739
 */
4740
      tree->status[MAXPLY] = tree->status[0];
4741
      ResignOrDraw(tree, value);
4742
/*
4743
 ************************************************************
4744
 *                                                          *
4745
 *  Now output the move chosen by the search, and the       *
4746
 *  "result" string if this move checkmates our opponent.   *
4747
 *                                                          *
4748
 ************************************************************
4749
 */
4750
      if (speech) {
108 pmbaty 4751
        char *moveptr = OutputMove(tree, 0, game_wtm, last_pv.path[1]);
33 pmbaty 4752
 
108 pmbaty 4753
        strcpy(announce, "./speak ");
4754
        strcat(announce, moveptr);
4755
        strcat(announce, " &");
4756
        v = system(announce);
4757
        if (v != 0)
4758
          perror("main() system() error: ");
33 pmbaty 4759
      }
4760
      if (!xboard && audible_alarm)
4761
        printf("%c", audible_alarm);
4762
      Print(1, "%s(%d): %s\n", SideToMove(game_wtm), move_number,
108 pmbaty 4763
          OutputMove(tree, 0, game_wtm, last_pv.path[1]));
33 pmbaty 4764
      if (xboard)
108 pmbaty 4765
        printf("move %s\n", OutputMove(tree, 0, game_wtm, last_pv.path[1]));
4766
      fflush(stdout);
33 pmbaty 4767
      if (value == MATE - 2) {
4768
        if (game_wtm) {
108 pmbaty 4769
          Print(-1, "1-0 {White mates}\n");
4770
          strcpy(pgn_result, "1-0");
33 pmbaty 4771
        } else {
108 pmbaty 4772
          Print(-1, "0-1 {Black mates}\n");
4773
          strcpy(pgn_result, "0-1");
33 pmbaty 4774
        }
4775
      }
4776
      time_used = program_end_time - program_start_time;
4777
      Print(1, "              time used: %s\n", DisplayTime(time_used));
108 pmbaty 4778
      TimeAdjust(game_wtm, time_used);
33 pmbaty 4779
      fseek(history_file, ((move_number - 1) * 2 + 1 - game_wtm) * 10,
4780
          SEEK_SET);
108 pmbaty 4781
      fprintf(history_file, "%9s\n", OutputMove(tree, 0, game_wtm,
4782
              last_pv.path[1]));
33 pmbaty 4783
      last_search_value = value;
4784
      if (kibitz) {
4785
        if (kibitz_depth)
4786
          Kibitz(2, game_wtm, kibitz_depth, end_time - start_time, value,
108 pmbaty 4787
              tree->nodes_searched, busy_percent, (int) tree->egtb_hits, // Pierre-Marie Baty -- added type cast
4788
              kibitz_text);
33 pmbaty 4789
        else
4790
          Kibitz(4, game_wtm, 0, 0, 0, 0, 0, 0, kibitz_text);
4791
      }
108 pmbaty 4792
      MakeMoveRoot(tree, game_wtm, last_pv.path[1]);
4793
      tree->curmv[0] = move;
33 pmbaty 4794
/*
4795
 ************************************************************
4796
 *                                                          *
4797
 *  From this point forward, we are in a state where it is  *
4798
 *                                                          *
4799
 *          O P P O N E N T ' S turn to move.               *
4800
 *                                                          *
4801
 *  We have made the indicated move, we need to determine   *
4802
 *  if the present position is a draw by rule.  If so, we   *
4803
 *  need to send the appropriate game result to xboard      *
4804
 *  and/or inform the operator/opponent.                    *
4805
 *                                                          *
4806
 ************************************************************
4807
 */
4808
      game_wtm = Flip(game_wtm);
4809
      if (game_wtm)
4810
        move_number++;
4811
      move_actually_played = 1;
108 pmbaty 4812
      if ((draw_type = Repeat3x(tree)) == 1) {
4813
        Print(1, "I claim a draw by 3-fold repetition after my move.\n");
33 pmbaty 4814
        if (xboard)
108 pmbaty 4815
          Print(-1, "1/2-1/2 {Drawn by 3-fold repetition}\n");
33 pmbaty 4816
        value = DrawScore(game_wtm);
4817
      }
4818
      if (draw_type == 2 && last_search_value < 32000) {
108 pmbaty 4819
        Print(1, "I claim a draw by the 50 move rule after my move.\n");
33 pmbaty 4820
        if (xboard)
108 pmbaty 4821
          Print(-1, "1/2-1/2 {Drawn by 50-move rule}\n");
33 pmbaty 4822
        value = DrawScore(game_wtm);
4823
      }
4824
      if (Drawn(tree, last_search_value) == 2) {
108 pmbaty 4825
        Print(1,
33 pmbaty 4826
            "I claim a draw due to insufficient material after my move.\n");
4827
        if (xboard)
108 pmbaty 4828
          Print(-1, "1/2-1/2 {Insufficient material}\n");
33 pmbaty 4829
      }
4830
#if !defined(TEST)
4831
      if (time_limit > 300)
4832
#endif
4833
        if (log_file)
4834
          DisplayChessBoard(log_file, tree->position);
4835
/*
4836
 ************************************************************
4837
 *                                                          *
4838
 *  Save the ponder_move from the current principal         *
4839
 *  variation, then shift it left two moves to use as the   *
4840
 *  starting point for the next search.  Adjust the depth   *
4841
 *  to start the next search at the right iteration.        *
4842
 *                                                          *
4843
 ************************************************************
4844
 */
4845
      if (last_pv.pathl > 2 && VerifyMove(tree, 0, game_wtm, last_pv.path[2])) {
4846
        ponder_move = last_pv.path[2];
4847
        for (i = 1; i < (int) last_pv.pathl - 2; i++)
4848
          last_pv.path[i] = last_pv.path[i + 2];
4849
        last_pv.pathl = (last_pv.pathl > 2) ? last_pv.pathl - 2 : 0;
4850
        last_pv.pathd -= 2;
4851
        if (last_pv.pathd > last_pv.pathl)
4852
          last_pv.pathd = last_pv.pathl;
4853
        if (last_pv.pathl == 0)
4854
          last_pv.pathd = 0;
108 pmbaty 4855
        tree->pv[0] = last_pv;
33 pmbaty 4856
      } else {
4857
        last_pv.pathd = 0;
4858
        last_pv.pathl = 0;
4859
        ponder_move = 0;
108 pmbaty 4860
        tree->pv[0] = last_pv;
33 pmbaty 4861
      }
4862
    }
4863
#if defined(TEST)
108 pmbaty 4864
    strcpy(buffer, "score");
33 pmbaty 4865
    Option(tree);
4866
#endif
4867
    if ((i = GameOver(game_wtm))) {
4868
      if (i == 1)
108 pmbaty 4869
        Print(-1, "1/2-1/2 {stalemate}\n");
33 pmbaty 4870
    }
4871
    if (book_move) {
4872
      moves_out_of_book = 0;
4873
      predicted++;
4874
      if (ponder_move)
108 pmbaty 4875
        sprintf(book_hint, "%s", OutputMove(tree, 0, game_wtm, ponder_move));
33 pmbaty 4876
    } else
4877
      moves_out_of_book++;
4878
#if defined(DEBUG)
4879
    ValidatePosition(tree, 0, last_pv.path[1], "Main(2)");
4880
#endif
4881
/*
4882
 ************************************************************
4883
 *                                                          *
4884
 *  Now execute LearnValue() to record the scores for the   *
4885
 *  first N searches out of book.  see learn.c for details  *
4886
 *  on how this is used and when.                           *
4887
 *                                                          *
4888
 ************************************************************
4889
 */
4890
    if (learning && moves_out_of_book && !learn_value)
4891
      LearnValue(last_value, last_pv.pathd + 2);
4892
    if (learn_positions_count < 63) {
4893
      learn_seekto[learn_positions_count] = book_learn_seekto;
4894
      learn_key[learn_positions_count] = book_learn_key;
4895
      learn_nmoves[learn_positions_count++] = book_learn_nmoves;
4896
    }
4897
    if (mode == tournament_mode) {
108 pmbaty 4898
      strcpy(buffer, "clock");
33 pmbaty 4899
      Option(tree);
108 pmbaty 4900
      Print(32, "if clocks are wrong, use 'settc' command to adjust them\n");
33 pmbaty 4901
    }
4902
  }
4903
}