def __init__(self, name, team, index):
     super().__init__(name, team, index)
     self.logger = get_logger('ExeSocket' + str(self.index))
     self.is_retired = False
     self.executable_path = None
     self.game_interface = GameInterface(self.logger)
     self.game_tick_packet = GameTickPacket()
     self.spawn_id_seen = False
Example #2
0
 def __init__(self, agent_metadata_queue, quit_event, options):
     super().__init__(agent_metadata_queue, quit_event, options)
     self.logger = get_logger('scratch_mgr')
     self.game_interface = GameInterface(self.logger)
     self.current_sockets = set()
     self.running_indices = set()
     self.port: int = options['port']
     self.sb3_file = options['sb3-file']
     self.has_received_input = False
Example #3
0
def configure_process(proc: psutil.Process,
                      cores: List[int],
                      infer_multi_team=False):
    try:
        if infer_multi_team:
            existing_affinity = proc.cpu_affinity()
            if len(existing_affinity) < psutil.cpu_count():
                # The process in question has already been pinned to a subset of the CPUs.
                # This might be a process spanning multiple teams, so we will set affinity to
                # the combination of existing and newly requested cores.
                cores = sorted(set(existing_affinity + cores))
        proc.cpu_affinity(cores)

        if platform.system() == 'Windows':
            proc.nice(psutil.HIGH_PRIORITY_CLASS
                      )  # Allow the process to run at high priority
    except Exception as e:
        get_logger(DEFAULT_LOGGER).info(e)
def run_module(python_file_with_playlist: Path, history_dir: Optional[Path] = None,
    reload_policy=ReloadPolicy.EACH_EXERCISE, render_policy=RenderPolicy.DEFAULT):
    """
    This function repeatedly runs exercises in the module and reloads the module to pick up
    any new changes to the Exercise. e.g. make_game_state() can be updated or
    you could implement a new Grader without needing to terminate the training.
    """

    # load the playlist initially, keep trying if we fail
    playlist_factory = None
    playlist: Playlist = None
    while playlist is None:
        try:
            playlist_factory = load_default_playlist(python_file_with_playlist)
            playlist = playlist_factory()
        except Exception:
            traceback.print_exc()
            time.sleep(1.0)

    log = get_logger(LOGGER_ID)
    with setup_manager_context() as setup_manager:
        apply_render_policy(render_policy, setup_manager)
        for seed in infinite_seed_generator():
            playlist = playlist_factory()
            wrapped_exercises = [TrainingExerciseAdapter(ex) for ex in playlist]
            result_iter = rlbot_run_exercises(setup_manager, wrapped_exercises, seed)

            for i, rlbot_result in enumerate(result_iter):
                result = ExerciseResult(
                    grade=rlbot_result.grade,
                    exercise=rlbot_result.exercise.exercise,  # unwrap the TrainingExerciseAdapter.
                    reproduction_info=ReproductionInfo(
                        seed=seed,
                        python_file_with_playlist=str(python_file_with_playlist.absolute()),
                        playlist_index=i,
                    )
                )

                log_result(result, log)
                if history_dir:
                    store_result(result, history_dir)

                # Reload the module and apply the new exercises
                if reload_policy == ReloadPolicy.EACH_EXERCISE:
                    try:
                        new_playlist_factory = load_default_playlist(python_file_with_playlist)
                        new_playlist = new_playlist_factory()
                    except Exception:
                        traceback.print_exc()
                        continue  # keep running previous exercises until new ones are fixed.
                    playlist_factory = new_playlist_factory
                    if len(new_playlist) != len(playlist) or any(e1.name != e2.name for e1,e2 in zip(new_playlist, playlist)):
                        log.warning(f'Need to restart to pick up new exercises.')
                        playlist = new_playlist
                        break  # different set of exercises. Can't monkeypatch.
                    for new_exercise, old_exercise in zip(new_playlist, playlist):
                        _monkeypatch_copy(new_exercise, old_exercise)
