dungeon.py 5.5 KB

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