Пример #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
Пример #2
0
    def act(self):
        angle = ai.enemyHeadingDeg(self.idE)
        pos = np.array([ai.screenEnemyXId(self.idE), ai.screenEnemyYId(self.idE)])
        vel = ai.enemySpeedId(self.idE) * np.array([np.cos(angle), np.sin(angle)])
        self.target = pos - vel * self.K
        self.open_fire()

        # Put up shield if no good enemy. XOR operation:
        if id_valid(self.idE) == ai.selfShield():
            ai.shield()
Пример #3
0
    def act(self):
        angle = ai.enemyHeadingDeg(self.idE)
        pos = np.array(
            [ai.screenEnemyXId(self.idE),
             ai.screenEnemyYId(self.idE)])
        vel = ai.enemySpeedId(self.idE) * np.array(
            [np.cos(angle), np.sin(angle)])
        self.target = pos - vel * self.K
        self.open_fire()

        # Put up shield if no good enemy. XOR operation:
        if id_valid(self.idE) == ai.selfShield():
            ai.shield()
Пример #4
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()
Пример #5
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)

    #######   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)