コード例 #1
0
ファイル: pursueoffset.py プロジェクト: krieghan/game_common
def pursueoffset(agent, 
                 targetAgent, 
                 targetToOffset):
    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()
    targetPosition = targetAgent.getPosition()
    targetDirection = targetAgent.getDirection()
    targetSpeed = targetAgent.getSpeed()
    targetVelocity = targetAgent.getVelocity()
    worldTargetToOffset = convert.vectorToWorldSpace(targetToOffset,
                                                     targetPosition,
                                                     targetDirection)
    offsetPosition = calculate.addPointAndVector(targetPosition,
                                                 worldTargetToOffset)
    
    agentToOffset = calculate.subtractPoints(offsetPosition,
                                             agentPosition)
    distanceToOffset = vector.getMagnitude(agentToOffset)
    
    if targetSpeed == 0:
        lookAheadTime = 0
    else:
        lookAheadTime = distanceToOffset / (agentMaxSpeed + targetSpeed)
    
    targetToPredictedPosition = calculate.multiplyVectorAndScalar(targetVelocity,
                                                                  lookAheadTime)
    predictedOffsetPosition = calculate.addPointAndVector(offsetPosition,
                                                          targetToPredictedPosition)
    
    return arrive.arrive(agent,
                         predictedOffsetPosition,
                         .9)
コード例 #2
0
ファイル: avoidwalls.py プロジェクト: krieghan/game_common
def createFeelers(agent):
    agentPosition = agent.getPosition()
    agentDirection = agent.getDirectionRadians()
    agentLength = agent.getLength()
    feelerLength = agentLength * 1.5
    
    leftVector = vector.createVector(magnitude=feelerLength,
                                     direction=agentDirection + RADIANS45)
    forwardVector = vector.createVector(magnitude=feelerLength,
                                        direction=agentDirection)
    rightVector = vector.createVector(magnitude=feelerLength,
                                      direction=agentDirection - RADIANS45)
    
    leftPoint = calculate.addPointAndVector(agentPosition,
                                            leftVector)
    forwardPoint = calculate.addPointAndVector(agentPosition,
                                               forwardVector)
    rightPoint = calculate.addPointAndVector(agentPosition,
                                             rightVector)
    
    leftLine = (agentPosition, leftPoint)
    forwardLine = (agentPosition, forwardPoint)
    rightLine = (agentPosition, rightPoint)
    
    return (leftLine, forwardLine, rightLine)
コード例 #3
0
ファイル: intersect.py プロジェクト: krieghan/game_common
def getLineSegmentIntersection(line1, line2):
    if linesAreParallel(line1, line2):
        return None

    A, B = line1
    C, D = line2
    bVector = calculate.subtractPoints(B, A)
    dVector = calculate.subtractPoints(D, C)
    cVector = calculate.subtractPoints(C, A)
    
    bperp = vector.getRightPerpendicular(bVector)
    dperp = vector.getRightPerpendicular(dVector)
    
    dperpDotB = calculate.dotProduct(dperp,
                                     bVector)
    dperpDotC = calculate.dotProduct(dperp,
                                     cVector)
    bperpDotC = calculate.dotProduct(bperp,
                                     cVector)
    
    distanceAlongB = float(dperpDotC) / float(dperpDotB)
    distanceAlongD = float(bperpDotC) / float(dperpDotB)
    
    if (distanceAlongB > 0 and distanceAlongB < 1 and
        distanceAlongD > 0 and distanceAlongD < 1):
        
        AToIntersectionPoint = calculate.multiplyVectorAndScalar(bVector,
                                                                 distanceAlongB)
        intersectionPoint = calculate.addPointAndVector(A,
                                                        AToIntersectionPoint)
        return intersectionPoint
    else:
        return None
