Prepare Coregal variant
[xogo.git] / base_rules.js
index 98c6bd4..6078da8 100644 (file)
@@ -97,6 +97,14 @@ export default class ChessRules {
     return true;
   }
 
+  // Allow to take (moving: not disappearing) own pieces?
+  get hasSelfCaptures() {
+    return (
+      this.options["recycle"] ||
+      (this.options["teleport"] && this.subTurnTeleport == 1)
+    );
+  }
+
   get hasReserve() {
     return (
       !!this.options["crazyhouse"] ||
@@ -225,9 +233,9 @@ export default class ChessRules {
       ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
       {
         randomness: this.options["randomness"],
-        between: {p1: 'k', p2: 'r'},
+        between: [{p1: 'k', p2: 'r'}],
         diffCol: ['b'],
-        flags: ['r']
+        flags: ['r', 'k']
       }
     );
     return {
@@ -408,14 +416,14 @@ export default class ChessRules {
   }
 
   // Some additional variables from FEN (variant dependant)
-  setOtherVariables(fenParsed) {
+  setOtherVariables(fenParsed, pieceArray) {
     // Set flags and enpassant:
     if (this.hasFlags)
       this.setFlags(fenParsed.flags);
     if (this.hasEnpassant)
       this.epSquare = this.getEpSquare(fenParsed.enpassant);
     if (this.hasReserve && !this.isDiagram)
-      this.initReserves(fenParsed.reserve);
+      this.initReserves(fenParsed.reserve, pieceArray);
     if (this.options["crazyhouse"])
       this.initIspawn(fenParsed.ispawn);
     if (this.options["teleport"]) {
@@ -1188,7 +1196,7 @@ export default class ChessRules {
   }
 
   pieces(color, x, y) {
-    const pawnShift = this.getPawnShift(color);
+    const pawnShift = this.getPawnShift(color || 'w');
     return {
       'p': {
         "class": "pawn",
@@ -1413,7 +1421,6 @@ export default class ChessRules {
                 {
                   attackOnly: true,
                   one: true,
-                  segments: this.options["cylinder"]
                 },
                 allowed
               )
@@ -1424,10 +1431,7 @@ export default class ChessRules {
                 this.options["zen"] &&
                 this.findCapturesOn(
                   [i, j],
-                  {
-                    one: true,
-                    segments: this.options["cylinder"]
-                  },
+                  {one: true},
                   allowed
                 )
               )
@@ -1692,7 +1696,6 @@ export default class ChessRules {
         [x, y],
         {
           attackOnly: true,
-          segments: this.options["cylinder"],
           stepSpec: stepSpec
         },
         ([i1, j1], [i2, j2]) => {
@@ -1707,7 +1710,6 @@ export default class ChessRules {
       [x, y],
       {
         moveOnly: !!stepSpec.attack || this.options["zen"],
-        segments: this.options["cylinder"],
         stepSpec: stepSpec
       }
     );
@@ -1720,22 +1722,17 @@ export default class ChessRules {
           !this.isKing(i1, j1) && this.canTake([i2, j2], [i1, j1])
       );
       // Technical step: segments (if any) are reversed
-      if (this.options["cylinder"]) {
-        zenCaptures.forEach(z => {
+      zenCaptures.forEach(z => {
+        if (!!z.segments)
           z.segments = z.segments.reverse().map(s => s.reverse())
-        });
-      }
+      });
       Array.prototype.push.apply(squares, zenCaptures);
     }
-    if (
-      this.options["recycle"] ||
-      (this.options["teleport"] && this.subTurnTeleport == 1)
-    ) {
+    if (this.hasSelfCaptures) {
       const selfCaptures = this.findDestSquares(
         [x, y],
         {
           attackOnly: true,
-          segments: this.options["cylinder"],
           stepSpec: stepSpec
         },
         ([i1, j1], [i2, j2]) => {
@@ -1749,7 +1746,7 @@ export default class ChessRules {
     }
     return squares.map(s => {
       let mv = this.getBasicMove([x, y], s.sq);
-      if (this.options["cylinder"] && !!s.segments && s.segments.length >= 2)
+      if (!!s.segments)
         mv.segments = s.segments;
       return mv;
     });
@@ -1760,23 +1757,21 @@ export default class ChessRules {
       allowed = (sq1, sq2) => this.canTake(sq1, sq2);
     const apparentPiece = this.getPiece(x, y); //how it looks
     let res = [];
-    // Next 3 for Cylinder mode: (unused if !o.segments)
+    // Next 3 for Cylinder mode or circular (useless otherwise)
     let explored = {};
     let segments = [];
     let segStart = [];
     const addSquare = ([i, j]) => {
       let elt = {sq: [i, j]};
-      if (o.segments)
+      if (segments.length >= 1)
         elt.segments = this.getSegments(segments, segStart, [i, j]);
       res.push(elt);
     };
     const exploreSteps = (stepArray, mode) => {
       for (let s of stepArray) {
         outerLoop: for (let step of s.steps) {
-          if (o.segments) {
-            segments = [];
-            segStart = [x, y];
-          }
+          segments = [];
+          segStart = [x, y];
           let [i, j] = [x, y];
           let stepCounter = 0;
           while (
@@ -1801,8 +1796,8 @@ export default class ChessRules {
               continue outerLoop;
             const oldIJ = [i, j];
             [i, j] = this.increment([i, j], step);
-            if (o.segments && Math.abs(j - oldIJ[1]) > 1) {
-              // Boundary between segments (cylinder mode)
+            if (Math.abs(j - oldIJ[1]) > 1 || Math.abs(i - oldIJ[0]) > 1) {
+              // Boundary between segments (cylinder or circular mode)
               segments.push([[segStart[0], segStart[1]], oldIJ]);
               segStart = [i, j];
             }
@@ -1873,7 +1868,6 @@ export default class ChessRules {
                 {
                   captureTarget: [x, y],
                   captureSteps: [{steps: [s], range: a.range}],
-                  segments: o.segments
                 },
                 allowed
               );
@@ -2143,7 +2137,6 @@ export default class ChessRules {
           [x, y],
           {
             byCol: oppCols,
-            segments: this.options["cylinder"],
             one: true
           }
         )
@@ -2155,7 +2148,6 @@ export default class ChessRules {
           [x, y],
           {
             attackOnly: true,
-            segments: this.options["cylinder"],
             one: true
           },
           ([i1, j1], [i2, j2]) => oppCols.includes(this.getColor(i2, j2))