Exemplo n.º 1
0
def shouldIBeStriker():
    # If there are 2 or less forwards alive, it is better that
    # non-attacker be a defender.
    if hTeam.forwardAlive() <= 2:
        return False

    # If there is an attacker and no striker, then be a striker.
    numAttacker = hTeam.countAttacker()
    numStriker = hTeam.countStriker()
    if numAttacker == 1 and numStriker == 0:
        return True
    # If there is no attacker and a striker, then be a striker.
    if numAttacker == 0 and numStriker == 1:
        return True

    # If my estimate time to reach striker pos is the smallest among
    # all the forwards except an attacker.
    selfLoc = Global.selfLoc
    myEstimateTime = hTrack.timeToReachStrikerPos(selfLoc.getX(),\
                                                 selfLoc.getY(),\
                                                 selfLoc.getHeading(),\
                                                 Global.myLastRole == Constant.STRIKER)
    for i in Global.otherValidForwards:
        mate = Global.teamPlayers[i]
        if not mate.isAttacker():

            mateLoc = Global.teammatesLoc[i]
            mateEstimateTime = hTrack.timeToReachStrikerPos(mateLoc.getX(),\
                                                       mateLoc.getY(),\
                                                       mateLoc.getHeading(),\
                                                       mate.isStriker())
            if myEstimateTime > mateEstimateTime:
                break
    else:
        # When this else branch is reached, it means I can be a
        # striker. But before that, I should check if I should
        # break symmetry when there are more than 1 striker on the
        # field.
        if Global.myLastRole == Constant.STRIKER\
            and Global.myRoleCounter > SYMMETRY_BREAK_COUNT:

            for i in Global.otherValidForwards:
                mate = Global.teamPlayers[i]
                if mate.isStriker()\
                    and mate.getRoleCounter() > SYMMETRY_BREAK_COUNT:

                    if shouldIBreakStrikerSymmetry(i):
                        return False
            else:
                return True

        else:
            return True

    return False
Exemplo n.º 2
0
def shouldIStart():

    global strikerPtX, strikerPtY

    debug = False
    strikerPtX, strikerPtY = getStrikerPointXPositioning()

    ##~     if (Global.ballSource == Constant.VISION_BALL)
    ##~         return False

    # If you don't know where the ball is (cant see and no wireless source), don't assume striker role.
    # TOTHINK: Do we want to let the robot to become striker if it uses the wireless ball?
    if (Global.ballSource == Constant.GPS_BALL
            and Global.lostBall > Constant.VBALL_LOST
            and Global.sharedBallVar > HelpMath.get95cf(20)):
        if debug:
            print "Striker I don't know where the ball is, I won't be striker!\n"
        return False

    # If ball is very close to target goal, assume striker role.
    # If you don't have ball, another robot has ball, and you are closest to the
    # striker point.
    # ie. If the ball is close to the target goal, try to ensure that there will be a striker.
    #      Not implementated at the moment, since this is really a hack. Put it back when needed.
    # ..
    # .. Not ported to python now.
    # WHAT THE HELL U ARE TALKING ABOUT TED. U ARE LAZY


##~     ballDistToTargetGoal = HelpMath.getDistanceBetween(bx,by,Constant.TARGET_GOAL_X,Constant.TARGET_GOAL_Y)
##~
##~     if ballDistToTargetGoal < 65:
##~         if (not HelpTrack.doIHaveTheBall() and HelpLong.teammateHasBall() and
##~             amIClosestToStrikerPoint(strikerPtX,strikerPtY)):
##~
##~                 for i in range (Constant.NUM_TEAM_MEMBER):
##~                     if (Global.teamPlayers[i].getCounter() <=0 or
##~                         Global.teamPlayers[i].isGoalie() != 0 or
##~                         i == Global.MyPlayerNum - 1):
##~                         continue
##~                     if (Global.teamPlayers[i].isStriker() and
##~                         Global.MyPlayerNum < i + 1):
##~                         return False
##~
##~                 if debug:
##~                     print "Striker TRUE, ball close to goal but no other striker"
##~                 return True

