From: Jean-Patrick Guerrero Date: Sat, 25 Aug 2018 12:28:52 +0000 (+0200) Subject: Chess cleanup X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=3c1423a462fa94e3eb681d975c06ca54d16e9993;p=xdecor.git Chess cleanup --- diff --git a/src/chess.lua b/src/chess.lua index 53d05f1..f037eee 100644 --- a/src/chess.lua +++ b/src/chess.lua @@ -218,6 +218,8 @@ function realchess.init(pos) meta:set_string("playerBlack", "") meta:set_string("playerWhite", "") meta:set_string("lastMove", "") + meta:set_string("blackAttacked", "") + meta:set_string("whiteAttacked", "") meta:set_int("lastMoveTime", 0) meta:set_int("castlingBlackL", 1) @@ -706,9 +708,24 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, _, player local blackAttacked = attacked("black", black_king_idx, board) local whiteAttacked = attacked("white", white_king_idx, board) - if (thisMove == "black" and blackAttacked) or - (thisMove == "white" and whiteAttacked) then - return 0 + if blackAttacked then + if thisMove == "black" and meta:get_string("blackAttacked") == "true" then + return 0 + else + meta:set_string("blackAttacked", "true") + end + else + meta:set_string("blackAttacked", "") + end + + if whiteAttacked then + if thisMove == "white" and meta:get_string("whiteAttacked") == "true" then + return 0 + else + meta:set_string("whiteAttacked", "true") + end + else + meta:set_string("whiteAttacked", "") end lastMove = thisMove @@ -730,10 +747,8 @@ function realchess.on_move(pos, from_list, from_index) local inv = meta:get_inventory() inv:set_stack(from_list, from_index, '') - local board = board_to_table(inv) - local black_king_idx, white_king_idx = locate_kings(board) - local black_king_attacked = attacked("black", black_king_idx, board) - local white_king_attacked = attacked("white", white_king_idx, board) + local black_king_attacked = meta:get_string("blackAttacked") == "true" + local white_king_attacked = meta:get_string("whiteAttacked") == "true" local playerWhite = meta:get_string("playerWhite") local playerBlack = meta:get_string("playerBlack")