From b161b61fe13cacd703125739b0e054c390bfdca8 Mon Sep 17 00:00:00 2001 From: fheinrich <fheinrich@techfak.uni-bielefeld.de> Date: Fri, 19 Jan 2024 12:55:29 +0100 Subject: [PATCH] Items on counter are visualized if not in deque --- .../game_content/player_config.yaml | 0 overcooked_simulator/game_server.py | 2 + .../gui_2d_vis/overcooked_gui.py | 101 +++++++----------- .../overcooked_environment.py | 11 ++ overcooked_simulator/websocket_connector.py | 0 5 files changed, 53 insertions(+), 61 deletions(-) delete mode 100644 overcooked_simulator/game_content/player_config.yaml delete mode 100644 overcooked_simulator/websocket_connector.py diff --git a/overcooked_simulator/game_content/player_config.yaml b/overcooked_simulator/game_content/player_config.yaml deleted file mode 100644 index e69de29b..00000000 diff --git a/overcooked_simulator/game_server.py b/overcooked_simulator/game_server.py index 0e027a71..4497a07c 100644 --- a/overcooked_simulator/game_server.py +++ b/overcooked_simulator/game_server.py @@ -103,7 +103,9 @@ def main(): for i in range(number_player): player_name = f"p{i}" simulator.register_player(player_name) + simulator.start() + print(simulator.get_state_simple_json()) connector = Connector(simulator) connector.start_connector() diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py index 94cce0d9..fc0dfcf5 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -24,7 +24,6 @@ from overcooked_simulator.gui_2d_vis.game_colors import BLUE from overcooked_simulator.gui_2d_vis.game_colors import colors, Color from overcooked_simulator.order import Order from overcooked_simulator.overcooked_environment import Action -from overcooked_simulator.simulation_runner import Simulator USE_PLAYER_COOK_SPRITES = True SHOW_INTERACTION_RANGE = False @@ -140,8 +139,8 @@ class PyGameGUI: self.menu_state = MenuStates.Start self.manager: pygame_gui.UIManager - def get_window_sizes(self, state: dir): - counter_positions = np.array([c.pos for c in state["counters"]]) + 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 if self.visualization_config["GameWindow"]["WhatIsFixed"] == "window_width": @@ -217,7 +216,6 @@ class PyGameGUI: move_vec = move_vec / np.linalg.norm(move_vec) action = Action(key_set.name, "movement", move_vec) - print("SENDING:", action) self.send_action(action, websocket) def handle_key_event(self, event, websocket): @@ -231,7 +229,7 @@ class PyGameGUI: for key_set in self.player_key_sets: if event.key == key_set.pickup_key and event.type == pygame.KEYDOWN: action = Action(key_set.name, "pickup", "pickup") - self.send_action(action) + self.send_action(action, websocket) if event.key == key_set.interact_key: if event.type == pygame.KEYDOWN: @@ -287,8 +285,6 @@ class PyGameGUI: Args: state: The game state returned by the environment. """ - print("Drawing players...") - print(state_dict["players"]) for p_idx, player_dict in enumerate(state_dict["players"]): # pos = player.pos * self.grid_size pos = np.array(player_dict["pos"]) * self.grid_size @@ -351,7 +347,6 @@ class PyGameGUI: if player_dict["holding"] is not None: holding_item_pos = pos + (20 * facing) - print("DRAWING HOLDING ITEM", player_dict["holding"]) self.draw_thing( holding_item_pos, self.visualization_config[player_dict["holding"]]["parts"], @@ -524,16 +519,20 @@ class PyGameGUI: pos, self.visualization_config[counter_type]["parts"], ) - # - # if counter.occupied_by is not None: - # # Multiple plates on plate return: - # if isinstance(counter.occupied_by, (list, deque)): - # with self.simulator.env.lock: - # for i, o in enumerate(counter.occupied_by): - # self.draw_item(np.abs([pos[0], pos[1] - (i * 3)]), o) - # # All other items: - # else: - # self.draw_item(pos, counter.occupied_by) + + occupied_by = counter_dict["occupied_by"] + if occupied_by is not None: + # Multiple plates on plate return: + # if isinstance(occupied_by, (list, deque)): + # with self.simulator.env.lock: + # for i, o in enumerate(occupied_by): + # self.draw_item(np.abs([pos[0], pos[1] - (i * 3)]), o) + # # All other items: + # else: + self.draw_thing( + pos, + self.visualization_config[occupied_by]["parts"], + ) def draw_counters(self, state): """Visualizes the counters in the environment. @@ -748,17 +747,17 @@ class PyGameGUI: object_id="#score_label", ) - def set_window_size(self, window_width, window_height, game_width, game_height): + def set_window_size(self): self.game_screen = pygame.Surface( ( - game_width, - game_height, + self.game_width, + self.game_height, ), ) self.main_window = pygame.display.set_mode( ( - window_width, - window_height, + self.window_width, + self.window_height, ) ) @@ -767,25 +766,9 @@ class PyGameGUI: self.window_height = self.min_height self.game_width = 0 self.game_height = 0 - self.set_window_size(self.min_width, self.min_height, 0, 0) + self.set_window_size() self.init_ui_elements() - def setup_simulation(self, config_path, layout_path): - self.simulator = Simulator(config_path, layout_path, 600) - number_player = len(self.player_names) - for i in range(number_player): - player_name = f"p{i}" - self.simulator.register_player(player_name) - self.simulator.start() - ( - self.window_width, - self.window_height, - self.game_width, - self.game_height, - self.grid_size, - ) = self.get_window_sizes(self.simulator.get_state()) - self.player_colors = self.create_player_colors() - def manage_button_visibility(self): match self.menu_state: case MenuStates.Start: @@ -821,38 +804,36 @@ class PyGameGUI: def start_button_press(self): self.menu_state = MenuStates.Game - layout_path = ( - ROOT_DIR - / "game_content" - / "layouts" - / self.layout_selection.selected_option - ) - config_path = ROOT_DIR / "game_content" / "environment_config.yaml" + with connect(self.websocket_url) as websocket: + state = self.request_state(websocket) - self.setup_simulation(config_path, layout_path) + ( + self.window_width, + self.window_height, + self.game_width, + self.game_height, + self.grid_size, + ) = self.get_window_sizes(state) - self.set_window_size(*(self.get_window_sizes(self.simulator.get_state()))[:-1]) + self.set_window_size() self.init_ui_elements() log.debug("Pressed start button") # self.api.set_sim(self.simulator) - def back_button_press(self): - self.menu_state = MenuStates.Start - self.reset_window_size() - self.simulator.stop() - log.debug("Pressed back button") + # def back_button_press(self): + # self.menu_state = MenuStates.Game + # # self.reset_window_size() + # log.debug("Pressed back button") def quit_button_press(self): - self.simulator.stop() self.running = False log.debug("Pressed quit button") def finished_button_press(self): self.menu_state = MenuStates.End self.reset_window_size() - self.simulator.stop() log.debug("Pressed finished button") def send_action(self, action: Action, websocket): @@ -912,7 +893,7 @@ class PyGameGUI: case self.start_button: self.start_button_press() case self.back_button: - self.back_button_press() + self.start_button_press() case self.finished_button: self.finished_button_press() case self.quit_button: @@ -931,7 +912,6 @@ class PyGameGUI: # drawing: - state = self.request_state(websocket) # state = self.simulator.get_state() self.main_window.fill( @@ -946,12 +926,13 @@ class PyGameGUI: pass case MenuStates.Game: + state = self.request_state(websocket) + self.draw_background() self.handle_keys(websocket) # state = self.simulator.get_state() - print(state) self.draw(state) if state["ended"]: @@ -977,11 +958,9 @@ class PyGameGUI: pygame.display.flip() except KeyboardInterrupt: - self.simulator.stop() pygame.quit() sys.exit() - self.simulator.stop() pygame.quit() sys.exit() diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py index 798a5e07..b8259ee2 100644 --- a/overcooked_simulator/overcooked_environment.py +++ b/overcooked_simulator/overcooked_environment.py @@ -4,6 +4,7 @@ import datetime import json import logging import random +from collections import deque from datetime import timedelta from pathlib import Path from threading import Lock @@ -613,12 +614,22 @@ class Environment: counter_type = f"{counter.dispensing.name}{counter.__class__.__name__}" else: counter_type = counter.__class__.__name__ + + if isinstance(counter.occupied_by, deque): + counter_item = None + elif counter.occupied_by: + counter_item = counter.occupied_by.name + else: + counter_item = None + counter_dict = { "pos": [float(counter.pos[0]), float(counter.pos[1])], "type": counter_type, + "occupied_by": counter_item, } counters.append(counter_dict) + print(self.game_ended) gamestate_dict = { "players": players, "counters": counters, diff --git a/overcooked_simulator/websocket_connector.py b/overcooked_simulator/websocket_connector.py deleted file mode 100644 index e69de29b..00000000 -- GitLab