Bugs fixing, finalization of rules in french+english
[vchess.git] / public / javascripts / utils / printDiagram.js
CommitLineData
da06a6eb
BA
1// Assuming V(ariantRules) class is loaded.
2// args: object with position (mandatory), orientation, marks (optional)
3function getDiagram(args)
4{
5 const [sizeX,sizeY] = [V.size.x,V.size.y];
6 // Obtain array of pieces images names
7 const board = VariantRules.GetBoard(args.position);
8 const orientation = args.orientation || "w";
9 let markArray = [];
5915f720 10 if (!!args.marks && args.marks != "-")
da06a6eb
BA
11 {
12 // Turn (human) marks into coordinates
13 markArray = doubleArray(sizeX, sizeY, false);
14 let squares = args.marks.split(",");
15 for (let i=0; i<squares.length; i++)
16 {
5915f720 17 const coords = V.SquareToCoords(squares[i]);
26c1e3bd 18 markArray[coords.x][coords.y] = true;
da06a6eb
BA
19 }
20 }
5915f720
BA
21 let shadowArray = [];
22 if (!!args.shadow && args.shadow != "-")
23 {
24 // Turn (human) shadow indications into coordinates
25 shadowArray = doubleArray(sizeX, sizeY, false);
26 let squares = args.shadow.split(",");
27 for (let i=0; i<squares.length; i++)
28 {
29 const rownum = V.size.x - parseInt(squares[i]);
30 if (!isNaN(rownum))
31 {
32 // Shadow a full row
33 for (let i=0; i<V.size.y; i++)
34 shadowArray[rownum][i] = true;
35 continue;
36 }
37 if (squares[i].length == 1)
38 {
39 // Shadow a full column
40 const colnum = V.ColumnToCoord(squares[i]);
41 for (let i=0; i<V.size.x; i++)
42 shadowArray[i][colnum] = true;
43 continue;
44 }
69f3d801
BA
45 if (squares[i].indexOf("-") >= 0)
46 {
47 // Shadow a range of squares, horizontally or vertically
48 const firstLastSq = squares[i].split("-");
49 const range =
50 [
51 V.SquareToCoords(firstLastSq[0]),
52 V.SquareToCoords(firstLastSq[1])
53 ];
54 const step =
55 [
56 range[1].x == range[0].x
57 ? 0
58 : (range[1].x - range[0].x) / Math.abs(range[1].x - range[0].x),
59 range[1].y == range[0].y
60 ? 0
61 : (range[1].y - range[0].y) / Math.abs(range[1].y - range[0].y)
62 ];
63 // Convention: range always from smaller to larger number
64 for (let x=range[0].x, y=range[0].y; x <= range[1].x && y <= range[1].y;
65 x += step[0], y += step[1])
66 {
67 shadowArray[x][y] = true;
68 }
69 continue;
70 }
5915f720
BA
71 // Shadow just one square:
72 const coords = V.SquareToCoords(squares[i]);
73 shadowArray[coords.x][coords.y] = true;
74 }
75 }
da06a6eb
BA
76 let boardDiv = "";
77 const [startX,startY,inc] = orientation == 'w'
78 ? [0, 0, 1]
79 : [sizeX-1, sizeY-1, -1];
80 for (let i=startX; i>=0 && i<sizeX; i+=inc)
81 {
82 boardDiv += "<div class='row'>";
83 for (let j=startY; j>=0 && j<sizeY; j+=inc)
84 {
85 boardDiv += "<div class='board board" + sizeY + " " +
5915f720
BA
86 ((i+j)%2==0 ? "light-square-diag" : "dark-square-diag") +
87 (shadowArray.length > 0 && shadowArray[i][j] ? " in-shadow" : "") +
88 "'>";
da06a6eb
BA
89 if (board[i][j] != V.EMPTY)
90 {
91 boardDiv += "<img src='/images/pieces/" +
92 V.getPpath(board[i][j]) + ".svg' class='piece'/>";
93 }
5915f720 94 if (markArray.length > 0 && markArray[i][j])
da06a6eb
BA
95 boardDiv += "<img src='/images/mark.svg' class='mark-square'/>";
96 boardDiv += "</div>";
97 }
98 boardDiv += "</div>";
99 }
100 return boardDiv;
101}