]> git.lizzy.rs Git - xdecor.git/commitdiff
Chess cleanup
authorJean-Patrick Guerrero <jeanpatrick.guerrero@gmail.com>
Sat, 25 Aug 2018 09:40:30 +0000 (11:40 +0200)
committerJean-Patrick Guerrero <jeanpatrick.guerrero@gmail.com>
Sat, 25 Aug 2018 09:40:30 +0000 (11:40 +0200)
src/chess.lua

index f6784bbca0d5d17cc27167fa4028ad4efb6b4ad2..193149013719a1b3716f706a1eaa703964f4558d 100644 (file)
@@ -27,7 +27,7 @@ local rookThreats   = {false, true,  false, true,  true,  false, true,  false}
 local queenThreats  = {true,  true,  true,  true,  true,  true,  true,  true}
 local kingThreats   = {true,  true,  true,  true,  true,  true,  true,  true}
 
-local function king_attack(color, idx, inv)
+local function attacked(color, idx, inv)
        local threatDetected = false
        local kill           = color == "white"
        local pawnThreats    = {kill, false, kill, false, false, not kill, false, not kill}
@@ -683,7 +683,7 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, _, player
                        meta:set_int("castlingWhiteL", 0)
                        meta:set_int("castlingWhiteR", 0)
 
-                       local whiteAttacked = king_attack("white", to_index, inv)
+                       local whiteAttacked = attacked("white", to_index, inv)
                        if whiteAttacked then
                                return 0
                        end
@@ -692,7 +692,7 @@ function realchess.move(pos, from_list, from_index, to_list, to_index, _, player
                        meta:set_int("castlingBlackL", 0)
                        meta:set_int("castlingBlackR", 0)
 
-                       local blackAttacked = king_attack("black", to_index, inv)
+                       local blackAttacked = attacked("black", to_index, inv)
                        if blackAttacked then
                                return 0
                        end
@@ -773,12 +773,12 @@ end
 
 function realchess.on_move(pos, from_list, from_index)
        local meta = minetest.get_meta(pos)
-       local inv  = minetest.get_meta(pos):get_inventory()
+       local inv  = meta:get_inventory()
        inv:set_stack(from_list, from_index, '')
 
        local black_king_idx, white_king_idx = locate_kings(inv)
-       local black_king_attacked = king_attack("black", black_king_idx, inv)
-       local white_king_attacked = king_attack("white", white_king_idx, inv)
+       local black_king_attacked = attacked("black", black_king_idx, inv)
+       local white_king_attacked = attacked("white", white_king_idx, inv)
 
        local playerWhite = meta:get_string("playerWhite")
        local playerBlack = meta:get_string("playerBlack")