Skip to content

Commit

Permalink
Added the rose piece (buggy)
Browse files Browse the repository at this point in the history
  • Loading branch information
Naviary2 committed Oct 4, 2024
1 parent 157bca1 commit 5771413
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/client/scripts/game/chess/formatconverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const pieceDictionary = {
"royalQueensW": "RQ", "royalQueensB": "rq",
"royalCentaursW": "RC", "royalCentaursB": "rc",
"knightridersW": "NR", "knightridersB": "nr",
"rosesW": "RO", "rosesB": "ro",
"obstaclesN": "ob",
"voidsN": "vo"
};
Expand Down
3 changes: 1 addition & 2 deletions src/client/scripts/game/chess/legalmoves.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import movepiece from './movepiece.js';
import gamefileutility from './gamefileutility.js';
import specialdetect from './specialdetect.js';
import organizedlines from './organizedlines.js';
import math from '../misc/math.js';
import checkdetection from './checkdetection.js';
import colorutil from '../misc/colorutil.js';
import typeutil from '../misc/typeutil.js';
Expand Down Expand Up @@ -195,7 +194,7 @@ function slide_CalcLegalLimit(line, direction, slideMoveset, coords, color) {

// For most we'll be comparing the x values, only exception is the vertical lines.
const axis = direction[0] === 0 ? 1 : 0;
const limit = math.copyCoords(slideMoveset);
const limit = coordutil.copyCoords(slideMoveset);
// Iterate through all pieces on same line
for (let i = 0; i < line.length; i++) {
// What are the coords of this piece?
Expand Down
3 changes: 3 additions & 0 deletions src/client/scripts/game/chess/movesets.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ function getPieceMovesets(slideLimit = Infinity) {
]
};
},
roses: function() {
return { individual: [] };
},
};
}

Expand Down
72 changes: 53 additions & 19 deletions src/client/scripts/game/chess/specialdetect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import colorutil from '../misc/colorutil.js';
import jsutil from '../misc/jsutil.js';
import coordutil from '../misc/coordutil.js';
import gamerules from '../variants/gamerules.js';
import math from '../misc/math.js';
// Import End

/**
Expand Down Expand Up @@ -39,7 +40,8 @@ function getSpecialMoves() {
return {
"kings": kings,
"royalCentaurs": kings,
"pawns": pawns
"pawns": pawns,
"roses": roses,
};
}

Expand Down Expand Up @@ -184,24 +186,6 @@ function pawns(gamefile, coords, color, individualMoves) {
addPossibleEnPassant(gamefile, individualMoves, coords, color);
}

// Use as inspiration for generating the rose piece's legal moves.
// function roses(startCol, startRow) {
// let allAvailableSquares = []
// let movements = [[-2, -1], [-1, -2], [1, -2], [2, -1], [2, 1], [1, 2], [-1, 2], [-2, 1]]

// for(let i = 0; i < movements.length; i++) {
// let last = [getSquareFromCords(startCol, startRow)]
// for(let j = i; j < movements.length + i; j++) {
// last = universal(...getCordsOfSquare(last[0]), ...movements[j % movements.length], 1)
// allAvailableSquares.push(last)
// if(last.length == 0 || getPieceFromSquare(last[last.length-1]) != undefined) break
// }
// }

// allAvailableSquares = [].concat(...allAvailableSquares)
// return allAvailableSquares
// }

/**
* Appends legal enpassant capture to the selected pawn's provided individual moves.
* @param {gamefile} gamefile - The gamefile
Expand Down Expand Up @@ -238,6 +222,56 @@ function addPossibleEnPassant(gamefile, individualMoves, coords, color) {
individualMoves.push(captureSquare);
}

/**
* Appends legal moves for the rose piece to the provided legal individual moves list.
* @param {gamefile} gamefile - The gamefile
* @param {number[]} coords - Coordinates of the rose selected
* @param {string} color - The color of the rose selected
* @param {array[]} individualMoves - The legal individual moves calculated so far
*/
function roses(gamefile, coords, color, individualMoves) {
if (!individualMoves) {
console.error("not present");
console.error(coords);
}
const movements = [[-2, -1], [-1, -2], [1, -2], [2, -1], [2, 1], [1, 2], [-1, 2], [-2, 1]]; // Counter-clockwise
const directions = [1, -1]; // Counter-clockwise and clockwise directions

for (let i = 0; i < movements.length; i++) {
for (const direction of directions) {
let currentCoord = coordutil.copyCoords(coords);
let b = i;
for (let c = 0; c < movements.length - 1; c++) { // Iterate 7 times, since we can't land on the square we started
const movement = movements[math.posMod(b, movements.length)];
currentCoord = coordutil.addCoordinates(currentCoord, movement);
const pieceOnSquare = gamefileutility.getPieceAtCoords(gamefile, currentCoord); // { type, index, coords }
if (pieceOnSquare) {
const colorOfPiece = colorutil.getPieceColorFromType(pieceOnSquare.type);
// eslint-disable-next-line max-depth
if (color !== colorOfPiece) appendCoordToIndividuals(individualMoves, currentCoord); // Capture is legal
break;
}
// There is not a piece
appendCoordToIndividuals(individualMoves, currentCoord);
b += direction; // Update 'b' for the next iteration
}
}
}
}

