it's alive!

This commit is contained in:
Yakudami 2020-04-28 00:41:57 +02:00
parent 61be80491d
commit 5d5cbb4e59

View File

@ -20,7 +20,7 @@ class Agent:
self.warehouse = assigned_warehouse self.warehouse = assigned_warehouse
self.is_loaded = False self.is_loaded = False
self.transported_package = None self.transported_package = None
self.dest = Node(0, 0) self.dest = Node(1, 1)
self.closed = list() self.closed = list()
self.open = list() self.open = list()
self.path = list() self.path = list()
@ -30,22 +30,22 @@ class Agent:
self.open.append(start_node) self.open.append(start_node)
while self.open: while self.open:
current_node = self.open.pop() current_node = self.open.pop()
print(current_node.x, current_node.y)
self.closed.append(current_node) self.closed.append(current_node)
if current_node.x == self.dest.x and current_node.y == self.dest.y: if current_node.x == self.dest.x and current_node.y == self.dest.y:
while current_node != start_node: while current_node.x != start_node.x and current_node.y != start_node.y:
path.append(current_node) self.path.append(current_node)
current_node = current_node.parent current_node = current_node.parent
return True return True
neighbour_list = self.get_neighbours(current_node) neighbour_list = self.get_neighbours(current_node)
for neighbour in neighbour_list: for neighbour in neighbour_list:
if neighbour in self.closed: if not self.check_if_closed(neighbour):
continue
cost = current_node.g_cost + self.heur_cost_est(current_node, neighbour) cost = current_node.g_cost + self.heur_cost_est(current_node, neighbour)
if cost < neighbour.g_cost or neighbour not in self.open: if (cost < neighbour.g_cost) or not self.check_if_open(neighbour):
neighbour.g_cost = cost neighbour.g_cost = cost
neighbour.h_cost = self.heur_cost_est(neighbour, self.dest) neighbour.h_cost = self.heur_cost_est(neighbour, self.dest)
neighbour.parent = current_node neighbour.parent = current_node
if neighbour not in self.open: if not self.check_if_open(neighbour):
self.open.append(neighbour); self.open.append(neighbour);
return False return False
@ -56,15 +56,30 @@ class Agent:
return (14 * deltaY) + (10 * (deltaX - deltaY)) return (14 * deltaY) + (10 * (deltaX - deltaY))
return (14 * deltaX) + (10 *(deltaY - deltaX)) return (14 * deltaX) + (10 *(deltaY - deltaX))
def check_if_open(self, nodeA: Node):
for node in self.open:
if node.x == nodeA.x and node.y == nodeA.y:
print("open")
return True
return False
def check_if_closed(self, nodeA: Node):
for node in self.closed:
if node.x == nodeA.x and node.y == nodeA.y:
print("closed")
return True
return False
def get_neighbours(self, node: Node): def get_neighbours(self, node: Node):
neighbours = list() neighbours = []
#if self.check_if_can_move(Coordinates(node.x + 1, node.y)): print(neighbours)
if self.check_if_can_move(Coordinates(node.x + 1, node.y)):
neighbours.append(Node(node.x + 1, node.y)) neighbours.append(Node(node.x + 1, node.y))
#if self.check_if_can_move(Coordinates(node.x - 1, node.y)): if self.check_if_can_move(Coordinates(node.x - 1, node.y)):
neighbours.append(Node(node.x - 1, node.y)) neighbours.append(Node(node.x - 1, node.y))
#if self.check_if_can_move(Coordinates(node.x, node.y + 1)): if self.check_if_can_move(Coordinates(node.x, node.y + 1)):
neighbours.append(Node(node.x, node.y + 1)) neighbours.append(Node(node.x, node.y + 1))
#if self.check_if_can_move(Coordinates(node.x, node.y - 1)): if self.check_if_can_move(Coordinates(node.x, node.y - 1)):
neighbours.append(Node(node.x, node.y - 1)) neighbours.append(Node(node.x, node.y - 1))
return neighbours return neighbours
@ -78,6 +93,8 @@ class Agent:
self.y = next.y self.y = next.y
def check_if_can_move(self, next_coords: Coordinates): def check_if_can_move(self, next_coords: Coordinates):
if next_coords.x < 0 or next_coords.y < 0:
return False
next_tile = self.warehouse.tiles[next_coords.x][next_coords.y] next_tile = self.warehouse.tiles[next_coords.x][next_coords.y]
tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable
tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height