# If you only know of 1 other forward don't become striker (its better to become supporter)
    if (HelpTeam.forwardAlive() < 3
            and Global.framesSince3ForwardsPresent >= 3):
        if debug:
            print "Striker FALSE, can't find enough forwards!\n"
        return False

    # Find the teammate that is furthest from ball
    teammateFurthestFromBallPlayer = -1
    teammateFurthestFromBallDistance = -1

    # Linear search all the teammates.
    for i in range(Constant.NUM_TEAM_MEMBER):
        # Ignore goalie, yourself, and invalid teammate.
        if (Global.teamPlayers[i].getCounter() <= 0
                or Global.teamPlayers[i].isGoalie() != 0
                or i == Global.myPlayerNum - 1):
            continue

        # If they are alrady doing the striker role and they have a high player number than you, don't do.
        # Player number thing is just to break symmetery.
        if Global.teamPlayers[i].isStriker() and Global.myPlayerNum < i + 1:
            if debug:
                print "Striker Someone higher is already a striker!"
            return False

        teammateDist = -1

        # If they can see the ball themselves.
        if Global.teamPlayers[i].getBallDist() < Constant.LOSTBALL_DIST:
            teammateDist = Global.teamPlayers[i].getBallDist()
        else:
            dx = Global.ballX - Global.teammatesLoc[i].getPos()[0]
            dy = Global.ballY - Global.teammatesLoc[i].getPos()[1]
            teammateDist = math.sqrt((dx * dx) + (dy * dy))

        if teammateDist > teammateFurthestFromBallDistance:
            teammateFurthestFromBallPlayer = i
            teammateFurthestFromBallDistance = teammateDist

    if debug:
        print "Teammate distance furthest from the ball: %f" % teammateFurthestFromBallDistance

    # If you're the only forward alive, don't become striker.
    if teammateFurthestFromBallPlayer == -1:
        if debug:
            print "False I am the only one alive!"
        return False

    # If you are obviously furthest away from the ball, become wing striker.
    if Global.ballD - teammateFurthestFromBallDistance > Constant.FURTHEST_OFFSET:
        if debug:
            print "True I am obviosuly furthest from the ball!\n"
        return True

    # If you are obviosuly not furthest away from the ball, don't become wing striker.
    if teammateFurthestFromBallDistance - Global.ballD > Constant.FURTHEST_OFFSET:
        if debug:
            print "False I am obviosuly not furthest away from the ball!\n"
        return False

    # At this point you are not obviosuly furthest away from the ball.

    # Obtain own distance to striker point.
    selfX, selfY = Global.selfLoc.getPos()
    ownDistToStrikerPt = HelpMath.getDistanceBetween(selfX, selfY, strikerPtX,
                                                     strikerPtY)

    if debug:
        print "My distance to strike point: %f" % ownDistToStrikerPt

    # Obtain teammate distance to striker point.
    # Return global position of the teammate.
    a = strikerPtX - Global.teammatesLoc[teammateFurthestFromBallPlayer].getX()
    b = strikerPtY - Global.teammatesLoc[teammateFurthestFromBallPlayer].getY()
    teammateDistToStrikerPt = math.sqrt((a * a) + (b * b))

    # If closer to the striker point, become striker.
    if ownDistToStrikerPt < teammateDistToStrikerPt:
        if debug:
            print "I am closer than my teammates for the striker point!"
        return True
    else:
        if debug:
            print "I am not closer than my teammates for the striker point!"
        return False
