2 * engineoutput.c - split-off backe-end from Engine output (PV) by HGM
4 * Author: Alessandro Scotti (Dec 2005)
6 * Copyright 2005 Alessandro Scotti
8 * ------------------------------------------------------------------------
10 * GNU XBoard is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at
13 * your option) any later version.
15 * GNU XBoard is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see http://www.gnu.org/licenses/. *
23 *------------------------------------------------------------------------
24 ** See the file ChangeLog for a revision history. */
26 #define SHOW_PONDERING
35 #else /* not STDC_HEADERS */
38 # else /* not HAVE_STRING_H */
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
46 #include "engineoutput.h"
61 // called by other front-end
62 void EngineOutputUpdate( FrontEndProgramStats * stats );
63 void OutputKibitz(int window, char *text);
65 // module back-end routines
66 static void VerifyDisplayMode();
67 static void UpdateControls( EngineOutputData * ed );
69 static int lastDepth[2] = { -1, -1 };
70 static int lastForwardMostMove[2] = { -1, -1 };
71 static int engineState[2] = { -1, -1 };
72 static char lastLine[2][MSG_SIZ];
73 static char header[MSG_SIZ];
76 static int scores[MAX_VAR], textEnd[MAX_VAR], curDepth[2], nrVariations[2];
78 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
79 void SetEngineState( int which, int state, char * state_data )
81 int x_which = 1 - which;
83 if( engineState[ which ] != state ) {
84 engineState[ which ] = state;
88 SetIcon( which, nStateIcon, nThinking );
89 if( engineState[ x_which ] == STATE_THINKING ) {
90 SetEngineState( x_which, STATE_IDLE, "" );
94 SetIcon( which, nStateIcon, nPondering );
97 SetIcon( which, nStateIcon, nAnalyzing );
100 SetIcon( which, nStateIcon, nClear );
105 if( state_data != 0 ) {
106 DoSetWindowText( which, nStateData, state_data );
110 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
111 void SetProgramStats( FrontEndProgramStats * stats ) // now directly called by back-end
114 int clearMemo = FALSE;
115 int which, depth, multi;
118 SetEngineState( 0, STATE_IDLE, "" );
119 SetEngineState( 1, STATE_IDLE, "" );
123 if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
124 return; // [HGM] kibitz: shut up engine if we are observing an ICS game
126 which = stats->which;
127 depth = stats->depth;
129 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
133 if( !EngineOutputDialogExists() ) {
141 ed.nodes = stats->nodes;
142 ed.score = stats->score;
143 ed.time = stats->time;
145 ed.hint = stats->hint;
146 ed.an_move_index = stats->an_move_index;
147 ed.an_move_count = stats->an_move_count;
149 /* Get target control. [HGM] this is moved to front end, which get them from a table */
151 ed.name = first.tidy;
154 ed.name = second.tidy;
157 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
158 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
163 /* Clear memo if needed */
164 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
168 if( lastForwardMostMove[which] != forwardMostMove ) {
173 DoClearMemo(which); nrVariations[which] = 0;
174 header[0] = NULLCHAR;
175 if(gameMode == AnalyzeMode && (multi = MultiPV(&first)) >= 0) {
176 snprintf(header, MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV setting = %d / more\n",
177 appData.whitePOV ? "white" : "mover", first.option[multi].value);
178 InsertIntoMemo( which, header, 0);
180 if(appData.ponderNextMove && lastLine[which][0]) {
181 InsertIntoMemo( which, lastLine[which], 0 );
182 InsertIntoMemo( which, "\n", 0 );
187 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
188 lastForwardMostMove[which] = forwardMostMove;
190 UpdateControls( &ed );
193 #define ENGINE_COLOR_WHITE 'w'
194 #define ENGINE_COLOR_BLACK 'b'
195 #define ENGINE_COLOR_UNKNOWN ' '
198 static char GetEngineColor( int which )
200 char result = ENGINE_COLOR_UNKNOWN;
202 if( which == 0 || which == 1 ) {
203 ChessProgramState * cps;
206 case MachinePlaysBlack:
207 case IcsPlayingBlack:
208 result = ENGINE_COLOR_BLACK;
210 case MachinePlaysWhite:
211 case IcsPlayingWhite:
212 result = ENGINE_COLOR_WHITE;
216 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
218 case TwoMachinesPlay:
219 cps = (which == 0) ? &first : &second;
220 result = cps->twoMachinesColor[0];
221 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
223 default: ; // does not happen, but suppresses pedantic warnings
231 static char GetActiveEngineColor()
233 char result = ENGINE_COLOR_UNKNOWN;
235 if( gameMode == TwoMachinesPlay ) {
236 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
243 static int IsEnginePondering( int which )
248 case MachinePlaysBlack:
249 case IcsPlayingBlack:
250 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
252 case MachinePlaysWhite:
253 case IcsPlayingWhite:
254 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
256 case TwoMachinesPlay:
257 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
258 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
261 default: ; // does not happen, but suppresses pedantic warnings
268 static void SetDisplayMode( int mode )
270 if( windowMode != mode ) {
273 ResizeWindowControls( mode );
278 static void VerifyDisplayMode()
282 /* Get proper mode for current game */
284 case IcsObserving: // [HGM] ICS analyze
285 if(!appData.icsEngineAnalyze) return;
288 case MachinePlaysWhite:
289 case MachinePlaysBlack:
292 case IcsPlayingWhite:
293 case IcsPlayingBlack:
294 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
296 case TwoMachinesPlay:
304 SetDisplayMode( mode );
307 // back end. Determine what icon to set in the color-icon field, and print it
308 void SetEngineColorIcon( int which )
310 char color = GetEngineColor(which);
313 if( color == ENGINE_COLOR_BLACK )
315 else if( color == ENGINE_COLOR_WHITE )
318 nicon = nColorUnknown;
320 SetIcon( which, nColorIcon, nicon );
323 #define MAX_NAME_LENGTH 32
325 // [HGM] multivar: sort Thinking Output within one depth on score
327 static int InsertionPoint( int len, EngineOutputData * ed )
329 int i, offs = 0, newScore = ed->score, n = ed->which;
331 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
332 newScore = 1e6; // info lines inserted on top
333 if(ed->depth != curDepth[n]) { // depth has changed
334 curDepth[n] = ed->depth;
335 nrVariations[n] = 0; // throw away everything we had
337 // loop through all lines. Note even / odd used for different panes
338 for(i=nrVariations[n]-2; i>=0; i-=2) {
339 // put new item behind those we haven't looked at
341 textEnd[i+n+2] = offs + len;
342 scores[i+n+2] = newScore;
343 if(newScore < scores[i+n]) break;
344 // if it had higher score as previous, move previous in stead
345 scores[i+n+2] = scores[i+n];
346 textEnd[i+n+2] = textEnd[i+n] + len;
350 textEnd[n] = offs + len;
351 scores[n] = newScore;
353 nrVariations[n] += 2;
354 return offs + (gameMode == AnalyzeMode)*strlen(header);
358 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
359 static void UpdateControls( EngineOutputData * ed )
361 // int isPondering = FALSE;
363 char s_label[MAX_NAME_LENGTH + 32];
365 char * name = ed->name;
368 if( name == 0 || *name == '\0' ) {
372 strncpy( s_label, name, MAX_NAME_LENGTH );
373 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
375 #ifdef SHOW_PONDERING
376 if( IsEnginePondering( ed->which ) ) {
381 if( ed->hint != 0 && *ed->hint != '\0' ) {
382 strncpy( buf, ed->hint, sizeof(buf) );
383 buf[sizeof(buf)-1] = '\0';
385 else if( ed->pv != 0 && *ed->pv != '\0' ) {
386 char * sep = strchr( ed->pv, ' ' );
387 int buflen = sizeof(buf);
390 buflen = sep - ed->pv + 1;
391 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
394 strncpy( buf, ed->pv, buflen );
395 buf[ buflen-1 ] = '\0';
398 SetEngineState( ed->which, STATE_PONDERING, buf );
400 else if( gameMode == TwoMachinesPlay ) {
401 SetEngineState( ed->which, STATE_THINKING, "" );
403 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
404 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
406 int time_secs = ed->time / 100;
407 int time_mins = time_secs / 60;
411 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
414 strncpy( mov, ed->hint, sizeof(mov) );
415 mov[ sizeof(mov)-1 ] = '\0';
417 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
418 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
421 SetEngineState( ed->which, STATE_ANALYZING, buf );
424 SetEngineState( ed->which, STATE_IDLE, "" );
428 DoSetWindowText( ed->which, nLabel, s_label );
432 if( ed->time > 0 && ed->nodes > 0 ) {
433 unsigned long nps_100 = ed->nodes / ed->time;
435 if( nps_100 < 100000 ) {
436 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %lu", nps_100 * 100 );
439 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %.1fk", nps_100 / 10.0 );
443 DoSetWindowText( ed->which, nLabelNPS, s_label );
446 if( ed->pv != 0 && *ed->pv != '\0' ) {
452 int time_secs = ed->time / 100;
453 int time_cent = ed->time % 100;
456 if( ed->nodes < 1000000 ) {
457 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display, ed->nodes );
460 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.1fM", u64ToDouble(ed->nodes) / 1000000.0 );
464 h = (gameMode == AnalyzeMode && appData.whitePOV && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
466 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f", h / 100.0 );
469 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "%.2f", h / 100.0 );
473 snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
475 /* Put all together... */
476 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
477 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
479 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
482 buflen = strlen(buf);
484 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
486 buf[ sizeof(buf) - 3 ] = '\0';
488 strcat( buf + buflen, "\r\n" );
491 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
492 strncpy(lastLine[ed->which], buf, MSG_SIZ);
496 SetEngineColorIcon( ed->which );
499 // [HGM] kibitz: write kibitz line; split window for it if necessary
500 void OutputKibitz(int window, char *text)
502 static int currentLineEnd[2];
504 if(!EngineOutputIsUp()) return;
505 if(!opponentKibitzes) { // on first kibitz of game, clear memos
506 DoClearMemo(1); currentLineEnd[1] = 0;
507 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
509 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
511 strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
512 if(gameMode == IcsObserving) {
513 DoSetWindowText(0, nLabel, gameInfo.white);
514 SetIcon( 0, nColorIcon, nColorWhite);
515 SetIcon( 0, nStateIcon, nClear);
517 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
518 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
519 SetIcon( 1, nStateIcon, nClear);
520 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
521 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
522 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
523 currentLineEnd[window-1] = where + strlen(text);