Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
15 kB
1
Indexable
Never
from inspect import walktree
from pickle import TRUE
from queue import Empty
from pyautogui import * 
import pyautogui 
import time 
import keyboard 
import random
import decimal
import os
import win32api, win32con #, win32gui
import cv2
from ahk import AHK
import pydirectinput
from time import sleep, perf_counter
from threading import Thread

ahk = AHK()

def get_window_coordinates():
    maple_window = pyautogui.getWindowsWithTitle("MapleMS")[0]
    print(get_window_coordinates)
    return maple_window.box
    
def logout(maple_window_box):
    while True:
        if not pyautogui.locateOnScreen("escape_menu.PNG", region=maple_window_box):
            pyautogui.press("esc")
        time.sleep(1)
        
        ahk.click(mx+1211, my+684)    
        sleep(3)
        os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")
        
win = ahk.find_window(title=b'MapleMS')
mx, my = 300, 300
win.move(mx, my, width=1286, height=749)

win.activate()
sleep(0.2)

def scan_bot_map():
    try:
        return list(pyautogui.locateCenterOnScreen('bot_map.png', region=(mx+30, my+60, 340, 170), grayscale=False, confidence=0.9))
    except Exception:
        pass

def scan(img):
    try: # list(pyautogui.locateAllOnScreen('stop.png')) becareful this is putting [0][0] this is for mob scanning mostly?
        return list(pyautogui.locateAllOnScreen(img, region=(mx, my+260, 1280, 340), grayscale=True, confidence=0.7)) #1580, 900
    except Exception:
        pass
   
def scanmyself(currentlocation):
    currentlocation = cancel_exception(pyautogui.locateCenterOnScreen(currentlocation, region=(mx, my+260, 1280, 340), grayscale=True, confidence=0.7))
    if currentlocation == 0:
        currentlocation = cancel_exception(pyautogui.locateCenterOnScreen('look_left.png', region=(mx, my+260, 1280, 340), grayscale=True, confidence=0.7))
        return currentlocation
    else:
        return currentlocation
            
def recovery(hp, mp):
    if hp is True: # trying to improve using ocr
        hpfound = pyautogui.pixelMatchesColor(570, 1033, (190, 190, 190))
        while hpfound == True:
            print("HP is less than 50%, using HP pot\n")
            ahk.key_down('Del')
            ahk.key_up('Del') 
            sleep(.5)
            hpfound = pyautogui.pixelMatchesColor(570, 1033, (190, 190, 190))
            
    if mp is True:
        mpfound = pyautogui.pixelMatchesColor(680, 1033, (190, 190, 190))
        while mpfound == True:
            print("MP is less than 50%, using MP pot\n")
            ahk.key_down('PgDn')
            ahk.key_up('PgDn') 
            sleep(1)
            mpfound = pyautogui.pixelMatchesColor(680, 1033, (190, 190, 190))
    
# commands isn't working yet...            
def feed_pet(img): # pet region 550, 500, 1570, 900
    try:
        pet_stats = pyautogui.locateCenterOnScreen(img, region=(mx, my+260, 1280, 340), grayscale=True, confidence=0.8)
        # a = cancel_exception(pet_stats)
        # print(pet_stats)
    
        if pet_stats[0] > 0:
            ahk.key_press('F8')
            sleep(5)
        if pet_stats[0] == None:
            print("Pet is not hungry - Trying to command...\n")
        # Pet command: 
            pet_commands = ["sit", "poop", "no", "bad", "badboy", "cutie", "adorable", "cute", "pretty", "iloveyou", "loveyou"]
            ahk.key_down('Enter')
            sleep(0.15)
            ahk.key_up('Enter') 
            sleep(0.5)
            ahk.type(random.choice(pet_commands))
            sleep(1)       
            ahk.key_down('Enter')
            sleep(0.15)
            ahk.key_up('Enter')
            sleep(0.15)
            ahk.key_down('Enter')
            sleep(0.15)
            ahk.key_up('Enter')
            sleep(0.1) 
    except Exception:
        pass

# not using it, not working properly...  
# def att_mob(direction):
#     # es = pyautogui.locateCenterOnScreen(img, region=(578, 617, 1341, 869), grayscale=True, confidence=0.7)
#     # mob = scan(mob)
#     # mob = cancel_exception(mob)
#     # mycur = scanmyself('look_right.png')
#     try:
#         if direction == 'right':
            
#             pydirectinput.keyDown('right')
#             pydirectinput.keyUp('right')
            
#             pydirectinput.keyDown('c')

#             pydirectinput.keyUp('c')
#             pydirectinput.keyDown('c')

#             pydirectinput.keyUp('c')
#             sleep(0.01)
#         elif direction == 'left':
            
