dungeon.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_battle2.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. capture_current_screen()
  52. name, locations = look_for_templates(templates)
  53. if name is not None:
  54. if name in ["to_battle", "auto_battle", "ok", "lock", "collect"]:
  55. if name in ["auto_battle"]:
  56. if is_mixed:
  57. moloch = first_template(tpl_moloch)
  58. if moloch and moloch[1] < 1000 and low_health(moloch, "moloch"):
  59. angus = first_template(tpl_angus2)
  60. tap(angus)
  61. time.sleep(0.5)
  62. tap(angus[0] + 500, angus[1])
  63. else:
  64. angus = first_template(tpl_angus)
  65. if angus and angus[1] < 1000 and low_health(angus, "angus"):
  66. moloch = first_template(tpl_moloch2)
  67. tap(moloch)
  68. time.sleep(0.5)
  69. tap(moloch[0] - 500, moloch[1])
  70. time.sleep(0.5)
  71. tap(*locations[0])
  72. if name in ["auto_battle", "ok"]:
  73. time.sleep(1.5)
  74. if name == "attack" and len(locations) == 1:
  75. element = first_template(tpl_mixed)
  76. is_mixed = element is not None
  77. tap(*locations[0])
  78. if name == "attack" and len(locations) == 2:
  79. element = first_template(tpl_mixed)
  80. if element:
  81. is_mixed = True
  82. else:
  83. is_mixed = False
  84. element = first_template(tpl_water)
  85. if element is None:
  86. element = first_template(tpl_earth)
  87. tap(element[0], locations[0][1])
  88. print("...")