Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. import cv2
  2. import ctypes
  3. import enum
  4. import numpy as np
  5. import os
  6. import pytesseract
  7. import time
  8. import win32gui
  9. import win32ui
  10. import win32con
  11. from PIL import Image
  12.  
  13. SendInput = ctypes.windll.user32.SendInput
  14.  
  15. KEY_SPACE = 0x39
  16. KEY_W = 0x11
  17. KEY_A = 0x1E
  18. KEY_S = 0x1F
  19. KEY_D = 0x20
  20. KEY_R = 0x13
  21.  
  22. PUL = ctypes.POINTER(ctypes.c_ulong)
  23. class KeyBdInput(ctypes.Structure):
  24. _fields_ = [("wVk", ctypes.c_ushort),
  25. ("wScan", ctypes.c_ushort),
  26. ("dwFlags", ctypes.c_ulong),
  27. ("time", ctypes.c_ulong),
  28. ("dwExtraInfo", PUL)]
  29.  
  30. class HardwareInput(ctypes.Structure):
  31. _fields_ = [("uMsg", ctypes.c_ulong),
  32. ("wParamL", ctypes.c_short),
  33. ("wParamH", ctypes.c_ushort)]
  34.  
  35. class MouseInput(ctypes.Structure):
  36. _fields_ = [("dx", ctypes.c_long),
  37. ("dy", ctypes.c_long),
  38. ("mouseData", ctypes.c_ulong),
  39. ("dwFlags", ctypes.c_ulong),
  40. ("time",ctypes.c_ulong),
  41. ("dwExtraInfo", PUL)]
  42.  
  43. class Input_I(ctypes.Union):
  44. _fields_ = [("ki", KeyBdInput),
  45. ("mi", MouseInput),
  46. ("hi", HardwareInput)]
  47.  
  48. class Input(ctypes.Structure):
  49. _fields_ = [("type", ctypes.c_ulong),
  50. ("ii", Input_I)]
  51.  
  52. def PressKey(hexKeyCode):
  53. extra = ctypes.c_ulong(0)
  54. ii_ = Input_I()
  55. ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
  56. x = Input( ctypes.c_ulong(1), ii_ )
  57. ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  58.  
  59. def ReleaseKey(hexKeyCode):
  60. extra = ctypes.c_ulong(0)
  61. ii_ = Input_I()
  62. ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
  63. x = Input( ctypes.c_ulong(1), ii_ )
  64. ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  65.  
  66. def KeyPress(key):
  67. time.sleep(0.1)
  68. PressKey(key)
  69. time.sleep(0.1)
  70. ReleaseKey(key)
  71.  
  72. def MouseClick(button):
  73. if button == MouseButton.LEFT:
  74. time.sleep(0.1)
  75. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)
  76. time.sleep(0.1)
  77. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)
  78. elif button == MouseButton.RIGHT:
  79. time.sleep(0.1)
  80. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0,0,0)
  81. time.sleep(0.1)
  82. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0,0,0)
  83.  
  84. class State(enum.Enum):
  85. START = 1
  86. FISHING = 2
  87. KEYS = 3
  88. CATCH = 4
  89. PICKUP = 5
  90. UNKNOWN = 6
  91. RESTART = 7
  92. END = 8
  93.  
  94. class MouseButton(enum.Enum):
  95. LEFT = 1
  96. RIGHT = 2
  97.  
  98. class Screenshot(object):
  99. def __init__(self, screen=None, image=None):
  100. self.screen = screen
  101. self.image = image
  102.  
  103. def takeScreenshot(self):
  104. try:
  105. left, top, right, bot = win32gui.GetWindowRect(hwnd)
  106. w = right - left
  107. h = bot - top
  108.  
  109. hwndDC = win32gui.GetWindowDC(hwnd)
  110. mfcDC = win32ui.CreateDCFromHandle(hwndDC)
  111. saveDC = mfcDC.CreateCompatibleDC()
  112.  
  113. saveBitMap = win32ui.CreateBitmap()
  114. saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
  115.  
  116. saveDC.SelectObject(saveBitMap)
  117. saveDC.BitBlt((0,0),(w, h),mfcDC,(0,0),win32con.SRCCOPY)
  118.  
  119. bmpinfo = saveBitMap.GetInfo()
  120. bmpstr = saveBitMap.GetBitmapBits(True)
  121.  
  122. self.screen = Image.frombuffer(
  123. 'RGB',
  124. (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
  125. bmpstr, 'raw', 'BGRX', 0, 1)
  126. finally:
  127. win32gui.DeleteObject(saveBitMap.GetHandle())
  128. saveDC.DeleteDC()
  129. mfcDC.DeleteDC()
  130. win32gui.ReleaseDC(hwnd, hwndDC)
  131.  
  132. def crop(self, area):
  133. img = self.screen.crop(area) # crop image from screenshot
  134. img = np.array(img) # convert to cv2 image
  135. self.image = img[:, :, ::-1].copy()
  136.  
  137. def convertToBinary(self):
  138. img_gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) # convert to grayscale
  139. self.image = cv2.threshold(img_gray,128,255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)[1]
  140.  
  141. def getText(self, config = ''):
  142. return pytesseract.image_to_string(self.image, config=config)
  143.  
  144. def getStartPos(self):
  145. for x in range(0, 50):
  146. for y in range(0, 50):
  147. r, g, b = self.image[x, y]
  148. color = int(r) + int(g) + int(b)
  149. if color_min < color and color < color_max:
  150. return (x+8, y+7)
  151. return (8, 7)
  152.  
  153. def getLetters(self):
  154. letters = []
  155. pos = self.getStartPos()
  156. img = self.image[pos[0]:pos[0]+y_len, pos[1]:pos[1]+x_len]
  157. for i in range(0, 10):
  158. x_pos = i*l_len + i*gap_len
  159. letter = img[:, x_pos:x_pos+l_len]
  160. letters.append(letter)
  161. self.image = cv2.hconcat(letters)
  162.  
  163. def GetWindow(title_name, exact = False):
  164. def enum_handler(hwnd, all_windows):
  165. all_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
  166. windows = []
  167. hwnds = []
  168. win32gui.EnumWindows(enum_handler, windows)
  169. if exact:
  170. hwnds = [hwnd for hwnd, title in windows if title_name == title.lower()]
  171. else:
  172. hwnds = [hwnd for hwnd, title in windows if title_name in title.lower()]
  173. return hwnds[0] if hwnds else None
  174.  
  175. def GetState():
  176. screen.takeScreenshot()
  177. screen.crop(areas['START'])
  178. screen.convertToBinary()
  179. if screen.getText().lower().startswith('start'):
  180. return State.START
  181. screen.crop(areas['FISHING'])
  182. screen.convertToBinary()
  183. if screen.getText().lower().startswith('fishing'):
  184. return State.FISHING
  185. screen.crop(areas['BITE'])
  186. screen.convertToBinary()
  187. if screen.getText().lower().startswith('bite'):
  188. return State.FISHING
  189. return State.UNKNOWN
  190.  
  191. def PressKeys(keys):
  192. for key in keys.upper():
  193. if (key == "W"):
  194. KeyPress(KEY_W)
  195. elif (key == "A"):
  196. KeyPress(KEY_A)
  197. elif (key == "S"):
  198. KeyPress(KEY_S)
  199. elif (key == "D"):
  200. KeyPress(KEY_D)
  201.  
  202. def Start():
  203. time.sleep(5)
  204. current_state = State.START
  205. state_loops = 0
  206. while current_state != State.END:
  207. if current_state == State.START:
  208. screen.takeScreenshot()
  209.  
  210. screen.crop(areas['START'])
  211. screen.convertToBinary()
  212. if screen.getText().lower().startswith('start'):
  213. print('START - Press space!')
  214. KeyPress(KEY_SPACE)
  215. current_state = State.FISHING
  216. state_loops = 0
  217. time.sleep(30)
  218. else:
  219. current_state = State.UNKNOWN
  220. elif current_state == State.FISHING:
  221. print('FISHING')
  222. screen.takeScreenshot()
  223.  
  224. screen.crop(areas['BITE'])
  225. screen.convertToBinary()
  226. if screen.getText().lower().startswith('bite'):
  227. print('BITE - Press space!')
  228. current_state = State.CATCH
  229. KeyPress(KEY_SPACE)
  230. time.sleep(1.4)
  231. continue
  232.  
  233. # Check if still fishing
  234. screen.crop(areas['FISHING'])
  235. screen.convertToBinary()
  236. if not screen.getText().lower().startswith('fishing'):
  237. current_state = State.UNKNOWN
  238. time.sleep(2)
  239. elif current_state == State.CATCH:
  240. KeyPress(KEY_SPACE)
  241. current_state = State.KEYS
  242. time.sleep(2)
  243. elif current_state == State.KEYS:
  244. screen.takeScreenshot()
  245. screen.crop(areas['KEYS'])
  246. screen.getLetters()
  247. # cv2.imwrite('keys.jpg', screen.image)
  248. keys = screen.getText(config)
  249. if keys and len(keys) > 1:
  250. print('KEYS: {0}'.format(keys.upper()))
  251. PressKeys(keys)
  252. current_state = State.PICKUP
  253. print('CATCHED - PICKUP FISH!')
  254. time.sleep(3)
  255. state_loops = state_loops + 1
  256. if state_loops > 5:
  257. current_state = State.UNKNOWN
  258. elif current_state == State.PICKUP:
  259. KeyPress(KEY_R)
  260. current_state = State.START
  261. time.sleep(2)
  262. elif current_state == State.UNKNOWN:
  263. state = GetState()
  264. print('UNKNOWN STATUS - retrieved {0}'.format(str(state)))
  265. if state_loops > 5:
  266. current_state = State.RESTART
  267. elif state != State.UNKNOWN:
  268. current_state = state
  269. state_loops = 0
  270. else:
  271. state_loops = state_loops + 1
  272. time.sleep(1)
  273. elif current_state == State.RESTART:
  274. # TAKE OUT FISHING ROD AND START AGAIN
  275. print('RESTART!')
  276. MouseClick(MouseButton.RIGHT)
  277. current_state = State.START
  278. time.sleep(5)
  279. else:
  280. # END
  281. print('DEFAULT STATUS = END')
  282. MouseClick(MouseButton.RIGHT)
  283. current_state = State.END
  284.  
  285. # begin of script
  286. pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract'
  287. config = '-c tessedit_char_whitelist=WASD --psm 13'
  288. color_min = 150
  289. color_max = 200
  290. x_len = 355
  291. y_len = 22
  292. l_len = 18
  293. gap_len = 19
  294. areas = {
  295. 'START': (1098, 52, 1160, 82),
  296. 'FISHING': (830, 52, 912, 85),
  297. 'BITE': (842, 52, 891, 82),
  298. 'KEYS': (750, 330, 1160, 430),
  299. }
  300. hwnd = GetWindow('black desert')
  301. screen = Screenshot()
  302.  
  303. if hwnd:
  304. Start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement