Exemplo n.º 1
0
    def wait_until_valid_packet(self):
        num_players = self.start_match_configuration.num_players
        if num_players > 10:
            self.logger.info(
                f"Will not wait for all players to spawn in because there isn't room for {num_players} anyway."
            )
            return

        self.logger.info('Waiting for valid packet...')
        for i in range(0, 60):
            packet = game_data_struct.GameTickPacket()
            self.update_live_data_packet(packet)
            if not packet.game_info.is_match_ended:
                spawn_ids = set()
                for k in range(0, self.start_match_configuration.num_players):
                    player_config = self.start_match_configuration.player_configuration[
                        k]
                    if player_config.rlbot_controlled:
                        spawn_ids.add(player_config.spawn_id)

                for n in range(0, packet.num_cars):
                    try:
                        spawn_ids.remove(packet.game_cars[n].spawn_id)
                    except KeyError:
                        pass

                if len(spawn_ids) == 0:
                    self.logger.info(
                        'Packets are looking good, all spawn ids accounted for!'
                    )
                    return

            time.sleep(0.5)
        self.logger.info('Gave up waiting for valid packet :(')
Exemplo n.º 2
0
 def __init__(self):
     self.logger = get_logger('packet reader')
     self.game_interface = GameInterface(self.logger)
     self.game_interface.inject_dll()
     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
Exemplo n.º 3
0
 def prepare_for_run(self):
     # Set up shared memory map (offset makes it so bot only writes to its own input!) and map to buffer
     self.bot_input = PlayerInput()
     # Set up shared memory for game data
     # We want to do a deep copy for game inputs so people don't mess with em
     self.game_tick_packet = gd.GameTickPacket()
     # Set up shared memory for Ball prediction
     self.ball_prediction = bp.BallPrediction()
     # Set up shared memory for rigid body tick
     self.rigid_body_tick = RigidBodyTick()
Exemplo n.º 4
0
    def launch_bot_process_helper(self, early_starters_only=False):
        # Start matchcomms here as it's only required for the bots.
        self.kill_matchcomms_server()
        self.matchcomms_server = launch_matchcomms_server()

        num_started = 0

        # Launch processes
        # TODO: this might be the right moment to fix the player indices based on a game tick packet.
        packet = game_data_struct.GameTickPacket()
        self.game_interface.update_live_data_packet(packet)

        # TODO: root through the packet and find discrepancies in the player index mapping.
        for i in range(self.num_participants):
            if not self.start_match_configuration.player_configuration[i].rlbot_controlled:
                continue
            if early_starters_only and not self.bot_bundles[i].supports_early_start:
                continue

            bot_manager_spawn_id = None

            if early_starters_only:
                # Don't use a spawn id stuff for the early start system. The bots will be starting up before
                # the car spawns, and we don't want the bot manager to panic.
                participant_index = i
            else:
                participant_index = None
                spawn_id = self.game_interface.start_match_configuration.player_configuration[i].spawn_id
                self.logger.info(f'Player in slot {i} was sent with spawn id {spawn_id}, will search in the packet.')
                for n in range(0, packet.num_cars):
                    packet_spawn_id = packet.game_cars[n].spawn_id
                    if spawn_id == packet_spawn_id:
                        self.logger.info(f'Looks good, considering participant index to be {n}')
                        participant_index = n
                        bot_manager_spawn_id = spawn_id
                if participant_index is None:
                    raise Exception("Unable to determine the bot index!")

            if participant_index not in self.bot_processes:
                reload_request = mp.Event()
                quit_callback = mp.Event()
                self.bot_reload_requests.append(reload_request)
                self.bot_quit_callbacks.append(quit_callback)
                process = mp.Process(target=SetupManager.run_agent,
                                     args=(self.quit_event, quit_callback, reload_request, self.bot_bundles[i],
                                           str(self.start_match_configuration.player_configuration[i].name),
                                           self.teams[i], participant_index, self.python_files[i], self.agent_metadata_queue,
                                           self.match_config, self.matchcomms_server.root_url, bot_manager_spawn_id))
                process.start()
                self.bot_processes[i] = process
                num_started += 1

        self.logger.debug(f"Successfully started {num_started} bot processes")
        return num_started
Exemplo n.º 5
0
 def wait_until_valid_packet(self):
     self.logger.info('Waiting for valid packet...')
     for i in range(0, 16):
         packet = game_data_struct.GameTickPacket()
         self.update_live_data_packet(packet)
         if packet.num_cars > 0:
             self.logger.info('Packets are looking good!')
             return
         else:
             time.sleep(0.5)
     self.logger.info('Gave up waiting for valid packet :(')
Exemplo n.º 6
0
 def wait_until_valid_packet(self):
     self.logger.info('Waiting for valid packet...')
     for i in range(0, 16):
         packet = game_data_struct.GameTickPacket()
         self.update_live_data_packet(packet)
         if packet.num_cars >= self.start_match_configuration.num_players and not packet.game_info.is_match_ended:
             self.logger.info('Packets are looking good!')
             return
         else:
             time.sleep(0.5)
     self.logger.info('Gave up waiting for valid packet :(')
