def wait_for_jump(): """Waits for a jump by looking for the cyan session-change icon in top left corner of the client window. Also checks if the 'low security system warning' window has appeared and is preventing the ship from jumping. Times out after about four minutes.""" # Confidence must be lower than normal since icon is partially # transparent. for tries in range(1, 240): jumped = lo.mlocate('./img/indicators/session_change_cloaked.bmp', conf=0.55) if jumped != 0: logging.debug('jump detected') time.sleep(float(random.randint(1000, 2000)) / 1000) return 1 elif jumped == 0: losec = lo.mlocate('./img/warnings/low_security_system.bmp', conf=0.9) if losec != 0: time.sleep(float(random.randint(2000, 5000)) / 1000) key.keypress('enter') continue logging.debug('waiting for jump ' + (str(tries))) time.sleep(float(random.randint(5, 20)) / 10) logging.error('timed out waiting for jump') emergency_terminate() traceback.print_stack() sys.exit()
def activate_miners(module_num): """Activates mining modules based on the number passed to this function. If the module is out of range, script will try to wait until ship gets within range before activating any more modules.""" for n in range(1, (module_num + 1)): key.keypress('f' + (str(n))) logging.debug('activating miner ' + (str(n))) out_of_range = lo.mlocate('./img/popups/miner_out_of_range.bmp', conf=0.90, grayscale=True) tries = 0 while out_of_range == 1 and tries <= 25: tries += 1 time.sleep(float(random.randint(15000, 30000)) / 1000) out_of_range = lo.mlocate('./img/popups/miner_out_of_range.bmp', conf=0.90, grayscale=True) if out_of_range == 0 and tries <= 25: time.sleep(float(random.randint(0, 3000)) / 1000) logging.debug('activating miner ' + (str(n))) key.keypress('f' + (str(n))) if out_of_range == 0 and tries <= 25: continue elif out_of_range == 1 and tries > 25: logging.error('timed out waiting for ship to get within ' 'module range') return 0 return 1
def wait_for_undock(): """Undock from the station with the default hotkey. The undock_loop has been completed once the script sees the cyan ship icon in the top left corner of the client window, indicating a session change has just ocurred.""" logging.info('undocking') pag.keyDown('ctrl') time.sleep(float(random.randint(100, 800)) / 1000) key.keypress('u') time.sleep(float(random.randint(100, 800)) / 1000) pag.keyUp('ctrl') # Wait for the 'undock' button to change to 'undocking', indicating the # undock action has been confirmed. tries = 0 while lo.mlocate('./img/buttons/undocking.bmp', conf=0.8, loctype='o') \ == 0 and tries <= 250: tries += 1 logging.debug('waiting for session change to begin ' + (str(tries))) time.sleep(int((random.randint(100, 200) / 1000))) if lo.mlocate('./img/buttons/undocking.bmp', conf=0.8, loctype='o') != 0 \ and tries <= 250: logging.debug('session change underway ' + (str(tries))) # Now wait for the undock to complete by looking for the session # change indicator. tries = 0 while lo.mlocate('./img/indicators/session_change_undocked.bmp', conf=0.55) == 0 and tries <= 100: tries += 1 time.sleep(int((random.randint(500, 2000) / 1000))) logging.debug('waiting for session change to complete ' + (str(tries))) if lo.mlocate('./img/indicators/session_change_undocked.bmp', conf=0.55) != 0 and tries <= 100: logging.debug('undock completed ' + (str(tries))) return 1 # If script times out waiting for the session change icon, simply # look for the undock button instead since ship has likely completed # an undock, but at this point the session change icon is probably # gone. elif lo.mlocate('./img/indicators/session_change_undocked.bmp', conf=0.55) == 0 and tries > 100: if is_docked() == 1: logging.error('cannot undock') sys.exit() elif is_docked() == 0: logging.warning('undock tentatively completed') return 1 elif lo.mlocate('./img/buttons/undocking.bmp', conf=0.9) == 0 and \ tries > 25: logging.error('timed out waiting for session change') sys.exit()
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 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 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 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 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
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