| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import cv2
- import os
- import time
- import numpy as np
- import random
- from helper import (
- tap as tap_helper,
- look_for_templates,
- first_template,
- capture_current_screen,
- get_current_screen,
- )
- fire_or_earth = os.getenv("FIRE_OR_EARTH", "earth").lower()
- stop_at_first_death = os.getenv("FIRST_DEATH_STOP", "False").lower() == "true"
- swap_tank_at_low_health = (
- os.getenv("SWAP_TANK_AT_LOW_HEALTH", "False").lower() == "true"
- )
- # templates
- templates = {
- "to_battle": cv2.imread("templates/dungeon/to_battle.png"),
- "attack": cv2.imread("templates/dungeon/attack.png"),
- "auto_battle": cv2.imread("templates/dungeon/auto_battle.png"),
- "ok": cv2.imread("templates/dungeon/ok.png"),
- "lock": cv2.imread("templates/dungeon/lock.png"),
- "collect": cv2.imread("templates/dungeon/collect.png"),
- }
- tpl_mixed = cv2.imread("templates/dungeon/mixed.png")
- tpl_water = cv2.imread("templates/dungeon/water.png")
- tpl_earth = cv2.imread("templates/dungeon/earth.png")
- tpl_fire = cv2.imread("templates/dungeon/fire.png")
- tpl_angus = cv2.imread("templates/dungeon/angus.png")
- tpl_moloch = cv2.imread("templates/dungeon/moloch.png")
- tpl_angus2 = cv2.imread("templates/dungeon/angus2.png")
- tpl_moloch2 = cv2.imread("templates/dungeon/moloch2.png")
- tpl_dead = cv2.imread("templates/dungeon/dead.png")
- def low_health(element, name):
- left = element[0] - 113
- right = element[0] + 108
- top = element[1] + 138
- bottom = top + 11
- screen = read_screen()
- health_bar = screen[top:bottom, left:right]
- # cv2.imwrite("test/health_bar.png", health_bar)
- health_bar_hsv = cv2.cvtColor(health_bar, cv2.COLOR_BGR2HSV)
- green_lower = np.array([40, 40, 40]) # Lower end of green in HSV
- green_upper = np.array([80, 255, 255]) # Upper end of green in HSV
- mask = cv2.inRange(health_bar_hsv, green_lower, green_upper)
- total_length = mask.shape[1]
- current_length = total_length - np.argmax(np.flip(mask[0]) > 0)
- health_percentage = (current_length / total_length) * 100
- print(f"Remaining Health ({name}): {health_percentage:.2f}%")
- return health_percentage < 70
- def read_screen():
- data = get_current_screen()
- image_data = np.frombuffer(data, dtype=np.uint8)
- return cv2.imdecode(image_data, cv2.IMREAD_COLOR)
- def tap(name, x, y=None):
- sleep = 0.5
- if name in ["ok", "lock"]:
- sleep = 2
- elif name in ["collect"]:
- sleep = 5
- elif name in ["remove angus", "remove moloch"]:
- sleep = 1
- sleep += random.uniform(0, 0.2)
- text = f"- {name} (pause for {sleep:.2f}s)"
- if y is None and isinstance(x, tuple):
- x, y, *_ = x
- tap_helper(x + random.randint(-3, 3), y + random.randint(-3, 3), text)
- time.sleep(sleep)
- print("watching the screen...")
- is_mixed = False
- idle_counter = 0
- while True:
- try:
- capture_current_screen()
- name, locations = look_for_templates(templates)
- if name is not None:
- if name in ["to_battle", "ok", "lock", "collect"]:
- tap(name, *locations[0])
- idle_counter = 0
- continue
- if name in ["auto_battle"]:
- dead = stop_at_first_death and first_template(tpl_dead)
- if dead:
- print("++++++++ at least one titan is dead. stopping")
- break
- if swap_tank_at_low_health and is_mixed:
- moloch = first_template(tpl_moloch)
- if (
- moloch
- and moloch[1] > 450
- and moloch[1] < 1350
- and low_health(moloch, "moloch")
- ):
- angus = first_template(tpl_angus2)
- if angus:
- tap("remove angus", angus)
- tap("add moloch", moloch)
- else:
- print(
- "######### seems like angus is dead already. stopping"
- )
- break
- else:
- angus = first_template(tpl_angus)
- if (
- angus
- and moloch[1] > 450
- and angus[1] < 1350
- and low_health(angus, "angus")
- ):
- moloch = first_template(tpl_moloch2)
- if moloch:
- tap("remove moloch", moloch)
- tap("add angus", angus)
- else:
- print(
- "######### seems like moloch is dead already. stopping"
- )
- break
- tap(name, *locations[0])
- idle_counter = 0
- continue
- if name == "attack":
- if len(locations) == 1:
- element = first_template(tpl_mixed)
- is_mixed = element is not None
- tap(name, *locations[0])
- continue
- if len(locations) == 2:
- element = first_template(tpl_mixed)
- if element:
- is_mixed = True
- else:
- is_mixed = False
- element = first_template(tpl_water)
- if element is None:
- if fire_or_earth == "earth":
- element = first_template(tpl_earth)
- else:
- element = first_template(tpl_fire)
- tap(name, element[0], locations[0][1])
- idle_counter = 0
- continue
- print("...")
- idle_counter += 1
- if idle_counter >= 3:
- print("######### stopping due to three idle loop iterations.")
- break
- except Exception as e:
- print(f"error: {e}")
|