diff --git a/aoe_bot.py b/aoe_bot.py new file mode 100644 index 0000000..2b1b1c0 --- /dev/null +++ b/aoe_bot.py @@ -0,0 +1,7 @@ +import aoe_commands +import aoe_start_routine +import aoe_recognition + +aoe_recognition.wait_for_start() +aoe_start_routine.run_start_routine() +aoe_recognition.collect_screenshots() \ No newline at end of file diff --git a/aoe_commands.py b/aoe_commands.py new file mode 100644 index 0000000..1a7c751 --- /dev/null +++ b/aoe_commands.py @@ -0,0 +1,17 @@ +from pyautogui import * +import pyautogui +import keyboard + +WAIT4CLICK = 0.04 + +def order_peasants(number): + print(f"Trying to order {number} peasants.") + select_town_center() + for i in range(number): + pyautogui.press('q') + time.sleep(WAIT4CLICK) + +def select_town_center(): + pyautogui.press('h') + time.sleep(WAIT4CLICK) + \ No newline at end of file diff --git a/aoe_recognition.py b/aoe_recognition.py new file mode 100644 index 0000000..07e9973 --- /dev/null +++ b/aoe_recognition.py @@ -0,0 +1,36 @@ +from pyautogui import * +import pyautogui +import time +import keyboard +import random +import win32api, win32con +import time +from PIL import Image, ImageDraw + +import aoe_commands + +def wait_for_start(): + start_started = False + while 1: + if pyautogui.locateOnScreen('images/startscreen.png', confidence=0.9) != None: + print("Startscreen found") + start_started = True + time.sleep(0.5) + elif start_started: + print("Proceeding") + return + else: + print("Startscreen not found") + time.sleep(0.5) + +def collect_screenshots(): + cnt = 0 + while True: #keyboard.is_pressed('q') == False: + time.sleep(10) + pic = pyautogui.screenshot() + pic.save(f"image_{cnt}.png") + cnt += 1 + +def initial_scout_trace(): + aoe_commands.select_town_center() + # TODO diff --git a/aoe_start_routine.py b/aoe_start_routine.py new file mode 100644 index 0000000..33f8a60 --- /dev/null +++ b/aoe_start_routine.py @@ -0,0 +1,65 @@ +from pyautogui import * +import pyautogui +import keyboard + +WAIT4CLICK = 0.04 + +import aoe_commands + +def assign_hotkeys(): + pyautogui.press(',') + time.sleep(WAIT4CLICK) + pyautogui.hotkey('ctrl', '1') + time.sleep(WAIT4CLICK) + + pyautogui.press('.') + time.sleep(WAIT4CLICK) + pyautogui.hotkey('ctrl', '2') + time.sleep(WAIT4CLICK) + + pyautogui.press('.') + time.sleep(WAIT4CLICK) + pyautogui.hotkey('ctrl', '3') + time.sleep(WAIT4CLICK) + + pyautogui.press('.') + time.sleep(WAIT4CLICK) + pyautogui.hotkey('ctrl', '4') + time.sleep(WAIT4CLICK) + +def build_houses(): + pyautogui.press('h') + time.sleep(WAIT4CLICK) + pyautogui.press('up') + time.sleep(0.5) + pyautogui.press('2') + time.sleep(WAIT4CLICK) + + pyautogui.press('q') + time.sleep(WAIT4CLICK) + pyautogui.press('q') + time.sleep(WAIT4CLICK) + pyautogui.click(650, 90) + time.sleep(WAIT4CLICK) + + pyautogui.press('3') + time.sleep(WAIT4CLICK) + pyautogui.rightClick(650, 90) + time.sleep(WAIT4CLICK) + + pyautogui.press('4') + time.sleep(WAIT4CLICK) + + pyautogui.press('q') + time.sleep(WAIT4CLICK) + pyautogui.press('q') + time.sleep(WAIT4CLICK) + pyautogui.click(300, 200) + time.sleep(WAIT4CLICK) + +def run_start_routine(): + aoe_commands.order_peasants(4) + assign_hotkeys() + build_houses() + pyautogui.press('1') + pyautogui.press('1') \ No newline at end of file diff --git a/images/startscreen.png b/images/startscreen.png new file mode 100644 index 0000000..24861d6 Binary files /dev/null and b/images/startscreen.png differ diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..ee42b98 --- /dev/null +++ b/readme.md @@ -0,0 +1,35 @@ +AoE Bot +======= + +Preparation +----------- + +- Install python > 3.8 +- Install these libraries from an administrator terminal (windows): + - pip install pywin32 + - pip install keyboard + - pip install pyautogui + - pip install opencv-python + + +Start +------ +run aoe_bot.py before starting the game. It will wait for the startscreen and do some standard actions when the game has started: + +- order 4 villagers +- assign hotkeys: 1: scout, 2-4: villagers +- build first house with villagers 1+2 +- build second house with villager 3 +- jump to scout + +After this it will collect screenshots during the game to train data + +Interesting Links +------------------ +- [Tutorial video + notes](https://github.com/KianBrose/Image-Recognition-Botting-Tutorial/blob/master/README.txt) +- [Pyautogui Doc](https://pyautogui.readthedocs.io/en/latest/keyboard.html) +- [PIL doc](https://pillow.readthedocs.io/en/stable/reference/Image.html) +- [Draw Image api](https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html) +- [Draw Image Example](http://effbot.org/imagingbook/imagedraw.htm) +- [AoE Hotykey editor](https://aokhotkeys.appspot.com/editor) +- [Structureing your python project](https://python-docs.readthedocs.io/en/latest/writing/structure.html) diff --git a/test.py b/test.py index 1359c91..425a47d 100644 --- a/test.py +++ b/test.py @@ -7,7 +7,7 @@ import win32api, win32con import time from PIL import Image, ImageDraw -WAIT4CLICK = 0.04 + # see https://github.com/KianBrose/Image-Recognition-Botting-Tutorial/blob/master/README.txt #def click(x,y): @@ -62,6 +62,7 @@ def wait_for_start(): def zoomout(): + "Tut nicht" time.sleep(0.5) print("Zooming out") pyautogui.click(x=500, y=500) @@ -81,13 +82,7 @@ def find_peasants(): print(f"Für Bild {img_path} wurden {len(matches)} matches gefunden: {matches}") return all_matches -def order_peasants(number): - print(f"Trying to order {number} peasants.") - pyautogui.press('h') - time.sleep(WAIT4CLICK) - for i in range(number): - pyautogui.press('q') - time.sleep(WAIT4CLICK) + def assign_hotkeys(): pyautogui.press(',')