def tooFarFromHome(threshold, player):
    """
    Returns true if LOC thinks we're more than *distance* away from our home
    position
    """
    if player.brain.ball.vis.frames_off < 10:
        ball = player.brain.ball
    elif player.brain.sharedBall.ball_on:
        ball = player.brain.sharedBall
    else:
        ball = None
        home = player.homePosition

    if ball != None:
        if role.isLeftDefender(player.role):
            home = findDefenderHome(True, ball, player.homePosition.h)
        elif role.isRightDefender(player.role):
            home = findDefenderHome(False, ball, player.homePosition.h)
        elif role.isStriker(player.role):
            home = findStrikerHome(ball, player.homePosition.h)
        else:
            home = player.homePosition

    distance = ((player.brain.loc.x - home.x) ** 2 + (player.brain.loc.y - home.y) ** 2) ** 0.5

    return distance > threshold
def checkForConsistency(player):
    """
    Checks to see if anyone else has the same role as us. If they also have
    a lower playerNumber then we change. Otherwise we assume that they will
    fix the issue. Very similar in structure to determineRole in penalty states.
    """
    if not player.roleSwitching:
        return

    openSpaces = [True, True, True, True]
    conflict = False
    position = 0

    for mate in player.brain.teamMembers:
        if mate.playerNumber == player.brain.playerNumber:
            continue
        openSpaces[mate.role - 2] = False
        if mate.role == player.role and mate.playerNumber > player.brain.playerNumber and mate.frameSinceActive < 30:
            conflict = True

    if not conflict:
        return  # The expected outcome

    for i in range(3):
        if openSpaces[i] and constants.canRoleSwitchTo(i + 2):
            constants.setRoleConstants(player, i + 2)
            return
        elif openSpaces[i]:
            position = i + 2

    if position == 0:
        print "We have conflicting role AND there are no more open roles..."

    constants.setRoleConstants(player, position)
    return
Example #3
0
def positionAtHome(player):
    """
    Go to the player's home position. Defenders look in the direction of the 
    shared ball if it is on with reliability >= 2. Cherry pickers look in the direction
    of the shared ball if it is on with reliability >= 1.
    """
    if player.firstFrame():
        player.brain.tracker.trackBall()

        home = RobotLocation(player.homePosition.x, player.homePosition.y, player.homePosition.h)
        if (player.brain.sharedBall.ball_on and player.brain.sharedBall.reliability >= 2 and 
            role.isDefender(player.role)):
            sharedball = Location(player.brain.sharedBall.x, player.brain.sharedBall.y)
            home.h = player.brain.loc.getRelativeBearing(sharedball)
        elif (player.brain.sharedBall.ball_on and player.brain.sharedBall.reliability >= 1 and 
              role.isCherryPicker(player.role)):
            sharedball = Location(player.brain.sharedBall.x, player.brain.sharedBall.y)
            home.h = player.brain.loc.getRelativeBearing(sharedball)

        fastWalk = role.isCherryPicker(player.role)
        player.brain.nav.goTo(home, precision = nav.HOME,
                              speed = nav.QUICK_SPEED, avoidObstacles = True,
                              fast = fastWalk, pb = False)

    home = RobotLocation(player.homePosition.x, player.homePosition.y, player.homePosition.h)
    if (player.brain.sharedBall.ball_on and player.brain.sharedBall.reliability >= 2 and 
        role.isDefender(player.role)):
        sharedball = Location(player.brain.sharedBall.x, player.brain.sharedBall.y)
        home.h = player.brain.loc.getRelativeBearing(sharedball)
    elif (player.brain.sharedBall.ball_on and player.brain.sharedBall.reliability >= 1 and 
          role.isCherryPicker(player.role)):
        sharedball = Location(player.brain.sharedBall.x, player.brain.sharedBall.y)
        home.h = player.brain.loc.getRelativeBearing(sharedball)

    player.brain.nav.updateDest(home)
Example #4
0
def switchRoles(player):
    """
    State to decide who on the team should become the new chaser and switch accordingly.
    """
    roleConstants.setRoleConstants(player, player.openChaser)

    return player.goLater(player.gameState)
Example #5
0
def positionAtHome(player):
    """
    Go to the player's home position. Defenders look in the direction of the 
    shared ball if it is on with reliability >= 2. Cherry pickers look in the direction
    of the shared ball if it is on with reliability >= 1.
    """
    home = RobotLocation(player.homePosition.x, player.homePosition.y, player.homePosition.h)
    # if (player.brain.sharedBall.ball_on and player.brain.sharedBall.reliability >= 2 and 
    #     role.isDefender(player.role)):
    #     sharedball = Location(player.brain.sharedBall.x, player.brain.sharedBall.y)
    #     home.h = player.brain.loc.getRelativeBearing(sharedball)
    # elif (player.brain.sharedBall.ball_on and player.brain.sharedBall.reliability >= 1 and 
    #       role.isCherryPicker(player.role)):
    #     sharedball = Location(player.brain.sharedBall.x, player.brain.sharedBall.y)
    #     home.h = player.brain.loc.getRelativeBearing(sharedball)

    if player.firstFrame():
        if role.isCherryPicker(player.role):
            player.brain.tracker.repeatWidePan()
        else:
            player.brain.tracker.trackBall()
        fastWalk = role.isCherryPicker(player.role)
        player.brain.nav.goTo(home, precision = nav.HOME,
                              speed = nav.QUICK_SPEED, avoidObstacles = True,
                              fast = fastWalk, pb = False)

    player.brain.nav.updateDest(home)