Example #5
0
    def __init__(self):
        self.logger = get_logger('packet reader')
        self.game_interface = GameInterface(self.logger)
        self.game_interface.load_interface()
        self.game_tick_packet = game_data_struct.GameTickPacket()

        self.rate_limit = rate_limiter.RateLimiter(
            GAME_TICK_PACKET_REFRESHES_PER_SECOND)
        self.last_call_real_time = datetime.now(
        )  # When we last called the Agent
Example #6
0
def setup_manager_context():
    """
    Creates a initialized context manager which shuts down at the end of the
    `with` block.

    usage:
    >>> with setup_manager_context() as setup_manager:
    ...     setup_manager.load_config(...)
    ...     # ... Run match
    """
    setup_manager = SetupManager()
    setup_manager.connect_to_game()
    try:
        yield setup_manager
    except Exception as e:
        get_logger(DEFAULT_LOGGER).error(e)
        raise e
    finally:
        setup_manager.shut_down(kill_all_pids=True)
Example #7
0
 def __init__(self):
     self.logger = get_logger(DEFAULT_LOGGER)
     self.game_interface = GameInterface(self.logger)
     self.quick_chat_manager = QuickChatManager(self.game_interface)
     self.quit_event = mp.Event()
     self.helper_process_manager = HelperProcessManager(self.quit_event)
     self.bot_quit_callbacks = []
     self.bot_reload_requests = []
     self.agent_metadata_map = {}
     self.ball_prediction_process = None
Example #8
0
    def __init__(self, model: BaseModel, input_formatter: BaseInputFormatter,
                 output_formatter: BaseOutputFormatter):
        self.logger = get_logger(str(type(self).__name__))
        self.model = model
        self.input_formatter = input_formatter
        self.output_formatter = output_formatter

        self.use_custom_fit = not hasattr(self.model.fit, 'is_native')
        self.use_custom_sample_action = not hasattr(self.model.predict,
                                                    'is_native')
Example #9
0
 def __init__(self):
     self.logger = get_logger(DEFAULT_LOGGER)
     self.game_interface = GameInterface(self.logger)
     self.quit_event = mp.Event()
     self.helper_process_manager = HelperProcessManager(self.quit_event)
     self.bot_quit_callbacks = []
     self.bot_reload_requests = []
     self.agent_metadata_map = {}
     self.match_config: MatchConfig = None
     self.rlbot_gateway_process = None
     self.matchcomms_server: MatchcommsServerThread = None
Example #10
0
def _wait_until_bots_ready(setup_manager: SetupManager,
                           match_config: MatchConfig):
    total_ready = 0
    total_ready += setup_manager.try_recieve_agent_metadata()
    logger = get_logger(DEFAULT_LOGGER)
    expected_metadata_calls = sum(1 for player in match_config.player_configs
                                  if player.rlbot_controlled)
    while total_ready < expected_metadata_calls:
        logger.debug('Waiting on all bots to post their metadata.')
        time.sleep(0.1)
        total_ready += setup_manager.try_recieve_agent_metadata()
