import cv2 as cv import numpy as np import pydirectinput from utils import mse from game_base_class import GameBase import random GREEN = 1 YELLOW = 2 RED = 3 BLUE = 4 ORANGE = 5 class MentiWords(GameBase): def __init__(self, overlay): super().__init__(overlay) self.observation = np.zeros((9, 9), dtype=int) self.colors = [1, 2, 3, 4, 5, 6, 7, 8, 9] self.offset_left = 1080 self.offset_down = 870 self.menti_play_button = cv.imread("control_elements/play.jpg", cv.IMREAD_COLOR) self.needles = { 'A': cv.imread("menti_words/a.jpg", cv.IMREAD_COLOR), 'B': cv.imread("menti_words/b.jpg", cv.IMREAD_COLOR), 'C': cv.imread("menti_words/c.jpg", cv.IMREAD_COLOR), 'D': cv.imread("menti_words/d.jpg", cv.IMREAD_COLOR), 'E': cv.imread("menti_words/e.jpg", cv.IMREAD_COLOR), 'F': cv.imread("menti_words/f.jpg", cv.IMREAD_COLOR), 'H': cv.imread("menti_words/h.jpg", cv.IMREAD_COLOR), 'I': cv.imread("menti_words/i.jpg", cv.IMREAD_COLOR), 'L': cv.imread("menti_words/l.jpg", cv.IMREAD_COLOR), 'M': cv.imread("menti_words/m.jpg", cv.IMREAD_COLOR), 'N': cv.imread("menti_words/n.jpg", cv.IMREAD_COLOR), 'O': cv.imread("menti_words/o.jpg", cv.IMREAD_COLOR), 'P': cv.imread("menti_words/p.jpg", cv.IMREAD_COLOR), 'R': cv.imread("menti_words/r.jpg", cv.IMREAD_COLOR), 'S': cv.imread("menti_words/s.jpg", cv.IMREAD_COLOR), 'T': cv.imread("menti_words/t.jpg", cv.IMREAD_COLOR), 'U': cv.imread("menti_words/u.jpg", cv.IMREAD_COLOR), 'W': cv.imread("menti_words/w.jpg", cv.IMREAD_COLOR) } self.current_letters = [] self.letter_coords = {} with open("menti_dic") as file: self.word_list = [line.rstrip() for line in file] def reset_lists(self): self.current_letters = [] self.letter_coords = {} def assess_playfield_and_make_move(self): if self.check_for_button_and_execute(self.capture_window.get_screenshot(), self.menti_play_button): cv.waitKey(2000) self.reset_lists() self.get_current_board_state() if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused': return matches = self.possible_words(self.word_list, self.current_letters) matches.sort() new_matches = list(map(list,matches)) for letter_list in new_matches: self.type_word_into_ui(letter_list) if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused': return def type_word_into_ui(self, letters): for letter in letters: pydirectinput.moveTo(self.letter_coords[letter][0] + self.offset_left, self.letter_coords[letter][1] + self.offset_down) pydirectinput.mouseDown() w = random.randint(25, 50) cv.waitKey(100 + w) if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused': return pydirectinput.mouseUp() cv.waitKey(400) def get_current_board_state(self): #screenshot = cv.imread("menti_words/screenshot.jpg") screenshot = self.capture_window.get_screenshot() screenshot = screenshot[870:1270, 1080:1480] #cv.imshow("screenshot", screenshot) #cv.waitKey(150) #continue for needle_key in self.needles.keys(): # gray_needle = cv.cvtColor(self.needles[needle_key], cv.COLOR_BGR2GRAY) # thresh_needle = cv.threshold(gray_needle, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.85, 56) if len(rectangles) == 0: continue points = self.vision_stun.get_click_points(rectangles) self.current_letters.append(needle_key) self.letter_coords[needle_key] = points[0] if self.overlay.run_mode == 'stopped' or self.overlay.run_mode == 'paused': return def possible_words(self, lwords, charSet): lst = [] for word in lwords: if len(word) <= 2: continue flag = 1 chars = self.charCount(word) for key in chars: if key not in charSet: flag = 0 else: if charSet.count(key) != chars[key]: flag = 0 if flag == 1: lst.append(word) #print(word) return list(set(lst)) def charCount(self, word): dict = {} for i in word: dict[i] = dict.get(i, 0) + 1 return dict