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
36 #else /* not STDC_HEADERS */
39 # else /* not HAVE_STRING_H */
41 # endif /* not HAVE_STRING_H */
42 #endif /* not STDC_HEADERS */
47 #include "engineoutput.h"
62 // called by other front-end
63 void EngineOutputUpdate( FrontEndProgramStats * stats );
64 void OutputKibitz(int window, char *text);
66 // module back-end routines
67 static void VerifyDisplayMode();
68 static void UpdateControls( EngineOutputData * ed );
70 static int lastDepth[2] = { -1, -1 };
71 static int lastForwardMostMove[2] = { -1, -1 };
72 static int engineState[2] = { -1, -1 };
75 static int scores[MAX_VAR], textEnd[MAX_VAR], curDepth[2], nrVariations[2];
77 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
78 void SetEngineState( int which, int state, char * state_data )
80 int x_which = 1 - which;
82 if( engineState[ which ] != state ) {
83 engineState[ which ] = state;
87 SetIcon( which, nStateIcon, nThinking );
88 if( engineState[ x_which ] == STATE_THINKING ) {
89 SetEngineState( x_which, STATE_IDLE, "" );
93 SetIcon( which, nStateIcon, nPondering );
96 SetIcon( which, nStateIcon, nAnalyzing );
99 SetIcon( which, nStateIcon, nClear );
104 if( state_data != 0 ) {
105 DoSetWindowText( which, nStateData, state_data );
109 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
110 void SetProgramStats( FrontEndProgramStats * stats ) // now directly called by back-end
113 int clearMemo = FALSE;
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 /* Clear memo if needed */
158 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1) ) {
162 if( lastForwardMostMove[which] != forwardMostMove ) {
166 if( clearMemo ) { DoClearMemo(which); nrVariations[which] = 0; }
169 lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
170 lastForwardMostMove[which] = forwardMostMove;
172 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
173 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
178 UpdateControls( &ed );
181 #define ENGINE_COLOR_WHITE 'w'
182 #define ENGINE_COLOR_BLACK 'b'
183 #define ENGINE_COLOR_UNKNOWN ' '
186 static char GetEngineColor( int which )
188 char result = ENGINE_COLOR_UNKNOWN;
190 if( which == 0 || which == 1 ) {
191 ChessProgramState * cps;
194 case MachinePlaysBlack:
195 case IcsPlayingBlack:
196 result = ENGINE_COLOR_BLACK;
198 case MachinePlaysWhite:
199 case IcsPlayingWhite:
200 result = ENGINE_COLOR_WHITE;
204 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
206 case TwoMachinesPlay:
207 cps = (which == 0) ? &first : &second;
208 result = cps->twoMachinesColor[0];
209 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
211 default: ; // does not happen, but suppresses pedantic warnings
219 static char GetActiveEngineColor()
221 char result = ENGINE_COLOR_UNKNOWN;
223 if( gameMode == TwoMachinesPlay ) {
224 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
231 static int IsEnginePondering( int which )
236 case MachinePlaysBlack:
237 case IcsPlayingBlack:
238 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
240 case MachinePlaysWhite:
241 case IcsPlayingWhite:
242 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
244 case TwoMachinesPlay:
245 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
246 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
249 default: ; // does not happen, but suppresses pedantic warnings
256 static void SetDisplayMode( int mode )
258 if( windowMode != mode ) {
261 ResizeWindowControls( mode );
266 static void VerifyDisplayMode()
270 /* Get proper mode for current game */
272 case IcsObserving: // [HGM] ICS analyze
273 if(!appData.icsEngineAnalyze) return;
276 case MachinePlaysWhite:
277 case MachinePlaysBlack:
280 case IcsPlayingWhite:
281 case IcsPlayingBlack:
282 mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
284 case TwoMachinesPlay:
292 SetDisplayMode( mode );
295 // back end. Determine what icon to set in the color-icon field, and print it
296 void SetEngineColorIcon( int which )
298 char color = GetEngineColor(which);
301 if( color == ENGINE_COLOR_BLACK )
303 else if( color == ENGINE_COLOR_WHITE )
306 nicon = nColorUnknown;
308 SetIcon( which, nColorIcon, nicon );
311 #define MAX_NAME_LENGTH 32
313 // [HGM] multivar: sort Thinking Output within one depth on score
315 static int InsertionPoint( int len, EngineOutputData * ed )
317 int i, offs = 0, newScore = ed->score, n = ed->which;
319 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
320 newScore = 1e6; // info lines inserted on top
321 if(ed->depth != curDepth[n]) { // depth has changed
322 curDepth[n] = ed->depth;
323 nrVariations[n] = 0; // throw away everything we had
325 // loop through all lines. Note even / odd used for different panes
326 for(i=nrVariations[n]-2; i>=0; i-=2) {
327 // put new item behind those we haven't looked at
329 textEnd[i+n+2] = offs + len;
330 scores[i+n+2] = newScore;
331 if(newScore < scores[i+n]) break;
332 // if it had higher score as previous, move previous in stead
333 scores[i+n+2] = scores[i+n];
334 textEnd[i+n+2] = textEnd[i+n] + len;
338 textEnd[n] = offs + len;
339 scores[n] = newScore;
341 nrVariations[n] += 2;
346 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
347 static void UpdateControls( EngineOutputData * ed )
349 // int isPondering = FALSE;
351 char s_label[MAX_NAME_LENGTH + 32];
353 char * name = ed->name;
356 if( name == 0 || *name == '\0' ) {
360 strncpy( s_label, name, MAX_NAME_LENGTH );
361 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
363 #ifdef SHOW_PONDERING
364 if( IsEnginePondering( ed->which ) ) {
369 if( ed->hint != 0 && *ed->hint != '\0' ) {
370 strncpy( buf, ed->hint, sizeof(buf) );
371 buf[sizeof(buf)-1] = '\0';
373 else if( ed->pv != 0 && *ed->pv != '\0' ) {
374 char * sep = strchr( ed->pv, ' ' );
375 int buflen = sizeof(buf);
378 buflen = sep - ed->pv + 1;
379 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
382 strncpy( buf, ed->pv, buflen );
383 buf[ buflen-1 ] = '\0';
386 SetEngineState( ed->which, STATE_PONDERING, buf );
388 else if( gameMode == TwoMachinesPlay ) {
389 SetEngineState( ed->which, STATE_THINKING, "" );
391 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
392 || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
394 int time_secs = ed->time / 100;
395 int time_mins = time_secs / 60;
399 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
402 strncpy( mov, ed->hint, sizeof(mov) );
403 mov[ sizeof(mov)-1 ] = '\0';
405 sprintf( buf, "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
406 ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
409 SetEngineState( ed->which, STATE_ANALYZING, buf );
412 SetEngineState( ed->which, STATE_IDLE, "" );
416 DoSetWindowText( ed->which, nLabel, s_label );
420 if( ed->time > 0 && ed->nodes > 0 ) {
421 unsigned long nps_100 = ed->nodes / ed->time;
423 if( nps_100 < 100000 ) {
424 sprintf( s_label, "NPS: %lu", nps_100 * 100 );
427 sprintf( s_label, "NPS: %.1fk", nps_100 / 10.0 );
431 DoSetWindowText( ed->which, nLabelNPS, s_label );
434 if( ed->pv != 0 && *ed->pv != '\0' ) {
440 int time_secs = ed->time / 100;
441 int time_cent = ed->time % 100;
444 if( ed->nodes < 1000000 ) {
445 sprintf( s_nodes, u64Display, ed->nodes );
448 sprintf( s_nodes, "%.1fM", u64ToDouble(ed->nodes) / 1000000.0 );
452 if( ed->score > 0 ) {
453 sprintf( s_score, "+%.2f", ed->score / 100.0 );
456 sprintf( s_score, "%.2f", ed->score / 100.0 );
460 sprintf( s_time, "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
462 /* Put all together... */
463 if(ed->nodes == 0 && ed->score == 0 && ed->time == 0) sprintf( buf, "%3d\t", ed->depth ); else
464 sprintf( buf, "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
467 buflen = strlen(buf);
469 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
471 buf[ sizeof(buf) - 3 ] = '\0';
473 strcat( buf + buflen, "\r\n" );
476 InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
480 SetEngineColorIcon( ed->which );
483 // [HGM] kibitz: write kibitz line; split window for it if necessary
484 void OutputKibitz(int window, char *text)
486 static int currentLineEnd[2];
488 if(!EngineOutputIsUp()) return;
489 if(!opponentKibitzes) { // on first kibitz of game, clear memos
490 DoClearMemo(1); currentLineEnd[1] = 0;
491 if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
493 opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
495 strcpy(text+strlen(text)-1, "\r\n"); // to not lose line breaks on copying
496 if(gameMode == IcsObserving) {
497 DoSetWindowText(0, nLabel, gameInfo.white);
498 SetIcon( 0, nColorIcon, nColorWhite);
499 SetIcon( 0, nStateIcon, nClear);
501 DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
502 SetIcon( 1, nColorIcon, gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
503 SetIcon( 1, nStateIcon, nClear);
504 if(strstr(text, "\\ ") == text) where = currentLineEnd[window-1]; // continuation line
505 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
506 InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
507 currentLineEnd[window-1] = where + strlen(text);