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