Exemplo n.º 7
0
    def _old_wait_until_valid_packet(self):
        self.logger.info('Waiting for valid packet...')
        expected_count = self.start_match_configuration.num_players

        for i in range(0, 300):
            packet = game_data_struct.GameTickPacket()
            self.update_live_data_packet(packet)
            if not packet.game_info.is_match_ended:
                spawn_ids = set()
                for k in range(0, expected_count):
                    player_config = self.start_match_configuration.player_configuration[
                        k]
                    if player_config.rlbot_controlled and player_config.spawn_id > 0:
                        spawn_ids.add(player_config.spawn_id)

                for n in range(0, packet.num_cars):
                    try:
                        spawn_ids.remove(packet.game_cars[n].spawn_id)
                    except KeyError:
                        pass

                if len(
                        spawn_ids
                ) == 0 and expected_count <= self.start_match_configuration.num_players:
                    self.logger.info(
                        'Packets are looking good, all spawn ids accounted for!'
                    )
                    return
                elif i > 4:
                    car_states = {}
                    for k in range(0,
                                   self.start_match_configuration.num_players):
                        player_info = packet.game_cars[k]
                        if player_info.spawn_id > 0:
                            car_states[k] = CarState(physics=Physics(
                                velocity=Vector3(z=500)))
                    if len(car_states) > 0:
                        self.logger.info(
                            "Scooting bots out of the way to allow more to spawn!"
                        )
                        self.set_game_state(GameState(cars=car_states))

            time.sleep(0.1)
        self.logger.info('Gave up waiting for valid packet :(')
Exemplo n.º 8
0
 def prepare_for_run(self):
     # Set up shared memory map (offset makes it so bot only writes to its own input!) and map to buffer
     self.bot_input = PlayerInput()
     # Set up shared memory for game data
     self.game_tick_packet = gd.GameTickPacket(
     )  # We want to do a deep copy for game inputs so people don't mess with em
Exemplo n.º 9
0
 def __init__(self):
     self.logger = get_logger('packet reader')
     self.game_interface = GameInterface(self.logger)
     self.game_interface.inject_dll()
     self.game_interface.load_interface()
     self.game_tick_packet = game_data_struct.GameTickPacket()
Exemplo n.º 10
0
    def launch_bot_process_helper(self,
                                  early_starters_only=False,
                                  match_config: MatchConfig = None):
        # Start matchcomms here as it's only required for the bots.
        self.kill_matchcomms_server()
        self.matchcomms_server = launch_matchcomms_server()
        self.bot_processes = {
            ind: proc
            for ind, proc in self.bot_processes.items() if proc.is_alive()
        }

        num_started = 0

        # Launch processes
        # TODO: this might be the right moment to fix the player indices based on a game tick packet.
        packet = game_data_struct.GameTickPacket()
        self.game_interface.update_live_data_packet(packet)

        # TODO: root through the packet and find discrepancies in the player index mapping.
        for i in range(
                min(self.num_participants, len(match_config.player_configs))):

            player_config = match_config.player_configs[i]
            if not player_config.has_bot_script():
                continue
            if early_starters_only and not self.bot_bundles[
                    i].supports_early_start:
                continue

            spawn_id = player_config.spawn_id

            if early_starters_only:
                # Danger: we have low confidence in this since we're not leveraging the spawn id.
                participant_index = i
            else:
                participant_index = None

                self.logger.info(
                    f'Player in slot {i} was sent with spawn id {spawn_id}, will search in the packet.'
                )
                for n in range(0, packet.num_cars):
                    packet_spawn_id = packet.game_cars[n].spawn_id
                    if spawn_id == packet_spawn_id:
                        self.logger.info(
                            f'Looks good, considering participant index to be {n}'
                        )
                        participant_index = n
                if participant_index is None:
                    for prox_index, proc_info in self.bot_processes.items():
                        if spawn_id == proc_info.player_config.spawn_id:
                            participant_index = prox_index
                    if participant_index is None:
                        raise Exception(
                            f"Unable to determine the bot index for spawn id {spawn_id}"
                        )

            if participant_index not in self.bot_processes:
                bundle = get_bot_config_bundle(player_config.config_path)
                name = str(self.start_match_configuration.
                           player_configuration[i].name)
                if bundle.supports_standalone:
                    executable = sys.executable
                    if bundle.use_virtual_environment:
                        executable = str(
                            Path(bundle.config_directory) / 'venv' /
                            'Scripts' / 'python.exe')
                    process = subprocess.Popen(
                        [
                            executable, bundle.python_file, '--config-file',
                            str(player_config.config_path), '--name', name,
                            '--team',
                            str(self.teams[i]), '--player-index',
                            str(participant_index), '--spawn-id',
                            str(spawn_id), '--matchcomms-url',
                            self.matchcomms_server.root_url.geturl()
                        ],
                        cwd=Path(bundle.config_directory).parent)
                    self.bot_processes[participant_index] = BotProcessInfo(
                        process=None,
                        subprocess=process,
                        player_config=player_config)

                    # Insert immediately into the agent metadata map because the standalone process has no way to communicate it back out
                    self.agent_metadata_map[participant_index] = AgentMetadata(
                        participant_index, name, self.teams[i], {process.pid})
                else:
                    reload_request = mp.Event()
                    quit_callback = mp.Event()
                    self.bot_reload_requests.append(reload_request)
                    self.bot_quit_callbacks.append(quit_callback)
                    process = mp.Process(
                        target=SetupManager.run_agent,
                        args=(self.quit_event, quit_callback, reload_request,
                              self.bot_bundles[i], name, self.teams[i],
                              participant_index, self.python_files[i],
                              self.agent_metadata_queue, match_config,
                              self.matchcomms_server.root_url, spawn_id))
                    process.start()
                    self.bot_processes[participant_index] = BotProcessInfo(
                        process=process,
                        subprocess=None,
                        player_config=player_config)
                num_started += 1

        self.logger.info(f"Successfully started {num_started} bot processes")

        process_configuration.configure_processes(self.agent_metadata_map,
                                                  self.logger)

        scripts_started = 0
        for script_config in match_config.script_configs:
            script_config_bundle = get_script_config_bundle(
                script_config.config_path)
            if early_starters_only and not script_config_bundle.supports_early_start:
                continue
            executable = sys.executable
            if script_config_bundle.use_virtual_environment:
                executable = str(
                    Path(script_config_bundle.config_directory) / 'venv' /
                    'Scripts' / 'python.exe')

            process = subprocess.Popen(
                [executable, script_config_bundle.script_file],
                cwd=Path(script_config_bundle.config_directory).parent)
            self.logger.info(
                f"Started script with pid {process.pid} using {process.args}")
            self.script_processes[process.pid] = process
            scripts_started += 1

        self.logger.debug(f"Successfully started {scripts_started} scripts")

        return num_started