Example #11
0
 def __init__(self, terminate_request_event, termination_complete_event,
              reload_request_event, bot_configuration, name, team, index,
              agent_class_wrapper, agent_metadata_queue,
              match_config: MatchConfig, matchcomms_root: URL,
              spawn_id: int):
     """
     :param terminate_request_event: an Event (multiprocessing) which will be set from the outside when the program is trying to terminate
     :param termination_complete_event: an Event (multiprocessing) which should be set from inside this class when termination has completed successfully
     :param reload_request_event: an Event (multiprocessing) which will be set from the outside to force a reload of the agent
     :param reload_complete_event: an Event (multiprocessing) which should be set from inside this class when reloading has completed successfully
     :param bot_configuration: parameters which will be passed to the bot's constructor
     :param name: name which will be passed to the bot's constructor. Will probably be displayed in-game.
     :param team: 0 for blue team or 1 for orange team. Will be passed to the bot's constructor.
     :param index: The player index, i.e. "this is player number <index>". Will be passed to the bot's constructor.
         Can be used to pull the correct data corresponding to the bot's car out of the game tick packet.
     :param agent_class_wrapper: The ExternalClassWrapper object that can be used to load and reload the bot
     :param agent_metadata_queue: a Queue (multiprocessing) which expects to receive AgentMetadata once available.
     :param match_config: Describes the match that is being played.
     :param matchcomms_root: The server to connect to if you want to communicate to other participants in the match.
     :param spawn_id: The identifier we expect to see in the game tick packet at our player index. If it does not
         match, then we will force the agent to retire. Pass None to opt out of this behavior.
     """
     self.terminate_request_event = terminate_request_event
     self.termination_complete_event = termination_complete_event
     self.reload_request_event = reload_request_event
     self.bot_configuration = bot_configuration
     self.name = name
     self.team = team
     self.index = index
     self.agent_class_wrapper = agent_class_wrapper
     self.agent_metadata_queue = agent_metadata_queue
     self.logger = get_logger('bot' + str(self.index))
     self.game_interface = GameInterface(self.logger)
     self.last_chat_time = time.time()
     self.chat_counter = 0
     self.reset_chat_time = True
     self.game_tick_packet = None
     self.bot_input = None
     self.ball_prediction = None
     self.rigid_body_tick = None
     self.match_config = match_config
     self.matchcomms_root = matchcomms_root
     self.last_message_index = 0
     self.agent = None
     self.agent_class_file = None
     self.last_module_modification_time = 0
     self.scan_last = 0
     self.scan_temp = 0
     self.file_iterator = None
     self.maximum_tick_rate_preference = bot_configuration.get(
         BOT_CONFIG_MODULE_HEADER, MAXIMUM_TICK_RATE_PREFERENCE_KEY)
     self.spawn_id = spawn_id
     self.spawn_id_seen = False
     self.counter = 0
Example #12
0
    def draw_polyline_3d(self, vectors, color):
        if len(vectors) < 2:
            get_logger("Renderer").error(
                "draw_polyline_3d requires atleast 2 vectors!")
            return self

        messageBuilder = self.builder

        for i in range(0, len(vectors) - 1):
            RenderMessage.RenderMessageStart(messageBuilder)
            RenderMessage.RenderMessageAddRenderType(messageBuilder,
                                                     RenderType.DrawLine3D)
            RenderMessage.RenderMessageAddColor(messageBuilder, color)
            RenderMessage.RenderMessageAddStart(
                messageBuilder, self.__create_vector(vectors[i]))
            RenderMessage.RenderMessageAddEnd(
                messageBuilder, self.__create_vector(vectors[i + 1]))
            message = RenderMessage.RenderMessageEnd(messageBuilder)
            self.render_list.append(message)

        return self
Example #13
0
 def __init__(self, agent_metadata_queue, quit_event, options):
     super().__init__(agent_metadata_queue, quit_event, options)
     self.logger = get_logger('scratch_mgr')
     self.game_interface = GameInterface(self.logger)
     self.current_sockets = set()
     self.running_indices = set()
     self.metadata_map = dict()
     self.port: int = options['port']
     self.sb3_file = options['sb3-file']
     self.pretend_blue_team = options['pretend_blue_team']
     self.has_received_input = False
     self.scratch_index_to_rlbot = {}
     self.should_flip_field = False
Example #14
0
    def initialize_agent(self):
        self.logger = get_logger(self.name)
        # This runs once before the bot starts up
        self.controller_state = SimpleControllerState()

        from examples.example_model_holder import ExampleModelHolder

        self.model_holder = ExampleModelHolder(self.create_model(),
                                               self.create_input_formatter(),
                                               self.create_output_formatter())

        self.model_holder.initialize_model(load=True)
        self.logger.info("Model has been initialized")
Example #15
0
 def __init__(self, name):
     super().__init__(name)
     self.logger = get_logger(name)
     self.__key = hash("BaseScript:" + name)
     self.game_tick_packet = GameTickPacket()
     self.ball_prediction = BallPrediction()
     self.game_interface = GameInterface(self.logger)
     self.game_interface.load_interface()
     fake_index = random.randint(
         100,
         10000)  # a number unlikely to conflict with bots or other scripts
     self.renderer = self.game_interface.renderer.get_rendering_manager(
         bot_index=fake_index, bot_team=2)
