Change evalgraph scale in drop games
[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 // back-end
89 /*
90     For a centipawn value, this function returns the height of the corresponding
91     histogram, centered on the reference axis.
92
93     Note: height can be negative!
94 */
95 static int GetValueY( int value )
96 {
97     if( value < -range*700 ) value = -range*700;
98     if( value > +range*700 ) value = +range*700;
99
100     return (nHeightPB / 2) - (int)(value * (nHeightPB - 2*MarginH) / (1400.*range));
101 }
102
103 // the brush selection is made part of the DrawLine, by passing a style argument
104 // the wrapper for doing the text output makes this back-end
105 static void DrawAxisSegmentHoriz( int value, Boolean drawValue )
106 {
107     int y = GetValueY( range*value*100 );
108
109     if( drawValue ) {
110         char buf[MSG_SIZ], *b = buf;
111
112         if( value > 0 ) *b++ = '+';
113         sprintf(b, "%d", range*value);
114
115         DrawEvalText(buf, strlen(buf), y);
116     }
117     // [HGM] counts on DrawEvalText to have select transparent background for dotted line!
118     DrawLine( MarginX, y, MarginX + MarginW, y, PEN_BLACK ); // Y-axis tick marks
119     DrawLine( MarginX + MarginW, y, nWidthPB - MarginW, y, PEN_DOTTED ); // hor grid
120 }
121
122 // The DrawLines again must select their own brush.
123 // the initial brush selection is useless? BkMode needed for dotted line and text
124 static void DrawAxis()
125 {
126     int cy = nHeightPB / 2;
127     
128 //    SelectObject( hdcPB, GetStockObject(NULL_BRUSH) );
129
130 //    SetBkMode( hdcPB, TRANSPARENT );
131
132     DrawAxisSegmentHoriz( +5, TRUE );
133     DrawAxisSegmentHoriz( +3, FALSE );
134     DrawAxisSegmentHoriz( +1, FALSE );
135     DrawAxisSegmentHoriz(  0, TRUE );
136     DrawAxisSegmentHoriz( -1, FALSE );
137     DrawAxisSegmentHoriz( -3, FALSE );
138     DrawAxisSegmentHoriz( -5, TRUE );
139
140     DrawLine( MarginX + MarginW, cy, nWidthPB - MarginW, cy, PEN_BLACK ); // x-axis
141     DrawLine( MarginX + MarginW, MarginH, MarginX + MarginW, nHeightPB - MarginH, PEN_BLACK ); // y-axis
142 }
143
144 // back-end
145 static void DrawHistogram( int x, int y, int width, int value, int side )
146 {
147     int left, top, right, bottom;
148
149     if( value > -25 && value < +25 ) return;
150
151     left = x;
152     right = left + width + 1;
153
154     if( value > 0 ) {
155         top = GetValueY( value );
156         bottom = y+1;
157     }
158     else {
159         top = y;
160         bottom = GetValueY( value ) + 1;
161     }
162
163
164     if( width == MIN_HIST_WIDTH ) {
165         right--;
166         DrawRectangle( left, top, right, bottom, side, FILLED );
167     }
168     else {
169         DrawRectangle( left, top, right, bottom, side, OPEN );
170     }
171 }
172
173 // back-end
174 static void DrawSeparator( int index, int x )
175 {
176     if( index > 0 ) {
177         if( index == currCurrent ) {
178             DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_BLUEDOTTED );
179         }
180         else if( (index % 20) == 0 ) {
181             DrawLineEx( x, MarginH, x, nHeightPB - MarginH, PEN_DOTTED );
182         }
183     }
184 }
185
186 // made back-end by replacing MoveToEx and LineTo by DrawSegment
187 /* Actually draw histogram as a diagram, cause there's too much data */
188 static void DrawHistogramAsDiagram( int cy, int paint_width, int hist_count )
189 {
190     double step;
191     int i;
192
193     /* Rescale the graph every few moves (as opposed to every move) */
194     hist_count -= hist_count % 8;
195     hist_count += 8;
196     hist_count /= 2;
197
198     step = (double) paint_width / (hist_count + 1);
199
200     for( i=0; i<2; i++ ) {
201         int index = currFirst;
202         int side = (currCurrent + i + 1) & 1; /* Draw current side last */
203         double x = MarginX + MarginW;
204
205         if( (index & 1) != side ) {
206             x += step / 2;
207             index++;
208         }
209
210         DrawSegment( (int) x, cy, NULL, NULL, PEN_NONE );
211
212         index += 2;
213
214         while( index < currLast ) {
215             x += step;
216
217             DrawSeparator( index, (int) x );
218
219             /* Extend line up to current point */
220             if( currPvInfo[index].depth > 0 ) {
221                 DrawSegment((int) x, GetValueY( GetPvScore(index) ), NULL, NULL, PEN_BOLD + side );
222             }
223
224             index += 2;
225         }
226     }
227 }
228
229 // back-end, delete pen selection
230 static void DrawHistogramFull( int cy, int hist_width, int hist_count )
231 {
232     int i;
233
234 //    SelectObject( hdcPB, GetStockObject(BLACK_PEN) );
235
236     for( i=0; i<hist_count; i++ ) {
237         int index = currFirst + i;
238         int x = MarginX + MarginW + index * hist_width;
239
240         /* Draw a separator every 10 moves */
241         DrawSeparator( index, x );
242
243         /* Draw histogram */
244         if( currPvInfo[i].depth > 0 ) {
245             DrawHistogram( x, cy, hist_width, GetPvScore(index), index & 1 );
246         }
247     }
248 }
249
250 typedef struct {
251     int cy;
252     int hist_width;
253     int hist_count;
254     int paint_width;
255 } VisualizationData;
256
257 // back-end
258 static Boolean InitVisualization( VisualizationData * vd )
259 {
260     Boolean result = FALSE;
261
262     vd->cy = nHeightPB / 2;
263     vd->hist_width = MIN_HIST_WIDTH;
264     vd->hist_count = currLast - currFirst;
265     vd->paint_width = nWidthPB - MarginX - 2*MarginW;
266
267     if( vd->hist_count > 0 ) {
268         result = TRUE;
269
270         /* Compute width */
271         vd->hist_width = vd->paint_width / vd->hist_count;
272
273         if( vd->hist_width > MAX_HIST_WIDTH ) vd->hist_width = MAX_HIST_WIDTH;
274
275         vd->hist_width -= vd->hist_width % 2;
276     }
277
278     return result;
279 }
280
281 // back-end
282 static void DrawHistograms()
283 {
284     VisualizationData vd;
285
286     if( InitVisualization( &vd ) ) {
287         if( vd.hist_width < MIN_HIST_WIDTH ) {
288             DrawHistogramAsDiagram( vd.cy, vd.paint_width, vd.hist_count );
289         }
290         else {
291             DrawHistogramFull( vd.cy, vd.hist_width, vd.hist_count );
292         }
293     }
294 }
295
296 // back-end
297 int GetMoveIndexFromPoint( int x, int y )
298 {
299     int result = -1;
300     int start_x = MarginX + MarginW;
301     VisualizationData vd;
302
303     if( x >= start_x && InitVisualization( &vd ) ) {
304         /* Almost an hack here... we duplicate some of the paint logic */
305         if( vd.hist_width < MIN_HIST_WIDTH ) {
306             double step;
307
308             vd.hist_count -= vd.hist_count % 8;
309             vd.hist_count += 8;
310             vd.hist_count /= 2;
311
312             step = (double) vd.paint_width / (vd.hist_count + 1);
313             step /= 2;
314
315             result = (int) (0.5 + (double) (x - start_x) / step);
316         }
317         else {
318             result = (x - start_x) / vd.hist_width;
319         }
320     }
321
322     if( result >= currLast ) {
323         result = -1;
324     }
325
326     return result;
327 }
328
329 // init and display part split of so they can be moved to front end
330 void PaintEvalGraph( void )
331 {
332     VariantClass v = gameInfo.variant;
333     range = (gameInfo.holdingsWidth && v != VariantSuper && v != VariantGreat) ? 2 : 1; // [HGM] double range in drop games
334     /* Draw */
335     DrawRectangle(0, 0, nWidthPB, nHeightPB, 2, FILLED);
336     DrawAxis();
337     DrawHistograms();
338 }
339