Exemplo n.º 3
0
def shouldIBeSupporter():
    # If there are 2 or less forwards alive, it is better that
    # non-attacker to be a defender.
    if hTeam.forwardAlive() <= 2:
        return False

    # If there is an attacker and no supporter, then be a supporter.
    numAttacker = hTeam.countAttacker()
    numSupporter = hTeam.countSupporter()
    if numAttacker == 1 and numSupporter == 0:
        return True
    # If there is no attacker and a supporter, then be a supporter.
    if numAttacker == 0 and numSupporter == 1:
        return True

    # If you are the furthest back in the field, then be a defender instead.
    if hTeam.amIFurthestBack(30):
        return False

    # If my estimate time to reach defender pos is the largest among
    # all the forwards except an attacker.
    selfLoc = Global.selfLoc
    myEstimateTime = hTrack.timeToReachDefenderPos(selfLoc.getX(),\
                                         selfLoc.getY(),\
                                         selfLoc.getHeading(),\
                                         Global.myLastRole == Constant.DEFENDER)
    for i in Global.otherValidForwards:
        mate = Global.teamPlayers[i]
        if not mate.isAttacker():

            mateLoc = Global.teammatesLoc[i]
            mateEstimateTime = hTrack.timeToReachDefenderPos(mateLoc.getX(),\
                                                       mateLoc.getY(),\
                                                       mateLoc.getHeading(),\
                                                       mate.isDefender())

            # hysterisis introduced for furthest back.
            if myEstimateTime < mateEstimateTime and hTeam.amIFurthestBack(0):
                break  # It returns False
    else:
        # When this else branch is reached, it means I can be a
        # supporter. But before that, I should check if I should
        # break symmetry when there are more than 1 supporter on the
        # field.
        if Global.myLastRole == Constant.SUPPORTER\
            and Global.myRoleCounter > pForward.SYMMETRY_BREAK_COUNT:

            for i in Global.otherValidForwards:
                mate = Global.teamPlayers[i]
                if mate.isSupporter()\
                    and mate.getRoleCounter() > pForward.SYMMETRY_BREAK_COUNT:

                    if pForward.shouldIBreakSupporterSymmetry(i):
                        return False
            else:
                return True

        else:
            return True

    return False
