async def move_and_face_previous_direction(direction: int, n: int): async with server.player_status_stream() as stream: ps = await stream.next() await game.move_n_tiles(direction, n, stream) await game.face_direction(ps["facingDirection"], stream, move_cursor=True)
async def run(self): req_data = {"characterType": "monster", "requiredName": None} req_builder = server.RequestBuilder('GET_NEAREST_CHARACTER', req_data) player_status_builder = server.RequestBuilder('PLAYER_STATUS') batched_request_builder = server.RequestBuilder.batch( player_status_builder, req_builder) batched_request_builder.data[1]['data'] = { **req_data, 'target': None, 'getPath': False } await game.equip_melee_weapon() async with server.player_status_stream() as player_stream: player_position = (await player_status_builder.request())['position'] while True: player_status, target = await batched_request_builder.request() if not target: return player_position = player_status['center'] closest_monster_position = target['center'] distance_from_monster = game.distance_between_points_diagonal( player_position, closest_monster_position) if distance_from_monster > 0: direction_to_face = game.direction_from_positions( player_position, closest_monster_position) await game.face_direction(direction_to_face, player_stream) if distance_from_monster < 110: await server.set_mouse_position( closest_monster_position[0], closest_monster_position[1], from_viewport=True) await game.swing_tool() await asyncio.sleep(0.1)
async def navigate_direction(direction: int): async with server.player_status_stream() as stream: player_status = await stream.next() location = player_status["location"] path_tiles = await server.request("PATH_TO_EDGE", {"direction": direction}) if path_tiles: path = game.Path(path_tiles, location) await path.travel(stream)
async def go_inside(): indoors_connections = [ x for x in (await get_location_connections()) if not x["TargetIsOutdoors"] ] if indoors_connections: with server.player_status_stream() as pss: current_tile = await get_current_tile(pss) indoors_connections.sort(key=lambda t: distance_between_points( current_tile, (t["X"], t["Y"]))) await pathfind_to_next_location( indoors_connections[0]["TargetName"], pss)
async def go_outside(): outdoor_connections = [ x for x in (await get_location_connections()) if x["TargetIsOutdoors"] ] if outdoor_connections: with server.player_status_stream() as pss: player_status = await pss.next() current_location = player_status["location"] if not current_location['TargetIsOutdoors']: current_tile = await get_current_tile(pss) outdoor_connections.sort(key=lambda t: distance_between_points( current_tile, (t["X"], t["Y"]))) await pathfind_to_next_location( outdoor_connections[0]["TargetName"], pss)
async def navigate_tiles( get_items, sort_items=generic_next_item_key, pathfind_fn=pathfind_to_adjacent, items_ok=lambda prev, curr: True, allow_action_on_same_tile=True, index=None, ): import events async with server.player_status_stream() as stream: player_status = await stream.next() start_tile = player_status["tileX"], player_status["tileY"] previous_items = [] while True: current_tile = player_status["tileX"], player_status["tileY"] items = await get_items(player_status["location"]) if not items: return sorted_items = sorted( items, key=lambda t: sort_items(start_tile, current_tile, t, player_status)) if index is not None: sorted_items = [sorted_items[index]] if not items_ok(previous_items, items): raise RuntimeError("Unable to modify current tile") previous_items = sorted_items item_path = None for item in sorted_items: item_tile = (item["tileX"], item["tileY"]) if current_tile == item_tile and not allow_action_on_same_tile: await pathfind_to_adjacent_tile_from_current(stream) await face_tile(stream, item_tile) try: item_path = await pathfind_fn(item["tileX"], item["tileY"], stream) except NavigationFailed: pass else: await set_mouse_position_on_tile(item_tile) yield item break if not item_path: return player_status = await stream.next()
async def move_to_point(point): async with server.player_status_stream() as stream: player_status = await stream.next() regex_mismatch = isinstance( point.location, re.Pattern) and not point.location.match(player_status['location']) str_mismatch = isinstance( point.location, str) and point.location != player_status['location'] if regex_mismatch or str_mismatch: raise game.NavigationFailed( f'Currently in {player_status["location"]} - unable to move to point in location {point.location}' ) await game.navigate_nearest_tile(point.get_tiles, pathfind_fn=point.pathfind_fn) if point.on_arrival: await point.on_arrival()
async def gather_items_on_ground(radius): """ Wood, coal, sap, stone etc. """ async with server.player_status_stream() as stream: player_status = await stream.next() location = player_status["location"] start_tile = player_status["tileX"], player_status["tileY"] tile_blacklist = set([start_tile]) while True: items_to_gather = collections.defaultdict(int) debris = await server.request("GET_DEBRIS", {"location": "location"}) test_tiles_set = set() for item in debris: within_radius = distance_between_points( start_tile, (item["tileX"], item["tileY"])) < radius if within_radius: debris_tile = item["tileX"], item["tileY"] for tile in get_adjacent_tiles(debris_tile) + [ debris_tile ]: items_to_gather[tile] += 1 if tile not in tile_blacklist: test_tiles_set.add(tile) if not test_tiles_set: return player_status = await stream.next() current_tile = player_status["tileX"], player_status["tileY"] test_tiles = sort_test_tiles(test_tiles_set, start_tile, current_tile, items_to_gather) path, invalid = await pathfind_to_resource(test_tiles, location, stream, cutoff=250) if path is None: server.log( f"Unable to gather {len(test_tiles)} in radius {radius}") return for tile in path.tiles: tile_blacklist.add(tile) for tile in invalid: tile_blacklist.add(tile)
async def start_shopping(): async with server.player_status_stream() as stream: loc = (await stream.next())['location'] if loc == 'AnimalShop': tile, facing_direction = (12, 16), constants.NORTH elif loc == 'Blacksmith': tile, facing_direction = (3, 15), constants.NORTH elif loc == 'FishShop': tile, facing_direction = (5, 6), constants.NORTH elif loc == 'JojaMart': tile, facing_direction = (11, 25), constants.WEST elif loc == 'LibraryMuseum': tile, facing_direction = (3, 9), constants.NORTH elif loc == 'Saloon': tile, facing_direction = (10, 20), constants.NORTH elif loc == 'ScienceHouse': tile, facing_direction = (8, 20), constants.NORTH elif loc == 'SeedShop': tile, facing_direction = (4, 19), constants.NORTH x, y = tile await game.pathfind_to_tile(x, y, stream) await game.do_action()
async def move(self): import objective tiles_from_target = self.tiles_from_target while True: npc = await self.get_character(None) if "pathTiles" in npc: async with server.player_status_stream() as travel_path_stream: path = tiles_to_adjacent_path( npc["pathTiles"], npc["location"], tiles_from_target=tiles_from_target) # don't share streams between tasks, otherwise they steal .next() from each other and cause resource starvation issues pathfind_coro = path.travel(travel_path_stream) pathfind_task_wrapper = objective.active_objective.add_task( pathfind_coro) while not pathfind_task_wrapper.done: npc = await self.get_character(npc) if "pathTiles" in npc: new_path = tiles_to_adjacent_path( npc["pathTiles"], npc["location"], tiles_from_target=tiles_from_target) path.retarget(new_path) if pathfind_task_wrapper.exception: raise pathfind_task_wrapper.exception try: await self.move_directly_to_character(npc, threshold=self.distance) except asyncio.TimeoutError: tiles_from_target = 1 else: break npc = await self.get_character(npc, get_path=False) npx_x, npc_y = npc["center"] await server.set_mouse_position(npx_x, npc_y, from_viewport=True) return npc
async def run(self): async with server.player_status_stream() as stream: await game.equip_item_by_name(constants.HOE) player_status = await stream.next() player_tile = player_status["tileX"], player_status["tileY"] facing_direction = player_status['facingDirection'] start_tile = game.next_tile(player_tile, facing_direction) plot_tiles = set() x_increment = -1 if game.last_faced_east_west == constants.WEST else 1 y_increment = -1 if game.last_faced_north_south == constants.NORTH else 1 for i in range(self.n1): x = start_tile[0] + i * x_increment for j in range(self.n2): y = start_tile[1] + j * y_increment plot_tiles.add((x, y)) get_next_diggable = functools.partial(game.get_diggable_tiles, plot_tiles) async for hdt in game.navigate_tiles( get_next_diggable, game.generic_next_item_key, allow_action_on_same_tile=False, items_ok=game.disallow_previous_item): await game.equip_item_by_name(constants.HOE) await game.swing_tool()
async def start_fishing(): async with server.player_status_stream() as stream: await game.equip_item_by_name(constants.FISHING_ROD) async with server.tool_status_stream() as tss: await cast_fishing_rod(tss) await wait_for_nibble(tss)
async def run(self): async with server.player_status_stream() as stream: await game.move_to_location(self.location.name, stream)
async def run(self): async with server.player_status_stream(ticks=1) as stream: await game.move_n_tiles(self.direction, self.n, stream)
async def run(self): async with server.player_status_stream() as stream: await game.face_direction(self.direction, stream, move_cursor=True)