From fc12475fd434835816796ece83d93341af6c1550 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 23 Jun 2022 19:00:17 +0200
Subject: [PATCH] Fix Align4, fix mushrooms effect for Chakart

---
 base_rules.js             |  2 +-
 variants/Align4/class.js  | 15 ++++++---------
 variants/Chakart/class.js | 32 +++++++++++++++++++++-----------
 3 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/base_rules.js b/base_rules.js
index a48bd3d..3fac51b 100644
--- a/base_rules.js
+++ b/base_rules.js
@@ -252,7 +252,7 @@ export default class ChessRules {
       parts.push(`"flags":"${flags}"`);
     if (this.hasEnpassant)
       parts.push('"enpassant":"-"');
-    if (this.hasReserve)
+    if (this.hasReserveFen)
       parts.push('"reserve":"000000000000"');
     if (this.options["crazyhouse"])
       parts.push('"ispawn":"-"');
diff --git a/variants/Align4/class.js b/variants/Align4/class.js
index b7b0c4a..b2683aa 100644
--- a/variants/Align4/class.js
+++ b/variants/Align4/class.js
@@ -26,23 +26,20 @@ export default class Align4Rules extends ChessRules {
 
   genRandInitFen(seed) {
     const baseFen = super.genRandInitFen(seed);
-    return "4k3/8" + baseFen.substring(17, 50) + " -"; //TODO: + flags 1188
+    const fen = baseFen.replace("rnbqkbnr/pppppppp", "4k3/8");
+    const fenParts = baseFen.split(" ");
+    let others = JSON.parse(fenParts[3]);
+    others["flags"] = others["flags"].substr(0, 2) + "88";
+    return fenParts.slice(0, 3).join(" ") + " " + JSON.stringify(others);
   }
 
-  setOtherVariables(fenParsed) {
-    super.setOtherVariables(fenParsed);
+  initReserves() {
     this.reserve = { b: { p: 1 } };
   }
 
   // Just do not update any reserve (infinite supply)
   updateReserve() {}
 
-  getCastleMoves([x, y]) {
-    if (this.GetColor(x, y) == 'b')
-      return [];
-    return super.getCastleMoves([x, y]);
-  }
-
   getCurrentScore(move) {
     const score = super.getCurrentScore(move);
     if (score != "*")
diff --git a/variants/Chakart/class.js b/variants/Chakart/class.js
index 37261e4..24bbd3e 100644
--- a/variants/Chakart/class.js
+++ b/variants/Chakart/class.js
@@ -133,8 +133,12 @@ export default class ChakartRules extends ChessRules {
   genRandInitFen(seed) {
     const options = Object.assign({mode: "suicide"}, this.options);
     const gr = new GiveawayRules({options: options, genFenOnly: true});
-    // Add Peach + mario flags
-    return gr.genRandInitFen(seed).slice(0, -17) + '{"flags":"1111"}';
+    const baseFen = gr.genRandInitFen(seed);
+    const fenParts = baseFen.split(" ");
+    let others = JSON.parse(fenParts[3]);
+    delete others["enpassant"];
+    others["flags"] = "1111"; //Peach + Mario flags
+    return fenParts.slice(0, 3).join(" ") + " " + JSON.stringify(others);
   }
 
   fen2board(f) {
@@ -645,19 +649,25 @@ export default class ChakartRules extends ChessRules {
   }
 
   getMushroomEffect(move) {
-    if (typeof move.start.x == "string") //drop move (toadette)
+    if (
+      typeof move.start.x == "string" || //drop move (toadette)
+      ['b', 'r', 'q'].includes(move.vanish[0].p) //slider
+    ) {
       return null;
-    let step = [move.end.x - move.start.x, move.end.y - move.start.y];
-    if ([0, 1].some(i => Math.abs(step[i]) >= 2 && Math.abs(step[1-i]) != 1)) {
-      // Slider, multi-squares: normalize step
-      for (let j of [0, 1])
-        step[j] = step[j] / Math.abs(step[j]) || 0;
     }
+    let step = [move.end.x - move.start.x, move.end.y - move.start.y];
+    if (Math.abs(step[0]) == 2 && Math.abs(step[1]) == 0)
+      // Pawn initial 2-squares move: normalize step
+      step[0] /= 2;
     const nextSquare = [move.end.x + step[0], move.end.y + step[1]];
-    const afterSquare =
-      [nextSquare[0] + step[0], nextSquare[1] + step[1]];
     let nextMove = null;
-    if (this.onBoard(nextSquare[0], nextSquare[1])) {
+    if (
+      this.onBoard(nextSquare[0], nextSquare[1]) &&
+      (
+        this.board[nextSquare[0]][nextSquare[1]] == "" ||
+        this.getColor(nextSquare[0], nextSquare[1]) == 'a'
+      )
+    ) {
       this.playOnBoard(move); //HACK for getBasicMove()
       nextMove = this.getBasicMove([move.end.x, move.end.y], nextSquare);
       this.undoOnBoard(move);
-- 
2.44.0