예제 #1
0
def guardGoal():
    """Go to the goal line and face the enemy with the ball"""
    me.goal = Goals.guardGoal
    angle_to_face = me.bearing(ball)

    # if not on our goal line, move into the middle of it
    if abs(me.currentPoint.x - ourGoal.x) > 15:
        angle_to_move = me.bearing(ourGoal)
        distance = me.distance(ourGoal)
        controller.move(angle_to_move, angle_to_move, distance)
    # if we're on our goal line, move to block the ball
    else:
        # work out which enemy has the ball
        if ball.status == BallStatus.enemyA:
            enemyNum = 0
        elif ball.status == BallStatus.enemyB:
            enemyNum = 1
        else:
            print("just guarding lol")
            controller.move(angle_to_face, 0, 0, False, rotate_in_place=True)
            return

        # calculate where on the y axis the ball would hit if kicked now
        x_dist = enemies[enemyNum].currentPoint.x - ourGoal.x
        y_dist = x_dist * tan(enemies[enemyNum].currentRotation)
        y_intersection = enemies[enemyNum].currentPoint.y + y_dist

        # calculate where we should therefore go to (we don't want to leave the goal line)
        minY = PITCH_WIDTH / 2 - 0.5 * GOAL_WIDTH + ROBOT_WIDTH / 2
        maxY = PITCH_WIDTH / 2 + 0.5 * GOAL_WIDTH - ROBOT_WIDTH / 2
        point_to_be = Point(ourGoal.x, max(minY, min(maxY, y_intersection)))

        # if we're not where we should be, move there holonomically
        if not nearEnough(point_to_be, me.currentPoint, near_enough_point=10):
            # turn to face the opposite side of the pitch before the holo movement
            if OUR_GOAL == 'left':
                angle_to_face = 0
            else:
                angle_to_face = 180
            # calculate the parameters to send to the robot
            distance = me.distance(point_to_be)
            # we want to move holonomically up and down
            if point_to_be.y < me.currentPoint.y:
                angle_to_move = 90
            else:
                angle_to_move = -90
            controller.move(angle_to_face, angle_to_move, distance)

        # if we're in position already but just facing wrongly, turn to face the robot with the ball
        elif not nearEnough(angle_to_face, me.currentRotation):
            controller.move(angle_to_face, 0, 0, False, rotate_in_place=True)
        # if we're all set, just wait for something to happen
        else:
            controller.stop_robot()
예제 #2
0
def guardGoal():
    """Go to the goal line and face the enemy with the ball"""
    me.goal = Goals.guardGoal
    angle_to_face = me.bearing(ball)

    # if not on our goal line, move into the middle of it
    if abs(me.currentPoint.x - ourGoal.x) > 15:
        angle_to_move = me.bearing(ourGoal)
        distance = me.distance(ourGoal)
        controller.move(angle_to_move,angle_to_move,distance)
    # if we're on our goal line, move to block the ball
    else:
        # work out which enemy has the ball
        if ball.status == BallStatus.enemyA:
            enemyNum=0
        elif ball.status == BallStatus.enemyB:
            enemyNum=1
        else:
            print("just guarding lol")
            controller.move(angle_to_face,0,0,False,rotate_in_place=True)
            return

        # calculate where on the y axis the ball would hit if kicked now
        x_dist = enemies[enemyNum].currentPoint.x  - ourGoal.x
        y_dist = x_dist * tan(enemies[enemyNum].currentRotation)
        y_intersection = enemies[enemyNum].currentPoint.y + y_dist

        # calculate where we should therefore go to (we don't want to leave the goal line)
        minY = PITCH_WIDTH/2 - 0.5*GOAL_WIDTH + ROBOT_WIDTH/2
        maxY = PITCH_WIDTH/2 + 0.5*GOAL_WIDTH - ROBOT_WIDTH/2
        point_to_be = Point(ourGoal.x, max(minY, min(maxY, y_intersection)))

        # if we're not where we should be, move there holonomically
        if not nearEnough(point_to_be, me.currentPoint, near_enough_point=10):
            # turn to face the opposite side of the pitch before the holo movement
            if OUR_GOAL=='left':
                angle_to_face=0
            else:
                angle_to_face=180
            # calculate the parameters to send to the robot
            distance = me.distance(point_to_be)
            # we want to move holonomically up and down
            if point_to_be.y<me.currentPoint.y:
                angle_to_move = 90
            else:
                angle_to_move = -90
            controller.move(angle_to_face,angle_to_move,distance)

        # if we're in position already but just facing wrongly, turn to face the robot with the ball
        elif not nearEnough(angle_to_face, me.currentRotation):
            controller.move(angle_to_face,0,0,False,rotate_in_place=True)
        # if we're all set, just wait for something to happen
        else:
            controller.stop_robot()
