-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpuzzle.py
More file actions
326 lines (244 loc) · 10.7 KB
/
puzzle.py
File metadata and controls
326 lines (244 loc) · 10.7 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import numpy as np
import pydirectinput
import cv2 as cv
import math
from time import time
from windowcapture import WindowCapture
from tetris import Tetris
from piece import Piece
import json
import constants
fish_jigsaw_chest = cv.imread("images/fish_jigsaw_chest.png")
class PuzzleBot:
#properties
botting = False
PUZZLE_WINDOW_SIZE = (260, 170)
PUZZLE_WINDOW_POSITION = (270, 227)
PUZZLE_GET_NEW_PIECE = (230, 85)
PUZZLE_COMFIRM = (100, 90)
PUZZLE_GET_NEW_PIECE_COLOR = (110, 150)
wincap = None
tetris = Tetris()
timer_action = time()
get_piece_time = 2
new_piece = None
state = 0
end = False
dictdump = None
def set_to_begin(self, values):
self.wincap = WindowCapture(constants.GAME_NAME)
self.state = 0
with open('pieces_second.json') as handle:
self.dictdump = json.loads(handle.read())
def set_puzzle_state(self, crop_img):
paint_c = 32
board = [[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0]]
for i in range(0, 4):
for j in range(0, 6):
if crop_img[15 + paint_c*i, 15 + paint_c*j, 0] < 50 and crop_img[15 + paint_c*i, 15 + paint_c*j, 1] < 50 and crop_img[15 + paint_c*i, 15 + paint_c*j, 2] < 50:
board[i][j] = 0
else:
board[i][j] = 1
cv.rectangle(crop_img, (15 + paint_c*j, 15 + paint_c*i), (15 + paint_c*j, 15 + paint_c*i),
color=(0, 255, 255), thickness=4, lineType=cv.LINE_4)
self.tetris.board = board
if self.tetris.count_zeros == 0:
self.tetris.first = 0
self.tetris.second = 0
else:
self.tetris.first = 1
self.tetris.second = 1
def get_image(self):
screenshot = self.wincap.get_screenshot()
crop_img = screenshot[self.PUZZLE_WINDOW_POSITION[1]:self.PUZZLE_WINDOW_POSITION[1]+self.PUZZLE_WINDOW_SIZE[1],
self.PUZZLE_WINDOW_POSITION[0]:self.PUZZLE_WINDOW_POSITION[0]+self.PUZZLE_WINDOW_SIZE[0]]
return crop_img
def press_comfirm(self):
mouse_x = int(self.PUZZLE_COMFIRM[0] + self.PUZZLE_WINDOW_POSITION[0] + self.wincap.offset_x)
mouse_y = int(self.PUZZLE_COMFIRM[1] + self.PUZZLE_WINDOW_POSITION[1] + self.wincap.offset_y)
pydirectinput.click(x=mouse_x, y=mouse_y, button='left')
def press_comfirm_cake(self):
mouse_x = int(self.PUZZLE_COMFIRM[0] + 20 + self.PUZZLE_WINDOW_POSITION[0] + self.wincap.offset_x)
mouse_y = int(self.PUZZLE_COMFIRM[1] + self.PUZZLE_WINDOW_POSITION[1] + self.wincap.offset_y)
pydirectinput.click(x=mouse_x, y=mouse_y, button='left')
def throw_pice(self):
mouse_x = int(self.PUZZLE_COMFIRM[0] + self.PUZZLE_WINDOW_POSITION[0] + self.wincap.offset_x)
mouse_y = int(self.PUZZLE_COMFIRM[1] + self.PUZZLE_WINDOW_POSITION[1] + self.wincap.offset_y)
pydirectinput.click(x=mouse_x, y=mouse_y, button='right')
def get_new_piece_color(self, crop_image):
x = int(self.PUZZLE_GET_NEW_PIECE_COLOR[0])
y = int(self.PUZZLE_GET_NEW_PIECE_COLOR[1])
if (crop_image[y, x, 0] > 35 and crop_image[y, x, 0] < 40 and
crop_image[y, x, 1] > 60 and crop_image[y, x, 1] < 70 and
crop_image[y, x, 2] > 240 and crop_image[y, x, 2] < 260):
return 4
elif (crop_image[y, x, 0] > 20 and crop_image[y, x, 0] < 30 and
crop_image[y, x, 1] > 150 and crop_image[y, x, 1] < 170 and
crop_image[y, x, 2] > 240 and crop_image[y, x, 2] < 260):
return 1
elif (crop_image[y, x, 0] > 35 and crop_image[y, x, 0] < 50 and
crop_image[y, x, 1] > 240 and crop_image[y, x, 1] < 260 and
crop_image[y, x, 2] > 35 and crop_image[y, x, 2] < 50):
return 5
elif (crop_image[y, x, 0] > 240 and crop_image[y, x, 0] < 260 and
crop_image[y, x, 1] > 240 and crop_image[y, x, 1] < 260 and
crop_image[y, x, 2] > 20 and crop_image[y, x, 2] < 30):
return 3
elif (crop_image[y, x, 0] > 240 and crop_image[y, x, 0] < 260 and
crop_image[y, x, 1] > 100 and crop_image[y, x, 1] < 115 and
crop_image[y, x, 2] > -10 and crop_image[y, x, 2] < 10):
return 2
elif (crop_image[y, x, 0] > 50 and crop_image[y, x, 0] < 60 and
crop_image[y, x, 1] > 235 and crop_image[y, x, 1] < 255 and
crop_image[y, x, 2] > 250 and crop_image[y, x, 2] < 260):
return 6
def detect_end_game(self, crop_img):
x = int(self.PUZZLE_GET_NEW_PIECE[0])
y = int(self.PUZZLE_GET_NEW_PIECE[1])
if crop_img[y, x, 0] > 100 and crop_img[y, x, 1] > 150 and crop_img[y, x, 2] > 150:
return False
else:
return True
def play_game(self):
piece = Piece(self.new_piece)
decision, pos = self.tetris.find_first(piece, self.dictdump)
paint_c = 32
if decision == 1:
self.tetris.insert_piece(pos[0], pos[1], piece)
if self.tetris.verify_end():
self.end = True
mouse_x = 15 + paint_c*pos[1] + self.PUZZLE_WINDOW_POSITION[0] + self.wincap.offset_x
mouse_y = 15 + paint_c*pos[0] + self.PUZZLE_WINDOW_POSITION[1] + self.wincap.offset_y
pydirectinput.click(mouse_x, mouse_y)
return None
if decision == 2:
return None
possibilites = self.tetris.find_possibles(piece)
pices_count = 0
for i in range(1,7):
if i != piece.piece_type:
possis = self.tetris.find_possibles(Piece(i))
if len(possis):
pices_count += 1
if piece.piece_type == 1 and pices_count != 0:
possibilites = [i for i in possibilites if self.tetris.verify_isolated(i[0], i[1])]
if len(possibilites):
a = self.tetris.choose_better(piece, possibilites)
self.tetris.insert_piece(a[0], a[1], piece)
if self.tetris.verify_end():
self.end = True
mouse_x = 15 + paint_c*a[1] + self.PUZZLE_WINDOW_POSITION[0] + self.wincap.offset_x
mouse_y = 15 + paint_c*a[0] + self.PUZZLE_WINDOW_POSITION[1] + self.wincap.offset_y
pydirectinput.click(mouse_x, mouse_y)
return True
return None
def try_to_put_chest(self):
screenshot = self.wincap.get_screenshot()
result = cv.matchTemplate(screenshot, fish_jigsaw_chest, cv.TM_CCOEFF_NORMED)
threshold = 0.7
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
# cv.rectangle(
# screenshot,
# max_loc,
# (max_loc[0] + fish_jigsaw_chest.shape[1], max_loc[1] + fish_jigsaw_chest.shape[0]),
# (0, 255, 255),
# 2,
# )
# cv.imshow("result", screenshot)
# cv.waitKey(0)
if max_val < threshold:
return False
mouse_x = int(
max_loc[0] + fish_jigsaw_chest.shape[1] / 2 + self.wincap.offset_x
)
mouse_y = int(
max_loc[1] + fish_jigsaw_chest.shape[0] / 2 + self.wincap.offset_y
)
# click the chest
pydirectinput.click(x=mouse_x, y=mouse_y, button="left")
mouse_x = int(
self.PUZZLE_GET_NEW_PIECE[0]
+ self.PUZZLE_WINDOW_POSITION[0]
+ self.wincap.offset_x
)
mouse_y = int(
self.PUZZLE_GET_NEW_PIECE[1]
+ self.PUZZLE_WINDOW_POSITION[1]
+ self.wincap.offset_y
)
# click the place where the piece will be
pydirectinput.click(x=mouse_x, y=mouse_y, button="left")
# click the board
pydirectinput.click(
self.wincap.offset_x + self.PUZZLE_WINDOW_POSITION[0],
self.wincap.offset_y + self.PUZZLE_WINDOW_POSITION[1],
button="left",
)
return True
def runHack(self):
crop_image = self.get_image()
timep = 0.2
if self.state == 0:
mouse_x = int(self.PUZZLE_GET_NEW_PIECE[0] + self.PUZZLE_WINDOW_POSITION[0] + self.wincap.offset_x)
mouse_y = int(self.PUZZLE_GET_NEW_PIECE[1] + self.PUZZLE_WINDOW_POSITION[1] + self.wincap.offset_y)
if time() - self.timer_action > timep:
if self.detect_end_game(crop_image):
if not self.try_to_put_chest():
self.botting = False
return None
pydirectinput.click(x=mouse_x, y=mouse_y, button='left')
self.state = 1
self.timer_action = time()
if self.state == 1:
if time() - self.timer_action > timep:
self.press_comfirm()
self.state = 2
self.timer_action = time()
if self.state == 2:
mouse_x = int(self.PUZZLE_GET_NEW_PIECE_COLOR[0] + self.PUZZLE_WINDOW_POSITION[0] + self.wincap.offset_x)
mouse_y = int(self.PUZZLE_GET_NEW_PIECE_COLOR[1] + self.PUZZLE_WINDOW_POSITION[1] + self.wincap.offset_y)
if time() - self.timer_action > timep:
self.state = 4
self.timer_action = time()
pydirectinput.moveTo(mouse_x, mouse_y)
if self.state == 4:
if time() - self.timer_action > timep:
self.state = 5
self.timer_action = time()
self.new_piece = self.get_new_piece_color(crop_image)
if self.state == 5:
if time() - self.timer_action > timep:
self.timer_action = time()
self.set_puzzle_state(crop_image)
if self.play_game():
self.state = 6
else:
self.state = 7
if self.state == 6:
if time() - self.timer_action > timep:
self.press_comfirm()
self.timer_action = time()
if self.end:
self.state = 9
else:
self.state = 0
if self.state == 7:
if time() - self.timer_action > timep:
self.throw_pice()
self.timer_action = time()
self.state = 8
if self.state == 8:
if time() - self.timer_action > timep:
self.press_comfirm()
self.timer_action = time()
self.state = 0
if self.state == 9:
if time() - self.timer_action > 2:
self.end = False
self.press_comfirm_cake()
self.timer_action = time()
self.state = 0
return None