Example #1
0
 def haveClearShotOfTarget(self,
                           target):
     owner = self.owner
     fleet = owner.fleet
     turretPosition = self.getPosition()
     targetPosition = target.getPosition()
     turretToTarget = calculate.subtractPoints(targetPosition,
                                               turretPosition)
     distanceSquaredToTarget = vector.getMagnitudeSquared(turretToTarget)
     headingToTarget = vector.normalize(turretToTarget)
     for friendlyShip in fleet.getAllShips():
         friendPosition = friendlyShip.getPosition()
         turretToFriend = calculate.subtractPoints(friendPosition,
                                                   turretPosition)
         distanceSquaredToFriend = vector.getMagnitudeSquared(turretToFriend)
         if distanceSquaredToFriend > distanceSquaredToTarget:
             continue
         
         headingToFriend = vector.normalize(turretToFriend)
         dotProductOfFriendAndTarget = calculate.dotProduct(headingToTarget,
                                                            headingToFriend)
         if calculate.withinTolerance(dotProductOfFriendAndTarget,
                                      1,
                                      self.clearShotTolerance):
             return False
     
     return True
Example #2
0
 def update(self,
            timeElapsed):
     world = self.world
     currentPosition = self.position
     shipHit = self.hit()
     if shipHit:
         shipHit.hitBy(self)
         world.removeShot(self)
         self.active = False
         return
     
     
     
     originToCurrent = calculate.subtractPoints(currentPosition,
                                                self.startingPosition)
     distanceSquaredFromOrigin = vector.getMagnitudeSquared(originToCurrent)
     
     
     
     if distanceSquaredFromOrigin > self.maxDistanceSquared:
         world.removeShot(self)
         return
     
     (x, y) = calculate.addPointAndVector(currentPosition,
                                          self.velocity)
     self.position = (x,
                      y)
Example #3
0
 def acquireTemporaryTarget(self):
     '''Collect any targets that are in range.  Then, collect from those
        the targets to which we have a clear shot.  Find the one that is closest.
        That is the temporary target.'''
     turretPosition = self.getPosition()
     owner = self.owner
     fleet = owner.fleet
     
     enemiesInRange = []
     for enemyShip in fleet.getAllEnemyShips():
         if not self.targetIsInRange(enemyShip):
             continue
         if not self.haveClearShotOfTarget(enemyShip):
             continue
         
         enemiesInRange.append(enemyShip)
             
     closestShip = None
     closestDistanceSquared = None
     
     for enemyShip in enemiesInRange:
         enemyPosition = enemyShip.getPosition()
         turretToEnemy = calculate.subtractPoints(enemyPosition,
                                                  turretPosition)
         distanceSquaredToEnemy = vector.getMagnitudeSquared(turretToEnemy)
         if closestShip is None or distanceSquaredToEnemy < closestDistanceSquared:
             closestShip = enemyShip
             closestDistanceSquared = distanceSquaredToEnemy
     
     if closestShip:
         self.setTarget(closestShip)
         return closestShip
     else:
         return None
Example #4
0
def plotLocalRouteAroundTarget(owner,
                               target):
    steeringController = owner.steeringController
    ownerPosition = owner.getPosition()
    targetPosition = target.getPosition()
    ownerToTarget = calculate.subtractPoints(targetPosition,
                                             ownerPosition)
    distanceToTarget = vector.getMagnitude(ownerToTarget)
    optimalPathDistance = min(distanceToTarget,
                              owner.getMaximumFiringRange())
    
    #These are the eight "compass" directions, projected
    #in the target's local space
    vectors = ((0, 1),
               (1, 1),
               (1, 0),
               (-1, 1),
               (-1, 0),
               (-1, -1),
               (0, -1),
               (1, -1))
    
    #Now scale the directions so that they have my magnitude.
    #We can treat these vectors as points in an octagonal
    #path around the target - a path scaled to an optimal distance.
    pathPoints = [calculate.multiplyVectorAndScalar(normalizedVector,
                                                    optimalPathDistance)
                  for normalizedVector in vectors]
    
    #Find the point in the path that is closest to my position.
    
    ownerLocalPosition = convert.pointToLocalSpace(ownerPosition,
                                                   targetPosition,
                                                   target.getDirection())
    
    closestDistanceSquared = None
    closestIndex = None
    for index in range(len(pathPoints)):
        pathPoint = pathPoints[index]
        ownerToPathPoint = calculate.subtractPoints(pathPoint,
                                                    ownerLocalPosition)
        distanceSquaredToPathPoint =\
            vector.getMagnitudeSquared(ownerToPathPoint)
        if (closestDistanceSquared is None or 
            distanceSquaredToPathPoint < closestDistanceSquared):
            closestIndex = index
            closestDistanceSquared = distanceSquaredToPathPoint
            
    
    #Now "shift" the path so that my closest point is the first in the list.
    
    path = pathPoints[closestIndex:] + pathPoints[:closestIndex]
        
    
    #Plot a course to visit the path.  If at any point I find a clear shot to the target,
    #I will dive at it.
    
    steeringController.plotPath(path,
                                closed=True)
