Esempio n. 1
0
    def triangulateClearKick(self):
        # Assert: Neither far post was seen.
        #         Both near posts were seen.
        bearingDiff = self.nearLeftPostBearing - self.nearRightPostBearing
        leftDist = self.nearRightPostDist
        rightDist = self.nearLeftPostDist
        leftPostY = constants.LANDMARK_BLUE_GOAL_TOP_POST_Y
        rightPostY = constants.LANDMARK_BLUE_GOAL_BOTTOM_POST_Y
        farSideLenth = constants.CROSSBAR_CM_WIDTH

        # Should be generalized into a helper method somewhere.
        y = (math.pow(leftPostY,2) + math.pow(leftDist,2) - math.pow(rightPostY,2) - math.pow(rightDist,2)) / (2 * (leftPostY - rightPostY))
        x = math.sqrt(math.pow(leftDist,2) - math.pow((y-rightPostY),2))

        # Use locations to get global heading.
        post = Objects.Location(constants.FIELD_WHITE_LEFT_SIDELINE_X,constants.LANDMARK_BLUE_GOAL_TOP_POST_Y)
        me = Objects.Location(x,y)
        meToPost = me.relativeLocationOf(post)

        myGlobalHeading = meToPost.bearing - self.nearLeftPostBearing
        if myGlobalHeading < -180:
            myGlobalHeading += 360
        elif myGlobalHeading > 180:
            myGlobalHeading -= 360

        return self.chooseClearKick(myGlobalHeading, x, y)
Esempio n. 2
0
    def goalieBasedKick(self):
        # Assert: Neither far post was seen.
        #         At least one near post was seen.
        #         Either Goalie can see the ball, or not.
        goalieBearing = 0
        goalieDist = 0
        kick = None

        for mate in self.brain.teamMembers:
            if mate.playerNumber == 1:
                goalieBearing = mate.ballBearing
                goalieDist = mate.ballDist

        # Sanity check.
        if goalieBearing == 0 or goalieDist == 0:
            # Something went wrong. Abort and default to no info orbit.
            kick = kicks.ORBIT_KICK_POSITION
            kick.h = 45
            return kick

        # Calculate coordinates of ball, assuming goalie is perfectly centered.
        goalieX = nogginConstants.FIELD_WHITE_LEFT_SIDELINE_X
        goalieY = nogginConstants.CENTER_FIELD_Y
        diffX = math.cos(math.radians(goalieBearing)) * goalieDist
        diffY = math.sin(math.radians(goalieBearing)) * goalieDist
        ballX = goalieX + diffX
        ballY = goalieY + diffY
        ballLocation = Objects.Location(ballX, ballY)

        # Calculate your bearing on the post with more sightings
        if len(self.nearGoalRightPostBearings) > len(
                self.nearGoalLeftPostBearings):
            # Use the right post
            relPostBearing = self.nearRightPostBearing
            ballToPost = ballLocation.relativeLocationOf(
                Objects.Location(
                    nogginConstants.FIELD_WHITE_LEFT_SIDELINE_X,
                    nogginConstants.LANDMARK_BLUE_GOAL_BOTTOM_POST_Y))
        else:
            # Use the left post
            relPostBearing = self.nearLeftPostBearing
            ballToPost = ballLocation.relativeLocationOf(
                Objects.Location(
                    nogginConstants.FIELD_WHITE_LEFT_SIDELINE_X,
                    nogginConstants.LANDMARK_BLUE_GOAL_TOP_POST_Y))

        ballToPostBearing = ballToPost.bearing
        myGlobalHeading = ballToPostBearing - relPostBearing
        if myGlobalHeading < -180:
            myGlobalHeading += 360
        elif myGlobalHeading > 180:
            myGlobalHeading -= 360

        return self.chooseClearKick(myGlobalHeading, ballX, ballY)
Esempio n. 3
0
def ballToOurLeft(player):
    """
    The ball is to our left.
    """
    goalCenter = objects.Location(nogginConstants.FIELD_WHITE_RIGHT_SIDELINE_X,
                                  nogginConstants.CENTER_FIELD_Y)
    ballLocation = objects.Location(player.brain.ball.x, player.brain.ball.y)

    headingBallToGoalCenter = ballLocation.headingTo(goalCenter)
    bearingForKick = headingBallToGoalCenter - player.brain.loc.h

    return (bearingForKick > 0)