#             pydirectinput.keyDown('left')
#             pydirectinput.keyUp('left')
            
#             pydirectinput.keyDown('c')

#             pydirectinput.keyUp('c')
#             pydirectinput.keyDown('c')

#             pydirectinput.keyUp('c')
#             sleep(0.01)
#         else:
#             tele()
#     except Exception:
#         pass
  
# not really using this for image detection, only works when mobs are detected and not in body...  
# def definemylocation(mob, mob1):
#     # mob = cancel_exception(scan(mob))
    
#     try:
#         gathered = scan(mob)
#         gathered1 = scan(mob1)      
#         gathered.sort()
#         gathered1.sort()
#         # print(gathered, gathered1)
#         myList = [gathered[0][0], gathered1[0][0]]
#         m1 = cancel_exception(gathered[0][0])
#         m2 = cancel_exception(gathered1[0][0])
#         print(myList, mycur)
#         s = min(myList, key=lambda x:abs(x-mycur))
#         # ahk.mouse_move(x=x, y=gathered[0][1], speed=2, blocking=True)
     
#         if m1 != 0 or m2 != 0:
#             # if mycur == 0:
#             #     print("I unable to find myself")
#             #     randomwalk()
#             # else:
#             #     randomwalk()
                
#                 if mycur < s:
#                     close = s - mycur
#                     if close <= 300:
#                         direction = 'Mob close within x300 facing right' 
#                         print(direction, mycur, s)
#                         att_mob('right')
#                 else:
#                     close = mycur - s
#                     if close <= 300:
#                         direction = 'Mob close within x300 facing left'
#                         print(direction, mycur, s)
#                         directionSend = 'left'
#                         att_mob('left')
        
#         else:
#             randomwalkv2()   
#     except Exception:
#         pass

# maybe for mage class only    
# def tele():
#     direction = ["left", "right"]
#     choosen = random.choice(direction)
#     sleep(0.05)
#     pydirectinput.keyDown(direction)
#     sleep(0.25)
#     pydirectinput.keyDown('x')
#     sleep(0.15)
#     pydirectinput.keyUp('x')
#     sleep(0.015)
#     pydirectinput.keyUp(direction)
#     sleep(0.05)
#     print(f"I am going {direction}")

# removing since v2 is better?
# def randomwalk():
#     if mycur < 900:
#         direction = "right"
#     elif mycur > 1100:
#         direction = "left"
#     else:
#         direction = ["left", "right"]
#         direction = random.choice(direction)
#     # direction = ["left", "right"]
#     holding = random.randint(80, 200)/100
#     sleep(0.05)
#     print(f'Mob not found, walking {direction}') 
#     pydirectinput.keyDown(direction)
#     sleep(holding)
#     pydirectinput.keyUp(direction)
#     sleep(0.5)
  

def cancel_exception(x):
    try:
        if x == None:
            return 0
        if x[0] > 0:
            return (int(x[0]))
        # else:
        #     print(x)
    except Exception:
        pass      

# future project, can set arrays for buttons
def refresh_buffs(btn):
    for j in btn:
        print(f"Pressing {j}")
        pydirectinput.keyDown(j)
        sleep(1.5)
        pydirectinput.keyUp(j)
        # sleep(0.5)
        # pydirectinput.keyDown("shift")
        # sleep(0.1)
        # pydirectinput.keyUp("shift")
        # sleep(0.5)
        # pydirectinput.keyDown("ctrl")
        # sleep(0.1)
        # pydirectinput.keyUp("ctrl")
                
def walk(direction):
    if previous > 2:
        direction = direction
    else:
        randomwalkv2(1)
    holding = random.randint(200, 350)/100
    
    print(f"I am going {direction} for {holding} seconds")
    pydirectinput.keyDown(direction)
    sleep(holding)
    if jump_att == True:
        ahk.key_press('space')
    if jump_att == True:
        for x in range(att_times):
            print(f"I am attacking for {x}times")
            pydirectinput.keyDown('c')
            pydirectinput.keyUp('c')
            sleep(0.05)
    pydirectinput.keyUp(direction)

def randomwalkv2(n):
    if n > 2:
        previous = 0 
    direction = ["left", "right"]
    direction = random.choice(direction)
    holding = random.randint(80, 180)/100     
    
    print(f'Walking to {direction}')
    pydirectinput.keyDown(direction)
    sleep(holding)
    if jump_att == True:
        ahk.key_press('space') 
    if jump_att == True:
        for x in range(att_times):
            print(f"I am attacking for {x+1} times")
            pydirectinput.keyDown('c')
            # # pydirectinput.keyDown('space')
            # # sleep(0.1)
            # # pydirectinput.keyUp('space')
            
            # 
            pydirectinput.keyUp('c')
            sleep(0.01)
    pydirectinput.keyUp(direction)

