Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- import cv2
- import numpy as np
- import time
- def getWindowID():
- #start = time.time()
- #get a list of all the windows currently running
- windows = subprocess.run("""for i in `xprop -root|grep "_NET_CLIENT_LIST_STACKING(WINDOW): window id" |tr '#' ','|tr ',' '\n'| grep 0x`;do xwininfo -id $i|grep "Window id" ;done""", stdout = subprocess.PIPE, shell=True)
- windowList = windows.stdout.decode("utf-8").strip().split("\n")
- runeIDList = []
- for window in windowList:
- windowInfo = window.split(" ") #List should look like ["xwininfo:", "window", "id:", ID, WindowName]
- windowID = windowInfo[3]
- windowName = windowInfo[4]
- if '"RuneScape"' in windowName:
- runeIDList.append(windowID)
- if len(runeIDList) < 1: #No runescape window found
- return False
- #Due to Runescape on Linux not closing the splash screen, we need to eliminate the splash screen windowID
- #We do this by assuming the rs window will have a larger width than the splash screen
- #Impossible to resize the rs window to smaller than the splash screen so this shouldn't break
- correctID = 0
- width = 0
- for id in runeIDList:
- xwininfo= subprocess.run("xwininfo -metric -id "+id, stdout=subprocess.PIPE, shell=True)
- windowInfo = xwininfo.stdout.decode("utf-8")
- currWidth = windowInfo.partition("Width: ")[2].split(" ")[0]
- currWidth = int(currWidth)
- if width < currWidth:
- width = currWidth
- correctID = id
- #print("getWindowID: {:.3}s".format(time.time() - start))
- return correctID
- def captureScreen(windowID):
- #start = time.time()
- screenshot = subprocess.run("import -window "+windowID+" ppm:-", stdout=subprocess.PIPE, shell=True).stdout
- nparr = np.fromstring(screenshot, np.uint8)
- screenColour = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
- screenGray = cv2.cvtColor(screenColour, cv2.COLOR_BGR2GRAY)
- #print("captureScreen: {:.3}s".format(time.time() - start))
- return screenColour, screenGray
- def match(screenGray, template):
- start = time.time()
- w, h = template.shape[::-1]
- res = cv2.matchTemplate(screenGray, template, cv2.TM_CCOEFF_NORMED)
- threshold = 0.65
- loc = np.where(res >= threshold)
- if len(loc[0]) < 1:
- #print("Template No Match: {:.3}s".format(time.time() - start))
- return []
- else:
- #print("Template Match: {:.3}s".format(time.time() - start))
- return res
- def getMatchLoopState(screenGray):
- #start = time.time()
- loginTemplate = cv2.imread("loginTemplate.png", 0)
- lobbyTemplate = cv2.imread("lobbyTemplate.png", 0)
- statbarTemplate = cv2.imread("statbarTemplate.png", 0)
- stateTemplates = [loginTemplate, lobbyTemplate, statbarTemplate][::-1]
- states = ["Login Screen", "Game Lobby", "Game Screen"][::-1]
- for i in range(len(states)):
- state = states[i]
- res = match(screenGray, stateTemplates[i])
- if len(res) > 0:
- #print("Get State: {:.3}s".format(time.time() - start))
- return state, res
- break
- else:
- pass
- return "Something went wrong", "-"
- def writeMatchCrop(screenGray, res, w, h):
- #start = time.time()
- coords = cv2.minMaxLoc(res)[-1]
- statsCrop = screenGray[coords[1]:coords[1]+h, coords[0]:coords[0]+w]
- hpCrop = statsCrop[6:6+12, 35:35+83]
- prayCrop = statsCrop[6:6+12, 273:273+60]
- factor = 3
- hpRescale = cv2.resize(hpCrop, (0,0), fx=factor, fy=factor)
- prayRescale = cv2.resize(prayCrop, (0,0), fx=factor, fy=factor)
- cv2.imwrite("hpText.png", hpRescale)
- cv2.imwrite("prayText.png", prayRescale)
- #print("Write Match Crop: {:.3}s".format(time.time() - start))
- def readText(tempFileName):
- #start = time.time()
- text = subprocess.run("tesseract "+tempFileName+".png stdout -l rune", stdout=subprocess.PIPE, shell=True).stdout
- text = text.decode("utf-8").strip()
- #print("Read "+tempFileName+": {:.3}s".format(time.time() - start))
- return text
- def main():
- while True:
- start = time.time()
- rsWindowID = getWindowID()
- if not rsWindowID:
- print("No Runescape Window"+"\n---", file = open("gameState.txt", "w"))
- #pass
- else:
- screenColour, screenGray = captureScreen(rsWindowID)
- state, res = getMatchLoopState(screenGray)
- if state == "Game Screen":
- template = cv2.imread("statbarTemplate.png", 0)
- w, h = template.shape[::-1]
- writeMatchCrop(screenGray, res, w, h)
- hpText = readText("hpText")
- prayText = readText("prayText")
- print("HP: "+hpText+" Prayer: "+prayText+"\n---", file = open("gameState.txt", "w"))
- print("HP: "+hpText+" .Prayer: "+prayText)
- else:
- if state == "Something went wrong":
- print(state+"\n---", file = open("gameState.txt", "w"))
- #print(state)
- else:
- print(state+"\n---", file = open("gameState.txt", "w"))
- #print(state)
- print("\n--Loop completed in: {:.3}s".format(time.time() - start), file = open("gameState.txt", "a"))
- time.sleep(1.5)
- main()
Add Comment
Please, Sign In to add comment