From 33974019f1ee5cc9a3f32f9beb8a0d6fe96af769 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 28 May 2020 20:02:09 +0200
Subject: [PATCH] Eightpieces: prevent lancer to orient toward outside board
 (except if being pushed)

---
 client/src/variants/Eightpieces.js | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/client/src/variants/Eightpieces.js b/client/src/variants/Eightpieces.js
index a232890c..38f0d303 100644
--- a/client/src/variants/Eightpieces.js
+++ b/client/src/variants/Eightpieces.js
@@ -622,13 +622,18 @@ export class EightpiecesRules extends ChessRules {
             let chooseMoves = [];
             dirMoves.forEach(m => {
               Object.keys(V.LANCER_DIRS).forEach(k => {
-                let mk = JSON.parse(JSON.stringify(m));
-                mk.appear[0].p = k;
-                moves.push(mk);
+                const newDir = V.LANCER_DIRS[k];
+                // Prevent orientations toward outer board:
+                if (V.OnBoard(m.end.x + newDir[0], m.end.y + newDir[1])) {
+                  let mk = JSON.parse(JSON.stringify(m));
+                  mk.appear[0].p = k;
+                  chooseMoves.push(mk);
+                }
               });
             });
             Array.prototype.push.apply(moves, chooseMoves);
-          } else Array.prototype.push.apply(moves, dirMoves);
+          }
+          else Array.prototype.push.apply(moves, dirMoves);
         });
         return moves;
       }
@@ -640,13 +645,18 @@ export class EightpiecesRules extends ChessRules {
     if (this.subTurn == 1) {
       monodirMoves.forEach(m => {
         Object.keys(V.LANCER_DIRS).forEach(k => {
-          let mk = JSON.parse(JSON.stringify(m));
-          mk.appear[0].p = k;
-          moves.push(mk);
+          const newDir = V.LANCER_DIRS[k];
+          // Prevent orientations toward outer board:
+          if (V.OnBoard(m.end.x + newDir[0], m.end.y + newDir[1])) {
+            let mk = JSON.parse(JSON.stringify(m));
+            mk.appear[0].p = k;
+            moves.push(mk);
+          }
         });
       });
       return moves;
-    } else {
+    }
+    else {
       // I'm pushed: add potential nudges, except for current orientation
       let potentialNudges = [];
       for (let step of V.steps[V.ROOK].concat(V.steps[V.BISHOP])) {
-- 
2.44.0