Merge branch 'v4.8.x'
[xboard.git] / board.c
1 /*
2  * board.c -- platform-independent drawing code for XBoard
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts.
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Free
9  * Software Foundation, Inc.
10  *
11  * The following terms apply to Digital Equipment Corporation's copyright
12  * interest in XBoard:
13  * ------------------------------------------------------------------------
14  * All Rights Reserved
15  *
16  * Permission to use, copy, modify, and distribute this software and its
17  * documentation for any purpose and without fee is hereby granted,
18  * provided that the above copyright notice appear in all copies and that
19  * both that copyright notice and this permission notice appear in
20  * supporting documentation, and that the name of Digital not be
21  * used in advertising or publicity pertaining to distribution of the
22  * software without specific, written prior permission.
23  *
24  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
26  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
27  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
28  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
29  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30  * SOFTWARE.
31  * ------------------------------------------------------------------------
32  *
33  * The following terms apply to the enhanced version of XBoard
34  * distributed by the Free Software Foundation:
35  * ------------------------------------------------------------------------
36  *
37  * GNU XBoard is free software: you can redistribute it and/or modify
38  * it under the terms of the GNU General Public License as published by
39  * the Free Software Foundation, either version 3 of the License, or (at
40  * your option) any later version.
41  *
42  * GNU XBoard is distributed in the hope that it will be useful, but
43  * WITHOUT ANY WARRANTY; without even the implied warranty of
44  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45  * General Public License for more details.
46  *
47  * You should have received a copy of the GNU General Public License
48  * along with this program. If not, see http://www.gnu.org/licenses/.  *
49  *
50  *------------------------------------------------------------------------
51  ** See the file ChangeLog for a revision history.  */
52
53 #define HIGHDRAG 1
54
55 #include "config.h"
56
57 #include <stdio.h>
58 #include <ctype.h>
59 #include <signal.h>
60 #include <errno.h>
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <pwd.h>
64 #include <math.h>
65
66 #if STDC_HEADERS
67 # include <stdlib.h>
68 # include <string.h>
69 #else /* not STDC_HEADERS */
70 extern char *getenv();
71 # if HAVE_STRING_H
72 #  include <string.h>
73 # else /* not HAVE_STRING_H */
74 #  include <strings.h>
75 # endif /* not HAVE_STRING_H */
76 #endif /* not STDC_HEADERS */
77
78 #if TIME_WITH_SYS_TIME
79 # include <sys/time.h>
80 # include <time.h>
81 #else
82 # if HAVE_SYS_TIME_H
83 #  include <sys/time.h>
84 # else
85 #  include <time.h>
86 # endif
87 #endif
88
89 #if HAVE_UNISTD_H
90 # include <unistd.h>
91 #endif
92
93 #if HAVE_SYS_WAIT_H
94 # include <sys/wait.h>
95 #endif
96
97 #include "common.h"
98 #include "frontend.h"
99 #include "backend.h"
100 #include "xboard2.h"
101 #include "moves.h"
102 #include "board.h"
103 #include "draw.h"
104
105
106 #ifdef __EMX__
107 #ifndef HAVE_USLEEP
108 #define HAVE_USLEEP
109 #endif
110 #define usleep(t)   _sleep2(((t)+500)/1000)
111 #endif
112
113
114 int squareSize, lineGap;
115
116 int damage[2][BOARD_RANKS][BOARD_FILES];
117
118 /* There can be two pieces being animated at once: a player
119    can begin dragging a piece before the remote opponent has moved. */
120
121 AnimState anims[NrOfAnims];
122
123 static void DrawSquare P((int row, int column, ChessSquare piece, int do_flash));
124 static Boolean IsDrawArrowEnabled P((void));
125 static void DrawArrowHighlight P((int fromX, int fromY, int toX,int toY));
126 static void ArrowDamage P((int s_col, int s_row, int d_col, int d_row));
127
128 static void
129 drawHighlight (int file, int rank, int type)
130 {
131     int x, y;
132
133     if (lineGap == 0) return;
134
135     if (flipView) {
136         x = lineGap/2 + ((BOARD_WIDTH-1)-file) *
137           (squareSize + lineGap);
138         y = lineGap/2 + rank * (squareSize + lineGap);
139     } else {
140         x = lineGap/2 + file * (squareSize + lineGap);
141         y = lineGap/2 + ((BOARD_HEIGHT-1)-rank) *
142           (squareSize + lineGap);
143     }
144
145     DrawBorder(x,y, type, lineGap & 1); // pass whether lineGap is odd
146 }
147
148 int hi1X = -1, hi1Y = -1, hi2X = -1, hi2Y = -1;
149 int pm1X = -1, pm1Y = -1, pm2X = -1, pm2Y = -1;
150
151 void
152 SetHighlights (int fromX, int fromY, int toX, int toY)
153 {
154     int arrow = hi2X >= 0 && hi1Y >= 0 && IsDrawArrowEnabled();
155
156     if (hi1X != fromX || hi1Y != fromY) {
157         if (hi1X >= 0 && hi1Y >= 0) {
158             drawHighlight(hi1X, hi1Y, 0);
159         }
160     } // [HGM] first erase both, then draw new!
161
162     if (hi2X != toX || hi2Y != toY) {
163         if (hi2X >= 0 && hi2Y >= 0) {
164             drawHighlight(hi2X, hi2Y, 0);
165         }
166     }
167
168     if(arrow) // there currently is an arrow displayed
169         ArrowDamage(hi1X, hi1Y, hi2X, hi2Y); // mark which squares it damaged
170
171     if (hi1X != fromX || hi1Y != fromY) {
172         if (fromX >= 0 && fromY >= 0) {
173             drawHighlight(fromX, fromY, 1);
174         }
175     }
176     if (hi2X != toX || hi2Y != toY) {
177         if (toX >= 0 && toY >= 0) {
178             drawHighlight(toX, toY, 1);
179         }
180     }
181
182     hi1X = fromX;
183     hi1Y = fromY;
184     hi2X = toX;
185     hi2Y = toY;
186
187     if(arrow || toX >= 0 && fromY >= 0 && IsDrawArrowEnabled())
188         DrawPosition(FALSE, NULL); // repair any arrow damage, or draw a new one
189 }
190
191 void
192 ClearHighlights ()
193 {
194     SetHighlights(-1, -1, -1, -1);
195 }
196
197
198 void
199 SetPremoveHighlights (int fromX, int fromY, int toX, int toY)
200 {
201     if (pm1X != fromX || pm1Y != fromY) {
202         if (pm1X >= 0 && pm1Y >= 0) {
203             drawHighlight(pm1X, pm1Y, 0);
204         }
205         if (fromX >= 0 && fromY >= 0) {
206             drawHighlight(fromX, fromY, 2);
207         }
208     }
209     if (pm2X != toX || pm2Y != toY) {
210         if (pm2X >= 0 && pm2Y >= 0) {
211             drawHighlight(pm2X, pm2Y, 0);
212         }
213         if (toX >= 0 && toY >= 0) {
214             drawHighlight(toX, toY, 2);
215         }
216     }
217     pm1X = fromX;
218     pm1Y = fromY;
219     pm2X = toX;
220     pm2Y = toY;
221 }
222
223 void
224 ClearPremoveHighlights ()
225 {
226   SetPremoveHighlights(-1, -1, -1, -1);
227 }
228
229 /*
230  * If the user selects on a border boundary, return -1; if off the board,
231  *   return -2.  Otherwise map the event coordinate to the square.
232  */
233 int
234 EventToSquare (int x, int limit)
235 {
236     if (x <= 0)
237       return -2;
238     if (x < lineGap)
239       return -1;
240     x -= lineGap;
241     if ((x % (squareSize + lineGap)) >= squareSize)
242       return -1;
243     x /= (squareSize + lineGap);
244     if (x >= limit)
245       return -2;
246     return x;
247 }
248
249 /* [HR] determine square color depending on chess variant. */
250 int
251 SquareColor (int row, int column)
252 {
253     int square_color;
254
255     if (gameInfo.variant == VariantXiangqi) {
256         if (column >= 3 && column <= 5 && row >= 0 && row <= 2) {
257             square_color = 1;
258         } else if (column >= 3 && column <= 5 && row >= 7 && row <= 9) {
259             square_color = 0;
260         } else if (row <= 4) {
261             square_color = 0;
262         } else {
263             square_color = 1;
264         }
265     } else {
266         square_color = ((column + row) % 2) == 1;
267     }
268
269     /* [hgm] holdings: next line makes all holdings squares light */
270     if(column < BOARD_LEFT || column >= BOARD_RGHT) square_color = 1;
271
272     if ( // [HGM] holdings: blank out area between board and holdings
273                  column == BOARD_LEFT-1
274              ||  column == BOARD_RGHT
275              || (column == BOARD_LEFT-2 && row < BOARD_HEIGHT-gameInfo.holdingsSize)
276              || (column == BOARD_RGHT+1 && row >= gameInfo.holdingsSize) )
277         square_color = 2; // black
278
279     return square_color;
280 }
281
282 /*      Convert board position to corner of screen rect and color       */
283
284 void
285 ScreenSquare (int column, int row, Pnt *pt, int *color)
286 {
287   if (flipView) {
288     pt->x = lineGap + ((BOARD_WIDTH-1)-column) * (squareSize + lineGap);
289     pt->y = lineGap + row * (squareSize + lineGap);
290   } else {
291     pt->x = lineGap + column * (squareSize + lineGap);
292     pt->y = lineGap + ((BOARD_HEIGHT-1)-row) * (squareSize + lineGap);
293   }
294   *color = SquareColor(row, column);
295 }
296
297 /*      Convert window coords to square                 */
298
299 void
300 BoardSquare (int x, int y, int *column, int *row)
301 {
302   *column = EventToSquare(x, BOARD_WIDTH);
303   if (flipView && *column >= 0)
304     *column = BOARD_WIDTH - 1 - *column;
305   *row = EventToSquare(y, BOARD_HEIGHT);
306   if (!flipView && *row >= 0)
307     *row = BOARD_HEIGHT - 1 - *row;
308 }
309
310 /*      Generate a series of frame coords from start->mid->finish.
311         The movement rate doubles until the half way point is
312         reached, then halves back down to the final destination,
313         which gives a nice slow in/out effect. The algorithmn
314         may seem to generate too many intermediates for short
315         moves, but remember that the purpose is to attract the
316         viewers attention to the piece about to be moved and
317         then to where it ends up. Too few frames would be less
318         noticeable.                                             */
319
320 static void
321 Tween (Pnt *start, Pnt *mid, Pnt *finish, int factor, Pnt frames[], int *nFrames)
322 {
323   int fraction, n, count;
324
325   count = 0;
326
327   /* Slow in, stepping 1/16th, then 1/8th, ... */
328   fraction = 1;
329   for (n = 0; n < factor; n++)
330     fraction *= 2;
331   for (n = 0; n < factor; n++) {
332     frames[count].x = start->x + (mid->x - start->x) / fraction;
333     frames[count].y = start->y + (mid->y - start->y) / fraction;
334     count ++;
335     fraction = fraction / 2;
336   }
337
338   /* Midpoint */
339   frames[count] = *mid;
340   count ++;
341
342   /* Slow out, stepping 1/2, then 1/4, ... */
343   fraction = 2;
344   for (n = 0; n < factor; n++) {
345     frames[count].x = finish->x - (finish->x - mid->x) / fraction;
346     frames[count].y = finish->y - (finish->y - mid->y) / fraction;
347     count ++;
348     fraction = fraction * 2;
349   }
350   *nFrames = count;
351 }
352
353 /****   Animation code by Hugh Fisher, DCS, ANU.
354
355         Known problem: if a window overlapping the board is
356         moved away while a piece is being animated underneath,
357         the newly exposed area won't be updated properly.
358         I can live with this.
359
360         Known problem: if you look carefully at the animation
361         of pieces in mono mode, they are being drawn as solid
362         shapes without interior detail while moving. Fixing
363         this would be a major complication for minimal return.
364 ****/
365
366 /*   Utilities  */
367
368 #undef Max  /* just in case */
369 #undef Min
370 #define Max(a, b) ((a) > (b) ? (a) : (b))
371 #define Min(a, b) ((a) < (b) ? (a) : (b))
372
373 typedef struct {
374   short int x, y, width, height;
375 } MyRectangle;
376
377 void
378 DoSleep (int n)
379 {
380     FrameDelay(n);
381 }
382
383 static void
384 SetRect (MyRectangle *rect, int x, int y, int width, int height)
385 {
386   rect->x = x;
387   rect->y = y;
388   rect->width  = width;
389   rect->height = height;
390 }
391
392 /*      Test if two frames overlap. If they do, return
393         intersection rect within old and location of
394         that rect within new. */
395
396 static Boolean
397 Intersect ( Pnt *old, Pnt *new, int size, MyRectangle *area, Pnt *pt)
398 {
399   if (old->x > new->x + size || new->x > old->x + size ||
400       old->y > new->y + size || new->y > old->y + size) {
401     return False;
402   } else {
403     SetRect(area, Max(new->x - old->x, 0), Max(new->y - old->y, 0),
404             size - abs(old->x - new->x), size - abs(old->y - new->y));
405     pt->x = Max(old->x - new->x, 0);
406     pt->y = Max(old->y - new->y, 0);
407     return True;
408   }
409 }
410
411 /*      For two overlapping frames, return the rect(s)
412         in the old that do not intersect with the new.   */
413
414 static void
415 CalcUpdateRects (Pnt *old, Pnt *new, int size, MyRectangle update[], int *nUpdates)
416 {
417   int        count;
418
419   /* If old = new (shouldn't happen) then nothing to draw */
420   if (old->x == new->x && old->y == new->y) {
421     *nUpdates = 0;
422     return;
423   }
424   /* Work out what bits overlap. Since we know the rects
425      are the same size we don't need a full intersect calc. */
426   count = 0;
427   /* Top or bottom edge? */
428   if (new->y > old->y) {
429     SetRect(&(update[count]), old->x, old->y, size, new->y - old->y);
430     count ++;
431   } else if (old->y > new->y) {
432     SetRect(&(update[count]), old->x, old->y + size - (old->y - new->y),
433                               size, old->y - new->y);
434     count ++;
435   }
436   /* Left or right edge - don't overlap any update calculated above. */
437   if (new->x > old->x) {
438     SetRect(&(update[count]), old->x, Max(new->y, old->y),
439                               new->x - old->x, size - abs(new->y - old->y));
440     count ++;
441   } else if (old->x > new->x) {
442     SetRect(&(update[count]), new->x + size, Max(new->y, old->y),
443                               old->x - new->x, size - abs(new->y - old->y));
444     count ++;
445   }
446   /* Done */
447   *nUpdates = count;
448 }
449
450 /* Animate the movement of a single piece */
451
452 static void
453 BeginAnimation (AnimNr anr, ChessSquare piece, ChessSquare bgPiece, int startColor, Pnt *start)
454 {
455   AnimState *anim = &anims[anr];
456
457   if(appData.upsideDown && flipView) piece += piece < BlackPawn ? BlackPawn : -BlackPawn;
458   /* The old buffer is initialised with the start square (empty) */
459   if(bgPiece == EmptySquare) {
460     DrawBlank(anr, start->x, start->y, startColor);
461   } else {
462        /* Kludge alert: When gating we want the introduced
463           piece to appear on the from square. To generate an
464           image of it, we draw it on the board, copy the image,
465           and draw the original piece again. */
466        if(piece != bgPiece) DrawSquare(anim->startBoardY, anim->startBoardX, bgPiece, 0);
467        CopyRectangle(anr, DISP, 2,
468                  start->x, start->y, squareSize, squareSize,
469                  0, 0); // [HGM] zh: unstack in stead of grab
470        if(piece != bgPiece) DrawSquare(anim->startBoardY, anim->startBoardX, piece, 0);
471   }
472   anim->prevFrame = *start;
473
474   SetDragPiece(anr, piece);
475 }
476
477 static void
478 AnimationFrame (AnimNr anr, Pnt *frame, ChessSquare piece)
479 {
480   MyRectangle updates[4];
481   MyRectangle overlap;
482   Pnt     pt;
483   AnimState *anim = &anims[anr];
484   int     count, i, x, y, w, h;
485
486   /* Save what we are about to draw into the new buffer */
487   CopyRectangle(anr, DISP, 0,
488             x = frame->x, y = frame->y, w = squareSize, h = squareSize,
489             0, 0);
490
491   /* Erase bits of the previous frame */
492   if (Intersect(&anim->prevFrame, frame, squareSize, &overlap, &pt)) {
493     /* Where the new frame overlapped the previous,
494        the contents in newBuf are wrong. */
495     CopyRectangle(anr, 2, 0,
496               overlap.x, overlap.y,
497               overlap.width, overlap.height,
498               pt.x, pt.y);
499     /* Repaint the areas in the old that don't overlap new */
500     CalcUpdateRects(&anim->prevFrame, frame, squareSize, updates, &count);
501     for (i = 0; i < count; i++)
502       CopyRectangle(anr, 2, DISP,
503                 updates[i].x - anim->prevFrame.x,
504                 updates[i].y - anim->prevFrame.y,
505                 updates[i].width, updates[i].height,
506                 updates[i].x, updates[i].y);
507     /* [HGM] correct expose rectangle to encompass both overlapping squares */
508     if(x > anim->prevFrame.x) w += x - anim->prevFrame.x, x = anim->prevFrame.x;
509     else  w += anim->prevFrame.x - x;
510     if(y > anim->prevFrame.y) h += y - anim->prevFrame.y, y = anim->prevFrame.y;
511     else  h += anim->prevFrame.y - y;
512   } else {
513     /* Easy when no overlap */
514     CopyRectangle(anr, 2, DISP,
515                   0, 0, squareSize, squareSize,
516                   anim->prevFrame.x, anim->prevFrame.y);
517     GraphExpose(currBoard, anim->prevFrame.x, anim->prevFrame.y, squareSize, squareSize);
518   }
519
520   /* Save this frame for next time round */
521   CopyRectangle(anr, 0, 2,
522                 0, 0, squareSize, squareSize,
523                 0, 0);
524   anim->prevFrame = *frame;
525
526   /* Draw piece over original screen contents, not current,
527      and copy entire rect. Wipes out overlapping piece images. */
528   InsertPiece(anr, piece);
529   CopyRectangle(anr, 0, DISP,
530                 0, 0, squareSize, squareSize,
531                 frame->x, frame->y);
532   GraphExpose(currBoard, x, y, w, h);
533 }
534
535 static void
536 EndAnimation (AnimNr anr, Pnt *finish)
537 {
538   MyRectangle updates[4];
539   MyRectangle overlap;
540   Pnt     pt;
541   int        count, i;
542   AnimState *anim = &anims[anr];
543
544   /* The main code will redraw the final square, so we
545      only need to erase the bits that don't overlap.    */
546   if (Intersect(&anim->prevFrame, finish, squareSize, &overlap, &pt)) {
547     CalcUpdateRects(&anim->prevFrame, finish, squareSize, updates, &count);
548     for (i = 0; i < count; i++)
549       CopyRectangle(anr, 2, DISP,
550                 updates[i].x - anim->prevFrame.x,
551                 updates[i].y - anim->prevFrame.y,
552                 updates[i].width, updates[i].height,
553                 updates[i].x, updates[i].y);
554   } else {
555     CopyRectangle(anr, 2, DISP,
556                 0, 0, squareSize, squareSize,
557                 anim->prevFrame.x, anim->prevFrame.y);
558   }
559 }
560
561 static void
562 FrameSequence (AnimNr anr, ChessSquare piece, int startColor, Pnt *start, Pnt *finish, Pnt frames[], int nFrames)
563 {
564   int n;
565
566   BeginAnimation(anr, piece, EmptySquare, startColor, start);
567   for (n = 0; n < nFrames; n++) {
568     AnimationFrame(anr, &(frames[n]), piece);
569     FrameDelay(appData.animSpeed);
570   }
571   EndAnimation(anr, finish);
572 }
573
574 void
575 AnimateAtomicCapture (Board board, int fromX, int fromY, int toX, int toY)
576 {
577     int i, x, y;
578     ChessSquare piece = board[fromY][toY];
579     board[fromY][toY] = EmptySquare;
580     DrawPosition(FALSE, board);
581     if (flipView) {
582         x = lineGap + ((BOARD_WIDTH-1)-toX) * (squareSize + lineGap);
583         y = lineGap + toY * (squareSize + lineGap);
584     } else {
585         x = lineGap + toX * (squareSize + lineGap);
586         y = lineGap + ((BOARD_HEIGHT-1)-toY) * (squareSize + lineGap);
587     }
588     for(i=1; i<4*kFactor; i++) {
589         int r = squareSize * 9 * i/(20*kFactor - 5);
590         DrawDot(1, x + squareSize/2 - r, y+squareSize/2 - r, 2*r);
591         FrameDelay(appData.animSpeed);
592     }
593     board[fromY][toY] = piece;
594     DrawGrid();
595 }
596
597 /* Main control logic for deciding what to animate and how */
598
599 void
600 AnimateMove (Board board, int fromX, int fromY, int toX, int toY)
601 {
602   ChessSquare piece;
603   int hop, x = toX, y = toY;
604   Pnt      start, finish, mid;
605   Pnt      frames[kFactor * 2 + 1];
606   int         nFrames, startColor, endColor;
607
608   if(killX >= 0 && IS_LION(board[fromY][fromX])) Roar();
609
610   /* Are we animating? */
611   if (!appData.animate || appData.blindfold)
612     return;
613
614   if(board[toY][toX] == WhiteRook && board[fromY][fromX] == WhiteKing ||
615      board[toY][toX] == BlackRook && board[fromY][fromX] == BlackKing ||
616      board[toY][toX] == WhiteKing && board[fromY][fromX] == WhiteRook || // [HGM] seirawan
617      board[toY][toX] == BlackKing && board[fromY][fromX] == BlackRook)
618         return; // [HGM] FRC: no animtion of FRC castlings, as to-square is not true to-square
619
620   if (fromY < 0 || fromX < 0 || toX < 0 || toY < 0) return;
621   piece = board[fromY][fromX];
622   if (piece >= EmptySquare) return;
623
624   if(killX >= 0) toX = killX, toY = killY; // [HGM] lion: first to kill square
625
626 again:
627
628 #if DONT_HOP
629   hop = FALSE;
630 #else
631   hop = abs(fromX-toX) == 1 && abs(fromY-toY) == 2 || abs(fromX-toX) == 2 && abs(fromY-toY) == 1;
632 #endif
633
634   ScreenSquare(fromX, fromY, &start, &startColor);
635   ScreenSquare(toX, toY, &finish, &endColor);
636
637   if (hop) {
638     /* Knight: make straight movement then diagonal */
639     if (abs(toY - fromY) < abs(toX - fromX)) {
640        mid.x = start.x + (finish.x - start.x) / 2;
641        mid.y = start.y;
642      } else {
643        mid.x = start.x;
644        mid.y = start.y + (finish.y - start.y) / 2;
645      }
646   } else {
647     mid.x = start.x + (finish.x - start.x) / 2;
648     mid.y = start.y + (finish.y - start.y) / 2;
649   }
650
651   /* Don't use as many frames for very short moves */
652   if (abs(toY - fromY) + abs(toX - fromX) <= 2)
653     Tween(&start, &mid, &finish, kFactor - 1, frames, &nFrames);
654   else
655     Tween(&start, &mid, &finish, kFactor, frames, &nFrames);
656   FrameSequence(Game, piece, startColor, &start, &finish, frames, nFrames);
657   if(Explode(board, fromX, fromY, toX, toY)) { // mark as damaged
658     int i,j;
659     for(i=0; i<BOARD_WIDTH; i++) for(j=0; j<BOARD_HEIGHT; j++)
660       if((i-toX)*(i-toX) + (j-toY)*(j-toY) < 6) damage[0][j][i] |=  1 + ((i-toX ^ j-toY) & 1);
661   }
662
663   /* Be sure end square is redrawn */
664   damage[0][toY][toX] |= True;
665
666   if(toX != x || toY != y) { fromX = toX; fromY = toY; toX = x; toY = y; goto again; } // second leg
667 }
668
669 void
670 ChangeDragPiece (ChessSquare piece)
671 {
672   anims[Player].dragPiece = piece;
673   SetDragPiece(Player, piece);
674   damage[0][fromY][fromX] = True;
675 }
676
677 void
678 DragPieceMove (int x, int y)
679 {
680     Pnt corner;
681
682     /* Are we animating? */
683     if (!appData.animateDragging || appData.blindfold)
684       return;
685
686     /* Sanity check */
687     if (! anims[Player].dragActive)
688       return;
689     /* Move piece, maintaining same relative position
690        of mouse within square    */
691     corner.x = x - anims[Player].mouseDelta.x;
692     corner.y = y - anims[Player].mouseDelta.y;
693     AnimationFrame(Player, &corner, anims[Player].dragPiece);
694 #if HIGHDRAG*0
695     if (appData.highlightDragging) {
696         int boardX, boardY;
697         BoardSquare(x, y, &boardX, &boardY);
698         SetHighlights(fromX, fromY, boardX, boardY);
699     }
700 #endif
701 }
702
703 void
704 DragPieceEnd (int x, int y)
705 {
706     int boardX, boardY, color;
707     Pnt corner;
708
709     /* Are we animating? */
710     if (!appData.animateDragging || appData.blindfold)
711       return;
712
713     /* Sanity check */
714     if (! anims[Player].dragActive)
715       return;
716     /* Last frame in sequence is square piece is
717        placed on, which may not match mouse exactly. */
718     BoardSquare(x, y, &boardX, &boardY);
719     ScreenSquare(boardX, boardY, &corner, &color);
720     EndAnimation(Player, &corner);
721
722     /* Be sure end square is redrawn */
723     damage[0][boardY][boardX] = True;
724
725     /* This prevents weird things happening with fast successive
726        clicks which on my Sun at least can cause motion events
727        without corresponding press/release. */
728     anims[Player].dragActive = False;
729 }
730
731 void
732 DragPieceBegin (int x, int y, Boolean instantly)
733 {
734     int  boardX, boardY, color;
735     Pnt corner;
736
737     /* Are we animating? */
738     if (!appData.animateDragging || appData.blindfold)
739       return;
740
741     /* Figure out which square we start in and the
742        mouse position relative to top left corner. */
743     BoardSquare(x, y, &boardX, &boardY);
744     anims[Player].startBoardX = boardX;
745     anims[Player].startBoardY = boardY;
746     ScreenSquare(boardX, boardY, &corner, &color);
747     anims[Player].startSquare  = corner;
748     anims[Player].startColor   = color;
749     /* As soon as we start dragging, the piece will jump slightly to
750        be centered over the mouse pointer. */
751     anims[Player].mouseDelta.x = squareSize/2;
752     anims[Player].mouseDelta.y = squareSize/2;
753     /* Initialise animation */
754     anims[Player].dragPiece = PieceForSquare(boardX, boardY);
755     /* Sanity check */
756     if (anims[Player].dragPiece >= 0 && anims[Player].dragPiece < EmptySquare) {
757         ChessSquare bgPiece = EmptySquare;
758         anims[Player].dragActive = True;
759         if(boardX == BOARD_RGHT+1 && PieceForSquare(boardX-1, boardY) > 1 ||
760            boardX == BOARD_LEFT-2 && PieceForSquare(boardX+1, boardY) > 1)
761             bgPiece = anims[Player].dragPiece;
762         if(gatingPiece != EmptySquare) bgPiece = gatingPiece;
763         BeginAnimation(Player, anims[Player].dragPiece, bgPiece, color, &corner);
764         /* Mark this square as needing to be redrawn. Note that
765            we don't remove the piece though, since logically (ie
766            as seen by opponent) the move hasn't been made yet. */
767         damage[0][boardY][boardX] = True;
768     } else {
769         anims[Player].dragActive = False;
770     }
771 }
772
773 /* Handle expose event while piece being dragged */
774
775 static void
776 DrawDragPiece ()
777 {
778   if (!anims[Player].dragActive || appData.blindfold)
779     return;
780
781   /* What we're doing: logically, the move hasn't been made yet,
782      so the piece is still in it's original square. But visually
783      it's being dragged around the board. So we erase the square
784      that the piece is on and draw it at the last known drag point. */
785   DrawOneSquare(anims[Player].startSquare.x, anims[Player].startSquare.y,
786                 EmptySquare, anims[Player].startColor, 0, NULL, NULL, 0);
787   AnimationFrame(Player, &anims[Player].prevFrame, anims[Player].dragPiece);
788   damage[0][anims[Player].startBoardY][anims[Player].startBoardX] = TRUE;
789 }
790
791 static void
792 DrawSquare (int row, int column, ChessSquare piece, int do_flash)
793 {
794     int square_color, x, y, align=0;
795     int i;
796     char tString[3], bString[2];
797     int flash_delay;
798
799     /* Calculate delay in milliseconds (2-delays per complete flash) */
800     flash_delay = 500 / appData.flashRate;
801
802     if (flipView) {
803         x = lineGap + ((BOARD_WIDTH-1)-column) *
804           (squareSize + lineGap);
805         y = lineGap + row * (squareSize + lineGap);
806     } else {
807         x = lineGap + column * (squareSize + lineGap);
808         y = lineGap + ((BOARD_HEIGHT-1)-row) *
809           (squareSize + lineGap);
810     }
811
812     square_color = SquareColor(row, column);
813
814     bString[1] = bString[0] = NULLCHAR;
815     if (appData.showCoords && row == (flipView ? BOARD_HEIGHT-1 : 0)
816                 && column >= BOARD_LEFT && column < BOARD_RGHT) {
817         bString[0] = 'a' + column - BOARD_LEFT;
818         align = 1; // coord in lower-right corner
819     }
820     if (appData.showCoords && column == (flipView ? BOARD_RGHT-1 : BOARD_LEFT)) {
821         snprintf(tString, 3, "%d", ONE - '0' + row);
822         align = 2; // coord in upper-left corner
823     }
824     if (column == (flipView ? BOARD_LEFT-1 : BOARD_RGHT) && piece > 1 ) {
825         snprintf(tString, 3, "%d", piece);
826         align = 3; // holdings count in upper-right corner
827     }
828     if (column == (flipView ? BOARD_RGHT : BOARD_LEFT-1) && piece > 1) {
829         snprintf(tString, 3, "%d", piece);
830         align = 4; // holdings count in upper-left corner
831     }
832     if(piece == DarkSquare) square_color = 2;
833     if(square_color == 2 || appData.blindfold) piece = EmptySquare;
834
835     if (do_flash && piece != EmptySquare && appData.flashCount > 0) {
836         for (i=0; i<appData.flashCount; ++i) {
837             DrawOneSquare(x, y, piece, square_color, 0, tString, bString, 0);
838             GraphExpose(currBoard, x, y, squareSize, squareSize);
839             FlashDelay(flash_delay);
840             DrawOneSquare(x, y, EmptySquare, square_color, 0, tString, bString, 0);
841             GraphExpose(currBoard, x, y, squareSize, squareSize);
842             FlashDelay(flash_delay);
843         }
844     }
845     DrawOneSquare(x, y, piece, square_color, partnerUp ? 0 : marker[row][column], tString, bString, align);
846 }
847
848 /* Returns 1 if there are "too many" differences between b1 and b2
849    (i.e. more than 1 move was made) */
850 static int
851 too_many_diffs (Board b1, Board b2)
852 {
853     int i, j;
854     int c = 0;
855
856     for (i=0; i<BOARD_HEIGHT; ++i) {
857         for (j=0; j<BOARD_WIDTH; ++j) {
858             if (b1[i][j] != b2[i][j]) {
859                 if (++c > 4)    /* Castling causes 4 diffs */
860                   return 1;
861             }
862         }
863     }
864     return 0;
865 }
866
867 /* Matrix describing castling maneuvers */
868 /* Row, ColRookFrom, ColKingFrom, ColRookTo, ColKingTo */
869 static int castling_matrix[4][5] = {
870     { 0, 0, 4, 3, 2 },          /* 0-0-0, white */
871     { 0, 7, 4, 5, 6 },          /* 0-0,   white */
872     { 7, 0, 4, 3, 2 },          /* 0-0-0, black */
873     { 7, 7, 4, 5, 6 }           /* 0-0,   black */
874 };
875
876 /* Checks whether castling occurred. If it did, *rrow and *rcol
877    are set to the destination (row,col) of the rook that moved.
878
879    Returns 1 if castling occurred, 0 if not.
880
881    Note: Only handles a max of 1 castling move, so be sure
882    to call too_many_diffs() first.
883    */
884 static int
885 check_castle_draw (Board newb, Board oldb, int *rrow, int *rcol)
886 {
887     int i, *r, j;
888     int match;
889
890     /* For each type of castling... */
891     for (i=0; i<4; ++i) {
892         r = castling_matrix[i];
893
894         /* Check the 4 squares involved in the castling move */
895         match = 0;
896         for (j=1; j<=4; ++j) {
897             if (newb[r[0]][r[j]] == oldb[r[0]][r[j]]) {
898                 match = 1;
899                 break;
900             }
901         }
902
903         if (!match) {
904             /* All 4 changed, so it must be a castling move */
905             *rrow = r[0];
906             *rcol = r[3];
907             return 1;
908         }
909     }
910     return 0;
911 }
912
913 void
914 DrawPosition (int repaint, Board board)
915 {
916     int i, j, do_flash, exposeAll = False;
917     static int lastFlipView = 0;
918     static int lastBoardValid[2] = {0, 0};
919     static Board lastBoard[2];
920     static char lastMarker[BOARD_RANKS][BOARD_FILES];
921     int rrow, rcol;
922     int nr = twoBoards*partnerUp;
923
924     if(DrawSeekGraph()) return; // [HGM] seekgraph: suppress any drawing if seek graph up
925
926     if (board == NULL) {
927         if (!lastBoardValid[nr]) return;
928         board = lastBoard[nr];
929     }
930     if (!lastBoardValid[nr] || (nr == 0 && lastFlipView != flipView)) {
931         MarkMenuItem("View.Flip View", flipView);
932     }
933
934     if(nr) { SlavePopUp(); SwitchWindow(0); } // [HGM] popup board if not yet popped up, and switch drawing to it.
935
936     /*
937      * It would be simpler to clear the window with XClearWindow()
938      * but this causes a very distracting flicker.
939      */
940
941     if (!repaint && lastBoardValid[nr] && (nr == 1 || lastFlipView == flipView)) {
942
943 //      if ( lineGap && IsDrawArrowEnabled())
944 //          DrawGrid();
945
946         /* If too much changes (begin observing new game, etc.), don't
947            do flashing */
948         do_flash = too_many_diffs(board, lastBoard[nr]) ? 0 : 1;
949
950         /* Special check for castling so we don't flash both the king
951            and the rook (just flash the king). */
952         if (do_flash) {
953             if (check_castle_draw(board, lastBoard[nr], &rrow, &rcol)) {
954                 /* Mark rook for drawing with NO flashing. */
955                 damage[nr][rrow][rcol] |= 1;
956             }
957         }
958
959         /* First pass -- Draw (newly) empty squares and repair damage.
960            This prevents you from having a piece show up twice while it
961            is flashing on its new square */
962         for (i = 0; i < BOARD_HEIGHT; i++)
963           for (j = 0; j < BOARD_WIDTH; j++)
964             if (((board[i][j] != lastBoard[nr][i][j] || !nr && marker[i][j] != lastMarker[i][j]) && board[i][j] == EmptySquare)
965                 || damage[nr][i][j]) {
966                 DrawSquare(i, j, board[i][j], 0);
967                 if(damage[nr][i][j] & 2) {
968                     drawHighlight(j, i, 0);   // repair arrow damage
969                     if(lineGap) damage[nr][i][j] = False; // this flushed the square as well
970                 } else damage[nr][i][j] = 1;  // mark for expose
971             }
972
973         /* Second pass -- Draw piece(s) in new position and flash them */
974         for (i = 0; i < BOARD_HEIGHT; i++)
975           for (j = 0; j < BOARD_WIDTH; j++)
976             if (board[i][j] != lastBoard[nr][i][j] || !nr && marker[i][j] != lastMarker[i][j]) {
977                 DrawSquare(i, j, board[i][j], do_flash);
978                 damage[nr][i][j] = 1; // mark for expose
979             }
980     } else {
981         if (lineGap > 0)
982           DrawGrid();
983
984         for (i = 0; i < BOARD_HEIGHT; i++)
985           for (j = 0; j < BOARD_WIDTH; j++) {
986               DrawSquare(i, j, board[i][j], 0);
987               damage[nr][i][j] = False;
988           }
989
990         exposeAll = True;
991     }
992
993     CopyBoard(lastBoard[nr], board);
994     lastBoardValid[nr] = 1;
995   if(nr == 0) { // [HGM] dual: no highlights on second board yet
996     lastFlipView = flipView;
997     for (i = 0; i < BOARD_HEIGHT; i++)
998         for (j = 0; j < BOARD_WIDTH; j++)
999             lastMarker[i][j] = marker[i][j];
1000
1001     /* Draw highlights */
1002     if (pm1X >= 0 && pm1Y >= 0) {
1003       drawHighlight(pm1X, pm1Y, 2);
1004       if(lineGap) damage[nr][pm1Y][pm1X] = False;
1005     }
1006     if (pm2X >= 0 && pm2Y >= 0) {
1007       drawHighlight(pm2X, pm2Y, 2);
1008       if(lineGap) damage[nr][pm2Y][pm2X] = False;
1009     }
1010     if (hi1X >= 0 && hi1Y >= 0) {
1011       drawHighlight(hi1X, hi1Y, 1);
1012       if(lineGap) damage[nr][hi1Y][hi1X] = False;
1013     }
1014     if (hi2X >= 0 && hi2Y >= 0) {
1015       drawHighlight(hi2X, hi2Y, 1);
1016       if(lineGap) damage[nr][hi2Y][hi2X] = False;
1017     }
1018     DrawArrowHighlight(hi1X, hi1Y, hi2X, hi2Y);
1019   }
1020   else DrawArrowHighlight (board[EP_STATUS-3], board[EP_STATUS-4], board[EP_STATUS-1], board[EP_STATUS-2]);
1021
1022     /* If piece being dragged around board, must redraw that too */
1023     DrawDragPiece();
1024
1025     if(exposeAll)
1026         GraphExpose(currBoard, 0, 0, BOARD_WIDTH*(squareSize + lineGap) + lineGap, BOARD_HEIGHT*(squareSize + lineGap) + lineGap);
1027     else {
1028         for (i = 0; i < BOARD_HEIGHT; i++)
1029             for (j = 0; j < BOARD_WIDTH; j++)
1030                 if(damage[nr][i][j]) {
1031                     int x, y;
1032                     if (flipView) {
1033                         x = lineGap + ((BOARD_WIDTH-1)-j) *
1034                           (squareSize + lineGap);
1035                         y = lineGap + i * (squareSize + lineGap);
1036                     } else {
1037                         x = lineGap + j * (squareSize + lineGap);
1038                         y = lineGap + ((BOARD_HEIGHT-1)-i) *
1039                           (squareSize + lineGap);
1040                     }
1041                     if(damage[nr][i][j] & 2) // damage by old or new arrow
1042                         GraphExpose(currBoard, x - lineGap, y - lineGap, squareSize + 2*lineGap, squareSize  + 2*lineGap);
1043                     else
1044                         GraphExpose(currBoard, x, y, squareSize, squareSize);
1045                     damage[nr][i][j] &= 2; // remember damage by newly drawn error in '2' bit, to schedule it for erasure next draw
1046                 }
1047     }
1048
1049     FlashDelay(0); // this flushes drawing queue;
1050     if(nr) SwitchWindow(1);
1051 }
1052
1053 /* [AS] Arrow highlighting support */
1054
1055 static double A_WIDTH = 5; /* Width of arrow body */
1056
1057 #define A_HEIGHT_FACTOR 6   /* Length of arrow "point", relative to body width */
1058 #define A_WIDTH_FACTOR  3   /* Width of arrow "point", relative to body width */
1059
1060 static double
1061 Sqr (double x)
1062 {
1063     return x*x;
1064 }
1065
1066 static int
1067 Round (double x)
1068 {
1069     return (int) (x + 0.5);
1070 }
1071
1072 void
1073 SquareToPos (int rank, int file, int *x, int *y)
1074 {
1075     if (flipView) {
1076         *x = lineGap + ((BOARD_WIDTH-1)-file) * (squareSize + lineGap);
1077         *y = lineGap + rank * (squareSize + lineGap);
1078     } else {
1079         *x = lineGap + file * (squareSize + lineGap);
1080         *y = lineGap + ((BOARD_HEIGHT-1)-rank) * (squareSize + lineGap);
1081     }
1082 }
1083
1084 /* Draw an arrow between two points using current settings */
1085 static void
1086 DrawArrowBetweenPoints (int s_x, int s_y, int d_x, int d_y)
1087 {
1088     Pnt arrow[8];
1089     double dx, dy, j, k, x, y;
1090
1091     if( d_x == s_x ) {
1092         int h = (d_y > s_y) ? +A_WIDTH*A_HEIGHT_FACTOR : -A_WIDTH*A_HEIGHT_FACTOR;
1093
1094         arrow[0].x = s_x + A_WIDTH + 0.5;
1095         arrow[0].y = s_y;
1096
1097         arrow[1].x = s_x + A_WIDTH + 0.5;
1098         arrow[1].y = d_y - h;
1099
1100         arrow[2].x = arrow[1].x + A_WIDTH*(A_WIDTH_FACTOR-1) + 0.5;
1101         arrow[2].y = d_y - h;
1102
1103         arrow[3].x = d_x;
1104         arrow[3].y = d_y;
1105
1106         arrow[5].x = arrow[1].x - 2*A_WIDTH + 0.5;
1107         arrow[5].y = d_y - h;
1108
1109         arrow[4].x = arrow[5].x - A_WIDTH*(A_WIDTH_FACTOR-1) + 0.5;
1110         arrow[4].y = d_y - h;
1111
1112         arrow[6].x = arrow[1].x - 2*A_WIDTH + 0.5;
1113         arrow[6].y = s_y;
1114     }
1115     else if( d_y == s_y ) {
1116         int w = (d_x > s_x) ? +A_WIDTH*A_HEIGHT_FACTOR : -A_WIDTH*A_HEIGHT_FACTOR;
1117
1118         arrow[0].x = s_x;
1119         arrow[0].y = s_y + A_WIDTH + 0.5;
1120
1121         arrow[1].x = d_x - w;
1122         arrow[1].y = s_y + A_WIDTH + 0.5;
1123
1124         arrow[2].x = d_x - w;
1125         arrow[2].y = arrow[1].y + A_WIDTH*(A_WIDTH_FACTOR-1) + 0.5;
1126
1127         arrow[3].x = d_x;
1128         arrow[3].y = d_y;
1129
1130         arrow[5].x = d_x - w;
1131         arrow[5].y = arrow[1].y - 2*A_WIDTH + 0.5;
1132
1133         arrow[4].x = d_x - w;
1134         arrow[4].y = arrow[5].y - A_WIDTH*(A_WIDTH_FACTOR-1) + 0.5;
1135
1136         arrow[6].x = s_x;
1137         arrow[6].y = arrow[1].y - 2*A_WIDTH + 0.5;
1138     }
1139     else {
1140         /* [AS] Needed a lot of paper for this! :-) */
1141         dy = (double) (d_y - s_y) / (double) (d_x - s_x);
1142         dx = (double) (s_x - d_x) / (double) (s_y - d_y);
1143
1144         j = sqrt( Sqr(A_WIDTH) / (1.0 + Sqr(dx)) );
1145
1146         k = sqrt( Sqr(A_WIDTH*A_HEIGHT_FACTOR) / (1.0 + Sqr(dy)) );
1147
1148         x = s_x;
1149         y = s_y;
1150
1151         arrow[0].x = Round(x - j);
1152         arrow[0].y = Round(y + j*dx);
1153
1154         arrow[1].x = Round(arrow[0].x + 2*j);   // [HGM] prevent width to be affected by rounding twice
1155         arrow[1].y = Round(arrow[0].y - 2*j*dx);
1156
1157         if( d_x > s_x ) {
1158             x = (double) d_x - k;
1159             y = (double) d_y - k*dy;
1160         }
1161         else {
1162             x = (double) d_x + k;
1163             y = (double) d_y + k*dy;
1164         }
1165
1166         x = Round(x); y = Round(y); // [HGM] make sure width of shaft is rounded the same way on both ends
1167
1168         arrow[6].x = Round(x - j);
1169         arrow[6].y = Round(y + j*dx);
1170
1171         arrow[2].x = Round(arrow[6].x + 2*j);
1172         arrow[2].y = Round(arrow[6].y - 2*j*dx);
1173
1174         arrow[3].x = Round(arrow[2].x + j*(A_WIDTH_FACTOR-1));
1175         arrow[3].y = Round(arrow[2].y - j*(A_WIDTH_FACTOR-1)*dx);
1176
1177         arrow[4].x = d_x;
1178         arrow[4].y = d_y;
1179
1180         arrow[5].x = Round(arrow[6].x - j*(A_WIDTH_FACTOR-1));
1181         arrow[5].y = Round(arrow[6].y + j*(A_WIDTH_FACTOR-1)*dx);
1182     }
1183
1184     DrawPolygon(arrow, 7);
1185 //    Polygon( hdc, arrow, 7 );
1186 }
1187
1188 static void
1189 ArrowDamage (int s_col, int s_row, int d_col, int d_row)
1190 {
1191     int hor, vert, i, n = partnerUp * twoBoards;
1192     hor = 64*s_col + 32; vert = 64*s_row + 32;
1193     for(i=0; i<= 64; i++) {
1194             damage[n][vert+8>>6][hor+8>>6] |= 2;
1195             damage[n][vert-8>>6][hor+8>>6] |= 2;
1196             damage[n][vert+8>>6][hor-8>>6] |= 2;
1197             damage[n][vert-8>>6][hor-8>>6] |= 2;
1198             hor += d_col - s_col; vert += d_row - s_row;
1199     }
1200 }
1201
1202 /* [AS] Draw an arrow between two squares */
1203 static void
1204 DrawArrowBetweenSquares (int s_col, int s_row, int d_col, int d_row)
1205 {
1206     int s_x, s_y, d_x, d_y;
1207
1208     if( s_col == d_col && s_row == d_row ) {
1209         return;
1210     }
1211
1212     /* Get source and destination points */
1213     SquareToPos( s_row, s_col, &s_x, &s_y);
1214     SquareToPos( d_row, d_col, &d_x, &d_y);
1215
1216     if( d_y > s_y && d_y - s_y > abs(d_x - s_x)/2) {
1217         d_y += squareSize / 2 - squareSize / 4; // [HGM] round towards same centers on all sides!
1218     }
1219     else if( d_y < s_y && s_y - d_y > abs(d_x - d_y)/2) {
1220         d_y += squareSize / 2 + squareSize / 4;
1221     }
1222     else {
1223         d_y += squareSize / 2;
1224     }
1225
1226     if( d_x > s_x && d_x - s_x > abs(d_y - s_y)/2) {
1227         d_x += squareSize / 2 - squareSize / 4;
1228     }
1229     else if( d_x < s_x && s_x - d_x > abs(d_y - s_y)/2) {
1230         d_x += squareSize / 2 + squareSize / 4;
1231     }
1232     else {
1233         d_x += squareSize / 2;
1234     }
1235
1236     s_x += squareSize / 2;
1237     s_y += squareSize / 2;
1238
1239     /* Adjust width */
1240     A_WIDTH = squareSize / 14.; //[HGM] make float
1241
1242     DrawArrowBetweenPoints( s_x, s_y, d_x, d_y );
1243     ArrowDamage(s_col, s_row, d_col, d_row);
1244 }
1245
1246 static Boolean
1247 IsDrawArrowEnabled ()
1248 {
1249     return (appData.highlightMoveWithArrow || twoBoards && partnerUp) && squareSize >= 32;
1250 }
1251
1252 static void
1253 DrawArrowHighlight (int fromX, int fromY, int toX,int toY)
1254 {
1255     if( IsDrawArrowEnabled() && fromX >= 0 && fromY >= 0 && toX >= 0 && toY >= 0)
1256         DrawArrowBetweenSquares(fromX, fromY, toX, toY);
1257 }