Example #6
0
def calculateHomePosition(player):
    """
    Calculate home position.
    """
    if player.brain.ball.vis.frames_off < 10:
        ball = player.brain.ball
        bearing = ball.bearing_deg
    elif player.brain.sharedBall.ball_on:
        ball = player.brain.sharedBall
        bearing = degrees(
            atan2(ball.y - player.brain.loc.y,
                  ball.x - player.brain.loc.x)) - player.brain.loc.h
    else:
        ball = None

    if ball != None and not (role.isDefender(player.role)
                             and NogginConstants.FIXED_D_HOME):
        if role.isLeftDefender(player.role):
            home = findDefenderHome(True, ball, bearing + player.brain.loc.h)
        elif role.isRightDefender(player.role):
            home = findDefenderHome(False, ball, bearing + player.brain.loc.h)
        elif role.isStriker(player.role):
            home = findStrikerHome(ball, bearing + player.brain.loc.h)
        else:
            home = player.homePosition
    else:
        home = player.homePosition

    return home
Example #7
0
def watchForBall(player):
    """
    The player is at home, waiting for the ball to be within box.
    """

    if player.firstFrame():
        # print "-----------Player at home - Watching for ball-----------"
        player.brain.tracker.trackBall()
        player.brain.nav.stand()

    # I commented this out because we were getting strange oscillations
    # between this and positonAtHome, and honestly we never go here unless
    # we are already at home... dumb...
    # if transitions.tooFarFromHome(player, 50, 20):
    #     return player.goLater('positionAtHome')

    if role.isFirstChaser(player.role):
        if player.stateTime >= tracking.INITIALIZE_HEADING_TIME + tracking.FULL_WIDE_PAN_TIME:
                return player.goNow('spinAtHome')
    elif role.isStriker(player.role):
        if player.stateTime >= tracking.INITIALIZE_HEADING_TIME + tracking.FULL_WIDE_PAN_TIME * 2:
            return player.goNow('spinAtHome')
    else:
        if player.stateTime >= tracking.INITIALIZE_HEADING_TIME + tracking.FULL_WIDE_PAN_TIME * 2:
            return player.goNow('spinAtHome')
Example #8
0
def switchRoles(player):
    """
    State to decide who on the team should become the new chaser and switch accordingly.
    """
    roleConstants.setRoleConstants(player, player.openChaser)

    return player.goLater(player.gameState)
Example #9
0
def calculateHomePosition(player):
    """
    Calculate home position.
    """
    if player.brain.ball.vis.frames_off < 10:
        ball = player.brain.ball
        bearing = ball.bearing_deg
    elif player.brain.sharedBall.ball_on:
        ball = player.brain.sharedBall
        bearing = degrees(atan2(ball.y - player.brain.loc.y,
                          ball.x - player.brain.loc.x)) - player.brain.loc.h
    else:
        ball = None

    if ball != None and not (role.isDefender(player.role) and NogginConstants.FIXED_D_HOME):
        if role.isLeftDefender(player.role):
            home = findDefenderHome(True, ball, bearing + player.brain.loc.h)
        elif role.isRightDefender(player.role):
            home = findDefenderHome(False, ball, bearing + player.brain.loc.h)
        elif role.isStriker(player.role):
            home = findStrikerHome(ball, bearing + player.brain.loc.h)
        else:
            home = player.homePosition
    else:
        home = player.homePosition

    return home
Example #10
0
def branchOnRole(player):
    """
    Chasers are going to have a different behavior again.
    We will branch on behavior based on role here
    """

    # print "----------In branch on role----------"

    # print "Entered Branch on Role"
    # print "----- evenDefenderIsForward, lastEvenDefenderForwardVal ----"
    # print player.brain.evenDefenderIsForward, lastEvenDefenderForwardVal

    # global lastEvenDefenderForwardVal
    # player.brain.evenDefenderIsForward = not lastEvenDefenderForwardVal
    # newEvenDefenderForwardVal = True
    # print("TIME SINCE PLAYING:", player.brain.gameController.timeSincePlaying)
    if role.isFirstChaser(player.role):
        if transitions.shouldFindSharedBall(player) and player.brain.gameController.timeSincePlaying > 75:
            return player.goNow('searchFieldForSharedBall')
        return player.goNow('playerFourSearchBehavior') 
    elif role.isStriker(player.role):
        return player.goNow('playerFiveSearchBehavior')
    elif role.isLeftDefender(player.role):

        # print "Player Brain Left Forward 1: " + str(leftDefenderIsForward)

        # if (player.brain.sharedBall.ball_on) and (player.brain.sharedBall.x < NogginConstants.MIDFIELD_X):

            # print "WE ARE IN HERE"

            # return player.goNow('leftDefenderBack')
        if leftDefenderIsForward:

            # print "Changing to False"

            global leftDefenderIsForward

            leftDefenderIsForward = False

            # print "Player Brain Left Forward 2: " + str(leftDefenderIsForward)

            return player.goNow('leftDefenderForward')
        else:

            # print "Changing to True"

            global leftDefenderIsForward

            leftDefenderIsForward = True

            # print "Player Brain Left Forward 3: " + str(leftDefenderIsForward)

            return player.goNow('leftDefenderBack')
    else:
        return player.goNow('positionAtHome')
Example #11
0
def switchRoles(player):
    """
    State to decide who on the team should become the new chaser and switch accordingly.
    """
    # US Open Hack
    if player.brain.game:
        oppTeam = player.brain.game.team(1).team_number
    else:
        oppTeam = -1
    roleConstants.setRoleConstants(player, player.openChaser, oppTeam)

    return player.goLater(player.gameState)
Example #12
0
def switchRoles(player):
    """
    State to decide who on the team should become the new chaser and switch accordingly.
    """
    # US Open Hack
    if player.brain.game:
        oppTeam = player.brain.game.team(1).team_number
    else:
        oppTeam = -1
    roleConstants.setRoleConstants(player, player.openChaser, oppTeam)

    return player.goLater(player.gameState)
Example #13
0
def commMonitor(player):
    if player.commMode == -1:
        pass
    elif player.commMode != 2 and transitions.awfulComm(player):
        print "Switched to awful comm mode!"
        player.role = player.brain.playerNumber
        player.prevRoleConfig = RoleConstants.roleConfiguration
        RoleConstants.roleConfiguration = RoleConstants.spread
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = False
        player.commMode = 2
