def __init__(self, name, team, index): super().__init__(name, team, index) self.info = GameInfo(index, team) self.last_turn = 0 self.controls = SimpleControllerState() self.controls.throttle = 1 self.controls.boost = 1
def initialize_agent(self): #self.gui = GUI() #Paul has a nice GUI that allows for drawing paths, creating and moving/editing events self.info = GameInfo(self.index, self.team) team = 0 if self.team == 1: team = -1 else: team = 1 self.action_state = '' self.frames = 0 self.yeet_start = time.time() self.yeet_kick_off = KickoffAgent('eat ass', self.info.team, self.info.index) self.last_frame = False self.flip_idx = 0 self.lock = threading.Lock() self.fps = 60 self.jump_frames = 0 self.kick = True self.car_locations = [ Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4) ]
def __init__(self, name, team, index): self.index = index self.info = GameInfo(index, team) self.team = team self.controls = SimpleControllerState() self.action = None self.last_time = 0.0 self.dt = 1.0 / 120.0 self.goals = 0 self.timer = 0.0 self.kickoff_pos = None self.state = None self.target = None self.target_speed = 1800 self.drift = False #bot parameters self.target_range = 200 self.low_boost = 25 self.max_ball_dist = 4000 self.def_extra_dist = 800 self.turn_c_quality = 20 self.min_target_s = 1000 self.dodge_dist = 400 self.RLwindow = [0]*4
def __init__(self, game_interface: GameInterface): super().__init__() self.game_interface = game_interface self.game_info = GameInfo(0, 0) self.renderer = self.game_interface.renderer self.drones_per_ring = 8
def __init__(self, name, team, index): self.index = index self.info = GameInfo(index, team) self.controls = SimpleControllerState() self.timer = 0.0 self.action = None
def __init__( self, agent, bot_container, model1_path=None, model2_path=None, save_path=None, mode=0, render=False, ): """ AgentModule. agent: The agent that this is running on model1_path: Path to load model to be trained from. Defaults to making a new one. model2_path: Path to load model to be learned from (Used for transfer learning only). save_path: Path to save the model being trained (Used only when mode is not 0). Defaults to not saving. mode: 0 - Just playing the game. 1 - Real time learning from human teacher (Rendering should be enabled). 2 - Transfer learning from model specified in model2. render: Boolean, forced rendering when mode = 1 """ self.training_history = [[], []] self.count = 1 self.ticks_per_update = 60 * 60 #~60 seconds self.save_path = save_path model2_bot_num = 0 self.flags = { "render": render or mode == 1, "train": mode == 1, "transfer": mode == 2 } if self.flags["train"] and self.flags["transfer"]: raise Exception("Both train and transfer flags cannot be true.") self.info = GameInfo(agent.index, agent.team) self.bots = bot_container(agent) self.ui = TrainingUI(self.bots) self.model = ModelBox(self.bots.num_bots(), agent.index, agent.team) self.controls = SimpleControllerState() self.reader = None if self.flags["train"] or self.flags["transfer"]: from keyboard_interface import reader self.reader = reader self.reader.set_bot_num(self.bots.num_bots()) if model1_path != None: self.model.load(model1_path) if self.flags["transfer"]: self.model2 = ModelBox(model2_bot_num, agent.index, agent.team) if model2_path != None: self.model2.load(model2_path) self.paused = True
def initialize_agent(self): self.info = GameInfo(self.index, self.team, self.get_field_info()) self.controls = SimpleControllerState() self.phase = None self.targets = [] self.guides = [] self.active_pads = [] self.plan_pads = []
def initialize_agent(self): self.model = self.get_model() self.input_formatter = self.create_input_formatter() self.output_formatter = self.create_output_formatter() self.model.load_state_dict(self.torch.load(self.get_file_path())) # initialize kickoff self.info = GameInfo(self.index, self.team, self.get_field_info())
class Paul(BaseAgent): def initialize_agent(self): #self.gui = GUI() #Paul has a nice GUI that allows for drawing paths, creating and moving/editing events self.info = GameInfo(self.index, self.team) team = 0 if self.team == 1: team = -1 else: team = 1 self.action_state = '' self.frames = 0 self.yeet_start = time.time() self.yeet_kick_off = KickoffAgent('eat ass', self.info.team, self.info.index) self.last_frame = False self.flip_idx = 0 self.lock = threading.Lock() self.fps = 60 self.jump_frames = 0 self.kick = True self.car_locations = [ Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4), Queue(maxsize=4) ] #self.orange_kickoff_hierarchy = [[2048, -2560, 0], [-2048, -2560, 0], [256, -3840, 0], [-256, -3840, 0]] def get_output(self, packet): self.info.read_packet(packet) game = self.convert_packet_to_v3( packet ) #Paul is a V3 Bot, a rework of preprocessing would be needed to re-optimize it self.frames = self.frames + 1 self.renderer.begin_rendering() self.renderer.draw_string_2d(50, 50, 5, 5, str(time.time() - self.yeet_start), self.renderer.red()) self.renderer.end_rendering() self.fps = self.frames / (time.time() - self.yeet_start) output = Bot.Process(self, game, packet, self.info, self.lock) controls = self.convert_output_to_v4(output) if (packet.game_info.is_kickoff_pause): controls = self.yeet_kick_off.get_output( packet, self.frames / (time.time() - self.yeet_start)) self.last_frame = True return controls else: self.kick = True self.yeet_kick_off = KickoffAgent('eat ass', self.info.team, self.info.index) return controls def is_hot_reload_enabled(self): return False
class Agent(BaseAgent): def __init__(self, name, team, index): self.info = GameInfo(index, team) self.controls = SimpleControllerState() self.action = None self.counter = 0 def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) if self.action == None or self.action.finished or self.counter == 400: target_pos = vec3( random.uniform(-3000, 3000), random.uniform(-2000, 2000), 25 ) target_speed = random.uniform(500, 2000) self.action = Drive(self.info.my_car, target_pos, target_speed) self.counter = 0 r = 200 self.renderer.begin_rendering() purple = self.renderer.create_color(255, 230, 30, 230) self.renderer.draw_line_3d(self.action.target_pos - r * vec3(1, 0, 0), self.action.target_pos + r * vec3(1, 0, 0), purple) self.renderer.draw_line_3d(self.action.target_pos - r * vec3(0, 1, 0), self.action.target_pos + r * vec3(0, 1, 0), purple) self.renderer.draw_line_3d(self.action.target_pos - r * vec3(0, 0, 1), self.action.target_pos + r * vec3(0, 0, 1), purple) self.renderer.end_rendering() if controller.L1: self.controls = controller.get_output() else: self.counter += 1 if (self.counter % 10) == 0: print(f"current speed: {norm(self.info.my_car.vel):4.4f}, \ desired speed: {self.action.target_speed:4.4f}") self.action.step(0.01666) self.controls = self.action.controls return self.controls
def update(self, master=None, agent=None, car=None, target=None, speed=None): self.master = master self.agent = agent self.car = car self.target = target self.speed = speed self.Ginfo = GameInfo(agent.index, agent.team)
def __init__(self, name, team, index): self.index = index self.info = GameInfo(index, team) self.controls = SimpleControllerState() self.skip = False self.timer = 0.0 self.action = None self.car_predictions = [] self.ball_predictions = [] self.csign = 1; self.bsign = 1;
class ATBA(BaseAgent): def initialize_agent(self): self.info = GameInfo(self.index, self.team) def get_output(self, packet: GameTickPacket) -> SimpleControllerState: return SimpleControllerState() def atba_fast(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) return self.drive_controls(self.info.ball.pos, target_speed=Speed.fast) def atba_normal(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) return self.drive_controls(self.info.ball.pos, target_speed=Speed.normal) def atba_slow(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) return self.drive_controls(self.info.ball.pos, target_speed=Speed.slow) def atba_wait(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) return self.drive_controls(self.info.ball.pos, target_speed=Speed.stop) def drive_controls(self, target, target_speed): self.action = self.action = Drive(self.info.my_car, target_pos = target, target_speed=target_speed) self.action.step(1/60) return self.action.controls
def __init__(self, master=None, agent=None, car=None, target=None, speed=None): self.name = 'RECOVER' self.master = master self.agent = agent self.car = car self.target = target self.speed = speed self.controls = SimpleControllerState() self.expired = False self.Ginfo = GameInfo(agent.index, agent.team)
class KickoffAgent(): def __init__(self, name, team, index): #This runs once before the bot starts up self.controller_state = SimpleControllerState() self.info = GameInfo(index, team) self.state = 'init' self.team = team def get_output(self, packet: GameTickPacket, fps) -> SimpleControllerState: self.info.read_packet(packet) if (self.state == 'init'): self.hit = Kickoff(info=self.info, fps=fps) self.state = 'run' #creating a basis for the field return self.hit.execute(self.info, fps).to_simple_controller_state()
class Snek(BaseAgent): def __init__(self, name, team, index): super().__init__(name, team, index) self.info = GameInfo(index, team) self.last_turn = 0 self.controls = SimpleControllerState() self.controls.throttle = 1 self.controls.boost = 1 def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) halfpi = math.pi / 2 car = self.info.my_car ball = self.info.ball delta_local = dot(ball.pos - car.pos, car.theta) ang = math.atan2(delta_local[1], delta_local[0]) car_state = CarState(boost_amount=100) if ang > math.pi / 2 and self.last_turn + TURN_COOLDOWN < time.time(): self.last_turn = time.time() m = axis_rotation(vec3(0, 0, halfpi)) nvel = dot(m, car.vel) nyaw = packet.game_cars[self.index].physics.rotation.yaw + halfpi car_state.physics = Physics(velocity=Vector3( nvel[0], nvel[1], nvel[2]), rotation=Rotator(pitch=0, roll=0, yaw=nyaw)) if -ang > math.pi / 2 and self.last_turn + TURN_COOLDOWN < time.time(): self.last_turn = time.time() m = axis_rotation(vec3(0, 0, -halfpi)) nvel = dot(m, car.vel) nyaw = packet.game_cars[self.index].physics.rotation.yaw - halfpi car_state.physics = Physics(velocity=Vector3( nvel[0], nvel[1], nvel[2]), rotation=Rotator(pitch=0, roll=0, yaw=nyaw)) game_state = GameState(cars={self.index: car_state}) self.set_game_state(game_state) return self.controls
def __init__(self, name, team, index): super().__init__(name, team, index) self.controls = SimpleControllerState() self.info = GameInfo(self.index, self.team) self.t_index = 0 self.standby_position = vec3(0, 0, 300) self.direction = vec3(0, 0, 1) self.state = self.KICKOFF self.shoot_time = 0 self.last_pos = self.standby_position self.last_elapsed_seconds = 0 self.kickoff_timer_edge = False self.ball_moved = False self.next_is_super = False self.doing_super = False self.hit_pos = vec3(0, 0, 0) self.standby_initiated = False
def __init__(self, name, team, index): super().__init__(name, team, index) from steps.step_handler import StepHandler from utils.quick_chat_handler import QuickChatHandler self.quick_chat_handler: QuickChatHandler = QuickChatHandler(self) self.step_handler: StepHandler = StepHandler(self) self.game_info: GameInfo = GameInfo(self.index, self.team)
class recover(): def __init__(self, master=None, agent=None, car=None, target=None, speed=None): self.name = 'RECOVER' self.master = master self.agent = agent self.car = car self.target = target self.speed = speed self.controls = SimpleControllerState() self.expired = False self.Ginfo = GameInfo(agent.index, agent.team) def update(self, master=None, agent=None, car=None, target=None, speed=None): self.master = master self.agent = agent self.car = car self.target = target self.speed = speed self.Ginfo = GameInfo(agent.index, agent.team) def available(self): return not self.car.has_wheel_contact def step(self, tick): self.Ginfo.read_packet(self.agent.packet) action = AerialTurn(self.Ginfo.my_car) action.step(tick) action.controls.throttle = 1 self.expired = action.finished self.expired = self.car.has_wheel_contact return action.controls
class BaseTestAgent(BaseAgent): def __init__(self, name, team, index): super(BaseTestAgent, self).__init__(name, team, index) self.action = self.create_action() self.info = GameInfo(index, team) def get_output(self, game_tick_packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(game_tick_packet) self.test_process(game_tick_packet) return self.action.get_output(self.info) def create_action(self) -> BaseAction: raise NotImplementedError def test_process(self, game_tick_packet: GameTickPacket): raise NotImplementedError def initialize_agent(self): raise NotImplementedError
class TorusChoreography(Choreography): def __init__(self, game_interface: GameInterface): super().__init__() self.game_interface = game_interface self.game_info = GameInfo(0, 0) self.renderer = self.game_interface.renderer self.drones_per_ring = 8 def pre_step(self, packet: GameTickPacket, drones: List[Drone]): self.game_info.read_packet(packet) @staticmethod def get_num_bots(): return 48 def generate_sequence(self, drones): self.sequence.clear() if len(drones) == 0: return pause_time = 0.2 self.sequence.append(HideBall()) self.sequence.append(LetAllCarsSpawn(self.get_num_bots())) self.sequence.append( BlindBehaviorStep(SimpleControllerState(), pause_time)) num_rings = len(drones) // self.drones_per_ring torus_period = 2 * math.pi / TORUS_RATE self.sequence.append( SubGroupOrchestrator(group_list=[ TorusSubChoreography( self.game_interface, self.game_info, -i * torus_period / num_rings, drones[i * self.drones_per_ring:(i + 1) * self.drones_per_ring], 0) for i in range(0, num_rings) ]))
def tick(self, packet: GameTickPacket) -> StepResult: if self.game_info is None: self.game_info = GameInfo(self.index, packet.game_cars[self.index].team) self.game_info.read_packet(packet) if self.aerial is None: self.aerial = Aerial( self.game_info.my_car, vec3(self.target.x, self.target.y, self.target.z), self.arrival_time) self.aerial.step(SECONDS_PER_TICK) controls = SimpleControllerState() controls.boost = self.aerial.controls.boost controls.pitch = self.aerial.controls.pitch controls.yaw = self.aerial.controls.yaw controls.roll = self.aerial.controls.roll controls.jump = self.aerial.controls.jump return StepResult(controls, packet.game_info.seconds_elapsed > self.arrival_time)
class AerialStep(Step): """ This uses the Aerial controller provided by RLUtilities. Thanks chip! It will take care of jumping off the ground and flying toward the target. This will only work properly if you call tick repeatedly on the same instance. """ def __init__(self, target: Vec3, arrival_time: float, index: int): self.index = index self.aerial: Aerial = None self.game_info: GameInfo = None self.target = target self.arrival_time = arrival_time def tick(self, packet: GameTickPacket) -> StepResult: if self.game_info is None: self.game_info = GameInfo(self.index, packet.game_cars[self.index].team) self.game_info.read_packet(packet) if self.aerial is None: self.aerial = Aerial( self.game_info.my_car, vec3(self.target.x, self.target.y, self.target.z), self.arrival_time) self.aerial.step(SECONDS_PER_TICK) controls = SimpleControllerState() controls.boost = self.aerial.controls.boost controls.pitch = self.aerial.controls.pitch controls.yaw = self.aerial.controls.yaw controls.roll = self.aerial.controls.roll controls.jump = self.aerial.controls.jump return StepResult(controls, packet.game_info.seconds_elapsed > self.arrival_time)
def initialize_agent(self): self.info: GameInfo = GameInfo(self.index, self.team) self.controls: SimpleControllerState = SimpleControllerState() self.maneuver: Maneuver = None self.time = 0 self.prev_time = 0 self.last_touch_time = 0 self.reset_time = 0 self.ticks = 0 self.draw: DrawingTool = DrawingTool(self.renderer) self.strategy = SoccarStrategy(self.info) # variables related to quick chats self.chat = QuickChatTool(self) self.last_ball_vel = 0 self.said_gg = False self.last_time_said_all_yours = 0 self.num_of_our_goals_reacted_to = 0 self.num_of_their_goals_reacted_to = 0
def __init__(self, name, team, index): super(BaseTestAgent, self).__init__(name, team, index) self.action = self.create_action() self.info = GameInfo(index, team)
class Agent(BaseAgent): def __init__(self, name, team, index): self.index = index self.info = GameInfo(index, team) self.controls = SimpleControllerState() self.timer = 0.0 self.action = None def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) self.controls = SimpleControllerState() if self.timer < 0.05: position = Vector3(random.uniform(-4000, 4000), random.uniform(-3000, 3000), random.uniform(500, 2000)) velocity = Vector3(random.uniform(-1500, 1500), random.uniform(-1500, 1500), random.uniform(-1000, -500)) rotation = Rotator(random.uniform(-1.5, 1.5), random.uniform(-1.5, 1.5), random.uniform(-1.5, 1.5)) angular_velocity = Vector3(random.uniform(-3.0, 3.0), random.uniform(-3.0, 3.0), random.uniform(-3.0, 3.0)) car_state = CarState( physics=Physics(location=position, velocity=velocity, rotation=rotation, angular_velocity=angular_velocity)) self.set_game_state(GameState(cars={self.index: car_state})) if self.timer > 0.10: if self.action == None: self.action = AerialTurn(self.info.my_car) self.renderer.begin_rendering() red = self.renderer.create_color(255, 255, 30, 30) self.renderer.draw_polyline_3d(self.action.trajectory, red) self.renderer.end_rendering() self.action.step(1.0 / 60.0) self.controls = self.action.controls self.timer += 1.0 / 60.0 if self.action.finished or self.timer > 5.0: print("target:\n", self.action.target) print("theta:\n", self.action.car.theta) print() self.timer = 0.0 self.action = None self.timer += 1.0 / 60.0 return self.controls
class BallPredictor: def __init__(self, index, team): self.index = index self.mass_car = 2.4 self.mass_ball = 4.5 self.car = CarModel(index) self.info = GameInfo(index, team) def update(packet): self.info.read_packet(packet) self.car.update(packet) def get_path( time, dt ): path = [] t, path1, path2=self.will_hit( ball, self.car, time ) newvel = self.predict_ball(ball, self.car) ball = Ball( self.info.ball ) swapped = False for i in range(int(time/dt)): ball.step(dt) path.append(vec3(ball.pos)) if i > t and not swapped: ball.vel = vec3(newvel[0].item(), newvel[1].item(), newvel[2].item()) swapped = True return path def get_step( self, p1, p2 ): #Really bad Approximation distance = self.distance( p1, p2 ) slopes = (p2 - p1)/distance return slopes def get_impact_point( self, loc ): step = self.get_step( self.car_pos, loc ) current = self.car_pos while self.point_in_car( current ): current = current + step return current def point_in_car( self, point ): point = point - self.car_pos point = self.rotate_axis(point, -self.car_rot[0], 0) point = self.rotate_axis(point, -self.car_rot[1], 1) point = self.rotate_axis(point, -self.car_rot[2], 2) between = [] for i in range(3): between.append( -self.car_dims[i].item()/2 < point[i].item() and point[i].item() < self.car_dims[i].item()/2 ) return between[0] and between[1] and between[2] def rotate_axis( self, x, rotation, axis ): x = x.unsqueeze(0) if axis == 0: r_x = rotation R_x = np.array([[1, 0, 0], [0, r_x.cos(), -1*r_x.sin()], [0, r_x.sin(), r_x.cos()]]) x = np.dot( x, R_x ) if axis == 1: r_y = rotation R_y = np.array([[r_y.cos(), 0, r_y.sin()], [0, 1, 0], [-1*r_y.sin(), 0, r_y.cos()]]) x = np.dot(x, R_y) if axis == 2: r_z = rotation R_z = np.array([[r_z.cos(), -1*r_z.sin(), 0], [r_z.sin(), r_z.cos(), 0], [0, 0, 1]]) x = np.dot(x, R_z) return x[0] def distance( self, p1, p2 ): return np.sum( (p1 - p2)**2 )**0.5 def predict_ball(self, ball_info, player_info): ball_pos, ball_vel, ball_avel = self.parse_ball_info( ball_info ) self.car_pos, self.car_vel, self.car_rot, self.car_avel = player_info.pos, player_info.vel, player_info.rot, player_info.avel impact_pos = self.get_impact_point( ball_pos ) impact_step_dir = self.get_step( impact_pos, ball_pos ) # This will not be accurate at all! magnitude = self.distance(self.car_vel * self.mass_car, ball_vel * self.mass_ball) J = impact_step_dir * magnitude # This will be more accurate J_final = J #+ self.impulse_correction return ball_vel + J_final/self.mass_ball def parse_ball_info(self, ball): ball_pos = np.array([ball.pos[0], ball.pos[1], ball.pos[2]]) ball_vel = np.array([ball.vel[0], ball.vel[1], ball.vel[2]]) ball_avel = np.array([ball.omega[0], ball.omega[1], ball.omega[2]]) return ball_pos, ball_vel, ball_avel
def __init__(self, index, team): self.index = index self.mass_car = 2.4 self.mass_ball = 4.5 self.car = CarModel(index) self.info = GameInfo(index, team)
def __init__(self, name, team, index): #This runs once before the bot starts up self.controller_state = SimpleControllerState() self.info = GameInfo(index, team) self.state = 'init' self.team = team
class Agent(BaseAgent): def __init__(self, name, team, index): self.index = index self.info = GameInfo(index, team) self.controls = SimpleControllerState() self.timer = 0.0 self.timeout = 3.0 self.action = None self.state = State.RESET def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) self.controls = SimpleControllerState() next_state = self.state if self.state == State.RESET: self.timer = 0.0 # put the car in the middle of the field car_state = CarState( physics=Physics(location=Vector3(0, 0, 20), velocity=Vector3(1000, 0, 0), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) # put the ball somewhere out of the way ball_state = BallState( physics=Physics(location=Vector3(0, -3000, 100), velocity=Vector3(0, 0, 0), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state( GameState(ball=ball_state, cars={self.index: car_state})) next_state = State.WAIT if self.state == State.WAIT: if self.timer > 0.2: next_state = State.INITIALIZE if self.state == State.INITIALIZE: self.action = HalfFlip(self.info.my_car) next_state = State.RUNNING if self.state == State.RUNNING: self.action.step(0.01666) self.controls = self.action.controls if self.timer > self.timeout: next_state = State.RESET self.timer += 0.01666 self.state = next_state return self.controls