예제 #3
0
def collectBall():
    """Move towards then grab the ball"""
    me.goal=Goals.collectBall
    
    # if we've caught up to the ball, stop and grab
    if nearEnough(me.bearing(ball), me.currentRotation, near_enough_angle=30) and ball.distance(me)< BALL_OWNERSHIP_DISTANCE:
        controller.stop_robot()
        controller.grab(True)
    # otherwise, go to the ball
    else:
        me.goal = Goals.collectBall
        angle_to_face = me.bearing(ball)
        angle_to_move = angle_to_face
        distance_to_move = me.distance(ball)
        if lineOfSight(me.currentPoint,ball.currentPoint):
            print "Real"
            controller.move(angle_to_face,angle_to_move,distance_to_move,True)
        else:
            ghost_walk(angle_to_face)
예제 #4
0
def receivePass():
    """Face our ally and get ready to grab the ball if they kick it to us"""
    me.goal = Goals.receivePass
    angle_to_face = me.bearing(ally)
    # if we're facing the ally, ungrab and get ready to receive the ball
    if nearEnough(angle_to_face, me.currentRotation):
        controller.stop_robot()
        controller.ungrab(True)
    # otherwise, turn to face our ally
    else:
        controller.move(angle_to_face,0,0,False,rotate_in_place=True)
예제 #5
0
def receivePass():
    """Face our ally and get ready to grab the ball if they kick it to us"""
    me.goal = Goals.receivePass
    angle_to_face = me.bearing(ally)
    # if we're facing the ally, ungrab and get ready to receive the ball
    if nearEnough(angle_to_face, me.currentRotation):
        controller.stop_robot()
        controller.ungrab(True)
    # otherwise, turn to face our ally
    else:
        controller.move(angle_to_face, 0, 0, False, rotate_in_place=True)
예제 #6
0
def passBall():
    """Kick the ball full power towards our ally"""
    me.goal = Goals.passBall
    angle_to_face = me.bearing(ally)
    # if we're facing the ally, pass them the ball
    if nearEnough(angle_to_face, me.currentRotation):
        controller.stop_robot()
        if not controller.haveIkicked:
            controller.kick(255)
    # otherwise, turn to face our ally
    else:
        controller.move(angle_to_face,0,0,False,rotate_in_place=True)
예제 #7
0
def passBall():
    """Kick the ball full power towards our ally"""
    me.goal = Goals.passBall
    angle_to_face = me.bearing(ally)
    # if we're facing the ally, pass them the ball
    if nearEnough(angle_to_face, me.currentRotation):
        controller.stop_robot()
        if not controller.haveIkicked:
            controller.kick(255)
    # otherwise, turn to face our ally
    else:
        controller.move(angle_to_face, 0, 0, False, rotate_in_place=True)
예제 #8
0
def collectBall():
    """Move towards then grab the ball"""
    me.goal = Goals.collectBall

    # if we've caught up to the ball, stop and grab
    if nearEnough(me.bearing(ball), me.currentRotation, near_enough_angle=30
                  ) and ball.distance(me) < BALL_OWNERSHIP_DISTANCE:
        controller.stop_robot()
        controller.grab(True)
    # otherwise, go to the ball
    else:
        me.goal = Goals.collectBall
        angle_to_face = me.bearing(ball)
        angle_to_move = angle_to_face
        distance_to_move = me.distance(ball)
        if lineOfSight(me.currentPoint, ball.currentPoint):
            print "Real"
            controller.move(angle_to_face, angle_to_move, distance_to_move,
                            True)
        else:
            ghost_walk(angle_to_face)
