def caughtPrey(self, coordinate, distance): message = "caught! " + str(coordinate) ai.talk(message) if distance <= 100: self.caughtPreyFlag = True else: # get last two messages sent lastMessage = self.MessageBuffer[-1] secLastMessage = self.MessageBuffer[-2] # flags for later caught1 = False caught2 = False split1 = lastMessage.split(" ") split2 = secLastMessage.split(" ") sender1 = "" sender2 = "" if "[" in split1[-1]: sender1 = split1[-1][1:-1] if "[" in split2[-1]: sender2 = split2[-1][1:-1] if sender1 != "" and sender2 != "" and sender1 != sender2: if split1[0] == "caught!" and split2[0] == "caught!": self.fitness = self.fitness + 100 with open('fitness.txt', 'a') as inFile: outString = str(self.fitness) + "\n" inFile.write(outString) ai.quitAI()
def checkSearchComplete(self): counter = 0 for spot in self.grid: if spot[0] == "checked!" or spot[0] == "checking!": counter = counter + 1 if counter == self.gridLength or counter == self.gridLength - 1: ai.talk("clear!") self.counter = 0 for spot in self.grid: spot[0] = "" return True else: return False
def markSpotChecked(self, coordinate, flag): for spot in self.grid: if spot[1] == coordinate: spot[0] = "checked!" finished = self.checkSearchComplete() if finished != True and flag == "me": # send message that coordinate is checked ai.talk("checked! " + str(coordinate)) # get new spot while self.grid[ self.counter][0] == "checked!" or self.grid[ self.counter][0] == "checking!": self.counter = (self.counter + 1) % self.gridLength # mark as checking newSpot = self.grid[self.counter][1] self.grid[self.counter][0] = "checking!" self.checking = False
def sendMessage(receiver): ai.talk(receiver + ":" + "completed " + latestTask)
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 lostPrey(self): message = "--- Lost the enemy!" ai.talk(message)
def foundPrey(self, coordinate): message = "*** " + str(coordinate) ai.talk(message)
def lostPrey(self): message = "--- Lost the enemy!" ai.talk(message) self.foundPreyFlag = False
def foundPrey(self, coordinate): message = "*** " + str(coordinate) ai.talk(message) self.foundPreyFlag = True self.preyLocation = coordinate
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 tick(): # # The API won't print out exceptions, so we have to catch and print them ourselves. # try: # # Declare global variables so we have access to them in the function # global tickCount global mode # # Reset the state machine if we die. # if not ai.selfAlive(): tickCount = 0 mode = "ready" return tickCount += 1 # # Read some "sensors" into local variables, to avoid excessive calls to the API # and improve readability. # selfX = ai.selfX() selfY = ai.selfY() selfVelX = ai.selfVelX() selfVelY = ai.selfVelY() selfSpeed = ai.selfSpeed() selfHeading = ai.selfHeadingRad() # 0-2pi, 0 in x direction, positive toward y # Add more sensors readings here print("tick count:", tickCount, "mode", mode) if mode == "ready": greetings = [ "Hey. ", "Hello. ", "Yooo. ", "Excuse me, sir? ", "It's yo boy in da house. ", "Goddaaaaag, NOLLA! ", "'Sup. Come here often? " ] questions = [ "What are your coords?", "What is your heading?", "How many items have you seen?", "What ships have you seen?", "What is your tracking?" ] if tickCount % 50 == 0: unansweredQuestions = [] for i in range(ai.getMaxMsgs()): if ":[Pelle]" in ai.scanTalkMsg(i): unansweredQuestions.append(ai.scanTalkMsg(i)) ai.removeTalkMsg(i) if name == "Stub": rand = random.SystemRandom() ai.talk("Pelle:" + rand.choice(greetings) + rand.choice(questions)) for s in unansweredQuestions: if ":[Pelle]" in s: msg = "Stub:Hey" if "coords" in s: msg = "Stub:My coords are (" + str( ai.selfX()) + ", " + str(ai.selfY()) + ")." elif "heading" in s: msg = "Stub:My heading is " + str( ai.selfHeadingRad()) + " radians." elif "tracking" in s: msg = "Stub:My tracking is " + str( ai.selfTrackingRad()) + " radians." elif "items" in s: msg = "Stub:I have seen " + str( ai.itemCountScreen()) + " items." elif "ships" in s: ships = [] if (ai.shipCountScreen() - 1) > 0: for player_id in range(ai.playerCountServer()): if ai.playerName( player_id) != ai.selfName(): ships.append(ai.playerName(player_id)) msg = "I see " + str(ai.shipCountScreen() - 1) + " ship(s). " + str(ships) ai.talk(msg) except: print(traceback.print_exc())