-
Notifications
You must be signed in to change notification settings - Fork 0
/
mienfield-bot.py
executable file
·237 lines (213 loc) · 6.87 KB
/
mienfield-bot.py
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
#! /usr/bin/env python
import shlex
import subprocess
import sys
import time
from PIL import Image
from PIL import ImageDraw
def wait_for_user(howlong):
for i in range(howlong, 0, -1):
print(i)
time.sleep(1)
print('go!')
def run(command):
splitcommand = shlex.split(command)
result = subprocess.run(splitcommand,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
return result
def make_screenshot(screenshotfilename = 'screenshot.png'):
result = run('scrot {}'.format(screenshotfilename))
if result.returncode:
raise Exception('scrot failed')
return screenshotfilename
def visgrep(where, what):
result = run('visgrep {} {}'.format(where, what))
if not result.stdout:
raise Exception('visgrep {} {} failed'.format(where, what))
allcoords = []
for output in result.stdout.splitlines():
coords = output.split()[0]
x, y = coords.split(',')
intx = int(x)
inty = int(y)
allcoords.append((intx, inty))
return allcoords
def click(x, y, what):
result = run('xte "mousemove {} {}"'.format(x, y))
if result.returncode:
raise Exception('xte mousemove failed')
result = run('xte "mouseclick {}"'.format(what))
if result.returncode:
raise Exception('xte mouseclick failed')
def determine_borders(screenshot):
[(rbx, _)] = visgrep(screenshot, 'images/sidebar.png')
[(_, rby)] = visgrep(screenshot, 'images/teleport.png')
dx, mx = divmod(rbx, 32)
dy, my = divmod(rby, 32)
bx = rbx - mx
by = rby - my
return dx, dy, bx, by
def convertcrop(screenshot, w, h):
croppedfilename = 'screenshotcropped.png'
result = run('convert {} -crop {}x{}+0+1 +repage {}'.format(
screenshot, w, h, croppedfilename))
if result.returncode:
raise Exception('convert crop failed')
return croppedfilename
class MienField():
def __init__(self, width, height, cells):
self.width = width
self.height = height
self.cells = cells
class Cell():
def __init__(self, cellx, celly, pixelx, pixely, what):
self.cellx = cellx
self.celly = celly
self.pixelx = pixelx
self.pixely = pixely
self.what = what
def parse_mienfield_for_what(screenshot, what):
try:
poses = visgrep(screenshot, 'images/{}.png'.format(what))
except Exception:
return {}
mienfield_what = {}
for rx, ry in poses:
cx = rx // 32
cy = ry // 32
mienfield_what[(cx, cy)] = Cell(cx, cy, rx, ry, what)
return mienfield_what
def complete_field(mienfield, dx, dy):
for x in range(dx+1):
for y in range(dy+1):
if (x, y) not in mienfield:
px = x * 32
py = y * 32
mienfield[(x, y)] = Cell(x, y, px, py, 'flag?')
def parse_mienfield(screenshot):
dx, dy, bx, by = determine_borders(screenshot)
screenshotcropped = convertcrop(screenshot, bx, by)
mienfield = {}
for what in ['1', '2', '3', '4', '5', '6', '7', 'closed', 'open', 'mine']:
print('looking for {}'.format(what))
mienfield_what = parse_mienfield_for_what(screenshotcropped, what)
mienfield.update(mienfield_what)
complete_field(mienfield, dx, dy)
mienfield = MienField(dx, dy, mienfield)
return mienfield
def mark_borders(mienfield):
for cell in mienfield.cells.values():
if cell.cellx in (0, mienfield.width) or cell.celly in (0, mienfield.height):
cell.border = True
else:
cell.border = False
def get_neighbours(mienfield, cx, cy):
neighbours = []
neighbours.append(mienfield.cells[(cx , cy-1)])
neighbours.append(mienfield.cells[(cx+1, cy-1)])
neighbours.append(mienfield.cells[(cx+1, cy )])
neighbours.append(mienfield.cells[(cx+1, cy+1)])
neighbours.append(mienfield.cells[(cx , cy+1)])
neighbours.append(mienfield.cells[(cx-1, cy+1)])
neighbours.append(mienfield.cells[(cx-1, cy )])
neighbours.append(mienfield.cells[(cx-1, cy-1)])
return neighbours
def count_neighbours(cell, neighbours):
openlist = []
closedlist = []
flaglist = []
for n in neighbours:
if n.what in ('1', '2', '3', '4', '5', '6', '7', 'open'):
openlist.append(n)
elif n.what == 'closed':
closedlist.append(n)
elif n.what in ('mine', 'flag?'):
flaglist.append(n)
return openlist, closedlist, flaglist
def classify_cells(mienfield):
boring = []
interesting = []
clickopen = []
clickflag = []
for cell in mienfield.cells.values():
if not cell.border:
if cell.what in ('1', '2', '3', '4', '5', '6', '7'):
if len(cell.closedlist) > 0:
if int(cell.what) - len(cell.flaglist) == len(cell.closedlist):
clickflag.append(cell)
elif int(cell.what) == len(cell.flaglist):
clickopen.append(cell)
else:
interesting.append(cell)
else:
boring.append(cell)
elif cell.what == 'open':
boring.append(cell)
elif cell.what == 'closed':
if len(cell.openlist) > 0:
interesting.append(cell)
else:
boring.append(cell)
elif cell.what in ('mine', 'flag?'):
boring.append(cell)
return boring, interesting, clickopen, clickflag
def mark_neighbours(mienfield):
for cell in mienfield.cells.values():
if not cell.border:
neighbours = get_neighbours(mienfield, cell.cellx, cell.celly)
openlist, closedlist, flaglist = count_neighbours(cell, neighbours)
cell.neighbours = neighbours
cell.openlist = openlist
cell.closedlist = closedlist
cell.flaglist = flaglist
def visualize(boring, interesting, clickopen, clickflag):
image = Image.open('screenshot.png')
draw = ImageDraw.Draw(image, 'RGBA')
for cell in boring:
x = cell.pixelx
y = cell.pixely
draw.rectangle([(x, y), (x+30, y+30)], (0, 0, 0, 127))
for cell in clickopen:
x = cell.pixelx
y = cell.pixely
draw.rectangle([(x, y), (x+30, y+30)], (127, 0, 0, 127))
for cell in clickflag:
x = cell.pixelx
y = cell.pixely
draw.rectangle([(x, y), (x+30, y+30)], (0, 127, 0, 127))
image.save('screenshotclick.png')
def count(mienfield):
print('counting mines and flags')
mark_borders(mienfield)
mark_neighbours(mienfield)
boring, interesting, clickopen, clickflag = classify_cells(mienfield)
visualize(boring, interesting, clickopen, clickflag)
mienfield.boring = boring
mienfield.interesting = interesting
mienfield.clickopen = clickopen
mienfield.clickflag = clickflag
def do_the_clicks(mienfield):
print('clicking')
for cell in mienfield.clickopen:
time.sleep(0.2)
click(cell.pixelx + 10, cell.pixely + 10, 1)
for cell in mienfield.clickflag:
time.sleep(0.2)
click(cell.pixelx + 10, cell.pixely + 10, 3)
def main():
print('put mienfield fullscreen')
delay = 3
if len(sys.argv) > 1:
delay = int(sys.argv[1])
wait_for_user(delay)
screenshot = make_screenshot()
mienfield = parse_mienfield(screenshot)
count(mienfield)
do_the_clicks(mienfield)
print('waiting')
time.sleep(1)
make_screenshot('screenshotafter.png')
if __name__ == '__main__':
main()