class SocketDataReporter: def __init__(self): self.latest_packet: GameTickPacket = None self.latest_field_info: FieldInfo = None self.latest_match_settings: MatchSettings = None self.socket_relay = SocketRelay() self.socket_relay.packet_handlers.append(self._handle_packet) self.socket_relay.field_info_handlers.append(self._handle_field_info) self.socket_relay.match_settings_handlers.append( self._handle_match_settings) self.thread = Thread(target=self.socket_relay.connect_and_run, args=(False, False, False)) self.thread.start() def _handle_packet(self, packet: GameTickPacket): self.latest_packet = packet def _handle_field_info(self, field_info: FieldInfo): self.latest_field_info = field_info def _handle_match_settings(self, match_settings: MatchSettings): self.latest_match_settings = match_settings def disconnect(self): self.socket_relay.disconnect()
class SampleScript: def __init__(self): self.socket_relay = SocketRelay() def handle_spectate(self, spectate: PlayerSpectate, seconds: float, frame_num: int): print(f'Spectating player index {spectate.PlayerIndex()}') # Make the bot jump whenever we start spectating them >:) controls = SimpleControllerState() controls.jump = True self.socket_relay.send_player_input(spectate.PlayerIndex(), controls) def handle_stat(self, stat: PlayerStatEvent, seconds: float, frame_num: int): stat_value = stat.StatType().decode('utf-8') print(f'Stat player index {stat.PlayerIndex()}: {stat_value}') def input_change(self, change: PlayerInputChange, seconds: float, frame_num: int): if change.ControllerState().Jump(): print(f'Player index {change.PlayerIndex()} is jumping!') def run(self): self.socket_relay.player_spectate_handlers.append(self.handle_spectate) self.socket_relay.player_stat_handlers.append(self.handle_stat) self.socket_relay.player_input_change_handlers.append(self.input_change) self.socket_relay.connect_and_run(wants_quick_chat=True, wants_game_messages=True, wants_ball_predictions=True)
def get_one_packet() -> GameTickPacket: socket_relay = SocketRelay() packet = None def handle_packet(p): nonlocal packet packet = p socket_relay.disconnect() socket_relay.packet_handlers.append(handle_packet) socket_relay.connect_and_run(False, False, False) return packet
def start_match(self): socket_relay = SocketRelay() match_config = self.match_config def connect_handler(): socket_relay.send_match_config(match_config) socket_relay.disconnect() socket_relay.on_connect_handlers.append(connect_handler) try: socket_relay.connect_and_run(False, False, False) except timeout: raise TimeoutError("Took too long to connect to RLBot.exe!")
def __init__(self): self.latest_packet: GameTickPacket = None self.latest_field_info: FieldInfo = None self.latest_match_settings: MatchSettings = None self.socket_relay = SocketRelay() self.socket_relay.packet_handlers.append(self._handle_packet) self.socket_relay.field_info_handlers.append(self._handle_field_info) self.socket_relay.match_settings_handlers.append( self._handle_match_settings) self.thread = Thread(target=self.socket_relay.connect_and_run, args=(False, False, False)) self.thread.start()
class SampleScript: def __init__(self): self.socket_relay = SocketRelay() def start_match(self): match_config = read_match_config_from_file( Path(__file__).parent.parent.parent.parent.parent.parent / 'rlbot.cfg') print("Sending match config") self.socket_relay.send_match_config(match_config) self.socket_relay.disconnect() def run(self): self.socket_relay.on_connect_handlers.append(self.start_match) self.socket_relay.connect_and_run(wants_quick_chat=False, wants_game_messages=False, wants_ball_predictions=False)
def main(self): self.set_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), "SuperchargedBots.cfg")) self.teams = [] if self.get_bool_from_config("Options", "help_blue_team"): self.teams.append(0) print(f"SuperchargedBots: help_blue_team = {0 in self.teams}") if self.get_bool_from_config("Options", "help_orange_team"): self.teams.append(1) print(f"SuperchargedBots: help_orange_team = {1 in self.teams}") self.bots_only = self.get_bool_from_config("Options", "bots_only") print(f"SuperchargedBots: bots_only = {self.bots_only}") self.bonus_boost_accel_percent = self.get_float_from_config("Options", "bonus_boost_accel_percent") / 100 print(f"SuperchargedBots: bonus_boost_accel_percent = {self.bonus_boost_accel_percent * 100}%") self.bonus_boost_tank = self.get_int_from_config("Options", "bonus_boost_tank") print(f"SuperchargedBots: bonus_boost_tank = {self.bonus_boost_tank}") self.minimum_boost = self.get_int_from_config("Options", "minimum_boost") print(f"SuperchargedBots: minimum_boost = {self.minimum_boost}") self.bonus_hit_percent = self.get_int_from_config("Options", "bonus_hit_percent") print(f"SuperchargedBots: bonus_hit_percent = {self.bonus_hit_percent}") self.demo_helper = self.get_bool_from_config("Options", "demo_helper") print(f"SuperchargedBots: demo_helper = {self.demo_helper}") self.socket_relay = SocketRelay() self.socket_relay.player_input_change_handlers.append(self.input_change) self.non_blocking_socket_relay = Thread(target=self.socket_relay.connect_and_run, args=(False, True, False)) self.non_blocking_socket_relay.start() while 1: try: self.packet: GameTickPacket = self.wait_game_tick_packet() time = self.packet.game_info.seconds_elapsed self.delta_time = time - self.time self.time = time supercharged_bots = [] cars = dict() for car_index in range(self.packet.num_cars): car = self.packet.game_cars[car_index] if (self.bots_only and not car.is_bot) or car.team not in self.teams: continue if car.name not in self.tracker: self.tracker[car.name] = DEFAULT_CAR.copy() supercharged_bots.append(car.name) if not self.packet.game_info.is_round_active: continue if self.packet.game_info.is_kickoff_pause: self.tracker[car.name]['total_boost'] = BOOST_CONSUMPTION self.tracker[car.name]['last_boost'] = BOOST_CONSUMPTION continue velocity = None if self.demo_helper: for other_car_index in range(self.packet.num_cars): other_car = self.packet.game_cars[other_car_index] if car.team == other_car.team: continue car_location = Vector.from_vector(car.physics.location) other_car_location = Vector.from_vector(other_car.physics.location) if car_location.flat_dist(other_car_location) < 200 and abs(Vector.from_vector(car.physics.velocity).angle(other_car_location - car_location)) < 0.5: velocity = Vector.from_vector(car.physics.velocity).flatten().scale(2300) if self.tracker[car.name]['boosting']: if not self.tracker[car.name]['steering'] and (car.boost > self.minimum_boost): CP = math.cos(car.physics.rotation.pitch) SP = math.sin(car.physics.rotation.pitch) CY = math.cos(car.physics.rotation.yaw) SY = math.sin(car.physics.rotation.yaw) forward = Vector(CP*CY, CP*SY, SP) if velocity is None: velocity = Vector.from_vector(car.physics.velocity) + forward * (BOOST_ACCEL * self.delta_time * self.bonus_boost_accel_percent) self.tracker[car.name]['total_boost'] -= BOOST_CONSUMPTION * self.delta_time * (100 / self.bonus_boost_tank) boost_amount = None if car.boost > self.minimum_boost and car.boost > self.tracker[car.name]['last_boost']: self.tracker[car.name]['total_boost'] += car.boost - self.tracker[car.name]['last_boost'] elif car.boost < self.minimum_boost: self.tracker[car.name]['total_boost'] = self.minimum_boost self.tracker[car.name]['total_boost'] = cap(self.tracker[car.name]['total_boost'], 0, 100) floored_boost = math.floor(self.tracker[car.name]['total_boost']) if floored_boost != car.boost: boost_amount = floored_boost self.tracker[car.name]['last_boost'] = car.boost if boost_amount is None else boost_amount if velocity is None and boost_amount is None: continue cars[car_index] = CarState( Physics( velocity=None if velocity is None else Vector3(*velocity) ), boost_amount=boost_amount ) last_ball_touch = self.packet.game_ball.latest_touch ball = None if last_ball_touch.time_seconds > self.last_ball_touch_time: if (last_ball_touch.time_seconds - self.last_ball_touch_time) > 0.5: if not self.bots_only or self.packet.game_cars[last_ball_touch.player_index].is_bot: if last_ball_touch.team in self.teams: bonus_hit_multiplier = self.bonus_hit_percent / 100 + 1 ball_velocity = Vector.from_vector(self.packet.game_ball.physics.velocity) * Vector(bonus_hit_multiplier, bonus_hit_multiplier, 1 / bonus_hit_multiplier) ball = BallState(physics=Physics( velocity=Vector3(*ball_velocity) )) self.last_ball_touch_time = last_ball_touch.time_seconds game_state = GameState() if cars: game_state.cars = cars if ball is not None: game_state.ball = ball self.set_game_state(game_state) if self.last_packet_time == -1 or self.time - self.last_packet_time >= 0.1: self.matchcomms.outgoing_broadcast.put_nowait({ "supercharged_bots": supercharged_bots, "supercharged_config": { "bonus_boost_accel_percent": self.bonus_boost_accel_percent, "bonus_boost_tank": self.bonus_boost_tank, "minimum_boost": self.minimum_boost, "bonus_hit_percent": self.bonus_hit_percent, "demo_helper": self.demo_helper, } }) except Exception: print_exc()
def __init__(self): self.socket_relay = SocketRelay()