예제 #9
0
def shoot():
    """Kick the ball full power towards the goal"""
    me.goal = Goals.shoot
    angle_to_face = me.bearing(opponentGoal)
    # if we're facing the goal, shoot!
    if nearEnough(angle_to_face, me.currentRotation):
        controller.stop_robot()
        print controller.haveIkicked
        if not controller.haveIkicked:
            controller.kick(255)
    # otherwise, turn to face the goal
    else:
        controller.move(angle_to_face,0,0,False,rotate_in_place=True)
예제 #10
0
def shoot():
    """Kick the ball full power towards the goal"""
    me.goal = Goals.shoot
    angle_to_face = me.bearing(opponentGoal)
    # if we're facing the goal, shoot!
    if nearEnough(angle_to_face, me.currentRotation):
        controller.stop_robot()
        print controller.haveIkicked
        if not controller.haveIkicked:
            controller.kick(255)
    # otherwise, turn to face the goal
    else:
        controller.move(angle_to_face, 0, 0, False, rotate_in_place=True)
예제 #11
0
def ghost_walk(angle_to_face):
    ghost_flag = False
    for i in range(0, 10):
        ghost_pt_x = random.random() * 30 * random.choice([1, -1])
        ghost_pt_y = random.random() * 30 * random.choice([1, -1])
        target = me.currentPoint.x + ghost_pt_y, me.currentPoint.y + ghost_pt_y
        if target[0] > 0 and target[0] < PITCH_LENGTH and target[1] > 0 and target[1] < PITCH_WIDTH and not isEnemyBox(Point(target[0],target[1])) and lineOfSight(me.currentPoint, Point(target[0],target[1])):
            ghost_flag = True
            break;
    if ghost_flag:
        angle_to_move = me.bearing(Point(target[0],target[1]))
        print "Ghost"
        controller.move(angle_to_face, angle_to_move, 60, None, None)
    else:
        print "None"
        controller.stop_robot()
예제 #12
0
def blockPass():
    """Move to inbetween the two enemies"""

    me.goal = Goals.blockPass
    # work out where to move to
    e0 = enemies[0].currentPoint
    e1 = enemies[1].currentPoint
    x = (e0.x + e1.x)/2
    y = (e0.y + e1.y)/2
    point_to_be = Point(x, y)

    # work out the parameters for the move command
    angle_to_face = point_to_be.bearing(ball.currentPoint)
    angle_to_move = me.bearing(point_to_be)
    distance = me.distance(point_to_be)
    if not isEnemyBox(point_to_be):
        controller.move(angle_to_move,angle_to_move,distance)
    else:
        ghost_walk(angle_to_face)
예제 #13
0
def blockPass():
    """Move to inbetween the two enemies"""

    me.goal = Goals.blockPass
    # work out where to move to
    e0 = enemies[0].currentPoint
    e1 = enemies[1].currentPoint
    x = (e0.x + e1.x) / 2
    y = (e0.y + e1.y) / 2
    point_to_be = Point(x, y)

    # work out the parameters for the move command
    angle_to_face = point_to_be.bearing(ball.currentPoint)
    angle_to_move = me.bearing(point_to_be)
    distance = me.distance(point_to_be)
    if not isEnemyBox(point_to_be):
        controller.move(angle_to_move, angle_to_move, distance)
    else:
        ghost_walk(angle_to_face)