def restart_devserver_on_source_change(history_dir, host, port):
    subprocess_command = f'python {_website_dev_server} {history_dir} {host} {port}'

    child_process = None

    def start_dev_server():
        nonlocal child_process
        if child_process:
            kill_proc_tree(child_process.pid)
        child_process = Popen(
            subprocess_command,
            shell=True,
        )
        get_logger('restarter').info('child started')
        atexit.register(lambda: child_process.kill())  # behave like a daemon

    start_dev_server()

    event_handler = CallOnModified(start_dev_server)
    observer = Observer()
    observer.schedule(event_handler, str(_website_source), recursive=True)

    # Also monitor the additional_aggregators_dir.
    symlink_file = history_dir / HistoryPaths.additional_website_code
    if symlink_file.exists():
        additional_aggregators_dir = symlink_file.read_text().strip()
        observer.schedule(event_handler,
                          additional_aggregators_dir,
                          recursive=True)

    observer.start()
    try:
        while True:
            time.sleep(.1)
    except KeyboardInterrupt:
        get_logger('restarter').info("Shutting down...")
        observer.stop()
    observer.join()
Example #17
0
async def send_from_queue(websocket: WebSocketClientProtocol, outgoing: Queue):
    logger = get_logger('matchcomms_json_encode')
    while True:
        # TODO: use something like https://github.com/aio-libs/janus to avoid polling
        while outgoing.empty():
            await asyncio.sleep(0.01)

        obj = outgoing.get_nowait()
        try:
            json_str = json.dumps(obj) # Serialize the object that was put on the outgoing queue
        except TypeError:
            logger.error(f'This object failed to encode: {obj}\n{traceback.format_exc()}')
        else:
            await websocket.send(json_str)
Example #18
0
    def __init__(self, agent_metadata_queue, quit_event):
        super().__init__(agent_metadata_queue, quit_event)
        sys.path.insert(
            0, get_repo_directory())  # this is for separate process imports
        self.logger = get_logger('base_hive_mgr')

        self.manager = self.setup_manager()
        self.game_memory = self.manager.Memory()
        self.actor_model = self.get_model()
        self.shared_model_handle = self.get_shared_model_handle()
        self.model_path = None
        self.load_model = None

        self.setup_trainer()
Example #19
0
 def __init__(self):
     self.logger = get_logger(DEFAULT_LOGGER)
     self.game_interface = GameInterface(self.logger)
     self.quit_event = mp.Event()
     self.helper_process_manager = HelperProcessManager(self.quit_event)
     self.bot_quit_callbacks = []
     self.bot_reload_requests = []
     self.agent_metadata_map: Dict[int, AgentMetadata] = {}
     self.match_config: MatchConfig = None
     self.launcher_preference = None
     self.rlbot_gateway_process = None
     self.matchcomms_server: MatchcommsServerThread = None
     self.early_start_seconds = 0
     self.num_metadata_received = 0
Example #20
0
    def __init__(self, agent_metadata_queue, quit_event, options):
        super().__init__(agent_metadata_queue, quit_event, options)

        # Sets up the logger. The string is the name of your hivemind.
        # Call this something unique so people can differentiate between hiveminds.
        self.logger = get_logger('Example Hivemind')

        # The game interface is how you get access to things
        # like ball prediction, the game tick packet, or rendering.
        self.game_interface = GameInterface(self.logger)

        # Running indices is a set of bot indices
        # which requested this hivemind with the same key.
        self.running_indices = set()
Example #21
0
 def __init__(self, connection_timeout:float=120):
     self.connection_timeout = connection_timeout
     self.logger = get_logger('socket_man')
     self.socket = socket()
     self.is_connected = False
     self._should_continue = True
     self.on_connect_handlers: List[Callable[[], None]] = []
     self.packet_handlers: List[Callable[[GameTickPacket], None]] = []
     self.field_info_handlers: List[Callable[[FieldInfo], None]] = []
     self.match_settings_handlers: List[Callable[[MatchSettings], None]] = []
     self.quick_chat_handlers: List[Callable[[QuickChat], None]] = []
     self.ball_prediction_handlers: List[Callable[[BallPrediction], None]] = []
     self.player_input_change_handlers: List[Callable[[PlayerInputChange, float, int], None]] = []
     self.player_stat_handlers: List[Callable[[PlayerStatEvent, float, int], None]] = []
     self.player_spectate_handlers: List[Callable[[PlayerSpectate, float, int], None]] = []
