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
39 #else /* not STDC_HEADERS */
42 # else /* not HAVE_STRING_H */
44 # endif /* not HAVE_STRING_H */
45 #endif /* not STDC_HEADERS */
50 #include "engineoutput.h"
65 // called by other front-end
66 void EngineOutputUpdate( FrontEndProgramStats * stats );
67 void OutputKibitz(int window, char *text);
69 // module back-end routines
70 static void VerifyDisplayMode();
71 static void UpdateControls( EngineOutputData * ed );
73 static int lastDepth[2] = { -1, -1 };
74 static int lastForwardMostMove[2] = { -1, -1 };
75 static int engineState[2] = { -1, -1 };
76 static char lastLine[2][MSG_SIZ];
79 static int scores[MAX_VAR], textEnd[MAX_VAR], curDepth[2], nrVariations[2];
81 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
82 void SetEngineState( int which, int state, char * state_data )
84 int x_which = 1 - which;
86 if( engineState[ which ] != state ) {
87 engineState[ which ] = state;
91 SetIcon( which, nStateIcon, nThinking );
92 if( engineState[ x_which ] == STATE_THINKING ) {
93 SetEngineState( x_which, STATE_IDLE, "" );
97 SetIcon( which, nStateIcon, nPondering );
100 SetIcon( which, nStateIcon, nAnalyzing );
103 SetIcon( which, nStateIcon, nClear );
108 if( state_data != 0 ) {
109 DoSetWindowText( which, nStateData, state_data );
113 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
114 void SetProgramStats( FrontEndProgramStats * stats ) // now directly called by back-end
117 int clearMemo = FALSE;
122 SetEngineState( 0, STATE_IDLE, "" );
123 SetEngineState( 1, STATE_IDLE, "" );
127 if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
128 return; // [HGM] kibitz: shut up engine if we are observing an ICS game
130 which = stats->which;
131 depth = stats->depth;
133 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
137 if( !EngineOutputDialogExists() ) {
145 ed.nodes = stats->nodes;
146 ed.score = stats->score;
147 ed.time = stats->time;
149 ed.hint = stats->hint;
150 ed.an_move_index = stats->an_move_index;
151 ed.an_move_count = stats->an_move_count;
153 /* Get target control. [HGM] this is moved to front end, which get them from a table */
155 ed.name = first.tidy;
158 ed.name = second.tidy;
161 /* Clear memo if needed */
162 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1) ) {
166 if( lastForwardMostMove[which] != forwardMostMove ) {
171 DoClearMemo(which); nrVariations[which] = 0;
172 if(appData.ponderNextMove && lastLine[which][0]) {
173 InsertIntoMemo( which, lastLine[which], 0 );
174 InsertIntoMemo( which, "\n", 0 );
179 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
180 lastForwardMostMove[which] = forwardMostMove;
182 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
183 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
188 UpdateControls( &ed );
191 #define ENGINE_COLOR_WHITE 'w'
192 #define ENGINE_COLOR_BLACK 'b'
193 #define ENGINE_COLOR_UNKNOWN ' '
196 static char GetEngineColor( int which )
198 char result = ENGINE_COLOR_UNKNOWN;
200 if( which == 0 || which == 1 ) {
201 ChessProgramState * cps;
204 case MachinePlaysBlack:
205 case IcsPlayingBlack:
206 result = ENGINE_COLOR_BLACK;
208 case MachinePlaysWhite:
209 case IcsPlayingWhite:
210 result = ENGINE_COLOR_WHITE;
214 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
216 case TwoMachinesPlay:
217 cps = (which == 0) ? &first : &second;
218 result = cps->twoMachinesColor[0];
219 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
221 default: ; // does not happen, but suppresses pedantic warnings
229 static char GetActiveEngineColor()
231 char result = ENGINE_COLOR_UNKNOWN;
233 if( gameMode == TwoMachinesPlay ) {
234 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
241 static int IsEnginePondering( int which )
246 case MachinePlaysBlack:
247 case IcsPlayingBlack:
248 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
250 case MachinePlaysWhite:
251 case IcsPlayingWhite:
252 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
254 case TwoMachinesPlay:
255 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
256 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
259 default: ; // does not happen, but suppresses pedantic warnings
266 static void SetDisplayMode( int mode )
268 if( windowMode != mode ) {
271 ResizeWindowControls( mode );
276 static void VerifyDisplayMode()
280 /* Get proper mode for current game */
282 case IcsObserving: // [HGM] ICS analyze
283 if(!appData.icsEngineAnalyze) return;
286 case MachinePlaysWhite:
287 case MachinePlaysBlack:
290 case IcsPlayingWhite:
291 case IcsPlayingBlack:
292 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
294 case TwoMachinesPlay:
302 SetDisplayMode( mode );
305 // back end. Determine what icon to set in the color-icon field, and print it
306 void SetEngineColorIcon( int which )
308 char color = GetEngineColor(which);
311 if( color == ENGINE_COLOR_BLACK )
313 else if( color == ENGINE_COLOR_WHITE )
316 nicon = nColorUnknown;
318 SetIcon( which, nColorIcon, nicon );
321 #define MAX_NAME_LENGTH 32
323 // [HGM] multivar: sort Thinking Output within one depth on score
325 static int InsertionPoint( int len, EngineOutputData * ed )
327 int i, offs = 0, newScore = ed->score, n = ed->which;
329 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
330 newScore = 1e6; // info lines inserted on top
331 if(ed->depth != curDepth[n]) { // depth has changed
332 curDepth[n] = ed->depth;
333 nrVariations[n] = 0; // throw away everything we had
335 // loop through all lines. Note even / odd used for different panes
336 for(i=nrVariations[n]-2; i>=0; i-=2) {
337 // put new item behind those we haven't looked at
339 textEnd[i+n+2] = offs + len;
340 scores[i+n+2] = newScore;
341 if(newScore < scores[i+n]) break;
342 // if it had higher score as previous, move previous in stead
343 scores[i+n+2] = scores[i+n];
344 textEnd[i+n+2] = textEnd[i+n] + len;
348 textEnd[n] = offs + len;
349 scores[n] = newScore;
351 nrVariations[n] += 2;
356 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
357 static void UpdateControls( EngineOutputData * ed )
359 // int isPondering = FALSE;
361 char s_label[MAX_NAME_LENGTH + 32];
363 char * name = ed->name;
366 if( name == 0 || *name == '\0' ) {
370 strncpy( s_label, name, MAX_NAME_LENGTH );
371 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
373 #ifdef SHOW_PONDERING
374 if( IsEnginePondering( ed->which ) ) {
379 if( ed->hint != 0 && *ed->hint != '\0' ) {
380 strncpy( buf, ed->hint, sizeof(buf) );
381 buf[sizeof(buf)-1] = '\0';
383 else if( ed->pv != 0 && *ed->pv != '\0' ) {
384 char * sep = strchr( ed->pv, ' ' );
385 int buflen = sizeof(buf);
388 buflen = sep - ed->pv + 1;
389 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
392 strncpy( buf, ed->pv, buflen );
393 buf[ buflen-1 ] = '\0';
396 SetEngineState( ed->which, STATE_PONDERING, buf );
398 else if( gameMode == TwoMachinesPlay ) {
399 SetEngineState( ed->which, STATE_THINKING, "" );
401 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
402 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
404 int time_secs = ed->time / 100;
405 int time_mins = time_secs / 60;
409 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
412 strncpy( mov, ed->hint, sizeof(mov) );
413 mov[ sizeof(mov)-1 ] = '\0';
415 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
416 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
419 SetEngineState( ed->which, STATE_ANALYZING, buf );
422 SetEngineState( ed->which, STATE_IDLE, "" );
426 DoSetWindowText( ed->which, nLabel, s_label );
430 if( ed->time > 0 && ed->nodes > 0 ) {
431 unsigned long nps_100 = ed->nodes / ed->time;
433 if( nps_100 < 100000 ) {
434 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %lu", nps_100 * 100 );
437 snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "NPS: %.1fk", nps_100 / 10.0 );
441 DoSetWindowText( ed->which, nLabelNPS, s_label );
444 if( ed->pv != 0 && *ed->pv != '\0' ) {
450 int time_secs = ed->time / 100;
451 int time_cent = ed->time % 100;
454 if( ed->nodes < 1000000 ) {
455 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display, ed->nodes );
458 snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.1fM", u64ToDouble(ed->nodes) / 1000000.0 );
462 if( ed->score > 0 ) {
463 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f", ed->score / 100.0 );
466 snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "%.2f", ed->score / 100.0 );
470 snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
472 /* Put all together... */
473 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
474 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
476 snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
479 buflen = strlen(buf);
481 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
483 buf[ sizeof(buf) - 3 ] = '\0';
485 strcat( buf + buflen, "\r\n" );
488 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
489 strncpy(lastLine[ed->which], buf, MSG_SIZ);
493 SetEngineColorIcon( ed->which );
496 // [HGM] kibitz: write kibitz line; split window for it if necessary
497 void OutputKibitz(int window, char *text)
499 static int currentLineEnd[2];
501 if(!EngineOutputIsUp()) return;
502 if(!opponentKibitzes) { // on first kibitz of game, clear memos
503 DoClearMemo(1); currentLineEnd[1] = 0;
504 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
506 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
508 strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
509 if(gameMode == IcsObserving) {
510 DoSetWindowText(0, nLabel, gameInfo.white);
511 SetIcon( 0, nColorIcon, nColorWhite);
512 SetIcon( 0, nStateIcon, nClear);
514 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
515 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
516 SetIcon( 1, nStateIcon, nClear);
517 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
518 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
519 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
520 currentLineEnd[window-1] = where + strlen(text);