Example #5
0
def circleWithCircle(circle1, circle2):
    position_1, radius_1 = circle1
    position_2, radius_2 = circle2
    circle1_to_circle2 = calculate.subtractPoints(position_2, position_1)
    bounding_distance_squared = (radius_1 ** 2) + (radius_2 ** 2)
    actual_distance_squared = vector.getMagnitudeSquared(
            circle1_to_circle2)
    return actual_distance_squared < bounding_distance_squared
Example #6
0
 def execute(cls,
             owner):
     motherShip = owner.fleet.motherShip
     ownerPosition = owner.getPosition()
     targetPosition = motherShip.getPosition()
     ownerToTarget = calculate.subtractPoints(targetPosition,
                                              ownerPosition)
     distanceSquaredToTarget = vector.getMagnitudeSquared(ownerToTarget)
     if distanceSquaredToTarget <= motherShip.hangarEntryThresholdSquared:
         motherShip.pickup(owner)
Example #7
0
def withinFiringRange(owner,
                      target):
    ownerPosition = owner.getPosition()
    targetPosition = target.getPosition()
    ownerToTarget = calculate.subtractPoints(targetPosition,
                                             ownerPosition)
    distanceSquaredToTarget = vector.getMagnitudeSquared(ownerToTarget)
    if distanceSquaredToTarget < owner.getMaximumFiringRangeSquared():
        return True
    else:
        return False
Example #8
0
def pastBreakThreshold(owner,
                       target):
    targetPosition = target.getPosition()
    ownerPosition = owner.getPosition()
    ownerToTarget = calculate.subtractPoints(targetPosition,
                                             ownerPosition)
    distanceSquaredToTarget = vector.getMagnitudeSquared(ownerToTarget)
    if distanceSquaredToTarget > breakThresholdSquared:
        return True
    else:
        return False
Example #9
0
def findClosestEntity(owner, listOfEntities):
    ownerPosition = owner.getPosition()
    closestEntity = None
    closestDistanceSquared = None
    for entity in listOfEntities:
        entityPosition = entity.getPosition()
        entityToOwner = calculate.subtractPoints(entityPosition,
                                                 ownerPosition)
        distanceSquaredToOwner = vector.getMagnitudeSquared(entityToOwner)
        if closestEntity is None or distanceSquaredToOwner < closestDistanceSquared:
            closestEntity = entity
            closestDistanceSquared = distanceSquaredToOwner
            
    return closestEntity
Example #10
0
 def targetIsInRange(self,
                     target):
     gunPosition = self.getPosition()
     targetPosition = target.getPosition()
     gunToTarget = calculate.subtractPoints(targetPosition,
                                            gunPosition)
     distanceSquaredToTarget = vector.getMagnitudeSquared(gunToTarget)
     headingToTarget = vector.normalize(gunToTarget)
     headingDotProduct = calculate.dotProduct(self.heading,
                                              headingToTarget) 
     if (distanceSquaredToTarget < self.firingRangeSquared and
         headingDotProduct > 0):
         return True
     
     return False
Example #11
0
def avoidwalls(agent, 
               walls):
    if not walls:
        return (0, 0)
    agentPosition = agent.getPosition()
    feelers = createFeelers(agent)
    
    closestWall = None
    closestIntersection = None
    distanceSquaredToClosestIntersection = None
    closestFeeler = None

    for feeler in feelers:
        for wall in walls:
            intersectPoint = intersect.lineWithLine(feeler,
                                                    wall)
            if intersectPoint is None:
                continue
            agentToIntersection = calculate.subtractPoints(intersectPoint,
                                                           agentPosition)
            distanceSquaredToIntersection = vector.getMagnitudeSquared(agentToIntersection)
        
            if closestIntersection is None or distanceSquaredToIntersection < distanceSquaredToClosestIntersection:
                distanceSquaredToClosestIntersection = distanceSquaredToIntersection
                closestWall = wall
                closestIntersection = intersectPoint
                closestFeeler = feeler
                
    if closestWall is None:
        return (0, 0)            
    
    (closestFeelerOrigin,
     closestFeelerEndpoint) = closestFeeler
    (wallOrigin,
     wallEndpoint) = closestWall
    wallVector = calculate.subtractPoints(wallEndpoint,
                                          wallOrigin)
    intersectionToFeelerEndpoint = calculate.subtractPoints(closestFeelerEndpoint,
                                                            closestIntersection)
    overshootLength = vector.getMagnitude(intersectionToFeelerEndpoint)
    normalizedWallVector = vector.normalize(wallVector)
    wallNormal = vector.getRightPerpendicular(normalizedWallVector)
    steeringForce = calculate.multiplyVectorAndScalar(wallNormal,
                                                      overshootLength)
    
    return steeringForce
Example #12
0
 def acquireTarget(cls,
                   owner):
     fleet = owner.fleet
     ownerPosition = owner.getPosition()
     enemyFleet = fleet.getEnemyFleet()
     closestShip = None
     closestDistanceSquared = None
     
     for enemy in enemyFleet.getAllFighters():
         enemyPosition = enemy.getPosition()
         ownerToEnemy = calculate.subtractPoints(enemyPosition,
                                                 ownerPosition)
         distanceSquaredToEnemy = vector.getMagnitudeSquared(ownerToEnemy)
         if closestShip is None or distanceSquaredToEnemy < closestDistanceSquared:
             closestShip = enemy
             closestDistanceSquared = distanceSquaredToEnemy
             
     return closestShip
Example #13
0
 def acquireTarget(cls,
                   owner):
     fleet = owner.fleet
     motherShip = fleet.getMotherShip()
     motherShipPosition = motherShip.getPosition()
     
     enemyFleet = fleet.getEnemyFleet()
     closestShip = None
     closestDistanceSquared = None
     
     enemyBombers = enemyFleet.getAllFighters(shipType='bomber')
     for enemy in enemyBombers:
         enemyPosition = enemy.getPosition()
         motherShipToEnemy = calculate.subtractPoints(enemyPosition,
                                                      motherShipPosition)
         distanceSquaredToEnemy = vector.getMagnitudeSquared(motherShipToEnemy)
         if closestShip is None or distanceSquaredToEnemy < closestDistanceSquared:
             closestShip = enemy
             closestDistanceSquared = distanceSquaredToEnemy
     
     return closestShip
Example #14
0
def flee(agent,
         targetPosition,
         evadeDistanceSquared=None):
    steeringController = agent.getSteeringController()
    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()
    agentVelocity = agent.getVelocity()
    
    agentToTarget = calculate.subtractPoints(agentPosition,
                                             targetPosition)
    distanceSquaredToTarget = vector.getMagnitudeSquared(agentToTarget)

    if (evadeDistanceSquared is not None and 
        distanceSquaredToTarget > evadeDistanceSquared):
        return (0, 0)
    
    desiredVelocity = vector.setMagnitude(agentToTarget,
                                          agentMaxSpeed)
    
    return calculate.subtractVectors(desiredVelocity,
                                     agentVelocity)
Example #15
0
 def intersectsPoint(self,
                     point):
     shipPosition = self.getPosition()
     shipToPoint = calculate.subtractPoints(point,
                                            shipPosition)
     distanceSquaredToPoint = vector.getMagnitudeSquared(shipToPoint)
     if (distanceSquaredToPoint > self.lengthSquared or
         distanceSquaredToPoint > self.widthSquared):
         return None
     
     directionRadians = self.getDirection()
     localPoint = convert.pointToLocalSpace(point,
                                            shipPosition,
                                            directionRadians)
     (topy, rightx, bottomy, leftx) = self.boundingBox
     x, y = localPoint
     if (x > leftx and
         x < rightx and
         y < topy and
         y > bottomy):
         return True
Example #16
0
def lineSegmentWithCircle(line_segment, circle):
    if not lineWithCircle(line_segment, circle):
        return False

    circle_point, circle_radius = circle
    line_seg_point1, line_seg_point2 = line_segment

    # Line to circle vector (pick an endpoint)
    a = calculate.subtractPoints(circle_point, line_seg_point1)
    # Line segment vector
    b = calculate.subtractPoints(line_seg_point2, line_seg_point1)
    
    # a is the vector from line_point1 and the circle's center-point
    # b is the vector from line_point1 to line_point2
    # If a and b are pointing in opposing directions, then there is 
    # no intersection
    if calculate.dotProduct(a, b) <= 0:
        return False

    # Project a onto b.  If this projection is longer than the 
    # length of the line segment, then there is no intersection
    a_onto_b = calculate.dotProduct(a, vector.normalize(b))
    return math.pow(a_onto_b, 2) <= vector.getMagnitudeSquared(b)
Example #17
0
def pointInCircle(point, circle):
    circle_point, circle_radius = circle
    point_to_circle = calculate.subtractPoints(circle_point, point)
    return vector.getMagnitudeSquared(point_to_circle) <= math.pow(circle_radius, 2)