/**
* Appends a coordinate to the individual moves list if it's not already present.
* @param {array[]} individualMoves - The list of individual moves.
* @param {number[]} newCoord - The coordinate to append [x, y].
*/
function appendCoordToIndividuals(individualMoves, newCoord) {
for (let i = 0; i < individualMoves.length; i++) {
const coord = individualMoves[i];
if (coordutil.areCoordsEqual(coord, newCoord)) return;
}
individualMoves.push(newCoord);
}

/**
* Tests if the piece at the given coordinates has it's special move rights.
* @param {gamefile} gamefile - The gamefile
Expand Down
23 changes: 23 additions & 0 deletions src/client/scripts/game/misc/coordutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,33 @@ function areCoordsEqual_noValidate(coord1, coord2) {
return coord1[0] === coord2[0] && coord1[1] === coord2[1];
}

/**
* Adds two coordinate pairs together component-wise.
*
* @param {number[]} coord1 - The first coordinate pair [x1, y1].
* @param {number[]} coord2 - The second coordinate pair [x2, y2].
* @returns {number[]} The resulting coordinate pair after addition [x1 + x2, y1 + y2].
*/
function addCoordinates(coord1, coord2) {
return [coord1[0] + coord2[0], coord1[1] + coord2[1]];
}

/**
* Makes a deep copy of the provided coordinates
* @param {number[]} coords - [x,y]
* @returns Copied coords
*/
function copyCoords(coords) {
return [coords[0], coords[1]];
}


export default {
areCoordsIntegers,
getKeyFromCoords,
getCoordsFromKey,
areCoordsEqual,
areCoordsEqual_noValidate,
addCoordinates,
copyCoords
};
10 changes: 0 additions & 10 deletions src/client/scripts/game/misc/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,6 @@ function toRadians(angleDegrees) {
return angleDegrees * (Math.PI / 180);
}

/**
* Makes a deep copy of the provided coordinates
* @param {number[]} coords - [x,y]
* @returns Copied coords
*/
function copyCoords(coords) {
return [coords[0], coords[1]];
}

function roundAwayFromZero(value) {
return value > 0 ? Math.ceil(value) : Math.floor(value);
}
Expand Down Expand Up @@ -493,7 +484,6 @@ export default {
manhattanDistance,
chebyshevDistance,
toRadians,
copyCoords,
roundAwayFromZero,
PseudoRandomGenerator,
decimalToPercent,
Expand Down
2 changes: 1 addition & 1 deletion src/client/scripts/game/misc/typeutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import colorutil from "./colorutil.js";
* They are arranged in this order for faster checkmate/draw detection,
* as we should check if the kings have a legal move first.
*/
const types = ['kings', 'giraffes', 'camels', 'zebras', 'knightriders', 'amazons', 'queens', 'royalQueens', 'hawks', 'chancellors', 'archbishops', 'centaurs', 'royalCentaurs', 'knights', 'guards', 'rooks', 'bishops', 'pawns'];
const types = ['kings', 'giraffes', 'camels', 'zebras', 'knightriders', 'amazons', 'queens', 'royalQueens', 'hawks', 'chancellors', 'archbishops', 'centaurs', 'royalCentaurs', 'roses', 'knights', 'guards', 'rooks', 'bishops', 'pawns'];
/** All neutral types the game is compatible with. */
const neutralTypes = ['obstacles', 'voids'];

Expand Down
5 changes: 3 additions & 2 deletions src/client/scripts/game/rendering/movement.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import guititle from '../gui/guititle.js';
import frametracker from './frametracker.js';
import config from '../config.js';
import game from '../chess/game.js';
import coordutil from '../misc/coordutil.js';
// Import End

"use strict";
Expand Down Expand Up @@ -49,7 +50,7 @@ let scaleIsLess1Pixel_Virtual = false; // Set to true when we're so zoomed out,
// Returns a copy of the boardPos in memory, otherwise the memory location
// could be used to modify the original.
function getBoardPos() {
return math.copyCoords(boardPos);
return coordutil.copyCoords(boardPos);
}

// Password for modifying is stored in "passwordForSetting", or is "pidough"
Expand Down Expand Up @@ -472,7 +473,7 @@ function eraseMomentum() {
function setPositionToArea(area, password) {
if (!area) console.error("Cannot set position to an undefined area.");

const copiedCoords = math.copyCoords(area.coords);
const copiedCoords = coordutil.copyCoords(area.coords);
setBoardPos(copiedCoords, password);
setBoardScale(area.scale, password);
}
Expand Down

0 comments on commit 5771413

Please sign in to comment.