Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow slides that are not orthogonal or diagonals #66

Merged
merged 64 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
71cdbe0
customslices
Idonotus Jul 10, 2024
a655f72
render positive and negative gradients
Idonotus Jul 10, 2024
56ade94
Legal move highlighting works™
Idonotus Jul 11, 2024
2df09f0
Now highlighting is actually alligned
Idonotus Jul 11, 2024
8204c6a
Merge remote-tracking branch 'origin/main' into exoticslides
Idonotus Jul 11, 2024
57accee
added knight rider and fixed slope discrepancy
Idonotus Jul 11, 2024
88cd83d
some checkdetection and stop highlights crash
Idonotus Jul 12, 2024
3acb519
general fixes + prep slide tacking
Idonotus Jul 12, 2024
b23b779
Check detection and blocking
Idonotus Jul 12, 2024
7c4bce7
Allow for any line to slide correctly
Idonotus Jul 12, 2024
64a2ee7
jank auto slide allocation
Idonotus Jul 12, 2024
01f3495
fix slide blocking and castling oversight
Idonotus Jul 13, 2024
f54fcb1
doc linekeyfunction
Idonotus Jul 13, 2024
a9f82e7
highlight lines render atleast
Idonotus Jul 14, 2024
7725479
Merge branch 'main' into exoticslides
Idonotus Jul 14, 2024
d3b89c1
boundng box interesection fix
Idonotus Jul 14, 2024
fc2d154
highlight line selection and cleanup
Idonotus Jul 14, 2024
44a907a
moveset slidelimit changes
Idonotus Jul 15, 2024
f023e54
made the rider get its own horse + jsdoc changes
Idonotus Jul 15, 2024
b399920
fixed highlight lines and orthoganol highlights
Idonotus Jul 15, 2024
034d1e5
vertical highlight line selection
Idonotus Jul 15, 2024
3a22d6c
Added knightriders to Knighted Chess variant
Naviary2 Jul 15, 2024
7fad1c0
Merge branch 'main' into exoticslides
Idonotus Jul 16, 2024
8ea0854
renames and arrows
Idonotus Jul 16, 2024
99c5c32
All arrow changes and arrow general fixes
Idonotus Jul 16, 2024
45a1a8f
Jsdoc and renaming
Idonotus Jul 16, 2024
e151390
highlight.js fix
Idonotus Jul 17, 2024
6aebcbf
Merge branch 'main' into exoticslides
Idonotus Jul 17, 2024
3128b1b
"fixed" edgecase
Idonotus Jul 17, 2024
9068375
cleanup
Idonotus Jul 17, 2024
1cd7a78
not me ignoring what the function does
Idonotus Jul 17, 2024
97be0ef
Fixed highlights by doing maths
Idonotus Jul 17, 2024
efb42ae
highlight shouldn't render outside bounding box
Idonotus Jul 17, 2024
2c81b7c
fixed icons being slightly out of place
Idonotus Jul 17, 2024
d5e8580
cleanup + perspective highlightlines
Idonotus Jul 17, 2024
65693b6
more math fixes woth highlights
Idonotus Jul 17, 2024
8b61690
Some cleaning up, added `existingTypes` to the startSnapshot
Naviary2 Jul 18, 2024
d2e6e9a
I presume this is now auto filled out
Naviary2 Jul 18, 2024
960cf4d
Ups ya forgot the archbishop slides
Naviary2 Jul 18, 2024
c9c09c9
Lines now have the correct key! Fixed checkdetection.js after I broke…
Naviary2 Jul 18, 2024
103fe10
Comment changes
Naviary2 Jul 18, 2024
a5cb0de
Added whitespace
Naviary2 Jul 18, 2024
6a69470
Fixed discovered checks
Naviary2 Jul 18, 2024
4bb1247
Use math func instead
Naviary2 Jul 18, 2024
d1c9c1e
fix negative values generating new keys
Idonotus Jul 18, 2024
e97d756
key format changes
Idonotus Jul 18, 2024
eb3448b
Cleaned up a lot in checkdetection.js. Added a lot of JSDoc. Moved so…
Naviary2 Jul 19, 2024
960dc5c
Moved checkmate alg to new script
Naviary2 Jul 19, 2024
d911421
Description for checkmate.js
Naviary2 Jul 19, 2024
9970506
Auto swap to royalcapture when games with colinear slides are detected
Naviary2 Jul 19, 2024
e740a4f
Func renamed
Naviary2 Jul 19, 2024
e6f4697
improve arrow required algorithm
Idonotus Jul 19, 2024
9ca4e82
Document of `math.getLineSteps`
Idonotus Jul 19, 2024
05609eb
highlight outside of render fix
Idonotus Jul 19, 2024
cb50110
cleanup and documentation
Idonotus Jul 19, 2024
3a3e4eb
Lesson learned: review a mass rename
Idonotus Jul 19, 2024
4eba167
CleanUp
Idonotus Jul 19, 2024
7af4499
Merge branch 'main' into exoticslides
Idonotus Jul 20, 2024
b7ccd11
Updated highlights.js algorithm to be bug free and more friendly to c…
Naviary2 Jul 22, 2024
63cab0d
Fix
Naviary2 Jul 22, 2024
aba8b11
Fix
Naviary2 Jul 22, 2024
86f0efa
Merge branch 'exoticslides' of https://github.com/idonotus/infinitech…
Naviary2 Jul 22, 2024
d44ae6d
uhhhh fix????
Naviary2 Jul 22, 2024
6df5bb5
Backwards compatibility with loading old Knighted Chess positions
Naviary2 Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 56 additions & 90 deletions src/client/scripts/game/chess/checkdetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,18 @@ const checkdetection = (function(){
if (!coord) return false;
if (colorOfFriendly !== 'white' && colorOfFriendly !== 'black') throw new Error(`Cannot detect if an opponent is attacking the square of the team of color ${colorOfFriendly}!`)

let atleast1Attacker = false;

// How do we find out if this square is attacked?

// 1. We check every square within a 3 block radius to see if there's any attacking pieces.

if (doesVicinityAttackSquare(gamefile, coord, colorOfFriendly, attackers)) atleast1Attacker = true;
if (doesVicinityAttackSquare(gamefile, coord, colorOfFriendly, attackers)) return true;
// What about pawns? Could they capture us?
if (doesPawnAttackSquare(gamefile, coord, colorOfFriendly, attackers)) atleast1Attacker = true;
if (doesPawnAttackSquare(gamefile, coord, colorOfFriendly, attackers)) return true;

// 2. We check every orthogonal and diagonal to see if there's any attacking pieces.
if (doesSlideAttackSquare(gamefile, coord, colorOfFriendly, attackers)) atleast1Attacker = true;
if (doesSlideAttackSquare(gamefile, coord, colorOfFriendly, attackers)) return true;

return atleast1Attacker; // Being attacked if true
return false; // Being attacked if true
}

// Checks to see if any piece within a 3-block radius can capture. Ignores sliding movesets.
Expand Down Expand Up @@ -115,34 +113,28 @@ const checkdetection = (function(){
}

// Returns true if there's any sliding piece that can capture on that square
/**
*
* @param {gamefile} gamefile
* @param {number[][]} coords
* @param {string} color
* @param {Array} attackers
* @returns
*/
function doesSlideAttackSquare (gamefile, coords, color, attackers) {
const lines = gamefile.slideMoves;
for (let i=0; i<0; i++) {
const line = lines[i]
const strline = math.getKeyFromCoords(line)
const key = math.getLineFromCoords(line, coords)
if (doesLineAttackSquare(gamefile, gamefile.piecesOrganizedByLines[strline][key], line, coords, color, attackers)) return true;
}

let atleast1Attacker = false;

// Horizontal
const row = gamefile.piecesOrganizedByRow[coords[1]];
if (doesLineAttackSquare(gamefile, row, 'horizontal', coords, color, attackers)) atleast1Attacker = true;

// Vertical
const column = gamefile.piecesOrganizedByColumn[coords[0]];
if (doesLineAttackSquare(gamefile, column, 'vertical', coords, color, attackers)) atleast1Attacker = true;

// Diagonal Up
let key = math.getUpDiagonalFromCoords(coords)
let diag = gamefile.piecesOrganizedByUpDiagonal[key];
if (doesLineAttackSquare(gamefile, diag, 'diagonalup', coords, color, attackers)) atleast1Attacker = true;

// Diagonal Down
key = math.getDownDiagonalFromCoords(coords)
diag = gamefile.piecesOrganizedByDownDiagonal[key];
if (doesLineAttackSquare(gamefile, diag, 'diagonaldown', coords, color, attackers)) atleast1Attacker = true;

return atleast1Attacker;
return false;
}

// Returns true if a piece on the specified line can capture on that square
// THIS REQUIRES coords be already on the line.
// direction = 'horizontal' / 'vertical' / 'diagonalup' / 'diagonaldown'
function doesLineAttackSquare(gamefile, line, direction, coords, colorOfFriendlys, attackers) {

if (!line) return false; // No line, no pieces to attack
Expand All @@ -155,12 +147,12 @@ const checkdetection = (function(){
if (thisPieceColor === 'neutral') continue;

const thisPieceMoveset = legalmoves.getPieceMoveset(gamefile, thisPiece.type)
const lineIsVertical = direction === 'vertical' ? true : false;
const moveset = direction === 'horizontal' ? thisPieceMoveset.horizontal
const lineIsVertical = direction[0]==0
/**const moveset = direction === 'horizontal' ? thisPieceMoveset.horizontal
: direction === 'vertical' ? thisPieceMoveset.vertical
: direction === 'diagonalup' ? thisPieceMoveset.diagonalUp
: thisPieceMoveset.diagonalDown;
const thisPieceLegalSlide = legalmoves.slide_CalcLegalLimit(line, lineIsVertical, moveset, thisPiece.coords, thisPieceColor)
: thisPieceMoveset.diagonalDown;*/
const thisPieceLegalSlide = legalmoves.slide_CalcLegalLimit(line, lineIsVertical, thisPieceMoveset.slideMoves[line], thisPiece.coords, thisPieceColor)
if (!thisPieceLegalSlide) continue; // This piece has no horizontal moveset, NEXT piece on this line!

// const rectangle = {left: thisPieceLegalSlide[0], right: thisPieceLegalSlide[1], bottom: coords[1], top: coords[1]}
Expand All @@ -170,6 +162,7 @@ const checkdetection = (function(){
if (isWithinMoveset) {
if (attackers) appendAttackerToList(attackers, { coords: thisPiece.coords, slidingCheck: true })
return true; // There'll never be more than 1 checker on the same line
// There are 2 sides? s<-k->s
}
}

Expand Down Expand Up @@ -230,10 +223,10 @@ const checkdetection = (function(){
return movepiece.simulateMove(gamefile, move, color).isCheck;
}

// Time complexity: O(1)
// Time complexity: O(slides) basically O(1) unless you add a ton of slides to a single piece
function removeSlidingMovesThatPutYouInCheck (gamefile, moves, pieceSelected, color) {

Idonotus marked this conversation as resolved.
Show resolved Hide resolved
if (!moves.horizontal && !moves.vertical && !moves.diagonalUp && !moves.diagonalDown) return; // No sliding movesets to remove
if (math.isEmpty) return;

const royalCoords = gamefileutility.getJumpingRoyalCoords(gamefile, color); // List of coordinates of all our royal jumping pieces

Expand Down Expand Up @@ -297,76 +290,49 @@ const checkdetection = (function(){
return true;

function eraseAllSlidingMoves() {
legalMoves.vertical = undefined;
legalMoves.horizontal = undefined;
legalMoves.diagonalUp = undefined;
legalMoves.diagonalDown = undefined;
legalMoves.slides = {}
}
}

function removeSlidingMovesThatOpenDiscovered (gamefile, moves, kingCoords, pieceSelected, color) {

const selectedPieceCoords = pieceSelected.coords;
let sameLines = [];
for (const line of gamefile.slideMoves) { // Only check current possible slides
if (math.getLineFromCoords(line, kingCoords) !== math.getLineFromCoords(line, selectedPieceCoords)) continue;
sameLines.push(line);
};

const sameRow = kingCoords[1] === selectedPieceCoords[1];
const sameColumn = kingCoords[0] === selectedPieceCoords[0];

const kingDiagUp = math.getUpDiagonalFromCoords(kingCoords);
const selectedPieceDiagUp = math.getUpDiagonalFromCoords(selectedPieceCoords);
const sameUpDiagonal = kingDiagUp === selectedPieceDiagUp;

const kingDiagDown = math.getDownDiagonalFromCoords(kingCoords);
const selectedPieceDiagDown = math.getDownDiagonalFromCoords(selectedPieceCoords);
const sameDownDiagonal = kingDiagDown === selectedPieceDiagDown;

// If not sharing any common line, there's no way we can open a discovered
if (!sameRow && !sameColumn && !sameUpDiagonal && !sameDownDiagonal) return;
if (sameLines.length===0) return;

// Delete the piece, and add it back when we're done!
const deletedPiece = math.deepCopyObject(pieceSelected);
movepiece.deletePiece(gamefile, pieceSelected, { updateData: false })

if (sameRow) {
// Does a movement off the row expose a discovered check?
const row = gamefile.piecesOrganizedByRow[kingCoords[1]]
const opensDiscovered = doesLineAttackSquare(gamefile, row, 'horizontal', kingCoords, color, [])
if (opensDiscovered) { // Remove the selected piece's vertical, and both diagonal movesets
moves.vertical = undefined;
moves.diagonalUp = undefined;
moves.diagonalDown = undefined;
}
}

else if (sameColumn) {
const column = gamefile.piecesOrganizedByColumn[kingCoords[0]]
const opensDiscovered = doesLineAttackSquare(gamefile, column, 'vertical', kingCoords, color, [])
if (opensDiscovered) { // Remove the selected piece's horizontal, and both diagonal movesets
moves.horizontal = undefined;
moves.diagonalUp = undefined;
moves.diagonalDown = undefined;
}
}

else if (sameUpDiagonal) {
const key = math.getUpDiagonalFromCoords(kingCoords)
const diagUp = gamefile.piecesOrganizedByUpDiagonal[key]
const opensDiscovered = doesLineAttackSquare(gamefile, diagUp, 'diagonalup', kingCoords, color, [])
if (opensDiscovered) { // Remove the selected piece's horizontal, vertical, and down diagonal movesets
moves.horizontal = undefined;
moves.vertical = undefined;
moves.diagonalDown = undefined;
}
movepiece.deletePiece(gamefile, pieceSelected, { updateData: false });

let checklines = [];
for (const line of sameLines) {
const strline = math.getKeyFromCoords(line);
const key = math.getLineFromCoords(line,kingCoords);
const opensDiscovered = doesLineAttackSquare(gamefile, gamefile.piecesOrganizedByLines[strline][key], 'horizontal', kingCoords, color, [])
if (!opensDiscovered) continue;
checklines.push(line);
}

else { // sameDownDiagonal === true
const key = math.getDownDiagonalFromCoords(kingCoords)
const diagDown = gamefile.piecesOrganizedByDownDiagonal[key]
const opensDiscovered = doesLineAttackSquare(gamefile, diagDown, 'diagonaldown', kingCoords, color, [])
if (opensDiscovered) { // Remove the selected piece's horizontal, vertical, and up diagonal movesets
moves.horizontal = undefined;
moves.vertical = undefined;
moves.diagonalUp = undefined;
const tempslides = {}
if (checklines.length > 1) {
if (math.areLinesCollinear(checklines)) {
for (const line of checklines) {
const strline = math.getKeyFromCoords(line)
tempslides[strline] = moves.slideMoves[strline]
}
} else {
// Cannot slide to block all attack lines so blank the slides
// Could probably blank regular attacks too
}
} else if (checklines.length === 1) {
const strline = math.getKeyFromCoords(checklines[0])
tempslides[strline] = moves.slideMoves[strline]
}

// Add the piece back with the EXACT SAME index it had before!!
Expand Down
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 @@ -31,6 +31,7 @@ const formatconverter = (function() {
"centaursW": "CE", "centaursB": "ce",
"royalQueensW": "RQ", "royalQueensB": "rq",
"royalCentaursW": "RC", "royalCentaursB": "rc",
"knightRidersW": "NR", "knightRidersB": "nr",
"obstaclesN": "ob",
"voidsN": "vo"
};
Expand Down
13 changes: 4 additions & 9 deletions src/client/scripts/game/chess/gamefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,10 @@ function gamefile(metadata, { moves = [], variantOptions, gameConclusion } = {})
this.ourPieces = undefined;
/** Pieces organized by key: `{ '1,2':'queensW', '2,3':'queensW' }` */
this.piecesOrganizedByKey = undefined;
/** Pieces organized by row: `{ 2:[{type:'queensW',coords:[1,2]}] }` */
this.piecesOrganizedByRow = undefined;
/** Pieces organized by column: `{ 1:[{type:'queensW',coords:[1,2]}] }` */
this.piecesOrganizedByColumn = undefined;
/** Pieces organized by up-diagonal (slope 1). Each diagonal is given the integer value of it's y-intercept on the grid. i.e. The '0' diagonal intersects (0,0), the '1' diagonal intersects (0,1)... */
this.piecesOrganizedByUpDiagonal = undefined;
/** Pieces organized by down-diagonal (slope -1). Each diagonal is given the integar value of it's y-intercept on the grid. i.e. The '0' diagonal intersects (0,0), the '1' diagonal intersects (0,1)... */
this.piecesOrganizedByDownDiagonal = undefined;
/** Pieces organized by lines: `{ '1,0' { 2:[{type:'queensW',coords:[1,2]}] } }` */
this.piecesOrganizedByLines = undefined;
/** Legal slides*/
this.slideMoves = [[1,1],[1,-1],[1,0],[0,1],[1,2],[1,-2],[2,1],[2,-1]];
Idonotus marked this conversation as resolved.
Show resolved Hide resolved

/** The object that contains the buffer model to render the pieces */
this.mesh = {
Expand Down Expand Up @@ -159,7 +155,6 @@ function gamefile(metadata, { moves = [], variantOptions, gameConclusion } = {})
/** The number of half-moves played since the last capture or pawn push. */
this.moveRuleState = this.gameRules.moveRule ? this.startSnapshot.moveRuleState : undefined;
area.initStartingAreaBox(this);

/** The move list. @type {Move[]} */
this.moves = [];
/** Index of the move we're currently viewing in the moves list. -1 means we're looking at the very beginning of the game. */
Expand Down
Loading