dungeon.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import cv2
  2. import os
  3. import time
  4. import numpy as np
  5. import random
  6. from helper import (
  7. tap as tap_helper,
  8. look_for_templates,
  9. first_template,
  10. capture_current_screen,
  11. get_current_screen,
  12. )
  13. fire_or_earth = os.getenv("FIRE_OR_EARTH", "earth").lower()
  14. stop_at_first_death = os.getenv("FIRST_DEATH_STOP", "False").lower() == "true"
  15. swap_tank_at_low_health = (
  16. os.getenv("SWAP_TANK_AT_LOW_HEALTH", "False").lower() == "true"
  17. )
  18. # templates
  19. templates = {
  20. "to_battle": cv2.imread("templates/dungeon/to_battle.png"),
  21. "attack": cv2.imread("templates/dungeon/attack.png"),
  22. "auto_battle": cv2.imread("templates/dungeon/auto_battle.png"),
  23. "ok": cv2.imread("templates/dungeon/ok.png"),
  24. "lock": cv2.imread("templates/dungeon/lock.png"),
  25. "collect": cv2.imread("templates/dungeon/collect.png"),
  26. }
  27. tpl_mixed = cv2.imread("templates/dungeon/mixed.png")
  28. tpl_water = cv2.imread("templates/dungeon/water.png")
  29. tpl_earth = cv2.imread("templates/dungeon/earth.png")
  30. tpl_fire = cv2.imread("templates/dungeon/fire.png")
  31. tpl_angus = cv2.imread("templates/dungeon/angus.png")
  32. tpl_moloch = cv2.imread("templates/dungeon/moloch.png")
  33. tpl_angus2 = cv2.imread("templates/dungeon/angus2.png")
  34. tpl_moloch2 = cv2.imread("templates/dungeon/moloch2.png")
  35. tpl_dead = cv2.imread("templates/dungeon/dead.png")
  36. def low_health(element, name):
  37. left = element[0] - 113
  38. right = element[0] + 108
  39. top = element[1] + 138
  40. bottom = top + 11
  41. screen = read_screen()
  42. health_bar = screen[top:bottom, left:right]
  43. # cv2.imwrite("test/health_bar.png", health_bar)
  44. health_bar_hsv = cv2.cvtColor(health_bar, cv2.COLOR_BGR2HSV)
  45. green_lower = np.array([40, 40, 40]) # Lower end of green in HSV
  46. green_upper = np.array([80, 255, 255]) # Upper end of green in HSV
  47. mask = cv2.inRange(health_bar_hsv, green_lower, green_upper)
  48. total_length = mask.shape[1]
  49. current_length = total_length - np.argmax(np.flip(mask[0]) > 0)
  50. health_percentage = (current_length / total_length) * 100
  51. print(f"Remaining Health ({name}): {health_percentage:.2f}%")
  52. return health_percentage < 70
  53. def read_screen():
  54. data = get_current_screen()
  55. image_data = np.frombuffer(data, dtype=np.uint8)
  56. return cv2.imdecode(image_data, cv2.IMREAD_COLOR)
  57. def tap(name, x, y=None):
  58. sleep = 0.5
  59. if name in ["ok", "lock"]:
  60. sleep = 2
  61. elif name in ["collect"]:
  62. sleep = 5
  63. elif name in ["remove angus", "remove moloch"]:
  64. sleep = 1
  65. sleep += random.uniform(0, 0.2)
  66. text = f"- {name} (pause for {sleep}s)"
  67. tap_helper(x + random.randint(-3, 3), y + random.randint(-3, 3), text)
  68. time.sleep(sleep)
  69. print("watching the screen...")
  70. is_mixed = False
  71. idle_counter = 0
  72. while True:
  73. try:
  74. capture_current_screen()
  75. name, locations = look_for_templates(templates)
  76. if name is not None:
  77. if name in ["to_battle", "ok", "lock", "collect"]:
  78. tap(name, *locations[0])
  79. idle_counter = 0
  80. continue
  81. if name in ["auto_battle"]:
  82. dead = stop_at_first_death and first_template(tpl_dead)
  83. if dead:
  84. print("++++++++ at least one titan is dead. stopping")
  85. break
  86. if swap_tank_at_low_health and is_mixed:
  87. moloch = first_template(tpl_moloch)
  88. if (
  89. moloch
  90. and moloch[1] > 450
  91. and moloch[1] < 1350
  92. and low_health(moloch, "moloch")
  93. ):
  94. angus = first_template(tpl_angus2)
  95. if angus:
  96. tap("remove angus", angus)
  97. tap("add moloch", angus[0] + 500, angus[1])
  98. else:
  99. print(
  100. "######### seems like angus is dead already. stopping"
  101. )
  102. break
  103. else:
  104. angus = first_template(tpl_angus)
  105. if (
  106. angus
  107. and moloch[1] > 450
  108. and angus[1] < 1350
  109. and low_health(angus, "angus")
  110. ):
  111. moloch = first_template(tpl_moloch2)
  112. if moloch:
  113. tap("remove moloch", moloch)
  114. tap("add angus", moloch[0] - 500, moloch[1])
  115. else:
  116. print(
  117. "######### seems like moloch is dead already. stopping"
  118. )
  119. break
  120. tap(name, *locations[0])
  121. idle_counter = 0
  122. continue
  123. if name == "attack":
  124. if len(locations) == 1:
  125. element = first_template(tpl_mixed)
  126. is_mixed = element is not None
  127. tap(name, *locations[0])
  128. continue
  129. if len(locations) == 2:
  130. element = first_template(tpl_mixed)
  131. if element:
  132. is_mixed = True
  133. else:
  134. is_mixed = False
  135. element = first_template(tpl_water)
  136. if element is None:
  137. if fire_or_earth == "earth":
  138. element = first_template(tpl_earth)
  139. else:
  140. element = first_template(tpl_fire)
  141. tap(name, element[0], locations[0][1])
  142. idle_counter = 0
  143. continue
  144. print("...")
  145. idle_counter += 1
  146. if idle_counter >= 3:
  147. print("######### stopping due to three idle loop iterations.")
  148. break
  149. except Exception as e:
  150. print(f"error: {e}")