Return all candidate moves from the probe.
[stockfish] / src / main.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
5   Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
6
7   Stockfish is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   Stockfish is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <iostream>
22 #include <thread>
23
24 #include "bitboard.h"
25 #include "position.h"
26 #include "search.h"
27 #include "thread.h"
28 #include "tt.h"
29 #include "uci.h"
30 #include "syzygy/tbprobe.h"
31
32 #include <grpc/grpc.h>
33 #include <grpc++/server.h>
34 #include <grpc++/server_builder.h>
35 #include "hashprobe.grpc.pb.h"
36
37 using grpc::Server;
38 using grpc::ServerBuilder;
39 using grpc::ServerContext;
40 using grpc::Status;
41 using grpc::StatusCode;
42
43 class HashProbeImpl final : public HashProbe::Service {
44 public:
45         Status Probe(ServerContext* context,
46                      const HashProbeRequest* request,
47                      HashProbeResponse *response) {
48                 Position pos(request->fen(), /*isChess960=*/false, Threads.main());
49                 if (!pos.pos_is_ok()) {
50                         return Status(StatusCode::INVALID_ARGUMENT, "Invalid FEN");
51                 }
52
53                 bool invert = (pos.side_to_move() == BLACK);
54
55                 HashProbeMove *root_move = response->add_move();
56                 root_move->set_move(MOVE_NONE);
57                 ProbeMove(pos.key(), invert, root_move);
58
59                 MoveList<LEGAL> moves(pos);
60                 for (const ExtMove* em = moves.begin(); em != moves.end(); ++em) {
61                         HashProbeMove *move = response->add_move();
62                         move->set_move(em->move);
63                         ProbeMove(pos.key_after(em->move), !invert, move);
64                 }
65
66                 return Status::OK;
67         }
68
69         void ProbeMove(const int64_t key, bool invert, HashProbeMove* response) {
70                 bool found;
71                 TTEntry *entry = TT.probe(key, found);
72                 response->set_found(found);
73                 if (found) {
74                         Value value = entry->value();
75                         Value eval = entry->eval();
76                         Bound bound = entry->bound();
77
78                         if (invert) {
79                                 value = -value;
80                                 eval = -eval;
81                                 if (bound == BOUND_UPPER) {
82                                         bound = BOUND_LOWER;
83                                 } else if (bound == BOUND_LOWER) {
84                                         bound = BOUND_UPPER;
85                                 }
86                         }
87
88                         response->set_pv_move(entry->move());
89                         response->set_depth(entry->depth());
90                         response->set_eval(eval);
91                         response->set_value(value);
92                         response->set_bound(HashProbeMove::ValueBound(bound));
93                 }
94         }
95 };
96         
97 void rpc_thread()
98 {
99         std::string server_address("0.0.0.0:50051");
100         HashProbeImpl service;
101
102         ServerBuilder builder;
103         builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
104         builder.RegisterService(&service);
105         std::unique_ptr<Server> server(builder.BuildAndStart());
106         std::cout << "Server listening on " << server_address << std::endl;
107         server->Wait();
108 }
109
110 namespace PSQT {
111   void init();
112 }
113
114 int main(int argc, char* argv[]) {
115
116   std::cout << engine_info() << std::endl;
117
118   UCI::init(Options);
119   PSQT::init();
120   Bitboards::init();
121   Position::init();
122   Bitbases::init();
123   Search::init();
124   Pawns::init();
125   Threads.set(Options["Threads"]);
126   Search::clear(); // After threads are up
127
128   std::thread(&rpc_thread).detach();
129
130   UCI::loop(argc, argv);
131
132   Threads.set(0);
133   return 0;
134 }