Esempio n. 4
0
def facingGoal(player):
    """
    We are facing generally towards the goal we want to score on.
    """
    goalCenter = objects.Location(nogginConstants.FIELD_WHITE_RIGHT_SIDELINE_X,
                                  nogginConstants.CENTER_FIELD_Y)
    ballLocation = objects.Location(player.brain.ball.x, player.brain.ball.y)

    headingBallToGoalCenter = ballLocation.headingTo(goalCenter)
    bearingForKick = headingBallToGoalCenter - player.brain.loc.h

    return (bearingForKick < constants.FACING_FORWARD_DEG and
            bearingForKick > -constants.FACING_FORWARD_DEG)
Esempio n. 5
0
def sync_sites(session: http_session.PortalSession, sites, locations: dict):
    """
    Convert UFO sites into Awesome locations.

    Either update or create a new location for each site.

    :param session: Awesome portal HTTP session
    :param sites: UFO sites
    :param locations: Map of Awesome location names to identifiers
    """

    for site in sites:
        LOGGER.debug("SITE %s", site)

        # Convert to Awesome object
        location = maps.site_to_location(site)

        try:
            # Retrieve existing location
            location_id = locations[site['name']]
            loc = objects.Location(location_id)

            # Amend existing location
            loc.update(session, location)

        # Location doesn't exist on in Awesome database
        except KeyError:

            # Create new location
            body = objects.Location.add(session, location)
            new_location = body['data']

            # Store id of new location
            locations[new_location['name']] = new_location['id']
Esempio n. 6
0
    def setUp(self):
        self.job = objects.Job()
        self.job.employer = "Nike"
        self.job.title = "Designer"
        self.loc = objects.Location()
        self.loc.city = "Portland"
        self.loc.state = "OR"

        self.obj = objects.Female()
        self.obj.name = "Anne"
        self.obj.age = 27
        self.obj.height = 65
        self.obj.weight = 120

        self.person = {
            "name":
            u"Anne",
            "age":
            27,
            "height":
            65.0,
            "weight":
            120.0,
            "genitalia":
            u"inny",
            "jobs": [{
                "employer": u"Nike",
                "title": u"Designer",
                "locations": [{
                    "city": u"Portland",
                    "state": u"OR"
                }]
            }]
        }
Esempio n. 7
0
 def setUp(self):
     self.name = "Anne"
     self.genitalia = "inny"
     self.obj = objects.Female()
     self.job = objects.Job()
     self.loca = objects.LocationGeo()
     self.location = objects.Location()
Esempio n. 8
0
    def test_bad_rel_type(self):
        with self.assertRaises(Exception) as cm:
            self.obj.jobs.append(objects.Location())

        print cm.exception