Example #14
0
def positionAsSupporter(player):
    if (role.isChaser(player.role) and role.isChaser(player.roleOfClaimer) and 
        player.brain.ball.distance > hypot(CHASER_DISTANCE, CHASER_DISTANCE)):
        fast = True
    else:
        fast = False

    positionAsSupporter.position = getSupporterPosition(player, player.role)

    # TODO don't call goTo every frame
    player.brain.nav.goTo(positionAsSupporter.position, precision = nav.GENERAL_AREA,
                          speed = nav.QUICK_SPEED, avoidObstacles = True,
                          fast = False, pb = False)
Example #15
0
def getSupporterPosition(player, role):
    """
    Returns a position to stand at to support teammate who is chasing the ball.
    Used in positionAsSupporter in PlayOffBallStates.
    """
    if RoleConstants.isLeftDefender(role):
        return leftDefender(player)
    elif RoleConstants.isRightDefender(role):
        return rightDefender(player)
    elif RoleConstants.isChaser(role):
        return chaser(player)
    else: # cherry picker
        return cherryPicker(player)
Example #16
0
def getSupporterPosition(player, role):
    """
    Returns a position to stand at to support teammate who is chasing the ball.
    Used in positionAsSupporter in PlayOffBallStates.
    """
    if RoleConstants.isLeftDefender(role):
        return leftDefender(player)
    elif RoleConstants.isRightDefender(role):
        return rightDefender(player)
    elif RoleConstants.isChaser(role):
        return chaser(player)
    else:  # cherry picker
        return cherryPicker(player)
Example #17
0
def positionAsSupporter(player):
    if (role.isChaser(player.role) and role.isChaser(player.roleOfClaimer) and 
        player.brain.ball.distance > hypot(CHASER_DISTANCE, CHASER_DISTANCE)):
        fast = True
    else:
        fast = False

    positionAsSupporter.position = getSupporterPosition(player, player.role)

    if player.firstFrame():
        player.brain.nav.goTo(positionAsSupporter.position, precision = nav.GENERAL_AREA,
                              speed = nav.QUICK_SPEED, avoidObstacles = True,
                              fast = False, pb = False)
    
    player.brain.nav.updateDest(positionAsSupporter.position, fast=fast)
Example #18
0
def chaser(player):
    """
    Chasers position off to one side of the ball about a meter away if a chaser
    or cherry picker is calling them off. Chasers position further away up field if
    a defender is calling them off.
    """
    if RoleConstants.isDefender(player.roleOfClaimer):
        if player.brain.ball.y >= player.brain.loc.y:
            return RobotLocation(player.brain.ball.x + 200,
                                 player.brain.ball.y - CHASER_DISTANCE,
                                 player.brain.ball.bearing_deg + player.brain.loc.h)
        return RobotLocation(player.brain.ball.x + 200,
                             player.brain.ball.y + CHASER_DISTANCE,
                             player.brain.ball.bearing_deg + player.brain.loc.h)
    else:
        southEast = RobotLocation(player.brain.ball.x - CHASER_DISTANCE,
                                  player.brain.ball.y - CHASER_DISTANCE,
                                  player.brain.ball.bearing_deg + player.brain.loc.h)
        southWest = RobotLocation(player.brain.ball.x - CHASER_DISTANCE,
                                 player.brain.ball.y + CHASER_DISTANCE,
                                 player.brain.ball.bearing_deg + player.brain.loc.h)
        northEast = RobotLocation(player.brain.ball.x + CHASER_DISTANCE,
                                 player.brain.ball.y - CHASER_DISTANCE,
                                 player.brain.ball.bearing_deg + player.brain.loc.h)
        northWest = RobotLocation(player.brain.ball.x + CHASER_DISTANCE,
                                 player.brain.ball.y + CHASER_DISTANCE,
                                 player.brain.ball.bearing_deg + player.brain.loc.h)

        supportPostitions = [southEast,southWest,northEast,northWest]
        positionsFilteredByInBounds = [position for position in supportPostitions if inBounds(position)]
        if len(positionsFilteredByInBounds) > 0:
            return min(positionsFilteredByInBounds, key=distanceToPosition(player))
        
        # print "no in bounds position"
        return southEast
Example #19
0
def checkForConsistency(player):
    """
    Checks to see if anyone else has the same role as us. If they also have
    a lower playerNumber then we change. Otherwise we assume that they will
    fix the issue. Very similar in structure to determineRole in penalty states.
    """
    if not player.roleSwitching:
        return

    openSpaces = [True, True, True, True]
    conflict = False
    position = 0

    for mate in player.brain.teamMembers:
        if mate.playerNumber == player.brain.playerNumber:
            continue
        openSpaces[mate.role - 2] = False
        if mate.role == player.role \
                and mate.playerNumber > player.brain.playerNumber \
                and mate.frameSinceActive < 30:
            conflict = True

    if not conflict:
        return  # The expected outcome

    for i in range(3):
        if openSpaces[i] and constants.canRoleSwitchTo(i + 2):
            # US Open Hacks
            if player.brain.game:
                oppTeam = player.brain.game.team(1).team_number
            else:
                oppTeam = -1
            constants.setRoleConstants(player, i + 2, oppTeam)
            return
        elif openSpaces[i]:
            position = i + 2

    if position == 0:
        print "We have conflicting role AND there are no more open roles..."

    # US Open Hacks
    if player.brain.game:
        oppTeam = player.brain.game.team(1).team_number
    else:
        oppTeam = -1
    constants.setRoleConstants(player, position, oppTeam)
    return
Example #20
0
def chaser(player):
    """
    Chasers position off to one side of the ball about a meter away if a chaser
    or cherry picker is calling them off. Chasers position further away up field if
    a defender is calling them off.
    """
    if role.isStriker(player.roleOfClaimer):
        return striker(player)

    if role.isDefender(player.roleOfClaimer):
        if player.brain.ball.y >= player.brain.loc.y:
            return RobotLocation(
                player.brain.ball.x + 250,
                player.brain.ball.y - CHASER_DISTANCE,
                player.brain.ball.bearing_deg + player.brain.loc.h)
        return RobotLocation(
            player.brain.ball.x + 250, player.brain.ball.y + CHASER_DISTANCE,
            player.brain.ball.bearing_deg + player.brain.loc.h)
    else:
        southEast = RobotLocation(
            player.brain.ball.x - CHASER_DISTANCE,
            player.brain.ball.y - CHASER_DISTANCE,
            player.brain.ball.bearing_deg + player.brain.loc.h)
        southWest = RobotLocation(
            player.brain.ball.x - CHASER_DISTANCE,
            player.brain.ball.y + CHASER_DISTANCE,
            player.brain.ball.bearing_deg + player.brain.loc.h)
        northEast = RobotLocation(
            player.brain.ball.x + CHASER_DISTANCE,
            player.brain.ball.y - CHASER_DISTANCE,
            player.brain.ball.bearing_deg + player.brain.loc.h)
        northWest = RobotLocation(
            player.brain.ball.x + CHASER_DISTANCE,
            player.brain.ball.y + CHASER_DISTANCE,
            player.brain.ball.bearing_deg + player.brain.loc.h)

        supportPostitions = [southEast, southWest, northEast, northWest]
        positionsFilteredByInBounds = [
            position for position in supportPostitions
            if (inBounds(position) and notBlockingGoal(position))
        ]
        if len(positionsFilteredByInBounds) > 0:
            return min(positionsFilteredByInBounds,
                       key=distanceToPosition(player))

        # print "no in bounds position"
        return southEast
Example #21
0
def shouldCedeClaim(player):
    if not player.useClaims:
        return False

    playerWeight = weightedDistAndHeading(player.brain.ball.distance, \
                                              player.brain.loc.h, player.brain.ball.bearing_deg)
    for mate in player.brain.teamMembers:
        if (mate.playerNumber == player.brain.playerNumber):
            continue
        if not mate.claimedBall or not mate.active or mate.fallen:
            continue

        # Now we get into actual claims
        if ((player.brain.time - mate.claimTime) > claimExpiration):
            print "claim expired"
            continue  # That claim has expired (Comm is probably lagging)

        mateWeight = weightedDistAndHeading(mate.ballDist, mate.h,
                                            mate.ballBearing)

        # sigmoid function so that the difference increases slowly at close distances but
        # grows quickly at mid-range to far distances and at very far distances, asymptotically
        # approaches a maximum. uses the distance of the close robot
        if player.brain.ball.distance < mate.ballDist:
            closerDistance = player.brain.ball.distance
        else:
            closerDistance = mate.ballDist
        closeWeightDifference = 25 + 150 / (1 + math.e**
                                            (6.25 - .05 * closerDistance))
        if (math.fabs(mateWeight - playerWeight) < closeWeightDifference):
            if roleConstants.isFirstChaser(mate.role):
                player.roleOfClaimer = mate.role
                player.claimedBall = False
                return True
            elif player.role < mate.role and not roleConstants.isFirstChaser(
                    player.role):
                player.roleOfClaimer = mate.role
                player.claimedBall = False
                return True
        elif (mateWeight < playerWeight):
            player.roleOfClaimer = mate.role
            player.claimedBall = False
            return True

    player.claimedBall = True
    return False
Example #22
0
def positionAtHome(player):
    """
    Go to the player's home position.
    """
    if role.isDefender(player.role):
        home = calculateHomePosition(player)
    else:
        home = player.homePosition

    if player.firstFrame():
        player.brain.tracker.trackBall()
        fastWalk = role.isChaser(player.role)
        player.brain.nav.goTo(home, precision = nav.HOME,
                              speed = nav.QUICK_SPEED, avoidObstacles = True,
                              fast = fastWalk, pb = False)

    player.brain.nav.updateDest(home)
Example #23
0
def doPan(player):
    """
    Wide pan for 5 seconds.
    """

    if player.firstFrame():
        # print "------------Doing Pan-------------"

        player.stand()
        player.brain.tracker.trackBall()

    if player.stateTime >= tracking.INITIALIZE_HEADING_TIME + tracking.FULL_WIDE_PAN_TIME:
        if role.isFirstChaser(player.role):
            return player.goNow('playerFourSearchBehavior')
        elif role.isStriker(player.role):
            return player.goNow('playerFiveSearchBehavior')
        else:
            return player.goNow('doSecondHalfSpin')
Example #24
0
def shouldCedeClaim(player):
    if not player.useClaims:
        return False

    playerWeight = weightedDistAndHeading(player.brain.ball.distance, \
                                              player.brain.loc.h, player.brain.ball.bearing_deg)
    for mate in player.brain.teamMembers:
        if (mate.playerNumber == player.brain.playerNumber):
            continue
        if not mate.claimedBall or not mate.active or mate.fallen:
            continue

        # Now we get into actual claims
        if ((player.brain.time - mate.claimTime) > claimExpiration):
            print "claim expired"
            continue # That claim has expired (Comm is probably lagging)

        mateWeight = weightedDistAndHeading(mate.ballDist, mate.h, mate.ballBearing)

        # sigmoid function so that the difference increases slowly at close distances but
        # grows quickly at mid-range to far distances and at very far distances, asymptotically
        # approaches a maximum. uses the distance of the close robot
        if player.brain.ball.distance < mate.ballDist:
            closerDistance = player.brain.ball.distance
        else:
            closerDistance = mate.ballDist
        closeWeightDifference = 25 + 150/(1 + math.e**(6.25 - .05*closerDistance))
        if (math.fabs(mateWeight - playerWeight) < closeWeightDifference):
            if roleConstants.isFirstChaser(mate.role):
                player.roleOfClaimer =  mate.role
                player.claimedBall = False
                return True
            elif player.role < mate.role and not roleConstants.isFirstChaser(player.role):
                player.roleOfClaimer =  mate.role
                player.claimedBall = False
                return True
        elif (mateWeight < playerWeight):
            player.roleOfClaimer =  mate.role
            player.claimedBall = False
            return True

    player.claimedBall = True
    return False
Example #25
0
def branchOnRole(player):
    """
    Chasers have different behavior than defenders, so we branch on
    role here.
    """
    if role.isChaser(player.role):
        if transitions.shouldFindSharedBall(player):
            return player.goNow('searchFieldForSharedBall')
        return player.goNow('searchFieldByQuad')
    return player.goNow('positionAtHome')
Example #26
0
def branchOnRole(player):
    """
    Chasers have different behavior than defenders, so we branch on
    role here.
    """
    if role.isChaser(player.role):
        if transitions.shouldFindSharedBall(player):
            return player.goNow('searchFieldForSharedBall')
        return player.goNow('searchFieldByQuad')
    return player.goNow('positionAtHome')
Example #27
0
def afterPenalty(player):

    if player.firstFrame():

        if DEBUG_PENALTY_STATES:
            print "Entering the 'afterPenalty' state."

        # pan for the ball
        player.brain.tracker.repeatWidePan()

        # reset state specific counters
        afterPenalty.goalLeft = 0
        afterPenalty.reset_loc = 0

    vis = player.brain.vision

    # Do we see the top of the goalbox
    for i in range(0, vis.line_size()):
        if vis.line(i).id == LineID.TopGoalbox:
            topGoalBox = vis.line(i).inner
            leftAngle = fabs(topGoalBox.t - pi) < .25
            # Goalbox to the left = 1 to the right = -1
            toLeft = 1 if leftAngle else -1
            afterPenalty.goalLeft += toLeft

    # If we've seen any landmark enough, reset localization.
    if fabs(afterPenalty.goalLeft) > OBJ_SEEN_THRESH:
        if DEBUG_PENALTY_STATES:
            print "Saw goalbox enough times"
        afterPenalty.reset_loc = copysign(1, afterPenalty.goalLeft)

    # Send the reset loc command.
    if afterPenalty.reset_loc != 0:
        if DEBUG_PENALTY_STATES:
            print "Sufficient sightings. reset_loc value is: " + str(
                afterPenalty.reset_loc)
        afterPenalty.goalLeft += afterPenalty.reset_loc
        afterPenalty.reset_loc = 0

    if fabs(afterPenalty.goalLeft) > 5 or player.stateTime > 15:
        if DEBUG_PENALTY_STATES:
            print "Consensus reached! Resetting loc. Is the goal to our right? " + str(
                afterPenalty.goalLeft < 0)
        # Yes, when goal_right is less than 0, our goal is to our right.
        # It seems counter intuitive, but that's how it works. -Josh Z
        if afterPenalty.goalLeft > 0:
            print "Goal is to left. Coming out of penalty"
        else:
            print "Goal is to right. Coming out of penalty"
        player.brain.resetLocalizationFromPenalty(afterPenalty.goalLeft < 0)
        if not roleConstants.isGoalie(player.role):
            return player.goNow('walkOut')
        return player.goLater(player.gameState)

    return player.stay()
Example #28
0
def branchOnRole(player):
    """
    Chasers are going to have a different behavior again.
    We will branch on behavior based on role here
    """
    # print("TIME SINCE PLAYING:", player.brain.gameController.timeSincePlaying)
    if role.isChaser(player.role):
        if transitions.shouldFindSharedBall(player) and player.brain.gameController.timeSincePlaying > 75:
            return player.goNow('searchFieldForSharedBall')
        return player.goNow('searchFieldByQuad')
    return player.goNow('positionAtHome')
Example #29
0
def positionAtHome(player):
    """
    Go to the player's home position.
    """
    if role.isDefender(player.role):
        home = calculateHomePosition(player)
    else:
        home = player.homePosition

    if player.firstFrame():
        player.brain.tracker.trackBall()
        fastWalk = role.isChaser(player.role)
        player.brain.nav.goTo(home,
                              precision=nav.HOME,
                              speed=nav.QUICK_SPEED,
                              avoidObstacles=True,
                              fast=fastWalk,
                              pb=False)

    player.brain.nav.updateDest(home)
def chaserIsOut(player):
    """
    There is a chaser spot ready to be filled.
    """
    if not player.roleSwitching or player.brain.gameController.penalized:
        return False

    if not player.gameState == "gamePlaying":
        return False

    checkForConsistency(player)

    if constants.canRoleSwitchTo(player.role):
        return False

    openPositions = ()
    positions = [False, False, False, False]
    for mate in player.brain.teamMembers:
        if mate.role <= 1:
            continue
        if mate.frameSinceActive < 30:
            positions[mate.role - 2] = True
        if mate.playerNumber == player.brain.playerNumber:
            continue
        if constants.canRoleSwitchTo(mate.role) and mate.frameSinceActive > 30:
            openPositions += (mate.role,)

        if (
            constants.willRoleSwitch(mate.role)
            and mate.playerNumber > player.brain.playerNumber
            and (mate.frameSinceActive < 30 or not mate.active)
        ):
            return False  # Active, higher numbered player takes precedence

    for pos in openPositions:
        if not positions[pos - 2]:
            player.openChaser = pos
            print "Switching to role: ", pos
            return True

    return False
Example #31
0
def ballInBox(player):
    """
    A transition which returns true if the ball is in the player's box
    """
    ball = player.brain.ball

    if ball.vis.frames_on > chaseConstants.BALL_ON_THRESH:
        if role.isChaser(player.role):
            return True
        return (ball.x > player.box[0][0] and ball.y > player.box[0][1] and
                ball.x < player.box[0][0] + player.box[1] and
                ball.y < player.box[0][1] + player.box[2])
Example #32
0
def afterPenalty(player):

    if player.firstFrame():

        if DEBUG_PENALTY_STATES:
            print "Entering the 'afterPenalty' state."

        # pan for the ball
        player.brain.tracker.repeatWidePan()
        
        # reset state specific counters
        afterPenalty.goalLeft = 0
        afterPenalty.reset_loc = 0

    lines = player.brain.visionLines

    # Do we see the top of the goalbox
    for i in range(0, lines.line_size()):
        if lines.line(i).id == LineID.TopGoalbox:
            topGoalBox = lines.line(i).inner
            leftAngle = fabs(topGoalBox.t - pi) < .25
            # Goalbox to the left = 1 to the right = -1
            toLeft = 1 if leftAngle else -1
            afterPenalty.goalLeft += toLeft

    # If we've seen any landmark enough, reset localization.
    if fabs(afterPenalty.goalLeft) > OBJ_SEEN_THRESH:
        if DEBUG_PENALTY_STATES:
            print "Saw goalbox enough times"
        afterPenalty.reset_loc = copysign(1, afterPenalty.goalLeft)

    # Send the reset loc command.
    if afterPenalty.reset_loc != 0:
        if DEBUG_PENALTY_STATES:
            print "Sufficient sightings. reset_loc value is: " + str(afterPenalty.reset_loc)
        afterPenalty.goalLeft += afterPenalty.reset_loc
        afterPenalty.reset_loc = 0

    if fabs(afterPenalty.goalLeft) > 5 or player.stateTime > 15:
        if DEBUG_PENALTY_STATES:
            print "Consensus reached! Resetting loc. Is the goal to our right? " + str(afterPenalty.goalLeft < 0)
        # Yes, when goal_right is less than 0, our goal is to our right.
        # It seems counter intuitive, but that's how it works. -Josh Z
        if afterPenalty.goalLeft > 0:
            print "Goal is to left. Coming out of penalty"
        else:
            print "Goal is to right. Coming out of penalty"
        player.brain.resetLocalizationFromPenalty(afterPenalty.goalLeft < 0)
        if not roleConstants.isGoalie(player.role):
            return player.goNow('walkOut')
        return player.goLater(player.gameState)

    return player.stay()
def ballInBox(player):
    """
    A transition which returns true if the ball is in the player's box
    """
    ball = player.brain.ball

    if ball.vis.frames_on > chaseConstants.BALL_ON_THRESH:
        if role.isChaser(player.role):
            return True
        return (ball.x > player.box[0][0] and ball.y > player.box[0][1]
                and ball.x < player.box[0][0] + player.box[1]
                and ball.y < player.box[0][1] + player.box[2])
Example #34
0
def determineRole(player):
    if not player.roleSwitching:
        return player.goLater(player.gameState)

    openSpaces = [True, True, True, True]
    for mate in player.brain.teamMembers:
        if mate.playerNumber == player.brain.playerNumber:
            continue
        if not roleConstants.isGoalie(mate.role) \
                and mate.frameSinceActive < 30:
            openSpaces[mate.role - 2] = False

    position = 0

    for i in range(4):
        if openSpaces[i] and roleConstants.canRoleSwitchTo(i+2):
            roleConstants.setRoleConstants(player, i+2)
            return player.goLater(player.gameState)
        elif openSpaces[i]:
            position = i+2

    if position == 0:
        print "Came out of penalty and found no open spaces!!!"

    roleConstants.setRoleConstants(player, i+2)
    return player.goLater(player.gameState)
Example #35
0
def determineRole(player):
    if not player.roleSwitching:
        return player.goLater(player.gameState)

    openSpaces = [True, True, True, True]
    for mate in player.brain.teamMembers:
        if mate.playerNumber == player.brain.playerNumber:
            continue
        if not roleConstants.isGoalie(mate.role) \
                and mate.frameSinceActive < 30:
            openSpaces[mate.role - 2] = False

    position = 0

    for i in range(4):
        if openSpaces[i] and roleConstants.canRoleSwitchTo(i + 2):
            roleConstants.setRoleConstants(player, i + 2)
            return player.goLater(player.gameState)
        elif openSpaces[i]:
            position = i + 2

    if position == 0:
        print "Came out of penalty and found no open spaces!!!"

    roleConstants.setRoleConstants(player, i + 2)
    return player.goLater(player.gameState)
Example #36
0
def commMonitor(player):
    if player.commMode == -1:
        pass
    elif player.commMode != 2 and transitions.awfulComm(player):
        print "Switched to awful comm mode!"
        player.role = player.brain.playerNumber
        player.prevRoleConfig = RoleConstants.roleConfiguration
        RoleConstants.roleConfiguration = RoleConstants.cautious
        RoleConstants.oddDefenderBox = RoleConstants.oddDefenderBoxCautious
        RoleConstants.evenDefenderBox = RoleConstants.evenDefenderBoxCautious
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = False
        player.commMode = 2
    elif player.commMode != 1 and transitions.mediocreComm(player):
        print "Switched to mediocre comm mode!"
        player.role = player.brain.playerNumber
        if player.commMode == 2:
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = False
        player.commMode = 1
    elif player.commMode != 0 and transitions.goodComm(player):
        print "Switched to good comm mode!"
        player.role = player.brain.playerNumber
        if player.commMode == 2:
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = True
        player.commMode = 0
Example #37
0
def panAtWayPoint(player):

    if player.firstFrame():
        player.stand()
        player.brain.tracker.trackBall()

    # if role.isFirstChaser(player.role) and not playerFourSearchBehavior.pointIndex % len(playerFourPoints) == 0:
    #     if player.stateTime >= FULL_WIDE_PAN_TIME:
    #         return player.goNow("playerFourSearchBehavior")

    # elif player.stateTime >= FULL_WIDE_PAN_TIME * 2:
    #     return player.goNow("spinAtHome")

    if role.isFirstChaser(player.role) and not playerFourSearchBehavior.pointIndex % len(playerFourPoints) == 0:
        if player.stateTime >= tracking.INITIALIZE_HEADING_TIME + tracking.FULL_WIDE_PAN_TIME:
            return player.goNow("playerFourSearchBehavior")
    elif role.isStriker(player.role):
        if player.stateTime >= tracking.INITIALIZE_HEADING_TIME + tracking.FULL_WIDE_PAN_TIME * 2:
            return player.goNow('spinAtHome')
    else:
        if player.stateTime >= tracking.INITIALIZE_HEADING_TIME + tracking.FULL_WIDE_PAN_TIME:
            return player.goNow('spinAtHome')