Exemplo n.º 11
0
    def launch_bot_process_helper(self, early_starters_only=False):
        # Start matchcomms here as it's only required for the bots.
        self.kill_matchcomms_server()
        self.matchcomms_server = launch_matchcomms_server()
        self.bot_processes = {
            ind: proc
            for ind, proc in self.bot_processes.items() if proc.is_alive()
        }

        num_started = 0

        # Launch processes
        # TODO: this might be the right moment to fix the player indices based on a game tick packet.
        packet = game_data_struct.GameTickPacket()
        self.game_interface.update_live_data_packet(packet)

        # TODO: root through the packet and find discrepancies in the player index mapping.
        for i in range(
                min(self.num_participants,
                    len(self.match_config.player_configs))):

            player_config = self.match_config.player_configs[i]
            if not player_config.has_bot_script():
                continue
            if early_starters_only and not self.bot_bundles[
                    i].supports_early_start:
                continue

            spawn_id = self.game_interface.start_match_configuration.player_configuration[
                i].spawn_id

            if early_starters_only:
                # Danger: we have low confidence in this since we're not leveraging the spawn id.
                participant_index = i
            else:
                participant_index = None

                self.logger.info(
                    f'Player in slot {i} was sent with spawn id {spawn_id}, will search in the packet.'
                )
                for n in range(0, packet.num_cars):
                    packet_spawn_id = packet.game_cars[n].spawn_id
                    if spawn_id == packet_spawn_id:
                        self.logger.info(
                            f'Looks good, considering participant index to be {n}'
                        )
                        participant_index = n
                if participant_index is None:
                    raise Exception("Unable to determine the bot index!")

            if participant_index not in self.bot_processes:
                reload_request = mp.Event()
                quit_callback = mp.Event()
                self.bot_reload_requests.append(reload_request)
                self.bot_quit_callbacks.append(quit_callback)
                process = mp.Process(
                    target=SetupManager.run_agent,
                    args=(self.quit_event, quit_callback, reload_request,
                          self.bot_bundles[i],
                          str(self.start_match_configuration.
                              player_configuration[i].name), self.teams[i],
                          participant_index, self.python_files[i],
                          self.agent_metadata_queue, self.match_config,
                          self.matchcomms_server.root_url, spawn_id))
                process.start()
                self.bot_processes[participant_index] = process
                num_started += 1

        self.logger.debug(f"Successfully started {num_started} bot processes")

        scripts_started = 0
        for script_config in self.match_config.script_configs:
            script_config_bundle = get_script_config_bundle(
                script_config.config_path)
            if early_starters_only and not script_config_bundle.supports_early_start:
                continue
            process = subprocess.Popen(
                [sys.executable, script_config_bundle.script_file],
                shell=True,
                cwd=Path(script_config_bundle.config_directory).parent)
            self.logger.info(
                f"Started script with pid {process.pid} using {process.args}")
            self.script_processes[process.pid] = process
            scripts_started += 1

        self.logger.debug(f"Successfully started {scripts_started} scripts")

        return num_started