Exemplo n.º 4
0
def readyMatchPlaying():
    global debug, timer, inStartPos, isFacingTargetGoal, getOutOfGoalDirection, lastTurningDirection, gUseDodgyDog

    selfX = Global.selfLoc.getPos()[0]
    selfY = Global.selfLoc.getPos()[1]
    selfH = Global.selfLoc.getHeading()
    Action.closeMouth()

    # Player number detection. The goalie *must* be player one. Freak if not.
    if Global.myPlayerNum == 1 and not Global.isGoalieBehaviour:
        Action.kick(Action.ChestPushWT)
        return
    elif Global.myPlayerNum != 1 and Global.isGoalieBehaviour:
        Action.kick(Action.ChestPushWT)
        return

    if not Global.penaltyShot and hStuck.amIStuckForward(dodgeTime=9):
        return

    # Need to move toward the field ?
    if shouldITurnTowardTheField():
        Action.walk(0, 0, getOutOfGoalDirection)

    # Am I in the correct starting position and facing toward the target goal
    # (or facing upward)?
    if inStartPos and isFacingTargetGoal:

        # use the ball distance to try to move as close as the centre circle
        #if VisionLink.getKickOffTeam() == VisionLink.getTeamColor() and \
        #       useBallDistanceSetKickOffPosition():
        #    return

        #  Do I need to move to a new position if I am in a wrong position?
        if Debug.allowReadyMove:
            if hMath.getDistanceBetween(selfX, selfY, \
                    Global.kickOffPos[0], Global.kickOffPos[1]) \
                        > Constant.POS_OFFSET_OUT_KICK:
                inStartPos = False
                return

        # Turn myself if I am not facing toward the desired heading. At the
        # moment 90 is the desired heading angle.
        # TODO: Accoiding to different positions, different desired heading.
        if abs(hMath.normalizeAngle_180(selfH - 90)) > \
            Constant.READY_HEADING_OFFSET:
            isFacingTargetGoal = False
            return

        inPositionAndFacingTargetLocalise()
        return

    # Am I in the correct starting position but not facing the target goal (or
    # facing upward)?
    if inStartPos:
        #  Do I need to move to a new position if I am in a wrong position?
        if Debug.allowReadyMove:
            if hMath.getDistanceBetween(selfX, selfY, \
                Global.kickOffPos[0], Global.kickOffPos[1])\
                    > Constant.POS_OFFSET_OUT_KICK:
                inStartPos = False
                return

        # Is me facing toward the target goal now? If I am facing the right
        # direction, then better reset the turning direction variable.
        if abs(selfH - 90) < Constant.READY_HEADING_OFFSET:
            isFacingTargetGoal = True
            lastTurningDirection = None

        # If I am not facing toward the target goal, then turn slowly without
        # moving forward or left.
        else:
            # Determine the best turning direction and stick with it.
            if lastTurningDirection == None:
                # I haven't determine my turning direction yet. Determine now.
                if Global.selfLoc.getHeading() >= 270 or \
                   Global.selfLoc.getHeading() <= 90: # -90 <= angle <= 90
                    lastTurningDirection = Constant.dANTICLOCKWISE
                    if Debug.readyDebug:
                        print "Anti-clockwise direction!"
                else:
                    # 90 < angle < 270
                    lastTurningDirection = Constant.dCLOCKWISE
                    if Debug.readyDebug:
                        print "Clockwise direction!"

            if lastTurningDirection == Constant.dCLOCKWISE:
                Action.walk(0, 0, -15)
            else:
                Action.walk(0, 0, 15)
        return

    # Phase1: I am not in the correct starting position. First step for me is
    # to stand still and beacon localise.
    if timer <= INITIAL_LOCALISE_TIME:
        if Debug.readyDebug and timer == 1:
            print "Phase 1 has been executed!"
        if timer <= 79:
            Global.kickOffState = Constant.NOTDECIDED
        Action.stopLegs()

        # Need to move toward the field ?
        if shouldITurnTowardTheField():
            Action.walk(0, 0, getOutOfGoalDirection)

        hTrack.stationaryLocalise()
        timer = timer + 1
        if timer >= 80:
            # Now decide my starting kickoff position dynamically now.
            if hTeam.robotAlive() != 1:
                getMyKickOffPosition()
        return

    lastTurningDirection = None

    # Now we can either use the static result from getMyKickOffPosition() or
    # dynamically calculated again,
    if hTeam.robotAlive() != 1:
        getMyKickOffPosition()

    # If I am unable to find the kickoff position earlier (possibly because of
    # wirless problem), then probably only 2 or less robots are on the field
    # now. Notice that I can't be a goalie in this case.  I must be either
    # player 2 forward or player 3 forward. Not player 4 forward because it
    # determines its kicking position without worrying about any other
    # forwards.
    if Global.kickOffState == Constant.NOTDECIDED or Global.kickOffPos == None:
        alive = hTeam.forwardAlive()
        if alive >= 3:
            if Debug.readyDebug:
                print "Something went wrong, three forwards without assigning! Automatical assign!"