def chaserIsOut(player):
    """
    There is a chaser spot ready to be filled.
    """
    if not player.roleSwitching or player.brain.gameController.penalized:
        return False

    if not player.gameState == "gamePlaying":
        return False

    checkForConsistency(player)

    if constants.canRoleSwitchTo(player.role):
        return False

    openPositions = ()
    positions = [False, False, False, False]
    for mate in player.brain.teamMembers:
        if mate.role <= 1:
            continue
        if mate.frameSinceActive < 30:
            positions[mate.role - 2] = True
        if mate.playerNumber == player.brain.playerNumber:
            continue
        if constants.canRoleSwitchTo(mate.role) and mate.frameSinceActive > 30:
            openPositions += (mate.role, )

        if constants.willRoleSwitch(mate.role) \
                and mate.playerNumber > player.brain.playerNumber \
                and (mate.frameSinceActive < 30 or not mate.active):
            return False  # Active, higher numbered player takes precedence

    for pos in openPositions:
        if not positions[pos - 2]:
            player.openChaser = pos
            print "Switching to role: ", pos
            return True

    return False
Example #39
0
def doSecondHalfSpin(player):
    """
    Keep spinning in the same direction.
    """
    if player.firstFrame():
        player.setWalk(0, 0, nav.QUICK_SPEED)
        player.brain.tracker.lookToSpinDirection(1)

    while player.stateTime < chaseConstants.SPUN_ONCE_TIME_THRESH / 2:
        return player.stay()

    if role.isFirstChaser(player.role):
        return player.goNow('searchFieldByQuad')
    return player.goNow('playOffBall')
Example #40
0
def ballNotInBufferedBox(player):
    """
    A transition which allows a stretching of a box so that the box isn't
    so ridged. Intended use is for in approachBall, ensuring that we don't loop
    between approachBall and positionAtHome if the ball is close to the edge of the box.
    """
    ball = player.brain.ball
    buf = role.boxBuffer
    inBox = (ball.x > player.box[0][0] - buf and ball.y > player.box[0][1] - buf and \
            ball.x < player.box[0][0] + player.box[1] + buf and \
            ball.y < player.box[0][1] + player.box[2] + buf)

    return (ball.vis.frames_off > chaseConstants.BALL_OFF_THRESH
            or (not inBox and not role.isFirstChaser(player.role)))
Example #41
0
def doSecondHalfSpin(player):
    """
    Keep spinning in the same direction.
    """
    if player.firstFrame():
        player.setWalk(0, 0, nav.QUICK_SPEED)
        player.brain.tracker.lookToSpinDirection(1)

    while player.stateTime < chaseConstants.SPUN_ONCE_TIME_THRESH / 2:
        return player.stay()

    if role.isFirstChaser(player.role):
        return player.goNow('searchFieldByQuad')
    return player.goNow('playOffBall')
Example #42
0
def ballNotInBufferedBox(player):
    """
    A transition which allows a stretching of a box so that the box isn't
    so ridged. Intended use is for in approachBall, ensuring that we don't loop
    between approachBall and positionAtHome if the ball is close to the edge of the box.
    """
    ball = player.brain.ball
    buf = role.boxBuffer
    inBox = (ball.x > player.box[0][0] - buf and ball.y > player.box[0][1] - buf and \
            ball.x < player.box[0][0] + player.box[1] + buf and \
            ball.y < player.box[0][1] + player.box[2] + buf)

    return (ball.vis.frames_off > chaseConstants.BALL_OFF_THRESH or 
            (not inBox and not role.isChaser(player.role)))
def tooFarFromHome(player, distThreshold, angleThreshold):
    """
    Returns true if LOC thinks we're more than *distance* away from our home
    position
    """
    if role.isDefender(player.role):
        home = calculateHomePosition(player)
    else:
        home = player.homePosition

    distanceTo = ((player.brain.loc.x - home.x)**2 + (player.brain.loc.y - home.y)**2)**.5
    angleTo = fabs(player.brain.loc.h - home.h)

    return distanceTo > distThreshold or angleTo > angleThreshold
Example #44
0
def tooFarFromHome(player, distThreshold, angleThreshold):
    """
    Returns true if LOC thinks we're more than *distance* away from our home
    position
    """
    if role.isDefender(player.role):
        home = calculateHomePosition(player)
    else:
        home = player.homePosition

    distanceTo = ((player.brain.loc.x - home.x)**2 +
                  (player.brain.loc.y - home.y)**2)**.5
    angleTo = fabs(player.brain.loc.h - home.h)

    return distanceTo > distThreshold or angleTo > angleThreshold
Example #45
0
def commMonitor(player):
    if player.commMode == -1:
        pass
    elif player.commMode != 0 and player.brain.game.have_remote_gc:
        print "Switched to good comm mode because we are on the GC BABAY!"
        player.role = player.brain.playerNumber
        if player.commMode == 2:
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = True
        player.commMode = 0
    elif not player.brain.game.have_remote_gc and player.commMode != 2 and transitions.awfulComm(
            player):
        print "Switched to awful comm mode!"
        player.role = player.brain.playerNumber
        player.prevRoleConfig = RoleConstants.roleConfiguration
        RoleConstants.roleConfiguration = RoleConstants.spread
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = False
        player.commMode = 2
    elif not player.brain.game.have_remote_gc and player.commMode != 1 and transitions.mediocreComm(
            player):
        print "Switched to mediocre comm mode!"
        player.role = player.brain.playerNumber
        if player.commMode == 2:
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = False
        player.commMode = 1
    elif player.commMode != 0 and transitions.goodComm(player):
        print "Switched to good comm mode!"
        player.role = player.brain.playerNumber
        if player.commMode == 2:
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = True
        player.commMode = 0
