242 lines
7.9 KiB
Python
242 lines
7.9 KiB
Python
from pyautogui import *
|
|
import pyautogui
|
|
import time
|
|
import keyboard
|
|
import random
|
|
import win32api, win32con
|
|
import time
|
|
from PIL import Image, ImageDraw
|
|
import aoe_recognition_ocr
|
|
|
|
import aoe_commands
|
|
DELTA = 1
|
|
|
|
class InfoHolder:
|
|
def __init__(self):
|
|
self.data = {}
|
|
|
|
INFO = InfoHolder()
|
|
|
|
MAPSIZES = {
|
|
'WINZIG': 120,
|
|
'KLEIN': 144,
|
|
'MITTEL': 168,
|
|
'NORMAL': 200,
|
|
'GROSS': 220,
|
|
'RIESIG': 240
|
|
}
|
|
|
|
def wait_for_start():
|
|
start_started = False
|
|
while 1:
|
|
if pyautogui.locateOnScreen('images/startscreen.png', confidence=0.9) != None:
|
|
print("Startscreen found")
|
|
eval_startscreen()
|
|
start_started = True
|
|
time.sleep(0.5)
|
|
elif start_started:
|
|
print("Proceeding")
|
|
return
|
|
else:
|
|
print("Startscreen not found")
|
|
time.sleep(0.5)
|
|
|
|
def eval_startscreen():
|
|
start_info = aoe_recognition_ocr.recognize_text(pyautogui.screenshot())
|
|
|
|
|
|
mapsizerec = re.findall(r'KARTENGROSSE +(\w+) ', start_info)
|
|
if len(mapsizerec) == 1 and mapsizerec[0] in MAPSIZES.keys():
|
|
INFO.data['mapsize'] = MAPSIZES[mapsizerec[0]]
|
|
print("Kartengroeße erkannt:", INFO.data['mapsize'])
|
|
else:
|
|
print("Startscreen text: ", start_info)
|
|
|
|
def find_top(image,x,y,classifyer):
|
|
left = (x,y)
|
|
print(f"xy: {x},{y}")
|
|
loop = True
|
|
while loop:
|
|
nxt = left[0], left[1] - 1
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = left[0] - DELTA, left[1] -1
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = left[0] + DELTA, left[1] -1
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
break
|
|
left = nxt
|
|
return left[1]
|
|
|
|
def find_bottom(image,x,y,classifyer):
|
|
right = (x,y)
|
|
loop = True
|
|
while loop:
|
|
nxt = right[0], right[1] + 1
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = right[0] - DELTA, right[1] + 1
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = right[0] + DELTA, right[1] + 1
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
break
|
|
right = nxt
|
|
return right[1]
|
|
|
|
def find_left(image, x, y, classifyer):
|
|
top = (x,y)
|
|
loop = True
|
|
while loop:
|
|
nxt = top[0] - 1, top[1]
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = top[0] - 1, top[1] - DELTA
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = top[0] - 1, top[1] + DELTA
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
break
|
|
top = nxt
|
|
return top[0]
|
|
|
|
def find_right(image, x, y, classifyer):
|
|
bottom = (x,y)
|
|
loop = True
|
|
while loop:
|
|
nxt = bottom[0] + 1, bottom[1]
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = bottom[0] + 1, bottom[1] - DELTA
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
nxt = bottom[0] + 1, bottom[1] + DELTA
|
|
rr,gg,bb = image.getpixel(nxt)
|
|
if not classifyer(rr,gg,bb):
|
|
break
|
|
bottom = nxt
|
|
return bottom[0]
|
|
|
|
def collect_screenshots():
|
|
cnt = 0
|
|
while True: #keyboard.is_pressed('q') == False:
|
|
time.sleep(10)
|
|
read_resources()
|
|
pic = pyautogui.screenshot()
|
|
pic.save(f"image_{cnt}.png")
|
|
cnt += 1
|
|
|
|
def find_town_center_in_map():
|
|
aoe_commands.select_town_center()
|
|
image = pyautogui.screenshot()
|
|
# Map: 1546 886 - 1920 1080
|
|
width, height = image.size
|
|
is_white = lambda r,g,b: r == 255 and b == 255 and g == 255
|
|
for x in range(1546, width, 1):
|
|
for y in range(886, height, 1):
|
|
r,g,b = image.getpixel((x,y))
|
|
if is_white(r,g,b):
|
|
top = find_top(image, x, y, is_white)
|
|
bottom = find_bottom(image, x, y, is_white)
|
|
left = find_left(image, x, y, is_white)
|
|
right = find_right(image, x, y, is_white)
|
|
print(f"Found white pixel ({x}/{y}) --> ({left}, {top}),({right}, {bottom})")
|
|
if bottom - top == right - left: # viereckig in der Karte
|
|
print (f"found town center in map: {left}/{top}")
|
|
return (left + 1, top + 1)
|
|
return (0,0)
|
|
|
|
def scale_to_minimap_size(pixels):
|
|
"Use MITTEL sized maps for inputs"
|
|
return int(pixels / (INFO.data["mapsize"] / (MAPSIZES["MITTEL"])))
|
|
|
|
def initial_scout_trace():
|
|
walk_further_out = 3
|
|
outer_circle = [(1730, 1072), (1570,986),(1730, 898), (1890, 984), (1730, 1072)]
|
|
mid_circle = [(1730, 1047), (1610,986),(1730, 920), (1840, 984), (1730, 1035)]
|
|
inner_circle = [(1730, 1020), (1650, 980), (1730, 940), (1790, 980), (1730, 1000) ]
|
|
center = [(1730, 980)]
|
|
x, y = find_town_center_in_map()
|
|
time.sleep(0.3)
|
|
not_black = lambda r,g,b: r > 1 or b > 1 or g > 1
|
|
image = pyautogui.screenshot()
|
|
lx = find_left(image, x, y, not_black)
|
|
rx = find_right(image, x, y, not_black)
|
|
|
|
ty = find_top(image, x, y, not_black)
|
|
by = find_bottom(image, x, y, not_black)
|
|
print(f"Town center x: {x}, y:{y} gave us minimal and maximal x values to watch: lx: {lx}, rx={rx}")
|
|
scout_trace = []
|
|
delta = scale_to_minimap_size(4)
|
|
for yy in range(by, ty, -5):
|
|
xx = find_left(image, x, yy, not_black) - delta
|
|
scout_trace.append((xx,yy))
|
|
for xx in range(lx, rx, 5):
|
|
yy = find_top(image, xx, y, not_black) - delta
|
|
scout_trace.append((xx,yy))
|
|
for yy in range(ty, by, 5):
|
|
xx = find_right(image, x, yy, not_black) + delta
|
|
scout_trace.append((xx,yy))
|
|
for xx in range(rx, lx, -5):
|
|
yy = find_bottom(image, xx, y, not_black) + delta
|
|
scout_trace.append((xx,yy))
|
|
|
|
delta = scale_to_minimap_size(12)
|
|
for yy in range(by, ty, -5):
|
|
xx = find_left(image, x, yy, not_black) - delta
|
|
scout_trace.append((xx,yy))
|
|
for xx in range(lx, rx, 5):
|
|
yy = find_top(image, xx, y, not_black) - delta
|
|
scout_trace.append((xx,yy))
|
|
for yy in range(ty, by, 5):
|
|
xx = find_right(image, x, yy, not_black) + delta
|
|
scout_trace.append((xx,yy))
|
|
for xx in range(rx, lx, -5):
|
|
yy = find_bottom(image, xx, y, not_black) + delta
|
|
scout_trace.append((xx,yy))
|
|
|
|
delta = scale_to_minimap_size(20)
|
|
for yy in range(by, ty, -5):
|
|
xx = find_left(image, x, yy, not_black) - delta
|
|
scout_trace.append((xx,yy))
|
|
for xx in range(lx, rx, 5):
|
|
yy = find_top(image, xx, y, not_black) - delta
|
|
scout_trace.append((xx,yy))
|
|
for yy in range(ty, by, 5):
|
|
xx = find_right(image, x, yy, not_black) + delta
|
|
scout_trace.append((xx,yy))
|
|
for xx in range(rx, lx, -5):
|
|
yy = find_bottom(image, xx, y, not_black) + delta
|
|
scout_trace.append((xx,yy))
|
|
|
|
print(f"scout_trace: {scout_trace}")
|
|
return scout_trace + center + inner_circle + mid_circle + outer_circle
|
|
|
|
RESOURCES_LOCATIONS = {
|
|
#'villager_wood': (28,39,20,10),
|
|
'wood': (50,20,50,20),
|
|
#'villager_food': (128,39,20,10),
|
|
'food': (150,20,50,20),
|
|
#'villager_gold': (228,39,20,10),
|
|
'gold': (250,20,50,20),
|
|
#'villager_stone': (330,39,20,10),
|
|
'stone': (350,20,50,20)#,
|
|
#'villager_houses': (450, 20, 70, 20),
|
|
#'free_villagers': (543,32,11,11)
|
|
}
|
|
|
|
def read_resources():
|
|
res = {}
|
|
for topic in RESOURCES_LOCATIONS.keys():
|
|
img = pyautogui.screenshot(region=RESOURCES_LOCATIONS[topic])
|
|
txt = aoe_recognition_ocr.recognize_number(img)
|
|
print(topic, txt)
|
|
#img.save(f"{topic}.png")
|
|
if txt == "":
|
|
txt = 0
|
|
res[topic] = int(txt)
|
|
return res |