コード例 #4
0
ファイル: pursue.py プロジェクト: krieghan/game_common
def pursue(agent,
           target):
    agentHeading = agent.getHeading()
    targetHeading = target.getHeading()
    targetPosition = target.getPosition()
    relativeHeading = calculate.dotProduct(agentHeading,
                                           targetHeading)
    
    #If the target is heading at me, then just Seek
    if relativeHeading < -.95:
        return seek.seek(agent,
                         targetPosition)

    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()

    
    targetSpeed = target.getSpeed()
    targetVelocity = target.getVelocity()
    agentToTarget = calculate.subtractPoints(targetPosition,
                                             agentPosition)
    distanceToTarget = vector.getMagnitude(agentToTarget)
    
    lookAheadTime = distanceToTarget / (agentMaxSpeed + targetSpeed)
    
    lookAheadVector = calculate.multiplyVectorAndScalar(targetVelocity,
                                                        lookAheadTime)
    
    lookAheadPosition = calculate.addPointAndVector(targetPosition,
                                                    lookAheadVector)
    
    return seek.seek(agent,
                     lookAheadPosition)
コード例 #5
0
 def test_originPlusVector_pointBecomesEndpointOfVector(self):
     point = (0, 0)
     vector = (1, 1)
     resultPoint = calculate.addPointAndVector(point=point,
                                          vector=vector)
     self.assertEquals((1, 1),
                       resultPoint)
コード例 #6
0
ファイル: agents.py プロジェクト: krieghan/escape
 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)
コード例 #7
0
 def test_pointPlusVector(self):
     point = (1, 1)
     vector = (2, 2)
     resultPoint = calculate.addPointAndVector(point=point,
                                          vector=vector)
     self.assertEquals((3, 3),
                       resultPoint)
コード例 #8
0
 def test_originPlusZero_stillOrigin(self):
     point = (0, 0)
     vector = (0, 0)
     resultPoint = calculate.addPointAndVector(point=point,
                                          vector=vector)
     self.assertEquals((0, 0),
                       resultPoint)
コード例 #9
0
    def update(self, timeElapsed):
        self.timeSinceLastShot += timeElapsed
        if self.timeSinceLastShot >= self.shotCooldown:
            self.timeSinceLastShot = 0
            tile = self.get_current_tile()
            target_tile = self.target.get_current_tile()
            x, y = tile.get_grid_position()
            target_x, target_y = target_tile.get_grid_position()
            if target_x == x or target_y == y:
                if target_x == x:
                    if y < target_y:
                        direction = (0, 1)
                    else:
                        direction = (0, -1)
                    agent_to_origination_point = vector.setMagnitude(
                        direction, self.height * .5)
                if target_y == y:
                    if x < target_x:
                        direction = (1, 0)
                    else:
                        direction = (-1, 0)
                    agent_to_origination_point = vector.setMagnitude(
                        direction, self.width * .5)

                origination_point = calculate.addPointAndVector(
                    self.position, agent_to_origination_point)

                shot = Shot(world=self.world,
                            direction=direction,
                            tile=tile,
                            owner=self,
                            position=origination_point)
                self.world.add_canvas_element(shot)

        super(Enemy, self).update(self)
コード例 #10
0
ファイル: hide.py プロジェクト: krieghan/game_common
def hide(agent,
         attacker, 
         obstacles):
    distanceFromObstacleBoundary = 30
    closestDistance = None
    closestHidingPlace = None
    attackerPosition = attacker.getPosition()
    agentPosition = agent.getPosition()
    for obstacle in obstacles:
        obstaclePosition = obstacle.getPosition()
        hidingPlaceDistanceToObstacle = distanceFromObstacleBoundary + obstacle.getRadius()
        attackerToObstacle = calculate.subtractPoints(obstaclePosition,
                                                      attackerPosition)
        attackerDistanceToObstacle = vector.getMagnitude(attackerToObstacle)
        attackerDistanceToHidingPlace =  hidingPlaceDistanceToObstacle + attackerDistanceToObstacle
        attackerToHidingPlace = vector.setMagnitude(attackerToObstacle,
                                                    attackerDistanceToHidingPlace)

        hidingPlace = calculate.addPointAndVector(attackerPosition,
                                                  attackerToHidingPlace)
        
        agentToHidingPlace = calculate.subtractPoints(hidingPlace,
                                                      agentPosition)
        distanceToHidingPlace = vector.getMagnitude(agentToHidingPlace)
        
        if closestDistance is None or distanceToHidingPlace < closestDistance:
            closestDistance = distanceToHidingPlace
            closestHidingPlace = hidingPlace
        
    if closestHidingPlace is None:
        return evade.evade(agent,
                           attacker)
        
    return arrive.arrive(agent,
                         closestHidingPlace)
コード例 #11
0
ファイル: interpose.py プロジェクト: krieghan/game_common
def interpose(agent,
              enemy,
              charge):
    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()
    enemyPosition = enemy.getPosition()
    enemyVelocity = enemy.getVelocity()
    chargePosition = charge.getPosition()
    chargeVelocity = charge.getVelocity()
    enemyToCharge = calculate.subtractPoints(chargePosition,
                                             enemyPosition)
    midVector = calculate.multiplyVectorAndScalar(enemyToCharge,
                                                  .5)
    midPoint = calculate.addPointAndVector(enemyPosition,
                                           midVector)
    
    agentToMidPoint = calculate.subtractPoints(midPoint,
                                               agentPosition)
    distanceToMidPoint = vector.getMagnitude(agentToMidPoint)
    timeToMidPoint = distanceToMidPoint / agentMaxSpeed
    
    enemyToFuturePosition = calculate.multiplyVectorAndScalar(enemyVelocity,
                                                              timeToMidPoint)
    enemyFuturePosition = calculate.addPointAndVector(enemyPosition,
                                                      enemyToFuturePosition)
    
    chargeToFuturePosition = calculate.multiplyVectorAndScalar(chargeVelocity,
                                                               timeToMidPoint)
    chargeFuturePosition = calculate.addPointAndVector(chargePosition,
                                                       chargeToFuturePosition)
        
    
    
    enemyFutureToChargeFuture = calculate.subtractPoints(chargeFuturePosition,
                                                         enemyFuturePosition)
    futureMidVector = calculate.multiplyVectorAndScalar(enemyFutureToChargeFuture,
                                                        .5)
    futureMidPoint = calculate.addPointAndVector(enemyFuturePosition,
                                                 futureMidVector)
    
    return arrive.arrive(agent,
                         futureMidPoint)
コード例 #12
0
ファイル: agents.py プロジェクト: krieghan/escape
 def launch(self):
     current_time = self.world.current_time
     if not self.flightGroups:
         return
     
     if current_time < self.timeOfNextLaunch:
         return
     else:
         self.timeOfNextLaunch = current_time + self.msBetweenLaunches
     
     desiredNumberOfFriendlyBombers = 1
     desiredNumberOfFriendlyFighters = 1
     
     fleet = self.fleet
     enemyFleet = fleet.getEnemyFleet()
     friendlyFlightGroups = fleet.flightGroups
     enemyFlightGroups = enemyFleet.flightGroups
     onboardFlightGroups = self.flightGroups
     
     onboardInterceptorGroups = onboardFlightGroups['interceptor']
     onboardBomberGroups = onboardFlightGroups['bomber']
     onboardFighterGroups = onboardFlightGroups['fighter']
     friendlyInterceptorGroups = friendlyFlightGroups['interceptor']
     friendlyBomberGroups = friendlyFlightGroups['bomber']
     friendlyFighterGroups = friendlyFlightGroups['fighter']
     enemyBomberGroups = enemyFlightGroups['bomber']
     
     if (len(enemyBomberGroups) > len(friendlyInterceptorGroups) and
         len(onboardInterceptorGroups) > 0):
         groupToLaunch = onboardInterceptorGroups[0]
     elif (len(friendlyBomberGroups) < desiredNumberOfFriendlyBombers and
           len(onboardBomberGroups) > 0):
         groupToLaunch = onboardBomberGroups[0]
     elif (len(friendlyFighterGroups) < desiredNumberOfFriendlyFighters and
           len(onboardFighterGroups) > 0):
         groupToLaunch = onboardFighterGroups[0]
     else:
         return
     
     carrierPosition = self.getPosition()
     worldLaunchOffset = convert.vectorToWorldSpace(self.launchOffset,
                                                    carrierPosition,
                                                    self.getDirection())
     newPosition = calculate.addPointAndVector(carrierPosition,
                                               worldLaunchOffset)
     groupToLaunch.setPosition(newPosition)
     groupToLaunch.setVelocity((1, 0))
     self.removeFlightGroup(groupToLaunch)
     fleet.addFlightGroups([groupToLaunch])
     groupToLaunch.startStateMachine()
コード例 #13
0
ファイル: agents.py プロジェクト: krieghan/escape
 def predictFuturePosition(self,
                           source,
                           target,
                           shotSpeed):
     sourcePosition = source.getPosition()
     targetPosition = target.getPosition()
     targetVelocity = target.getVelocity()
     targetSpeed = target.getSpeed()
     sourceToTarget = calculate.subtractPoints(targetPosition,
                                               sourcePosition)
     manhattanDistanceToTarget = vector.getManhattanMagnitude(sourceToTarget)
     lookAheadTime = manhattanDistanceToTarget / (shotSpeed + targetSpeed)
 
     lookAheadVector = calculate.multiplyVectorAndScalar(targetVelocity,
                                                         lookAheadTime)
 
     lookAheadPosition = calculate.addPointAndVector(targetPosition,
                                                     lookAheadVector)
     return lookAheadPosition
コード例 #14
0
ファイル: agents.py プロジェクト: krieghan/escape
    def update(self,
               timeElapsed):
        current_time = self.world.current_time
        if self.timeOfNormalColor and current_time < self.timeOfNormalColor:
            self.color = (random.random(), random.random(), random.random())
        elif self.timeOfNormalColor:
            self.timeOfNormalColor = None
            self.color = self.normalColor

        world = self.world
        maxspeed = self.maxSpeed
        throttlespeed = self.throttleSpeed
        maxforce = self.maxForce
        minDetectionLength = self.minDetectionLength
        for stateMachine in self.stateMachines:
            stateMachine.update()
        for turret in self.turrets:
            turret.update(timeElapsed)
            
        self.launch()
        
        force = self.steeringController.calculate()
        force = vector.truncate(vectorTuple=force,
                                cap=maxforce)
        
        acceleration = calculate.multiplyVectorAndScalar(vector=force,
                                                         scalar=timeElapsed / (self.mass * 1000.0))
        
        velocity = calculate.addVectors(self.velocity,
                                        acceleration)

        velocity = vector.truncate(velocity,
                                   throttlespeed)
        self.velocity = velocity                

        speed = vector.getMagnitude(velocity)
       
        (x, y) = calculate.addPointAndVector(self.position,
                                             velocity)
        self.position = (x, y)
                
        self.obstacleDetectionDimensions[0] =\
            minDetectionLength + (speed / maxspeed) * minDetectionLength 
コード例 #15
0
ファイル: evade.py プロジェクト: krieghan/game_common
def evade(agent, 
          target,
          evadeDistanceSquared=None):
    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()
    targetPosition = target.getPosition()
    targetSpeed = target.getSpeed()
    targetVelocity = target.getVelocity()
    targetToAgent = calculate.subtractPoints(agentPosition,
                                             targetPosition)
    distanceToTarget = vector.getMagnitude(targetToAgent)
    lookAheadTime = distanceToTarget / (agentMaxSpeed + targetSpeed)
    lookAheadVector = calculate.multiplyVectorAndScalar(targetVelocity,
                                                        lookAheadTime)
    lookAheadPosition = calculate.addPointAndVector(targetPosition,
                                                    lookAheadVector)
    return flee.flee(agent,
                     lookAheadPosition,
                     evadeDistanceSquared)
コード例 #16
0
    def update(self, timeElapsed):
        for machine in self.get_state_machines():
            machine.update()

        self.position = calculate.addPointAndVector(self.position,
                                                    self.velocity)