##~             # This case may happen in friendly games where not all four robots are present.
##~             # Firstly if there are three forwards alive, but I am not assigned a position. Then:
##~             #   1. I can't be player 1 because this is a goalie
##~             #   2. I can't be player number 4 forward.
##~             # So I must be either player 2 or player 3 forward.
##~             #
##~             # If I am a player 3, then probably the three forwards are: 1, 2, 3
##~             # If I am a player 2, then probably the three forwards are: 1, 2, 4
##~
            if Global.myPlayerNum == 3:
                _, Global.kickOffPos, _, _ = getAllPossibleKickingOffPosition()
                Global.kickOffState = Constant.DECIDEPOSITION_B

            elif Global.myPlayerNum == 2:
                # Try to get player 4's decision.
                if Global.teamPlayers[3].getCounter() > 0:
                    position = Global.teamPlayers[3].getKickedOff()

                    if position == Constant.DECIDEPOSITION_A:
                        determineMyKickingOffPositionIfOnlyMeForwardAlive(
                            False, True, True)
                    elif position == Constant.DECIDEPOSITION_B:
                        determineMyKickingOffPositionIfOnlyMeForwardAlive(
                            True, False, True)
                    elif position == Constant.DECIDEPOSITION_C:
                        determineMyKickingOffPositionIfOnlyMeForwardAlive(
                            True, True, False)
                    else:
                        Debug.goAndMakeTraceBack()
                        return

                else:
                    # Well, lets wait.
                    return

        elif alive == 2:
            if Debug.readyDebug:
                print "Only two alive case - What the hell?"
            if Global.myPlayerNum == 2:
                Global.kickOffPos, b, c, d = getAllPossibleKickingOffPosition()
                Global.kickOffState = Constant.DECIDEPOSITION_A
            elif Global.myPlayerNum == 3:
                a, Global.kickOffPos, c, d = getAllPossibleKickingOffPosition()
                Global.kickOffState = Constant.DECIDEPOSITION_B
            else:
                if Debug.readyDebug:
                    print "Something went wrong, I am not player 2 or 3, why not decided position?"

        elif alive == 1:
            if Debug.readyDebug:
                print "Only one alive case!"

            a, b, c, d = getAllPossibleKickingOffPosition()

            if Global.myPlayerNum == 2:
                Global.kickOffPos = a
                Global.kickOffState = Constant.DECIDEPOSITION_A
            elif Global.myPlayerNum == 3:
                Global.kickOffPos = b
                Global.kickOffState = Constant.DECIDEPOSITION_B
            elif Global.myPlayerNum == 4:
                Global.kickOffPos = c
                Global.kickOffState = Constant.DECIDEPOSITION_C
            else:  # Goalie
                Global.kickOffPos = d
                Global.kickOffState = Constant.DECIDEPOSITION_D

    # At this point the kicking off position must been determined.
    showKickOffHeadIndicator()
    # Set the initial roles based on position
    if Global.kickOffPos == P_REC_CENTRE:
        Global.myRole = Constant.ATTACKER
    elif Global.kickOffPos == P_REC_LEFT_WIDE \
            or Global.kickOffPos == P_REC_RIGHT_WIDE:
        Global.myRole = Constant.DEFENDER
    elif Global.kickOffPos == P_REC_RIGHT_CLOSE \
            or Global.kickOffPos == P_REC_LEFT_CLOSE:
        Global.myRole = Constant.SUPPORTER
    elif Global.kickOffPos == P_KICK_KICKER:
        Global.myRole = Constant.ATTACKER
    elif Global.kickOffPos == P_KICK_LEFT_BACK \
            or Global.kickOffPos == P_KICK_RIGHT_BACK:
        Global.myRole = Constant.DEFENDER
    elif Global.kickOffPos == P_KICK_RIGHT_FORWARD \
            or Global.kickOffPos== P_KICK_LEFT_FORWARD:
        Global.myRole = Constant.SUPPORTER

    # Testing the kickoff position without actually moving them.
##~     if Debug.readyDebug and Debug.testKickOffPosition:
##~         if Global.kickOffState == None:
##~             print "My kickoff position is: None %f" %Global.myPlayerNum
##~         else:
##~             print "My player number: %f" %Global.myPlayerNum
##~             print "My kickoff position is: %f" %Global.kickOffState
##~         return

# Move from phase 2 to phase 3. Am I in the correct starting position now?
    dist = hMath.getDistanceBetween(selfX, selfY, Global.kickOffPos[0],
                                    Global.kickOffPos[1])
    if dist < Constant.POS_OFFSET_IN_KICK:
        # Ok, I am in the correct starting position. I stop myself now.
        Action.stopLegs()
        inStartPos = True

    # Phase2: I need to walk to my starting position. Localise by moving the
    # head while I move.
    else:
        if dist < sDodgyDog.MIN_DODGY_TARGET_DIST:
            hTrack.stationaryLocalise()
        else:
            # Move head less while dodgy to see obstacles
            hTrack.stationaryLocalise(5, 30, -30)

        if gUseDodgyDog and \
                sDodgyDog.shouldIBeDodgy(Global.kickOffPos[0],
                                        Global.kickOffPos[1], True,
                                        Constant.POS_OFFSET_OUT_KICK):
            if sDodgyDog.dodgyDogTo(
                    Global.kickOffPos[0], Global.kickOffPos[1], True,
                    Constant.POS_OFFSET_OUT_KICK) == Constant.STATE_SUCCESS:
                inStartPos = True
        else:
            movingAction()