def __init__(self, env, precount, history): super(GStrategy2, self).__init__(env, precount, history) my_nearest_hockeyist = min( shortcuts.my_field_hockeyists(env), key=lambda h: assessments.ticks_to_reach_point(env, h, env.world.puck) ) my_other_hockeyist = filter( lambda h: h.id != my_nearest_hockeyist.id, shortcuts.my_field_hockeyists(env) )[0] if self.hwp is None or self.hwp.player_id != env.me.player_id: self.do_get_puck = True self.defenceman = my_other_hockeyist self.active_puck_taker = my_nearest_hockeyist else: self.strike_point = self.count_strike_point(env) if self.pass_condition(env, self.strike_point): self.do_pass = True self.pass_maker = self.hwp self.pass_taker = filter( lambda h: h.id != self.hwp.id, shortcuts.my_field_hockeyists(env) )[0] else: self.do_goal = True self.attacker = self.hwp self.defenceman = filter( lambda h: h.id != self.hwp.id, shortcuts.my_field_hockeyists(env) )[0]
def do_pass(self, env): collega = filter(lambda h: h.id != env.me.id, shortcuts.my_field_hockeyists(env))[0] angle = env.me.get_angle_to_unit(collega) env.move.turn = angle if abs(angle) <= env.game.pass_sector / 2.0: env.move.action = ActionType.PASS env.move.pass_angle = angle env.move.pass_power = 0.8
def stick_sectors(env): for h in shortcuts.my_field_hockeyists(env) + shortcuts.opponent_field_hockeyists(env): p1 = geometry.point_plus_vector( h, h.angle + env.game.stick_sector / 2., env.game.stick_length) p2 = geometry.point_plus_vector( h, h.angle - env.game.stick_sector / 2., env.game.stick_length) draw.line(h, p1) draw.line(h, p2)
def __init__(self, env): self.env = env self.id_to_fh_free = {} self.tick_to_puck = {0: env.world.puck} fhs = shortcuts.my_field_hockeyists(self.env) + shortcuts.opponent_field_hockeyists(self.env) for h in fhs: self.id_to_fh_free[h.id] = {0: h}
def __init__(self, env, precount, history): super(GStrategy3, self).__init__(env, precount, history) if self.hwp is None or self.hwp.player_id != env.me.player_id: self.do_get_puck = True puck_nearest_hockeyist = min( shortcuts.my_field_hockeyists(env), key=lambda h: assessments.ticks_to_reach_point(env, h, env.world.puck) ) self.active_puck_taker = puck_nearest_hockeyist def_nearest_hockeyist = min( filter( lambda h: h.id != puck_nearest_hockeyist.id, shortcuts.my_field_hockeyists(env) ), key=lambda h: assessments.ticks_to_reach_point(env, h, self.precount.defence_point) ) self.defenceman = def_nearest_hockeyist other_hockeyist = filter( lambda h: h.id not in (def_nearest_hockeyist.id, puck_nearest_hockeyist.id), shortcuts.my_field_hockeyists(env) )[0] self.second_puck_taker = other_hockeyist else: self.strike_point = self.count_strike_point(env) if self.pass_condition(env, self.strike_point): self.do_pass = True self.pass_maker = self.hwp self.pass_taker = self.count_pass_taker(env) other = filter( lambda h: h.id not in (self.pass_maker.id, self.pass_taker.id), shortcuts.my_field_hockeyists(env) )[0] def_nearest_hockeyist = min( filter( lambda h: h.id != self.hwp.id, shortcuts.my_field_hockeyists(env) ), key=lambda h: assessments.ticks_to_reach_point(env, h, self.precount.defence_point) ) if other.id == def_nearest_hockeyist.id: self.defenceman = other else: self.attack_supporter = other else: self.do_goal = True self.attacker = self.hwp self.defenceman = min( filter( lambda h: h.id != self.attacker.id, shortcuts.my_field_hockeyists(env) ), key=lambda h: assessments.ticks_to_reach_point(env, h, self.precount.defence_point) ) other = filter( lambda h: h.id not in (self.attacker.id, self.defenceman.id), shortcuts.my_field_hockeyists(env) )[0] self.attack_supporter = other
def pass_condition(self, env, strike_point): if env.world.tick - self.history.game_start_tick <= 75: return True h1, h2 = shortcuts.my_field_hockeyists(env) if geometry.distance(h1, h2) > 200: for oh in shortcuts.opponent_field_hockeyists(env): if geometry.interval_point_distance(self.hwp, strike_point, oh) < 60: return True if any(geometry.point_in_convex_polygon(self.hwp, p) for p in self.precount.dead_polygons): return True return False
def count_pass_taker(self, env): def f(h): return min( geometry.distance(h, oh) for oh in shortcuts.opponent_field_hockeyists(env) ) if self.hwp is None or self.hwp.player_id != env.me.player_id: return None res = max( filter( lambda h: h.id != self.hwp.id, shortcuts.my_field_hockeyists(env) ), key=lambda h: f(h) ) return res
def move(self, me, world, game, move): env = environment.Environment(me, world, game, move) if world.tick == 0: op = shortcuts.opponent_player(env) mp = shortcuts.my_player(env) self.attack_polygons = experiments.count_optimistic_attack_polygons(env, op) self.target_polygons = [ experiments.count_target_up_attack_polygon(env, op), experiments.count_target_down_attack_polygon(env, op), ] self.defence_point = experiments.count_defence_point(env) self.weak_polygons = experiments.count_cautious_attack_polygons(env, mp) self.dead_polygons = experiments.count_dead_polygons(env, op) self.last_puck_owner_player_id = None self.save_last_puck_owner(env) self.save_start_game_tick(env) if env.me.state == HockeyistState.SWINGING: if self.swing_condition(env): env.move.action = ActionType.SWING if self.strike_condition(env): env.move.action = ActionType.STRIKE return hockeyist_with_puck = shortcuts.hockeyist_with_puck(env) my_nearest_hockeyist = min( shortcuts.my_field_hockeyists(env), key=lambda h: assessments.ticks_to_reach_point(env, h, env.world.puck) ) if hockeyist_with_puck is None or hockeyist_with_puck.player_id != env.me.player_id: if my_nearest_hockeyist.id == me.id: self.do_get_puck_actions(env) else: self.do_protect_goal_actions(env) elif hockeyist_with_puck.player_id == env.me.player_id: if hockeyist_with_puck.id == env.me.id: self.attack_with_puck(env) else: self.do_protect_goal_actions(env)
def pass_condition(self, env, strike_point): if env.world.tick - self.game_start_tick <= 75: return True collega = filter(lambda h: h.id != env.me.id, shortcuts.my_field_hockeyists(env))[0] net_center = shortcuts.net_front_center(env, shortcuts.opponent_player(env)) if geometry.distance(collega, env.me) > 200: for oh in shortcuts.opponent_field_hockeyists(env): # my_angle = geometry.ray_ray_angle(env.me, strike_point, net_center) # o_angle = geometry.ray_ray_angle(oh, strike_point, net_center) # my_distance = geometry.distance(env.me, strike_point) # o_distance = geometry.distance(oh, strike_point) # if my_angle > o_angle and my_distance > o_distance: # return True if geometry.interval_point_distance(env.me, strike_point, oh) < 60: return True if any(geometry.point_in_convex_polygon(env.me, p) for p in self.dead_polygons): return True return False
def count_free_motion_next_tick(env): new_env = copy_env(env) def update_puck(new_puck, puck): new_puck.x = count_xn(puck.x, puck.speed_x, 0, 1, k=puck_friction_factor()) new_puck.y = count_xn(puck.y, puck.speed_y, 0, 1, k=puck_friction_factor()) new_puck.speed_x = count_vn(puck.speed_x, 0, 1, k=puck_friction_factor()) new_puck.speed_y = count_vn(puck.speed_y, 0, 1, k=puck_friction_factor()) def update_hockeyist(new_h, h): new_h.x = count_xn(h.x, h.speed_x, 0, 1, k=friction_factor()) new_h.y = count_xn(h.y, h.speed_y, 0, 1, k=friction_factor()) new_h.speed_x = count_vn(h.speed_x, 0, 1, k=friction_factor()) new_h.speed_y = count_vn(h.speed_y, 0, 1, k=friction_factor()) update_puck(new_env.world.puck, env.world.puck) for h in shortcuts.my_field_hockeyists(env) + shortcuts.opponent_field_hockeyists(env): new_h = filter( lambda nh: nh.id == h.id, new_env.world.hockeyists )[0] update_hockeyist(new_h, h) return new_env
def stick_circles(env): for h in shortcuts.my_field_hockeyists(env) + shortcuts.opponent_field_hockeyists(env): draw.circle(h, env.game.stick_length)