Silence warning
[xboard.git] / engineoutput.c
1 /*
2  * engineoutput.c - split-off backe-end from Engine output (PV) by HGM
3  *
4  * Author: Alessandro Scotti (Dec 2005)
5  *
6  * Copyright 2005 Alessandro Scotti
7  *
8  * Enhancements Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014,
9  * 2015, 2016 Free Software Foundation, Inc.
10  *
11  * ------------------------------------------------------------------------
12  *
13  * GNU XBoard is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or (at
16  * your option) any later version.
17  *
18  * GNU XBoard is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see http://www.gnu.org/licenses/.  *
25  *
26  *------------------------------------------------------------------------
27  ** See the file ChangeLog for a revision history.  */
28
29 #define SHOW_PONDERING
30
31 #include "config.h"
32
33 #include <stdio.h>
34 #include <ctype.h>
35
36 #if STDC_HEADERS
37 # include <stdlib.h>
38 # include <string.h>
39 #else /* not STDC_HEADERS */
40 # if HAVE_STRING_H
41 #  include <string.h>
42 # else /* not HAVE_STRING_H */
43 #  include <strings.h>
44 # endif /* not HAVE_STRING_H */
45 #endif /* not STDC_HEADERS */
46
47 #include "common.h"
48 #include "frontend.h"
49 #include "backend.h"
50 #include "moves.h"
51 #include "engineoutput.h"
52 #include "gettext.h"
53
54 #ifdef ENABLE_NLS
55 # define  _(s) gettext (s)
56 # define N_(s) gettext_noop (s)
57 #else
58 # ifdef WIN32
59 #  define  _(s) T_(s)
60 #  undef  ngettext
61 #  define  ngettext(s,p,n) T_(p)
62 # else
63 #  define  _(s) (s)
64 # endif
65 # define N_(s)  s
66 #endif
67
68 typedef struct {
69     char * name;
70     int which;
71     int depth;
72     u64 nodes;
73     int score;
74     int time;
75     char * pv;
76     char * hint;
77     int an_move_index;
78     int an_move_count;
79     int moveKey;
80 } EngineOutputData;
81
82 // called by other front-end
83 void EngineOutputUpdate( FrontEndProgramStats * stats );
84 void OutputKibitz(int window, char *text);
85
86 // module back-end routines
87 static void VerifyDisplayMode();
88 static void UpdateControls( EngineOutputData * ed );
89
90 static int  lastDepth[2] = { -1, -1 };
91 static int  lastForwardMostMove[2] = { -1, -1 };
92 static int  engineState[2] = { -1, -1 };
93 static char lastLine[2][MSG_SIZ];
94 static char header[2][MSG_SIZ];
95 static char columnHeader[MSG_SIZ] = "dep\tscore\tnodes\ttime\t(not shown:  tbhits\tknps\tseldep)\n";
96 static int  columnMask = 0xF0;
97
98 #define MAX_VAR 400
99 static int scores[MAX_VAR], textEnd[MAX_VAR], keys[MAX_VAR], curDepth[2], nrVariations[2];
100 static char fail[MAX_VAR];
101
102 extern int initialRulePlies;
103
104 void
105 MakeEngineOutputTitle ()
106 {
107         static char buf[MSG_SIZ];
108         static char oldTitle[MSG_SIZ];
109         char title[MSG_SIZ];
110         int count, rule = 2*appData.ruleMoves;
111
112         snprintf(title, MSG_SIZ, _("Engine Output") );
113
114         if(!EngineOutputIsUp()) return;
115         // figure out value of 50-move counter
116         count = currentMove;
117         while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove ) count--;
118         if( count == backwardMostMove ) count -= initialRulePlies;
119         count = currentMove - count;
120         if(!rule) rule = 100;
121         if(count >= rule - 40 && (!appData.icsActive || gameMode == IcsObserving || appData.zippyPlay)) {
122                 snprintf(buf, MSG_SIZ, ngettext("%s (%d reversible ply)", "%s (%d reversible plies)", count), title, count);
123                 safeStrCpy(title, buf, MSG_SIZ);
124         }
125         if(!strcmp(oldTitle, title)) return;
126         safeStrCpy(oldTitle, title, MSG_SIZ);
127         SetEngineOutputTitle(title);
128 }
129
130 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
131 void
132 SetEngineState (int which, enum ENGINE_STATE state, char * state_data)
133 {
134     int x_which = 1 - which;
135
136     if( engineState[ which ] != state ) {
137         engineState[ which ] = state;
138
139         switch( state ) {
140         case STATE_THINKING:
141             SetIcon( which, nStateIcon, nThinking );
142             if( engineState[ x_which ] == STATE_THINKING ) {
143                 SetEngineState( x_which, STATE_IDLE, "" );
144             }
145             break;
146         case STATE_PONDERING:
147             SetIcon( which, nStateIcon, nPondering );
148             break;
149         case STATE_ANALYZING:
150             SetIcon( which, nStateIcon, nAnalyzing );
151             break;
152         default:
153             SetIcon( which, nStateIcon, nClear );
154             break;
155         }
156     }
157
158     if( state_data != 0 ) {
159         DoSetWindowText( which, nStateData, state_data );
160     }
161 }
162
163 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
164 void
165 SetProgramStats (FrontEndProgramStats * stats) // now directly called by back-end
166 {
167     EngineOutputData ed;
168     int clearMemo = FALSE;
169     int which, depth, multi;
170     ChessMove moveType;
171     int ff, ft, rf, rt;
172     char pc;
173
174     if( stats == 0 ) {
175         SetEngineState( 0, STATE_IDLE, "" );
176         SetEngineState( 1, STATE_IDLE, "" );
177         return;
178     }
179
180     if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
181         return; // [HGM] kibitz: shut up engine if we are observing an ICS game
182
183     which = stats->which;
184     depth = stats->depth;
185
186     if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
187         return;
188     }
189
190     if( !EngineOutputDialogExists() ) {
191         return;
192     }
193
194     VerifyDisplayMode();
195
196     ed.which = which;
197     ed.depth = depth;
198     ed.nodes = stats->nodes;
199     ed.score = stats->score;
200     ed.time = stats->time;
201     ed.pv = stats->pv;
202     ed.hint = stats->hint;
203     ed.an_move_index = stats->an_move_index;
204     ed.an_move_count = stats->an_move_count;
205
206     /* Get target control. [HGM] this is moved to front end, which get them from a table */
207     if( which == 0 ) {
208         ed.name = first.tidy;
209     }
210     else {
211         ed.name = second.tidy;
212     }
213
214     if( ed.pv != 0 && ed.pv[0] == ' ' ) {
215         if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
216             ed.pv = "";
217         }
218     }
219
220     /* Clear memo if needed */
221     if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
222         clearMemo = TRUE;
223     }
224
225     if( lastForwardMostMove[which] != forwardMostMove ) {
226         clearMemo = TRUE;
227     }
228
229     if( clearMemo ) {
230         if(!appData.headers) columnHeader[0] = NULLCHAR;
231         DoClearMemo(which); nrVariations[which] = 0;
232         header[which][0] = NULLCHAR;
233         if(gameMode == AnalyzeMode) {
234           ChessProgramState *cps = (which ? &second : &first);
235           char *exclu = cps->excludeMoves ? exclusionHeader : "";
236           if((multi = MultiPV(cps, 3)) != -1) {
237             char *s = "setting";
238             if(multi < -1) multi = -2 - multi, s = "margin";
239             snprintf(header[which], MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV %s = %d / more\n",
240                                        appData.whitePOV || appData.scoreWhite ? "white" : "mover", s, cps->option[multi].value);
241           }
242           if(!which) snprintf(header[which]+strlen(header[which]), MSG_SIZ-strlen(header[which]), "%s%s", exclu, columnHeader);
243           InsertIntoMemo( which, header[which], 0);
244         } else {
245           snprintf(header[which], MSG_SIZ, "%s", columnHeader);
246           if(appData.ponderNextMove && lastLine[which][0]) {
247             InsertIntoMemo( which, lastLine[which], 0 );
248             InsertIntoMemo( which, "\n", 0 );
249           }
250           InsertIntoMemo( which, header[which], 0);
251         }
252     }
253
254     if(ed.pv && ed.pv[0] && ParseOneMove(ed.pv, currentMove, &moveType, &ff, &rf, &ft, &rt, &pc))
255         ed.moveKey = (ff<<24 | rf << 16 | ft << 8 | rt) ^ pc*87161;
256     else ed.moveKey = ed.nodes; // kludge to get unique key unlikely to match any move
257
258     /* Update */
259     lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
260     lastForwardMostMove[which] = forwardMostMove;
261
262     UpdateControls( &ed );
263 }
264
265 #define ENGINE_COLOR_WHITE      'w'
266 #define ENGINE_COLOR_BLACK      'b'
267 #define ENGINE_COLOR_UNKNOWN    ' '
268
269 // pure back end
270 static char
271 GetEngineColor (int which)
272 {
273     char result = ENGINE_COLOR_UNKNOWN;
274
275     if( which == 0 || which == 1 ) {
276         ChessProgramState * cps;
277
278         switch (gameMode) {
279         case MachinePlaysBlack:
280         case IcsPlayingBlack:
281             result = ENGINE_COLOR_BLACK;
282             break;
283         case MachinePlaysWhite:
284         case IcsPlayingWhite:
285             result = ENGINE_COLOR_WHITE;
286             break;
287         case AnalyzeMode:
288         case AnalyzeFile:
289             result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
290             break;
291         case TwoMachinesPlay:
292             cps = (which == 0) ? &first : &second;
293             result = cps->twoMachinesColor[0];
294             result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
295             break;
296         default: ; // does not happen, but suppresses pedantic warnings
297         }
298     }
299
300     return result;
301 }
302
303 // pure back end
304 static char
305 GetActiveEngineColor ()
306 {
307     char result = ENGINE_COLOR_UNKNOWN;
308
309     if( gameMode == TwoMachinesPlay ) {
310         result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
311     }
312
313     return result;
314 }
315
316 // pure back end
317 static int
318 IsEnginePondering (int which)
319 {
320     int result = FALSE;
321
322     switch (gameMode) {
323     case MachinePlaysBlack:
324     case IcsPlayingBlack:
325         if( WhiteOnMove(forwardMostMove) ) result = TRUE;
326         break;
327     case MachinePlaysWhite:
328     case IcsPlayingWhite:
329         if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
330         break;
331     case TwoMachinesPlay:
332         if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
333             if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
334         }
335         break;
336     default: ; // does not happen, but suppresses pedantic warnings
337     }
338
339     return result;
340 }
341
342 // back end
343 static void
344 SetDisplayMode (int mode)
345 {
346     if( windowMode != mode ) {
347         windowMode = mode;
348
349         ResizeWindowControls( mode );
350     }
351 }
352
353 // pure back end
354 static void
355 VerifyDisplayMode ()
356 {
357     int mode;
358
359     /* Get proper mode for current game */
360     switch( gameMode ) {
361     case IcsObserving:    // [HGM] ICS analyze
362         if(!appData.icsEngineAnalyze) return;
363     case AnalyzeFile:
364     case MachinePlaysWhite:
365     case MachinePlaysBlack:
366         mode = 0;
367         break;
368     case AnalyzeMode:
369         mode = second.analyzing;
370         break;
371     case IcsPlayingWhite:
372     case IcsPlayingBlack:
373         mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
374         break;
375     case TwoMachinesPlay:
376         mode = 1;
377         break;
378     default:
379         /* Do not change */
380         return;
381     }
382
383     SetDisplayMode( mode );
384 }
385
386 // back end. Determine what icon to set in the color-icon field, and print it
387 void
388 SetEngineColorIcon (int which)
389 {
390     char color = GetEngineColor(which);
391     int nicon = 0;
392
393     if( color == ENGINE_COLOR_BLACK )
394         nicon = nColorBlack;
395     else if( color == ENGINE_COLOR_WHITE )
396         nicon = nColorWhite;
397     else
398         nicon = nColorUnknown;
399
400     SetIcon( which, nColorIcon, nicon );
401 }
402
403 #define MAX_NAME_LENGTH 32
404
405 // [HGM] multivar: sort Thinking Output within one depth on score
406
407 static int
408 MateFlip (int n)
409 {   // map mate-score to monotonous scale, so sorting compares them correctly
410     if(n >=  MATE_SCORE) return 2*MATE_SCORE - n;
411     if(n <= -MATE_SCORE) return -2*MATE_SCORE - n;
412     return n;
413 }
414
415 static int
416 InsertionPoint (int len, EngineOutputData *ed)
417 {
418         int i, offs = 0, newScore = ed->score, n = ed->which;
419         char failType;
420
421         if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
422                 newScore = 1e6; // info lines inserted on top
423         if(ed->depth != curDepth[n]) { // depth has changed
424                 curDepth[n] = ed->depth;
425                 nrVariations[n] = 0; // throw away everything we had
426         }
427         i = strlen(ed->pv); if(i > 0) i--;
428         failType = ed->pv[i];
429         if(failType != '?' && failType != '!') failType = ' ';
430         // loop through all lines. Note even / odd used for different panes
431         for(i=nrVariations[n]-2; i>=0; i-=2) {
432                 // put new item behind those we haven't looked at
433                 offs = textEnd[i+n];
434                 textEnd[i+n+2] = offs + len;
435                 scores[i+n+2] = newScore;
436                 keys[i+n+2] = ed->moveKey;
437                 fail[i+n+2] = failType;
438                 if(ed->moveKey != keys[i+n] && // same move always tops previous one (as a higher score must be a fail low)
439                    MateFlip(newScore) < MateFlip(scores[i+n]) && fail[i+n] == ' ') break;
440                 // if it had higher score as previous, move previous in stead
441                 scores[i+n+2] = ed->moveKey == keys[i+n] ? newScore : scores[i+n]; // correct scores of fail-low/high searches
442                 textEnd[i+n+2] = textEnd[i+n] + len;
443                 keys[i+n+2] = keys[i+n];
444                 fail[i+n+2] = fail[i+n];
445         }
446         if(i<0) {
447                 offs = 0;
448                 textEnd[n] = offs + len;
449                 scores[n] = newScore;
450                 keys[n] = ed->moveKey;
451                 fail[n] = failType;
452         }
453         nrVariations[n] += 2;
454       return offs + strlen(header[ed->which]);
455 }
456
457 static char spaces[] = "            "; // [HGM] align: spaces for padding
458
459 static void
460 Format(char *buf, int val)
461 { // [HGM] tbhits: print a positive integer with trailing whitespace to give it fixed width
462         if( val < 1000000 ) {
463             int h = val, i=0;
464             while(h > 0) h /= 10, i++;
465             snprintf( buf, 24, "%d%s\t", val, spaces + 2*i);
466         }
467         else {
468             char unit = 'M';
469             if(val >= 1e9) val /= 1e3, unit = 'G';
470             snprintf( buf, 24, "%.*f%c%s\t", 1 + (val < 1e7), val/1e6, unit, spaces + 10 + 2*(val >= 1e8));
471         }
472 }
473
474 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
475 static void
476 UpdateControls (EngineOutputData *ed)
477 {
478 //    int isPondering = FALSE;
479
480     char s_label[MAX_NAME_LENGTH + 32];
481     int h;
482     char * name = ed->name;
483     char *q, *pvStart = ed->pv;
484
485     /* Label */
486     if( name == 0 || *name == '\0' ) {
487         name = "?";
488     }
489
490     strncpy( s_label, name, MAX_NAME_LENGTH );
491     s_label[ MAX_NAME_LENGTH-1 ] = '\0';
492
493     if(pvStart) { // [HGM] tbhits: plit up old PV into extra infos and real PV
494         while(strchr(pvStart, '\t')) { // locate last tab before non-int (real PV starts after that)
495             for(q=pvStart; isdigit(*q) || *q == ' '; q++);
496             if(*q != '\t') break;
497             pvStart = q + 1;
498         }
499     }
500
501 #ifdef SHOW_PONDERING
502     if( IsEnginePondering( ed->which ) ) {
503         char buf[12];
504
505         buf[0] = '\0';
506
507         if( ed->hint != 0 && *ed->hint != '\0' ) {
508             strncpy( buf, ed->hint, sizeof(buf) );
509             buf[sizeof(buf)-1] = '\0';
510         }
511         else if( pvStart != 0 && *pvStart != '\0' ) {
512             char * sep;
513             int buflen = sizeof(buf);
514
515             sep = strchr( pvStart, ' ' );
516             if( sep != NULL ) {
517                 buflen = sep - pvStart + 1;
518                 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
519             }
520
521             strncpy( buf, pvStart, buflen );
522             buf[ buflen-1 ] = '\0';
523         }
524
525         SetEngineState( ed->which, STATE_PONDERING, buf );
526     }
527     else if( gameMode == TwoMachinesPlay ) {
528         SetEngineState( ed->which, STATE_THINKING, "" );
529     }
530     else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
531           || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
532         char buf[64];
533         int time_secs = ed->time / 100;
534         int time_mins = time_secs / 60;
535
536         buf[0] = '\0';
537
538         if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
539             char mov[16];
540
541             strncpy( mov, ed->hint, sizeof(mov) );
542             mov[ sizeof(mov)-1 ] = '\0';
543
544             snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
545                         ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
546         }
547
548         SetEngineState( ed->which, STATE_ANALYZING, buf );
549     }
550     else {
551         SetEngineState( ed->which, STATE_IDLE, "" );
552     }
553 #endif
554
555     DoSetWindowText( ed->which, nLabel, s_label );
556
557     s_label[0] = '\0';
558
559     if( ed->time > 0 && ed->nodes > 0 ) {
560         unsigned long nps_100 = ed->nodes / ed->time;
561
562         if( nps_100 < 100000 ) {
563           snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "%s: %lu", _("NPS"), nps_100 * 100 );
564         }
565         else {
566           snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "%s: %.1fk", _("NPS"), nps_100 / 10.0 );
567         }
568     }
569
570     DoSetWindowText( ed->which, nLabelNPS, s_label );
571
572     /* Memo */
573     if( pvStart != 0 && *pvStart != '\0' ) {
574         char s_nodes[24];
575         char s_score[16];
576         char s_time[24];
577         char s_hits[24];
578         char s_seld[24];
579         char s_knps[24];
580         char buf[256], fail;
581         int buflen, hits, i, params[5], extra;
582         int time_secs = ed->time / 100;
583         int time_cent = ed->time % 100;
584
585         /* Nodes */
586         if( ed->nodes < 1000000 ) {
587             int h = ed->nodes, i=0;
588             while(h > 0) h /= 10, i++; // [HGM] align: count digits; pad with 2 spaces for every missing digit
589             snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display "%s\t", ed->nodes, spaces + 2*i);
590         }
591         else {
592             double x = u64ToDouble(ed->nodes);
593             char unit = 'M';
594             if(x >= 1e9) x /= 1e3, unit = 'G';
595             snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.*f%c%s\t", 1 + (x < 1e7), x / 1e6,
596                       unit, spaces + 10 + 2*(ed->nodes >= 1e8));
597         }
598
599         /* TB Hits etc. */
600         for(i=hits=0; i<5; i++) params[i] = 0;
601 //fprintf(stderr, "%s\n%s\n", ed->pv, pvStart);
602         if(pvStart != ed->pv) { // check if numbers before PV
603             strncpy(buf, ed->pv, 256); buf[pvStart - ed->pv] = NULLCHAR;
604             extra = sscanf(buf, "%d %d %d %d %d", params, params+1, params+2, params+3, params+4);
605 //fprintf(stderr, "extra=%d len=%d\n", extra, pvStart - ed->pv);
606             if(extra) hits = params[extra-1], params[extra-1] = 0; // last one is tbhits
607         }
608         Format(s_seld, params[0]); Format(s_knps, params[1]); Format(s_hits, hits); 
609
610         if(*ed->pv) fail = ed->pv[strlen(ed->pv)-1]; else fail = ' ';
611         if(fail != '?' && fail != '!') fail = ' ';
612
613         /* Score */
614         h = ((gameMode == AnalyzeMode && appData.whitePOV || appData.scoreWhite) && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
615         if( h == 0 ) {
616           snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "  0.00%c\t", fail );
617         } else
618         if( h >= MATE_SCORE) snprintf(s_score, 16, "  %s#%d%c\t", ( h > MATE_SCORE+9 ? "" : "  "),  h - MATE_SCORE, fail ); else
619         if(-h >= MATE_SCORE) snprintf(s_score, 16, " %s#-%d%c\t", (-h > MATE_SCORE+9 ? "" : "  "), -h - MATE_SCORE, fail ); else
620         if( h > 0 ) {
621           snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f%c\t", h / 100.0, fail );
622         }
623         else {
624           snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), " %.2f%c\t", h / 100.0, fail );
625         }
626
627         /* Time */
628         if(time_secs >= 3600)
629             snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d:%02d\t", time_secs / 3600, (time_secs / 60) % 60, time_secs % 60 );
630         else
631         snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d\t", time_secs / 60, time_secs % 60, time_cent );
632
633         if(columnMask & 2) s_score[0] = NULLCHAR; // [HGM] hide: erase columns the user has hidden
634         if(columnMask & 4) s_nodes[0] = NULLCHAR;
635         if(columnMask & 8) s_time[0]  = NULLCHAR;
636         if(columnMask & 16) s_hits[0]  = NULLCHAR;
637         if(columnMask & 32) s_knps[0]  = NULLCHAR;
638         if(columnMask & 64) s_seld[0]  = NULLCHAR;
639
640         /* Put all together... */
641         if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
642           snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
643         else
644           snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s%s%s%s%s%s", ed->depth, s_score, s_nodes, s_time, s_hits, s_knps, s_seld );
645
646         /* Add PV */
647         buflen = strlen(buf);
648
649         strncpy( buf + buflen, pvStart, sizeof(buf) - buflen );
650
651         buf[ sizeof(buf) - 3 ] = '\0';
652
653         strcat( buf + buflen, "\r\n" );
654
655         /* Update memo */
656         InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
657         strncpy(lastLine[ed->which], buf, MSG_SIZ);
658     }
659
660     /* Colors */
661     SetEngineColorIcon( ed->which );
662 }
663
664 static char *titles[] = { "score\t", "nodes\t", "time\t", "tbhits\t", "knps\t", "seldep\t" };
665
666 void
667 Collapse(int n)
668 {   // handle click on column headers, to hide / show them
669     int i, j, nr=0, m=~columnMask, Ncol=7;
670     for(i=0; columnHeader[i] && i<n; i++) nr += (columnHeader[i] == '\t');
671     if(!nr) return; // depth always shown, so clicks on it ignored
672     for(i=j=0; i<Ncol; i++) if(m & 1<<i) j++; // count hidden columns
673     if(nr < j) { // shown column clicked: hide it
674         for(i=j=0; i<Ncol; i++) if(m & 1<<i && j++ == nr) break;
675         columnMask |= 1<<i;
676     } else { // hidden column clicked: show it
677         m = ~m; nr -= j;
678         for(i=j=0; i<Ncol; i++) if(m & 1<<i && j++ == nr) break;
679         columnMask &= ~(1<<i);
680     }
681     // create new header line
682     strcpy(columnHeader, "dep\t");
683     m = ~columnMask;
684     for(i=j=1; i<Ncol; i++) if(m & 1<<i) strcat(columnHeader, titles[i-1]), j++;
685     if(j != Ncol) { // list hidden columns, so user ca click them
686         m = ~m; strcat(columnHeader, "(not shown:  ");
687         for(i=1; i<Ncol; i++) if(m & 1<<i) strcat(columnHeader, titles[i-1]);
688         strcat(columnHeader, ")");
689     }
690     strcat(columnHeader, "\n");
691 }
692
693 // [HGM] kibitz: write kibitz line; split window for it if necessary
694 void
695 OutputKibitz (int window, char *text)
696 {
697         static int currentLineEnd[2];
698         int where = 0;
699         if(!EngineOutputIsUp()) return;
700         if(!opponentKibitzes) { // on first kibitz of game, clear memos
701             DoClearMemo(1); currentLineEnd[1] = 0;
702             if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
703         }
704         opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
705         VerifyDisplayMode();
706         strncpy(text+strlen(text)-1, "\r\n", 4); // to not lose line breaks on copying
707         if(gameMode == IcsObserving) {
708             DoSetWindowText(0, nLabel, gameInfo.white);
709             SetIcon( 0, nColorIcon,  nColorWhite);
710             SetIcon( 0, nStateIcon,  nClear);
711         }
712         DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
713         SetIcon( 1, nColorIcon,  gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
714         SetIcon( 1, nStateIcon,  nClear);
715         if(strstr(text, "\\  ") == text) where = currentLineEnd[window-1]; // continuation line
716 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
717         InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
718         currentLineEnd[window-1] = where + strlen(text);
719 }