def act(self): enemy_pos = np.array( [ai.screenEnemyXId(self.idE), ai.screenEnemyYId(self.idE)]) if ai.screenEnemyXId(self.idE) != -1: self.position = enemy_pos + self.pos_offset self.control()
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
def __init__(self, idE): self.idE = idE angle = uniform(0, np.pi) offset = DISTANCE_TO_ENEMY * np.array([np.cos(angle), np.sin(angle)]) self.pos_offset = [np.round(coord) for coord in offset] enemy_pos = np.array([ai.screenEnemyXId(idE), ai.screenEnemyYId(idE)]) pos = enemy_pos + self.pos_offset super().__init__(position=[np.round(coord) for coord in pos])
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()
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()
def id_valid(idE): """ Return wether idE is a valid enemy id. """ return ai.screenEnemyXId(idE) != -1
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
def is_done(self): return ai.screenEnemyXId(self.idE) == -1
def AI_loop(self): if self.team == False: ai.talk("/team 2") self.team = True # 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) 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() enemyX1 = ai.screenEnemyXId(0) enemyY1 = ai.screenEnemyYId(0) enemyX2 = ai.screenEnemyXId(1) enemyY2 = ai.screenEnemyYId(1) enemyTeam1 = ai.enemyTeamId(0) enemyTeam2 = ai.enemyTeamId(1) myTeam = ai.selfTeam() coordinate = self.grid[self.counter][1] message = ai.scanMsg(0) # print(enemyX1, enemyY1, enemyX2, enemyY2) # print(myTeam, enemyTeam1, enemyTeam2) # 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 it is not: send message that we lost enemy if enemyX1 != -1 and enemyY1 != -1: print("enemy 1") enemyCoordinate = (enemyX1, enemyY1) self.foundPrey(enemyCoordinate) coordinate = enemyCoordinate elif enemyX2 != -1 and enemyY2 != -1: print("enemy 2") enemyCoordinate = (enemyX2, enemyY2) self.foundPrey(enemyCoordinate) coordinate = enemyCoordinate elif self.foundPreyFlag == True: print("lost prey") self.lostPrey() targetX = coordinate[0] targetY = coordinate[1] toTurn = self.angleToPoint(x, y, targetX, targetY, heading) distance = self.distance(x, targetX, y, targetY) if self.foundPreyFlag == False and self.checking == False: ai.talk("checking! " + str(coordinate)) self.checking = True # If speed is too fast, turn around and thrust to negate velocity if speed > 5: turning = ai.angleDiff(heading, tracking) if abs(turning) > 165 and abs(turning) <= 180: ai.turnLeft(0) ai.turnRight(0) if self.frames % 10 == 0: ai.thrust(1) elif turning <= 165 and turning > 0: ai.turnRight(1) else: ai.turnLeft(1) else: #-------------------- Go to coordinate / enemy --------------------# if abs(toTurn) < 10 and distance > 100: ai.turnLeft(0) ai.turnRight(0) if self.frames % 3 == 0: ai.thrust(1) elif toTurn >= 10: ai.turnLeft(1) elif toTurn <= -10: ai.turnRight(1) if self.foundPreyFlag == True and distance < 150: print("Caught enemy!") ai.quitAI() elif distance < 150: self.markSpotChecked(coordinate, "me") #-------------------- Old turn and thrust rules --------------------# if speed <= 3 and frontWall >= 200: ai.thrust(1) elif trackWall < 50: ai.thrust(1) elif backWall < 40: ai.thrust(1) # Figures out what corner we are in and turns the right directon if (backWall < 30) and (rightWallStraight < 200): ai.turnLeft(1) elif backWall < 30 and (leftWallStraight < 200): ai.turnRight(1) # Walls along our periphery (90 degree feelers) elif leftWallStraight < rightWallStraight and trackWall < 75: ai.turnRight(1) elif leftWallStraight > rightWallStraight and trackWall < 75: ai.turnLeft(1) self.frames = self.frames + 1
def act(self): enemy_pos = np.array([ai.screenEnemyXId(self.idE), ai.screenEnemyYId(self.idE)]) if ai.screenEnemyXId(self.idE) != -1: self.position = enemy_pos + self.pos_offset self.control()