From 70bb6c6b5ef01de8a0bae8be0a2533ee488afd9e Mon Sep 17 00:00:00 2001 From: andrzej Date: Sun, 5 Apr 2020 16:49:45 +0200 Subject: [PATCH] =?UTF-8?q?Poprawa=20generowania=20rega=C5=82=C3=B3w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- warehouse.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 6cbb1d2..9097e6e 100644 --- a/main.py +++ b/main.py @@ -26,7 +26,7 @@ CIRCLE_CENTER_X, CIRCLE_CENTER_Y = int(TILE_WIDTH/2), int(TILE_HEIGHT/2) class MainGameFrame: def __init__(self): self.display = pygame.display.set_mode(WINDOW_SIZE) - self.warehouse_map = warehouse.Warehouse(20, 20, 100, 40) + self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, 20) diff --git a/warehouse.py b/warehouse.py index 37e6cbc..cbc13f7 100644 --- a/warehouse.py +++ b/warehouse.py @@ -1,8 +1,11 @@ from attributes import PackSize, PackStatus import random import queue +from collections import namedtuple import itertools +Coordinates = namedtuple("Coordinates",'x y') + class CategoryData: def __init__(self, name, passable=False, can_store=True, location='general', pack_size=PackSize.ALL): self.name = name @@ -45,6 +48,7 @@ class Warehouse: self.no_of_racks = no_of_racks self.no_of_packages = no_of_packages self.generate_racks() + self.open_isolated_areas() self.packages = self.place_packages(no_of_packages) # import pdb # pdb.set_trace() @@ -62,8 +66,6 @@ class Warehouse: def generate_racks(self): q = queue.Queue() rack_counter = 0 - # import pdb - # pdb.set_trace() node_x, node_y = random.randrange(1, self.width-1), random.randrange(1, self.height-1) node = self.tiles[node_x][node_y] next_node = None @@ -97,6 +99,65 @@ class Warehouse: possible_nodes = [line for line in adjacent_tiles if line.category.name != "Rack"] return possible_nodes + def open_isolated_areas(self): + racks = self.get_all_racks() + for rack in racks: + isolated = self.check_if_isolated(rack.x_position, rack.y_position) + if isolated: + self.tiles[rack.x_position][rack.y_position] = Tile('floor', rack.x_position, rack.y_position) + tmp_tiles = self.tiles + for row_number, row_tiles in enumerate(self.tiles): + if all([(t.category.name == 'Rack') for t in row_tiles]): + puncture_point = random.randrange(1, self.width-1) + tmp_tiles[row_number][puncture_point] = Tile('floor', row_number, puncture_point) + for column in range(self.height): + column_tiles = [r[column] for r in self.tiles] + if all([(t.category.name == 'Rack') for t in column_tiles]): + puncture_point = random.randrange(1, self.height-1) + tmp_tiles[puncture_point][column] = Tile('floor', row_number, puncture_point) + self.files = tmp_tiles + + + def check_if_isolated(self, x, y): + diagonals = self.get_diagonals(x,y) + diagonal_racks = [t for t in diagonals if t.category.name == 'Rack'] + return len(diagonal_racks) > 0 + + def get_diagonals(self, x, y): + upper_right = Coordinates( + x + 1 if x + 1 < self.width else self.width - 1, + y + 1 if y + 1 < self.height else self.height - 1 + ) + upper_left = Coordinates( + x - 1 if x - 1 > 0 else 0, + y + 1 if y + 1 < self.height else self.height - 1 + ) + bottom_right = Coordinates( + x + 1 if x + 1 < self.width else self.width - 1, + y - 1 if y - 1 > 0 else 0 + ) + bottom_left = Coordinates( + x - 1 if x - 1 > 0 else 0, + y - 1 if y - 1 > 0 else 0 + ) + diagonals = [ + self.tiles[upper_right.x][upper_right.y], + self.tiles[upper_left.x][upper_left.y], + self.tiles[bottom_right.x][bottom_right.y], + self.tiles[bottom_left.x][bottom_left.y] + ] + return diagonals + def get_all_racks(self): + """:return list of Tile objects""" + racks = [] + for x in self.tiles: + # row_racks = [t for t in self.tiles[x] if t.category.name == 'Rack'] + for y in x: + if y.category.name == 'Rack': + racks.append(y) + return racks + + def place_packages(self, no_of_packages): packages = [] for i in range(no_of_packages):