| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
%% mpchess.mp %% |
| 3 |
%% draw chessboards with metapost %% |
| 4 |
%% [email protected] %% |
| 5 |
%% Version 0.9 (decembre 2024) %% |
| 6 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7 |
% |
| 8 |
% This work may be distributed and/or modified under the conditions of |
| 9 |
% the LaTeX Project Public License, either version 1.3c of this license |
| 10 |
% or (at your option) any later version. The latest version of this |
| 11 |
% license is in http://www.latex-project.org/lppl.txt |
| 12 |
% |
| 13 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 14 |
|
| 15 |
% Don't load this package twice: |
| 16 |
if known mpchess_version: expandafter endinput; fi; |
| 17 |
|
| 18 |
numeric mpchess_version;string mpchess_date; |
| 19 |
mpchess_version=0.9; |
| 20 |
mpchess_date="2024/12/16"; |
| 21 |
% The banner: |
| 22 |
message "******* mpchess " & decimal (mpchess_version) & |
| 23 |
" (c) Maxime Chupin (" & mpchess_date & ") *******";message ""; |
| 24 |
|
| 25 |
|
| 26 |
%input format; |
| 27 |
input hatching; |
| 28 |
%input minim-mp; |
| 29 |
%input minim-hatching; |
| 30 |
input mpchess-pieces; % piece of mpchess package |
| 31 |
input mpchess-cburnett; % piece of lichess cburnett |
| 32 |
input mpchess-skak; % piece of skak package |
| 33 |
input mpchess-chessboard; |
| 34 |
input mpchess-pgn; |
| 35 |
input mpchess-fen; |
| 36 |
input fiziko; |
| 37 |
|
| 38 |
if not known mplib: input latexmp fi; |
| 39 |
|
| 40 |
warningcheck:=0; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
% pieces |
| 45 |
picture _chess_bB, _chess_bK, _chess_bN,_chess_bP, _chess_bQ, _chess_bR; |
| 46 |
picture _chess_wB, _chess_wK, _chess_wN,_chess_wP, _chess_wQ, _chess_wR; |
| 47 |
|
| 48 |
% coordinates font |
| 49 |
string _coords_font; |
| 50 |
_coords_font:=defaultfont; |
| 51 |
|
| 52 |
% chose the font to write coordinates on board |
| 53 |
def set_coordinates_font(expr f)= |
| 54 |
_coords_font:=f ; |
| 55 |
enddef; |
| 56 |
|
| 57 |
% player name |
| 58 |
string _chess_white_player, _chess_black_player; |
| 59 |
_chess_white_player:=""; |
| 60 |
_chess_black_player:=""; |
| 61 |
|
| 62 |
def set_white_player(expr s)= |
| 63 |
_chess_white_player:=s; |
| 64 |
enddef; |
| 65 |
|
| 66 |
|
| 67 |
def set_black_player(expr s)= |
| 68 |
_chess_black_player:=s; |
| 69 |
enddef; |
| 70 |
|
| 71 |
% boolean to choose the side view |
| 72 |
boolean _chess_view_wb; |
| 73 |
_chess_view_wb := true; % true for white |
| 74 |
|
| 75 |
def set_black_view= |
| 76 |
_chess_view_wb := false; |
| 77 |
enddef; |
| 78 |
|
| 79 |
def set_white_view= |
| 80 |
_chess_view_wb := true; |
| 81 |
enddef; |
| 82 |
|
| 83 |
boolean _chess_players_side; |
| 84 |
_chess_players_side:=false; |
| 85 |
def set_players_side= |
| 86 |
_chess_players_side:=true; |
| 87 |
enddef; |
| 88 |
|
| 89 |
% boolean to choose if we print coordinates |
| 90 |
boolean _chess_print_coords,_chess_coords_inout; |
| 91 |
_chess_print_coords:=true; |
| 92 |
_chess_coords_inout:=true; %true for inside |
| 93 |
def hide_coordinates= |
| 94 |
_chess_print_coords:=false; |
| 95 |
enddef; |
| 96 |
|
| 97 |
def show_coordinates= |
| 98 |
_chess_print_coords:=true; |
| 99 |
enddef; |
| 100 |
|
| 101 |
def set_coordinates_position(expr s)= |
| 102 |
if(s="inside"): |
| 103 |
_chess_coords_inout:=true; |
| 104 |
elseif(s="outside"): |
| 105 |
_chess_coords_inout:=false; |
| 106 |
fi |
| 107 |
enddef; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
% boolean for white or black to move |
| 113 |
boolean _white_to_move; |
| 114 |
_white_to_move:=true; |
| 115 |
|
| 116 |
def set_black_to_move= |
| 117 |
_white_to_move:=false; |
| 118 |
enddef; |
| 119 |
|
| 120 |
def set_white_to_move= |
| 121 |
_white_to_move:=true; |
| 122 |
enddef; |
| 123 |
|
| 124 |
% boolean to show a mark to indicate whos to move |
| 125 |
boolean _show_whos_to_move; |
| 126 |
_show_whos_to_move:=true; |
| 127 |
|
| 128 |
def show_whos_to_move= |
| 129 |
_show_whos_to_move:=true; |
| 130 |
enddef; |
| 131 |
|
| 132 |
def hide_whos_to_move= |
| 133 |
_show_whos_to_move:=false; |
| 134 |
enddef; |
| 135 |
|
| 136 |
|
| 137 |
% integer correspondance : |
| 138 |
_intWPawn:=1; |
| 139 |
_intBPawn:=-1; |
| 140 |
_intWKnight:=2; |
| 141 |
_intBKnight:=-2; |
| 142 |
_intWBishop:=3; |
| 143 |
_intBBishop:=-3; |
| 144 |
_intWRook:=4; |
| 145 |
_intBRook:=-4; |
| 146 |
_intWQueen:=5; |
| 147 |
_intBQueen:=-5; |
| 148 |
_intWKing:=6; |
| 149 |
_intBKing:=-6; |
| 150 |
|
| 151 |
|
| 152 |
% macro to get thi ith char of a string |
| 153 |
vardef _get_char(expr s,i)= |
| 154 |
save output; |
| 155 |
string output; |
| 156 |
output:=substring(i,i+1) of s; |
| 157 |
output |
| 158 |
enddef; |
| 159 |
|
| 160 |
% size of the chessboard |
| 161 |
numeric _chessSize; |
| 162 |
_chessSize:=8; |
| 163 |
|
| 164 |
def set_backboard_size(expr w)= |
| 165 |
_chessSize:=w; |
| 166 |
enddef; |
| 167 |
|
| 168 |
|
| 169 |
def get_backboard_size= |
| 170 |
_chessSize |
| 171 |
enddef; |
| 172 |
|
| 173 |
% unit default |
| 174 |
numeric _chessWidth,_chessSquareU; |
| 175 |
_chessWidth:=5cm; |
| 176 |
_chessSquareU:=_chessWidth/_chessSize; |
| 177 |
|
| 178 |
def set_backboard_width(expr w)= |
| 179 |
_chessWidth:=w; |
| 180 |
enddef; |
| 181 |
|
| 182 |
|
| 183 |
def get_square_dim= |
| 184 |
_chessSquareU |
| 185 |
enddef; |
| 186 |
|
| 187 |
def get_backboard_width= |
| 188 |
_chessWidth |
| 189 |
enddef; |
| 190 |
|
| 191 |
% theme |
| 192 |
string _chessTheme; |
| 193 |
% default |
| 194 |
_chessTheme := "BlueLichess"; |
| 195 |
|
| 196 |
% color of black and white square |
| 197 |
color _blackColorSquare,_whiteColorSquare; |
| 198 |
% default |
| 199 |
_blackColorSquare:=(0.549,0.6353,0.6784); |
| 200 |
_whiteColorSquare:=(0.8706,0.8912,0.902); |
| 201 |
|
| 202 |
color _chess_main_lines_color; |
| 203 |
_chess_main_lines_color:=0.3[_blackColorSquare,black]; |
| 204 |
|
| 205 |
def set_main_lines_color(expr c)= |
| 206 |
_chess_main_lines_color:=c; |
| 207 |
enddef; |
| 208 |
|
| 209 |
|
| 210 |
picture _blackSquarePic, _whiteSquarePic; |
| 211 |
picture _blackSquareColoredPic, _whiteSquareColoredPic; |
| 212 |
picture _blackSquareClassicalPic, _whiteSquareClassicalPic; |
| 213 |
|
| 214 |
def _wood_draw(expr N,d,p,c,_SquareUnit)= |
| 215 |
save _up_wood,_down_wood; |
| 216 |
path _up_wood,_down_wood; |
| 217 |
fill unitsquare scaled _SquareUnit withcolor c; |
| 218 |
for i=1 upto N-1: |
| 219 |
_up_wood:=(i/N+uniformdeviate(d),0.0+0.5p/_SquareUnit)*_SquareUnit--(i/N,0.0+0.5p/_SquareUnit)*_SquareUnit--(i/N,0.5)*_SquareUnit--(i/N+uniformdeviate(d),0.7+uniformdeviate(.3)-p/_SquareUnit)*_SquareUnit--cycle; |
| 220 |
fill _up_wood withcolor ((0.9+uniformdeviate |
| 221 |
.25)[white,c]); |
| 222 |
_down_wood:=((i-1)/N+uniformdeviate(d)+0.5/N,1.0-0.5p/_SquareUnit)*_SquareUnit--((i-1)/N+0.5/N,1.0-0.5p/_SquareUnit)*_SquareUnit--((i-1)/N+0.5/N,0.5)*_SquareUnit--((i-1)/N+uniformdeviate(d)+0.5/N,0.3-uniformdeviate(.3)+p/_SquareUnit)*_SquareUnit--cycle; |
| 223 |
fill _down_wood withcolor ((0.9+uniformdeviate |
| 224 |
.25)[white,c]); |
| 225 |
endfor; |
| 226 |
enddef; |
| 227 |
|
| 228 |
def _buildColoredSquare(expr _SquareUnit,_BlackColor,_WhiteColor)= |
| 229 |
if(_chess_board_type="flat"): |
| 230 |
_blackSquarePic:=image( |
| 231 |
fill unitsquare scaled _SquareUnit withcolor _BlackColor; |
| 232 |
); |
| 233 |
_whiteSquarePic:=image( |
| 234 |
fill unitsquare scaled _SquareUnit withcolor _WhiteColor; |
| 235 |
); |
| 236 |
elseif(_chess_board_type="pyramidal"): |
| 237 |
_blackSquarePic:=image( |
| 238 |
fill unitsquare scaled _SquareUnit withcolor _BlackColor; |
| 239 |
fill (1,0)--(1,1)--(0.5,0.5)--cycle scaled _SquareUnit withcolor |
| 240 |
0.97[black,_BlackColor]; |
| 241 |
fill (0,1)--(0,0)--(0.5,0.5)--cycle scaled _SquareUnit withcolor |
| 242 |
0.97[white,_BlackColor]; |
| 243 |
); |
| 244 |
_whiteSquarePic:=image( |
| 245 |
fill unitsquare scaled _SquareUnit withcolor _WhiteColor; |
| 246 |
fill (1,0)--(1,1)--(0.5,0.5)--cycle scaled _SquareUnit withcolor |
| 247 |
0.97[black,_WhiteColor]; |
| 248 |
fill (0,1)--(0,0)--(0.5,0.5)--cycle scaled _SquareUnit withcolor |
| 249 |
0.97[white,_WhiteColor]; |
| 250 |
); |
| 251 |
elseif(_chess_board_type="wood"): |
| 252 |
_N_wood:=20; |
| 253 |
_deviate_wood:=0.02; |
| 254 |
_pencile_wood:=0.7pt; |
| 255 |
path _up_wood,_down_wood,_q,_p; |
| 256 |
_blackSquarePic:=image( |
| 257 |
_wood_draw(_N_wood,_deviate_wood,_pencile_wood,_BlackColor,_SquareUnit); |
| 258 |
); |
| 259 |
_whiteSquarePic:=image(_wood_draw(_N_wood,_deviate_wood,_pencile_wood,_WhiteColor,_SquareUnit); |
| 260 |
); |
| 261 |
fi |
| 262 |
enddef; |
| 263 |
|
| 264 |
def _buildClassicalSquare(expr _SquareUnit)= |
| 265 |
_blackSquarePic:=image( |
| 266 |
hatchfill (unitsquare scaled _SquareUnit) withcolor (white) withcolor (45,0.65*_SquareUnit/6 bp,-0.2bp); |
| 267 |
); |
| 268 |
_whiteSquarePic:=image( |
| 269 |
fill unitsquare scaled _SquareUnit withcolor white; |
| 270 |
); |
| 271 |
enddef; |
| 272 |
|
| 273 |
def _buildDottedSquare(expr _SquareUnit)= |
| 274 |
_blackSquarePic:=image( |
| 275 |
fill (unitsquare scaled _SquareUnit) withcolor white; |
| 276 |
save N; |
| 277 |
N:= 11; |
| 278 |
for i:=0 upto N+1: |
| 279 |
for j:=0 upto N+1: |
| 280 |
fill (fullcircle shifted (1/N*i*_SquareUnit if odd(j): +1/(2*N)*_SquareUnit fi,1/N*j*_SquareUnit)) |
| 281 |
; |
| 282 |
endfor; |
| 283 |
endfor; |
| 284 |
draw (unitsquare scaled _SquareUnit); |
| 285 |
clip currentpicture to (unitsquare scaled _SquareUnit); |
| 286 |
); |
| 287 |
_whiteSquarePic:=image( |
| 288 |
fill unitsquare scaled _SquareUnit withcolor white; |
| 289 |
draw (unitsquare scaled _SquareUnit); |
| 290 |
clip currentpicture to (unitsquare scaled _SquareUnit); |
| 291 |
); |
| 292 |
enddef; |
| 293 |
|
| 294 |
% default theme lichess blue |
| 295 |
_blackSquarePic:=_blackSquareColoredPic; |
| 296 |
_whiteSquarePic:=_whiteSquareColoredPic; |
| 297 |
|
| 298 |
def set_color_theme(expr t)= |
| 299 |
_chessTheme := t; |
| 300 |
if(t="BlueLichess"): |
| 301 |
_chess_board_type:="flat"; |
| 302 |
_blackColorSquare:=(0.549,0.6353,0.6784); |
| 303 |
_whiteColorSquare:=(0.8706,0.8912,0.902); |
| 304 |
elseif(t="BrownLichess"): |
| 305 |
_chess_board_type:="flat"; |
| 306 |
_blackColorSquare:=(0.709803922,0.5333,0.3882); |
| 307 |
_whiteColorSquare:=(0.941176471,0.850980392,0.709803922); |
| 308 |
elseif(t="GreenLichess"): |
| 309 |
_chess_board_type:="flat"; |
| 310 |
_blackColorSquare:=(0.5255,0.651,0.4); |
| 311 |
_whiteColorSquare:=(1.0,1.0,0.8666); |
| 312 |
elseif(t="PinkPyramidalLichess"): |
| 313 |
_chess_board_type:="pyramidal"; |
| 314 |
_blackColorSquare:=(0.949,0.451,0.451); |
| 315 |
_whiteColorSquare:=(0.9294,0.9333,0.73333); |
| 316 |
elseif(t="Wood"): |
| 317 |
_chess_board_type:="wood"; |
| 318 |
_blackColorSquare:=(0.4902,0.3569,0.2118); |
| 319 |
_whiteColorSquare:=(0.8039,0.7843,0.7725); |
| 320 |
elseif(t="Classical"): |
| 321 |
_blackColorSquare:=black; |
| 322 |
_whiteColorSquare:=black; |
| 323 |
elseif(t="Dotted"): |
| 324 |
_blackColorSquare:=black; |
| 325 |
_whiteColorSquare:=black; |
| 326 |
fi |
| 327 |
enddef; |
| 328 |
|
| 329 |
def set_white_squares_color(expr c)= |
| 330 |
_whiteColorSquare:=c; |
| 331 |
enddef; |
| 332 |
|
| 333 |
def set_black_squares_color(expr c)= |
| 334 |
_blackColorSquare:=c; |
| 335 |
enddef; |
| 336 |
|
| 337 |
_chess_bB:=_chess_mpchess_bB; |
| 338 |
_chess_bK:=_chess_mpchess_bK; |
| 339 |
_chess_bN:=_chess_mpchess_bN; |
| 340 |
_chess_bP:=_chess_mpchess_bP; |
| 341 |
_chess_bQ:=_chess_mpchess_bQ; |
| 342 |
_chess_bR:=_chess_mpchess_bR; |
| 343 |
|
| 344 |
|
| 345 |
_chess_wB:=_chess_mpchess_wB; |
| 346 |
_chess_wK:=_chess_mpchess_wK; |
| 347 |
_chess_wN:=_chess_mpchess_wN; |
| 348 |
_chess_wP:=_chess_mpchess_wP; |
| 349 |
_chess_wQ:=_chess_mpchess_wQ; |
| 350 |
_chess_wR:=_chess_mpchess_wR; |
| 351 |
|
| 352 |
def set_pieces_theme(expr t)= |
| 353 |
if(t="mpchess"): |
| 354 |
_chess_bB:=_chess_mpchess_bB; |
| 355 |
_chess_bK:=_chess_mpchess_bK; |
| 356 |
_chess_bN:=_chess_mpchess_bN; |
| 357 |
_chess_bP:=_chess_mpchess_bP; |
| 358 |
_chess_bQ:=_chess_mpchess_bQ; |
| 359 |
_chess_bR:=_chess_mpchess_bR; |
| 360 |
|
| 361 |
|
| 362 |
_chess_wB:=_chess_mpchess_wB; |
| 363 |
_chess_wK:=_chess_mpchess_wK; |
| 364 |
_chess_wN:=_chess_mpchess_wN; |
| 365 |
_chess_wP:=_chess_mpchess_wP; |
| 366 |
_chess_wQ:=_chess_mpchess_wQ; |
| 367 |
_chess_wR:=_chess_mpchess_wR; |
| 368 |
elseif(t="cburnett"): |
| 369 |
_chess_bB:=_chess_cburnett_bB; |
| 370 |
_chess_bK:=_chess_cburnett_bK; |
| 371 |
_chess_bN:=_chess_cburnett_bN; |
| 372 |
_chess_bP:=_chess_cburnett_bP; |
| 373 |
_chess_bQ:=_chess_cburnett_bQ; |
| 374 |
_chess_bR:=_chess_cburnett_bR; |
| 375 |
|
| 376 |
|
| 377 |
_chess_wB:=_chess_cburnett_wB; |
| 378 |
_chess_wK:=_chess_cburnett_wK; |
| 379 |
_chess_wN:=_chess_cburnett_wN; |
| 380 |
_chess_wP:=_chess_cburnett_wP; |
| 381 |
_chess_wQ:=_chess_cburnett_wQ; |
| 382 |
_chess_wR:=_chess_cburnett_wR; |
| 383 |
elseif(t="skak"): |
| 384 |
_chess_bB:=_chess_skak_bB; |
| 385 |
_chess_bK:=_chess_skak_bK; |
| 386 |
_chess_bN:=_chess_skak_bN; |
| 387 |
_chess_bP:=_chess_skak_bP; |
| 388 |
_chess_bQ:=_chess_skak_bQ; |
| 389 |
_chess_bR:=_chess_skak_bR; |
| 390 |
|
| 391 |
|
| 392 |
_chess_wB:=_chess_skak_wB; |
| 393 |
_chess_wK:=_chess_skak_wK; |
| 394 |
_chess_wN:=_chess_skak_wN; |
| 395 |
_chess_wP:=_chess_skak_wP; |
| 396 |
_chess_wQ:=_chess_skak_wQ; |
| 397 |
_chess_wR:=_chess_skak_wR; |
| 398 |
fi |
| 399 |
enddef; |
| 400 |
|
| 401 |
|
| 402 |
def _init_piece(expr w)= |
| 403 |
begingroup |
| 404 |
bboxmargin:=0pt; |
| 405 |
_chess_marginBottom:=0.13w; |
| 406 |
% the queen and king as reference |
| 407 |
%bQ |
| 408 |
_chess_bQ_width:=abs((urcorner _chess_bQ)-(ulcorner _chess_bQ)); |
| 409 |
_chess_bQ_height:=abs((urcorner _chess_bQ)-(lrcorner _chess_bQ)); |
| 410 |
_chess_bK_width:=abs((urcorner _chess_bK)-(ulcorner _chess_bK)); |
| 411 |
_chess_bK_height:=abs((urcorner _chess_bK)-(lrcorner _chess_bK)); |
| 412 |
_chess_max_wh:=max(_chess_bQ_height,_chess_bQ_width,_chess_bK_height,_chess_bK_width); |
| 413 |
ratio:= (0.8w/_chess_max_wh); |
| 414 |
_chess_bQ:=((_chess_bQ shifted -(llcorner _chess_bQ-(bboxmargin,bboxmargin))) scaled ratio ); |
| 415 |
_chess_bQ_width_new:=abs((urcorner _chess_bQ)-(ulcorner _chess_bQ)); |
| 416 |
_chess_bQ:= _chess_bQ shifted ((w-_chess_bQ_width_new)*0.5,_chess_marginBottom) ; |
| 417 |
%bB |
| 418 |
_chess_bB:=((_chess_bB shifted -(llcorner _chess_bB-(bboxmargin,bboxmargin))) scaled ratio ); |
| 419 |
_chess_bB_width:=abs((urcorner _chess_bB)-(ulcorner _chess_bB)); |
| 420 |
_chess_bB:=_chess_bB shifted ((w-_chess_bB_width)*0.5,_chess_marginBottom) ; |
| 421 |
%bK |
| 422 |
_chess_bK:=((_chess_bK shifted -(llcorner _chess_bK-(bboxmargin,bboxmargin))) scaled ratio ); |
| 423 |
_chess_bK_width:=abs((urcorner _chess_bK)-(ulcorner _chess_bK)); |
| 424 |
_chess_bK:=_chess_bK shifted ((w-_chess_bK_width)*0.5,_chess_marginBottom) ; |
| 425 |
%bN |
| 426 |
_chess_bN:=((_chess_bN shifted -(llcorner _chess_bN-(bboxmargin,bboxmargin))) scaled ratio ); |
| 427 |
_chess_bN_width:=abs((urcorner _chess_bN)-(ulcorner _chess_bN)); |
| 428 |
_chess_bN:=_chess_bN shifted ((w-_chess_bN_width)*0.5,_chess_marginBottom) ; |
| 429 |
%bP |
| 430 |
_chess_bP:=((_chess_bP shifted -(llcorner _chess_bP-(bboxmargin,bboxmargin))) scaled ratio ); |
| 431 |
_chess_bP_width:=abs((urcorner _chess_bP)-(ulcorner _chess_bP)); |
| 432 |
_chess_bP:=_chess_bP shifted ((w-_chess_bP_width)*0.5,_chess_marginBottom) ; |
| 433 |
%bR |
| 434 |
_chess_bR:=((_chess_bR shifted -(llcorner _chess_bR-(bboxmargin,bboxmargin))) scaled ratio ); |
| 435 |
_chess_bR_width:=abs((urcorner _chess_bR)-(ulcorner _chess_bR)); |
| 436 |
_chess_bR:=_chess_bR shifted ((w-_chess_bR_width)*0.5,_chess_marginBottom) ; |
| 437 |
%wQ |
| 438 |
_chess_wQ:=((_chess_wQ shifted -(llcorner _chess_wQ-(bboxmargin,bboxmargin))) scaled ratio ); |
| 439 |
_chess_wQ_width:=abs((urcorner _chess_wQ)-(ulcorner _chess_wQ)); |
| 440 |
_chess_wQ:=_chess_wQ shifted ((w-_chess_wQ_width)*0.5,_chess_marginBottom) ; |
| 441 |
%wB |
| 442 |
_chess_wB:=((_chess_wB shifted -(llcorner _chess_wB-(bboxmargin,bboxmargin))) scaled ratio ); |
| 443 |
_chess_wB_width:=abs((urcorner _chess_wB)-(ulcorner _chess_wB)); |
| 444 |
_chess_wB:=_chess_wB shifted ((w-_chess_wB_width)*0.5,_chess_marginBottom) ; |
| 445 |
%wK |
| 446 |
_chess_wK:=((_chess_wK shifted -(llcorner _chess_wK-(bboxmargin,bboxmargin))) scaled ratio ); |
| 447 |
_chess_wK_width:=abs((urcorner _chess_wK)-(ulcorner _chess_wK)); |
| 448 |
_chess_wK:=_chess_wK shifted ((w-_chess_wK_width)*0.5,_chess_marginBottom) ; |
| 449 |
%wN |
| 450 |
_chess_wN:=((_chess_wN shifted -(llcorner _chess_wN-(bboxmargin,bboxmargin))) scaled ratio ); |
| 451 |
_chess_wN_width:=abs((urcorner _chess_wN)-(ulcorner _chess_wN)); |
| 452 |
_chess_wN:=_chess_wN shifted ((w-_chess_wN_width)*0.5,_chess_marginBottom) ; |
| 453 |
%wP |
| 454 |
_chess_wP:=((_chess_wP shifted -(llcorner _chess_wP-(bboxmargin,bboxmargin))) scaled ratio ); |
| 455 |
_chess_wP_width:=abs((urcorner _chess_wP)-(ulcorner _chess_wP)); |
| 456 |
_chess_wP:=_chess_wP shifted ((w-_chess_wP_width)*0.5,_chess_marginBottom) ; |
| 457 |
%wR |
| 458 |
_chess_wR:=((_chess_wR shifted -(llcorner _chess_wR-(bboxmargin,bboxmargin))) scaled ratio ); |
| 459 |
_chess_wR_width:=abs((urcorner _chess_wR)-(ulcorner _chess_wR)); |
| 460 |
_chess_wR:=_chess_wR shifted ((w-_chess_wR_width)*0.5,_chess_marginBottom) ; |
| 461 |
endgroup |
| 462 |
enddef; |
| 463 |
|
| 464 |
def _int_to_letter_w(expr i)= |
| 465 |
if(i=1): |
| 466 |
"a" |
| 467 |
elseif(i=2): |
| 468 |
"b" |
| 469 |
elseif(i=3): |
| 470 |
"c" |
| 471 |
elseif(i=4): |
| 472 |
"d" |
| 473 |
elseif(i=5): |
| 474 |
"e" |
| 475 |
elseif(i=6): |
| 476 |
"f" |
| 477 |
elseif(i=7): |
| 478 |
"g" |
| 479 |
elseif(i=8): |
| 480 |
"h" |
| 481 |
fi |
| 482 |
enddef; |
| 483 |
|
| 484 |
def _int_to_letter_b(expr i)= |
| 485 |
if(i=1): |
| 486 |
"h" |
| 487 |
elseif(i=2): |
| 488 |
"g" |
| 489 |
elseif(i=3): |
| 490 |
"f" |
| 491 |
elseif(i=4): |
| 492 |
"e" |
| 493 |
elseif(i=5): |
| 494 |
"d" |
| 495 |
elseif(i=6): |
| 496 |
"c" |
| 497 |
elseif(i=7): |
| 498 |
"b" |
| 499 |
elseif(i=8): |
| 500 |
"a" |
| 501 |
fi |
| 502 |
enddef; |
| 503 |
|
| 504 |
|
| 505 |
def _int_to_str_w(expr i)= |
| 506 |
if(i=1): |
| 507 |
"1" |
| 508 |
elseif(i=2): |
| 509 |
"2" |
| 510 |
elseif(i=3): |
| 511 |
"3" |
| 512 |
elseif(i=4): |
| 513 |
"4" |
| 514 |
elseif(i=5): |
| 515 |
"5" |
| 516 |
elseif(i=6): |
| 517 |
"6" |
| 518 |
elseif(i=7): |
| 519 |
"7" |
| 520 |
elseif(i=8): |
| 521 |
"8" |
| 522 |
fi |
| 523 |
enddef; |
| 524 |
|
| 525 |
|
| 526 |
def _int_to_str_b(expr i)= |
| 527 |
if(i=1): |
| 528 |
"8" |
| 529 |
elseif(i=2): |
| 530 |
"7" |
| 531 |
elseif(i=3): |
| 532 |
"6" |
| 533 |
elseif(i=4): |
| 534 |
"5" |
| 535 |
elseif(i=5): |
| 536 |
"4" |
| 537 |
elseif(i=6): |
| 538 |
"3" |
| 539 |
elseif(i=7): |
| 540 |
"2" |
| 541 |
elseif(i=8): |
| 542 |
"1" |
| 543 |
fi |
| 544 |
enddef; |
| 545 |
|
| 546 |
|
| 547 |
vardef _chess_get_img_w(expr im)= |
| 548 |
save width; |
| 549 |
numeric width; |
| 550 |
width:=abs(ulcorner im-urcorner im); |
| 551 |
width |
| 552 |
enddef; |
| 553 |
|
| 554 |
vardef _chess_get_img_h(expr im)= |
| 555 |
save height; |
| 556 |
numeric height; |
| 557 |
height:=abs(ulcorner im-llcorner im) ; |
| 558 |
height |
| 559 |
enddef; |
| 560 |
|
| 561 |
_chess_eps_cadre := 0.05; |
| 562 |
|
| 563 |
def set_border_width(expr c)= |
| 564 |
_chess_eps_cadre := c; |
| 565 |
enddef; |
| 566 |
|
| 567 |
|
| 568 |
vardef _chess_build_playername(expr str)= |
| 569 |
save _player_img,output; |
| 570 |
picture _player_img,output; |
| 571 |
_player_img:=image(draw textext(str)); |
| 572 |
output:=image( |
| 573 |
linecap:=butt; |
| 574 |
linejoin:=mitered; |
| 575 |
_tmp:=_chess_get_img_h(_player_img); |
| 576 |
fill (0-_chess_eps_cadre,0)*_chessSquareU--(_chessSize+_chess_eps_cadre,0)*_chessSquareU--((_chessSize+_chess_eps_cadre)*_chessSquareU,1.8*_tmp)--(0-_chess_eps_cadre*_chessSquareU,1.8*_tmp)--cycle withcolor black; |
| 577 |
draw (0-_chess_eps_cadre,0)*_chessSquareU--(_chessSize+_chess_eps_cadre,0)*_chessSquareU--((_chessSize+_chess_eps_cadre)*_chessSquareU,1.8*_tmp)--(0-_chess_eps_cadre*_chessSquareU,1.8*_tmp)--cycle withcolor black withpen pencircle scaled (0.1*_chessSquareU); |
| 578 |
draw _player_img shifted (0.15*_chessSquareU,0.4*_tmp) withcolor white; |
| 579 |
); |
| 580 |
output |
| 581 |
enddef; |
| 582 |
|
| 583 |
picture backboard; |
| 584 |
|
| 585 |
string _chess_board_type; % define flat color or "pyramidal" like lichess |
| 586 |
|
| 587 |
% default value flat |
| 588 |
_chess_board_type:="flat"; |
| 589 |
|
| 590 |
vardef set_board_type(expr s) = |
| 591 |
_chess_board_type:=s; |
| 592 |
enddef; |
| 593 |
|
| 594 |
|
| 595 |
def init_backboard = |
| 596 |
_chessSquareU:=_chessWidth/_chessSize; |
| 597 |
_init_piece(_chessSquareU); |
| 598 |
if(_chessTheme="Classical"): |
| 599 |
_buildClassicalSquare(_chessSquareU); |
| 600 |
elseif(_chessTheme="Dotted"): |
| 601 |
_buildDottedSquare(_chessSquareU); |
| 602 |
elseif(_chessTheme="User"): |
| 603 |
buildUserSquare(_chessSquareU); |
| 604 |
else: % colored themes |
| 605 |
_buildColoredSquare(_chessSquareU,_blackColorSquare,_whiteColorSquare); |
| 606 |
fi |
| 607 |
picture _letter_img; |
| 608 |
picture _wplayer_img,_bplayer_img; |
| 609 |
|
| 610 |
backboard:=image( |
| 611 |
linecap:=butt; |
| 612 |
linejoin:=mitered; |
| 613 |
draw (0-_chess_eps_cadre,0-_chess_eps_cadre)*_chessSquareU--(_chessSize+_chess_eps_cadre,0-_chess_eps_cadre)*_chessSquareU--(_chessSize+_chess_eps_cadre,_chessSize+_chess_eps_cadre)*_chessSquareU--(0-_chess_eps_cadre,_chessSize+_chess_eps_cadre)*_chessSquareU--cycle withpen pencircle scaled (0.1*_chessSquareU); |
| 614 |
for i:=1 upto _chessSize: |
| 615 |
for j:=1 upto _chessSize: |
| 616 |
if(odd((i-1)*_chessSize+j+i)): |
| 617 |
draw _whiteSquarePic |
| 618 |
shifted ((j-1)*_chessSquareU,(i-1)*_chessSquareU) ; |
| 619 |
else: |
| 620 |
draw _blackSquarePic shifted ((j-1)*_chessSquareU,(i-1)*_chessSquareU) ; |
| 621 |
fi |
| 622 |
if(_chess_print_coords and (_chessSize=8)): |
| 623 |
if(_chess_coords_inout): % coords inside the backboard |
| 624 |
if(i=1): % lettre on first line |
| 625 |
_letter_img:=image( |
| 626 |
if(_chess_view_wb): |
| 627 |
draw _int_to_letter_w(j) infont _coords_font; |
| 628 |
else: |
| 629 |
draw _int_to_letter_b(j) infont _coords_font; |
| 630 |
fi |
| 631 |
); |
| 632 |
if(j=1): |
| 633 |
_lettre_width:=abs(urcorner _letter_img-ulcorner _letter_img); |
| 634 |
_lettre_height:=abs(urcorner _letter_img-lrcorner _letter_img); |
| 635 |
fi |
| 636 |
draw _letter_img scaled (0.1*_chessSquareU/_lettre_width) shifted ((j-1)*_chessSquareU+0.8*_chessSquareU,(i-1)*_chessSquareU+0.05*_chessSquareU) withcolor ((j mod 2)*_whiteColorSquare+(1-(j mod 2))*_blackColorSquare); |
| 637 |
fi |
| 638 |
if(j=1): |
| 639 |
_letter_img:=image( |
| 640 |
if(_chess_view_wb): |
| 641 |
draw _int_to_str_w(i) infont _coords_font; |
| 642 |
else: |
| 643 |
draw _int_to_str_b(i) infont _coords_font; |
| 644 |
fi |
| 645 |
); |
| 646 |
if(i=1): |
| 647 |
_lettre_width:=abs(urcorner _letter_img-ulcorner _letter_img); |
| 648 |
_lettre_height:=abs(urcorner _letter_img-lrcorner _letter_img); |
| 649 |
fi |
| 650 |
draw _letter_img scaled (0.1*_chessSquareU/_lettre_width) shifted ((j-1)*_chessSquareU+0.05*_chessSquareU,(i-1)*_chessSquareU+0.8*_chessSquareU) withcolor ((i mod 2)*_whiteColorSquare+(1-(i mod 2))*_blackColorSquare); |
| 651 |
fi |
| 652 |
else: % coords outside the backboard |
| 653 |
if(i=1): % lettre on first line |
| 654 |
_letter_img:=image( |
| 655 |
if(_chess_view_wb): |
| 656 |
draw _int_to_letter_w(j) infont _coords_font; |
| 657 |
else: |
| 658 |
draw _int_to_letter_b(j) infont _coords_font; |
| 659 |
fi |
| 660 |
); |
| 661 |
if(j=1): |
| 662 |
_lettre_width:=abs(urcorner _letter_img-ulcorner _letter_img); |
| 663 |
_lettre_height:=abs(urcorner _letter_img-lrcorner _letter_img); |
| 664 |
fi |
| 665 |
draw _letter_img scaled (0.2*_chessSquareU/_lettre_width) shifted ((j-1)*_chessSquareU+0.5*(_chessSquareU-_lettre_width),(i-1)*_chessSquareU-0.5*_chessSquareU) withcolor black; |
| 666 |
fi |
| 667 |
if(j=1): |
| 668 |
_letter_img:=image( |
| 669 |
if(_chess_view_wb): |
| 670 |
draw _int_to_str_w(i) infont _coords_font; |
| 671 |
else: |
| 672 |
draw _int_to_str_b(i) infont _coords_font; |
| 673 |
fi |
| 674 |
); |
| 675 |
if(i=1): |
| 676 |
_lettre_width:=abs(urcorner _letter_img-ulcorner _letter_img); |
| 677 |
_lettre_height:=abs(urcorner _letter_img-lrcorner _letter_img); |
| 678 |
fi |
| 679 |
draw _letter_img scaled (0.2*_chessSquareU/_lettre_width) shifted ((j-1)*_chessSquareU-0.5*_chessSquareU,(i-1)*_chessSquareU+0.5*(_chessSquareU-_lettre_height)) withcolor black; |
| 680 |
fi |
| 681 |
fi |
| 682 |
fi |
| 683 |
endfor; |
| 684 |
endfor; |
| 685 |
if(_chess_view_wb): % white view |
| 686 |
if(_chess_white_player<>""): % if white player |
| 687 |
if((not _chess_coords_inout) or _chess_players_side): % if coord outside or option so set players to the site |
| 688 |
label.urt(textext(_chess_white_player),(1.05*_chessSize*_chessSquareU,0)); |
| 689 |
else: |
| 690 |
draw _chess_build_playername(_chess_white_player) shifted (0,-_chess_get_img_h(_chess_build_playername(_chess_white_player))+_chess_eps_cadre*_chessSquareU); |
| 691 |
fi |
| 692 |
fi |
| 693 |
if(_chess_black_player<>""): |
| 694 |
if((not _chess_coords_inout) or _chess_players_side): |
| 695 |
label.lrt(textext(_chess_black_player),(1.05*_chessSize*_chessSquareU,_chessSize*_chessSquareU)); |
| 696 |
else: |
| 697 |
draw _chess_build_playername(_chess_black_player) shifted (0,_chessSquareU*(_chessSize+2*_chess_eps_cadre)); |
| 698 |
fi |
| 699 |
fi |
| 700 |
else: % black view |
| 701 |
if(_chess_black_player<>""): |
| 702 |
if((not _chess_coords_inout) or _chess_players_side): |
| 703 |
label.urt(textext(_chess_black_player),(1.05*_chessSize*_chessSquareU,0)); |
| 704 |
else: |
| 705 |
draw _chess_build_playername(_chess_black_player) shifted (0,-_chess_get_img_h(_chess_build_playername(_chess_black_player))); |
| 706 |
fi |
| 707 |
fi |
| 708 |
if(_chess_white_player<>""): |
| 709 |
if((not _chess_coords_inout) or _chess_players_side): |
| 710 |
label.lrt(textext(_chess_white_player),(1.05_chessSize*_chessSquareU,_chessSize*_chessSquareU)); |
| 711 |
else: |
| 712 |
draw _chess_build_playername(_chess_white_player) shifted (0,_chessSquareU*(_chessSize+_chess_eps_cadre)); |
| 713 |
fi |
| 714 |
fi |
| 715 |
fi |
| 716 |
); |
| 717 |
enddef; |
| 718 |
|
| 719 |
def _int_to_piece(expr i)= |
| 720 |
if(i=_intWPawn): |
| 721 |
_chess_wP |
| 722 |
elseif(i=_intBPawn): |
| 723 |
_chess_bP |
| 724 |
elseif(i=_intWKnight): |
| 725 |
_chess_wN |
| 726 |
elseif(i=_intBKnight): |
| 727 |
_chess_bN |
| 728 |
elseif(i=_intWBishop): |
| 729 |
_chess_wB |
| 730 |
elseif(i=_intBBishop): |
| 731 |
_chess_bB |
| 732 |
elseif(i=_intWRook): |
| 733 |
_chess_wR |
| 734 |
elseif(i=_intBRook): |
| 735 |
_chess_bR |
| 736 |
elseif(i=_intWQueen): |
| 737 |
_chess_wQ |
| 738 |
elseif(i=_intBQueen): |
| 739 |
_chess_bQ |
| 740 |
elseif(i=_intWKing): |
| 741 |
_chess_wK |
| 742 |
elseif(i=_intBKing): |
| 743 |
_chess_bK |
| 744 |
fi |
| 745 |
enddef; |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
color _chess_last_move_color; |
| 750 |
|
| 751 |
_chess_last_move_color:=(0.6078,0.7803,0); |
| 752 |
|
| 753 |
|
| 754 |
def set_last_move_color(expr c)= |
| 755 |
_chess_last_move_color:=c; |
| 756 |
enddef; |
| 757 |
|
| 758 |
def show_last_move(expr k)= |
| 759 |
if(k>0): |
| 760 |
for i:=1 upto _chessSize: |
| 761 |
for j:=1 upto _chessSize: |
| 762 |
if(chessboard_table[k][i][j]<>chessboard_table[k-1][i][j]): |
| 763 |
if(_chess_view_wb):%white side |
| 764 |
_chess_color_square(_chess_last_move_color,i,j); |
| 765 |
else: |
| 766 |
_chess_color_square(_chess_last_move_color,8+1-i,8+1-j); |
| 767 |
fi |
| 768 |
fi |
| 769 |
endfor |
| 770 |
endfor |
| 771 |
fi |
| 772 |
enddef; |
| 773 |
|
| 774 |
|
| 775 |
%% markers |
| 776 |
|
| 777 |
numeric _marker_size; |
| 778 |
|
| 779 |
_marker_size := 0.35; |
| 780 |
|
| 781 |
def set_marker_scale(expr s)= |
| 782 |
_marker_size:=s; |
| 783 |
enddef; |
| 784 |
|
| 785 |
def _triangle_marker(expr s, r)= |
| 786 |
((0,0)--(1,0)--(0.5,0.5)--cycle) if r: rotatedaround((0.5, 0.5/3), 180) fi scaled (s*_chessSquareU) |
| 787 |
enddef; |
| 788 |
|
| 789 |
def _square_marker(expr s, r)= |
| 790 |
((0,0)--(1,0)--(1,1)--(0,1)--cycle) scaled (s*_chessSquareU) |
| 791 |
enddef; |
| 792 |
|
| 793 |
|
| 794 |
def _disk_marker(expr s, r)= |
| 795 |
(fullcircle shifted (0.5,0.5)) scaled (s*_chessSquareU) |
| 796 |
enddef; |
| 797 |
|
| 798 |
string _marker_type; |
| 799 |
|
| 800 |
_marker_type := "triangle"; |
| 801 |
|
| 802 |
def _marker(expr s,r)= |
| 803 |
if(_marker_type="square"): |
| 804 |
_square_marker(s,r) |
| 805 |
elseif(_marker_type="disk"): |
| 806 |
_disk_marker(s,r) |
| 807 |
elseif(_marker_type="custom"): |
| 808 |
custom_marker(s,r) |
| 809 |
else: |
| 810 |
_triangle_marker(s,r) |
| 811 |
fi |
| 812 |
enddef; |
| 813 |
|
| 814 |
def set_marker_type(expr s)= |
| 815 |
_marker_type:=s; |
| 816 |
enddef; |
| 817 |
|
| 818 |
|
| 819 |
vardef _draw_marker(expr c,b)= |
| 820 |
% c : black or white (color) |
| 821 |
% b : true or false for placing top (with rotation) or bottom |
| 822 |
save _height,_width,_pic,_shifted,_margin; |
| 823 |
picture _pic; |
| 824 |
pair _shifted; |
| 825 |
_pic := image(draw _marker(_marker_size,false);); |
| 826 |
_height := abs(ulcorner _pic - llcorner _pic); |
| 827 |
_width := abs(urcorner _pic-ulcorner _pic); |
| 828 |
_margin:=0.01*_chessSquareU; |
| 829 |
if(b): % top rotating |
| 830 |
_shifted:= (_chessSquareU*_chessSize+_width/2.0+_margin,_chessSquareU*_chessSize); |
| 831 |
else: |
| 832 |
_shifted:= (_chessSquareU*_chessSize+_width/2.0+_margin,-_height); |
| 833 |
fi |
| 834 |
draw _marker(_marker_size,b) shifted _shifted ; |
| 835 |
fill _marker(_marker_size,b) shifted _shifted withcolor c; |
| 836 |
enddef; |
| 837 |
|
| 838 |
%%%% end markers |
| 839 |
|
| 840 |
|
| 841 |
vardef _chess_build_chessboard_pic(expr k)= |
| 842 |
save chessboard_pic; |
| 843 |
save shiftPiece; |
| 844 |
pair shiftPiece; |
| 845 |
picture chessboard_pic; |
| 846 |
chessboard_pic:= image( |
| 847 |
%draw backboard; |
| 848 |
for i:=1 upto _chessSize: |
| 849 |
for j:=1 upto _chessSize: |
| 850 |
if(chessboard_table[k][i][j] <> 0): |
| 851 |
if(_chess_view_wb): |
| 852 |
shiftPiece:=((i-1)*_chessSquareU,(j-1)*_chessSquareU); |
| 853 |
else: |
| 854 |
shiftPiece:=((_chessSize-i)*_chessSquareU,(_chessSize-j)*_chessSquareU); |
| 855 |
fi |
| 856 |
draw ( _int_to_piece(chessboard_table[k][i][j]) shifted shiftPiece); |
| 857 |
fi |
| 858 |
endfor; |
| 859 |
endfor; |
| 860 |
if(_show_whos_to_move): |
| 861 |
if(_chess_view_wb): %white side |
| 862 |
if(not _white_to_move): |
| 863 |
_draw_marker(black,true); |
| 864 |
else: |
| 865 |
_draw_marker(white,false); |
| 866 |
fi |
| 867 |
else: % black side |
| 868 |
if(_white_to_move): |
| 869 |
_draw_marker(white,true); |
| 870 |
else: |
| 871 |
_draw_marker(black,false); |
| 872 |
fi |
| 873 |
fi |
| 874 |
fi |
| 875 |
); |
| 876 |
chessboard_pic |
| 877 |
enddef; |
| 878 |
|
| 879 |
vardef chessboard_step(expr k) = |
| 880 |
save chessboard_pic; |
| 881 |
picture chessboard_pic; |
| 882 |
if((k mod 2)=0): |
| 883 |
set_white_to_move; |
| 884 |
else: |
| 885 |
set_black_to_move; |
| 886 |
fi |
| 887 |
chessboard_pic:=_chess_build_chessboard_pic(k); |
| 888 |
chessboard_pic |
| 889 |
enddef; |
| 890 |
|
| 891 |
vardef chessboard = |
| 892 |
save chessboard_pic; |
| 893 |
picture chessboard_pic; |
| 894 |
chessboard_pic:=_chess_build_chessboard_pic(0); |
| 895 |
chessboard_pic |
| 896 |
enddef; |
| 897 |
|
| 898 |
|
| 899 |
%%% colored square |
| 900 |
picture _chess_colored_squared; |
| 901 |
vardef color_square(expr c)(text slist)= |
| 902 |
% c: color |
| 903 |
% s: list of algebraic notation for a square (ex: "e5","f6",etc.) |
| 904 |
save _i,_j,_str; |
| 905 |
for _str = slist: |
| 906 |
_i:=_lettre_to_int(_get_char(_str,0)); |
| 907 |
_j:=_str_to_int(_get_char(_str,1)); |
| 908 |
path ss; |
| 909 |
if(_chess_view_wb):%white side |
| 910 |
ss:=(unitsquare scaled _chessSquareU) shifted |
| 911 |
((_i-1)*_chessSquareU,(_j-1)*_chessSquareU); |
| 912 |
else: |
| 913 |
ss:=(unitsquare scaled _chessSquareU) shifted |
| 914 |
((8+1-_i-1)*_chessSquareU,(8+1-_j-1)*_chessSquareU); |
| 915 |
fi |
| 916 |
transparence(ss,c,0.41); |
| 917 |
endfor |
| 918 |
enddef; |
| 919 |
|
| 920 |
vardef _chess_color_square(expr c,i,j)= |
| 921 |
% c: color |
| 922 |
% s: algebraic notation for a square (ex: e5) |
| 923 |
path ss; |
| 924 |
ss:=(unitsquare scaled _chessSquareU) shifted ((i-1)*_chessSquareU,(j-1)*_chessSquareU); |
| 925 |
transparence(ss,c,0.41); |
| 926 |
enddef; |
| 927 |
|
| 928 |
%% "inspired" by Thruston’s code: |
| 929 |
% https://github.com/thruston/Drawing-with-Metapost |
| 930 |
def transparence(expr p,c,a)= |
| 931 |
save shape,alpha,filler,bg,fg; |
| 932 |
path shape; |
| 933 |
shape := p; |
| 934 |
alpha := a; % alpha: 0=invisible, 1=opaque |
| 935 |
color filler; |
| 936 |
filler = c; |
| 937 |
picture fg, bg; |
| 938 |
bg = currentpicture; % capture the current drawing |
| 939 |
fg = image( |
| 940 |
for e within bg: % redraw everything in blended color |
| 941 |
draw e withcolor alpha[colorpart e, filler]; |
| 942 |
endfor % and add some decorations |
| 943 |
); |
| 944 |
clip fg to shape; % now clip the fg drawing to the shape |
| 945 |
fill shape withcolor filler; % fill the shape |
| 946 |
draw fg; % and put the fg drawing on top |
| 947 |
enddef; |
| 948 |
|
| 949 |
|
| 950 |
% draw arrow on the board between two algebraic notation |
| 951 |
% depending on the syntax, strat line or rectangular one |
| 952 |
% s="a2--a4" or s="b1|-c3" or s="b1-|c3" |
| 953 |
% allow a list of string |
| 954 |
_chess_arrow_width:=0.08*_chessSquareU; |
| 955 |
_chess_arrow_head:=0.4*_chessSquareU; |
| 956 |
|
| 957 |
def set_arrow_width(expr c)= |
| 958 |
_chess_arrow_width:=c*_chessSquareU; |
| 959 |
enddef; |
| 960 |
|
| 961 |
def draw_arrows(expr c)(text slist)= |
| 962 |
save _chess_arrow,_str; |
| 963 |
save _i,_j,_k,_l,_vec,_ortho_vec,_unit_vec,_init,_end; |
| 964 |
pair _vec,_unit_vec,_ortho_vec,_init,_end; |
| 965 |
path _chess_arrow; |
| 966 |
for _str = slist: |
| 967 |
_i:=_lettre_to_int(_get_char(_str,0)); |
| 968 |
_j:=_str_to_int(_get_char(_str,1)); |
| 969 |
_k:=_lettre_to_int(_get_char(_str,4)); |
| 970 |
_l:=_str_to_int(_get_char(_str,5)); |
| 971 |
if(_chess_view_wb): %white side |
| 972 |
_end:=(_k,_l)*_chessSquareU-(0.5,0.5)*_chessSquareU; |
| 973 |
_init:=(_i,_j)*_chessSquareU-(0.5,0.5)*_chessSquareU; |
| 974 |
else: |
| 975 |
_end:=(8-_k,8-_l)*_chessSquareU+(0.5,0.5)*_chessSquareU; |
| 976 |
_init:=(8-_i,8-_j)*_chessSquareU+(0.5,0.5)*_chessSquareU; |
| 977 |
fi |
| 978 |
if((substring (2,4) of _str)="--"): |
| 979 |
_vec:= _end-_init; |
| 980 |
_unit_vec:=_chess_unit_vec(_vec); |
| 981 |
_init:=_init+0.2*_chessSquareU*_unit_vec; |
| 982 |
_end:=_end-0.2*_chessSquareU*_unit_vec; |
| 983 |
_vec:= _end-_init; |
| 984 |
_unit_vec:=_chess_unit_vec(_vec); |
| 985 |
_ortho_vec:=_chess_unit_ortho_vec(_vec); |
| 986 |
_vec_norm:=abs(_vec); |
| 987 |
_chess_arrow:=_init--(_init+(_chess_arrow_width*_ortho_vec))-- |
| 988 |
(_init+(_chess_arrow_width*_ortho_vec)+((_vec_norm-_chess_arrow_head)*_unit_vec))-- |
| 989 |
(_init+(_chess_arrow_width*_ortho_vec)+((_vec_norm-_chess_arrow_head)*_unit_vec)+(2*_chess_arrow_width*_ortho_vec))-- |
| 990 |
_end -- |
| 991 |
(_init-(_chess_arrow_width*_ortho_vec)+((_vec_norm-_chess_arrow_head)*_unit_vec)-(2*_chess_arrow_width*_ortho_vec))-- |
| 992 |
(_init-(_chess_arrow_width*_ortho_vec)+((_vec_norm-_chess_arrow_head)*_unit_vec))-- |
| 993 |
(_init-(_chess_arrow_width*_ortho_vec))--cycle; |
| 994 |
elseif((substring (2,4) of _str)="|-"): |
| 995 |
if((xpart _end)> (xpart _init)): % to the right |
| 996 |
_pm_rl:=+1; |
| 997 |
else: |
| 998 |
_pm_rl:=-1; |
| 999 |
fi |
| 1000 |
if((ypart _end)>(ypart _init)): % to the to |
| 1001 |
_pm_ul:=+1; |
| 1002 |
else: |
| 1003 |
_pm_ul:=-1; |
| 1004 |
fi |
| 1005 |
_init:=(xpart _init, ypart _init+0.2*(_pm_ul*_chessSquareU)); |
| 1006 |
_end:=(xpart _end-0.2*(_pm_rl*_chessSquareU),ypart _end); |
| 1007 |
_chess_arrow:=(_init-_pm_rl*(_chess_arrow_width,0))-- |
| 1008 |
(xpart _init -_pm_rl*_chess_arrow_width, ypart _end + _pm_ul*_chess_arrow_width)-- |
| 1009 |
(xpart _end-_pm_rl*_chess_arrow_head, ypart _end + _pm_ul*_chess_arrow_width)-- |
| 1010 |
(xpart _end-_pm_rl*_chess_arrow_head, ypart _end + 3*_pm_ul*_chess_arrow_width)-- |
| 1011 |
_end-- |
| 1012 |
(xpart _end-_pm_rl*_chess_arrow_head, ypart _end - 3*_pm_ul*_chess_arrow_width)-- |
| 1013 |
(xpart _end-_pm_rl*_chess_arrow_head, ypart _end - _pm_ul*_chess_arrow_width)-- |
| 1014 |
(xpart _init +_pm_rl*_chess_arrow_width, ypart _end - _pm_ul*_chess_arrow_width) -- |
| 1015 |
(_init+_pm_rl*(_chess_arrow_width,0))--cycle; |
| 1016 |
elseif((substring (2,4) of _str)="-|"): |
| 1017 |
if((xpart _end)>(xpart _init)): % to the right |
| 1018 |
_pm_rl:=+1; |
| 1019 |
else: |
| 1020 |
_pm_rl:=-1; |
| 1021 |
fi |
| 1022 |
if((ypart _end)>(ypart _init)): % to the to |
| 1023 |
_pm_ul:=+1; |
| 1024 |
else: |
| 1025 |
_pm_ul:=-1; |
| 1026 |
fi |
| 1027 |
_init:=(xpart _init +0.2*(_pm_rl*_chessSquareU), ypart _init); |
| 1028 |
_end:=(xpart _end,ypart _end-0.2*(_pm_ul*_chessSquareU)); |
| 1029 |
_chess_arrow:=(_init+_pm_ul*(0,_chess_arrow_width))-- |
| 1030 |
(xpart _end -_pm_rl*_chess_arrow_width, ypart _init + _pm_ul*_chess_arrow_width)-- |
| 1031 |
(xpart _end -_pm_rl*_chess_arrow_width,ypart _end-_pm_ul*_chess_arrow_head)-- |
| 1032 |
(xpart _end -3*_pm_rl*_chess_arrow_width,ypart _end-_pm_ul*_chess_arrow_head)-- |
| 1033 |
_end-- |
| 1034 |
(xpart _end +3*_pm_rl*_chess_arrow_width,ypart _end-_pm_ul*_chess_arrow_head)-- |
| 1035 |
(xpart _end +_pm_rl*_chess_arrow_width,ypart _end-_pm_ul*_chess_arrow_head)-- |
| 1036 |
(xpart _end +_pm_rl*_chess_arrow_width, ypart _init - _pm_ul*_chess_arrow_width)-- |
| 1037 |
(_init-_pm_ul*(0,_chess_arrow_width))--cycle; |
| 1038 |
|
| 1039 |
fi |
| 1040 |
%draw _chess_arrow; |
| 1041 |
transparence(_chess_arrow,c,0.91); |
| 1042 |
endfor |
| 1043 |
enddef; |
| 1044 |
|
| 1045 |
vardef _chess_unit_ortho_vec(expr p)= |
| 1046 |
save _out; |
| 1047 |
pair _out; |
| 1048 |
_out := p rotated 90 scaled (1/abs(p)); |
| 1049 |
_out |
| 1050 |
enddef; |
| 1051 |
|
| 1052 |
|
| 1053 |
vardef _chess_unit_vec(expr p)= |
| 1054 |
save _out; |
| 1055 |
pair _out; |
| 1056 |
_out := p scaled (1/abs(p)); |
| 1057 |
_out |
| 1058 |
enddef; |
| 1059 |
|
| 1060 |
|
| 1061 |
def draw_circles(expr c)(text slist)= |
| 1062 |
save _str; |
| 1063 |
save _i,_j; |
| 1064 |
for _str = slist: |
| 1065 |
_i:=_lettre_to_int(_get_char(_str,0)); |
| 1066 |
_j:=_str_to_int(_get_char(_str,1)); |
| 1067 |
if(_chess_view_wb): % white side |
| 1068 |
draw (fullcircle scaled (0.95*_chessSquareU)) shifted |
| 1069 |
((_i-0.5,_j-0.5)*_chessSquareU) withpen pencircle scaled |
| 1070 |
(0.08*_chessSquareU) withcolor c; |
| 1071 |
else: |
| 1072 |
draw (fullcircle scaled (0.95*_chessSquareU)) shifted |
| 1073 |
((8+1-_i-0.5,8+1-_j-0.5)*_chessSquareU) withpen pencircle scaled |
| 1074 |
(0.08*_chessSquareU) withcolor c; |
| 1075 |
fi |
| 1076 |
endfor; |
| 1077 |
enddef; |
| 1078 |
|
| 1079 |
def draw_crosses(expr c)(text slist)= |
| 1080 |
save _str; |
| 1081 |
save _i,_j; |
| 1082 |
for _str = slist: |
| 1083 |
_i:=_lettre_to_int(_get_char(_str,0)); |
| 1084 |
_j:=_str_to_int(_get_char(_str,1)); |
| 1085 |
if(_chess_view_wb):% white side |
| 1086 |
draw (((-0.45,-0.45)--(0.45,0.45)) scaled (0.95*_chessSquareU)) shifted ((_i-0.5,_j-0.5)*_chessSquareU) withpen pencircle scaled (0.1*_chessSquareU) withcolor c; |
| 1087 |
draw (((-0.45,0.45)--(0.45,-0.45)) scaled (0.95*_chessSquareU)) shifted |
| 1088 |
((_i-0.5,_j-0.5)*_chessSquareU) withpen pencircle scaled |
| 1089 |
(0.1*_chessSquareU) withcolor c; |
| 1090 |
else: |
| 1091 |
draw (((-0.45,-0.45)--(0.45,0.45)) scaled (0.95*_chessSquareU)) shifted ((8+1-_i-0.5,8+1-_j-0.5)*_chessSquareU) withpen pencircle scaled (0.1*_chessSquareU) withcolor c; |
| 1092 |
draw (((-0.45,0.45)--(0.45,-0.45)) scaled (0.95*_chessSquareU)) shifted |
| 1093 |
((8+1-_i-0.5,8+1-_j-0.5)*_chessSquareU) withpen pencircle scaled |
| 1094 |
(0.1*_chessSquareU) withcolor c; |
| 1095 |
fi |
| 1096 |
endfor; |
| 1097 |
enddef; |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
% clip current chessboard to the area of type "a2-f7" |
| 1102 |
def clip_chessboard(expr s)= |
| 1103 |
save _first_letter,_firt_nbr, _second_letter,_second_nbr,_fl,_fn,_sl,_sn,_str; |
| 1104 |
string _first_letter,_first_nbr, _second_letter,_second_nbr,_str; |
| 1105 |
_str:=s; |
| 1106 |
_first_letter:=substring (0,1) of _str; |
| 1107 |
_first_nbr:=substring (1,2) of _str; |
| 1108 |
_second_letter:=substring (3,4) of _str; |
| 1109 |
_second_nbr:=substring (4,5) of _str; |
| 1110 |
_fl:=_lettre_to_int(_first_letter)-1; |
| 1111 |
_fn:=_str_to_int(_first_nbr)-1; |
| 1112 |
_sl:=_lettre_to_int(_second_letter); |
| 1113 |
_sn:=_str_to_int(_second_nbr); |
| 1114 |
clip currentpicture to (_fl,_fn)*_chessSquareU--(_fl,_sn)*_chessSquareU--(_sl,_sn)*_chessSquareU--(_sl,_fn)*_chessSquareU--cycle; |
| 1115 |
enddef; |
| 1116 |
|
| 1117 |
% reset backboard |
| 1118 |
def reset_mpchess= |
| 1119 |
_chessWidth:=5cm; |
| 1120 |
_chessSize:=8; |
| 1121 |
_chessSquareU:=_chessWidth/_chessSize; |
| 1122 |
_chessTheme := "BlueLichess"; |
| 1123 |
_blackColorSquare:=(0.549,0.6353,0.6784); |
| 1124 |
_whiteColorSquare:=(0.8706,0.8912,0.902); |
| 1125 |
_chess_print_coords:=true; |
| 1126 |
_chess_coords_inout:=true; |
| 1127 |
_chess_view_wb := true; % true for white |
| 1128 |
_chess_players_side:=false; |
| 1129 |
_chess_eps_cadre := 0.05; |
| 1130 |
_chess_last_move_color:=(0.6078,0.7803,0); |
| 1131 |
_chess_arrow_width:=0.2*_chessSquareU; |
| 1132 |
_chess_arrow_head:=0.6*_chessSquareU; |
| 1133 |
clear_chessboard; |
| 1134 |
_chess_wcaptures_nbr[0]:=0; |
| 1135 |
_chess_bcaptures_nbr[0]:=0; |
| 1136 |
set_color_theme("BlueLichess"); |
| 1137 |
set_pieces_theme("cburnett"); |
| 1138 |
_chess_white_player:=""; |
| 1139 |
_chess_black_player:=""; |
| 1140 |
_chess_arrow_width:=0.08*_chessSquareU; |
| 1141 |
_chess_arrow_head:=0.4*_chessSquareU; |
| 1142 |
_chess_main_lines_color:=0.3[_blackColorSquare,black]; |
| 1143 |
enddef; |
| 1144 |
|
| 1145 |
color _commentColor; |
| 1146 |
_commentColor:=(0.6,0.2,0.2); |
| 1147 |
|
| 1148 |
def set_comment_color(expr c)= |
| 1149 |
_commentColor:=c; |
| 1150 |
enddef; |
| 1151 |
|
| 1152 |
_commentScale:=0.2; |
| 1153 |
|
| 1154 |
def draw_comment(expr ind, pos)= |
| 1155 |
save _img, _letter, _nbr; |
| 1156 |
picture _img; |
| 1157 |
_img:=image( |
| 1158 |
draw textext("\bfseries"&ind); |
| 1159 |
); |
| 1160 |
_width:=abs(urcorner _img-ulcorner _img); |
| 1161 |
_height:=abs(urcorner _img-lrcorner _img); |
| 1162 |
_img:=image( |
| 1163 |
fill fullcircle scaled (2*max(_width,_height)) shifted (0.5*_width,0.5*_height) withcolor 0.8[_commentColor,white]; |
| 1164 |
draw fullcircle scaled (2*max(_width,_height)) shifted (0.5*_width,0.5*_height) withcolor _commentColor; |
| 1165 |
draw _img; |
| 1166 |
); |
| 1167 |
_letter:=_lettre_to_int(_get_char(pos,0)); |
| 1168 |
_nbr:=_str_to_int(_get_char(pos,1)); |
| 1169 |
if(_chess_view_wb): % white view |
| 1170 |
draw _img scaled (_commentScale*_chessSquareU/max(_width,_height)) shifted ((_letter)*_chessSquareU-0.15*(_chessSquareU-_width),(_nbr)*_chessSquareU-0.15*_chessSquareU); |
| 1171 |
else: |
| 1172 |
draw _img scaled (_commentScale*_chessSquareU/max(_width,_height)) shifted ((8+1-_letter)*_chessSquareU-0.15*(_chessSquareU-_width),(8+1-_nbr)*_chessSquareU-0.15*_chessSquareU); |
| 1173 |
fi |
| 1174 |
enddef; |