diff --git a/overcooked_simulator/gui_2d_vis/drawing.py b/overcooked_simulator/gui_2d_vis/drawing.py index c257730d3be73f30bbacbcbc090a045da3b34eb1..f074f6545f9d0ea2e292095dfa88b01614db4cde 100644 --- a/overcooked_simulator/gui_2d_vis/drawing.py +++ b/overcooked_simulator/gui_2d_vis/drawing.py @@ -17,8 +17,8 @@ from overcooked_simulator.state_representation import ( ) USE_PLAYER_COOK_SPRITES = True -SHOW_INTERACTION_RANGE = False -SHOW_COUNTER_CENTERS = False +SHOW_INTERACTION_RANGE = True +SHOW_COUNTER_CENTERS = True def create_polygon(n, length): @@ -66,10 +66,10 @@ class Visualizer: self, screen, state, - width, - height, grid_size, ): + width = int(np.ceil(state["kitchen"]["width"] * grid_size)) + height = int(np.ceil(state["kitchen"]["height"] * grid_size)) self.draw_background( surface=screen, width=width, @@ -140,6 +140,7 @@ class Visualizer: for p_idx, player_dict in enumerate(state_dict["players"]): player_dict: PlayerState pos = np.array(player_dict["pos"]) * grid_size + pos += grid_size / 2 # correct for grid offset facing = np.array(player_dict["facing_direction"]) @@ -195,7 +196,7 @@ class Visualizer: ) if player_dict["holding"] is not None: - holding_item_pos = pos + (20 * facing) + holding_item_pos = pos + (20 * facing) - (grid_size / 2) self.draw_item( pos=holding_item_pos, grid_size=grid_size, @@ -204,13 +205,15 @@ class Visualizer: ) if player_dict["current_nearest_counter_pos"]: - pos = player_dict["current_nearest_counter_pos"] + nearest_pos = ( + np.array(player_dict["current_nearest_counter_pos"]) * grid_size + ) + pygame.draw.rect( screen, colors[self.player_colors[p_idx]], rect=pygame.Rect( - pos[0] * grid_size - (grid_size // 2), - pos[1] * grid_size - (grid_size // 2), + *nearest_pos, grid_size, grid_size, ), @@ -236,48 +239,37 @@ class Visualizer: """ for part in parts: part_type = part["type"] + + draw_pos = pos.copy() + if "center_offset" in part: + draw_pos += np.array(part["center_offset"]) * grid_size + + draw_pos += grid_size / 2 # Correction for grid offset match part_type: case "image": - if "center_offset" in part: - d = np.array(part["center_offset"]) * grid_size - pos = np.array(pos) - pos += d - self.draw_image( screen, part["path"], part["size"] * scale * grid_size, - pos, + draw_pos, ) case "rect": height = part["height"] * grid_size width = part["width"] * grid_size color = part["color"] - if "center_offset" in part: - dx, dy = np.array(part["center_offset"]) * grid_size - rect = pygame.Rect(pos[0] + dx, pos[1] + dy, height, width) - pygame.draw.rect(screen, color, rect) - else: - rect = pygame.Rect( - pos[0] - (height / 2), - pos[1] - (width / 2), - height, - width, - ) + draw_pos = pos.copy() + rect = pygame.Rect( + draw_pos[0] - (height / 2) + (grid_size / 2), + draw_pos[1] - (width / 2) + (grid_size / 2), + height, + width, + ) pygame.draw.rect(screen, color, rect) case "circle": radius = part["radius"] * grid_size color = colors[part["color"]] - if "center_offset" in part: - pygame.draw.circle( - screen, - color, - np.array(pos) - + (np.array(part["center_offset"]) * grid_size), - radius, - ) - else: - pygame.draw.circle(screen, color, pos, radius) + # draw_pos = pos.copy() + pygame.draw.circle(screen, color, draw_pos, radius) def draw_item( self, @@ -354,8 +346,8 @@ class Visualizer: bar_height = grid_size * 0.2 progress_width = percent * grid_size progress_bar = pygame.Rect( - pos[0] - (grid_size / 2), - pos[1] - (grid_size / 2) + grid_size - bar_height, + pos[0], + pos[1] + grid_size - bar_height, progress_width, bar_height, ) @@ -431,7 +423,10 @@ class Visualizer: ) if SHOW_COUNTER_CENTERS: pygame.draw.circle( - screen, colors["green1"], np.array(counter["pos"]) * grid_size, 3 + screen, + colors["green1"], + np.array(counter["pos"]) * grid_size + (grid_size / 2), + 3, ) def draw_orders( @@ -463,9 +458,7 @@ class Visualizer: ), width=2, ) - center = np.array(order_upper_left) + np.array( - [grid_size / 2, grid_size / 2] - ) + center = np.array(order_upper_left) self.draw_thing( pos=center, parts=config["Plate"]["parts"], diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py index 3a6d9ceaa4bb4658655d13f6d43d4b9c53272ff1..c9ae367d2cc7c8f452c25900a34df8030637f9b0 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -128,9 +128,8 @@ class PyGameGUI: self.vis.create_player_colors(len(self.player_names)) def get_window_sizes(self, state: dict): - counter_positions = np.array([c["pos"] for c in state["counters"]]) - kitchen_width = counter_positions[:, 0].max() + 0.5 - kitchen_height = counter_positions[:, 1].max() + 0.5 + kitchen_width = state["kitchen"]["width"] + kitchen_height = state["kitchen"]["height"] if self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_width": game_width = self.visualization_config["GameWindow"]["size"] kitchen_aspect_ratio = kitchen_height / kitchen_width @@ -349,8 +348,6 @@ class PyGameGUI: self.vis.draw_gamescreen( self.game_screen, state, - self.game_width, - self.game_height, self.grid_size, ) diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py index d3c27dc5b59482fbdd2098215b7163e720bfe434..51eaef1a8aec0f89495b761f27cca6a5c87bf3fe 100644 --- a/overcooked_simulator/overcooked_environment.py +++ b/overcooked_simulator/overcooked_environment.py @@ -281,23 +281,22 @@ class Environment: [counter_size/2, counter_size/2], counters are directly next to each other (of no empty space is specified in layout). """ - current_y: float = 0.5 + + starting_at: float = 0.0 + current_y: float = starting_at counters: list[Counter] = [] designated_player_positions: list[npt.NDArray] = [] free_positions: list[npt.NDArray] = [] - self.kitchen_width = 0 - if self.as_files: with open(self.layout_config, "r") as layout_file: lines = layout_file.readlines() else: lines = self.layout_config.split("\n") - self.kitchen_height = len(lines) for line in lines: line = line.replace("\n", "").replace(" ", "") # remove newline char - current_x = 0.5 + current_x: float = starting_at for character in line: character = character.capitalize() pos = np.array([current_x, current_y]) @@ -316,11 +315,13 @@ class Environment: free_positions.append(np.array([current_x, current_y])) current_x += 1 - if current_x > self.kitchen_width: - self.kitchen_width = current_x + # if current_x > self.kitchen_width: + # self.kitchen_width = current_x current_y += 1 - self.kitchen_width -= 0.5 + # self.kitchen_width -= 0.5 + self.kitchen_width: float = len(lines[0]) + self.kitchen_height = len(lines) self.counter_factory.post_counter_setup(counters) @@ -585,26 +586,27 @@ class Environment: counter.progress(passed_time=passed_time, now=self.env_time) self.order_and_score.progress(passed_time=passed_time, now=self.env_time) - def get_state(self): - """Get the current state of the game environment. The state here is accessible by the current python objects. - - Returns: Dict of lists of the current relevant game objects. - - """ - return { - "players": self.players, - "counters": self.counters, - "score": self.order_and_score.score, - "orders": self.order_and_score.open_orders, - "ended": self.game_ended, - "env_time": self.env_time, - "remaining_time": max(self.env_time_end - self.env_time, timedelta(0)), - } + # def get_state(self): + # """Get the current state of the game environment. The state here is accessible by the current python objects. + # + # Returns: Dict of lists of the current relevant game objects. + # + # """ + # return { + # "players": self.players, + # "counters": self.counters, + # "score": self.order_and_score.score, + # "orders": self.order_and_score.open_orders, + # "ended": self.game_ended, + # "env_time": self.env_time, + # "remaining_time": max(self.env_time_end - self.env_time, timedelta(0)), + # } def get_json_state(self, player_id: str = None): state = { "players": [p.to_dict() for p in self.players.values()], "counters": [c.to_dict() for c in self.counters], + "kitchen": {"width": self.kitchen_width, "height": self.kitchen_height}, "score": self.order_and_score.score, "orders": self.order_and_score.order_state(), "ended": self.game_ended, diff --git a/overcooked_simulator/state_representation.py b/overcooked_simulator/state_representation.py index e8ad5d17a79b4db29c2977934f0127c7a09c83f9..4fc0a137a097a81047c790c1a8741399c2ad220d 100644 --- a/overcooked_simulator/state_representation.py +++ b/overcooked_simulator/state_representation.py @@ -50,9 +50,15 @@ class PlayerState(TypedDict): current_nearest_counter_id: str | None +class KitchenInfo(BaseModel): + width: float + height: float + + class StateRepresentation(BaseModel): players: list[PlayerState] counters: list[CounterState] + kitchen: KitchenInfo score: float | int orders: list[OrderState] ended: bool