예제 #14
0
def ghost_walk(angle_to_face):
    ghost_flag = False
    for i in range(0, 10):
        ghost_pt_x = random.random() * 30 * random.choice([1, -1])
        ghost_pt_y = random.random() * 30 * random.choice([1, -1])
        target = me.currentPoint.x + ghost_pt_y, me.currentPoint.y + ghost_pt_y
        if target[0] > 0 and target[0] < PITCH_LENGTH and target[
                1] > 0 and target[1] < PITCH_WIDTH and not isEnemyBox(
                    Point(target[0], target[1])) and lineOfSight(
                        me.currentPoint, Point(target[0], target[1])):
            ghost_flag = True
            break
    if ghost_flag:
        angle_to_move = me.bearing(Point(target[0], target[1]))
        print "Ghost"
        controller.move(angle_to_face, angle_to_move, 60, None, None)
    else:
        print "None"
        controller.stop_robot()
예제 #15
0
def updatePositions():
    """Updates the system's belief of the state of the game based on the vision system"""
    # get the info on the robots from the vision system
    if api.getMyPosition() is not None:
        mePosition = api.getMyPosition()
        me.update(Point(mePosition[0]*X_RATIO,mePosition[1]*Y_RATIO))
    else:
        print "Can't find my position this tick :("
    if api.getMyOrientation() is not None:
        meOrientation = api.getMyOrientation()[1]
        me.updateRotation(meOrientation)
    else:
        print "Can't find my orientation this tick :("

    if api.getAllyPosition() is not None:
        allyPosition = api.getAllyPosition()
        ally.update(Point(allyPosition[0]*X_RATIO,allyPosition[1]*Y_RATIO))
    else:
        print "Can't find my friend's position this tick :("
    if api.getAllyOrientation() is not None:
        allyOrientation = api.getAllyOrientation()[1]
        ally.updateRotation(allyOrientation)
    else:
        print "Can't find my friend's orientation this tick :("

    if api.getEnemyPositions()[0] is not None:
        enemy0Position = api.getEnemyPositions()[0]
        enemies[0].update(Point(enemy0Position[0]*X_RATIO,enemy0Position[1]*Y_RATIO))
    else:
        print "Can't find enemy 0 this tick :("
    if api.getEnemyOrientation()[0] is not None:
        enemy0Orientation =  api.getEnemyOrientation()[0][1]
        enemies[0].updateRotation(enemy0Orientation)
    else:
        print "Can't find enemy 0's orientation this tick :("

    if api.getEnemyPositions()[1] is not None:
        enemy1Position = api.getEnemyPositions()[1]
        enemies[1].update(Point(enemy1Position[0]*X_RATIO,enemy1Position[1]*Y_RATIO))
    else:
        print "Can't find enemy 1 this tick :("
    if api.getEnemyOrientation()[1] is not None:
        enemy1Orientation = api.getEnemyOrientation()[1][1]
        enemies[1].updateRotation(enemy1Orientation)
    else:
        print "Can't find enemy 1's orientation this tick :("

    if api.getBallCenter() is not None:
        ballPosition =  api.getBallCenter()
        ball.update(Point(ballPosition[0]*X_RATIO,ballPosition[1]*Y_RATIO))
    else:
        print "Shit! Where's the ball gone"

    try:#see who has ball posesion - needs work
        if nearEnough(enemies[0].bearing(ball),enemies[0].currentRotation, near_enough_angle=45) and ball.distance(enemies[0]) < BALL_OWNERSHIP_DISTANCE:
            ball.status = BallStatus.enemyA
        elif nearEnough(enemies[1].bearing(ball),enemies[1].currentRotation, near_enough_angle=45) and  ball.distance(enemies[1]) < BALL_OWNERSHIP_DISTANCE:
            ball.status = BallStatus.enemyB
        elif nearEnough(ally.bearing(ball),ally.currentRotation, near_enough_angle=45) and  ball.distance(ally)< BALL_OWNERSHIP_DISTANCE:
            ball.status = BallStatus.ally
        elif nearEnough(me.bearing(ball), me.currentRotation, near_enough_angle=45) and ball.distance(me)< BALL_OWNERSHIP_DISTANCE and me.grabbed:
            ball.status = BallStatus.me
        # if we can't see it, assume it's the same
        elif api.world['ball_center']==None:
            pass
        # if it's far enough from everything, it's free
        else:
            ball.status = BallStatus.free
    except (TypeError, AttributeError):
        print("Location of some objects unknown")