forked from bonezone2001/auto-chess-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieChess.lua
193 lines (169 loc) · 4.91 KB
/
CookieChess.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
local HttpService = game:GetService("HttpService")
local plr = game:GetService("Players").LocalPlayer
local scriptPath = plr.PlayerGui:WaitForChild("Client")
local pieces = {
["Pawn"] = "p",
["Knight"] = "n",
["Bishop"] = "b",
["Rook"] = "r",
["Queen"] = "q",
["King"] = "k"
}
-- Fuck you error logger
if game:GetService("ReplicatedStorage").Connections:FindFirstChild("ReportClientError") then
game:GetService("ReplicatedStorage").Connections.ReportClientError:Destroy()
for _,v in pairs(getconnections(game:GetService("ScriptContext").Error)) do
v:Disable()
end
end
local client = nil
for _,v in pairs(getreg()) do
if type(v) == "function" then
for _, v in pairs(getupvalues(v)) do
if type(v) == "table" and v.processRound then
client = v
end
end
end
end
assert(client, "failed to find client")
-- Board from client
function getBoard()
for _,v in pairs(debug.getupvalues(client.processRound)) do
if type(v) == "table" and v.tiles then
return v
end
end
return nil
end
-- Gets client's team (white/black)
function getLocalTeam(board)
-- Bot match detection
if board.players[false] == plr and board.players[true] == plr then
return "w"
end
for i, v in pairs(board.players) do
if v == plr then
-- If the index is true, they are white
if i then
return "w"
else
return "b"
end
end
end
return nil
end
function willCauseDesync(board)
-- Bot match detection
if board.players[false] == plr and board.players[true] == plr then
return board.activeTeam == false
end
for i,v in pairs(board.players) do
if v == plr then
-- If the index is true, they are white
return not (board.activeTeam == i)
end
end
return true
end
-- Converts awful format of board table to a sensible one
function createBoard(board)
local newBoard = {}
for _,v in pairs(board.whitePieces) do
if v and v.position then
local x, y = v.position[1], v.position[2]
if not newBoard[x] then
newBoard[x] = {}
end
newBoard[x][y] = string.upper(pieces[v.object.Name])
end
end
for _,v in pairs(board.blackPieces) do
if v and v.position then
local x, y = v.position[1], v.position[2]
if not newBoard[x] then
newBoard[x] = {}
end
newBoard[x][y] = pieces[v.object.Name]
end
end
return newBoard
end
-- Board to FEN encoding
function board2fen(board)
local result = ""
local boardPieces = createBoard(board)
for y = 8, 1, -1 do
local empty = 0
for x = 8, 1, -1 do
if not boardPieces[x] then boardPieces[x] = {} end
local piece = boardPieces[x][y]
if piece then
if empty > 0 then
result = result .. tostring(empty)
empty = 0
end
result = result .. piece
else
empty += 1
end
end
if empty > 0 then
result = result .. tostring(empty)
end
if not (y == 1) then
result = result .. "/"
end
end
result = result .. " " .. getLocalTeam(board)
return result
end
function runGame()
-- Get chess board
local board = getBoard()
-- Check if we're able to run without desync
if willCauseDesync(board) then
return false
end
-- Ask engine for result using fen encoded board
local res = syn.request({
Url = "http://localhost:3000/api/solve?fen=" .. HttpService:UrlEncode(board2fen(board)),
Method = "GET"
})
local result = res.Body
-- Ensure result is valid
if string.len(result) > 4 then
error(result)
end
-- Extrapolate movement from result string
local chars = {}
for c in result:gmatch(".") do
table.insert(chars, c)
end
-- Get move positions from table
local x1 = 9 - (string.byte(chars[1]) - 96)
local y1 = tonumber(chars[2])
local x2 = 9 - (string.byte(chars[3]) - 96)
local y2 = tonumber(chars[4])
-- Make the move
game.ReplicatedStorage.Connections.MovePiece:FireServer(board.id,
{x1, y1},
{x2, y2, ["moveOnly"] = false, ["doublestep"] = 999}
)
client:processRound(
board:getPiece({x1, y1}),
{x2, y2, ["moveOnly"] = false, ["doublestep"] = 999}
)
return true
end
game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
-- Run AI
if (inputObject.KeyCode == shared.runBind) and not gameProcessedEvent then
if runGame() then
print("Ran AI")
else
print("Cannot run AI right now")
end
end
end)