2020-04-23 19:43:51 +02:00
|
|
|
from warehouse import Coordinates, Tile, Pack
|
|
|
|
from attributes import PackStatus
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
class Agent:
|
2020-04-06 15:12:50 +02:00
|
|
|
def __init__(self, start_x, start_y, assigned_warehouse, radius=5):
|
2020-04-02 15:25:15 +02:00
|
|
|
self.x = start_x
|
2020-04-04 22:03:15 +02:00
|
|
|
self.y = start_y
|
2020-04-07 01:18:04 +02:00
|
|
|
self.direction = 'down'
|
2020-04-06 15:12:50 +02:00
|
|
|
self.radius = radius
|
|
|
|
self.assigned_warehouse = assigned_warehouse
|
2020-04-06 16:11:57 +02:00
|
|
|
self.is_loaded = False
|
2020-04-23 19:43:51 +02:00
|
|
|
self.transported_package = None
|
2020-04-07 01:18:04 +02:00
|
|
|
|
2020-04-07 18:50:25 +02:00
|
|
|
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
|
2020-04-07 01:18:04 +02:00
|
|
|
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
|
2020-04-07 18:50:25 +02:00
|
|
|
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
|
2020-04-07 01:18:04 +02:00
|
|
|
|
2020-04-06 15:12:50 +02:00
|
|
|
def move_right(self):
|
2020-04-07 01:18:04 +02:00
|
|
|
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)
|
2020-04-06 15:12:50 +02:00
|
|
|
|
|
|
|
def move_left(self):
|
2020-04-07 01:18:04 +02:00
|
|
|
pos_x = self.x - 1
|
|
|
|
if pos_x < 0:
|
|
|
|
pos_x = 0
|
|
|
|
return Coordinates(x=pos_x, y=self.y)
|
2020-04-06 15:12:50 +02:00
|
|
|
|
|
|
|
def move_down(self):
|
2020-04-07 01:18:04 +02:00
|
|
|
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)
|
2020-04-06 15:12:50 +02:00
|
|
|
|
|
|
|
def move_up(self):
|
2020-04-07 01:18:04 +02:00
|
|
|
pos_y = self.y - 1
|
|
|
|
if pos_y < 0:
|
|
|
|
pos_y = 0
|
2020-04-23 19:43:51 +02:00
|
|
|
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
|