-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy patheasy_rise.py
More file actions
1205 lines (977 loc) · 45.4 KB
/
easy_rise.py
File metadata and controls
1205 lines (977 loc) · 45.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
@author: Emilio Moretti
Copyright 2013 Emilio Moretti <emilio.morettiATgmailDOTcom>
This program is distributed under the terms of the GNU Lesser General Public License.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import random
import time
from datetime import datetime, timedelta
import win32api
import win32gui
# The example starts here
from AutoHotPy import AutoHotPy # we need to tell python that we are going to use the library
#TESTING
import cv2
import pytesseract
from PIL import ImageGrab
import os
import numpy as np
import numpy as np
from InterceptionWrapper import InterceptionMouseState, InterceptionMouseStroke, InterceptionMouseFlag
from PIL import Image
# CONFIGURATION
HEALING_POT_KEY = 0 # Change accordingly
MANA_POT_KEY = 9 # Change accordingly
HEAL_PARTY_MEMBER_KEY = 2 # Change accordingly
start_time=0
# Available pages -> F1, F2, F3, F4, F5, F6, F7
ACTIVE_SKILL_PAGES = {
#'F4': [1,2,3,4,5] # Change accordingly
'F1':[2]
}
# Be careful not to overlap any skills with your HEAL/MANA pot keys
# One skill page with F4 example is below
# ACTIVE_SKILL_PAGES = {
# 'F4': [3, 4, 5, 6]
# }
ENABLE_R_HITS = True # Change it to True for basic attacks
# Do not change anything below this line
SELF_HP_X = 195
SELF_HP_Y = 46
SELF_MP_X = 180
SELF_MP_Y = 66
MONSTER_HP_X=960-30
MONSTER_HP_y=46
FIRST_PARTY_MEMBER_X = 1799
FIRST_PARTY_MEMBER_Y = 258
CURRENT_PARTY = []
PARTY_VALID_R = 159
PARTY_VALID_G = 57
PARTY_VALID_B = 39
PARTY_MEMBER_OFFSET_Y = 145
repeat_always = False
is_r_used=False
SELECTED_RUNTIME_CONFIGURATION = 0 # None
TIME_DELAY_BETWEEN_SKILLS = 0.1 # in seconds
HP_R = 0
HP_G = 0
HP_B = 0
MP_R = 0
MP_G = 0
MP_B = 0
number_template_dict = {
0: "num_0.png",
1: "num_1.png",
2: "num_2.png",
3: "num_3.png",
4: "num_4.png",
5: "num_5.png",
6: "num_6.png",
7: "num_7.png",
8: "num_8.png",
9: "num_9.png",
}
# END CONFIGURATION
# IMAGE PROCESSING
def nothing(x):
pass
def enhance_contrast(image, clip_limit=2.0, tile_grid_size=(8, 8)):
# Convert to LAB color space
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
# Apply CLAHE (Contrast Limited Adaptive Histogram Equalization)
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
l = clahe.apply(l)
# Merge channels and convert back to BGR color space
enhanced_lab = cv2.merge([l, a, b])
enhanced_image = cv2.cvtColor(enhanced_lab, cv2.COLOR_LAB2BGR)
return enhanced_image
def enhance_contrast_grayscale(image, clip_limit=2.0, tile_grid_size=(8, 8)):
# Apply CLAHE (Contrast Limited Adaptive Histogram Equalization) to a grayscale image
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
enhanced_image = clahe.apply(image)
return enhanced_image
def refine_red_extraction_and_debug(image, specific_red_rgb, save_path_prefix):
# Ensure the save_path_prefix directory exists
os.makedirs(os.path.dirname(save_path_prefix), exist_ok=True)
img = image
# # Create a window
# cv2.namedWindow('image')
#
# # create trackbars for color change
# cv2.createTrackbar('HMin', 'image', 0, 179, nothing) # Hue is from 0-179 for Opencv
# cv2.createTrackbar('SMin', 'image', 0, 255, nothing)
# cv2.createTrackbar('VMin', 'image', 0, 255, nothing)
# cv2.createTrackbar('HMax', 'image', 0, 179, nothing)
# cv2.createTrackbar('SMax', 'image', 0, 255, nothing)
# cv2.createTrackbar('VMax', 'image', 0, 255, nothing)
#
# # Set default value for MAX HSV trackbars.
# cv2.setTrackbarPos('HMax', 'image', 179)
# cv2.setTrackbarPos('SMax', 'image', 255)
# cv2.setTrackbarPos('VMax', 'image', 255)
#
# # Initialize to check if HSV min/max value changes
# hMin = sMin = vMin = hMax = sMax = vMax = 0
# phMin = psMin = pvMin = phMax = psMax = pvMax = 0
#
# output = image
# wait_time = 33
#
# while (1):
#
# # get current positions of all trackbars
# hMin = cv2.getTrackbarPos('HMin', 'image')
# sMin = cv2.getTrackbarPos('SMin', 'image')
# vMin = cv2.getTrackbarPos('VMin', 'image')
#
# hMax = cv2.getTrackbarPos('HMax', 'image')
# sMax = cv2.getTrackbarPos('SMax', 'image')
# vMax = cv2.getTrackbarPos('VMax', 'image')
#
# # Set minimum and max HSV values to display
# lower = np.array([hMin, sMin, vMin])
# upper = np.array([hMax, sMax, vMax])
#
# # Create HSV Image and threshold into a range.
# hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# mask = cv2.inRange(hsv, lower, upper)
# output = cv2.bitwise_and(image, image, mask=mask)
#
# # Print if there is a change in HSV value
# if ((phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax)):
# print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (
# hMin, sMin, vMin, hMax, sMax, vMax))
# phMin = hMin
# psMin = sMin
# pvMin = vMin
# phMax = hMax
# psMax = sMax
# pvMax = vMax
#
# # Display output image
# cv2.imshow('image', output)
#
# # Wait longer to prevent freeze for videos.
# if cv2.waitKey(wait_time) & 0xFF == ord('q'):
# break
#
# cv2.destroyAllWindows()
# exit(1)
lower = np.array([18, 0, 0])
upper = np.array([179, 255, 255])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image, image, mask=mask)
cv2.imwrite(f"{save_path_prefix}output_img_00_NEW.png", output)
image = output
# Apply the mask to isolate red color
isolated_red = cv2.bitwise_and(image, image, mask=mask)
cv2.imwrite(f"{save_path_prefix}_03_isolated_red.png", isolated_red)
# Convert to grayscale
gray_image = cv2.cvtColor(isolated_red, cv2.COLOR_BGR2GRAY)
cv2.imwrite(f"{save_path_prefix}_04_gray.png", gray_image)
# Enhance contrast using histogram equalization
equalized_image = cv2.equalizeHist(gray_image)
cv2.imwrite(f"{save_path_prefix}_05_equalized.png", equalized_image)
# Background is black, text is white. just reverse and return
#your code
# Invert the image to get white background and black text
inverted_image = cv2.bitwise_not(equalized_image)
cv2.imwrite(f"{save_path_prefix}_06_inverted_for_ocr.png", inverted_image)
new_img = enhance_contrast_grayscale(inverted_image)
contrasted_image_path = f"{save_path_prefix}_07_contrasted_for_ocr.png"
cv2.imwrite(contrasted_image_path, new_img)
return inverted_image
def process_and_extract_text(screenshot, save_path_prefix):
image = np.array(screenshot)
specific_red_rgb = [239, 50, 110] # The specific red of the text
processed_image = refine_red_extraction_and_debug(image, specific_red_rgb, save_path_prefix)
cv2.imwrite(f"{save_path_prefix}ocr_input_image.png", processed_image)
text = pytesseract.image_to_string(Image.open("test_4.png"), config='--psm 6')
return text.strip()
def match_template(original_image, template_image, threshold=0.8):
# Read the main image and template
main_img = original_image
template_img = template_image
# Convert images to grayscale
main_gray = cv2.cvtColor(main_img, cv2.COLOR_BGR2GRAY)
template_gray = cv2.cvtColor(template_img, cv2.COLOR_BGR2GRAY)
# Apply template matching
res = cv2.matchTemplate(main_gray, template_gray, cv2.TM_CCOEFF_NORMED)
# Find locations where the matching result exceeds the threshold
locations = np.where(res >= threshold)
w, h = template_gray.shape[::-1]
return locations, w, h
# Get the dimensions of the template image
# Draw rectangles around the detected numpad
for loc in zip(*locations[::-1]):
top_left = loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(main_img, top_left, bottom_right, color=(0, 255, 0), thickness=2)
# If any rectangles are drawn (i.e., numpad is found), save the debug image
if locations[0].size > 0:
debug_image_path = save_path_prefix + "detected_numpad.png"
cv2.imwrite(debug_image_path, main_img)
print(f"Numpad detected and image saved to {debug_image_path}")
return True, top_left, bottom_right # Return the bounding box coordinates of the numpad
return False, None, None # Return False if no numpad is found
def find_numpad_in_image(image_path, template_path, save_path_prefix, threshold=0.8):
# Read the main image and template
main_img = cv2.imread(image_path)
template_img = cv2.imread(template_path)
# Convert images to grayscale
main_gray = cv2.cvtColor(main_img, cv2.COLOR_BGR2GRAY)
template_gray = cv2.cvtColor(template_img, cv2.COLOR_BGR2GRAY)
# Apply template matching
res = cv2.matchTemplate(main_gray, template_gray, cv2.TM_CCOEFF_NORMED)
# Find locations where the matching result exceeds the threshold
locations = np.where(res >= threshold)
# Get the dimensions of the template image
w, h = template_gray.shape[::-1]
# Draw rectangles around the detected numpad
for loc in zip(*locations[::-1]):
top_left = loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(main_img, top_left, bottom_right, color=(0, 255, 0), thickness=2)
# If any rectangles are drawn (i.e., numpad is found), save the debug image
if locations[0].size > 0:
debug_image_path = save_path_prefix + "detected_numpad.png"
cv2.imwrite(debug_image_path, main_img)
print(f"Numpad detected and image saved to {debug_image_path}")
return True, top_left, bottom_right # Return the bounding box coordinates of the numpad
return False, None, None # Return False if no numpad is found
image_path = '5_mob_test.png'
template_path = 'harpy_template_2.png'
save_path_prefix = 'C:/Users/pFFed/PycharmProjects/captcha/'
numpad_found, top_left, bottom_right = find_numpad_in_image(image_path, template_path, save_path_prefix)
if numpad_found:
print(f"Numpad found at: Top Left - {top_left}, Bottom Right - {bottom_right}")
else:
print("Numpad not found in the image.")
# Setup paths and pytesseract executable location
save_path_prefix = 'C:\\Users\\pFFed\\PycharmProjects\\easyko' # Update this path as needed
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# Define coordinates and dimensions for the image capture
x, y = 146, 1272
width, height = 400, 30
# Extract text
def extractMobName():
# screenshot = ImageGrab.grab(bbox=(x, y, x + width, y + height))
screenshot = Image.open("test.png")
debug_image_path = "first_debug_screenshot.png"
screenshot.save(debug_image_path)
extracted_text = process_and_extract_text(screenshot, save_path_prefix)
return extracted_text
# END IMAGE PROCESSING
# INITIALIZATION
def readPixel(x, y):
if x >= 1920:
x = 1919
if y >= 1080:
y = 1079
color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), x, y)
r, g, b = rgbint2rgbtuple(color)
return r, g, b
def checkMonsterHealtLow():
global HP_R
x, y = MONSTER_HP_X, MONSTER_HP_y
r, g, b = readPixel(x, y)
if(r+10<HP_R):
return True
else:
return False
def checkMana():
global SELF_MP_R, SELF_MP_G, SELF_MP_B, SELF_MP_X, SELF_MP_Y
x, y = SELF_MP_X, SELF_MP_Y
r, g, b = readPixel(x, y)
# print("Checking mana with RGB: {}, {}, {} against {}, {}, {}".format(r, g, b, MP_R, MP_G, MP_B))
if r != 0 and g != 0 and b != 0:
return False
# print("MP is above 35%")
else:
# print("Using MP with KEY {}".format(MANA_POT_KEY))
return True
def checkHealth():
if checkMana():
return
global SELF_HP_R, SELF_HP_G, SELF_HP_B, SELF_HP_X, SELF_HP_Y
x, y = SELF_HP_X, SELF_HP_Y
r, g, b = readPixel(x, y)
#print("Checking health with RGB: {}, {}, {} against {}, {}, {}".format( r, g, b, HP_R, HP_G, HP_B))
if r != 0 and g != 0 and b != 0:
return False
#print("\n")
# print("HP is above 40%")
else:
# print("Using HP with KEY {}".format(HEALING_POT_KEY))
return True
def checkPartyMemberHealth(x,y):
r, g, b = readPixel(x, y)
print(f"[{x}, {y}] -> RGB: {r} {g} {b}")
if r == 169 and g == 6 and b == 9:
return False
else:
return True
def clickPartyMember(autohotpy, event, x,y):
autohotpy.moveMouseToPosition(x,y)
time.sleep(0.3)
leftButton(autohotpy, event)
def checkRepair():
global SELF_ITEM_R, SELF_ITEM_G, SELF_ITEM_B, SELF_ITEM_X, SELF_ITEM_Y
x, y = SELF_ITEM_X, SELF_ITEM_Y
r, g, b = readPixel(x, y)
#print("Checking health with RGB: {}, {}, {} against {}, {}, {}".format( r, g, b, HP_R, HP_G, HP_B))
if r < 200:
return False
#print("\n")
# print("HP is above 40%")
else:
# print("Using HP with KEY {}".format(HEALING_POT_KEY))
return True
# def checkPartyMemberHealth(x, y):
# color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), x, y)
# r, g, b = rgbint2rgbtuple(color)
# if r < 75:
# print("Party member [{},{}] is at RGB: {}, {}, {}".format(
# x, y, r, g, b))
def rgbint2rgbtuple(RGBint):
red = RGBint & 255
green = (RGBint >> 8) & 255
blue = (RGBint >> 16) & 255
return (red, green, blue)
def recordColorOfCursorPos():
for i in range(2,0,-1):
print("Recording color in {} seconds".format(i))
time.sleep(1)
x, y = win32api.GetCursorPos()
r,g,b = readPixel(x,y)
print("Recored r,g,b = {}, {}, {} at x,y = [{}, {}]".format(r,g,b,x,y))
return x,y,r,g,b
#
print("Can nereye gelince pot basılsın? (mouse'u üzerinde beklet)")
SELF_HP_X, SELF_HP_Y, SELF_HP_R, SELF_HP_G, SELF_HP_B = recordColorOfCursorPos()
print("Mana nereye gelince pot basılsın? (mouse'u üzerinde beklet)")
SELF_MP_X, SELF_MP_Y, SELF_MP_R, SELF_MP_G, SELF_MP_B = recordColorOfCursorPos()
#print("Ilk party member can nereye gelince party heal atilsin (mouse beklet)")
#time.sleep(1)
#PARTY_FIRST_X, PARTY_FIRST_Y, PARTY_FIRST_R, PARTY_FIRST_G, PARTY_FIRST_B = recordColorOfCursorPos()
#SELF_ITEM_R, SELF_ITEM_G, SELF_ITEM_B = readPixel(PARTY_FIRST_X, PARTY_FIRST_Y)
#print("item rgb {} {} {}".format(SELF_ITEM_R, SELF_ITEM_G, SELF_ITEM_B))
# END INITIALIZATION
# The following function is called when you press ESC.
# autohotpy is the instance that controlls the library, you should do everything through it.
def exitAutoHotKey(autohotpy, event):
print("Stopping with " + event)
autohotpy.stop() # makes the program finish successfully. Thisis the right way to stop it
def waitBetweenKeys():
random_number = random.uniform(0.05, 0.1)
time.sleep(random_number)
def pressZ(autohotpy):
autohotpy.Z.press()
waitBetweenKeys()
return extractMobName()
def leftButton(autohotpy,event):
"""
This function simulates a left click
"""
stroke = InterceptionMouseStroke()
autohotpy.sendToDefaultMouse(stroke)
stroke.state = InterceptionMouseState.INTERCEPTION_MOUSE_LEFT_BUTTON_DOWN
autohotpy.sendToDefaultMouse(stroke)
time.sleep(0.05)
stroke.state = InterceptionMouseState.INTERCEPTION_MOUSE_LEFT_BUTTON_UP
autohotpy.sendToDefaultMouse(stroke)
def rightButtonAndDrag(autohotpy, event):
stroke = InterceptionMouseStroke()
stroke.state = InterceptionMouseState.INTERCEPTION_MOUSE_RIGHT_BUTTON_DOWN
autohotpy.sendToDefaultMouse(stroke)
waitBetweenKeys()
for i in range(750,901,50):
event.x += 50
time.sleep(0.01)
autohotpy.sendToDefaultMouse(event)
stroke.state = InterceptionMouseState.INTERCEPTION_MOUSE_RIGHT_BUTTON_UP
autohotpy.sendToDefaultMouse(stroke)
def torment(autohotpy, event):
global torment_time
if torment_time is None:
torment_time = datetime.now()
print(f"torment_time recorded at: {malice_time}")
autohotpy.Z.press()
time.sleep(0.3)
pressKeyNTimes(autohotpy.N5)
time.sleep(0.2)
leftButton(autohotpy, event)
else:
if datetime.now() - torment_time > timedelta(seconds=15):
print("15 secs have passed since torment_time was recorded.")
time.sleep(0.2)
time.sleep(0.3)
pressKeyNTimes(autohotpy.N5)
time.sleep(0.2)
leftButton(autohotpy, event)
def helishCombo(autohotpy):
autohotpy.R.press()
time.sleep(0.20)
autohotpy.R.press()
time.sleep(0.20)
autohotpy.R.press()
time.sleep(0.10)
autohotpy.R.press()
time.sleep(0.10)
autohotpy.N2.press()
time.sleep(0.10)
kitap_time = None
str_time = None
malice_time = None
torment_time = None
def pressKeyNTimes(key):
random_number = int(random.uniform(2, 8))
for i in range(0, random_number):
key.press()
key.press()
def kitap(autohotpy):
global kitap_time
if kitap_time is None:
kitap_time = datetime.now()
print(f"kitap_time recorded at: {kitap_time}")
pressKeyNTimes(autohotpy.N7)
else:
if datetime.now() - kitap_time > timedelta(minutes=0, seconds=21):
print("4 minutes have passed since kitap_time was recorded.")
pressKeyNTimes(autohotpy.N7)
kitap_time = datetime.now()
def selfStr(autohotpy):
global str_time
if str_time is None:
str_time = datetime.now()
print(f"str_time recorded at: {str_time}")
pressKeyNTimes(autohotpy.N7)
else:
if datetime.now() - str_time > timedelta(minutes=12, seconds=1):
print("12 minutes have passed since str_time was recorded.")
pressKeyNTimes(autohotpy.N7)
str_time = datetime.now()
def malice(autohotpy):
global malice_time
if malice_time is None:
malice_time = datetime.now()
print(f"malice_time recorded at: {malice_time}")
time.sleep(0.2)
autohotpy.Z.press()
autohotpy.F2.press()
pressKeyNTimes(autohotpy.N3)
autohotpy.F1.press()
malice_time = datetime.now()
time.sleep(0.8)
else:
if datetime.now() - malice_time > timedelta(seconds=8):
print("8 secs have passed since malice_time was recorded.")
time.sleep(0.2)
autohotpy.Z.press()
autohotpy.F2.press()
pressKeyNTimes(autohotpy.N3)
autohotpy.F1.press()
malice_time = datetime.now()
time.sleep(0.8)
def sprint(autohotpy):
pressKeyNTimes(autohotpy.N8)
right_drag_time = datetime.now()
def partyHealer(autohotpy, event):
global PARTY_FIRST_X, PARTY_FIRST_Y
partySize = 8
needs = []
for i in range(0,partySize):
needs.append(False)
trueCount = 0
for i in range(0, partySize):
#print(f"Trying to read {(PARTY_FIRST_X, PARTY_FIRST_Y + (i*50))}")
if i == 7:
needs[i] = checkPartyMemberHealth(PARTY_FIRST_X, (PARTY_FIRST_Y + (i * 51) + 2))
else:
needs[i] = checkPartyMemberHealth(PARTY_FIRST_X, PARTY_FIRST_Y + (i * 51))
if needs[i]:
trueCount += 1
print(f"Party member {i} needs heal")
if trueCount > 3:
# party heal
autohotpy.N2.press()
autohotpy.N3.press()
else:
for i in range(0, partySize):
if needs[i]:
clickPartyMember(autohotpy, event, PARTY_FIRST_X, PARTY_FIRST_Y + (i*50))
time.sleep(0.6)
autohotpy.N0.press()
time.sleep(1.62)
def capture_screenshot_to_cv2():
# Capture the screen
screenshot = ImageGrab.grab()
# Convert the screenshot to a numpy array
screenshot_np = np.array(screenshot)
# Convert the color space from BGR (Pillow's default) to RGB, which OpenCV uses
screenshot_cv2 = cv2.cvtColor(screenshot_np, cv2.COLOR_BGR2RGB)
screenshot_filename = 'screenshot_taken_live.png'
cv2.imwrite(screenshot_filename, screenshot_cv2)
return screenshot_cv2
def get_random_point_within_rectangle(top_left, bottom_right):
"""
Returns a random point within the rectangle defined by top_left and bottom_right.
Parameters:
top_left (tuple): A tuple of the form (x, y) representing the top left corner of the rectangle.
bottom_right (tuple): A tuple of the form (x, y) representing the bottom right corner of the rectangle.
Returns:
tuple: A tuple representing a random point within the rectangle.
"""
if top_left[0] >= bottom_right[0] or top_left[1] >= bottom_right[1]:
raise ValueError("Top left must be above and to the left of bottom right.")
random_x = random.randint(top_left[0], bottom_right[0])
random_y = random.randint(top_left[1], bottom_right[1])
return (random_x, random_y)
def leftClick(autohotpy, event, x, y):
# stroke = InterceptionMouseStroke()
# stroke.state = InterceptionMouseState.INTERCEPTION_MOUSE_MOVE
# stroke.x = x
# stroke.y = y
# autohotpy.sendToDefaultMouse(stroke)
# time.sleep(2)
# leftButton(autohotpy, event)
# Simulate a left mouse button click
stroke = InterceptionMouseStroke()
stroke.state = InterceptionMouseState.INTERCEPTION_MOUSE_LEFT_BUTTON_DOWN
autohotpy.sendToDefaultMouse(stroke)
waitBetweenKeys() # Small delay to simulate hold time of a real click
stroke.state = InterceptionMouseState.INTERCEPTION_MOUSE_LEFT_BUTTON_UP
autohotpy.sendToDefaultMouse(stroke)
from PIL import Image, ImageOps, ImageEnhance
def getCoords(autohotpy, event):
# Create a window
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
# Set the window size to 480x480
cv2.resizeWindow('image', 480, 480)
# create trackbars for color change
cv2.createTrackbar('HMin', 'image', 0, 179, nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin', 'image', 0, 255, nothing)
cv2.createTrackbar('VMin', 'image', 0, 255, nothing)
cv2.createTrackbar('HMax', 'image', 0, 179, nothing)
cv2.createTrackbar('SMax', 'image', 0, 255, nothing)
cv2.createTrackbar('VMax', 'image', 0, 255, nothing)
# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)
# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0
coor_top_left = (2374, 269)
coor_bottom_right = (2437, 283)
z_class_type_image = capture_screenshot(coor_top_left, coor_bottom_right)
image = np.array(z_class_type_image)
cv2.imwrite("Coords_img.png", image)
output = image
wait_time = 33
while (1):
coor_top_left = (2374, 269)
coor_bottom_right = (2437, 283)
z_class_type_image = capture_screenshot(coor_top_left, coor_bottom_right)
image = np.array(z_class_type_image)
cv2.imwrite("Coords_img.png", image)
# get current positions of all trackbars
hMin = cv2.getTrackbarPos('HMin', 'image')
sMin = cv2.getTrackbarPos('SMin', 'image')
vMin = cv2.getTrackbarPos('VMin', 'image')
hMax = cv2.getTrackbarPos('HMax', 'image')
sMax = cv2.getTrackbarPos('SMax', 'image')
vMax = cv2.getTrackbarPos('VMax', 'image')
# Set minimum and max HSV values to display
lower = np.array([hMin, sMin, vMin])
upper = np.array([hMax, sMax, vMax])
# Create HSV Image and threshold into a range.
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, lower, upper)
output = cv2.bitwise_and(image, image, mask=mask)
# Print if there is a change in HSV value
if ((phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax)):
print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (
hMin, sMin, vMin, hMax, sMax, vMax))
phMin = hMin
psMin = sMin
pvMin = vMin
phMax = hMax
psMax = sMax
pvMax = vMax
# Display output image
cv2.imshow('image', output)
z_class = pytesseract.image_to_string(output)
z_class = z_class.strip()
print(f"Coords: {z_class}")
# Wait longer to prevent freeze for videos.
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
coor_top_left = (2374, 269)
coor_bottom_right = (2437, 283)
z_class_type_image = capture_screenshot(coor_top_left, coor_bottom_right)
z_class_type_image_np = np.array(z_class_type_image)
# Convert the color space from BGR (Pillow's default) to RGB, which OpenCV uses
z_class_type_image_np_confirmed = cv2.cvtColor(z_class_type_image_np, cv2.COLOR_BGR2RGB)
cv2.imwrite("Coords.png", z_class_type_image_np_confirmed)
z_class = pytesseract.image_to_string(z_class_type_image_np_confirmed)
z_class = z_class.strip()
print(f"Coords: {z_class}")
def preprocess_image_for_ocr(image):
# Convert the image to grayscale
gray_image = ImageOps.grayscale(image)
# Enhance the contrast of the image
enhancer = ImageEnhance.Contrast(gray_image)
enhanced_image = enhancer.enhance(2.0) # Adjust the factor to get the desired contrast
# Invert the image colors if necessary
inverted_image = ImageOps.invert(enhanced_image)
# Convert image to binary (black and white) using a threshold
threshold = 200 # You might need to adjust this threshold
binary_image = inverted_image.point(lambda p: p > threshold and 255)
return binary_image
def getZMobName(autohotpy, event):
autohotpy.Z.press()
mob_top_left = (762, 78)
mob_bottom_right = (812, 92)
z_class_type_image = capture_screenshot(mob_top_left, mob_bottom_right)
z_class_type_image_np = np.array(z_class_type_image)
# Convert the color space from BGR (Pillow's default) to RGB, which OpenCV uses
z_class_type_image_np_confirmed = cv2.cvtColor(z_class_type_image_np, cv2.COLOR_BGR2RGB)
#cv2.imwrite("3535.png", z_class_type_image_np_confirmed)
#z_class = pytesseract.image_to_string(z_class_type_image_np_confirmed)
#z_class = z_class.strip()
# Assuming you have an image in numpy array format
# You can convert it to a PIL image for preprocessing
z_class_type_image_np_confirmed_pil = Image.fromarray(z_class_type_image_np_confirmed)
# Preprocess the image
preprocessed_image = preprocess_image_for_ocr(z_class_type_image_np_confirmed_pil)
preprocessed_image = np.array(preprocessed_image)
# Use pytesseract to do OCR on the preprocessed image
z_class = pytesseract.image_to_string(preprocessed_image)
z_class = z_class.strip()
print(f"Z ClassType: {z_class}")
return z_class
def getZEnabled(autohotpy, event):
mob_top_left = (1111, 63)
mob_bottom_right = (1185, 87)
z_class_type_image = capture_screenshot(mob_top_left, mob_bottom_right)
z_class_type_image_np = np.array(z_class_type_image)
# Convert the color space from BGR (Pillow's default) to RGB, which OpenCV uses
z_class_type_image_np_confirmed = cv2.cvtColor(z_class_type_image_np, cv2.COLOR_BGR2RGB)
cv2.imwrite("z_clsas222s_img.png", z_class_type_image_np_confirmed)
z_class = pytesseract.image_to_string(z_class_type_image_np_confirmed)
z_class = z_class.strip()
def preprocess_image_for_ocr(image):
# Convert the image to grayscale
gray_image = ImageOps.grayscale(image)
# Enhance the contrast of the image
enhancer = ImageEnhance.Contrast(gray_image)
enhanced_image = enhancer.enhance(2.0) # Adjust the factor to get the desired contrast
# Invert the image colors if necessary
inverted_image = ImageOps.invert(enhanced_image)
# Convert image to binary (black and white) using a threshold
threshold = 200 # You might need to adjust this threshold
binary_image = inverted_image.point(lambda p: p > threshold and 255)
return binary_image
# Assuming you have an image in numpy array format
# You can convert it to a PIL image for preprocessing
z_class_type_image_np_confirmed_pil = Image.fromarray(z_class_type_image_np_confirmed)
# Preprocess the image
preprocessed_image = preprocess_image_for_ocr(z_class_type_image_np_confirmed_pil)
preprocessed_image = np.array(preprocessed_image)
cv2.imwrite("mob_preprocessed.png", preprocessed_image)
# Use pytesseract to do OCR on the preprocessed image
z_class = pytesseract.image_to_string(preprocessed_image)
z_class = z_class.strip()
print(f"Z ClassType: {z_class}")
autohotpy.Z.press()
r,g,b = readPixel(1064, 75)
if r == 255 and g == 255 and b == 255:
if z_class != "Priest" and z_class != "Warrior" and z_class != "Rogue" and z_class != "Mage":
return True
else:
return False
else:
return False
coor_top_left = (2372, 367)
coor_bottom_right = (2437, 87)
def capture_screenshot(top_left, bottom_right):
# Capture the entire screen
screenshot = ImageGrab.grab()
# Crop the screenshot to the specified area
# The crop method takes a tuple of (left, top, right, bottom)
cropped_screenshot = screenshot.crop(top_left + bottom_right)
# Save or process the cropped screenshot as needed
cropped_screenshot.save("cropped_screenshot.png")
return cropped_screenshot
# For displaying directly in Jupyter, you can use the display function
# display(cropped_screenshot)
import re
def remove_non_numeric(s):
# This regex pattern matches any character that is NOT a digit
return re.sub(r'[^\d]', '', s)
#class CaptchaManager:
offsets = [(-134, 2, 100, 40), #Captcha
(-114, 132, 10, 5), #Minus
(-80, 132, 14, 4), #Plusr
(6, 67, 92, 27), #Continue
(-301, -114, 427, 523)
]
def __init__(self):
pass
def manageImageExtractionFromOffset(self, screenshot, top_left, index):
if top_left:
# Define the offsets for each additional image
(offset_x, offset_y, width, height) = self.offsets[index]
new_top_left_x = top_left[0] + offset_x
new_top_left_y = top_left[1] + offset_y
# Make sure the coordinates are within the screenshot boundaries
new_top_left_x = max(new_top_left_x, 0)
new_top_left_y = max(new_top_left_y, 0)
new_bottom_right_x = min(new_top_left_x + width, screenshot.shape[1])
new_bottom_right_y = min(new_top_left_y + height, screenshot.shape[0])
# Extract the image using calculated coordinates
extracted_image = screenshot[new_top_left_y:new_bottom_right_y, new_top_left_x:new_bottom_right_x]
return extracted_image, (new_top_left_x, new_top_left_y), (new_bottom_right_x, new_bottom_right_y)
def getCaptchaImage(self, screenshot, top_left):
return self.manageImageExtractionFromOffset(screenshot, top_left, 0)
def getMinusImage(self, screenshot, top_left):
return self.manageImageExtractionFromOffset(screenshot, top_left, 1)
def getPlusImage(self, screenshot, top_left):
return self.manageImageExtractionFromOffset(screenshot, top_left, 2)
def getContinueImage(self, screenshot, top_left):
return self.manageImageExtractionFromOffset(screenshot, top_left, 3)
def getMacroDetectionImage(self, screenshot, top_left):
return self.manageImageExtractionFromOffset(screenshot, top_left, 4)
def isCaptchaOnScreen(self, autohotpy, event) -> bool:
captcha_template = "captcha_reload.png"
screenshot = capture_screenshot_to_cv2()
locations, template_width, template_height = match_template(screenshot, cv2.imread(captcha_template))
if len(locations[0]) > 0:
print("Captcha found on screen! Resolving...")
# Get the first matching location
top_left = next(zip(*locations[::-1]), None)
if top_left:
captcha_image, _, _ = self.getCaptchaImage(screenshot, top_left)
text = pytesseract.image_to_string(captcha_image, config='--psm 6')
text = text.strip()
number_to_enter = remove_non_numeric(text)
print(f"Extracted text: {number_to_enter}")
minus_img, minus_top_left, minus_bottom_right = self.getMinusImage(screenshot, top_left)
plus_img, plus_top_left, plus_bottom_right = self.getPlusImage(screenshot, top_left)
cont_img, continue_top_left, continue_bottom_right = self.getContinueImage(screenshot, top_left)
macro_img, macro_top_left, macro_bottom_right = self.getMacroDetectionImage(screenshot, top_left)
cv2.imwrite(f"continue_image.png", cont_img)
cv2.imwrite(f"minus_img.png", minus_img)
cv2.imwrite(f"plus_image.png", plus_img)
cv2.imwrite(f"captcha_image.png", captcha_image)
cv2.imwrite(f"macro_img.png", macro_img)
x, y = get_random_point_within_rectangle(plus_top_left, plus_bottom_right)
print(f"clicking {x}, {y} in 5 seconds..")
leftClick(autohotpy, event, x,y)
autohotpy.S.press()
def matchWindow():
return_obj = None
to_match_templates = ["0.png", "-1.png", "1.png", "-2.png", "2.png"]
main_img = macro_img
for del_template in to_match_templates:
resolved_img = cv2.imread(del_template)
del_locations, del_temp_w, del_temp_h = match_template(macro_img, resolved_img)
# Draw rectangles around the detected numpad
for loc in zip(*del_locations[::-1]):
top_left = loc
bottom_right = (top_left[0] + del_temp_w, top_left[1] + del_temp_h)
cv2.rectangle(main_img, top_left, bottom_right, color=(0, 255, 0), thickness=2)
# If any rectangles are drawn (i.e., numpad is found), save the debug image
if del_locations[0].size > 0:
debug_image_path = f"{del_template}_detected_numpad.png"
cv2.imwrite(debug_image_path, main_img)
print(f"Numpad detected and image saved to {debug_image_path}")
return_obj = del_template
return return_obj, top_left, bottom_right