1
0
forked from s434673/AI_Waiter

Zaktualizuj 'src/waiter.py'

This commit is contained in:
Dominik Zawadzki 2020-04-26 16:25:50 +00:00
parent 4e763004f6
commit 1ee56bf721

View File

@ -55,23 +55,30 @@ class Waiter(pygame.sprite.Sprite):
# AStar
def findPath(self, goal):
#Stworzenie startowego i koncowego wierzcholka
startNode = self.matrix.matrix[self.X][self.Y]
goalNode = self.matrix.matrix[goal[5]][goal[7]]
#Inicjalizacja list
# Stworzenie startowego i koncowego wierzcholka
startNode = self.matrix.matrix[self.X][self.Y]
goalNode = self.matrix.matrix[goal[0]][goal[1]]
# Inicjalizacja list
openList = []
closedList = []
openList.append(startNode)
parentNode = startNode
while len(openList) > 0:
openList.sort(key=self.tile.totalCost)
openList.sort(key=getTotalCost)
currentNode = openList.pop(0)
if currentNode != parentNode:
currentNode.parent = parentNode
parentNode = currentNode
closedList.append(currentNode)
#Tutaj odbywac sie bedzie budowanie sciezki gdy algorytm osiagnie cel
# Tutaj odbywac sie bedzie budowanie sciezki gdy algorytm osiagnie cel
if currentNode == goalNode:
path = []
current = currentNode
@ -81,52 +88,71 @@ class Waiter(pygame.sprite.Sprite):
return path[::-1]
children = []
if currentNode.X >= 0:
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X][currentNode.Y - 1])
if currentNode.X + 1 < len(self.matrix.matrix):
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X + 1][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X + 1][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y - 1])
if currentNode.Y >= 0:
if self.matrix.matrix[currentNode.X + 1][currentNode.Y].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y])
if currentNode.position[0] >= 0:
if currentNode.position[1] + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.position[0]][currentNode.position[1] + 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0]][currentNode.position[1] + 1])
if currentNode.position[1] - 1 >= 0:
if self.matrix.matrix[currentNode.position[0]][currentNode.position[1] - 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0]][currentNode.position[1] - 1])
if currentNode.X - 1 >= 0:
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X - 1][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X - 1][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y - 1])
if currentNode.Y >= 0:
if self.matrix.matrix[currentNode.X - 1][currentNode.Y].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y])
if currentNode.position[0] + 1 < len(self.matrix.matrix):
if currentNode.position[1] + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] + 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] + 1])
if currentNode.position[1] - 1 >= 0:
if self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] - 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] - 1])
if currentNode.position[1] >= 0:
if self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1]].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1]])
if currentNode.position[0] - 1 >= 0:
if currentNode.position[1] + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] + 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] + 1])
if currentNode.position[1] - 1 >= 0:
if self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] - 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] - 1])
if currentNode.position[1] >= 0:
if self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1]].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1]])
perm = 0
for child in children:
for closedChild in closedList:
if child == closedChild:
perm = 1
continue
if perm == 1:
perm = 0
continue
child.startCost = currentNode.startCost + 1
child.heuristic = ((child.position[0] - goalNode.position[0]) ** 2) + ((child.position[1] - goalNode.position[1]) ** 2)
child.heuristic = ((child.position[0] - goalNode.position[0]) ** 2) + (
(child.position[1] - goalNode.position[1]) ** 2)
child.totalCost = child.startCost + child.heuristic
for openNode in openList:
if child == openNode and child.startCost > openNode.startCost:
if child == openNode and child.startCost >= openNode.startCost:
perm = 1
continue
if perm == 1:
perm = 0
continue
openList.append(child)
def getTotalCost(tile):
return tile.totalCost