Esempio n. 9
0
    def shoot(self):
        """
        returns the kick we should do in a shooting situation
        """
        kick = None

        if DEBUG_KICK_DECISION:
            print "In method shoot."

        # Is loc GOOD_ENOUGH for a kick decision?
        # Need to use aimCenter in decision.
        if USE_LOC: #self.brain.my.locScore == constants.locScore.GOOD_LOC:

            if DEBUG_KICK_DECISION:
                #print "loc Score is good. Using it to decide kick."
                print "Flag is set: using loc for decision."

            relLocationBallToGoal = self.brain.ball.loc.relativeLocationOf(Objects.Location(constants.FIELD_WHITE_RIGHT_SIDELINE_X,constants.CENTER_FIELD_Y))
            bearingBallToGoal = relLocationBallToGoal.bearing
            # Assume our bearing at the ball will equal our current bearing
            relLocationMeToBall = self.brain.my.relativeLocationOf(self.brain.ball.loc)
            bearingMeToBall = relLocationMeToBall.bearing

            bearingDifference = bearingBallToGoal - bearingMeToBall

            if bearingDifference < 35 and bearingDifference > -35:
                #choose straight kick!
                kick = self.chooseQuickFrontKick()
                kick.h = 0 - bearingDifference
            elif bearingDifference > 35 and bearingDifference < 125:
                #choose a right side kick! (using right foot)
                kick = kicks.RIGHT_SIDE_KICK
                kick.h = 70 - bearingDifference
            elif bearingDifference < -35 and bearingDifference > -125:
                #choose a left side kick! (using left foot)
                kick = kicks.LEFT_SIDE_KICK
                kick.h = -70 - bearingDifference
            else:
                #choose a back kick!
                kick = self.chooseBackKick()
                if bearingDifference < -125:
                    kick.h = -180 - bearingDifference
                else:
                    kick.h = 180 - bearingDifference

            # Make sure heading is an int before passing it to the orbit.
            kick.h = int(kick.h)
            return kick


        # Loc is bad- use only visual information to choose a kick.
        if DEBUG_KICK_DECISION:
            print "Using vision for kick decision."
            print "Dangerous ball count: ",self.dangerousBallCount
            print "Own goalie in near goal? ",self.nearGoalieOwn

        # Determine which goal to aim at
        if self.farAvgPostDist != 0 and self.nearAvgPostDist != 0:
            # Goalie detection too easily fooled.
            #  Screw it. We need it anyway.
            if self.dangerousBallCount > 5 or self.nearGoalieOwn or \
                    self.brain.onOwnFieldSide:
                rightPostBearing = self.farRightPostBearing
                leftPostBearing = self.farLeftPostBearing
            else:
                rightPostBearing = self.nearRightPostBearing
                leftPostBearing = self.nearLeftPostBearing
        elif self.farAvgPostDist != 0:
            rightPostBearing = self.farRightPostBearing
            leftPostBearing = self.farLeftPostBearing
        elif self.nearAvgPostDist != 0:
            # Goalie detection too easily fooled.
            #  Screw it. We need it anyway.
            if self.dangerousBallCount > 5 or self.nearGoalieOwn or \
                    self.brain.onOwnFieldSide:

                # Can only see our own goal: Use goalie to make decision
                if self.dangerousBallCount > 5:
                    if DEBUG_KICK_DECISION:
                        print "Doing a goalie based clearing kick."
                    return self.goalieBasedKick()

                # Saw two posts: use them to triangulate.
                if self.nearRightPostBearing != 0 and self.nearLeftPostBearing != 0:
                    if DEBUG_KICK_DECISION:
                        print "Saw two own posts. Triangulating and clearing."
                    return self.triangulateClearKick()

                # Saw one post: oh god, what now?
                # Default to no-info orbit.
                """
                if self.nearRightPostBearing != 0:
                    pass
                else:
                    pass
                """
                kick = kicks.ORBIT_KICK_POSITION
                kick.h = 45
                return kick
            else:
                rightPostBearing = self.nearRightPostBearing
                leftPostBearing = self.nearLeftPostBearing
        else:
            # No information at all? Orbit arbitrarily and try again.
            kick = kicks.ORBIT_KICK_POSITION
            kick.h = 45
            return kick

        if DEBUG_KICK_DECISION:
            print "farRightPostBearing: ",self.farRightPostBearing
            print "farLeftPostBearing: ",self.farLeftPostBearing
            print "nearRightPostBearing: ",self.nearRightPostBearing
            print "nearLeftPostBearing: ",self.nearLeftPostBearing
            print "rightPostBearing: ",rightPostBearing
            print "leftPostBearing:  ",leftPostBearing

        if rightPostBearing == 0 and leftPostBearing == 0:
            # Can't see any posts: orbit.
            # Note: this case should already be covered above,
            #  but is repeated for safety.
            kick = kicks.ORBIT_KICK_POSITION
            kick.h = 45
            return kick
        elif rightPostBearing != 0 and leftPostBearing != 0:
            # Can see both posts: shoot between them.
            leftScorePoint = rightPostBearing - 10
            rightScorePoint = leftPostBearing + 10

            if leftScorePoint < rightScorePoint:
                # less than 20 degrees of goal available to shoot in.
                leftScorePoint = ((rightPostBearing + leftPostBearing) / 2) + 5
                rightScorePoint = leftScorePoint - 10
        elif rightPostBearing != 0:
            # Can only see the right post.
            leftScorePoint = rightPostBearing - 15
            rightScorePoint = leftScorePoint - 10
        else:
            # Can only see the left post.
            rightScorePoint = leftPostBearing + 15
            leftScorePoint = rightScorePoint + 10

        if DEBUG_KICK_DECISION:
            print "rightScorePoint: ",rightScorePoint
            print "leftScorePoint:  ",leftScorePoint

        # If any kick is currently valid, choose that kick.
        # Note: this ignores the aimCenter distinction.
        if leftScorePoint > 0 and rightScorePoint < 0:
            kick = self.chooseQuickFrontKick()
        elif leftScorePoint > 70 and rightScorePoint < 70:
            kick = kicks.RIGHT_SIDE_KICK
        elif leftScorePoint > -70 and rightScorePoint < -70:
            kick = kicks.LEFT_SIDE_KICK
        elif leftScorePoint < -90 and rightScorePoint > 90:
            kick =self.chooseBackKick()

        # Did we pick a kick yet?
        if kick is not None:
            kick.h = 0 # Straight is fine.
            return kick

        # Choose whichever kick is closest to being between the score points.
        # Note: no kick bearing is between the posts, so they are all
        #   to the right of the rightScorePoint or left of leftScorePoint.
        # If not aimCenter, pick whichever kick is closest to either score point.
        avgScorePoint = int((rightScorePoint + leftScorePoint) * .5)

        if DEBUG_KICK_DECISION:
            print "Didn't choose a 0 heading kick.\navgScorePoint: ",avgScorePoint

        if rightScorePoint > 70:
                # Quadrant 2
            if (180 - leftScorePoint) - (rightScorePoint - 70) < 0:
                    #Closer to the leftScorePoint
                kick = self.chooseBackKick()
                if self.aimCenter:
                    kick.h = 180 - avgScorePoint
                else:
                    kick.h = 180 - leftScorePoint
            else:
                kick = kicks.RIGHT_SIDE_KICK
                if self.aimCenter:
                    kick.h = 70 - avgScorePoint
                else:
                    kick.h = 70 - rightScorePoint
        elif rightScorePoint > 0:
            # Quadrant 1
            if (70 - leftScorePoint) - (rightScorePoint - 0) < 0:
                kick = kicks.RIGHT_SIDE_KICK
                if self.aimCenter:
                    kick.h = 70 - avgScorePoint
                else:
                    kick.h = 70 - leftScorePoint
            else:
                kick = self.chooseQuickFrontKick()
                if self.aimCenter:
                    kick.h = 0 - avgScorePoint
                else:
                    kick.h = 0 - rightScorePoint
        elif rightScorePoint > -70:
            # Quadrant 4
            if (0 - leftScorePoint) - (rightScorePoint + 70) < 0:
                kick = self.chooseQuickFrontKick()
                if self.aimCenter:
                    kick.h = 0 - avgScorePoint
                else:
                    kick.h = 0 - leftScorePoint
            else:
                kick = kicks.LEFT_SIDE_KICK
                if self.aimCenter:
                    kick.h = -70 - avgScorePoint
                else:
                    kick.h = -70 - rightScorePoint
        else:
            # Quadrant 3
            if (-70 - leftScorePoint) - (rightScorePoint + 180) < 0:
                kick = kicks.LEFT_SIDE_KICK
                if self.aimCenter:
                    kick.h = -70 - avgScorePoint
                else:
                    kick.h = -70 - leftScorePoint
            else:
                kick = self.chooseBackKick()
                if self.aimCenter:
                    kick.h = -180 - avgScorePoint
                else:
                    kick.h = -180 - rightScorePoint

        if kick is not None:
            return kick

        if DEBUG_KICK_DECISION:
            print "Somehow got to end without a kick..."

        # If all else fails, orbit and re-decide.
        # Note: this case should already be covered above,
        #  but is repeated for safety.
        kick = kicks.ORBIT_KICK_POSITION
        kick.h = 45
        return kick
