-
Notifications
You must be signed in to change notification settings - Fork 1
/
detection.lua
263 lines (255 loc) · 9.05 KB
/
detection.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
local detection = {}
detection.pieceAt = function (x, y, white)
if white == nil or white == true then
for i, piece in ipairs(wpieces) do
if piece.dead == false and piece.x == x and piece.y == y then
return piece
end
end
end
if white == nil or white == false then
for i, piece in ipairs(bpieces) do
if piece.dead == false and piece.x == x and piece.y == y then
return piece
end
end
end
end
detection.highlightAt = function (x, y)
for i, highlight in ipairs(highlights) do
if highlight.x == x and highlight.y == y then
return true
end
end
return false
end
--if checkingAttack is true, it checks if a certain square is being attacked instead of creating highlights
--ax and ay are attack x and attack y
detection.createHighlights = function (piece, checkingAttack, ax, ay)
if not checkingAttack then
pieceIndexOffset = 0
print(piece)
elseif pieceIndexOffset == nil then
--to avoid errors when checking for pinned pieces
pieceIndexOffset = 0
end
--pawn
if piece.type == 6 then
--if piece is white, ydelta = -1, else = 1
yDelta = piece.white and -1 or 1
--check if there's a piece directly in front
if detection.pieceAt(piece.x, piece.y + yDelta) ~= nil then
pieceIndexOffset = pieceIndexOffset + 2
else
--check if pawn is allowed to move 2 squares
if piece.firstMove and detection.pieceAt(piece.x, piece.y + yDelta * 2) == nil and not checkingAttack then
highlights[2] = Highlight:create{x = piece.x, y = piece.y + yDelta}
highlights[3 - pieceIndexOffset] = Highlight:create{x = piece.x, y = piece.y + yDelta * 2}
--should also store that it moved 2 squares for en passant
--if not, move one square
elseif not checkingAttack then
highlights[2] = Highlight:create{x = piece.x, y = piece.y + yDelta}
pieceIndexOffset = pieceIndexOffset + 1
end
end
--checks if it can take left
if detection.pieceAt(piece.x - 1, piece.y + yDelta, not piece.white) ~= nil or checkingAttack then
detection.createHighlightAt(4 - pieceIndexOffset, piece.x - 1,piece.y + yDelta, checkingAttack, ax, ay)
elseif not checkingAttack then
pieceIndexOffset = pieceIndexOffset + 1
end
--checks if it can take right
if detection.pieceAt(piece.x + 1, piece.y + yDelta, not piece.white) ~= nil or checkingAttack then
detection.createHighlightAt(5 - pieceIndexOffset, piece.x + 1, piece.y + yDelta, checkingAttack, ax, ay)
elseif not checkingAttack then
pieceIndexOffset = pieceIndexOffset + 1
end
--checks for en passant left
if pawnMoved2Squares ~= nil and detection.pieceAt(piece.x - 1, piece.y, not piece.white) == pawnMoved2Squares then
detection.createHighlightAt(6 - pieceIndexOffset, piece.x - 1, piece.y + yDelta, checkingAttack, ax, ay)
elseif not checkingAttack then
pieceIndexOffset = pieceIndexOffset + 1
end
--checks for en passant right
if pawnMoved2Squares ~= nil and detection.pieceAt(piece.x + 1, piece.y, not piece.white) == pawnMoved2Squares then
detection.createHighlightAt(7 - pieceIndexOffset, piece.x + 1, piece.y + yDelta, checkingAttack, ax, ay)
elseif not checkingAttack then
pieceIndexOffset = pieceIndexOffset + 1
end
--rook
elseif piece.type == 5 then
for li = 1, 4 do
x = (li % 2) * (li <= 2 and 1 or -1)
y = ((li + 1) % 2) * (li <= 2 and 1 or -1)
for i = 1, 7 do
if detection.pieceAt(piece.x + x * i, piece.y + y * i, piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i + 1)
end
break
end
detection.createHighlightAt(1 + i + ((li - 1) * 7) - pieceIndexOffset, piece.x + x * i, piece.y + y * i, checkingAttack, ax, ay)
if detection.pieceAt(piece.x + x * i, piece.y + y * i, not piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i)
end
break
end
end
end
--knight
elseif piece.type == 4 then
for i = 1, 8 do
if i <= 4 then
if detection.pieceAt(piece.x + ((i % 2) * 2) - 1, piece.y + (i <= 2 and 2 or -2), piece.white) ~= nil and not checkingAttack then
pieceIndexOffset = pieceIndexOffset + 1
else
detection.createHighlightAt(i + 1 - pieceIndexOffset, piece.x + ((i % 2) * 2) - 1, piece.y + (i <= 2 and 2 or -2), checkingAttack, ax, ay)
end
else
if detection.pieceAt(piece.x + (i <= 6 and 2 or -2), piece.y + ((i % 2) * 2) - 1, piece.white) ~= nil and not checkingAttack then
pieceIndexOffset = pieceIndexOffset + 1
else
detection.createHighlightAt(i + 1 - pieceIndexOffset, piece.x + (i <= 6 and 2 or -2), piece.y + ((i % 2) * 2) - 1, checkingAttack, ax, ay)
end
end
end
--bishop
elseif piece.type == 3 then
for li = 1, 4 do
x = (li % 2) * 2 - 1
y = (li <= 2 and 1 or -1)
for i = 1, 7 do
if detection.pieceAt(piece.x + x * i, piece.y + y * i, piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i + 1)
end
break
end
detection.createHighlightAt(1 + i + ((li - 1) * 7) - pieceIndexOffset, piece.x + x * i, piece.y + y * i, checkingAttack, ax, ay)
if detection.pieceAt(piece.x + x * i, piece.y + y * i, not piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i)
end
break
end
end
end
--queen
elseif piece.type == 2 then
--diagonal
for li = 1, 4 do
x = (li % 2) * (li <= 2 and 1 or -1)
y = ((li + 1) % 2) * (li <= 2 and 1 or -1)
for i = 1, 7 do
if detection.pieceAt(piece.x + x * i, piece.y + y * i, piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i + 1)
end
break
end
detection.createHighlightAt(1 + i + ((li - 1) * 7) - pieceIndexOffset, piece.x + x * i, piece.y + y * i, checkingAttack, ax, ay)
if detection.pieceAt(piece.x + x * i, piece.y + y * i, not piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i)
end
break
end
end
end
--hor/ver
for li = 1, 4 do
x = (li % 2) * 2 - 1
y = (li <= 2 and 1 or -1)
for i = 1, 7 do
if detection.pieceAt(piece.x + x * i, piece.y + y * i, piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i + 1)
end
break
end
detection.createHighlightAt(29 + i + ((li - 1) * 7) - pieceIndexOffset, piece.x + x * i, piece.y + y * i, checkingAttack, ax, ay)
if detection.pieceAt(piece.x + x * i, piece.y + y * i, not piece.white) ~= nil then
if not checkingAttack then
pieceIndexOffset = pieceIndexOffset + (7 - i)
end
break
end
end
end
--king
elseif piece.type == 1 and not checkingAttack then
for i = 1, 4 do
if (detection.pieceAt(piece.x + (i - 1) % 3 - 1, piece.y + math.floor((i - 1) / 3) - 1, piece.white) ~= nil or detection.beingAttackedAt(piece.x + (i - 1) % 3 - 1, piece.y + math.floor((i - 1) / 3) - 1, not piece.white)) then
pieceIndexOffset = pieceIndexOffset + 1
else
print("created highlight")
detection.createHighlightAt(i + 1 - pieceIndexOffset, piece.x + (i - 1) % 3 - 1, piece.y + math.floor((i - 1) / 3) - 1, checkingAttack, ax, ay)
end
end
for i = 6, 9 do
if (detection.pieceAt(piece.x + (i - 1) % 3 - 1, piece.y + math.floor((i - 1) / 3) - 1, piece.white) ~= nil or detection.beingAttackedAt(piece.x + (i - 1) % 3 - 1, piece.y + math.floor((i - 1) / 3) - 1, not piece.white)) then
pieceIndexOffset = pieceIndexOffset + 1
else
print("created highlight")
detection.createHighlightAt(i - pieceIndexOffset, piece.x + (i - 1) % 3 - 1, piece.y + math.floor((i - 1) / 3) - 1, checkingAttack, ax, ay)
end
end
end
end
detection.createHighlightAt = function (index, x, y, checkingAttack, ax, ay)
if checkingAttack == nil or checkingAttack == false then
highlights[index] = Highlight:create{x = x, y = y}
elseif x == ax and y == ay then
attacksSquare = true
end
end
--checks if any given square is actively being attacked by any piece on the board
--if white = true, check if any white piece attacks said square
detection.beingAttackedAt = function (x, y, white)
test = 0
if white then
arr = wpieces
else
arr = bpieces
end
for i, piece in ipairs(arr) do
if piece.dead == false then
--checks if square at x, y is being attacked by the currently indexed white piece
detection.createHighlights(piece, true, x, y)
end
if attacksSquare then
attacksSquare = false
--txt = txt .. " f: " .. test .. " t type: " .. piece.type .. "x: " .. x .. "y: " .. y
test = 0
return true
else
test = test + 1
end
end
return false
end
detection.isChecked = function ()
if whitesTurn then
if detection.beingAttackedAt(wpieces[16].x, wpieces[16].y, not whitesTurn) then
print("white is checked")
return true
end
else
if detection.beingAttackedAt(bpieces[16].x, bpieces[16].y, not whitesTurn) then
print("black is checked")
return true
end
end
return false
end
detection.piecePinned = function(piece)
piece.dead = true
if detection.isChecked() then
print("this piece is pinned!")
end
piece.dead = false
print("back to life")
--return outp
end
return detection