def set_dest(): """Issues a 'set destination' command for the lowest-numbered bookmark that isn't blacklisted.""" target_dest = 1 dest = lo.mlocate('./img/dest/dest' + (str(target_dest)) + '.bmp', conf=0.98, loctype='c') while dest == 0: target_dest += 1 logging.debug('looking for dest ' + (str(target_dest))) dest = lo.mlocate('./img/dest/dest' + (str(target_dest)) + '.bmp', conf=0.98, loctype='c') if dest != 0: logging.debug('setting destination waypoint') (x, y) = dest pag.moveTo( (x + (random.randint(-1, 200))), (y + (random.randint(-3, 3))), mouse.duration(), mouse.path()) mouse.click_right() pag.moveRel((0 + (random.randint(10, 80))), (0 + (random.randint(20, 25))), mouse.duration(), mouse.path()) mouse.click() time.sleep(float(random.randint(1000, 2000)) / 1000) return
def blacklist_set_bookmark(target_site): """Blacklist a specific bookmark by changing its name.""" # TODO: possibly blacklist bookmarks instead by deleting them, which # could lead to fewer bugs as sometimes the 'rename bookmark' window # does not behave consistently. logging.debug('blacklisting bookmark ' + (str(target_site))) bookmark_to_blacklist = pag.locateCenterOnScreen( ('./img/dest/at_dest' + (str(target_site)) + '.bmp'), confidence=conf, region=(originx, originy, windowx, windowy)) (bookmark_to_blacklistx), (bookmark_to_blacklisty) = bookmark_to_blacklist pag.moveTo((bookmark_to_blacklistx + (random.randint(-1, 200))), (bookmark_to_blacklisty + (random.randint(-3, 3))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(1000, 2000)) / 1000) # Multiple clicks to focus the text input field. mouse.click() time.sleep(float(random.randint(1000, 2000)) / 1000) mouse.click() time.sleep(float(random.randint(5, 50)) / 1000) mouse.click() time.sleep(float(random.randint(3000, 4000)) / 1000) key.keypress('home') key.keypress('b') time.sleep(float(random.randint(0, 1000)) / 1000) key.keypress('enter') return 1
def select_overview_tab(tab): """Switches to the specified overview tab. If the specified tab is already selected, this function does nothing. Assumes the default overview configuration.""" logging.debug('focusing ' + (str(tab)) + ' tab') # Requires very high confidence since the overview tab buttons look only # slightly different when selected. selected = lo.mlocate('./img/overview/' + (str(tab)) + '_overview_tab_selected.bmp', conf=0.998, grayscale=True) if selected != 0: logging.debug('tab ' + (str(tab)) + ' already selected') return 1 elif selected == 0: unselected = lo.mlocate('./img/overview/' + (str(tab)) + '_overview_tab.bmp', loctype='co', grayscale=True) if unselected != 0: (x, y) = unselected pag.moveTo((x + (random.randint(-12, 12))), (y + (random.randint(-6, 6))), mouse.duration(), mouse.path()) mouse.click() return 1 elif unselected == 0: logging.error('unable to find overview tabs') return 0
def drag_items_from_ship_inv(): """Clicks and drags all items from ship inventory to station inventory.""" (x1, y1) = lo.mlocate('./img/indicators/station_inv_name.bmp', loctype='c') (x2, y2) = lo.mlocate('./img/buttons/station_inv.bmp', loctype='c') pag.moveTo((x1 + (random.randint(-5, 250))), (y1 + (random.randint(10, 25))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(0, 1000)) / 1000) pag.mouseDown() time.sleep(float(random.randint(0, 1000)) / 1000) pag.moveTo( (x2 + (random.randint(-15, 60))), (y2 + (random.randint(-10, 10))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(0, 1000)) / 1000) pag.mouseUp() logging.debug('moved all item stacks from ship inventory') return
def warp_to_local_bookmark(target_site_num): """Tries warping to the provided target bookmark, assuming the bookmark is in the current system. If the ship is already at or near the requested bookmark, return the function.""" # Confidence must be >0.95 because script will confuse 6 with 0. target_site_bookmark = lo.mlocate('./img/dest/at_dest' + (str(target_site_num)) + '.bmp', conf=0.98, loctype='c') if target_site_bookmark != 0: (x, y) = target_site_bookmark pag.moveTo( (x + (random.randint(10, 200))), (y + (random.randint(-3, 3))), mouse.duration(), mouse.path()) mouse.click_right() approach_location = lo.mlocate( './img/buttons/detect_warp_to_bookmark.bmp', conf=0.90) # If the 'approach location' option is found in the right-click # menu, the ship is already near the bookmark. if approach_location != 0: logging.debug('already at bookmark ' + (str(target_site_bookmark))) key.keypress('esc') # Close right-click menu. return 0 # If the 'approach location' option is not found, look for a 'warp # to' option and select it. elif approach_location == 0: warp_to_site = lo.mlocate('./img/buttons/warp_to_bookmark.bmp', conf=0.90, loctype='c') if warp_to_site != 0: logging.info('warping to bookmark ' + (str(target_site_bookmark))) pag.moveRel((0 + (random.randint(10, 80))), (0 + (random.randint(10, 15))), mouse.duration(), mouse.path()) mouse.click() time.sleep(float(random.randint(1500, 1800)) / 1000) return 1 elif warp_to_site == 0: logging.error('unable to warp to target site, is ship docked?') return 0
def set_home(): """Sets destination to the first bookmark beginning with '000'.""" home = lo.mlocate('./img/dest/dest0.bmp', loctype='c') if home != 0: logging.debug('setting home waypoint') (x, y) = home pag.moveTo( (x + (random.randint(-1, 200))), (y + (random.randint(-3, 3))), mouse.duration(), mouse.path()) mouse.click_right() pag.moveRel((0 + (random.randint(10, 80))), (0 + (random.randint(20, 25))), mouse.duration(), mouse.path()) mouse.click() return 1 else: logging.error('could not find home waypoint!') return 0
def warp_to_waypoint(): """Clicks on the current waypoint and uses the warp hotkey to warp to waypoint. Currently only supports warping to stargate and station waypoints.""" # TODO: add support for warping to citadels and engineering complexes logging.debug('looking for waypoints') # Speed up image searching by looking within overview only. This # obviously requires the user to place the overview on the right side of # the client window. for tries in range(1, 15): stargate = lo.mlocate('./img/overview/stargate_waypoint.bmp', conf=0.96) if stargate != 0: logging.debug('found stargate waypoint') (x, y) = stargate pag.moveTo((x + (random.randint(-8, 30)))), \ (y + (random.randint(-8, 8))), \ mouse.duration(), mouse.path() mouse.click() key.keypress('d') # 'dock / jump' hotkey. # Move mouse to the left side of the client to prevent # tooltips from interfering with image searches. mouse.move_to_neutral() return 2 station = lo.mlocate('./img/overview/station_waypoint.bmp', conf=0.96) if station != 0: logging.debug('found station waypoint') (x, y) = station pag.moveTo((x + (random.randint(-8, 30))), (y + (random.randint(-8, 8))), mouse.duration(), mouse.path()) mouse.click() key.keypress('d') mouse.move_to_neutral() return 2 if stargate == 0 and station == 0: time.sleep(float(random.randint(500, 1500)) / 1000) logging.debug('looking for waypoints ' + (str(tries))) logging.error('no waypoints found') return 0
def focus_client(): """Clicks on a blank area in the left half of the client window, assuming user has properly configured the UI.""" logging.debug('focusing client') pag.moveTo((originx + (random.randint(50, 300))), (originy + (random.randint(300, 500))), mouse.duration(), mouse.path()) mouse.click() return 1
def dock_at_local_bookmark(): """Docks at the first bookmark beginning with a '0' in its name, assuming it's in the same system as you and the bookmark is a station.""" dock = lo.mlocate('./img/dest/at_dest0.bmp', loctype='c') if dock != 0: (x, y) = dock pag.moveTo( (x + (random.randint(-1, 200))), (y + (random.randint(-3, 3))), mouse.duration(), mouse.path()) mouse.click_right() pag.moveRel((0 + (random.randint(10, 80))), (0 + (random.randint(35, 40))), mouse.duration(), mouse.path()) # Sleep used to fix bug in which client doesn't immediately # highlight 'dock' after opening right-click menu. # (see video 2019-07-06_13-26-14 at 33m50s for bug). time.sleep(float(random.randint(700, 1000)) / 1000) mouse.click() wait_for_dock()
def drag_to_ship_inv(): """Click and drag the first item stack from station's inventory to ship's inventory. This function assumed the relevant window is already open.""" logging.debug('moving item stack to ship inventory') station_inv = lo.mlocate('./img/indicators/station_inv_name.bmp', loctype='c') if station_inv == 0: logging.critical("can't find name column") traceback.print_exc() traceback.print_stack() sys.exit() else: tries = 0 ship_inv = lo.mlocate('./img/buttons/ship_inv.bmp', loctype='c') while ship_inv == 0 and tries <= 25: tries += 1 logging.critical("can't find ship inventory") time.sleep(float(random.randint(1000, 2000)) / 1000) ship_inv = lo.mlocate('./img/buttons/ship_inv.bmp', loctype='c') if ship_inv != 0 and tries <= 25: (x, y) = station_inv (sx, sy) = ship_inv pag.moveTo((x + (random.randint(-5, 250))), (y + (random.randint(10, 25))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(0, 1000)) / 1000) pag.mouseDown() time.sleep(float(random.randint(0, 1000)) / 1000) pag.moveTo((sx + (random.randint(-5, 60))), (sy + (random.randint(-8, 8))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(0, 1000)) / 1000) pag.mouseUp() return
def drag_to_ship_specinv(invtype): """Drag item stack to ship's special inventory.""" logging.debug('moving item stack to special inventory') station_inv = lo.mlocate('./img/indicators/station_inv_name.bmp', loctype='c') if station_inv != 0: tries = 0 spec_inv = lo.mlocate('./img/buttons/spec_inv_' + invtype + '.bmp', loctype='c') while spec_inv == 0 and tries <= 25: tries += 1 logging.critical("can't find ship inventory") time.sleep(float(random.randint(1000, 2000)) / 1000) spec_inv = lo.mlocate('./img/buttons/spec_inv_' + invtype + '.bmp', loctype='c') if spec_inv != 0 and tries <= 25: (x, y) = station_inv (sx, sy) = spec_inv pag.moveTo((x + (random.randint(-5, 250))), (y + (random.randint(10, 25))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(0, 1000)) / 1000) pag.mouseDown() time.sleep(float(random.randint(0, 1000)) / 1000) pag.moveTo((sx + (random.randint(-15, 40))), (sy + (random.randint(-3, 3))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(0, 1000)) / 1000) pag.mouseUp() return else: logging.critical("can't find name column") traceback.print_exc() traceback.print_stack() sys.exit()
def focus_overview(): """Clicks somewhere on the lower half of the overview (assuming it's on the rightmost quarter of the client window) to focus the client. If ship is docked, this click whill occur somewhere in the station services window below all the buttons.""" logging.debug('focusing overview') x = (originx + (windowx - (int(windowx / 4.5)))) y = originy randx = (random.randint(0, (int(windowx / 4.5) - 30))) randy = (random.randint((int(windowx / 2)), (windowy - 10))) pag.moveTo((x + randx), (y + randy), mouse.duration(), mouse.path()) mouse.click() return 1
def open_ship_inv(): """Clicks on the ship's inventory button within the inventory window. Assumes the ship is docked and the inventory window is already open.""" logging.debug('opening ship inventory') for tries in range(1, 25): ship_inv = lo.mlocate('./img/buttons/ship_inv.bmp', loctype='c') if ship_inv != 0: (x, y) = ship_inv pag.moveTo((x + (random.randint(-4, 50))), (y + (random.randint(-6, 6))), mouse.duration(), mouse.path()) mouse.click() return 1 elif ship_inv == 0: logging.error('cannot find ship inventory ' + (str(tries))) time.sleep(float(random.randint(500, 2000)) / 1000) logging.error('timed out looking for ship inventory') return 0
def open_specinv(invtype): """Opens the ship's specified special inventory (for storing ore, minerals, planetary products etc.) Assumes the ship is docked and the inventory window is already open.""" logging.debug('opening ' + invtype + ' inventory') for tries in range(1, 25): spec_inv = lo.mlocate('./img/buttons/spec_inv_' + invtype + '.bmp', loctype='c') if spec_inv != 0: (x, y) = spec_inv pag.moveTo((x + (random.randint(-4, 50))), (y + (random.randint(-3, 3))), mouse.duration(), mouse.path()) mouse.click() return 1 if spec_inv == 0: logging.error('cannot find ' + invtype + ' inventory ' + (str(tries))) time.sleep(float(random.randint(500, 2000)) / 1000) logging.error('timed out looking for ' + invtype + ' inventory') return 0
def blacklist_station(): """Blacklists the first green bookmark by editing its name. This will prevent other functions from identifying the bookmark as a potential site.""" at_dest = detect_bookmark_location() if at_dest != 0: logging.debug('blacklisting station') at_dest = pag.locateCenterOnScreen( ('./img/dest/at_dest' + (str(n)) + '.bmp'), confidence=conf, region=(originx, originy, windowx, windowy)) (at_destx), (at_desty) = at_dest pag.moveTo((at_destx + (random.randint(-1, 200))), (at_desty + (random.randint(-3, 3))), mouse.duration(), mouse.path()) time.sleep(float(random.randint(1000, 2000)) / 1000) mouse.click() # Click once to focus entry, then double-click the entry to edit. time.sleep(float(random.randint(1000, 2000)) / 1000) mouse.click() time.sleep(float(random.randint(5, 50)) / 1000) mouse.click() time.sleep(float(random.randint(3000, 4000)) / 1000) pag.keyDown('home') time.sleep(float(random.randint(0, 500)) / 1000) pag.keyUp('home') time.sleep(float(random.randint(0, 1000)) / 1000) pag.keyDown('b') # Add a 'b' to beginning of the name indicating site is blacklisted. pag.keyUp('b') time.sleep(float(random.randint(0, 1000)) / 1000) pag.keyDown('enter') time.sleep((random.randint(0, 200)) / 100) pag.keyUp('enter') return
def focus_inv_window(): """Clicks somewhere inside the station inventory window to focus it. Looks for the sorting buttons in top right corner of the inventory window and positions the mouse cursor relative to those buttons to click an inavtive area within the inventory window.""" tries = 0 window = lo.mlocate('./img/buttons/station_sorting.bmp', loctype='c') while window == 0 and tries <= 25: logging.error('cannot find sorting icon ' + (str(tries))) tries += 1 time.sleep(float(random.randint(500, 2000)) / 1000) window = lo.mlocate('./img/buttons/station_sorting.bmp', loctype='c') if window != 0 and tries <= 25: (x, y) = window pag.moveTo((x - (random.randint(0, 250))), (y + (random.randint(60, 300))), mouse.duration(), mouse.path()) mouse.click() return 1 elif tries > 25: logging.error('timed out looking for sorting buttons') return 0
def initiate_target_lock(target): """Selects topmost user-defined target on the overview. Approaches the selected target and attempts to lock the target once ship is close enough. Tries multiple times to lock the target before giving up. Checks for jamming attempts while approaching the target.""" if target != 0: # Break apart tuple into coordinates. (x, y, l, w) = target # Adjust coordinates for screen. x = (x + (originx + (windowx - (int(windowx / 3.8))))) y = (y + originy) pag.moveTo( (x + (random.randint(-100, 20))), (y + (random.randint(-3, 3))), mouse.duration(), mouse.path()) mouse.click() # Press 'keep at range' hotkey. This is used instead # 'orbit' because if another mining ship depletes the asteroid, # your ship will continue flying forever in a straight line. key.keypress('e') # Change to the general tab to detect jamming. select_overview_tab('general') # Try 5 times to get a target lock. for tries in range(1, 6): # Limit how long the ship spends approaching its target before # giving up for approachtime in range(1, 50): # Once target is in range, attempt to lock it. if is_target_lockable() == 1 and is_jammed(1) == 0: logging.debug('try #' + (str(tries)) + ' to lock target') lock_icon = lo.mlocate( './img/indicators/target_lock_available.bmp', loctype='c', grayscale=True) (x, y) = lock_icon pag.moveTo((x + (random.randint(-10, 10))), (y + (random.randint(-10, 10))), mouse.duration(), mouse.path()) mouse.click() mouse.move_to_neutral() target_locked = wait_for_target_lock() if target_locked == 1: return 1 elif target_locked == 0: # if wait_for_target_lock() times out, continue the # outer 'for' loop and try locking # target again break elif is_target_lockable() == 0 and is_jammed(1) == 0: logging.debug('target not yet within range ' + (str(approachtime))) time.sleep(float(random.randint(10, 20)) / 10) elif is_jammed(1) == 1: logging.warning('jammed while approaching target') return 0 logging.warning( 'timed out waiting for target to get within range!') return 0 logging.warning('lock target failed') return 0 else: logging.info('no targets available') return 0
def blacklist_local_bookmark(): """Determines which bookmark ship is at by looking at the right-click menu. If a bookmark is on grid with the user's ship, blacklist the bookmark by editing its name.""" logging.debug('blacklisting local bookmark') # First check to see if the bookmark even exists. bookmark = 1 bookmark_to_blacklist = pag.locateCenterOnScreen( ('./img/dest/at_dest' + (str(bookmark)) + '.bmp'), confidence=0.95, region=(originx, originy, windowx, windowy)) # If bookmark exists, check right-click menu. while bookmark_to_blacklist != 0: bookmark_to_blacklist = pag.locateCenterOnScreen( ('./img/dest/at_dest' + (str(bookmark)) + '.bmp'), confidence=0.95, region=(originx, originy, windowx, windowy)) if bookmark_to_blacklist != 0: (bookmark_to_blacklistx), ( bookkmark_to_blacklisty) = bookmark_to_blacklist pag.moveTo((bookmark_to_blacklistx + (random.randint(-1, 200))), (bookkmark_to_blacklisty + (random.randint(-3, 3))), mouse.duration(), mouse.path()) # Right-click on bookmark to check if an 'approach location' # option is available. If it is, blacklist bookmark. If it # isn't, try another bookmark. time.sleep(float(random.randint(1000, 2000)) / 1000) mouse.click_right() time.sleep(float(random.randint(1000, 2000)) / 1000) at_bookmark = pag.locateCenterOnScreen( './img/buttons/detect_warp_to_bookmark.bmp', confidence=0.90, region=(originx, originy, windowx, windowy)) # If 'approach location' is present, blacklist that bookmark. if at_bookmark != 0: logging.debug('blacklisting bookmark ' + (str(bookmark))) time.sleep(float(random.randint(1000, 2000)) / 1000) key.keypress('esc') mouse.click() # Click once to focus entry, then double-click the entry to # edit. time.sleep(float(random.randint(1000, 2000)) / 1000) mouse.click() time.sleep(float(random.randint(50, 100)) / 1000) mouse.click() time.sleep(float(random.randint(3000, 4000)) / 1000) pag.keyDown('home') time.sleep(float(random.randint(0, 500)) / 1000) pag.keyUp('home') time.sleep(float(random.randint(0, 1000)) / 1000) pag.keyDown('b') # Add a 'b' to beginning of the name indicating site # is blacklisted. pag.keyUp('b') time.sleep(float(random.randint(0, 1000)) / 1000) pag.keyDown('enter') time.sleep((random.randint(0, 200)) / 100) pag.keyUp('enter') return 1 # If 'approach location' is not present, # close the right-click menu and check the next bookmark. if at_bookmark == 0: logging.debug('not at bookmark ' + (str(bookmark))) key.keypress('esc') bookmark += 1 continue elif bookmark_to_blacklist == 0: logging.warning('out of bookmarks to look for') return 0
def emergency_terminate(): """Looks for the nearest station and docks immediately. Incrementally lowers the confidence required to match the station icon each time the loop runs in order to increase the chances of a warp. If a station cannot be found after a certain number of checks, warps to the nearest planet. After warp completes, simulates a client disconnection by forcing an unsafe logout in space.""" logging.warning('EMERGENCY TERMINATE CALLED !!!') confidence = 1 o.select_overview_tab('general') # Look for a station to dock at until confidence is <0.85 for tries in range(1, 15): station_icon = lo.mlocate('./img/overview/station.bmp', conf=confidence) if station_icon != 0: logging.debug('emergency docking ' + (str(tries))) (x, y) = station_icon pag.moveTo((x + (random.randint(-2, 50))), (y + (random.randint(-2, 2))), mouse.duration(), mouse.path()) mouse.click() time.sleep(float(random.randint(600, 1200)) / 1000) pag.keyDown('d') time.sleep(float(random.randint(600, 1200)) / 1000) pag.keyUp('d') mouse.move_to_neutral() if wait_for_dock() == 1: emergency_logout() elif wait_for_dock() == 0: time.sleep(float(random.randint(20000, 40000)) / 1000) emergency_logout() return 1 elif station_icon == 0: confidence -= 0.01 logging.debug('looking for station to dock at' + (str(tries)) + ', confidence is ' + (str(confidence))) # Keep time interval relatively short since ship may be in combat. time.sleep(float(random.randint(500, 1000)) / 1000) # If confidence lowers below threshold, try warping to a planet # instead. logging.debug('could not find station to emergency dock at, warping to' 'planet instead') confidence = 1 o.select_overview_tab('warpto') for tries in range(1, 50): planet = lo.mlocate('./img/overview/planet.bmp', conf=confidence) if planet != 0: logging.debug('emergency warping to planet') (x, y) = planet pag.moveTo((x + (random.randint(-2, 50))), (y + (random.randint(-2, 2))), mouse.duration(), mouse.path()) mouse.click() time.sleep(float(random.randint(600, 1200)) / 1000) key.keypress('s') mouse.move_to_neutral() wait_for_warp_to_complete() emergency_logout() return 1 elif planet == 0: logging.debug('looking for planet ' + (str(tries)) + ', confidence is ' + (str(confidence))) # Lower confidence on every third try. if (tries % 3) == 0: confidence -= 0.01 time.sleep(float(random.randint(600, 2000)) / 1000) logging.debug('timed out looking for planet') emergency_logout() return 0