dungeon.py 4.4 KB

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