moje podejscie
This commit is contained in:
parent
976d285e4b
commit
b2463050bc
BIN
__pycache__/agent.cpython-37.pyc
Normal file
BIN
__pycache__/agent.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/attributes.cpython-37.pyc
Normal file
BIN
__pycache__/attributes.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/warehouse.cpython-37.pyc
Normal file
BIN
__pycache__/warehouse.cpython-37.pyc
Normal file
Binary file not shown.
181
agent.py
181
agent.py
@ -1,96 +1,91 @@
|
||||
from warehouse import Coordinates, Tile, Pack
|
||||
from attributes import PackStatus, TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS
|
||||
|
||||
class Node:
|
||||
def __init__(self, coord_x, coord_y):
|
||||
self.x = coord_x
|
||||
self.y = coord_y
|
||||
self.parent = None
|
||||
self.g_cost = 0
|
||||
self.h_cost = 0
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Node):
|
||||
return self.x == other.x and self.y == self.y
|
||||
return False
|
||||
|
||||
class Agent:
|
||||
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
|
||||
self.x = start_x
|
||||
self.y = start_y
|
||||
self.direction = 'down'
|
||||
self.radius = radius
|
||||
self.assigned_warehouse = assigned_warehouse
|
||||
self.is_loaded = False
|
||||
self.transported_package = None
|
||||
|
||||
def demo_agent_move(self, demo_agent_step, demo_step_max=5):
|
||||
demo_agent_sign = 0
|
||||
next_coords = self.get_next_move_coordinates()
|
||||
can_move = self.check_if_can_move(next_coords)
|
||||
if demo_agent_step >= demo_step_max:
|
||||
demo_agent_sign = -1
|
||||
self.direction = 'up'
|
||||
elif demo_agent_step == 0:
|
||||
demo_agent_sign = 1
|
||||
self.direction = 'down'
|
||||
if not can_move:
|
||||
alternative_move = ('left' if self.check_if_can_move(self.move_left()) else None) or ('right' if self.check_if_can_move(self.move_right()) else self.direction)
|
||||
self.direction = alternative_move
|
||||
else:
|
||||
self.x = next_coords.x
|
||||
self.y = next_coords.y
|
||||
|
||||
def turn_left(self):
|
||||
new_direction = TURN_LEFT_DIRECTIONS.get(self.direction, self.direction)
|
||||
self.direction = new_direction
|
||||
|
||||
def turn_right(self):
|
||||
new_direction = TURN_RIGHT_DIRECTIONS.get(self.direction, self.direction)
|
||||
self.direction = new_direction
|
||||
|
||||
def move(self):
|
||||
next_coords = self.get_next_move_coordinates()
|
||||
can_move = self.check_if_can_move(next_coords)
|
||||
if can_move:
|
||||
self.x = next_coords.x
|
||||
self.y = next_coords.y
|
||||
|
||||
def get_next_move_coordinates(self):
|
||||
direction_moves = {
|
||||
'up': self.move_up(),
|
||||
'down': self.move_down(),
|
||||
'left': self.move_left(),
|
||||
'right': self.move_right()
|
||||
}
|
||||
next_coords = direction_moves.get(self.direction, Coordinates(self.x, self.y))
|
||||
return next_coords
|
||||
|
||||
def check_if_can_move(self, next_coords: Coordinates):
|
||||
next_tile = self.assigned_warehouse.tiles[next_coords.x][next_coords.y]
|
||||
tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable
|
||||
tile_on_map = 0 <= next_coords.x < self.assigned_warehouse.width and 0 <= next_coords.y < self.assigned_warehouse.height
|
||||
return tile_passable and tile_on_map
|
||||
|
||||
def move_right(self):
|
||||
pos_x = self.x + 1
|
||||
if pos_x > self.assigned_warehouse.width - 1:
|
||||
pos_x = self.assigned_warehouse.width - 1
|
||||
return Coordinates(x=pos_x, y=self.y)
|
||||
|
||||
def move_left(self):
|
||||
pos_x = self.x - 1
|
||||
if pos_x < 0:
|
||||
pos_x = 0
|
||||
return Coordinates(x=pos_x, y=self.y)
|
||||
|
||||
def move_down(self):
|
||||
pos_y = self.y + 1
|
||||
if pos_y > self.assigned_warehouse.height - 1:
|
||||
pos_y = self.assigned_warehouse.height - 1
|
||||
return Coordinates(x=self.x, y=pos_y)
|
||||
|
||||
def move_up(self):
|
||||
pos_y = self.y - 1
|
||||
if pos_y < 0:
|
||||
pos_y = 0
|
||||
return Coordinates(x=self.x, y=pos_y)
|
||||
|
||||
def package_ahead(self):
|
||||
next_coords = self.get_next_move_coordinates()
|
||||
potential_package = self.assigned_warehouse.tiles[next_coords.x][next_coords.y]
|
||||
return isinstance(potential_package, Pack) and potential_package.status != PackStatus.STORED
|
||||
|
||||
def pick_up_package(self, pack):
|
||||
tile = pack.lays_on_field
|
||||
self.assigned_warehouse.tiles[tile.x_position][tile.y_position] = tile
|
||||
self.is_loaded = True
|
||||
pack.lays_on_field = None
|
||||
self.transported_package = pack
|
||||
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
|
||||
self.x = start_x
|
||||
self.y = start_y
|
||||
self.radius = radius
|
||||
self.warehouse = assigned_warehouse
|
||||
self.is_loaded = False
|
||||
self.transported_package = None
|
||||
self.dest = Node(0, 0)
|
||||
self.closed = list()
|
||||
self.open = list()
|
||||
self.path = list()
|
||||
|
||||
def find_path(self):
|
||||
start_node = Node(self.x, self.y)
|
||||
self.open.append(start_node)
|
||||
while self.open:
|
||||
current_node = self.open.pop()
|
||||
self.closed.append(current_node)
|
||||
if current_node.x == self.dest.x and current_node.y == self.dest.y:
|
||||
while current_node != start_node:
|
||||
path.append(current_node)
|
||||
current_node = current_node.parent
|
||||
return True
|
||||
neighbour_list = self.get_neighbours(current_node)
|
||||
for neighbour in neighbour_list:
|
||||
if neighbour in self.closed:
|
||||
continue
|
||||
cost = current_node.g_cost + self.heur_cost_est(current_node, neighbour)
|
||||
if cost < neighbour.g_cost or neighbour not in self.open:
|
||||
neighbour.g_cost = cost
|
||||
neighbour.h_cost = self.heur_cost_est(neighbour, self.dest)
|
||||
neighbour.parent = current_node
|
||||
if neighbour not in self.open:
|
||||
self.open.append(neighbour);
|
||||
return False
|
||||
|
||||
def heur_cost_est(self, nodeA: Node, nodeB: Node):
|
||||
deltaX = abs(nodeA.x - nodeB.x)
|
||||
deltaY = abs(nodeA.y - nodeB.y)
|
||||
if deltaX > deltaY:
|
||||
return (14 * deltaY) + (10 * (deltaX - deltaY))
|
||||
return (14 * deltaX) + (10 *(deltaY - deltaX))
|
||||
|
||||
def get_neighbours(self, node: Node):
|
||||
neighbours = list()
|
||||
#if self.check_if_can_move(Coordinates(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)):
|
||||
neighbours.append(Node(node.x - 1, node.y))
|
||||
#if self.check_if_can_move(Coordinates(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)):
|
||||
neighbours.append(Node(node.x, node.y - 1))
|
||||
return neighbours
|
||||
|
||||
def move(self):
|
||||
if not self.path:
|
||||
if not self.find_path():
|
||||
return
|
||||
else:
|
||||
next = self.path.pop()
|
||||
self.x = next.x
|
||||
self.y = next.y
|
||||
|
||||
def check_if_can_move(self, next_coords: Coordinates):
|
||||
next_tile = self.warehouse.tiles[next_coords.x][next_coords.y]
|
||||
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
|
||||
return tile_passable and tile_on_map
|
||||
|
||||
def pick_up_package(self, pack):
|
||||
tile = pack.lays_on_field
|
||||
self.assigned_warehouse.tiles[tile.x_position][tile.y_position] = tile
|
||||
self.is_loaded = True
|
||||
pack.lays_on_field = None
|
||||
self.transported_package = pack
|
||||
|
@ -26,18 +26,3 @@ COLORS = {
|
||||
'lightgreen': (70, 238, 70),
|
||||
'red': (255, 0, 0)
|
||||
}
|
||||
|
||||
|
||||
TURN_LEFT_DIRECTIONS = {
|
||||
"left": "down",
|
||||
"down": "right",
|
||||
"right": "up",
|
||||
"up": "left"
|
||||
}
|
||||
|
||||
TURN_RIGHT_DIRECTIONS = {
|
||||
"left": "up",
|
||||
"up": "right",
|
||||
"right": "down",
|
||||
"down": "left"
|
||||
}
|
12
main.py
12
main.py
@ -25,8 +25,6 @@ class MainGameFrame:
|
||||
self.clock = pygame.time.Clock()
|
||||
|
||||
def run(self):
|
||||
demo_agent_step = 1
|
||||
demo_agent_sign = 1
|
||||
while True:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
@ -35,13 +33,7 @@ class MainGameFrame:
|
||||
self.draw_floor()
|
||||
self.draw_packages()
|
||||
self.draw_agent()
|
||||
self.agent.demo_agent_move(demo_agent_step, 8) #linijka w celu zademonstrowania poruszania się agenta
|
||||
# self.agent.move() #oryginalna linijka
|
||||
demo_agent_step += (1*demo_agent_sign)
|
||||
if demo_agent_step >= 8:
|
||||
demo_agent_sign = -1
|
||||
if demo_agent_step == 0:
|
||||
demo_agent_sign = 1
|
||||
self.agent.move()
|
||||
pygame.display.update()
|
||||
self.clock.tick(5)
|
||||
|
||||
@ -90,4 +82,4 @@ class MainGameFrame:
|
||||
|
||||
if __name__ == '__main__':
|
||||
maingame = MainGameFrame()
|
||||
maingame.run()
|
||||
maingame.run()
|
||||
|
Loading…
Reference in New Issue
Block a user