Display score/depth in Eval Graph title
[xboard.git] / evalgraph.c
1 /*
2  * evalgraph.c - Evaluation graph back-end part
3  *
4  * Author: Alessandro Scotti (Dec 2005)
5  *
6  * Copyright 2005 Alessandro Scotti
7  *
8  * ------------------------------------------------------------------------
9  *
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.
14  *
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.
19  *
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/.  *
22  *
23  *------------------------------------------------------------------------
24  ** See the file ChangeLog for a revision history.  */
25
26 // code refactored by HGM to obtain front-end / back-end separation
27
28 #include "config.h"
29
30 #include <stdio.h>
31
32 #if STDC_HEADERS
33 # include <stdlib.h>
34 # include <string.h>
35 #else /* not STDC_HEADERS */
36 # if HAVE_STRING_H
37 #  include <string.h>
38 # else /* not HAVE_STRING_H */
39 #  include <strings.h>
40 # endif /* not HAVE_STRING_H */
41 #endif /* not STDC_HEADERS */
42
43 #include "common.h"
44 #include "frontend.h"
45 #include "backend.h"
46 #include "evalgraph.h"
47
48 /* Module globals */
49 ChessProgramStats_Move * currPvInfo;
50 int currFirst = 0;
51 int currLast = 0;
52 int currCurrent = -1;
53 int range = 1;
54
55 int nWidthPB = 0;
56 int nHeightPB = 0;
57
58 int MarginX = 18;
59 int MarginW = 4;
60 int MarginH = 4;
61
62 // back-end
63 static void DrawLine( int x1, int y1, int x2, int y2, int penType )
64 {
65     DrawSegment( x1, y1, NULL, NULL, PEN_NONE );
66     DrawSegment( x2, y2, NULL, NULL, penType );
67 }
68
69 // back-end
70 static void DrawLineEx( int x1, int y1, int x2, int y2, int penType )
71 {
72     int savX, savY;
73     DrawSegment( x1, y1, &savX, &savY, PEN_NONE );
74     DrawSegment( x2, y2, NULL, NULL, penType );
75     DrawSegment( savX, savY, NULL, NULL, PEN_NONE );
76 }
77
78 // back-end
79 static int GetPvScore( int index )
80 {
81     int score = currPvInfo[ index ].score;
82
83     if( index & 1 ) score = -score; /* Flip score for black */
84
85     return score;
86 }
87
88 char* MakeEvalTitle( char * title)
89 {
90     int score = currPvInfo[ currCurrent ].score;
91     int depth = currPvInfo[ currCurrent ].depth;
92     static char buf[MSG_SIZ];
93
94     if( depth <=0 ) return title;
95     if( currCurrent & 1 ) score = -score; /* Flip score for black */
96     snprintf(buf, MSG_SIZ, "%s {%s%.2f/%-2d %d}", title, score>0 ? "+" : " ", score/100., depth, (currPvInfo[currCurrent].time+50)/100);
97
98     return buf;
99 }
100
101 // back-end
102 /*
103     For a centipawn value, this function returns the height of the corresponding
104     histogram, centered on the reference axis.
105
106     Note: height can be negative!
107 */
108 static int GetValueY( int value )
109 {
110     if( value < -range*700 ) value = -range*700;
111     if( value > +range*700 ) value = +range*700;
112
113     return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / (1400.*range));
114 }
115
116 // the brush selection is made part of the DrawLine, by passing a style argument
117 // the wrapper for doing the text output makes this back-end
118 static void DrawAxisSegmentHoriz( int value, Boolean drawValue )
119 {
120     int y = GetValueY( range*value*100 );
121
122     if( drawValue ) {
123         char buf[MSG_SIZ], *b = buf;
124
125         if( value > 0 ) *b++ = '+';
126         sprintf(b, "%d", range*value);
127
128         DrawEvalText(buf, strlen(buf), y);
129     }
130     // [HGM] counts on DrawEvalText to have select transparent background for dotted line!
131     DrawLine( MarginX, y, MarginX + MarginW, y, PEN_BLACK ); // Y-axis tick marks
132     DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y, PEN_DOTTED ); // hor grid
133 }
134
135 // The DrawLines again must select their own brush.
136 // the initial brush selection is useless? BkMode needed for dotted line and text
137 static void DrawAxis()
138 {
139     int cy = nHeightPB / 2;
140     
141 //    SelectObject( hdcPB, GetStockObject(NULL_BRUSH) );
142
143 //    SetBkMode( hdcPB, TRANSPARENT );
144
145     DrawAxisSegmentHoriz( +5, TRUE );
146     DrawAxisSegmentHoriz( +3, FALSE );
147     DrawAxisSegmentHoriz( +1, FALSE );
148     DrawAxisSegmentHoriz(  0, TRUE );
149     DrawAxisSegmentHoriz( -1, FALSE );
150     DrawAxisSegmentHoriz( -3, FALSE );
151     DrawAxisSegmentHoriz( -5, TRUE );
152
153     DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy, PEN_BLACK ); // x-axis
154     DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH, PEN_BLACK ); // y-axis
155 }
156
157 // back-end
158 static void DrawHistogram( int x, int y, int width, int value, int side )
159 {
160     int left, top, right, bottom;
161
162     if( value > -25 && value < +25 ) return;
163
164     left = x;
165     right = left + width + 1;
166
167     if( value > 0 ) {
168         top = GetValueY( value );
169         bottom = y+1;
170     }
171     else {
172         top = y;
173         bottom = GetValueY( value ) + 1;
174     }
175
176
177     if( width == MIN_HIST_WIDTH ) {
178         right--;
179         DrawRectangle( left, top, right, bottom, side, FILLED );
180     }
181     else {
182         DrawRectangle( left, top, right, bottom, side, OPEN );
183     }
184 }
185
186 // back-end
187 static void DrawSeparator( int index, int x )
188 {
189     if( index > 0 ) {
190         if( index == currCurrent ) {
191             DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_BLUEDOTTED );
192         }
193         else if( (index % 20) == 0 ) {
194             DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_DOTTED );
195         }
196     }
197 }
198
199 // made back-end by replacing MoveToEx and LineTo by DrawSegment
200 /* Actually draw histogram as a diagram, cause there's too much data */
201 static void DrawHistogramAsDiagram( int cy, int paint_width, int hist_count )
202 {
203     double step;
204     int i;
205
206     /* Rescale the graph every few moves (as opposed to every move) */
207     hist_count -= hist_count % 8;
208     hist_count += 8;
209     hist_count /= 2;
210
211     step = (double) paint_width / (hist_count + 1);
212
213     for( i=0; i<2; i++ ) {
214         int index = currFirst;
215         int side = (currCurrent + i + 1) & 1; /* Draw current side last */
216         double x = MarginX + MarginW;
217
218         if( (index & 1) != side ) {
219             x += step / 2;
220             index++;
221         }
222
223         DrawSegment( (int) x, cy, NULL, NULL, PEN_NONE );
224
225         index += 2;
226
227         while( index < currLast ) {
228             x += step;
229
230             DrawSeparator( index, (int) x );
231
232             /* Extend line up to current point */
233             if( currPvInfo[index].depth > 0 ) {
234                 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, PEN_BOLD + side );
235             }
236
237             index += 2;
238         }
239     }
240 }
241
242 // back-end, delete pen selection
243 static void DrawHistogramFull( int cy, int hist_width, int hist_count )
244 {
245     int i;
246
247 //    SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
248
249     for( i=0; i<hist_count; i++ ) {
250         int index = currFirst + i;
251         int x = MarginX + MarginW + index * hist_width;
252
253         /* Draw a separator every 10 moves */
254         DrawSeparator( index, x );
255
256         /* Draw histogram */
257         if( currPvInfo[i].depth > 0 ) {
258             DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
259         }
260     }
261 }
262
263 typedef struct {
264     int cy;
265     int hist_width;
266     int hist_count;
267     int paint_width;
268 } VisualizationData;
269
270 // back-end
271 static Boolean InitVisualization( VisualizationData * vd )
272 {
273     Boolean result = FALSE;
274
275     vd->cy = nHeightPB / 2;
276     vd->hist_width = MIN_HIST_WIDTH;
277     vd->hist_count = currLast - currFirst;
278     vd->paint_width = nWidthPB - MarginX - 2*MarginW;
279
280     if( vd->hist_count > 0 ) {
281         result = TRUE;
282
283         /* Compute width */
284         vd->hist_width = vd->paint_width / vd->hist_count;
285
286         if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
287
288         vd->hist_width -= vd->hist_width % 2;
289     }
290
291     return result;
292 }
293
294 // back-end
295 static void DrawHistograms()
296 {
297     VisualizationData vd;
298
299     if( InitVisualization( &vd ) ) {
300         if( vd.hist_width < MIN_HIST_WIDTH ) {
301             DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
302         }
303         else {
304             DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
305         }
306     }
307 }
308
309 // back-end
310 int GetMoveIndexFromPoint( int x, int y )
311 {
312     int result = -1;
313     int start_x = MarginX + MarginW;
314     VisualizationData vd;
315
316     if( x >= start_x && InitVisualization( &vd ) ) {
317         /* Almost an hack here... we duplicate some of the paint logic */
318         if( vd.hist_width < MIN_HIST_WIDTH ) {
319             double step;
320
321             vd.hist_count -= vd.hist_count % 8;
322             vd.hist_count += 8;
323             vd.hist_count /= 2;
324
325             step = (double) vd.paint_width / (vd.hist_count + 1);
326             step /= 2;
327
328             result = (int) (0.5 + (double) (x - start_x) / step);
329         }
330         else {
331             result = (x - start_x) / vd.hist_width;
332         }
333     }
334
335     if( result >= currLast ) {
336         result = -1;
337     }
338
339     return result;
340 }
341
342 // init and display part split of so they can be moved to front end
343 void PaintEvalGraph( void )
344 {
345     VariantClass v = gameInfo.variant;
346     range = (gameInfo.holdingsWidth && v != VariantSuper && v != VariantGreat && v != VariantSChess) ? 2 : 1; // [HGM] double range in drop games
347     /* Draw */
348     DrawRectangle(0, 0, nWidthPB, nHeightPB, 2, FILLED);
349     DrawAxis();
350     DrawHistograms();
351 }
352