class DrivesToBallExercise(TrainingExercise): """ Checks that we drive to the ball when it's in the center of the field. """ grader: Grader = field(default_factory=DriveToBallGrader) carPos: Vector3 = Vector3(0, 0, 0) carSpeed: Vector3 = 500 ballPos: Vector3 = Vector3(-1500, -555, 100) ballVel: Vector3 = Vector3(0, 0, 500) def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: return GameState( ball=BallState(physics=Physics( location=self.ballPos, velocity=self.ballVel, angular_velocity=Vector3(0, 0, 0))), cars={ 0: CarState( physics=Physics( location=self.carPos, rotation=Rotator(0, -3.1416 / 2, 0), velocity=Vector3(0, -self.carSpeed, 0), angular_velocity=Vector3(0, 0, 0)), boost_amount=100) }, boosts={i: BoostState(0) for i in range(34)}, )
def main(self): while 1: packet: GameTickPacket = self.wait_game_tick_packet() # Places the ball in the air on kickoff. if packet.game_info.is_round_active: if packet.game_info.is_kickoff_pause: self.delay_start_time = packet.game_info.seconds_elapsed continue if packet.game_info.seconds_elapsed - self.delay_start_time < RESET_DELAY or abs( packet.game_ball.physics.location.y ) < BALL_OFFSET + packet.game_ball.collision_shape.sphere.diameter / 2: self.handled = False continue if self.handled: continue ball_y_side = -1 if packet.game_ball.physics.location.y < 0 else 1 ball_state = BallState( Physics(location=Vector3(0, 5500 * ball_y_side, 325), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state(GameState(ball=ball_state)) self.handled = True
class Spawns(): """Default Kickoffs Spawns (BLUE)""" CORNER_R = SpawnLocation(Vector3(-2048, -2560, 18), Rotator(0, 0.25 * pi, 0)) CORNER_L = SpawnLocation(Vector3(2048, -2560, 18), Rotator(0, 0.75 * pi, 0)) BACK_R = SpawnLocation(Vector3(-256, -3840, 18), Rotator(0, 0.5 * pi, 0)) BACK_L = SpawnLocation(Vector3(256.0, -3840, 18), Rotator(0, 0.5 * pi, 0)) STRAIGHT = SpawnLocation(Vector3(0, -4608, 18), Rotator(0, 0.5 * pi, 0))
def make_squares(self, packet, drones, start_time) -> StepResult: """ Separates all the bots into two squares, facing each other. """ self.squareA = drones[:32] self.squareB = drones[32:] spacing = 250 y_offset = 3500 x_offset = 7 * spacing / 2 car_states = {} for i, drone in enumerate(self.squareA): car_states[drone.index] = CarState( Physics(location=Vector3(x_offset - spacing * (i % 8), -y_offset - spacing * (i // 8), 20), velocity=Vector3(0, 0, 0), rotation=Rotator(0, np.pi / 2, 0))) for i, drone in enumerate(self.squareB): car_states[drone.index] = CarState( Physics(location=Vector3(-x_offset + spacing * (i % 8), y_offset + spacing * (i // 8), 20), velocity=Vector3(0, 0, 0), rotation=Rotator(0, -np.pi / 2, 0))) self.game_interface.set_game_state(GameState(cars=car_states)) return StepResult(finished=True)
def shoot_ball(self): ball_state = BallState( Physics(location=Vector3(2500, -4500, 100), velocity=Vector3(0, 1000, 1500), angular_velocity=Vector3(0, -10, 0))) game_state = GameState(ball=ball_state) self.set_game_state(game_state)
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: num_players = self.match_config.num_players assert num_players == len( self.spawns ), 'Number of players does not match the number of spawns.' car_states = {} for index in range(num_players): car_states[index] = CarState( boost_amount=33, physics=Physics(location=self.spawns[index].pos, velocity=Vector3(0, 0, 0), rotation=self.spawns[index].rot, angular_velocity=Vector3(0, 0, 0))) ball_state = BallState( Physics(location=Vector3(0, 0, 93), velocity=Vector3(0, 0, 0), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) # game_state = GameState(ball=ball_state, cars=car_states, console_commands=['Set WorldInfo TimeDilation 0.5']) game_state = GameState( ball=ball_state, cars=car_states, console_commands=['Set WorldInfo TimeDilation 1'], boosts={i: BoostState(0) for i in range(34)}) return game_state
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: num_players = self.match_config.num_players assert num_players == len( self.spawns ), 'Number of players does not match the number of spawns.' car_states = {} for index, player in enumerate(self.match_config.player_configs): assert (player.team == 0) == (index < len(self.blue_spawns)), \ f"Blue/Orange players in match_config don't match the Blue/Orange spawns: Expected {len(self.blue_spawns)} blue player_configs, followed by {len(self.orange_spawns)} orange ones." car_states[index] = CarState( boost_amount=33, physics=Physics(location=self.spawns[index].pos, velocity=Vector3(0, 0, 0), rotation=self.spawns[index].rot, angular_velocity=Vector3(0, 0, 0))) ball_state = BallState( Physics(location=Vector3(0, 0, 93), velocity=Vector3(0, 0, 0), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) game_state = GameState(ball=ball_state, cars=car_states) return game_state
def start(self): while True: time.sleep(0.5) # Update packet packet: GameTickPacket = self.get_game_tick_packet() if not packet.game_info.is_round_active: continue self.set_game_state( GameState(game_info=GameInfoState( world_gravity_z=WORLD_GRAVITY))) # Renders ball prediction. ball_prediction: BallPrediction = self.get_ball_prediction_struct() self.renderer.begin_rendering() sparse_slices = [ step.physics.location for step in ball_prediction.slices[::10] ] self.renderer.draw_polyline_3d(sparse_slices, self.renderer.cyan()) self.renderer.end_rendering() # Places the ball in the air on kickoff. if packet.game_info.is_kickoff_pause and round( packet.game_ball.physics.location.z ) != KICKOFF_BALL_HEIGHT: ball_state = BallState( Physics(location=Vector3(z=KICKOFF_BALL_HEIGHT), velocity=Vector3(0, 0, 0))) self.set_game_state(GameState(ball=ball_state))
def createBallStateFromGUI(self): x = float(self.entry_final_x.get()) y = float(self.entry_final_y.get()) ballState = BallState( Physics(location=Vector3(x, y, 1000), velocity=Vector3(0, 0, 0))) return ballState
def setup(self, rng: random.Random) -> GameState: self._reset_state() return GameState( ball=BallState(physics=Physics( location=self.ball_location, velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0))), cars={ 0: CarState( physics=Physics( location=self.car_location, rotation=Vector3( 0, math.atan2( # face the ball self.ball_location.y - self.car_location.y, self.ball_location.x - self.car_location.x, ), 0 ), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0)), jumped=False, double_jumped=False, boost_amount=100) }, boosts={i: BoostState(0) for i in range(34)}, )
def reset_shot(self): zero_vector = Vector3(x=0, y=0, z=0) ball_start_location = Vector3(x=0, y=0, z=800) car_start_location = Vector3(x=0, y=0, z=600) car_start_rotation = Rotator( # pitch=math.pi / 2, pitch=math.pi / 2 + random.random() * 0.1 * math.pi, yaw=random.random() * 2 * math.pi, roll=random.random() * 2 * math.pi) game_state = GameState( ball=BallState( Physics(location=ball_start_location, velocity=zero_vector, angular_velocity=zero_vector)), cars={ self.index: CarState(physics=Physics(location=car_start_location, rotation=car_start_rotation, velocity=zero_vector, angular_velocity=zero_vector)) }) self.set_game_state(game_state) self.has_started_episode = False self.has_reset = True
def set_state_test(self, game_tick_packet: GameTickPacket): my_car = game_tick_packet.game_cars[self.index] car_location = my_car.physics.location car_state = CarState() if math.fabs(car_location.x) > 2000 and car_location.z < 100: car_state = CarState(Physics(velocity=Vector3(z=500, x=-car_location.x * .5), rotation=Rotator(math.pi / 2, 0, 0), angular_velocity=Vector3(0, 0, 0)), jumped=False, double_jumped=False, boost_amount=87) ball_phys = game_tick_packet.game_ball.physics ball_state = BallState() if ball_phys.location.z > 500: ball_state = BallState(Physics(velocity=Vector3(z=-2000))) if math.fabs(car_location.x) > 1000: boost_states = {i: BoostState(1.0) for i in range(34)} else: boost_states = {i: BoostState(0.0) for i in range(34)} game_state = GameState(ball=ball_state, cars={self.index: car_state}, boosts=boost_states) self.set_game_state(game_state)
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.game.read_game_information(packet, self.get_rigid_body_tick(), self.get_field_info()) x = 2000 * math.sin(self.game.time / 2.7) y = 4900 z = 1250 + 300 * math.sin(self.game.time / 4.3 + 1.9) # put the ball somewhere out of the way ball_state = BallState( physics=Physics(location=Vector3(x, y, z), velocity=Vector3(0, 0, 0), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state(GameState(ball=ball_state)) if not self.action: self.action = FollowPath(self.game.my_car) self.action.arrival_speed = 1600 self.navigator = Navigator(self.game.my_car) if controller.L1: self.navigator.analyze_surroundings(3.0) self.action.path = self.navigator.path_to( vec3(x, y, z), vec3(1, 0, 0), self.action.arrival_speed) self.action.arrival_time = self.game.my_car.time + 3 self.controls = controller.get_output() else: # self.controls = SimpleControllerState() self.action.step(self.game.time_delta) if self.action.finished: self.controls = SimpleControllerState() else: self.controls = self.action.controls print(f"step {self.game.time_delta}\n") if self.action: vertices = self.action.path.points self.renderer.begin_rendering("path") red = self.renderer.create_color(255, 255, 255, 255) for i in range(0, len(vertices) - 1): self.renderer.draw_line_3d(vertices[i], vertices[i + 1], red) self.renderer.draw_string_2d( 50, 50, 3, 3, str(self.action.arrival_time - self.game.my_car.time), red) self.renderer.end_rendering() return self.controls
def execute(self, agent): agent.controller = dribbleController agent.fuzzy = self.fuzzy [agent.ruleset, agent.universes, agent.labels] = [self.ruleset, self.universes, self.labels] agent.num_rules = self.num_rules leftPost = Vector3([-sign(agent.team)*900 , 5150*-sign(agent.team), 93.15]) rightPost = Vector3([sign(agent.team)*900, 5150*-sign(agent.team), 93.15]) action_display = "Ballchase State" landing_spot = [] for line_point in range(0, 11): lin_space = line_point/10 point_x = future(agent.ball, lin_space*timeZ(agent.ball)) landing_spot.append([point_x.data[0], point_x.data[1] ,point_x.data[2]]) # Place all rendering based methods within begin + end_rendering agent.renderer.begin_rendering() agent_to_object(agent.renderer, agent.me, agent.ball, action_display) #agent_to_target(agent.renderer, agent.players[0], landing_spot.data) #agent_to_target(agent.renderer, agent.ball, leftPost.data) #agent_to_target(agent.renderer, agent.ball, rightPost.data) agent.renderer.draw_polyline_3d(landing_spot, agent.renderer.white()) agent.renderer.end_rendering() return agent.controller(agent)
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.game.read_game_information(packet, self.get_rigid_body_tick(), self.get_field_info()) self.controls = SimpleControllerState() if not self.action: self.action = Reorient(self.my_car) self.action.eps_phi = 0.01 self.action.eps_omega = 0.02 if self.timer == 0.0: # randomly generate a proper orthogonal matrix self.action.target_orientation = axis_to_rotation( vec3(random.uniform(-2, 2), random.uniform(-2, 2), random.uniform(-2, 2))) o = self.action.target_orientation f = vec3(o[0, 0], o[1, 0], o[2, 0]) position = Vector3(0, 0, 1000) velocity = Vector3(0, 0, 0) car_state = CarState( physics=Physics(location=position, velocity=velocity)) # spawn the ball in front of the desired orientation to visually indicate where we're trying to go ball_state = BallState( physics=Physics(location=Vector3(500 * f[0], 500 * f[1], 500 * f[2] + 1000), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state(GameState(ball=ball_state, cars={0: car_state})) self.action.step(self.game.time_delta) self.controls = self.action.controls self.timer += self.game.time_delta if self.timer > 2.0: self.timer = 0.0 error = angle_between(self.my_car.orientation, self.action.target_orientation) self.renderer.begin_rendering("path") red = self.renderer.create_color(255, 230, 30, 30) self.renderer.draw_string_2d(50, 50, 3, 3, f"error: {error:.4f} radians", red) self.renderer.draw_string_2d(50, 100, 3, 3, f"Roll: {self.controls.roll:+.2f}", red) self.renderer.draw_string_2d(50, 150, 3, 3, f"Pitch: {self.controls.pitch:+.2f}", red) self.renderer.draw_string_2d(50, 200, 3, 3, f"Yaw: {self.controls.yaw:+.2f}", red) self.renderer.end_rendering() return self.controls
def __hide(self, packet_car): print("There I go!") self.__car_sim.reset(SimPhysics.p(packet_car.physics)) p = Physics(location=Vector3(3520, 5100, 0), velocity=Vector3(0, 0, 0)) self.set_game_state(GameState(cars={self.index: CarState(physics=p)})) self.__hidden = True self.__teleporting = 0
def game_state_update(self, packet): ball_state = BallState( physics=Physics(location=Vector3(0, 0, 1500), velocity=Vector3(0, 0, 0), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) car_state = CarState(boost_amount=100) return GameState(ball=ball_state, cars={self.index: car_state})
def game_state_update(self, packet): ball_state = BallState( physics=Physics(location=Vector3(self.x, self.y, 500), velocity=Vector3(0, 0, 0), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) return GameState(ball=ball_state)
def hide_ball(self, packet, drones, start_time) -> StepResult: """ Places the ball above the roof of the arena to keep it out of the way. """ self.game_interface.set_game_state(GameState(ball=BallState(physics=Physics( location=Vector3(0, 0, 3000), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0))))) return StepResult(finished=True)
def parse_ball(ball: Json) -> BallState: return BallState(physics=Physics( location=Vector3( x=ball['StartLocationX'], y=ball['StartLocationY'], z=ball['StartLocationZ'], ), velocity=parse_ball_vel(ball), angular_velocity=Vector3(0, 0, 0), ))
def make_default_playlist() -> Playlist: exercises = [ DrivesToBallExercise(name="patience", carPos=Vector3(2002, 255, 0), ballVel=Vector3(500, 1000, 200)) # DrivesToBallExercise('Get close to ball', grader=DriveToBallGrader(carPos=Vector3(100, 100, 100), timeout_seconds=5)) #DrivesToBallExercise('Get close to ball', grader=DriveToBallGrader(5)), ] for exercise in exercises: exercise.match_config = make_match_config_with_my_bot() return exercises
def __init__(self): #define all different car states here self.carStateBasic = CarState(boost_amount=100, physics=Physics( location=Vector3(0, 0, 17.01), velocity=Vector3(0, 0, 0), rotation=Rotator(pitch=0, yaw=0, roll=0), angular_velocity=Vector3(0, 0, 0)))
def reset_for_aerial(self): self.initial_ball_location = Vector3(0, 2000, 100) self.initial_ball_velocity = Vector3(randint(-250, 250), randint(-250, 250), 650 * 2) self.initial_car_location = Vector3(randint(-2000, 2000), 0, 0) self.initial_car_velocity = Vector3(0, 0, 0) self.not_hit_yet = True self.ball_predictions = [] self.last_dist = None self.last_touch_location = Vec3(0, 0, 0)
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: return GameState( ball=BallState(physics=Physics( location=Vector3(1437.9,4074.5999,290.43), velocity=Vector3(481.481,1407.231,-1633.341), angular_velocity=Vector3(5.54691,-1.9003099,-1.27281), rotation=Rotator(0.04343083,-0.69824886,-2.1147842))), cars={ 0: CarState( physics=Physics( location=Vector3(947.70996,810.54,17.05), velocity=Vector3(620.66095,959.18097,0.27099997), angular_velocity=Vector3(-4.0999998E-4,0.0,0.0), rotation=Rotator(-0.016682042,0.9965123,0.0)), jumped=False, double_jumped=False, boost_amount=0.0), 1: CarState( physics=Physics( location=Vector3(-373.35,949.32996,43.309998), velocity=Vector3(115.690994,1242.251,340.411), angular_velocity=Vector3(-0.30971,0.02471,0.08900999), rotation=Rotator(-0.014668691,1.4870985,9.58738E-5)), jumped=True, double_jumped=False, boost_amount=0.0) }, boosts={i: BoostState(0) for i in range(34)}, )
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: return GameState( ball=BallState(physics=Physics( location=Vector3(-1611.2999, 3176.77, 1294.03), velocity=Vector3(-271.841, 482.451, -253.021), angular_velocity=Vector3(-5.21131, -2.9428098, 0.42601), rotation=Rotator(0.96103895, 0.5027622, -1.1579638))), cars={ 0: CarState(physics=Physics( location=Vector3(-4056.2898, 703.43, 160.06), velocity=Vector3(-51.251, 1661.851, 150.251), angular_velocity=Vector3(-1.5182099, 0.44171, -0.53651), rotation=Rotator(0.05119661, 1.6048316, -1.1736871)), jumped=False, double_jumped=False, boost_amount=100.0), 1: CarState(physics=Physics( location=Vector3(-875.76996, 2313.48, 16.279999), velocity=Vector3(529.34094, 1679.131, 12.241), angular_velocity=Vector3(0.02711, -4.0999998E-4, 0.45961002), rotation=Rotator(-0.011217235, 1.2832708, 4.79369E-4)), jumped=False, double_jumped=False, boost_amount=0.0) }, boosts={i: BoostState(0) for i in range(34)}, )
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: return GameState( ball=BallState(physics=Physics( location=Vector3(2490.8398, -3985.97, 276.15), velocity=Vector3(-609.27094, 1042.121, -371.301), angular_velocity=Vector3(-1.61381, 5.77801, -0.10161), rotation=Rotator(-0.38013962, 1.32488, 0.3973969))), cars={ 0: CarState(physics=Physics( location=Vector3(2425.29, -4639.4097, 199.39), velocity=Vector3(-797.49097, -249.651, -15.901), angular_velocity=Vector3(-3.03761, -4.13571, 1.9794099), rotation=Rotator(-0.199801, -1.6142272, -2.3683705)), jumped=True, double_jumped=True, boost_amount=100.0), 1: CarState(physics=Physics( location=Vector3(2463.5999, -1264.83, 17.01), velocity=Vector3(-597.831, -1213.0409, 0.23099999), angular_velocity=Vector3(1.0999999E-4, 1.0999999E-4, 2.23661), rotation=Rotator(-0.00958738, -1.9631119, 0.0)), jumped=False, double_jumped=False, boost_amount=100.0) }, boosts={i: BoostState(0) for i in range(34)}, )
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: return GameState( ball=BallState(physics=Physics( location=Vector3(3989.49, 2429.52, 192.53), velocity=Vector3(-27.470999, -517.66095, -79.101), angular_velocity=Vector3(4.67741, 1.07971, 3.5994098), rotation=Rotator(0.8031348, -2.7694106, 0.77092123))), cars={ 0: CarState(physics=Physics( location=Vector3(3347.52, 1390.15, 17.05), velocity=Vector3(-549.081, -1298.891, 0.26099998), angular_velocity=Vector3(0.0, -4.0999998E-4, -1.0999999E-4), rotation=Rotator(-0.016682042, -1.9707818, 0.0)), jumped=False, double_jumped=False, boost_amount=46.0), 1: CarState(physics=Physics( location=Vector3(3170.66, 3257.52, 17.01), velocity=Vector3(1075.281, -1061.281, 0.191), angular_velocity=Vector3(-4.0999998E-4, 1.0999999E-4, -0.33201), rotation=Rotator(-0.00958738, -0.787028, 0.0)), jumped=False, double_jumped=False, boost_amount=0.0) }, boosts={i: BoostState(0) for i in range(34)}, )
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: return GameState( ball=BallState(physics=Physics( location=Vector3(-2104.1099,5025.9497,164.47), velocity=Vector3(299.101,-154.431,253.47101), angular_velocity=Vector3(-2.68911,3.7743099,3.09181), rotation=Rotator(0.34274882,0.6972901,-0.25080585))), cars={ 0: CarState( physics=Physics( location=Vector3(-2593.05,4884.88,16.69), velocity=Vector3(422.621,48.641,6.8209996), angular_velocity=Vector3(0.015109999,-0.03071,0.79331), rotation=Rotator(-0.01840777,0.086669914,0.0010546118)), jumped=False, double_jumped=False, boost_amount=96.0), 1: CarState( physics=Physics( location=Vector3(-2882.72,4746.54,469.69998), velocity=Vector3(1246.9609,294.511,-619.78094), angular_velocity=Vector3(-4.97911,-2.27091,0.54881), rotation=Rotator(-0.5048714,0.48617604,-0.14611167)), jumped=True, double_jumped=True, boost_amount=0.0) }, boosts={i: BoostState(0) for i in range(34)}, )
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: return GameState( ball=BallState(physics=Physics( location=Vector3(self.ball_x, self.ball_y, self.ball_z), velocity=Vector3(self.ball_vx, self.ball_vy, self.ball_vz), angular_velocity=Vector3(self.ball_sx, self.ball_sy, self.ball_sz))), cars={ 0: CarState(physics=Physics(location=Vector3( self.car_x, self.car_y, self.car_z), rotation=Rotator(0, self.car_spin, 0), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0)), jumped=False, double_jumped=False, boost_amount=100), 1: CarState(physics=Physics( location=Vector3(self.car_x, -self.car_y, self.car_z), rotation=Rotator(0, -self.car_spin, 0), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0)), jumped=False, double_jumped=False, boost_amount=100) }, boosts={i: BoostState(0) for i in range(34)}, # Is this needed. )
def make_game_state(self, rng: SeededRandomNumberGenerator) -> GameState: vel_mul = rng.uniform(1.5, 2.0) return GameState( ball=BallState(physics=Physics( location=Vector3(0, -1500, 100), velocity=Vector3(vel_mul * 350 * rng.uniform(-1, 1), vel_mul * -2000, 0), angular_velocity=Vector3(0, 0, 0))), cars={ # Striker 0: CarState( physics=Physics( location=Vector3(0, 0, 15), rotation=Rotator(0, -pi / 2, 0), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0)), jumped=False, double_jumped=False, boost_amount=100), # Goalie 1: CarState( physics=Physics( location=Vector3(0, -5000, 15), rotation=Rotator(0, rng.uniform(-.1, .1), 0), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0)), jumped=True, double_jumped=True, boost_amount=100), }, boosts={i: BoostState(0) for i in range(34)}, )