dungeon.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import cv2
  2. import time
  3. import numpy as np
  4. from helper import (
  5. tap,
  6. look_for_templates,
  7. find_template,
  8. first_template,
  9. capture_current_screen,
  10. get_current_screen,
  11. )
  12. # templates
  13. templates = {
  14. "to_battle": cv2.imread("templates/dungeon/to_battle2.png"),
  15. "attack": cv2.imread("templates/dungeon/attack.png"),
  16. "auto_battle": cv2.imread("templates/dungeon/auto_battle.png"),
  17. "ok": cv2.imread("templates/dungeon/ok.png"),
  18. "lock": cv2.imread("templates/dungeon/lock.png"),
  19. "collect": cv2.imread("templates/dungeon/collect.png"),
  20. }
  21. tpl_mixed = cv2.imread("templates/dungeon/mixed.png")
  22. tpl_water = cv2.imread("templates/dungeon/water.png")
  23. tpl_earth = cv2.imread("templates/dungeon/earth.png")
  24. tpl_angus = cv2.imread("templates/dungeon/angus.png")
  25. tpl_moloch = cv2.imread("templates/dungeon/moloch.png")
  26. tpl_angus2 = cv2.imread("templates/dungeon/angus2.png")
  27. tpl_moloch2 = cv2.imread("templates/dungeon/moloch2.png")
  28. def low_health(element, name):
  29. left = element[0] - 113
  30. right = element[0] + 108
  31. top = element[1] + 158
  32. bottom = top + 11
  33. screen = read_screen()
  34. health_bar = screen[top:bottom, left:right]
  35. cv2.imwrite("test/health_bar.png", health_bar)
  36. health_bar_hsv = cv2.cvtColor(health_bar, cv2.COLOR_BGR2HSV)
  37. green_lower = np.array([40, 40, 40]) # Lower end of green in HSV
  38. green_upper = np.array([80, 255, 255]) # Upper end of green in HSV
  39. mask = cv2.inRange(health_bar_hsv, green_lower, green_upper)
  40. total_length = mask.shape[1]
  41. current_length = total_length - np.argmax(np.flip(mask[0]) > 0)
  42. health_percentage = (current_length / total_length) * 100
  43. print(f"Remaining Health ({name}): {health_percentage:.2f}%")
  44. return health_percentage < 60
  45. def read_screen():
  46. data = get_current_screen()
  47. image_data = np.frombuffer(data, dtype=np.uint8)
  48. return cv2.imdecode(image_data, cv2.IMREAD_COLOR)
  49. print("watching the screen...")
  50. is_mixed = False
  51. while True:
  52. time.sleep(0.5) # Polling interval
  53. capture_current_screen()
  54. name, locations = look_for_templates(templates)
  55. if name is not None:
  56. if name in ["to_battle", "auto_battle", "ok", "lock", "collect"]:
  57. if name in ["auto_battle"]:
  58. if is_mixed:
  59. moloch = first_template(tpl_moloch)
  60. if moloch and moloch[1] < 1000 and low_health(moloch, "moloch"):
  61. angus = first_template(tpl_angus2)
  62. tap(angus)
  63. time.sleep(0.5)
  64. tap(angus[0] + 500, angus[1])
  65. else:
  66. angus = first_template(tpl_angus)
  67. if angus and angus[1] < 1000 and low_health(angus, "angus"):
  68. moloch = first_template(tpl_moloch2)
  69. tap(moloch)
  70. time.sleep(0.5)
  71. tap(moloch[0] - 500, moloch[1])
  72. time.sleep(0.5)
  73. tap(*locations[0])
  74. if name in ["auto_battle", "ok"]:
  75. time.sleep(1.5)
  76. if name == "attack" and len(locations) == 1:
  77. element = first_template(tpl_mixed)
  78. is_mixed = element is not None
  79. tap(*locations[0])
  80. if name == "attack" and len(locations) == 2:
  81. element = first_template(tpl_mixed)
  82. if element:
  83. is_mixed = True
  84. else:
  85. is_mixed = False
  86. element = first_template(tpl_water)
  87. if element is None:
  88. element = first_template(tpl_earth)
  89. tap(element[0], locations[0][1])
  90. print("...")