def exec(self, bot): car = bot.info.my_car ball = bot.info.ball car_to_ball = ball.pos - car.pos ball_to_enemy_goal = bot.info.enemy_goal - ball.pos own_goal_to_ball = ball.pos - bot.info.own_goal dist = norm(car_to_ball) offence = ball.pos.y * bot.info.team_sign < 0 dot_enemy = dot(car_to_ball, ball_to_enemy_goal) dot_own = dot(car_to_ball, own_goal_to_ball) right_side_of_ball = dot_enemy > 0 if offence else dot_own > 0 if right_side_of_ball: # Aim cone dir_to_post_1 = (bot.info.enemy_goal + Vec3(3800, 0, 0)) - bot.info.ball.pos dir_to_post_2 = (bot.info.enemy_goal + Vec3(-3800, 0, 0)) - bot.info.ball.pos cone = AimCone(dir_to_post_1, dir_to_post_2) cone.get_goto_point(bot, car.pos, bot.info.ball.pos) if bot.do_rendering: cone.draw(bot, bot.info.ball.pos) # Chase ball return bot.drive.go_towards_point(bot, xy(ball.pos), 2000, True, True, can_dodge=dist > 2200) else: # Go home return bot.drive.go_towards_point(bot, bot.info.own_goal_field, 2000, True, True)
class ClearBall(Choice): def __init__(self, bot): if bot.team == 0: # blue ra = 0.85 * math.pi la = 0.15 * math.pi self.aim_cone = AimCone(Vec3(math.cos(ra), math.sin(ra), 0), Vec3(math.cos(la), math.sin(la), 0)) else: # orange ra = -0.15 * math.pi la = -0.85 * math.pi self.aim_cone = AimCone(Vec3(math.cos(ra), math.sin(ra), 0), Vec3(math.cos(la), math.sin(la), 0)) def utility(self, bot) -> float: team_sign = bot.info.team_sign length = team_sign * Field.LENGTH / 2 ball_own_half_01 = clip01( remap(-length, length, -0.2, 1.2, bot.info.ball.pos.y)) reachable_ball = predict.ball_predict( bot, predict.time_till_reach_ball(bot.info.my_car, bot.info.ball)) car_to_ball = reachable_ball.pos - bot.info.my_car.pos in_position = self.aim_cone.contains_direction(car_to_ball, math.pi / 8) return ball_own_half_01 * in_position def exec(self, bot) -> SimpleControllerState: car = bot.info.my_car shoot_controls = bot.shoot.with_aiming( bot, self.aim_cone, predict.time_till_reach_ball(bot.info.my_car, bot.info.ball)) hit_pos = bot.shoot.ball_when_hit.pos if bot.do_rendering: self.aim_cone.draw(bot, hit_pos, r=0, g=170, b=255) if bot.shoot.can_shoot: if bot.shoot.using_curve and bot.do_rendering: rendering.draw_bezier( bot, [car.pos, bot.shoot.curve_point, hit_pos]) return shoot_controls else: # go home-ish own_goal = lerp(bot.info.own_goal, bot.info.ball.pos, 0.5) return bot.drive.go_towards_point(bot, own_goal, target_vel=1460, slide=True, boost_min=0, can_keep_speed=True)
class SaveGoal(Choice): def __init__(self, bot): team_sign = bot.info.team_sign self.own_goal_right = Vec3(-820 * team_sign, 5120 * team_sign, 0) self.own_goal_left = Vec3(820 * team_sign, 5120 * team_sign, 0) self.aim_cone = None self.ball_to_goal_right = None self.ball_to_goal_left = None def utility(self, bot) -> float: team_sign = bot.info.team_sign ball = bot.info.ball ball_to_goal = bot.info.own_goal - ball.pos too_close = norm(ball_to_goal) < Field.GOAL_WIDTH / 2 + Ball.RADIUS hits_goal_prediction = predict.will_ball_hit_goal(bot) hits_goal = hits_goal_prediction.happens and sign( ball.vel.y) == team_sign and hits_goal_prediction.time < 3 return hits_goal or too_close def exec(self, bot) -> SimpleControllerState: car = bot.info.my_car ball = bot.info.ball hits_goal_prediction = predict.will_ball_hit_goal(bot) reach_time = clip(predict.time_till_reach_ball(car, ball), 0, hits_goal_prediction.time - 0.5) reachable_ball = predict.ball_predict(bot, reach_time) self.ball_to_goal_right = self.own_goal_right - reachable_ball.pos self.ball_to_goal_left = self.own_goal_left - reachable_ball.pos self.aim_cone = AimCone(self.ball_to_goal_left, self.ball_to_goal_right) if bot.do_rendering: self.aim_cone.draw(bot, reachable_ball.pos, r=200, g=0, b=160) shoot_controls = bot.shoot.with_aiming(bot, self.aim_cone, reach_time) if not bot.shoot.can_shoot: # Go home return bot.drive.go_home(bot) else: return shoot_controls
class ShootAtGoal(Choice): def __init__(self): self.aim_cone = None self.ball_to_goal_right = None self.ball_to_goal_left = None self.temp_utility_desire_boost = 0 def utility(self, bot) -> float: if self.temp_utility_desire_boost > 0: self.temp_utility_desire_boost = max( 0, self.temp_utility_desire_boost - bot.info.dt) elif self.temp_utility_desire_boost < 0: self.temp_utility_desire_boost = min( 0, self.temp_utility_desire_boost + bot.info.dt) ball_soon = predict.ball_predict(bot, 1) arena_length2 = bot.info.team_sign * Field.LENGTH / 2 own_half_01 = clip01( remap(arena_length2, -arena_length2, 0.0, 1.1, ball_soon.pos.y)) reachable_ball = predict.ball_predict( bot, predict.time_till_reach_ball(bot.info.my_car, bot.info.ball)) self.ball_to_goal_right = bot.info.enemy_goal_right - reachable_ball.pos self.ball_to_goal_left = bot.info.enemy_goal_left - reachable_ball.pos self.aim_cone = AimCone(self.ball_to_goal_right, self.ball_to_goal_left) car_to_ball = reachable_ball.pos - bot.info.my_car.pos in_position = self.aim_cone.contains_direction(car_to_ball) # Chase ball right after kickoff. High right after kickoff kickoff_bias01 = max(0, 1 - bot.info.time_since_last_kickoff * 0.3) return clip01(own_half_01 + 0.1 * in_position + self.temp_utility_desire_boost + kickoff_bias01) def exec(self, bot) -> SimpleControllerState: car = bot.info.my_car ball = bot.info.ball my_hit_time = predict.time_till_reach_ball(car, ball) shoot_controls = bot.shoot.with_aiming(bot, self.aim_cone, my_hit_time) if bot.do_rendering: self.aim_cone.draw(bot, bot.shoot.ball_when_hit.pos, b=0) hit_pos = bot.shoot.ball_when_hit.pos dist = norm(car.pos - hit_pos) closest_enemy, enemy_dist = bot.info.closest_enemy( 0.5 * (hit_pos + ball.pos)) if not bot.shoot.can_shoot and is_closer_to_goal_than( car.pos, hit_pos, bot.info.team): # Can't shoot but or at least on the right side: Chase goal_to_ball = normalize(hit_pos - bot.info.enemy_goal) offset_ball = hit_pos + goal_to_ball * Ball.RADIUS * 0.9 enemy_hit_time = predict.time_till_reach_ball(closest_enemy, ball) enemy_hit_pos = predict.ball_predict(bot, enemy_hit_time).pos if enemy_hit_time < 1.5 * my_hit_time: self.temp_utility_desire_boost -= bot.info.dt if bot.do_rendering: bot.renderer.draw_line_3d(closest_enemy.pos, enemy_hit_pos, bot.renderer.red()) return bot.drive.go_home(bot) if bot.do_rendering: bot.renderer.draw_line_3d(car.pos, offset_ball, bot.renderer.yellow()) return bot.drive.go_towards_point(bot, offset_ball, target_vel=2200, slide=False, boost_min=0) elif not bot.shoot.aim_is_ok and hit_pos.y * -bot.info.team_sign > 4250 and abs( hit_pos.x) > 900 and not dist < 420: # hit_pos is an enemy corner and we are not close: Avoid enemy corners and just wait enemy_to_ball = normalize(hit_pos - closest_enemy.pos) wait_point = hit_pos + enemy_to_ball * enemy_dist # a point 50% closer to the center of the field wait_point = lerp(wait_point, ball.pos + Vec3(0, bot.info.team_sign * 3000, 0), 0.5) if bot.do_rendering: bot.renderer.draw_line_3d(car.pos, wait_point, bot.renderer.yellow()) return bot.drive.go_towards_point(bot, wait_point, norm(car.pos - wait_point), slide=False, can_keep_speed=True, can_dodge=False) elif not bot.shoot.can_shoot: enemy_to_ball = normalize(xy(ball.pos - closest_enemy.pos)) ball_to_my_goal = normalize(xy(bot.info.own_goal - ball.pos)) dot_threat = dot( enemy_to_ball, ball_to_my_goal ) # 1 = enemy is in position, -1 = enemy is NOT in position if car.boost == 0 and ball.pos.y * bot.info.team_sign < 500 and dot_threat < 0.1: collect_center = ball.pos.y * bot.info.team_sign <= 0 collect_small = closest_enemy.pos.y * bot.info.team_sign <= 0 or enemy_dist < 900 pads = filter_pads(bot, bot.info.big_boost_pads, big_only=not collect_small, enemy_side=False, center=collect_center) bot.maneuver = CollectClosestBoostManeuver(bot, pads) # return home return bot.drive.go_home(bot) else: # Shoot ! if bot.shoot.using_curve and bot.do_rendering: rendering.draw_bezier( bot, [car.pos, bot.shoot.curve_point, hit_pos]) return shoot_controls