Skip to content

Commit

Permalink
Merge pull request #398 from Naviary3/arrows-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Naviary2 authored Jan 25, 2025
2 parents 63e8aa9 + 37c82e6 commit ce78959
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 172 deletions.
7 changes: 5 additions & 2 deletions src/client/scripts/esm/chess/logic/gamefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import wincondition from './wincondition.js';
import gamerules from '../variants/gamerules.js';
// Type Definitions...

/** @typedef {import('../../util/math.js').Vec2} Vec2 */
/** @typedef {import('../../util/math.js').BoundingBox} BoundingBox */
/** @typedef {import('./movepiece.js').Move} Move */
/** @typedef {import('../../game/rendering/buffermodel.js').BufferModel} BufferModel */
Expand All @@ -23,7 +24,7 @@ import gamerules from '../variants/gamerules.js';
/** @typedef {import('./organizedlines.js').PiecesByKey} PiecesByKey */
/** @typedef {import('./organizedlines.js').LinesByStep} LinesByStep */

'use strict';
'use strict';

/**
* Constructs a gamefile from provided arguments. Use the *new* keyword.
Expand Down Expand Up @@ -63,11 +64,13 @@ function gamefile(metadata, { moves = [], variantOptions, gameConclusion, clockV
box: undefined,
/** A set of all types of pieces that are in this game, without their color extension: `['pawns','queens']` @type {string[]} */
existingTypes: undefined,
/** Possible sliding moves in this game, dependant on what pieces there are: `[[1,1],[1,0]]` @type {Coords[]}*/
/** Possible sliding moves in this game, dependant on what pieces there are: `[[1,1],[1,0]]` @type {Vec2[]}*/
slidingPossible: undefined,
/** Whether hippogonal lines, or greater, are present in the gamefile.
* True if there are knightriders, or greater, riders. @type {boolean} */
hippogonalsPresent: undefined,
/** Whether any single piece has a custom blocking function for the game. */
atleastOneCustomBlocking: undefined
};

/** @type {GameRules} */
Expand Down
3 changes: 3 additions & 0 deletions src/client/scripts/esm/chess/logic/initvariant.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import colorutil from '../util/colorutil.js';
import coordutil from '../util/coordutil.js';
import variant from '../variants/variant.js';
import organizedlines from './organizedlines.js';
import movesets from './movesets.js';
// Import End

/**
Expand Down Expand Up @@ -72,6 +73,8 @@ function initExistingTypes(gamefile) {
function initSlidingMoves(gamefile) {
gamefile.startSnapshot.slidingPossible = getPossibleSlides(gamefile);
gamefile.startSnapshot.hippogonalsPresent = organizedlines.areHippogonalsPresentInGame(gamefile.startSnapshot.slidingPossible);
gamefile.startSnapshot.atleastOneCustomBlocking = movesets.doMovesetsContainAtleastOneCustomBlocking(gamefile.pieceMovesets);
console.log("Game contains atleast one custom blocking: " + gamefile.startSnapshot.atleastOneCustomBlocking);
}

/**
Expand Down
20 changes: 15 additions & 5 deletions src/client/scripts/esm/chess/logic/movesets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ interface PieceMoveset {
/**
* The initial function that determines how far a piece is legally able to slide
* according to what pieces block it.
*
* This should be provided if we're not using the default.
*/
blocking?: BlockingFunction,
/**
* The secondary function that *actually* determines whether each individual
* square in a slide is legal to move to.
*
* This should be provided if we're not using the default.
*/
ignore?: IgnoreFunction
}
Expand All @@ -72,10 +76,7 @@ interface PieceMoveset {
* as it can test if the squares are check for positive.
*/
// eslint-disable-next-line no-unused-vars
type IgnoreFunction = (startCoords: Coords, endCoords: Coords, gamefile?: gamefile, detectCheck?: (gamefile: gamefile, color: string, attackers: {
coords: Coords,
slidingCheck: boolean
}) => boolean) => boolean;
type IgnoreFunction = (startCoords: Coords, endCoords: Coords) => boolean;

/**
* This runs once for every piece on the same line of the selected piece.
Expand All @@ -90,7 +91,7 @@ type IgnoreFunction = (startCoords: Coords, endCoords: Coords, gamefile?: gamefi
* pieces "transparent", allowing friendly pieces to phase through them.
*/
// eslint-disable-next-line no-unused-vars
type BlockingFunction = (friendlyColor: string, blockingPiece: Piece, coords: Coords, gamefile?: gamefile) => 0 | 1 | 2;
type BlockingFunction = (friendlyColor: string, blockingPiece: Piece, coords: Coords) => 0 | 1 | 2;



Expand Down Expand Up @@ -287,12 +288,21 @@ function getPieceDefaultMovesets(slideLimit: number = Infinity): Movesets {
};
}

/** Whether the provided movesets contain atleast one custom blocking function. */
function doMovesetsContainAtleastOneCustomBlocking(movesets: Movesets) {
for (const pieceMoveset of Object.values(movesets)) {
if ('blocking' in pieceMoveset) return true;
}
return false;
}



export default {
getPieceDefaultMovesets,
defaultBlockingFunction,
defaultIgnoreFunction,
doMovesetsContainAtleastOneCustomBlocking,
};

export type { Movesets, PieceMoveset, Coords, BlockingFunction, IgnoreFunction };
4 changes: 2 additions & 2 deletions src/client/scripts/esm/chess/logic/organizedlines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ function getXFromLine(step: Coords, coords: Coords): number {
}

/** Splits the `C` value out of the line key */
function getCFromKey(lineKey: LineKey): number {
function getXFromKey(lineKey: LineKey): number {
return Number(lineKey.split('|')[0]);
}

Expand Down Expand Up @@ -408,7 +408,7 @@ export default {
buildStateFromKeyList,
getKeyFromLine,
getCFromLine,
getCFromKey,
getXFromKey,
areColinearSlidesPresentInGame,
areHippogonalsPresentInGame,
};
3 changes: 3 additions & 0 deletions src/client/scripts/esm/game/chess/movesequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ function makeMove(gamefile: gamefile, moveDraft: MoveDraft, { doGameOverChecks =
if (gamefileutility.isGameOver(gamefile) && !onlinegame.areInOnlineGame()) gameslot.concludeGame();
}

// Whenever a move is made in the game, the color of the legal move highlights
// of the hovered arrows often changes.
// Erase the list so they can be regenerated next frame with the correct color.
arrows.clearListOfHoveredPieces();

return move;
Expand Down
Loading

0 comments on commit ce78959

Please sign in to comment.