Exemple #1
0
def get_readings():
    """ Returns the  main  sensor readings """
    cl_enemy = ai.closestShipId()
    data = {}
    data["X"] = ai.selfX()
    data["Y"] = ai.selfY()
    data["VelX"] = ai.selfVelX()
    data["VelY"] = ai.selfVelY()
    data["RadarX"] = ai.selfRadarX()
    data["RadarY"] = ai.selfRadarY()
    data["Orientation"] = ai.selfHeadingDeg()
    data["ClosestRadarX"] = ai.closestRadarX()
    data["ClosestRadarY"] = ai.closestRadarY()
    data["ClosestItemX"] = ai.closestItemX()
    data["ClosestItemY"] = ai.closestItemY()
    data["EnemySpeed"] = ai.enemySpeedId(cl_enemy)
    data["EnemyX"] = ai.screenEnemyXId(cl_enemy)
    data["EnemyY"] = ai.screenEnemyYId(cl_enemy)
    data["EnemyHeading"] = ai.enemyHeadingDegId(cl_enemy)
    data["EnemyShield"] = ai.enemyShieldId(cl_enemy)
    data["EnemyDistance"] = ai.enemyDistanceId(cl_enemy)
    data["ShotAlert"] = ai.shotAlert(0)
    data["ShotDist"] = ai.shotDist(0)
    data["ShotVel"] = ai.shotVel(0)
    data["ShotVelDir"] = ai.shotVelDir(0)
    return data
Exemple #2
0
def AI_loop():
  #Release keys
  ai.thrust(0)
  ai.turnLeft(0)
  ai.turnRight(0)
  #Set variables
  heading = int(ai.selfHeadingDeg())
  tracking = int(ai.selfTrackingDeg())
  frontWall = ai.wallFeeler(500,heading)
  left45Wall = ai.wallFeeler(500,heading+45)
  right45Wall = ai.wallFeeler(500,heading-45)
  left90Wall = ai.wallFeeler(500,heading+90)
  right90Wall = ai.wallFeeler(500,heading-90)
  left135Wall = ai.wallFeeler(500,heading+135)
  right135Wall = ai.wallFeeler(500,heading-135)
  backWall = ai.wallFeeler(500,heading-180) 
  trackWall = ai.wallFeeler(500,tracking)
  
  result_list = []
  risk_list = []
  for i in range(8):
    Degree = tracking+(45*i)
    Speed = ai.selfSpeed()
    Distance = ai.wallFeeler(10000,tracking+(45*i))
    result = Closing_Rate(Degree, tracking, Speed, Distance)
    result_list.append(result)
  	
    ### Fuzzy membership ###
    closing_rate, distance = Closing_Rate(Degree, tracking, Speed, Distance)
    low, medium, fast = Fuzzy_Speed(closing_rate)
    close, far = Fuzzy_Distance(distance)
    risk = Fuzzy_Risk(low, medium, fast, close, far)
    risk_list.append(risk)
  
  ## Get the direction in deg that is most risky for the robot ##
  max_risk = max(risk_list)
  track_risk = (tracking + (risk_list.index(max_risk)*45) % 360)
  min_risk = min(risk_list)
  

  #######   Shooting Ennemies  ########
  
  ##Find the closest ennemy##
  ClosestID = ai.closestShipId()
  #print(ClosestID)
  ##Get the closest ennemy direction and speed##
  ClosestSpeed = ai.enemySpeedId(ClosestID)
  ClosestDir = ai.enemyTrackingDegId(ClosestID)
  ## Get the lockheadingdeg ##
  enemy = ai.lockClose()
  #print(enemy)
  head = ai.lockHeadingDeg()
  #print(head)
  enemyDist = ai.selfLockDist()
  #print(enemyDist, ClosestSpeed, ClosestDir, head)
  int1, int2, int3, int4, int5 = Data(enemyDist, ClosestSpeed, ClosestDir, head, heading, tracking)
  output = Out(int1, int2, int3, int4, int5)
  #print(output)
  
  if(output > 0.5):
  	addDeg = output * 20
  else: 
  	addDeg = (output -0.5) * 20 * -1
  
  ## Get the angles on both side between tracking and heading ##
  dist = (heading - track_risk) % 360
  dist2 = (360 - dist) % 360
  
  ## Production system rules based off fuzzy output ##
  if(dist <= 130 and dist >= 0 and ai.selfSpeed() > 0 and max_risk >= 75):
    ai.turnLeft(1)
    #print("turning left")
  elif(dist2 <= 130 and dist2 >= 0 and ai.selfSpeed() > 0 and max_risk >= 75):
    ai.turnRight(1)
    #print("turning right")
  elif(ai.selfSpeed() <= 10):
    ai.thrust(1)
    #print("thrust")
  elif(trackWall <= 150):
    ai.thrust(1)
    #print("thrust")
  elif(enemyDist <= 400 and heading > (head) and enemyDist != 0):
    ai.turnRight(1)
    ai.fireShot()
  elif(enemyDist <= 400 and heading < (head) and enemyDist != 0):
    ai.turnLeft(1)
    ai.fireShot()
  elif(enemyDist > 400 and heading > (head + addDeg) and enemyDist != 0):
    ai.turnRight(1)
    ai.fireShot()
  elif(enemyDist > 400 and heading < (head + addDeg) and enemyDist != 0):
    ai.turnLeft(1)
    ai.fireShot()
  else:
    #print("chilling")
    ai.thrust(0)
    ai.fireShot()
Exemple #3
0
    def AI_loop(self):

        # Release keys
        ai.thrust(0)
        ai.turnLeft(0)
        ai.turnRight(0)

        if self.quitFlag == True:
            with open('fitness.txt', 'a') as inFile:
                outString = str(self.fitness) + "\n"
                inFile.write(outString)
            ai.quitAI()

        if ai.selfAlive() == 0 or self.frames > 3000:
            self.foundPreyFlag = False
            self.parterFoundPreyFlag = False
            self.checking = False

        if self.foundPreyFlag == True and self.parterFoundPreyFlag == True:
            self.fitness = self.fitness + 2

        #-------------------- Set variables --------------------#
        heading = int(ai.selfHeadingDeg())
        tracking = int(ai.selfTrackingDeg())
        frontWall = ai.wallFeeler(500, heading)
        leftWall = ai.wallFeeler(500, heading + 45)
        rightWall = ai.wallFeeler(500, heading - 45)
        leftWallStraight = ai.wallFeeler(500, heading + 90)
        rightWallStraight = ai.wallFeeler(500, heading - 90)
        leftBack = ai.wallFeeler(500, heading + 135)
        rightBack = ai.wallFeeler(500, heading - 135)
        backWall = ai.wallFeeler(500, heading - 180)
        trackWall = ai.wallFeeler(500, tracking)
        R = (heading - 90) % 360
        L = (heading + 90) % 360
        aim = ai.aimdir(0)
        bullet = ai.shotAlert(0)
        speed = ai.selfSpeed()
        x = ai.selfX()
        y = ai.selfY()
        enemyX = -1
        enemyY = -1
        enemyTeam = -1

        if self.preyID != -1:
            enemyX = ai.screenEnemyXId(self.preyID)
            enemyY = ai.screenEnemyYId(self.preyID)
            enemyTeam = ai.enemyTeamId(self.preyID)
        else:
            enemyX = ai.screenEnemyXId(ai.closestShipId())
            enemyY = ai.screenEnemyYId(ai.closestShipId())
            enemyTeam = ai.enemyTeamId(ai.closestShipId())

        myTeam = ai.selfTeam()
        coordinate = self.grid[self.counter][1]
        message = ai.scanMsg(0)

        # Continually check messages
        if message != self.MessageBuffer[-1]:
            self.checkMessage(message)

        # Check if enemy is on screen
        # If it is: broadcast location of enemy
        if enemyX != -1 and enemyY != -1 and enemyTeam != 2:
            coordinate = (enemyX, enemyY)
            self.foundPreyFlag = True
            self.foundPrey(coordinate)
            self.fitness += 1
        elif self.foundPreyFlag == True:
            self.foundPreyFlag = False
            ai.talk("--- " + "Lost prey!")

        if self.parterFoundPreyFlag == True:
            coordinate = self.preyLocation

        # Calculate most efficient way to turn to get where we want to
        targetX = coordinate[0]
        targetY = coordinate[1]
        toTurn = self.angleToPoint(x, y, targetX, targetY, heading)
        distance = self.distance(x, targetX, y, targetY)

        if self.checking == False and self.foundPreyFlag == False:
            ai.talk("checking! " + str(coordinate))
            self.checking = True

        # If speed is too fast, turn around and thrust to negate velocity
        if speed > self.gene0:
            turning = ai.angleDiff(heading, tracking)
            if abs(turning) > self.gene1 and abs(turning) <= self.gene2:
                ai.turnLeft(0)
                ai.turnRight(0)
                if self.frames % self.gene3 == 0:
                    ai.thrust(1)
            elif turning <= self.gene4 and turning > self.gene5:
                ai.turnRight(1)
            else:
                ai.turnLeft(1)

            if self.foundPreyFlag == True and distance <= 150:
                self.caughtPrey(coordinate, distance)

        else:

            #-------------------- Go to coordinate / enemy --------------------#
            if abs(toTurn) < self.gene6 and distance > self.gene7:
                ai.turnLeft(0)
                ai.turnRight(0)
                if self.frames % self.gene8 == 0:
                    ai.thrust(1)
            elif toTurn >= self.gene9:
                ai.turnLeft(1)
            elif toTurn <= -self.gene10:
                ai.turnRight(1)

            if self.foundPreyFlag == True and distance <= 150:
                self.caughtPrey(coordinate, distance)
            elif self.foundPreyFlag == True and distance > 150:
                self.foundPrey(coordinate)
            elif distance < 150:
                self.markSpotChecked(coordinate, "me")

        #-------------------- Old turn and thrust rules --------------------#
        if speed <= self.gene14 and frontWall >= self.gene15:
            ai.thrust(1)
        elif trackWall < self.gene16:
            ai.thrust(1)
        elif backWall < self.gene17:
            ai.thrust(1)
        if (backWall < self.gene18) and (rightWallStraight < self.gene19):
            ai.turnLeft(1)
        elif backWall < self.gene20 and (leftWallStraight < self.gene21):
            ai.turnRight(1)
        elif leftWallStraight < rightWallStraight and trackWall < self.gene22:
            ai.turnRight(1)
        elif leftWallStraight > rightWallStraight and trackWall < self.gene23:
            ai.turnLeft(1)

        self.frames = self.frames + 1

        if self.caughtPreyFlag == True and self.quitFlag == False:
            ai.talk("quit!")
            self.quitFlag = True

        if ai.selfAlive() == 0 or self.frames > 1800:
            self.quitFlag = True
Exemple #4
0
def AI_loop():

    #Inserted Code
    if ai.selfAlive():
        DataMinerBD.updateInputs()
        DataMinerBD.updateOutputs()
        DataMinerBD.savePair()
        print(DataMinerBD.length())
        if DataMinerBD.length() > 10000:
            DataMinerBD.writeData()
            print("Finished")
            ai.quitAI()
    
    global lastTurn
    #Release keys
    DataMinerBD.tthrust(0)
    DataMinerBD.tturnLeft(0)
    DataMinerBD.tturnRight(0)
    ai.setTurnSpeed(45)
    #Set variables"""
    heading = int(ai.selfHeadingDeg())
    tracking = int(ai.selfTrackingDeg())
    trackWall = ai.wallFeeler(500,  tracking)
    trackLWall = ai.wallFeeler(500,  tracking+3)
    trackRWall = ai.wallFeeler(500,  tracking - 3)
    frontWall = ai.wallFeeler(500,heading)
    flWall = ai.wallFeeler(500,  heading + 10)
    frWall = ai.wallFeeler(500,  heading - 10)
    leftWall = ai.wallFeeler(500,heading+90)
    rightWall = ai.wallFeeler(500,heading-90)
    trackWall = ai.wallFeeler(500,tracking)
    backWall = ai.wallFeeler(500, heading - 180)
    backLeftWall = ai.wallFeeler(500,  heading - 185)
    backRightWall = ai.wallFeeler(500,  heading - 175)
    speed = ai.selfSpeed()
   
    closest = min(frontWall, leftWall, rightWall, backWall)
    def closestWall(x): #Find the closest Wall
        return {
            frontWall : 1, 
            leftWall : 2, 
            rightWall : 3, 
            backWall : 4, 
            flWall : 5, 
            frWall : 6, 
        }[x]
    wallNum = closestWall(closest)
    
    #Code for finding the angle to the closest ship
    target = ai.closestShipId()
    targetX,  targetY = ai.screenEnemyX(0), ai.screenEnemyY(0)
    calcDir = 0
    
    if targetX- ai.selfX() != 0:
        calcDir = (math.degrees(math.atan2((targetY - ai.selfY()), (targetX- ai.selfX()))) + 360)%360
    targetDir = calcDir
    crashWall = min(trackWall,  trackLWall,  trackRWall) #The wall we are likely to crash into if we continue on our current course
    #Rules for turning
    if crashWall > 25*speed and closest > 30 and targetX != -1:  #If we are far enough away from a predicted crash and no closer than 25 pixels to a wall, and there isn't a wall between us and the closest enemy
        #print("Aiming",  targetDir,  " Current",  heading)
        diff = (calcDir - heading)
        if ai.shotAlert(0) > -1 and ai.shotAlert(0) < 35:
            DataMinerBD.tturnRight(1)
            DataMinerBD.tthrust(1)
        elif diff >= 0:
            if diff >= 180:
                a = 0
                DataMinerBD.tturnRight(1)     #If the target is to our right- turn right
            elif diff != 0 :              
                a = 0
                
                DataMinerBD.tturnLeft(1)      #If the target is to our left - turn left
        else :
            if diff > -180:
                a = 0
                DataMinerBD.tturnRight(1)     #If the target is to our right - turn right
            else :
                a = 0
                DataMinerBD.tturnLeft(1)      #If the target is to our left - turn left
    else : #Rules for avoiding death
       # if crashWall/ai.selfSpeed() > ai.closestShot() :
       #We find a target heading using our current trajectory and the closest wall then turn in it's direction
        targetHeading = heading
        print(heading)
        if wallNum == 1or wallNum == 6 or wallNum == 5:    #Front Wall is Closest
            if lastTurn == 1:
                targetHeading += 270
                targetHeading = (targetHeading)%360
            else :
                targetHeading +=90
                targetHeading = targetHeading%360
            
            print("front")
        elif wallNum == 2  :  # Left Wall is Closest
            targetHeading += 270
            targetHeading = (targetHeading)%360
            lastTurn = 1
            print("leftwall")
        elif wallNum == 3  :
            targetHeading = targetHeading + 90
            targetHeading = (targetHeading)%360
            lastTurn = 2
            print("rightWall")
        else :
            if backLeftWall < backRightWall:
                lastTurn = 2
                targetHeading += 5
                targetHeading = (targetHeading)%360
            if backLeftWall > backRightWall:
                lastTurn = 1
                targetHeading -= 5
                targetHeading = (targetHeading)%360
       
        speedConcern = ai.selfSpeed() - 4
        
        if speedConcern < 0:
            speedConcern = 0
        elif speedConcern > 5:
            speedConcern = 5
        
        #targetHeading = (targetHeading*(1-(speedConcern/5))) + (((tracking+170)%360)*(speedConcern/5))
        if speedConcern > 2:
            targetHeading = (tracking + 180)%360
        
        diff = (targetHeading - heading)
        print("targetHEading : ", targetHeading,  " heading : ",  heading)
        if diff >= 0:
            if diff >= 180:
                DataMinerBD.tturnRight(1)     #If the targetHEading is to our right- turn right
                
                print("right")
            elif diff != 0 :                       
                DataMinerBD.tturnLeft(1)      #If the targeHeadingt is to our left - turn left
                print("left")
        else :
            if diff > -180:
                print("right")
                DataMinerBD.tturnRight(1)     #If the targetHeading is to our right - turn right
                #print("right")
            else :
                print("left")
                DataMinerBD.tturnLeft(1)      #If the targetHeading is to our left - turn left
            #print("nice")
    
    #Rules for thrusting
    
    if speed < 5 and frontWall > 200:   #If we are moving slowly and we won't ram into anything, accelerate
        DataMinerBD.tthrust(1)
    elif crashWall < 25*speed  and (abs(tracking - heading) > 120):  #If we are getting close to a wall, and we can thrust away from it, do so
        DataMinerBD.tthrust(1)
    elif backWall < 30: #If there is a wall very close behind us, get away from it
        DataMinerBD.tthrust(1)
    
    if abs(calcDir - heading) < 15 : #If we are close to the current proper trajectory for a shot then fire
        DataMinerBD.tshoot()
Exemple #5
0
 def __init__(self):
     idE = ai.closestShipId()
     super().__init__(idE)
Exemple #6
0
 def __init__(self, gain=FIRE_GAIN):
     super(FireClosestEnemy, self).__init__(ai.closestShipId(), gain)
def AI_loop():
    #Release keys
    ai.thrust(0)
    ai.turnLeft(0)
    ai.turnRight(0)
    #Set variables
    heading = int(ai.selfHeadingDeg())
    tracking = int(ai.selfTrackingDeg())
    frontWall = ai.wallFeeler(500, heading)
    left45Wall = ai.wallFeeler(500, heading + 45)
    right45Wall = ai.wallFeeler(500, heading - 45)
    left90Wall = ai.wallFeeler(500, heading + 90)
    right90Wall = ai.wallFeeler(500, heading - 90)
    left135Wall = ai.wallFeeler(500, heading + 135)
    right135Wall = ai.wallFeeler(500, heading - 135)
    backWall = ai.wallFeeler(500, heading - 180)
    trackWall = ai.wallFeeler(500, tracking)

    #######   Shooting Ennemies  ########
    ##Find the closest ennemy##
    ClosestID = ai.closestShipId()
    #print(ClosestID)
    ##Get the closest ennemy direction and speed##
    ClosestSpeed = ai.enemySpeedId(ClosestID)
    #print(ClosestSpeed)
    ClosestDir = ai.enemyTrackingDegId(ClosestID)
    #print(ClosestDir)
    ## Get the lockheadingdeg ##
    enemy = ai.lockNext()
    print(enemy)
    head = ai.lockHeadingDeg()
    print(head)
    enemyDist = ai.selfLockDist()
    print(enemyDist)

    ### Turning Rules ###
    if frontWall <= 200 and (left45Wall < right45Wall):
        print("turning right")
        ai.turnRight(1)
    elif frontWall <= 200 and (left45Wall > right45Wall):
        ai.turnLeft(1)
    elif left90Wall <= 200:
        print("turning right")
        ai.turnRight(1)
    elif right90Wall <= 200:
        print("turning left")
        ai.turnLeft(1)
    ### Thrust commands ####
    elif ai.selfSpeed() <= 10 and (frontWall >= 200) and (
            left45Wall >= 200) and (right45Wall >= 200) and (
                right90Wall >= 200) and (left90Wall >= 200) and (
                    left135Wall >= 50) and (right135Wall >= 50) and (backWall
                                                                     >= 50):
        print("go forward")
        ai.thrust(1)
    elif trackWall < 75 and ai.selfSpeed() >= 10:
        ai.thrust(1)
    elif trackWall < 50 and ai.selfSpeed() <= 10:
        ai.thrust(1)
    elif backWall <= 75:
        ai.thrust(1)
    elif left135Wall <= 75:
        ai.thrust(1)
    elif right135Wall <= 75:
        ai.thrust(1)
    ##### Shooting Ennemy Commands #####
    elif enemyDist <= 500 and heading > (head):
        ai.turnRight(1)
        ai.fireShot()
    elif enemyDist <= 500 and heading < (head):
        ai.turnLeft(1)
        ai.fireShot()
    else:
        print("chilling")
        ai.thrust(0)
Exemple #8
0
def AI_loop():
    """ The main loop for this agent!
        Prod.py is a rule-based expert system.
    """

    # Release keys
    ai.thrust(0)
    ai.turnLeft(0)
    ai.turnRight(0)

    # Heuristics
    sideFeelerOffset = 65  # Offsets from heading for wall feelers (degrees)
    perpFeelerOffset = 90  # (degrees)
    trackFeelerOffset = 45  # (degrees)

    nearLimit = 150  # Threshold for a relatively "close" object (xp distance units)
    nearLimitThreat = 300  # Threshold for a "very close" object (xp distance units)
    shotDanger = 130  # Threshold for relatively "close" bullets (xp distance units)

    speedLimit = 5  # (xp speed units)
    powerHigh = 45  # (xp thrust power units)
    powerLow = 20  # (xp thrust power units)
    targetingAccuracy = 5  # Tolerance from heading within which firing is OK (degrees)

    # Reset everything else
    ai.setTurnSpeedDeg(20)  # Artificial handicap!
    ai.setPower(powerLow)

    # Acquire information
    heading = int(ai.selfHeadingDeg())
    tracking = int(ai.selfTrackingDeg())

    feelers = []
    heading = int(ai.selfHeadingDeg())
    tracking = int(ai.selfTrackingDeg())
    frontWall = ai.wallFeeler(500, heading)
    leftWall = ai.wallFeeler(500, heading + perpFeelerOffset)
    rightWall = ai.wallFeeler(500, heading - perpFeelerOffset)
    trackWall = ai.wallFeeler(500, tracking)
    rearWall = ai.wallFeeler(500, heading - 180)
    backLeftWall = ai.wallFeeler(500, heading + 135)
    backRightWall = ai.wallFeeler(500, heading - 135)
    frontLeftWall = ai.wallFeeler(500, heading + 45)
    frontRightWall = ai.wallFeeler(500, heading - 45)

    feelers.append(frontWall)
    feelers.append(leftWall)
    feelers.append(rightWall)
    feelers.append(trackWall)
    feelers.append(rearWall)
    feelers.append(backLeftWall)
    feelers.append(backRightWall)
    feelers.append(frontLeftWall)
    feelers.append(frontRightWall)

    # Collect distances
    if ai.enemyDistanceId(ai.closestShipId()) > 0:
        closestPlayerDistance = ai.enemyDistanceId(ai.closestShipId())
    else:
        closestPlayerDistance = math.inf

    if ai.shotDist(0) > 0:
        closestBulletDistance = ai.shotDist(0)
    else:
        closestBulletDistance = math.inf

    dcw = min(feelers)
    distToNearestThreat = min(closestPlayerDistance, closestBulletDistance)

    # Assign priority to nearest threat
    if closestBulletDistance <= dcw and closestBulletDistance <= closestPlayerDistance:  # if closest threat is a bullet
        priority = 1
    elif dcw <= closestPlayerDistance and dcw <= closestBulletDistance:  # if closest threat is a wall
        priority = 2
    else:  # closest threat is a player
        priority = 3

    if distToNearestThreat < nearLimitThreat:
        ai.setPower(powerHigh)

    # If the closest threat is a bullet
    if priority == 1:
        m = angleToPointDeg((ai.selfX(), ai.selfY()),
                            (ai.shotX(0), ai.shotY(0)))
        if m >= 0:
            ai.turnRight(1)
        else:
            ai.turnLeft(1)

        if ai.shotAlert(0) < shotDanger:
            ai.thrust(1)

    # If the closest threat is a wall
    elif priority == 2:
        # Thrust
        if ai.selfSpeed() <= speedLimit:
            ai.thrust(1)
        elif trackWall < nearLimit and angleDiff(heading, tracking) > 90:
            ai.thrust(1)
        elif rearWall < nearLimit and angleDiff(heading, tracking) > 90:
            ai.thrust(1)

        # Turn
        if trackWall < nearLimit and leftWall < rightWall:
            ai.turnRight(1)
        elif trackWall < nearLimit and rightWall < leftWall:
            ai.turnLeft(1)
        elif backLeftWall < nearLimit and rightWall > 50:
            ai.turnRight(1)
        elif backRightWall < nearLimit and leftWall > 50:
            ai.turnLeft(1)
        elif frontRightWall < nearLimit:
            ai.turnLeft(1)
        elif frontLeftWall < nearLimit:
            ai.turnRight(1)

    # If the closest threat is a player
    elif priority == 3:
        m = angleToPointDeg((ai.selfX(), ai.selfY()),
                            (ai.shotX(0), ai.shotY(0)))
        if m <= 0:
            ai.turnRight(1)
        else:
            ai.turnLeft(1)

        if ai.selfHeadingDeg() <= (targetingAccuracy + angleToPointDeg(
            (ai.selfX(), ai.selfY()),
            (ai.screenEnemyX(0), ai.screenEnemyY(0)))) and ai.selfHeadingDeg(
            ) >= (targetingAccuracy - angleToPointDeg(
                (ai.selfX(), ai.selfY()),
                (ai.screenEnemyX(0), ai.screenEnemyY(0)))):
            ai.fireShot()
Exemple #9
0
 def __init__(self):
     idE = ai.closestShipId()
     super().__init__(idE)
Exemple #10
0
def AI_loop():
    """ The main loop for this agent!
        Greenock is a rule-based expert system.
    """

    # Release keys
    ai.thrust(0)
    ai.turnLeft(0)
    ai.turnRight(0)

    # Heuristics
    sideFeelerOffset = 15  # Offsets from heading for wall feelers (degrees)
    nearLimit = 20 * ai.selfSpeed(
    )  # Threshold for close objects (xp distance units)
    shotDanger = 80  # Threshold for close bullets (xp distance units)

    # Reset everything else
    ai.setTurnSpeedDeg(20)  # Artificial handicap!

    # Acquire information
    heading = int(ai.selfHeadingDeg())
    tracking = int(ai.selfTrackingDeg())

    frontLeftWall = ai.wallFeeler(500, heading + sideFeelerOffset)
    frontRightWall = ai.wallFeeler(500, heading - sideFeelerOffset)

    rshipnum = ai.closestShipId()

    # Combat decisions
    if (ai.shotAlert(0) > -1) and (ai.shotAlert(0) < shotDanger):
        ai.turnToDeg(angleDiff(heading, ai.angleAdd(ai.shot_idir(0), 90)))
        ai.thrust(1)
    elif (ai.closestShipId() > -1):
        ai.turnToDeg(angleDiff(heading, ai.aimdir(ai.closestShipId())))
        ai.fireShot()
    elif (rshipnum > -1):
        ai.turnToDeg(angleDiff(heading, ai.aimdir(ai.closestShipId)))
        if (ai.selfSpeed() < 10):
            ai.thrust(1)
        else:
            ai.fireShot()

    # Navigation decisions
    if ((frontRightWall == frontLeftWall) and (frontRightWall < nearLimit)
            and (ai.selfSpeed() > 1)):
        ai.turnToDeg(angleDiff(heading, ai.angleAdd(180,
                                                    ai.selfTrackingDeg())))
        ai.thrust(1)
    elif ((frontRightWall < frontLeftWall) and (frontRightWall < nearLimit)
          and (ai.selfSpeed() > 1)):
        ai.turnToDeg(
            angleDiff(heading,
                      ai.angleAdd(180, ai.angleAdd(-15,
                                                   ai.selfTrackingDeg()))))
        ai.thrust(1)
    elif ((frontRightWall > frontLeftWall) and (frontRightWall < nearLimit)
          and (ai.selfSpeed() > 1)):
        ai.turnToDeg(
            angleDiff(heading,
                      ai.angleAdd(180, ai.angleAdd(15, ai.selfTrackingDeg()))))
        ai.thrust(1)
Exemple #11
0
 def __init__(self, gain=FIRE_GAIN):
     super(FireClosestEnemy, self).__init__(ai.closestShipId(), gain)