def solve_boss_behind(alw_sleep=False):
    global im, stx, sty, im_gray, height, width, moved
    if exist("enemy_boss") != (-1, -1): return
    if exist("yingji") and not moved:
        stpos = (im.shape[0] // 2, im.shape[1] // 2)
        edpos = (stpos[0] + 37, stpos[1] + 84)
        stpos = trans_to_screenpos(stpos)
        edpos = trans_to_screenpos(edpos)
        mouse_drag(stpos, edpos, delay=8)
        moved = True
        im, stx, sty = get_im_xy()
        im_gray = color.rgb2gray(im)
        height = im.shape[0]
        width = im.shape[1]
        if exist("enemy_boss") != (-1, -1) or exist("enemy_boss") != (
                -1, -1) or exist("enemy_boss") != (
                    -1, -1) or exist("enemy_boss") != (
                        -1, -1) or exist("enemy_boss") != (-1, -1):
            return
        pos = (182, 766)
        pos = trans_to_screenpos(pos)
        mouse_click(pos, delay=20)
        im, stx, sty = get_im_xy()
        im_gray = color.rgb2gray(im)
        height = im.shape[0]
        width = im.shape[1]
        if exist("enemy_boss") != (-1, -1) or exist("enemy_boss") != (
                -1, -1) or exist("enemy_boss") != (
                    -1, -1) or exist("enemy_boss") != (
                        -1, -1) or exist("enemy_boss") != (-1, -1):
            return
    if alw_sleep:
        mail_to_me("solve boss behind fail,begin sleep")
        time.sleep(10000)
def simple_CU():
    if decide_fleet(): return
    print('exist yingji')
    if exist("yingji") != (-1, -1):
        if exist("guibi") != (-1, -1):
            match_order = labels_order
        else:
            if cjcnt >= 7: solve_boss_behind()
            if cjcnt > 9:
                mail_to_me("boss behind")
                solve_boss_behind(alw_sleep=True)
            match_order = objs_order
    else:
        match_order = labels_order
    for name in match_order:
        if name != "yingji" and click_lab(name): return
def simple_CU2():
    if click_lab("enemy_boss") or click_lab("enemy_boss") or click_lab(
            "enemy_boss") or click_lab("enemy_boss"):
        return
    if exist("yingji") != (-1, -1):
        if exist("guibi") != (-1, -1):
            match_order = labels_order
        else:
            if cjcnt >= 7: solve_boss_behind()
            if cjcnt > 9:
                mail_to_me("boss behind")
                solve_boss_behind(alw_sleep=True)
            match_order = objs_order
    else:
        match_order = labels_order
    for name in match_order:
        if name != "yingji" and click_lab(name): return
def log_click(lab):
    global timelast
    freq = dict()
    if not os.path.exists("freq.txt"): open("freq.txt", "w").close()
    with open("freq.txt", "r") as f:
        lines = f.readlines()
        for line in lines:
            name, num = line.split()
            freq[name] = int(num)
    if lab in freq:
        freq[lab] += 1
    else:
        freq[lab] = 1
    dic2str = ""
    with open("freq.txt", "w") as f:
        for name, num in freq.items():
            dic2str += name + " " + str(num) + "\n"
        f.write(dic2str)
    if (time.time() - timelast) > 20 * 60:
        mail_to_me(dic2str)
        timelast = time.time()
def click_lab(lab):
    global cjcnt, search_fail, moved
    if lab not in im_labels_gray: return False
    #result=feature.match_template(im_gray,im_labels_gray[lab]);
    x, y = exist(lab)
    print(x, y)
    if x != -1:
        print("click", lab)
        if lab == "xingneng": x -= 100
        if lab == "zhenglikuozhan":
            mail_to_me("zhenglikuozhan,begin sleep")
            time.sleep(10000)
        if lab == "3-4":
            cjcnt = 0
            moved = False
        if lab == "chujiinmap": cjcnt += 1
        search_fail = 0
        click_pos = trans_to_screenpos((x, y))
        mouse_click(click_pos, 0.5)
        #log_click(lab)
        return True
    return False
    height = im.shape[0]
    width = im.shape[1]
    print("*", end="")
    sys.stdout.flush()
    search_fail += 1
    # x,y=exist("chujiinmap")
    # if x!=-1:
    # print("x,y ",x,y)
    # im_test=im.copy()
    # im_test[x:x+10,y:y+10]=[255,0,0]
    # debugim(im_test)
    # break
    try:
        simple_CU()
        if search_fail > 20:
            mail_to_me("search_fail>20")
            solve_boss_behind(alw_sleep=True)
    except Exception as e:
        print(e)
        print("Error")
        traceback.print_exc()
        try:
            mail_to_me(str(traceback.format_exc()) + str(e))
        except Exception as e2:
            pass
    time.sleep(1.5)

    # DEBUG
im_match_test = im.copy()

for i in range(grid_h):