# define some settings for your bot
maple_window_box = get_window_coordinates()    
print(maple_window_box)         
hours = 3600
loops = 0
previous = 0 # testing this loops

stopped = False
start_time = time.time()
run_time = 3*hours # seconds, default 3 hours  
buffs = True # if you want buffs before starting 
buffs_btn = ["end", "shift"] # put correctly with "" and button inside e.g "end"
jump_att = True # if you want jump and att
att_times = 12 # how many times you want to attack, especially for Aran
hp_pot = True # use hp pot? default is 50%
mp_pot = True # use mp pot? default is 50%
has_pet = True # do you want to feed pet? put pet food on F8
pet_sad = 'pet_sad.png' # find pet's sad emotion, put .png
pause = False

# initial steps
refresh_buffs(buffs_btn)
feed_pet(pet_sad)

class myClassPots(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        if stopped:
            print("Stopped detecting HP/MP")
            self.stop()
        while hp_pot or mp_pot == True:
            sleep(3)
            recovery(hp_pot, mp_pot) # regen potions, first is HP and second is MP
            print("Detecting HP/MP...")

class myClassPet(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        if stopped:
            print("Stopped detecting pet stats")
            self.stop()
        while has_pet == True:
            sleep(60)
            feed_pet(pet_sad)
            print("Detecting pet stats...")
        
                        
myClassPots() # starts detecting HP/MP different CPUs
myClassPet() # start detecting pet stats from different CPUs
                         
while True:
    if keyboard.is_pressed('1'):  # if key '1' is pressed, good only when you need to take over 
            if pause != True:
                print('You pressed 1, pausing 60s')
                sleep(60)
                pause = True
                break
    if keyboard.is_pressed('`'):  # if key '`' is pressed 
            print('You pressed `, finish loops')
            stopped = True
            quit()
            break
    if time.time() > start_time + run_time:
            logout(maple_window_box)
            print(f" Bot Successfully ran for {loops} loops. ")
            break
    if loops > 0:
            if loops % 60 == 0:
                refresh_buffs(buffs_btn)
    
    #mycur = scanmyself('my_tag_aran.png') # not using, but this is your char medal screenshot

    try:
        bot_map = scan_bot_map() # bottom of the mini map
        flag = 0
        xt = mx+5 # top left corner of the mini map
        yt = my+50
        xb = bot_map[0] # x of the bottom of the mini map
        yb = bot_map[1]
        print(xb, yb)
        percentleft = 2
        percentright = 8
        # print(f"I am inside try, ref {previous}")
        
        pic = pyautogui.screenshot(region=(xt, yt, xb, yb)) # search

        width, height = pic.size # screenshot size

        for x in range(0, width, 2): # scan every 2 pixels - width
            for y in range(0, height, 2): # scan every 2 pixels - height

                r, g, b = pic.getpixel((x, y)) # define rgb

                if r == 255 and g == 255 and b == 136: # yellow dot, myself on mini map
                    flag = 1
                    # click(x+xt, y+yt)
                    mypos = x+xt # my x
                    # 384 468 e.g location
                    # print(xt, xb)
                    cal1 = xb - xt
                    # print(cal1)
                    cal2 = cal1 / 10
                    # # print(cal2)
                    # print(sorted([cal2*2, cal2*4]))
                    # print(cal2*2)
                    firsthalf =(xt + cal2*percentleft) # this is within 20%
                    secondhalf = (xt + cal2*percentright) # this is within 80%
                    if mypos < firsthalf:
                        print(f"I am at left side of the map at x{firsthalf}")
                        previous = +1
                        print(f"i am inside decision, ref: right, now showing: {previous}")
                        walk("right")
                    elif mypos > secondhalf:
                        print(f"I am at right side of the map at x{secondhalf}")
                        previous = +1
                        print(f"i am inside decision, ref: left, now showing: {previous}")
                        walk("left")
                        
                    
                    break

            if flag == 1:
                break
    except Exception:
        pass
    
   
    randomwalkv2(0)

    
    loops += 1
    print(f"Loops completed: {loops}")    


# get windows size
# def callback(hwnd, extra):
#     rect = win32gui.GetWindowRect(hwnd)
#     x = rect[0]
#     y = rect[1]
#     w = rect[2] - x
#     h = rect[3] - y
#     print("Window %s:" % win32gui.GetWindowText(hwnd))
#     print("\tLocation: (%d, %d)" % (x, y))
#     print("\t    Size: (%d, %d)" % (w, h))