+ // Helper to set and apply banana/bomb effect
+ getRandomSquare([x, y], steps, freeSquare) {
+ let validSteps = steps.filter(s => this.onBoard(x + s[0], y + s[1]));
+ if (freeSquare) {
+ // Square to put banana/bomb cannot be occupied by a piece
+ validSteps = validSteps.filter(s => {
+ return ["", 'a'].includes(this.getColor(x + s[0], y + s[1]))
+ });
+ }
+ if (validSteps.length == 0)
+ return null;
+ const step = validSteps[Random.randInt(validSteps.length)];
+ return [x + step[0], y + step[1]];
+ }
+
+ getEggEffect(move) {
+ const getRandomPiece = (c) => {
+ let bagOfPieces = [];
+ for (let i=0; i<this.size.x; i++) {
+ for (let j=0; j<this.size.y; j++) {
+ const pieceIJ = this.getPiece(i, j);
+ if (
+ this.getColor(i, j) == c && pieceIJ != 'k' &&
+ (
+ // The color will change, so pawns on first rank are ineligible
+ pieceIJ != 'p' ||
+ (c == 'w' && i < this.size.x - 1) || (c == 'b' && i > 0)
+ )
+ ) {
+ bagOfPieces.push([i, j]);
+ }
+ }
+ }
+ if (bagOfPieces.length >= 1)
+ return Random.sample(bagOfPieces);
+ return null;
+ };
+ const color = this.turn;
+ let em = null;
+ switch (move.egg) {
+ case "luigi":
+ case "waluigi":
+ // Change color of friendly or enemy piece, king excepted
+ const oldColor = (move.egg == "waluigi" ? color : C.GetOppTurn(color));
+ const newColor = C.GetOppTurn(oldColor);
+ const coords = getRandomPiece(oldColor);
+ if (coords) {
+ const piece = this.getPiece(coords[0], coords[1]);
+ if (coords[0] == move.start.x && coords[1] == move.start.y) {
+ // Moving piece change color: fix coords
+ coords = [move.end.x, move.end.y];
+ }
+ em = new Move({
+ appear: [
+ new PiPo({x: coords[0], y: coords[1], c: newColor, p: piece})
+ ],
+ vanish: [
+ new PiPo({x: coords[0], y: coords[1], c: oldColor, p: piece})
+ ]
+ });
+ em.luigiEffect = true; //avoid dropping bomb/banana by mistake
+ }
+ break;
+ case "bowser":
+ em = new Move({
+ appear: [
+ new PiPo({
+ x: move.end.x,
+ y: move.end.y,
+ c: color,
+ p: V.IMMOBILIZE_CODE[move.appear[0].p]
+ })
+ ],
+ vanish: [
+ new PiPo({
+ x: move.end.x,
+ y: move.end.y,
+ c: color,
+ p: move.appear[0].p
+ })
+ ]
+ });
+ break;
+ case "koopa":
+ // Reverse move, if possible
+ em = new Move({
+ appear: [],
+ vanish: [
+ new PiPo({
+ x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p
+ })
+ ],
+ end: {x: move.start.x, y: move.start.y} //may be irrelevant
+ });
+ em.koopa = true; //avoid applying effect
+ if (move.vanish.length == 0)
+ // After toadette+drop, just erase piece
+ break;
+ em.appear.push(
+ new PiPo({
+ x: move.start.x, y: move.start.y, c: color, p: move.appear[0].p
+ })
+ );
+ if (this.board[move.start.x][move.start.y] != "") {
+ // Pawn or knight let something on init square
+ em.vanish.push(new PiPo({
+ x: move.start.x,
+ y: move.start.y,
+ c: 'a',
+ p: this.getPiece(move.start.x, move.start.y)
+ }));
+ }
+ break;
+ case "chomp":
+ // Eat piece
+ em = new Move({
+ appear: [],
+ vanish: [
+ new PiPo({
+ x: move.end.x, y: move.end.y, c: color, p: move.appear[0].p
+ })
+ ],
+ end: {x: move.end.x, y: move.end.y}
+ });
+ break;
+ }
+ if (em && move.egg != "koopa")
+ em.noAnimate = true; //static move
+ return em;