def attack(self, gameState): # if self.curInDangerous or self.teammateInDangerous: # eatCapsule ###自己或队友在危险中,自己能吃到capsule,自己离capsule比队友近,则去吃capsule,否则吃豆子 foodsList = self.getFood(gameState) foodList = self.getSafeFood(foodsList, self.block) foodCluster = self.getTeammateTargetRegion(gameState) self.foodGrid = self.removeFoodsForTeammate(foodCluster) walls = self.getNewWalls(self.block) if foodList != []: problem = myProblem.EatOneSafeFoodProblem(gameState, self) action, target = myProblem.minDistance(self.curPos, foodList, walls, self) agentMod[self.index] = ("eatFood", target) else: action, target = myProblem.reachOwnMidWithEnemyInsight()
def defence(self): minDist = 999 closestMid = random.choice(self.midLine) for pos in self.midLine: for index in self.enemyIndex: dist = self.distancer.getDistance( self.enemyPositionsToDefend[index], pos) if dist < minDist: minDist = dist closestMid = pos # print(closestMid) action, target = myProblem.minDistance(self.curPos, [closestMid], self.walls, self) agentMod[self.index] = ("go defence", target) # self.debugDraw(target,[0.8,0.4,0.2]) return action, target
def trace(self, gameState, enemyIndex): enemyPos = self.enemyPositionsToDefend[enemyIndex] action, target = myProblem.minDistance(self.curPos, [enemyPos], self.walls, self) agentMod[self.index] = ("trace enemy", target) return action, target
def chooseAction(self, gameState): self.debugClear() self.curPos = gameState.getAgentPosition(self.index) teammateCluster = self.getTeammateTargetRegion(gameState) closestFood = self.getClosedFood(gameState, teammateCluster) # if self.index != 0 : # self.debugDraw(closestFood,[1,0,0.1]) enemyIndices = self.getOpponents(gameState) enemyPos = [] for idx in enemyIndices: enemyPos.append(gameState.getAgentPosition(idx)) self.foodGrid = self.getFood(gameState) s = time.clock() self.curPos = gameState.getAgentPosition(self.index) self.carryFoods = gameState.data.agentStates[self.index].numCarrying # if debug: # print("last action:",self.lastAction) for i in deadEnemy: if deadEnemy[i] > 0: deadEnemy[i] += -1 curPos = gameState.getAgentPosition(self.index) self.block = self.getBlockRegions(gameState) # self.debugClear() # if debug: # for i in block: # if self.index <2: # self.debugDraw(i, [1, 0, 0]) # else: # self.debugDraw(i,[0,1,0]) teammateIndex = self.getIndex(2) teammatePos = gameState.getAgentPosition(teammateIndex) # type: 'dict' # key: enemyIndex # value: list of positions self.getEnemyPosition(gameState) self.enemyInRegion = self.getEnemyInRegion(gameState, enemyPosition.enemyPosition) # print(self.enemyInRegion) # ghostEnemy = self.ghostEnemy(enemyPos) # pacmanEnemy = self.pacmanEnemy(enemyPos) # enemyPositionsToDefend = self.getNeedOfDefenceEnemyPosition(gameState, enemyPosition.enemyPosition) posibblePos = set() for i in list((enemyPosition.enemyPosition).values()): for j in i: if j not in posibblePos: posibblePos.add(j) # enemySight = self.getEnemySight(list(posibblePos)) # [ [(),(),()...], [(),(),(),...] ] # if debug: # for i in enemySight: # self.debugDraw(i, [enemySight[i]/70+0.2,.2,.2]) self.foodGrid = self.getFood(gameState) self.getEnemyCarryFoodNumber(gameState, enemyPosition.enemyPosition) # print(enemyCarryFoodNumber) self.updateEnemyDied() self.teammatePos = gameState.getAgentPosition(self.allienIndex) self.curInDangerous = self.curInsightOfEnemy(self.curPos, enemyPos) self.teammateInDangerous = self.curInsightOfEnemy( self.teammatePos, enemyPos) self.enemyPositionsToDefend = self.getNeedOfDefenceEnemyPosition( gameState, enemyPosition.enemyPosition) enemySight = self.getEnemySight( enemyPosition.enemyPosition) # [ [(),(),()...], [(),(),(),...] ] # print(enemySight) posInsight = self.getEnemyInsightTogether(enemySight) foodCluster = self.getTeammateTargetRegion(gameState) # print(self.getFood(gameState)) # for i in foodCluster: # self.debugDraw(i,[1,0,0]) self.removeFoodsForTeammate(foodCluster) # print(self.foodGrid) # print(self.foodGrid) # self.debugClear() # for i in self.foodGrid.asList(): # if self.index == 2: # self.debugDraw(i,[1,0,1]) # print(posInsight) # # self.debugClear() # # if True: #debug # # for i in enemySight: # # for j in enemySight[i]: # # self.debugDraw(j, [122,122,122]) self.enemyAllHome = self.getEnemyAllatHome() # print(self.enemyDied,self.enemyDied[self.enemyIndex[0]] or self.enemyDied[self.enemyIndex[1]]) #(self.index == 0 or self.index == 2) if (self.index == 0 or self.index == 1) or (self.enemyAllHome and (self.enemyDied[self.enemyIndex[0]] or self.enemyDied[self.enemyIndex[1]])): foodFarFromEnemy = self.getFoodFarFromEnemy( gameState, curPos, self.enemyPositionsToDefend) # self.debugDraw(foodFarFromEnemy,[1,0,0]) distToFood = 100 if foodFarFromEnemy: distToFood = self.distancer.getDistance( foodFarFromEnemy, curPos) closestMidDist, closestMidPos = self.getClostestMidDistance( curPos, gameState) if (((closestMidDist < 5) and (distToFood > closestMidDist + 4) and self.carryFoods > 0) or (len(self.getFood(gameState).asList()) <= 2) and (self.curPos[0] in self.enemyRegionX)) or (foodFarFromEnemy is None): # self.debugDraw(foodFarFromEnemy,[self.index/2,0,0]) if self.curInsightOfEnemy(curPos, enemyPos): escapeProblem = myProblem.EscapeProblem1(gameState, self) actions, target = self.aStarSearch( escapeProblem, gameState, escapeProblem.EscapeHeuristic) if actions == [] or actions == None or actions == "TIMEEXCEED": # print("reachOwnMidWithEnemyInsight2") action, target = myProblem.reachOwnMidWithEnemyInsight( self, gameState, self.index) agentMod[self.index] = ("BackToMid", target) if debug: print("reachOwnMidWithEnemyInsight2", action) else: action = actions[0] agentMod[self.index] = ("BackToMid", target) else: action, target = myProblem.minDistance( curPos, [closestMidPos], self.walls, self) agentMod[self.index] = ("BackToMid", target) else: # self.debugDraw(foodFarFromEnemy,[self.index/2,self.index/2,0]) if self.curInsightOfEnemy(curPos, enemyPos): self.block = self.getBlockRegions(gameState) safeFood = self.getSafeFood(gameState, self.block) capsules = self.getCapsules(gameState) safeFood = safeFood + capsules if safeFood != []: problem = myProblem.EatOneSafeFoodProblem( gameState, self) actions, target = self.aStarSearch( problem, gameState, problem.eatOneSafeHeuristic) if (not actions is None) and (actions[0] != "T"): action = actions[0] agentMod[self.index] = ("eatFood", target) else: action, target = myProblem.eatCloseFood( self, gameState, self.index) agentMod[self.index] = ("eatFood", target) else: action, target = myProblem.eatCloseFood( self, gameState, self.index) agentMod[self.index] = ("eatFood", target) else: # if self.carryFoods == 0: # problem = myProblem.EatOneFoodHiddenProblem(gameState,self,posInsight) # actions,target = self.aStarSearch(problem,gameState,problem.eatOneHeuristic) # action = actions[0] # self.debugDraw(target,[1,0,0]) action, target = myProblem.minDistance( curPos, [foodFarFromEnemy], self.walls, self) agentMod[self.index] = ("eatFood", target) # else: # action,target = myProblem.eatCloseFood(self,gameState,self.index) # agentMod[self.index] = ("eatFood",target) else: if self.enemyAllHome: # print(self.index,"I'm going to defend") action, target = self.defence() agentMod[self.index] = ("defend", target) else: # print(self.index,"Trace") enemyNeedToTrace = self.getEnemyNeedToTrace() # print(enemyNeedToTrace) action, target = self.trace(gameState, enemyNeedToTrace) agentMod[self.index] = ("defend", target) # print(self.enemyDied,self.enemyAllHome) self.updateDeath(gameState, action) # print(self.index,time.clock() - s) teammateState[self.index] = gameState.generateSuccessor( self.index, action) # print(self.index,time.clock() - s) actionHistory[self.index].append(action) # print(agentMod) return action