Example #46
0
def commMonitor(player):
    if player.commMode == -1:
        pass
    elif player.commMode != 0 and player.brain.game.have_remote_gc:
        print "Switched to good comm mode because we are on the GC BABAY!"
        player.role = player.brain.playerNumber
        if player.commMode == 2: 
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = True
        player.commMode = 0
    elif not player.brain.game.have_remote_gc and player.commMode != 2 and transitions.awfulComm(player):
        print "Switched to awful comm mode!"
        player.role = player.brain.playerNumber
        player.prevRoleConfig = RoleConstants.roleConfiguration
        RoleConstants.roleConfiguration = RoleConstants.spread
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = False
        player.commMode = 2
    elif not player.brain.game.have_remote_gc and player.commMode != 1 and transitions.mediocreComm(player):
        print "Switched to mediocre comm mode!"
        player.role = player.brain.playerNumber
        if player.commMode == 2: 
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = False
        player.commMode = 1
    elif player.commMode != 0 and transitions.goodComm(player):
        print "Switched to good comm mode!"
        player.role = player.brain.playerNumber
        if player.commMode == 2: 
            RoleConstants.roleConfiguration = player.prevRoleConfig
        RoleConstants.oddDefenderBox = RoleConstants.defenderBox
        RoleConstants.evenDefenderBox = RoleConstants.defenderBox
        RoleConstants.setRoleConstants(player, player.role)
        player.roleSwitching = True
        player.commMode = 0
Example #47
0
def positionAsSupporter(player):
    positionAsSupporter.position = getSupporterPosition(player, player.role)

    if player.firstFrame():
        player.brain.tracker.trackBall()

        fastWalk = role.isChaser(player.role)

        player.brain.nav.goTo(positionAsSupporter.position, precision = nav.GENERAL_AREA,
                              speed = nav.QUICK_SPEED, avoidObstacles = True,
                              fast = fastWalk, pb = False)

    if positionAsSupporter.position.distTo(player.brain.loc) > 20:
        player.brain.nav.goTo(positionAsSupporter.position, precision = nav.GENERAL_AREA,
                              speed = nav.QUICK_SPEED, avoidObstacles = True,
                              fast = fastWalk, pb = False)
    
    player.brain.nav.updateDest(positionAsSupporter.position, fast = fastWalk)
Example #48
0
def doSecondHalfSpin(player):
    """
    Keep spinning in the same direction.
    """

    if player.firstFrame():
        # print "----------Doing second half spin-------------"
        player.brain.tracker.repeatFixedPitchLookAhead()

        if player.brain.playerNumber == 3:
            player.setWalk(0, 0, speeds.SPEED_SIX)
        else:
            player.setWalk(0, 0, -speeds.SPEED_SIX)

    if player.stateTime > chaseConstants.SPEED_SIX_SPUN_ONCE_TIME / 2:
        if role.isLeftDefender(player.role):
            return player.goNow('defenderPan')
        else:
            return player.goNow('playOffBall')
Example #49
0
def positionAtHome(player):
    """
    Go to the player's home position.
    """



    home = player.homePosition

    if player.firstFrame():

        # print "-----------Positioning at home-------------"

        player.brain.tracker.trackBall()
        fastWalk = role.isChaser(player.role)
        player.brain.nav.goTo(home, precision = nav.HOME,
                              speed = speeds.SPEED_EIGHT, avoidObstacles = True,
                              fast = fastWalk, pb = False)

    player.brain.nav.updateDest(home)
Example #50
0
def adjustHeading(player):

    if player.firstFrame():
        # Spin to home heading
        player.stand()
        player.brain.tracker.repeatFixedPitchLookAhead()
        dest = RelRobotLocation(0, 0, adjustHeading.desiredHeading - player.brain.loc.h)
        player.brain.nav.goTo(dest, precision = nav.HOME,
                          speed = speeds.SPEED_SIX, avoidObstacles = False,
                          fast = True, pb = False)
        # player.setWalk(0, 0, player.brain.loc.h - adjustHeading.desiredHeading)

        # or math.fabs()
    if fabs(player.brain.loc.h - adjustHeading.desiredHeading) < 15:
        player.stand()

        if role.isDefender(player.role):
            return player.goNow('watchForBall')
        else:
            return player.goNow("panAtWayPoint")
Example #51
0
def prepareForKick(player):
    if player.firstFrame():
        player.decider = KickDecider.KickDecider(player.brain)
        player.brain.nav.stand()

    if not player.inKickOffPlay:
        if player.shouldKickOff or player.brain.gameController.timeSincePlaying < 10:
            print "Overriding kick decider for kickoff!"
            player.shouldKickOff = False
            player.kick = player.decider.kicksBeforeBallIsFree()
        else:
            if roleConstants.isDefender(player.role):
                player.kick = player.decider.defender()
            else:
                player.kick = player.decider.attacker()
        player.inKickingState = True

    elif player.finishedPlay:
        player.inKickOffPlay = False

    player.motionKick = True
    return player.goNow('followPotentialField')
Example #52
0
def shouldSpinSearchFromWatching(player):
    shouldExtendTimer = player.commMode == 2 and role.isDefender(player.role)
    spinTimer = 25 if shouldExtendTimer else 12
    return (player.stateTime > spinTimer
            and player.brain.ball.vis.frames_off > 30)
Example #53
0
def ballInTheirHalf(player):
    if role.isDefender(player.role):
        return player.brain.sharedBall.ball_on and (
            player.brain.sharedBall.x >
            (nogginC.MIDFIELD_X + nogginC.CENTER_CIRCLE_RADIUS))
    return False
Example #54
0
def shouldFindSharedBall(player):
    return (role.isFirstChaser(player.role)
            and player.brain.ball.vis.frames_off > 10
            and player.brain.sharedBall.ball_on
            and player.brain.sharedBall.reliability >= 1)