Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import ctypes
- import enum
- import numpy as np
- import os
- import pytesseract
- import time
- import win32gui
- import win32ui
- import win32con
- from PIL import Image
- SendInput = ctypes.windll.user32.SendInput
- KEY_SPACE = 0x39
- KEY_W = 0x11
- KEY_A = 0x1E
- KEY_S = 0x1F
- KEY_D = 0x20
- KEY_R = 0x13
- PUL = ctypes.POINTER(ctypes.c_ulong)
- class KeyBdInput(ctypes.Structure):
- _fields_ = [("wVk", ctypes.c_ushort),
- ("wScan", ctypes.c_ushort),
- ("dwFlags", ctypes.c_ulong),
- ("time", ctypes.c_ulong),
- ("dwExtraInfo", PUL)]
- class HardwareInput(ctypes.Structure):
- _fields_ = [("uMsg", ctypes.c_ulong),
- ("wParamL", ctypes.c_short),
- ("wParamH", ctypes.c_ushort)]
- class MouseInput(ctypes.Structure):
- _fields_ = [("dx", ctypes.c_long),
- ("dy", ctypes.c_long),
- ("mouseData", ctypes.c_ulong),
- ("dwFlags", ctypes.c_ulong),
- ("time",ctypes.c_ulong),
- ("dwExtraInfo", PUL)]
- class Input_I(ctypes.Union):
- _fields_ = [("ki", KeyBdInput),
- ("mi", MouseInput),
- ("hi", HardwareInput)]
- class Input(ctypes.Structure):
- _fields_ = [("type", ctypes.c_ulong),
- ("ii", Input_I)]
- def PressKey(hexKeyCode):
- extra = ctypes.c_ulong(0)
- ii_ = Input_I()
- ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
- x = Input( ctypes.c_ulong(1), ii_ )
- ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
- def ReleaseKey(hexKeyCode):
- extra = ctypes.c_ulong(0)
- ii_ = Input_I()
- ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
- x = Input( ctypes.c_ulong(1), ii_ )
- ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
- def KeyPress(key):
- time.sleep(0.1)
- PressKey(key)
- time.sleep(0.1)
- ReleaseKey(key)
- def MouseClick(button):
- if button == MouseButton.LEFT:
- time.sleep(0.1)
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0)
- time.sleep(0.1)
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0)
- elif button == MouseButton.RIGHT:
- time.sleep(0.1)
- win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0,0,0)
- time.sleep(0.1)
- win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0,0,0)
- class State(enum.Enum):
- START = 1
- FISHING = 2
- KEYS = 3
- CATCH = 4
- PICKUP = 5
- UNKNOWN = 6
- RESTART = 7
- END = 8
- class MouseButton(enum.Enum):
- LEFT = 1
- RIGHT = 2
- class Screenshot(object):
- def __init__(self, screen=None, image=None):
- self.screen = screen
- self.image = image
- def takeScreenshot(self):
- try:
- left, top, right, bot = win32gui.GetWindowRect(hwnd)
- w = right - left
- h = bot - top
- hwndDC = win32gui.GetWindowDC(hwnd)
- mfcDC = win32ui.CreateDCFromHandle(hwndDC)
- saveDC = mfcDC.CreateCompatibleDC()
- saveBitMap = win32ui.CreateBitmap()
- saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
- saveDC.SelectObject(saveBitMap)
- saveDC.BitBlt((0,0),(w, h),mfcDC,(0,0),win32con.SRCCOPY)
- bmpinfo = saveBitMap.GetInfo()
- bmpstr = saveBitMap.GetBitmapBits(True)
- self.screen = Image.frombuffer(
- 'RGB',
- (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
- bmpstr, 'raw', 'BGRX', 0, 1)
- finally:
- win32gui.DeleteObject(saveBitMap.GetHandle())
- saveDC.DeleteDC()
- mfcDC.DeleteDC()
- win32gui.ReleaseDC(hwnd, hwndDC)
- def crop(self, area):
- img = self.screen.crop(area) # crop image from screenshot
- img = np.array(img) # convert to cv2 image
- self.image = img[:, :, ::-1].copy()
- def convertToBinary(self):
- img_gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) # convert to grayscale
- self.image = cv2.threshold(img_gray,128,255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)[1]
- def getText(self, config = ''):
- return pytesseract.image_to_string(self.image, config=config)
- def getStartPos(self):
- for x in range(0, 50):
- for y in range(0, 50):
- r, g, b = self.image[x, y]
- color = int(r) + int(g) + int(b)
- if color_min < color and color < color_max:
- return (x+8, y+7)
- return (8, 7)
- def getLetters(self):
- letters = []
- pos = self.getStartPos()
- img = self.image[pos[0]:pos[0]+y_len, pos[1]:pos[1]+x_len]
- for i in range(0, 10):
- x_pos = i*l_len + i*gap_len
- letter = img[:, x_pos:x_pos+l_len]
- letters.append(letter)
- self.image = cv2.hconcat(letters)
- def GetWindow(title_name, exact = False):
- def enum_handler(hwnd, all_windows):
- all_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
- windows = []
- hwnds = []
- win32gui.EnumWindows(enum_handler, windows)
- if exact:
- hwnds = [hwnd for hwnd, title in windows if title_name == title.lower()]
- else:
- hwnds = [hwnd for hwnd, title in windows if title_name in title.lower()]
- return hwnds[0] if hwnds else None
- def GetState():
- screen.takeScreenshot()
- screen.crop(areas['START'])
- screen.convertToBinary()
- if screen.getText().lower().startswith('start'):
- return State.START
- screen.crop(areas['FISHING'])
- screen.convertToBinary()
- if screen.getText().lower().startswith('fishing'):
- return State.FISHING
- screen.crop(areas['BITE'])
- screen.convertToBinary()
- if screen.getText().lower().startswith('bite'):
- return State.FISHING
- return State.UNKNOWN
- def PressKeys(keys):
- for key in keys.upper():
- if (key == "W"):
- KeyPress(KEY_W)
- elif (key == "A"):
- KeyPress(KEY_A)
- elif (key == "S"):
- KeyPress(KEY_S)
- elif (key == "D"):
- KeyPress(KEY_D)
- def Start():
- time.sleep(5)
- current_state = State.START
- state_loops = 0
- while current_state != State.END:
- if current_state == State.START:
- screen.takeScreenshot()
- screen.crop(areas['START'])
- screen.convertToBinary()
- if screen.getText().lower().startswith('start'):
- print('START - Press space!')
- KeyPress(KEY_SPACE)
- current_state = State.FISHING
- state_loops = 0
- time.sleep(30)
- else:
- current_state = State.UNKNOWN
- elif current_state == State.FISHING:
- print('FISHING')
- screen.takeScreenshot()
- screen.crop(areas['BITE'])
- screen.convertToBinary()
- if screen.getText().lower().startswith('bite'):
- print('BITE - Press space!')
- current_state = State.CATCH
- KeyPress(KEY_SPACE)
- time.sleep(1.4)
- continue
- # Check if still fishing
- screen.crop(areas['FISHING'])
- screen.convertToBinary()
- if not screen.getText().lower().startswith('fishing'):
- current_state = State.UNKNOWN
- time.sleep(2)
- elif current_state == State.CATCH:
- KeyPress(KEY_SPACE)
- current_state = State.KEYS
- time.sleep(2)
- elif current_state == State.KEYS:
- screen.takeScreenshot()
- screen.crop(areas['KEYS'])
- screen.getLetters()
- # cv2.imwrite('keys.jpg', screen.image)
- keys = screen.getText(config)
- if keys and len(keys) > 1:
- print('KEYS: {0}'.format(keys.upper()))
- PressKeys(keys)
- current_state = State.PICKUP
- print('CATCHED - PICKUP FISH!')
- time.sleep(3)
- state_loops = state_loops + 1
- if state_loops > 5:
- current_state = State.UNKNOWN
- elif current_state == State.PICKUP:
- KeyPress(KEY_R)
- current_state = State.START
- time.sleep(2)
- elif current_state == State.UNKNOWN:
- state = GetState()
- print('UNKNOWN STATUS - retrieved {0}'.format(str(state)))
- if state_loops > 5:
- current_state = State.RESTART
- elif state != State.UNKNOWN:
- current_state = state
- state_loops = 0
- else:
- state_loops = state_loops + 1
- time.sleep(1)
- elif current_state == State.RESTART:
- # TAKE OUT FISHING ROD AND START AGAIN
- print('RESTART!')
- MouseClick(MouseButton.RIGHT)
- current_state = State.START
- time.sleep(5)
- else:
- # END
- print('DEFAULT STATUS = END')
- MouseClick(MouseButton.RIGHT)
- current_state = State.END
- # begin of script
- pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract'
- config = '-c tessedit_char_whitelist=WASD --psm 13'
- color_min = 150
- color_max = 200
- x_len = 355
- y_len = 22
- l_len = 18
- gap_len = 19
- areas = {
- 'START': (1098, 52, 1160, 82),
- 'FISHING': (830, 52, 912, 85),
- 'BITE': (842, 52, 891, 82),
- 'KEYS': (750, 330, 1160, 430),
- }
- hwnd = GetWindow('black desert')
- screen = Screenshot()
- if hwnd:
- Start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement