=head1 NAME

PolyGlot -  Winboard protocol to UCI protocol adapter
         -  book engine for Polyglot books 
         -  a collection of utilities for creating and analyzing opening books
         -  a utility for analyzing epd files
         -  a perft counter

=head1 SYNOPSIS

polyglot [configfile] [-noini] [-ec engine] [-ed enginedirectory] [-en enginename] [-log true/false] [-lf logfile] [-pg <name>=<value>]* [-uci <name>=<value>]*

polyglot make-book [-pgn inputfile] [-bin outputfile] [-max-ply ply] [-min-game games] [-min-score score] [-only-white] [-only-black] [-uniform]

polyglot merge-book -in1 inputfile1 -in2 inputfile2 [-out outputfile]

polyglot info-book [-bin inputfile] [-exact]

polyglot dump-book [-bin inputfile] -color color [-out outputfile]

polyglot [configfile] epd-test [engineoptions] [-epd inputfile] [-min-depth depth] [-max-depth depth] [-max-time time] [-depth-delta delta] 

polyglot perft [-fen fen] [-max-depth depth]

=head1 DESCRIPTION

=head2 PolyGlot as adapter and book engine

PolyGlot is a "UCI adapter".  It connects a GUI interface (such as
XBoard, Winboard, Arena or Chessbase) to a UCI chess engine.

By specifying an opening book (in PolyGlot book format) chess engines can
transparently use such books.

PolyGlot understands the two main GUI protocols: UCI and
xboard. Normally the protocol will be auto detected but this can be
overridden in the configuration file.

In xboard mode PolyGlot fully translates between the xboard and UCI protocols.
In addition it tries to solve known problems with other adapters.
For instance, it detects and reports draws by fifty-move rule,
repetition, etc ... It also supports Chess960.

When in UCI mode PolyGlot mostly passes commands from the GUI
to the engine and vice versa, except that it will play book moves on
behalf of the engine when the occasion arises.

The engine options are exported as UCI options in UCI
mode and as "feature option=" commands in xboard mode. The latter form
an extension of the xboard protocol as defined by H.G. Muller.

Options which normally appear in the [PolyGlot] section of the
config file (see below) are exported as options with their name prefixed
by "Polyglot". This makes it easy to filter them in the GUI.

NOTE: Not all options are exported, only those that make sense in the
given mode.

=head2 Book making utilities

PolyGlot supports the "PolyGlot opening book format". This is the
defacto standard non-proprietary opening book format. It is fully documented
here

http://hgm.nubati.net/book_format.html

Roughly speaking a PolyGlot opening book is a collection of triples
(position, move, weight). A "position" is represented by a 64-bit
Zobrist hash key. The weight is proportional to the probability the move should
be played. 

Other opening book formats such as ChessBase's .ctg format and Arena's
.abk format are undocumented and proprietary. They can only be used 
by their own GUIs. 

PolyGlot can compile a pgn file into a binary PolyGlot book and furthermore
it can merge two such binary books into a third one.

PolyGlot can also extract some useful information from PolyGlot books. The utility
"dump-book" dumps the "lines" in a book for a given color. By definition
a line is a sequence of moves (from the starting position) in which
the given color makes only book moves and the other color makes 
arbitrary moves (i.e. not necessarily book moves).

Since a PolyGlot book is built up from positions and not lines there
may be (and there usually are) many positions in the book that are not
on a "line" as defined in the previous paragraph. It is convenient
to call such positions "isolated" positions. The utility "info-book"
counts such isolated positions.

Some of the isolated positions are provably unreachable and they
could in principle be deleted from the book. For example if a book
contains only the move "e4" in the starting position but also the
position after "d4 d5" then this last position is provably unreachable
since it requires white to make a non-book move when a book move is
available. Such situations arise frequently from the priority rules
in merging books.

Unfortunately not all isolated positions are provably unreachable and
it is difficult to identify the latter. If invoked with "-exact" the
utility info-book will attempt to count the isolated positions which
require a player to make a non-book move when a book move is available.
Due to the possibility of transpositions this is not a fool proof method.

=head2 Epd test mode

In epd test mode, PolyGlot will search positions in an epd file and
record the number of times the right best move was found.  The
arguments specify when to stop the search in any given position.

=head2 Perft counts

A perft count is the number of legal move sequence in a given position
up to a given depth. PolyGlot can perform such perft counts. It
is however much slower than other more dedicated programs.

=head1 OPTIONS

When PolyGlot is invoked as an adapter of in epd-test mode it gets its
options from a config file and then from the command line.  The
default config file is "polyglot.ini" but an alternative one may be
optionally included as first argument. The config file format is
described below.

The following engine options may be specified on the command line.

=over 4
    
=item B<-noini> 

Do not use a config file, even if one was specified on the command line.

=item B<-pg>

The argument is a string of the form <name>=<value>. This option will
set the Polyglot option <name> to <value>. 

=item B<-uci>

The argument is a string of the form <name>=<value>. This option will
set the engine option <name> to <value>. 

=item B<-ec> 

This is an alias for -pg "EngineCommand=<value>"

=item B<-ed>

This is an alias for -pg "EngineDir=<value>"

=item B<-en>

This is an alias for -pg "EngineName=<value>"

=item B<-log> (default: false)

This is an alias for -pg "Log=<value>"

=item B<-lf> (default: "polyglot.log")

This is an alias for -pg "LogFile=<value>".

=item B<-wb> (default: "true")

This is an alias for -pg "OnlyWbOptions=<value>".

=back

When invoked as

=head2 polyglot make-book

PolyGlot supports the following options

=over 4

=item B<-pgn> (default: "book.pgn")

Input file in pgn format. 

=item B<-bin> (default: "book.bin")

Output file in PolyGlot format. 

=item B<-max-ply> (default: 1024)

Specifies the maximum ply-depth of lines included in the book.

=item B<-min-game> (default: 3)

Specifies the minimum number of games that have to contain this move for it to be included in the book.

=item B<-min-score> (default: 0.0)

Specifies the minimum score (or weight) this move should have received for 
it to  be included in the book. The score is 2*(wins)+(draws), globally scaled
to fit into 16 bits. 

=item B<-only-white>

Include only moves for white in the book.

=item B<-only-black>
    
Include only moves for black in the book.

=item B<-uniform>
    
Set all weights to 1. In other words, all moves will be selected with 
equal probability. 

=back

When invoked
as

=head2 polyglot merge-book

PolyGlot supports the following options

=over 4

=item B<-in1>
    
First input file (in PolyGlot book format).

=item B<-in2>
    
Second input file (in PolyGlot book format).

=item B<-out> (default: out.bin)
    
Output file (in PolyGlot book format).

=back

Input files are not symmetrical, "in1" has priority over "in2". In other
words when a position occurs both in "in1" and "in2" only the
moves and weights from "in1" will be retained in "out".

When invoked
as

=head2 polyglot dump-book

PolyGlot supports the following options

=over 4

=item B<-bin> (default: book.bin)

Input file in PolyGlot book format.

=item B<-color> 

The color for whom to generate the lines.

=item B<-out> (default: book_<color>.txt)

The name of the output file.

=back

When invoked
as

=head2 polyglot info-book

PolyGlot supports the following options

=over 4

=item B<-bin> (default: book.bin)

Input file in PolyGlot book format.

=item B<-exact> 

Attempt to count the provably unreachable positions among the isolated ones.
Note that this takes a very long time. 

=back

When invoked as

=head2 polyglot epd-test

(possibly with a config file as first argument) PolyGlot supports
besides the generic options described above the following additional
options.

=over 4

=item B<-max-depth> (default: 63)

Unconditionally stop the search when this depth has
been reached.

