def next_move(self, map, bot): move = Point(0, 0) resource = [] for i in range(21): for j in range(21): if map[i][j] == 4: resource.append(Node(i, j)) print("Resource en : ") print(i + j) closest = Node(999, 999) for node in resource: if closest is None or node.distTo(bot) < closest.distTo(bot): closest = node drows = closest.x - bot.x dcols = closest.y - bot.y if drows < 0: return Point(0, -1) elif drows > 0: return Point(0, -1) elif dcols < 0: return Point(-1, 0) elif dcols > 0: return Point(0, -1)
def getMoveTowards(fromPoint, toPoint): distX = toPoint.x - fromPoint.x distY = toPoint.y - fromPoint.y absoluteDistX = abs(distX) absoluteDistY = abs(distY) # If we are closer in X position, move in the y axis if absoluteDistX <= absoluteDistY and distY != 0: return Point(0, distY // absoluteDistY) # abs is used to keep the sign on the move. elif absoluteDistX > absoluteDistY and distX != 0: return Point(distX // absoluteDistX, 0) else: print("[MapHelper.getMoveTowards] You are already at the given position.") return Point(0, 0)
def getStandardMap(self, gameMap, bot): map = [[j for j in range(21)] for i in range(21)] for i in range(21): for j in range(21): map[i][j] = gameMap.getTileAt(Point(i, j)) return map
def execute_turn(self, gameMap, visiblePlayers): if (self.actionList[self.action] == 1): #Droite self.action += 1 return create_move_action(Point(1, 0)) elif (self.actionList[self.action] == 2): #Gauche self.action += 1 return create_move_action(Point(-1, 0)) elif (self.actionList[self.action] == 3): #Up self.action += 1 return create_move_action(Point(0, -1)) elif (self.actionList[self.action] == 4): #Down self.action += 1 return create_move_action(Point(0, 1)) elif (self.actionList[self.action] == 5): #miner self.action += 1 return create_collect_action(Point(0, -1))
def astar(map, start, goal): startNode = Node(None, start) startNode.g = startNode.h = startNode.f = 0 goalNode = Node(None, goal) goalNode.g = goalNode.h = goalNode.f = 0 openList = [] closedList = [] openList.append(startNode) while len(openList) > 0: currentNode = openList[0] currentIndex = 0 for index, item in enumerate(openList): if item.f < currentNode.f: # prochain noeud qui à le plus petit coût currentNode = item currentIndex = index openList.pop(currentIndex) closedList.append(currentNode) if currentNode == goalNode: path = [] current = currentNode while current is not None: path.append(current.position) current = current.parent cost = 0 for pos in path[::-1]: if pos == start or pos == goal: continue if map.getTileAt(pos) == TileContent.Wall: cost += 5 else: cost += 1 return path[::-1], cost # Return reversed path children = [] for newPosition in [(0, -1), (0, 1), (-1, 0), (1, 0)]: # Carrés adajacents # Get node position nodePosition = Point(currentNode.position.x + newPosition[0], currentNode.position.y + newPosition[1]) # Si c'est une tuile marchable (lave et ressources, peut-être les maisons des autres joueurs?) currentContent = map.getTileAt( Point(nodePosition.x, nodePosition.y)) if currentContent == TileContent.Lava: continue # ne prends pas en compte les tuiles de ressources sauf si c'est le but if currentContent == TileContent.Resource and nodePosition != goal: continue if currentContent == TileContent.House and nodePosition != goal: continue # Check if node is already in list if Node(currentNode, nodePosition) in closedList: continue # Create new node newNode = Node(currentNode, nodePosition) # Append children.append(newNode) # Loop through children for child in children: # Child is on the closed list for closedChild in closedList: if child == closedChild: continue # Create the f, g, and h values child.g = currentNode.g + 1 child.h = ((child.position.x - goalNode.position.x)**2) + ( (child.position.y - goalNode.position.y)**2) child.f = child.g + child.h # Ajout du poids des objets brisables if map.getTileAt(Point(child.position.x, child.position.y)) == TileContent.Wall: child.f += 5 # Child is already in the open list for openNode in openList: if child == openNode and child.g > openNode.g: continue # Add the child to the open list openList.append(child)
def __init__(self, tile_content, x, y): self.TileContent = tile_content self.Position = Point(x, y) pass
def getPath(gameMap, position, destination): mapTile = [] mapContent = [] print("DESSSS") print(destination) for i in range(0, len(gameMap.tiles)): mapTile.append(list()) mapContent.append(list()) for j in range(0, len(gameMap.tiles[0])): mapTile[i].append(gameMap.tiles[i][j]) mapContent[i].append(gameMap.tiles[i][j].TileContent) subMapTile = [] subMapContent = [] x = -1 y = -1 posDepartX = 0 posDepartY = 0 posDestX = 0 posDestY = 0 for i in range(position.x, destination.x, int(copysign(1, destination.x - position.x))): x += 1 subMapTile.append(list()) subMapContent.append(list()) for j in range(position.y, destination.y, int(copysign(1, destination.y - position.y))): y += 1 subMapTile[x].append(mapTile[i][j]) subMapContent[x].append(mapTile[i][j].TileContent) if mapTile[i][j].Position.x == position.x and mapTile[i][ j].Position.y == position.y: posDepartX = x posDepartY = y if mapTile[i][j].Position.x == destination.x and mapTile[i][ j].Position.y == destination.y: posDestX = x posDestY = y path = [Point(10, 0), Point(11, 0)] pr1 = goingTo(path) path2 = [ Point(10, 0), Point(11, 0), Point(11, 1), Point(10, 1), Point(10, 0) ] pr2 = goingTo(path2) # for e in pr2: # print(e.__str__()) # print("TEST MAP") mat2 = [[TileContent.Empty, TileContent.Empty, TileContent.Empty], [TileContent.Empty, TileContent.Lava, TileContent.Empty], [TileContent.Empty, TileContent.Empty, TileContent.Empty]] #print("translate : {}".format(translate(subMapContent))) #print("findPath : {}".format(find_shortest_path(translate(subMapContent),"{},{}".format(posDepartX, posDepartY),"{},{}".format(posDestX, posDestY)))) p1rel2 = goingTo( pathStrToPoint( find_shortest_path(translate(subMapContent), "{},{}".format(posDepartX, posDepartY), "{},{}".format(posDestX, posDestY)))) print(p1rel2) return p1rel2 for e in p1rel2: print(e.__str__())