def setFieldConstantsFromField_Dimensions(value): Field.Length = value.Length() Field.Width = value.Width() Field.Border = value.Border() Field.LineWidth = value.LineWidth() Field.GoalWidth = value.GoalWidth() Field.GoalDepth = value.GoalDepth() Field.GoalHeight = value.GoalHeight() Field.PenaltyDist = value.PenaltyDist() Field.PenaltyDiam = value.PenaltyDiam() Field.ArcRadius = value.ArcRadius() Field.CenterRadius = value.CenterRadius() Field.CenterDiameter = value.CenterDiameter() Field.GoalFlat = value.GoalFlat() Field.FloorLength = value.FloorLength() Field.FloorWidth = value.FloorWidth() Field.OurGoalZoneShape = robocup.CompositeShape() Field.OurGoalZoneShape.add_shape(robocup.Circle(robocup.Point(-Field.GoalFlat / 2.0, 0), Field.ArcRadius)) Field.OurGoalZoneShape.add_shape(robocup.Circle(robocup.Point(Field.GoalFlat / 2.0, 0), Field.ArcRadius)) Field.OurGoalZoneShape.add_shape(robocup.Rect(robocup.Point(-Field.GoalFlat / 2.0, Field.ArcRadius), robocup.Point(Field.GoalFlat / 2.0, 0))) Field.TheirGoalShape = robocup.CompositeShape() Field.TheirGoalShape.add_shape(robocup.Circle(robocup.Point(-Field.GoalFlat / 2.0, Field.Length), Field.ArcRadius)) Field.TheirGoalShape.add_shape(robocup.Circle(robocup.Point(Field.GoalFlat / 2.0, Field.Length), Field.ArcRadius)) Field.TheirGoalShape.add_shape(robocup.Rect(robocup.Point(-Field.GoalFlat / 2.0, Field.Length), robocup.Point(Field.GoalFlat / 2.0, Field.Length - Field.ArcRadius))) Field.TheirGoalSegment = robocup.Segment(robocup.Point(Field.GoalWidth / 2.0, Field.Length), robocup.Point(-Field.GoalWidth / 2.0, Field.Length)) Field.OurGoalSegment = robocup.Segment(robocup.Point(Field.GoalWidth / 2.0, 0), robocup.Point(-Field.GoalWidth / 2.0, 0)) Field.TheirHalf = robocup.Rect(robocup.Point(-Field.Width/2, Field.Length), robocup.Point(Field.Width/2, Field.Length/2)) Field.OurHalf = robocup.Rect(robocup.Point(-Field.Width/2, 0), robocup.Point(Field.Width/2, Field.Length/2))
class Field: Length = 6.05 Width = 4.05 Border = 0.25 LineWidth = 0.01 GoalWidth = 0.700 GoalDepth = 0.180 GoalHeight = 0.160 # Distance of the penalty marker from the goal line PenaltyDist = 0.750 PenaltyDiam = 0.010 # Radius of the goal arcs ArcRadius = 0.8 # diameter of the center circle CenterRadius = 0.5 CenterDiameter = 1.0 # flat area for defense markings GoalFlat = 0.35 FloorLength = Length + 2.0 * Border FloorWidth = Width + 2.0 * Border CenterPoint = robocup.Point(0.0, Length / 2.0) OurGoalZoneShape = robocup.CompositeShape() OurGoalZoneShape.add_shape( robocup.Circle(robocup.Point(-GoalFlat / 2.0, 0), ArcRadius)) OurGoalZoneShape.add_shape( robocup.Circle(robocup.Point(GoalFlat / 2.0, 0), ArcRadius)) OurGoalZoneShape.add_shape( robocup.Rect(robocup.Point(-GoalFlat / 2.0, ArcRadius), robocup.Point(GoalFlat / 2.0, 0))) TheirGoalShape = robocup.CompositeShape() TheirGoalShape.add_shape( robocup.Circle(robocup.Point(-GoalFlat / 2.0, Length), ArcRadius)) TheirGoalShape.add_shape( robocup.Circle(robocup.Point(GoalFlat / 2.0, Length), ArcRadius)) TheirGoalShape.add_shape( robocup.Rect(robocup.Point(-GoalFlat / 2.0, Length), robocup.Point(GoalFlat / 2.0, Length - ArcRadius))) TheirGoalSegment = robocup.Segment(robocup.Point(GoalWidth / 2.0, Length), robocup.Point(-GoalWidth / 2.0, Length)) OurGoalSegment = robocup.Segment(robocup.Point(GoalWidth / 2.0, 0), robocup.Point(-GoalWidth / 2.0, 0)) TheirHalf = robocup.Rect(robocup.Point(-Width / 2, Length), robocup.Point(Width / 2, Length / 2)) OurHalf = robocup.Rect(robocup.Point(-Width / 2, 0), robocup.Point(Width / 2, Length / 2))
def execute_marking(self): move = self.subbehavior_with_name('move') move.pos = self.move_target arc = robocup.Circle(robocup.Point(0,0), self._defend_goal_radius) if move.pos != None: main.system_state().draw_circle(move.pos, 0.02, constants.Colors.Green, "Mark") main.system_state().draw_circle(robocup.Point(0,0), self._defend_goal_radius, constants.Colors.Green, "Mark")
def block_line(self, value): self._block_line = value # we move somewhere along this arc to mark our 'block_line' arc = robocup.Circle(robocup.Point(0,0), self._defend_goal_radius) # TODO: use the real shape instead of this arc approximation default_pt = arc.nearest_point(robocup.Point(0, constants.Field.Length / 2.0)) target = main.ball().pos if self._block_line != None: intersects, pt1, pt2 = self._block_line.intersects_circle(arc) if intersects: # print("CIRCLE INTERSECTS: " + str([pt1, pt2])) # choose the pt farther from the goal self._move_target = max([pt1, pt2], key=lambda p: p.y) else: self._move_target = default_pt else: self._move_target = default_pt
def get_circle_points(self, num_of_points): radius = constants.Field.CenterRadius + constants.Robot.Radius + 0.01 ball_pos = main.ball().pos if main.ball() != None else robocup.Point( constants.Field.Width / 2, constants.Field.Length / 2) circle_ball = robocup.Circle(ball_pos, radius) intersection_points = [] for i in constants.Field.FieldBorders: for j in circle_ball.intersects_line(i): # Using near_point because of rounding errors if constants.Field.FieldRect.near_point(j, 0.001): intersection_points.append(j) angles = [] candidate_arcs = [] if len(intersection_points) > 1: for i in intersection_points: new_angle = (i - circle_ball.center).angle() new_angle = self.normalize_angle(new_angle) angles.append(new_angle) # Get angles going sequentially angles.sort() counter = 1 while counter < len(angles): candidate_arcs.append(robocup.Arc(circle_ball.center, radius, angles[counter - 1], angles[ counter])) counter = counter + 1 candidate_arcs.append(robocup.Arc(circle_ball.center, radius, angles[len(angles) - 1], angles[ 0])) i = 0 while i < len(candidate_arcs): angle_between = candidate_arcs[i].end() - candidate_arcs[ i].start() angle_between = self.normalize_angle(angle_between) angle_diff = candidate_arcs[i].start() + (angle_between) / 2.0 angle_diff = self.normalize_angle(angle_diff) midpoint = ( candidate_arcs[i].center() + robocup.Point(radius, 0)) midpoint.rotate(candidate_arcs[i].center(), angle_diff) if not constants.Field.FieldRect.contains_point(midpoint): candidate_arcs.pop(i) else: i = i + 1 candidate_arcs.sort( key=lambda arc: self.normalize_angle(arc.end() - arc.start()), reverse=True) if len(candidate_arcs) <= 0: final_arc = robocup.Arc(CircleNearBall.BackupBallLocation, radius, math.pi / 2, 5 * math.pi / 2) else: final_arc = candidate_arcs[0] else: midpoint = (circle_ball.center + robocup.Point(radius, 0)) if not constants.Field.FieldRect.contains_point(midpoint): final_arc = robocup.Arc(CircleNearBall.BackupBallLocation, radius, math.pi / 2, 5 * math.pi / 2) else: final_arc = robocup.Arc(circle_ball.center, radius, math.pi / 2, 5 * math.pi / 2) arc_angle = final_arc.end() - final_arc.start() arc_angle = self.normalize_angle(arc_angle) perRobot = arc_angle / (num_of_points + 1) dirvec = robocup.Point(radius, 0) dirvec.rotate(robocup.Point(0, 0), final_arc.start()) dirvec.rotate(robocup.Point(0, 0), perRobot) final_points = [] for i in range(num_of_points): pt = final_arc.center() + dirvec final_points.append(pt) dirvec.rotate(robocup.Point(0, 0), perRobot) return final_points
def execute_running(self): their_robots = main.their_robots() mark_one = self.subbehavior_with_name('mark_one') mark_two = self.subbehavior_with_name('mark_two') centerCircle = robocup.Circle(constants.Field.CenterPoint, constants.Field.CenterRadius) # Don't select robots that are # 1. Not on our side of the field # 2. behind or inside the goal circle mark_robot_right = list(filter( lambda robot: (robot.pos.x >= 0 and robot.pos.y < constants.Field.Length * TheirKickoff.FieldRatio and constants.Field.FieldRect.contains_point(robot.pos) and not centerCircle.contains_point(robot.pos)), their_robots)) # Don't select robots that are # 1. Not on our side of the field # 2. behind or inside the goal circle # 3. Not the robot selected before mark_robot_left = list(filter( lambda robot: (robot.pos.x <= 0 and robot.pos.y < constants.Field.Length * TheirKickoff.FieldRatio and constants.Field.FieldRect.contains_point(robot.pos) and not centerCircle.contains_point(robot.pos) and robot != mark_one.mark_robot), their_robots)) # Special cases if len(mark_robot_left) + len(mark_robot_right) == 0: # Can't do anything mark_robot_left = None mark_robot_right = None elif len(mark_robot_left) + len(mark_robot_right) == 1: if len(mark_robot_left) == 1: mark_robot_right = mark_robot_left[0] mark_robot_left = None else: mark_robot_right = mark_robot_right[0] mark_robot_left = None elif len(mark_robot_left) == 0: mark_robot_right = mark_robot_right mark_robot_left = mark_robot_right elif len(mark_robot_right) == 0: mark_robot_right = mark_robot_left mark_robot_left = mark_robot_left # Else, everything can proceed as normal (pick best one from each side) # Make every element a list to normalize for the next step if type(mark_robot_right) is not list and mark_robot_right is not None: mark_robot_right = [mark_robot_right] if type(mark_robot_left) is not list and mark_robot_left is not None: mark_robot_left = [mark_robot_left] # Select best robot from candidate lists selected = None if mark_robot_right is not None: mark_robot_right = min(mark_robot_right, key=lambda robot: robot.pos.y).pos selected = robocup.Point(mark_robot_right) else: mark_robot_right = robocup.Point(TheirKickoff.DefaultDist, constants.Field.Length / 2) # Set x and y seperately as we want a constant y value (just behind the kick off line) mark_robot_right.y = min( constants.Field.Length / 2 - TheirKickoff.LineBuffer, mark_robot_right.y) mark_robot_right.x = self.absmin(mark_robot_right.x, TheirKickoff.DefaultDist) mark_one.mark_point = mark_robot_right # Do the same thing as above on the left robot. if mark_robot_left is not None: # Don't mark the same robot twice mark_robot_left = filter( lambda x: True if selected is None else not x.pos.nearly_equals(selected), mark_robot_left) mark_robot_left = min(mark_robot_left, key=lambda robot: robot.pos.y).pos else: mark_robot_left = robocup.Point(-TheirKickoff.DefaultDist, constants.Field.Length / 2) mark_robot_left.y = min( constants.Field.Length / 2 - TheirKickoff.LineBuffer, mark_robot_left.y) mark_robot_left.x = self.absmin(mark_robot_left.x, TheirKickoff.DefaultDist) mark_two.mark_point = mark_robot_left
def execute_marking(self): #main.system_state().draw_line(robocup.Line(self._area.get_pt(0), self._area.get_pt(1)), (127,0,255), "Defender") self.block_robot = self.find_robot_to_block() if self.block_robot is not None: # self.robot.add_text("Blocking Robot " + str(self.block_robot.shell_id()), (255,255,255), "RobotText") pass if self.robot.pos.near_point(robocup.Point(0, 0), self._opponent_avoid_threshold): self.robot.set_avoid_opponents(False) else: self.robot.set_avoid_opponents(True) target = None if self.block_robot is None: target = main.ball().pos + main.ball().vel * 0.3 else: target = self.block_robot.pos + self.block_robot.vel * 0.3 goal_line = robocup.Segment( robocup.Point(-constants.Field.GoalWidth / 2.0, 0), robocup.Point(constants.Field.GoalWidth / 2.0, 0)) self._win_eval.excluded_robots = [self.robot] # TODO defenders should register themselves with some static list on init # TODO make this happen in python-land # for (Defender *f : otherDefenders) # { # if (f->robot) # { # _winEval.exclude.push_back(f->robot->pos); # } # } windows = self._win_eval.eval_pt_to_seg(target, goal_line)[0] best = None goalie = main.our_robot_with_id(main.root_play().goalie_id) if goalie is not None and self.side is not Defender.Side.center: for window in windows: if best is None: best = window elif self.side is Defender.Side.left and window.segment.center.x < goalie.pos.x and window.segment.length > best.segment.length: best = window elif self.side is Defender.Side.right and window.segment.center.x > goalie.pos.x and window.segment.length > best.segment.length: best = window else: best_dist = 0 for window in windows: seg = robocup.Segment(window.segment.center(), main.ball().pos) new_dist = seg.dist_to(self.robot.pos) if best is None or new_dist < best_dist: best = window best_dist = new_dist shoot_seg = None if best is not None: if self.block_robot is not None: dirvec = robocup.Point.direction(self.block_robot.angle * (math.pi / 180.0)) shoot_seg = robocup.Segment( self.block_robot.pos, self.block_robot.pos + dirvec * 7.0) else: shoot_seg = robocup.Segment( main.ball().pos, main.ball().pos + main.ball().vel.normalized() * 7.0) need_task = False if best is not None: winseg = best.segment if main.ball().vel.magsq() > 0.03 and winseg.segment_intersection( shoot_seg) != None: self.robot.move_to(shoot_seg.nearest_point(self.robot.pos)) self.robot.face_none() else: winsize = winseg.length() if winsize < constants.Ball.Radius: need_task = True else: arc = robocup.Circle(robocup.Point(0, 0), self._defend_goal_radius) shot = robocup.Line(winseg.center(), target) dest = [robocup.Point(0, 0), robocup.Point(0, 0)] intersected, dest[0], dest[1] = shot.intersects_circle(arc) if intersected: self.robot.move_to( dest[0] if dest[0].y > 0 else dest[1]) if self.block_robot is not None: self.robot.face(self.block_robot.pos) else: self.robot.face(main.ball().pos) else: need_task = True if need_task: self.robot.face(main.ball().pos) backVec = robocup.Point(1, 0) backPos = robocup.Point(-constants.Field.Width / 2, 0) shotVec = robocup.Point(main.ball().pos - self.robot.pos) backVecRot = robocup.Point(backVec.perp_ccw()) facing_back_line = (backVecRot.dot(shotVec) < 0) if not facing_back_line and self.robot.has_ball(): if self.robot.has_chipper(): self.robot.chip(255) else: self.robot.kick(255)
def execute_area_marking(self): if self.robot.pos.near_point(robocup.Point(0, 0), self._opponent_avoid_threshold): self.robot.set_avoid_opponents(False) else: self.robot.set_avoid_opponents(True) goal_target = robocup.Point(0, -constants.Field.GoalDepth / 2.0) goal_line = robocup.Segment( robocup.Point(-constants.Field.GoalWidth / 2.0, 0), robocup.Point(constants.Field.GoalWidth / 2.0, 0)) if self.side is Defender.Side.left: goal_line.get_pt(1).x = 0 goal_line.get_pt(1).y = 0 if self.side is Defender.Side.right: goal_line.get_pt(0).x = 0 goal_line.get_pt(0).y = 0 for robot in main.system_state().their_robots: self._win_eval.excluded_robots.append(robot) if main.root_play().goalie_id is not None: self._win_eval.excluded_robots.append( main.our_robot_with_id(main.root_play().goalie_id)) # TODO (cpp line 186) # windows = self._win_eval. windows = [] best = None angle = 0.0 for window in windows: if best is None: best = window angle = window.a0 - window.a1 elif window.a0 - window.a1 > angle: best = window angle = window.a0 - window.a1 shootline = robocup.Segment(robocup.Point(0, 0), robocup.Point(0, 0)) if best is not None: angle = (best.a0 + best.a1) / 2.0 shootline = robocup.Segment( self._win_eval.origin(), robocup.Point.direction( angle * (math.pi / 180.0))) # FIXME :no origin. main.system_state().draw_line(shootline, (255, 0, 0), "Defender") need_task = False if best is not None: winseg = best.segment arc = robocup.Circle(robocup.Point(0, 0), self._defend_goal_radius) shot = robocup.Line(shootline.get_pt(0), shootline.get_pt(1)) dest = [robocup.Point(0, 0), robocup.Point(0, 0)] intersected, dest[0], dest[1] = shot.intersects_circle(arc) if intersected: self.robot.move_to(dest[0] if dest[0].y > 0 else dest[1]) else: need_task = True if need_task: self.robot.face(main.ball().pos) if main.ball().pos.y < constants.Field.Length / 2.0: self.robot.set_dribble_speed(255) backVec = robocup.Point(1, 0) backPos = robocup.Point(-constants.Field.Width / 2, 0) shotVec = robocup.Point(main.ball().pos - self.robot.pos) backVecRot = robocup.Point(backVec.perp_ccw()) facing_back_line = (backVecRot.dot(shotVec) < 0) if not facing_back_line and self.robot.has_ball(): if self.robot.has_chipper(): self.robot.chip(255) else: self.robot.kick(255)
def is_in_center(self): if main.ball().valid: return robocup.Circle(constants.Field.CenterPoint, constants.Field.CenterRadius).contains_point( main.ball().pos) return False