forked from scr-autopilot/scr-autopilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
259 lines (236 loc) · 9.01 KB
/
main.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import numpy as nm
import pytesseract
import re
import pydirectinput
import time
import cv2
import math
from PIL import ImageGrab
from PIL import ImageFilter
import sched
import sys
import tkinter
from tkinter import ttk
from throttle import *
import requests
import logging
from win32 import win32api
import ctypes
print("SCR-Autopilot v0.3-beta by MaTY (matyroblox01)")
print("Checking for updates...")
URL = "https://matyapi.matymt.repl.co/scr-autopilot/newest-version"
r = requests.get(url=URL)
data = r.json()
version = data['version']
if not version == "0.3":
print("Your version is outdated! Please install the latest release on https://github.com/MaTY-MT/scr-autopilot/releases")
else:
print("Your version is up-to-date.")
logging.basicConfig(filename='autopilot.log', filemode='w',
level=logging.DEBUG, format="[%(levelname)s] [%(asctime)s] %(message)s")
input("By using this software, you agree, that if the software makes a fault, you are always ready to take over. We are not responsible for any penalties given to your account! It is highly reccomended to use this software only on VIP servers, yet. This software is not an exploit (confirmed by the SCR staff team) and you can use it freely. Press ENTER to continue.")
display_size = ImageGrab.grab().size
logging.debug(f'Display resolution: {display_size[0]}, {display_size[1]}')
root = tkinter.Tk()
resolution = input("What is the resolution? (fhd, hd) > ")
if resolution == "fhd":
spd_pos = 884, 957, 947, 985
lim_pos = 889, 987, 942, 1016
green_pos = 1440, 983, 1441, 984
yellow_pos = 1438, 1016, 1439, 1017
double_yellow_pos = 1438, 950, 1439, 951
red_pos = 1438, 1045, 1439, 1046
distance_pos = 555, 1046, 605, 1070
awsbutton_pos = 1364, 960, 1365, 961
throttle_pos = 843, 931, 845, 1074
doors_pos = 870,822,871,823
loading_pos = 781,823,782,824
elif resolution == "hd":
print("The autopilot can be a little more buggy because of the HD resolution.")
time.sleep(1)
spd_pos = 573, 594, 630, 630
lim_pos = 569, 627, 618, 653
green_pos = 1118, 624, 1119, 625
yellow_pos = 1120, 654, 1121, 655
double_yellow_pos = 1120, 590, 1121, 591
red_pos = 1120, 688, 1121, 689
distance_pos = 239, 686, 284, 708
awsbutton_pos = 1047, 597, 1048, 598
throttle_pos = 522, 570, 525, 713
else:
print('Hmm, the resolution is not right... Try it again. Please type only "fhd" (without the quotation marks) if you have FHD monitor, or type "hd" (without the quotation marks) if you have HD monitor.')
input("Press ENTER to close the program.")
sys.exit()
max_speed = int(input(
"What is the maximum speed of your train in MPH? (E.g. 100, 125, 75 etc.) > "))
PROCESS_PER_MONITOR_DPI_AWARE = 2
MDT_EFFECTIVE_DPI = 0
def print_dpi():
shcore = ctypes.windll.shcore
monitors = win32api.EnumDisplayMonitors()
hresult = shcore.SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE)
assert hresult == 0
dpiX = ctypes.c_uint()
dpiY = ctypes.c_uint()
for i, monitor in enumerate(monitors):
shcore.GetDpiForMonitor(
monitor[0].handle,
MDT_EFFECTIVE_DPI,
ctypes.byref(dpiX),
ctypes.byref(dpiY)
)
logging.debug(
f"Monitor {i} = dpiX: {dpiX.value}, dpiY: {dpiY.value}"
)
print_dpi()
pytesseract.pytesseract.tesseract_cmd = './Tesseract-OCR/tesseract.exe'
active = False
time.sleep(1)
solve = None
def task():
global solve
im = ImageGrab.grab(bbox=(awsbutton_pos))
pix = im.load()
awsbutton_value = pix[0, 0] # Set the RGBA Value of the image (tuple)
if not awsbutton_value == (0, 0, 0):
pydirectinput.keyDown("q")
pydirectinput.keyUp("q")
print("AWSBUTTON:", "clicked")
logging.debug(f'AWS pixel RGB: {awsbutton_value}')
cap = ImageGrab.grab(bbox=(throttle_pos))
img = cap
count = 0
bottom_throttle_pixel = None
for y in range(img.height):
for x in range(img.width):
pixel = img.getpixel((x, y))
if y == img.height - 1:
bottom_throttle_pixel = pixel
if pixel == (0, 176, 85):
count += 1
currentThrottle = int(math.floor(100 * (count / 142)))
speed = currentThrottle/100 * max_speed
if currentThrottle == 0:
logging.debug(f'Throttle pixel RGB: {bottom_throttle_pixel}')
print("Current throttle: ", currentThrottle)
#print(count,"pixels are green.")
if currentThrottle == None:
print("I can't read the throttle.")
else:
# LIMIT
cap = ImageGrab.grab(bbox=(lim_pos))
cap = cap.filter(ImageFilter.MedianFilter())
cap = cv2.cvtColor(nm.array(cap), cv2.COLOR_RGB2GRAY)
tesstr = pytesseract.image_to_string(
cap,
config="--psm 7")
lim = 0
lim = [int(s) for s in re.findall(r'\b\d+\b', tesstr)]
if lim == []:
print("I can't read the limit.")
else:
templim = lim[0]
lim = lim[0]
lim = int(lim)
im = ImageGrab.grab(bbox=(red_pos))
pix = im.load()
# Set the RGBA Value of the image (tuple)
red_value = pix[0, 0]
im = ImageGrab.grab(bbox=(yellow_pos))
pix = im.load()
# Set the RGBA Value of the image (tuple)
yellow_value = pix[0, 0]
im = ImageGrab.grab(bbox=(green_pos))
pix = im.load()
# Set the RGBA Value of the image (tuple)
green_value = pix[0, 0]
im = ImageGrab.grab(bbox=(double_yellow_pos))
pix = im.load()
# Set the RGBA Value of the image (tuple)
double_yellow_value = pix[0, 0]
if red_value == (255, 0, 0):
print("AWS:", "red")
lim = 0
if yellow_value == (255, 190, 0):
print("AWS:", "yellow")
if templim > 45:
lim = 45
if double_yellow_value == (255, 190, 0):
print("AWS:", "double_yellow")
if templim > 75:
lim = 75
if green_value == (0, 255, 0):
print("AWS:", "green")
print("Limit: ", lim)
limitThrottle = int((lim / max_speed) * 100)
print("Limit throttle: ", limitThrottle)
cap = ImageGrab.grab(bbox=(distance_pos))
cap = cap.filter(ImageFilter.MedianFilter())
cap = cv2.cvtColor(nm.array(cap), cv2.COLOR_RGB2GRAY)
tesstr = pytesseract.image_to_string(
cap,
config="--psm 7")
distance = 0
distance = [int(s) for s in re.findall(r'\b\d+\b', tesstr)]
try:
m_distance = distance[0]
distance = distance[1]
if distance == 00 and m_distance == 0:
im = ImageGrab.grab(bbox=(loading_pos))
pix = im.load()
loading_value = pix[0, 0]
im = ImageGrab.grab(bbox=(doors_pos))
pix = im.load()
doors_value = pix[0, 0]
if doors_value == (255, 255, 255):
print("CLOSING DOORS")
pydirectinput.keyDown("t")
pydirectinput.keyUp("t")
time.sleep(4)
elif loading_value == (255, 255, 255):
print("LOADING")
else:
print("Autopilot is currently stopping.")
pydirectinput.keyDown("s")
time.sleep(5)
pydirectinput.keyUp("s")
pydirectinput.keyDown("t")
pydirectinput.keyUp("t")
elif distance <= 20 and m_distance == 0:
if lim >= 45:
print("Slowing down to prepare for station arrival.")
throttle(currentThrottle, int((42 / max_speed) * 100))
else:
throttle(currentThrottle, limitThrottle)
else:
throttle(currentThrottle, limitThrottle)
except IndexError:
pass
solve = root.after(600, task)
def onClick():
global active
if active == False:
active = True
button.configure(bg="green")
root.after(600, task)
else:
active = False
button.configure(bg="red")
root.after_cancel(solve)
photo = tkinter.PhotoImage(file="img/ap_icon.png")
root.title("button")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
w = 50
h = 50
x = 1599
y = 986
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.lift()
root.overrideredirect(True)
root.call('wm', 'attributes', '.', '-topmost', '1')
button = tkinter.Button(root, text="button1",
image=photo, bg="red", command=onClick)
button.grid(column=1, row=1, sticky=tkinter.E+tkinter.W)
root.grid_columnconfigure(2, weight=2)
root.mainloop()