Example #22
0
    def call_agent(self, agent: BaseAgent, agent_class):
        controller_input = agent.get_output(self.game_tick_packet)
        if controller_input is None:
            get_logger("BotManager" + str(self.index))\
                .error("Agent %s did not return any output.", str(agent_class.__name__))
            return

        player_input = self.bot_input

        if isinstance(controller_input, list):
            # Write all player inputs
            get_logger("BotManager" + str(self.index)).error(
                "Sending legacy packet type, please convert to v4")
            controller_input = agent.convert_output_to_v4(controller_input)

        player_input.throttle = controller_input.throttle
        player_input.steer = controller_input.steer
        player_input.pitch = controller_input.pitch
        player_input.yaw = controller_input.yaw
        player_input.roll = controller_input.roll
        player_input.jump = controller_input.jump
        player_input.boost = controller_input.boost
        player_input.handbrake = controller_input.handbrake
        self.game_interface.update_player_input(player_input, self.index)
Example #23
0
def launch_with_epic_simple(ideal_args: List[str]) -> bool:
    logger = get_logger(DEFAULT_LOGGER)
    try:
        # Try launch via Epic Games
        epic_exe_path = locate_epic_games_launcher_rocket_league_binary()
        if epic_exe_path is not None:
            exe_and_args = [str(epic_exe_path)] + ideal_args
            logger.info(f'Launching Rocket League with: {exe_and_args}')
            try:
                _ = subprocess.Popen(exe_and_args)
                return True
            except Exception as e:
                logger.info(f'Unable to launch via Epic due to: {e}')
    except:
        logger.debug('Unable to launch via Epic.')
    return False
Example #24
0
    def __init__(self, agent_metadata_queue, quit_event, options):
        super().__init__(agent_metadata_queue, quit_event, options)

        # Sets up the logger. The string is the name of your hivemind.
        # This is configured inside your config file under hivemind_name.
        self.logger = get_logger(options['name'])

        # Give a warning if exec_path was given.
        if 'exec_path' is None:
            raise NotImplementedError(
                "Please specify the exec_path in your subclass of SubprocessHivemind."
            )

        # drone_indices is a set of bot indices
        # which requested this hivemind with the same key.
        self.drone_indices = set()
Example #25
0
    def __init__(self, queue, choreo_obj):
        # Sets up the logger. The string is the name of your hivemind.
        # Call this something unique so people can differentiate between hiveminds.
        self.logger = get_logger('Choreography Hivemind')

        # The game interface is how you get access to things
        # like ball prediction, the game tick packet, or rendering.
        self.game_interface = GameInterface(self.logger)

        self.drones = []

        self.choreo = choreo_obj(self.game_interface)
        self.choreo.generate_sequence(self.drones)

        # Set up queue to know when to stop and reload.
        self.queue = queue
Example #26
0
def find_existing_process():
    logger = get_logger(DEFAULT_LOGGER)
    for proc in psutil.process_iter():
        try:
            if proc.name() == executable_name:
                if len(proc.cmdline()) > 1:
                    port = int(proc.cmdline()[1])
                    return proc, port
                logger.error(
                    f"Failed to find the RLBot port being used in the process args! Guessing "
                    f"{IDEAL_RLBOT_PORT}.")
                return proc, IDEAL_RLBOT_PORT
        except Exception as e:
            logger.error(
                f"Failed to read the name of a process while hunting for RLBot.exe: {e}"
            )
    return None, None
Example #27
0
 def __init__(self):
     self.player_configs: List[PlayerConfig] = []
     self.game_mode: str = None
     self.game_map: str = None
     self.skip_replays: bool = None
     self.instant_start: bool = None
     self.mutators: MutatorConfig = None
     self.extension_config: ExtensionConfig = None
     self.existing_match_behavior: str = None
     self.enable_lockstep: bool = None
     self.networking_role: str = None
     self.network_address: str = None
     self.enable_rendering: bool = None
     self.enable_state_setting: bool = None
     self.auto_save_replay: bool = None
     self.script_configs: List[ScriptConfig] = []
     self.logger = get_logger('match_config')
Example #28
0
def launch_with_epic_login_trick(ideal_args: List[str]) -> bool:
    """
    This needs to fail gracefully! Sometimes people only have the Steam version,
    so we want to be able to fall back to Steam if Epic is going nowhere.
    """
    try:
        logger = get_logger(DEFAULT_LOGGER)

        if not launch_with_epic_simple(ideal_args):
            return False

        process = None
        while True:
            sleep(1)
            process = get_process('RocketLeague.exe', 'RocketLeague.exe',
                                  set())
            if process is not None:
                break
            logger.info("Waiting for RocketLeague.exe to start...")

        while True:
            all_args = get_auth_args_from_process()
            if all_args is not None:
                break
            all_args = get_auth_args_from_logs()
            if all_args is not None:
                break
            time.sleep(1)
            logger.info("Waiting for Rocket League args...")

        process = get_process('RocketLeague.exe', 'RocketLeague.exe', set())
        if process:
            try:
                process.kill()
            except psutil.NoSuchProcess:
                # the process killed itself before we did, but result is the same.
                pass
        modified_args = ideal_args + all_args
        logger.info(
            f"Killed old rocket league, reopening with {modified_args}")
        launch_with_epic_simple(modified_args)
        return True
    except Exception as ex:
        logger.warn(f"Trouble with epic launch: {ex}")
        return False
Example #29
0
 def __init__(self):
     self.game_interface = GameInterface(get_logger("Commentator"))
     self.game_interface.load_interface()
     self.game_interface.wait_until_loaded()
     self.touchTimer = 0
     self.currentTime = 0
     self.firstIter = True
     self.overTime = False
     self.shotDetection = True
     self.ballHistory = []
     self.lastTouches = []
     self.teams = []
     self.joinTimer = 0
     self.q = Queue(maxsize=3)
     self.host = threading.Thread(target=host, args=(self.q,))
     self.host.start()
     self.main()
     self.host.join()
Example #30
0
    def __init__(self, agent_metadata_queue, quit_event, options):
        super().__init__(agent_metadata_queue, quit_event, options)

        # Controls.
        pygame.init()
        pygame.joystick.init()
        self.controller = pygame.joystick.Joystick(0)
        self.controller.init()
        self.axis_data = [0] * self.controller.get_numaxes()
        self.button_data = [False] * self.controller.get_numbuttons()

        # RLBot.
        self.index = options["index"]
        self.game_interface = GameInterface(get_logger(str(self.index)))

        self.recording = False
        self.recorded = []
        self.recording_time = None
Example #31
0
import sys

# https://stackoverflow.com/a/51704613
try:
    from pip import main as pipmain
except ImportError:
    from pip._internal import main as pipmain

DEFAULT_LOGGER = 'rlbot'

if __name__ == '__main__':

    try:
        from rlbot.utils import public_utils, logging_utils

        logger = logging_utils.get_logger(DEFAULT_LOGGER)
        if not public_utils.have_internet():
            logger.log(logging_utils.logging_level,
                       'Skipping upgrade check for now since it looks like you have no internet')
        elif public_utils.is_safe_to_upgrade():
            pipmain(['install', '-r', 'requirements.txt', '--upgrade', '--upgrade-strategy=eager'])

            # https://stackoverflow.com/a/44401013
            rlbots = [module for module in sys.modules if module.startswith('rlbot')]
            for rlbot_module in rlbots:
                sys.modules.pop(rlbot_module)

    except ImportError:
        pipmain(['install', '-r', 'requirements.txt', '--upgrade', '--upgrade-strategy=eager'])

    try: