Esempio n. 1
0
    def find_path(self, start, end):
        """
        Finds a path from start to end in the current game state.

        @param start: The position to start from
        @param end: The position to end at
        @return A list of Position objects from start to end or an empty list if no path is possible.
        """
        if isinstance(start, position.Position) and isinstance(
                end, position.Position):
            url = self.API_SERVER_URL + "pathFinding"
            payload = api_pb2.APIPathFindingRequest()
            payload.gameState.CopyFrom(self.game_state)
            payload.start.CopyFrom(start.build_proto_class())
            payload.end.CopyFrom(end.build_proto_class())

            response = requests.post(
                url,
                headers={'Content-Type': 'application/octet-stream'},
                data=payload.SerializeToString())
            APIresponse = api_pb2.APIPathFindingResponse()
            APIresponse.ParseFromString(response.content)

            if APIresponse.status.status != 200:
                return APIresponse.status

            path = []
            for tile in APIresponse.path:
                path.append(position.Position(tile))
            return path
        else:
            return None
Esempio n. 2
0
    def find_items_in_range_by_distance(self, pos, range):
        """
        Finds all items within a given range of the given position

        @param position: The position around which to search
        @param range: The range to search within
        @return A List of Items found in the search
        """
        if isinstance(pos, position.Position) and isinstance(range, int):
            url = self.API_SERVER_URL + "findItemsInRangeByDistance"
            payload = api_pb2.APIFindItemsInRangeByDistanceRequest()
            payload.gameState.CopyFrom(self.game_state)
            payload.position.CopyFrom(pos.build_proto_class())
            payload.player_name = self.player_name
            payload.range = range

            response = requests.post(
                url,
                headers={'Content-Type': 'application/octet-stream'},
                data=payload.SerializeToString())
            APIresponse = api_pb2.APIFindItemsInRangeByDistanceResponse()
            APIresponse.ParseFromString(response.content)
            if APIresponse.status.status != 200:
                return APIresponse.status

            items = []
            item_positions = []
            for i in APIresponse.items:
                if i.HasField("accessory"):
                    items.append(accessory.Accessory(i.accessory))
                elif i.HasField("clothes"):
                    items.append(clothes.Clothes(i.clothes))
                elif i.HasField("consumable"):
                    items.append(consumable.Consumable(i.consumable))
                elif i.HasField("hat"):
                    items.append(hat.Hat(i.hat))
                elif i.HasField("shoes"):
                    items.append(shoes.Shoes(i.shoes))
                elif i.HasField("weapon"):
                    items.append(weapon.Weapon(i.weapon))

            for i_pos in APIresponse.positions:
                item_positions.append(position.Position(i_pos))

            return (items, item_positions)
        else:
            return None
Esempio n. 3
0
    def find_closest_portal(self, pos):
        """
        Finds the closest portal to the given position

        @param position: The position to begin searching from
        @return A Position representing the location of the closest portal, or null if an error occurred.
        """
        if isinstance(pos, position.Position):
            url = self.API_SERVER_URL + "findClosestPortal"
            payload = api_pb2.APIFindClosestPortalRequest()
            payload.gameState.CopyFrom(self.game_state)
            payload.position.CopyFrom(pos.build_proto_class())

            response = requests.post(url, headers={'Content-Type': 'application/protobuf'}, data=payload.SerializeToString())
            APIresponse = api_pb2.APIFindClosestPortalResponse()
            APIresponse.ParseFromString(response.content)
            if APIresponse.status.status != 200:
                return None
            return position.Position(APIresponse.portal)
        else:
            return None