Implement blocking.

This commit is contained in:
yukirij 2024-08-17 18:50:23 -07:00
parent dc4827c982
commit fd1d99dd77

View File

@ -76,6 +76,7 @@ GAME.Board = class {
reset() {
for(let i = 0; i < this.tiles.length; ++i) { this.tiles[i].reset(); }
for(let i = 0; i < this.columns.length; ++i) { this.columns[i].reset(); }
for(let i = 0; i < this.pieces.length; ++i) { if(this.pieces[i] !== null) { this.pieces[i].reset(); } }
}
};
@ -114,11 +115,12 @@ GAME.Tile = class {
};
GAME.MovementTile = class {
constructor(tile, valid, threat, check) {
constructor(tile, valid, threat, check, block) {
this.tile = tile;
this.valid = valid;
this.threat = threat;
this.check = check;
this.block = block;
}
};
@ -235,6 +237,9 @@ GAME.Game = class {
this.board.tiles[movement.tile].checking = true;
this.state.check = true;
}
if(movement.block != 0) {
this.board.pieces[this.board.tiles[movement.tile].piece].blocking = movement.block;
}
}
}
}
@ -343,12 +348,12 @@ GAME.Game = class {
let directions = moves.direction;
let permitted_moves = directions;
if(piece.blocking != 0) {
if(piece.piece == GAME.Const.PieceId.Omen) {
permitted_moves &= ~piece.blocking;
} else {
permitted_moves &= piece.blocking;
}
let block_directions = piece.blocking;
if(block_directions != 0) {
block_directions |= BITWISE.rotate_blocks(piece.blocking);
//if(piece.piece != GAME.Const.PieceId.Omen) {
// permitted_moves &= piece.blocking;
//}
}
// Check directions of movement.
@ -359,6 +364,7 @@ GAME.Game = class {
// Get initial status in direction.
let valid = (permitted_moves & mask) != 0;
let pieces_blocking = 0;
let move_hex = hex.copy();
@ -369,6 +375,9 @@ GAME.Game = class {
let threat = valid;
let check = false;
let block = 0;
let swap = false;
let tile_occupied = false;
if(HEX.is_valid(move_hex)) {
let tile_id = HEX.hex_to_tile(move_hex);
@ -376,6 +385,7 @@ GAME.Game = class {
let result = valid;
// Prevent moves that do not uncheck the King.
if(piece.player == (this.turn & 1)
&& this.state.check && !tile_data.checking
&& piece.piece != GAME.Const.PieceId.Omen) {
@ -390,12 +400,15 @@ GAME.Game = class {
// Handle occupied tile.
if(tile_data.piece !== null) {
let target = this.board.pieces[tile_data.piece];
tile_occupied = true;
// Target piece is ally.
if(target.player == piece.player) {
// Move is only valid if pieces are swappable.
if(!this.movement_swappable(piece, target, mask, dist, tile)) {
if(this.movement_swappable(piece, target, mask, dist, tile)) {
swap = true;
} else {
result = false;
}
valid = false;
@ -407,18 +420,39 @@ GAME.Game = class {
if(target.piece == GAME.Const.PieceId.Omen) {
if(valid) {
check = true;
block = mask;
// Apply check to previous moves.
for(let idist = 1; idist < dist; idist++) {
tiles[tiles.length - idist].check = true;
}
}
if(pieces_blocking == 1) {
// Apply blocking to last .
for(let idist = 1; idist < dist; idist++) {
if(GAME_DATA.board.tiles[tiles[tiles.length - idist].tile].piece !== null) {
tiles[tiles.length - idist].block = mask;
}
}
}
}
pieces_blocking += 1;
valid = false;
}
}
tiles.push(new GAME.MovementTile(tile_id, result, threat, check));
// Handle blocking restrictions.
if(block_directions != 0) {
if(piece.piece == GAME.Const.PieceId.Omen) {
result = result && ((mask & block_directions) == 0 || tile_occupied);
} else {
result = result && ((mask & block_directions) != 0 || swap);
}
}
tiles.push(new GAME.MovementTile(tile_id, result, threat, check, block));
} else { break; }
}
@ -438,7 +472,8 @@ GAME.Game = class {
// King cannot swap onto tile that is threatened.
// This case should also cover blocking.
if(target.piece == GAME.Const.PieceId.Omen && this.board.tiles[tile].threaten[+(!target.player)]) {
if(target.piece == GAME.Const.PieceId.Omen
&& (this.board.tiles[tile].threaten[+(!target.player)] || piece.blocking != 0)) {
return false;
}
return ((moves.direction & mask) != 0 && (range == 1 || (moves.stride & mask) != 0));