=item B<-max-time> (default: 5.0)

Unconditionally stop the seach after this amount of time.

=item B<-depth-delta> (default: 3)

Stop the search if the solution as been found and the best move has
been constant for this many depths, on condition that the mininal
depth and minimal time have been reached.

=item B<-min-depth> (default: 8)

Minimal search depth when the search is stopped using "-depth-delta".

=item B<-min-time> (default: 1.0)

Minimal search time when the search is stopped using "-depth-delta".


=back

When invoked as

=head2 polyglot perft

PolyGlot supports the following
options

=over 4

=item B<-fen> (default: starting position) 

Fen at which to start searching.

=item B<-max-depth> (default: 1) 

Maximum depth to search.

=back

=head1 CONFIG FILE FORMAT

There should be a different config file for each engine.  

The config file is in the traditional INI format.  

    [PolyGLot]
    option = value
    ...
    [Engine]
    option = value
    ...

The characters "#" and ";" serve as comment characters. 

Initial and final white space is stripped from option names and
values. If you need to use characters which have a special meaning to
PolyGlot (these are ';#[]=\') you should quote them by preceding them
with '\'. "Quoting" other characters in this way has no effect. In
particular the use of '\' as a path separator in windows should
normally not affected.

=head2 [PolyGlot] section

This section is used by PolyGlot only.  The engine is unaware of these
options.  The list of available options is detailed below.

=over 4

=item B<EngineName> (default: UCI name)

This is the name that will appear in the GUI.  It is
cosmetic only.  You can use different names for tweaked versions of
the same engine.

=item B<EngineDir> (default: ".")

Full path of the directory where the engine is installed.  You can use
"." (without the quotes) if you know that PolyGlot will be launched in
the engine directory or the engine is in the "path" and does not need
any data file.

=item B<EngineCommand>

Put here the name of the engine executable file.  You can also add
command-line arguments.  Path searching is used and the current
directory will be "EngineDir". On Linux the EngineCommand is passed
to wordexp so that shell quoting rules and expansions are applied.
On Windows the EngineCommand is simply passed to CreateProcess which
does its own shell like processing.

=item B<SettingsDir> (default: $HOME/.polyglot on Linux; ".\_PG" on Windows)

The directory where ini files are stored for engines that are started with
-noini. Such ini files may be created by pushing the "Save" button in the
Engine settings dialog in WB/XB 4.4.0 and higher. As a special exception
(for WB/XB 4.4.0 compatibility) this directory is also used in case
PolyGlot is started with config files named "polyglot_1st.ini" or
"polyglot_2nd.ini".

=item B<Log> (default: false)

Whether PolyGlot should log all transactions with the interface and
the engine.  This should be necessary only to locate problems.

=item B<LogFile> (default: polyglot.log)

The name of the log file.  Note that it is put where PolyGlot was
launched from, not into the engine directory.

WARNING: Log files are not cleared between sessions, and can become
very large.  It is safe to remove them though.

=item B<Resign> (default: false)

Set this to "true" if you want PolyGlot to resign on behalf of the
engine.

NOTE: Some engines display buggy scores from time to time although the
best move is correct.  Use this option only if you know what you are
doing (e.g. you always check the final position of games).

=item B<ResignMoves> (default: 3)

Number of consecutive moves with "resign" score (see below) before
PolyGlot resigns for the engine.  Positions with only one legal move
are ignored.

=item B<ResignScore> (default: 600)

This is the score in centipawns that will trigger resign "counting".

=item B<QueenNeverResigns> (default: true)

Suppresses resigning irrespective of the score,
as long as the engine still has a Queen (hoping for a perpetual).

=item B<HandleDraws> (default: false)

When set Polyglot can accept draw offers on behalf of the engine,
when the last reported engine score is below Polyglot's contempt.

=item B<ContemptScore> (default: 30)

This is the score in centipawns that the engine must minimally see
in its own disadvantage before Polyglot will accept a draw offer.

=item B<ShowPonder> (default: true)

Show search information during engine pondering.  Turning this off
might be better for interactive use in some interfaces.

=item B<ShowTbHits> (default: false)

Reports extra info on selective depth, search speed (knps) and
tablebase hits at the beginning of the PV field in the thinking output
of WinBoard protocol, just before the real PV.

=item B<ScoreWhite> (default: true)

Report score from white's point of view in xboard mode. (Wrecks XBoard...).

=item B<KibitzMove> (default: false)

Whether to kibitz when playing a move.

=item B<KibitzPV> (default: false)

Whether to kibitz when the PV is changed (new iteration or new best move).

=item B<KibitzCommand> (default: "tellall")

xboard command to use for kibitzing, normally "tellall" for kibitzing
or "tellothers" for whispering.

=item B<KibitzDelay> (default: 5)

How many seconds to wait before starting kibitzing.  This has an
effect only if "KibitzPV" is selected, move kibitzes are always sent
regardless of the delay.

=item B<KibitzInterval> (default: 0)

This is another form of throttling. PolyGlot will usually wait this
many seconds before doing the next kibitz. 

=item B<UCI> (default: false)

If true PolyGlot will not understand xboard commands. 

=item B<MateScore> (default: 10000)

Mate score reported to GUI when in xboard mode.

=item B<Book> (default: false)

Indicates whether a PolyGlot book should be used.  This has no effect
on the engine own book (which can be controlled with the UCI option
"OwnBook" in the [Engine] section).  In particular, it is possible to
use both a PolyGlot book and an engine book.  In that case, the engine
book will be used whenever PolyGlot is out of book.  Remember that
PolyGlot is unaware of whether the engine is itself using a book or
not.

=item B<BookFile> (default: book.bin)

The name of the (binary) book file.  Note that PolyGlot will look for
it in the directory it was launched from, not in the engine directory.
Of course, full path can be used in which case the current directory
does not matter.

=item B<BookRandom> (default: true)

Select moves according to their weights in the book. If false the move
with the highest weight is selected. 

=item B<BookLearn> (default: false)

Switches on updating of the 'learn fields' of the opening book,
which for each move record the number of times the move was played,
and the number of half-points scored with it.
It also switches on making use of this information already in the book,
for altering the move-selection probabilities from those
specified by the book weights.
(Rarely played moves will be favored,
moves with a significant poor performance will be shunned.)
This option must be set before the book is specified,
or the updating of the learn info will not work!

=item B<BookDepth> (default: 256)

Stop using the book after this number of moves. 

=item B<BookTreshold> (default: 5)

Do not play moves with a weight (probability) lower than this (in per mil). 

=item B<UseNice> (default: false)

Run the engine at nice level 5, or "NiceValue" if it set.  On some
operating systems it may be necessary to run the engine at lower
priority for it to be responsive to commands from PolyGlot while
searching.

=item B<NiceValue> (default: 5)

Nice levels go from -20 to 20 with 20 being the lowest priority.
On Unix only root can set negative nice levels. On Windows the standard
Win32 priority levels are mapped in a sensible way to Unix nice levels.


=item B<Affinity> (default: -1)

This a bit vector in which each bit represents the processors that a
process is allowed to run on. This option works only on Windows. 

=item B<STFudge> (default: 20)

PolyGlot will translate "st x" as "go movetime 1000*x-STFudge". 
The rationale is that in the UCI specification the argument of movetime
is defined as the exact search time whereas the argument of the
st command is only an upperbound. 

=item B<OnlyWbOptions> (default: true)

If true then PolyGlot restricts the options it sends to those that
are potentially useful for WinBoard. 

=back

=head2 Work arounds 

Work arounds are identical to options except that they should be used
only when necessary.  Their purpose is to try to hide problems with
various software (not just engines).  

IMPORTANT: Any of these work arounds might be removed in future
versions of PolyGlot.  You are strongly recommended to contact the
author of faulty software and truly fix the problem.

PolyGlot supports the following work arounds:

=over 4

=item B<UCIVersion> (default: 2)

The default value of 2 corresponds to UCI+.  Use 1 to select plain
UCI for engines that have problems with UCI+.

=item B<CanPonder> (default: false)

PolyGlot now conforms to the documented UCI behaviour: the engine will
be allowed to ponder only if it (the engine) declares the "Ponder" UCI
option.  However some engines which can actually ponder do not declare
the option.  This work around lets PolyGlot know that they can ponder.

=item B<SyncStop> (default: false)

When a ponder miss occurs, Polyglot interrupts the engine and
IMMEDIATELY launches a new search.  While there should be no problem
with this, some engines seem confused and corrupt their search board.
"SyncStop" forces PolyGlot to wait for the (now useless) ponder search
to finish before launching the new search.

=item B<PromoteWorkAround> (default: false)

Some engines do not specify a promotion piece, e.g. they send "e7e8"
instead of the correct "e7e8q".  This work around enables the
incorrect form (and of course promotes into a queen).

=item B<RepeatPV> (default: true)

When true, PolyGlot repeats the last pv string (which also contains
score,depth and time usage) it got from the engine. Some engines
however do not send a new pv string just before sending the move.
In that case the output of PolyGlot would be inconsistent. 
When RepeatPV is false PolyGlot does not repeat the last pv string.
Due to the way kibitzing is implemented, KibitzMove is disabled in that case. 

=back


=head2 [Engine] section

This section contains engine UCI options.  PolyGlot does not
understand them, but sends the information to the engine at startup
(converted to UCI form).  You can add any UCI option that makes sense
to the engine (not just the common options about hash-table size and
tablebases).

NOTE: use INI syntax, not UCI.  For example "OwnBook = true" is
correct.  It will be replaced by PolyGlot with "setoption name OwnBook
value true" at engine startup.

Standard UCI options are 

    Hash 
    NalimovPath
    NalimovCache
    GaviotaPath
    GaviotaCache
    SyzygyPath
    OwnBook

Hidden options like "Ponder" or "UCI_xxx" are automatic
and should not be put in an INI file.

The other options are engine-specific.  Check their name using a UCI
GUI or launch the engine in a console and type "uci".

=head1 EXAMPLES

Running the UCI engine "fruit" under xboard 4.3.15 and later (this invokes PolyGlot internally).

    xboard -fcp fruit -fUCI

An explicit command line for using the UCI engine "fruit" with logging enabled (this works also with older versions of xboard).

    xboard -fcp "polyglot -noini -log true -ec fruit"

The equivalent config file would be:

    [PolyGlot]
    EngineCommand = fruit
    Log = true
    [Engine]

Compile "games.pgn" into a book "book.bin" retaining all lines of at
most 30 plies.

    polyglot make-book -pgn games.pgn -bin book.bin -max-ply 30

Merge books "w1.bin" and "w2.bin" into a book "w.bin".

    polyglot merge-book -in1 w1.bin -in2 w2.bin -out w.bin

Inspect lines for white in "w.bin"

    polyglot dump-book -bin w.bin -color white -out w_white.txt

Test epd file "test.epd" with a (maximum) search time of 7 minutes per position

    polyglot epd-test -epd test.epd -max-time 420

=head1 EXIT STATUS

PolyGlot always returns 0 on exit. 

=head1 AUTHORS

Main author: Fabien Letouzey<fabien_letouzey(at)hotmail.com>

Native Windows port:  Huang Chen<webmaster@elephantbase.net> ("Morning Yellow")

Various enhancements: Fonzy Bleumers<match(at)geenvis.net>

UCI port and implementation of new WB protocol: Michel Van den Bergh
<michel.vandenbergh(at)uhasselt.be>



=head1 SEE ALSO

xboard(6)