Esempio n. 10
0
    def shoot(self):
        """
        returns the kick we should do in a shooting situation
        """
        kick = None

        # Is loc good enough for a kick decision?
        # Need to use aimCenter in decision.
        if USE_LOC:
            # COMPETITION HACK -- dribbling from the corner
            if (self.brain.ball.x > constants.FIELD_WHITE_RIGHT_SIDELINE_X - 65
                    and
                (self.brain.ball.y < constants.LANDMARK_OPP_GOAL_RIGHT_POST_Y -
                 30 or self.brain.ball.y >
                 constants.LANDMARK_OPP_GOAL_LEFT_POST_Y + 30)):
                self.brain.player.corner_dribble = True
                kick = self.chooseShortFrontKick()
                kick.h = self.brain.loc.headingTo(
                    Objects.Location(constants.YELLOW_GOALBOX_LEFT_X - 100,
                                     constants.MIDFIELD_Y))
                return kick

            # Get the bearing for the shot, i.e. from the ball to the goal.
            # Note: currently always aimCenter
            goalCenter = Objects.Location(
                constants.FIELD_WHITE_RIGHT_SIDELINE_X,
                constants.CENTER_FIELD_Y)
            goalLeft = Objects.Location(
                constants.LANDMARK_OPP_GOAL_LEFT_POST_X,
                constants.LANDMARK_OPP_GOAL_LEFT_POST_Y)
            goalRight = Objects.Location(
                constants.LANDMARK_OPP_GOAL_RIGHT_POST_X,
                constants.LANDMARK_OPP_GOAL_RIGHT_POST_Y)

            ballLocation = Objects.Location(self.brain.ball.x,
                                            self.brain.ball.y)
            closeShot = (self.brain.loc.distTo(goalCenter) < 100
                         )  # within 1 meter
            ourHalf = (self.brain.loc.x < constants.MIDFIELD_X
                       )  # on our half of the field

            headingBallToGoalCenter = ballLocation.headingTo(goalCenter)
            headingBallToGoalLeft = ballLocation.headingTo(goalLeft)
            headingBallToGoalRight = ballLocation.headingTo(goalRight)

            if DEBUG_KICK_DECISION:
                print "Heading from the ball to the goal center: " + str(
                    headingBallToGoalCenter)
                print "My global heading on the field: " + str(
                    self.brain.loc.h)

            # Assume our heading at the ball will equal our current heading
            # We shouldn't be spinning at this point, so the assumption is valid.
            # Note: all headings are in degrees at this point.
            bearingForKick = headingBallToGoalCenter - self.brain.loc.h
            bearingKickLeft = headingBallToGoalLeft - self.brain.loc.h
            bearingKickRight = headingBallToGoalRight - self.brain.loc.h

            bearingLimitLeft = headingBallToGoalLeft - headingBallToGoalCenter
            bearingLimitRight = headingBallToGoalRight - headingBallToGoalCenter

            if not closeShot:

                if DEBUG_KICK_DECISION:
                    print("Acceptable bearing range for kick: " +
                          str(headingBallToGoalLeft) + "/" +
                          str(headingBallToGoalRight))

                kickBearings = [[0, 0], [70, 0],
                                [-70, 0]]  # straight, right side, left side
                for b in kickBearings:
                    adjustedBearingLeft = bearingKickLeft - b[0]
                    adjustedBearingRight = bearingKickRight - b[0]

                    if adjustedBearingLeft > 0 and adjustedBearingRight < 0:
                        #goal is in front of us
                        b[1] = 0
                    elif adjustedBearingLeft < 0 and adjustedBearingRight > 0:
                        #goal is behind us
                        if math.fabs(adjustedBearingLeft) < math.fabs(
                                adjustedBearingRight):
                            #use the left bearing
                            b[1] = -adjustedBearingLeft
                        else:
                            #use the right bearing
                            b[1] = -adjustedBearingRight
                    elif adjustedBearingRight > 0:
                        #goal is to our left
                        b[1] = -adjustedBearingRight
                    elif adjustedBearingLeft < 0:
                        #goal is to our right
                        b[1] = -adjustedBearingLeft

                sortedBearings = sorted(
                    map(math.fabs, [x[1] for x in kickBearings]))

                if sortedBearings[0] < math.fabs(
                        kickBearings[0][1]) - STRAIGHT_KICK_BIAS:
                    # side kicks must beat straight kick by more than ~10 degrees
                    if sortedBearings[0] == math.fabs(kickBearings[1][1]):
                        # choose a right side kick
                        kick = kicks.RIGHT_SIDE_KICK
                    elif sortedBearings[0] == math.fabs(kickBearings[2][1]):
                        # choose a left side kick
                        kick = kicks.LEFT_SIDE_KICK
                else:
                    # choose a straight kick
                    kick = self.chooseShortFrontKick()

                kick.h = sortedBearings[0]

                # If we're defending near our goal box, just kick it: clearing the ball
                # is more important than being super accurate.
                if (self.brain.loc.x < constants.LANDMARK_MY_FIELD_CROSS[0]
                        and self.brain.loc.y < constants.MY_GOALBOX_TOP_Y
                        and self.brain.loc.y > constants.MY_GOALBOX_BOTTOM_Y):
                    kick.h = 0
            else:  # if closeShot is True
                if DEBUG_KICK_DECISION:
                    print "I am shooting very close to the opponent's goal."

                if DEBUG_KICK_DECISION:
                    print("Acceptable bearing range for kick: " +
                          str(headingBallToGoalLeft) + "/" +
                          str(headingBallToGoalRight))

                if (bearingKickLeft - bearingKickRight) > 60:
                    # even an inaccurate straight kick will work
                    if (30 < bearingKickLeft and -30 > bearingKickLeft):
                        #choose a straight motion kick with no orbit NOW!
                        kick = self.chooseFrontMotionKick()
                        kick.h = 0
                        return kick
                    elif (bearingKickLeft < 30):
                        # closer to left limit
                        straightBearing = 30 - bearingKickLeft
                    else:  #assert bearingLimitRight > -30
                        # closer to right limit
                        straightBearing = -30 - bearingKickRight
                else:
                    # aim for the center and hope we're accurate enough
                    straightBearing = 0 - bearingForKick

                if (bearingKickLeft - bearingKickRight > 35):
                    # even an inaccurate side kick will work
                    if (90 < bearingKickLeft and 60 > bearingKickRight):
                        #choose a right side motion kick with no orbit NOW!
                        self.brain.player.motionKick = True
                        kick = kicks.M_RIGHT_SIDE
                        kick.h = 0
                        return kick
                    elif (-60 < bearingKickLeft and -90 > bearingKickRight):
                        #choose a left side kick with no orbit!
                        self.brain.player.motionKick = True
                        kick = kicks.M_LEFT_SIDE
                        kick.h = 0
                        return kick

                    if (bearingKickLeft < 90):
                        # right side kick is closer to left limit
                        rightSideBearing = 90 - bearingKickLeft
                    else:  #assert bearingKickRight > 60
                        rightSideBearing = 60 - bearingKickRight

                    if (bearingKickLeft < -60):
                        # left side kick is closer to the left limit
                        leftSideBearing = -60 - bearingKickLeft
                    else:  #assert bearingKickRight > -90
                        leftSideBearing = -90 - bearingKickRight
                else:
                    # aim for the center and hope we're accurate enough
                    rightSideBearing = 70 - bearingForKick
                    leftSideBearing = -70 - bearingForKick

                # Currently, we ignore the possibility of doing a back kick here.

                # make sure all the kick bearings are sub-180 angles
                straightBearing = MyMath.sub180Angle(straightBearing)
                rightSideBearing = MyMath.sub180Angle(rightSideBearing)
                leftSideBearing = MyMath.sub180Angle(leftSideBearing)

                # the kick bearing that has least magnitude should be chosen
                kickList = sorted(
                    [straightBearing, leftSideBearing, rightSideBearing],
                    key=math.fabs)

                if math.fabs(
                        kickList[0]
                ) < math.fabs(straightBearing) - STRAIGHT_KICK_BIAS:
                    # side kicks must beat straight kicks by more than ~10 degrees
                    if (kickList[0] == rightSideBearing):
                        kick = kicks.RIGHT_SIDE_KICK
                        kick.h = rightSideBearing
                    elif (kickList[0] == leftSideBearing):
                        kick = kicks.LEFT_SIDE_KICK
                        kick.h = leftSideBearing
                else:
                    # choose a straight kick
                    kick = self.chooseFrontMotionKick()
                    kick.h = straightBearing

            # Convert kick.h to be a desired global heading for the orbit.
            kick.h = self.brain.loc.h - kick.h

            # Make sure heading is an int before passing it to the orbit.
            kick.h = int(MyMath.sub180Angle(kick.h))

            if DEBUG_KICK_DECISION:
                print "Returning a kick with heading: " + str(kick.h)

            return kick

        # Loc is bad- use only visual information to choose a kick.
        if DEBUG_KICK_DECISION:
            print "Using vision for kick decision."
            print "Dangerous ball count: ", self.dangerousBallCount
            print "Own goalie in near goal? ", self.nearGoalieOwn

        # Determine which goal to aim at
        if self.farAvgPostDist != 0 and self.nearAvgPostDist != 0:
            # Goalie detection too easily fooled.
            #  Screw it. We need it anyway.
            if (self.dangerousBallCount > 5 or self.nearGoalieOwn):
                # Don't aim at the near goal.
                rightPostBearing = self.farRightPostBearing
                leftPostBearing = self.farLeftPostBearing
            else:
                # Aim at the near goal.
                rightPostBearing = self.nearRightPostBearing
                leftPostBearing = self.nearLeftPostBearing
        elif self.farAvgPostDist != 0:
            # Only saw far goal? Kick towards it and hope.
            # Goalie detection too unreliable for far goal?
            rightPostBearing = self.farRightPostBearing
            leftPostBearing = self.farLeftPostBearing
        elif self.nearAvgPostDist != 0:
            # Goalie detection too easily fooled.
            #  Screw it. We need it anyway.
            if (self.dangerousBallCount > 5 or self.nearGoalieOwn):

                # Can only see our own goal: Use goalie to make decision
                if self.dangerousBallCount > 5:
                    if DEBUG_KICK_DECISION:
                        print "Doing a goalie based clearing kick."
                    return self.goalieBasedKick()

                # Saw two posts: use them to triangulate.
                if self.nearRightPostBearing != 0 and self.nearLeftPostBearing != 0:
                    if DEBUG_KICK_DECISION:
                        print "Saw two own posts. Triangulating and clearing."
                    return self.triangulateClearKick()

                # Saw one post: oh god, what now?
                # Default to no-info orbit.
                """
                if self.nearRightPostBearing != 0:
                    pass
                else:
                    pass
                """
                kick = kicks.ORBIT_KICK_POSITION
                kick.h = 45
                return kick
            else:
                rightPostBearing = self.nearRightPostBearing
                leftPostBearing = self.nearLeftPostBearing
        else:
            # No information at all? Orbit arbitrarily and try again.
            kick = kicks.ORBIT_KICK_POSITION
            kick.h = 45
            return kick

        if DEBUG_KICK_DECISION:
            print "farRightPostBearing: ", self.farRightPostBearing
            print "farLeftPostBearing: ", self.farLeftPostBearing
            print "nearRightPostBearing: ", self.nearRightPostBearing
            print "nearLeftPostBearing: ", self.nearLeftPostBearing
            print "rightPostBearing: ", rightPostBearing
            print "leftPostBearing:  ", leftPostBearing

        if rightPostBearing == 0 and leftPostBearing == 0:
            # Can't see any posts: orbit.
            # Note: this case should already be covered above,
            #  but is repeated for safety.
            kick = kicks.ORBIT_KICK_POSITION
            kick.h = 45
            return kick
        elif rightPostBearing != 0 and leftPostBearing != 0:
            # Can see both posts: shoot between them.
            leftScorePoint = rightPostBearing - 10
            rightScorePoint = leftPostBearing + 10

            if leftScorePoint < rightScorePoint:
                # less than 20 degrees of goal available to shoot in.
                leftScorePoint = ((rightPostBearing + leftPostBearing) / 2) + 5
                rightScorePoint = leftScorePoint - 10
        elif rightPostBearing != 0:
            # Can only see the right post.
            leftScorePoint = rightPostBearing - 15
            rightScorePoint = leftScorePoint - 10
        else:
            # Can only see the left post.
            rightScorePoint = leftPostBearing + 15
            leftScorePoint = rightScorePoint + 10

        if DEBUG_KICK_DECISION:
            print "rightScorePoint: ", rightScorePoint
            print "leftScorePoint:  ", leftScorePoint

        # If any kick is currently valid, choose that kick.
        # Note: this ignores the aimCenter distinction.
        if leftScorePoint > 0 and rightScorePoint < 0:
            kick = self.chooseQuickFrontKick()
        elif leftScorePoint > 70 and rightScorePoint < 70:
            kick = kicks.RIGHT_SIDE_KICK
        elif leftScorePoint > -70 and rightScorePoint < -70:
            kick = kicks.LEFT_SIDE_KICK
        elif leftScorePoint < -90 and rightScorePoint > 90:
            kick = self.chooseBackKick()

        # Did we pick a kick yet?
        if kick is not None:
            kick.h = 0  # Straight is fine- don't orbit.
            return kick

        # Choose whichever kick is closest to being between the score points.
        # Note: no kick bearing is between the posts, so they are all
        #   to the right of the rightScorePoint or left of leftScorePoint.
        # If not aimCenter, pick whichever kick is closest to either score point.
        avgScorePoint = int((rightScorePoint + leftScorePoint) * .5)

        if DEBUG_KICK_DECISION:
            print "Didn't choose a 0 heading kick.\navgScorePoint: ", avgScorePoint

        if rightScorePoint > 70:
            # Quadrant 2
            if (180 - leftScorePoint) - (rightScorePoint - 70) < 0:
                #Closer to the leftScorePoint
                kick = self.chooseBackKick()
                if self.aimCenter:
                    kick.h = 180 - avgScorePoint
                else:
                    kick.h = 180 - leftScorePoint
            else:
                kick = kicks.RIGHT_SIDE_KICK
                if self.aimCenter:
                    kick.h = 70 - avgScorePoint
                else:
                    kick.h = 70 - rightScorePoint
        elif rightScorePoint > 0:
            # Quadrant 1
            if (70 - leftScorePoint) - (rightScorePoint - 0) < 0:
                kick = kicks.RIGHT_SIDE_KICK
                if self.aimCenter:
                    kick.h = 70 - avgScorePoint
                else:
                    kick.h = 70 - leftScorePoint
            else:
                kick = self.chooseQuickFrontKick()
                if self.aimCenter:
                    kick.h = 0 - avgScorePoint
                else:
                    kick.h = 0 - rightScorePoint
        elif rightScorePoint > -70:
            # Quadrant 4
            if (0 - leftScorePoint) - (rightScorePoint + 70) < 0:
                kick = self.chooseQuickFrontKick()
                if self.aimCenter:
                    kick.h = 0 - avgScorePoint
                else:
                    kick.h = 0 - leftScorePoint
            else:
                kick = kicks.LEFT_SIDE_KICK
                if self.aimCenter:
                    kick.h = -70 - avgScorePoint
                else:
                    kick.h = -70 - rightScorePoint
        else:
            # Quadrant 3
            if (-70 - leftScorePoint) - (rightScorePoint + 180) < 0:
                kick = kicks.LEFT_SIDE_KICK
                if self.aimCenter:
                    kick.h = -70 - avgScorePoint
                else:
                    kick.h = -70 - leftScorePoint
            else:
                kick = self.chooseBackKick()
                if self.aimCenter:
                    kick.h = -180 - avgScorePoint
                else:
                    kick.h = -180 - rightScorePoint

        if kick is not None:
            return kick

        if DEBUG_KICK_DECISION:
            print "Somehow got to end without a kick..."

        # If all else fails, orbit and re-decide.
        # Note: this case should already be covered above,
        #  but is repeated for safety.
        kick = kicks.ORBIT_KICK_POSITION
        kick.h = 45
        return kick
Esempio n. 11
0
        print(type(data))
        print(data.keys())

        sd_objects = []
        for i in range(len(data['results'])):
            if (isInsidePolygon(
                {
                    'lat': float(data['results'][i]['latitude']),
                    'lng': float(data['results'][i]['longitude'])
                }, dublin_coordinates) == True):
                # if np.sqrt((53.343792 - float(data['results'][i]['latitude']))**2 + (-6.254572 - float(data['results'][i]['longitude']))**2) < 0.08:
                sd_objects.append(
                    oo.Stop(
                        data['results'][i]['stopid'],
                        data['results'][i]['shortname'],
                        oo.Location(float(data['results'][i]['latitude']),
                                    float(data['results'][i]['longitude'])),
                        True))

        feature_collection = []

        for i in range(len(sd_objects)):
            point = Point(
                (sd_objects[i].location.lng, sd_objects[i].location.lat))
            feature = Feature(geometry=point,
                              id=sd_objects[i].ID,
                              properties=dict(name=sd_objects[i].name,
                                              isOpen=sd_objects[i].isOpen))
            feature_collection.append(feature)

        geojson_file = FeatureCollection(feature_collection)