Chess: Fix eaten pieces list being wrong sometimes

This commit is contained in:
Wuzzy 2023-07-17 09:46:51 +02:00
parent d7b681e1b9
commit 5f210286bf

View File

@ -1383,7 +1383,9 @@ end
-- Verify eaten list -- Verify eaten list
local verify_eaten_list local verify_eaten_list
if CHESS_DEBUG then if CHESS_DEBUG then
verify_eaten_list = function(meta, board) verify_eaten_list = function(meta)
local inv = meta:get_inventory()
local board = board_to_table(inv)
local whitePiecesLeft = 0 local whitePiecesLeft = 0
local whitePiecesEaten = 0 local whitePiecesEaten = 0
local blackPiecesLeft = 0 local blackPiecesLeft = 0
@ -1424,17 +1426,22 @@ if CHESS_DEBUG then
end end
end end
local function add_to_eaten_list(meta, pieceTo, board) -- Reports that a piece was "eaten" (=captured).
if pieceTo ~= "" then -- Must be called right after the board inventory was updated
-- on which the piece is already removed
-- * meta: Chessboard node metadata
-- * piece: The itemname of the piece that was captured
local function add_to_eaten_list(meta, piece)
if piece ~= "" then
local eaten = meta:get_string("eaten") local eaten = meta:get_string("eaten")
if eaten ~= "" then if eaten ~= "" then
eaten = eaten .. "," eaten = eaten .. ","
end end
local pieceTo_s = pieceTo:match(":(%w+_%w+)") or "" local piece_s = piece:match(":(%w+_%w+)") or ""
eaten = eaten .. pieceTo_s eaten = eaten .. piece_s
meta:set_string("eaten", eaten) meta:set_string("eaten", eaten)
if CHESS_DEBUG then if CHESS_DEBUG then
verify_eaten_list(meta, board) verify_eaten_list(meta)
end end
end end
end end
@ -2358,6 +2365,7 @@ function realchess.move(meta, from_list, from_index, to_list, to_index, playerNa
if pieceTo ~= "" then if pieceTo ~= "" then
resetHalfmoveClock = true resetHalfmoveClock = true
end end
-- The halfmove clock counts the number of consecutive halfmoves -- The halfmove clock counts the number of consecutive halfmoves
-- in which neither a pawn was moved nor a piece was captured. -- in which neither a pawn was moved nor a piece was captured.
if resetHalfmoveClock then if resetHalfmoveClock then
@ -2367,7 +2375,10 @@ function realchess.move(meta, from_list, from_index, to_list, to_index, playerNa
end end
if en_passant_target then if en_passant_target then
-- Capture pawn en passant
local capturedPiece = inv:get_stack(to_list, en_passant_target):get_name()
inv:set_stack(to_list, en_passant_target, "") inv:set_stack(to_list, en_passant_target, "")
add_to_eaten_list(meta, capturedPiece)
end end
if kingMoved and thisMove == "white" then if kingMoved and thisMove == "white" then
@ -2736,9 +2747,17 @@ end
-- Will also update the state for the Chessboard. -- Will also update the state for the Chessboard.
function realchess.move_piece(meta, pieceFrom, from_list, from_index, to_list, to_index) function realchess.move_piece(meta, pieceFrom, from_list, from_index, to_list, to_index)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local pieceTo = inv:get_stack(to_list, to_index):get_name()
-- Update inventory slots
inv:set_stack(from_list, from_index, "") inv:set_stack(from_list, from_index, "")
inv:set_stack(to_list, to_index, pieceFrom) inv:set_stack(to_list, to_index, pieceFrom)
-- Report the eaten piece
if pieceTo ~= "" then
add_to_eaten_list(meta, pieceTo)
end
local promo = meta:get_string("promotionActive") ~= "" local promo = meta:get_string("promotionActive") ~= ""
if not promo then if not promo then
update_game_result(meta) update_game_result(meta)
@ -2800,7 +2819,6 @@ function realchess.update_state(meta, from_index, to_index, thisMove, promoteFro
special = "promo__"..promoteTo special = "promo__"..promoteTo
end end
add_move_to_moves_list(meta, pieceFrom, pieceTo, from_index, to_index, special) add_move_to_moves_list(meta, pieceFrom, pieceTo, from_index, to_index, special)
add_to_eaten_list(meta, pieceTo, board)
end end
function realchess.promote_pawn(meta, color, promoteTo) function realchess.promote_pawn(meta, color, promoteTo)