From 24740ea8e2e4ff03a896236f28cd59aa7c49dd7f Mon Sep 17 00:00:00 2001 From: fheinrich <fheinrich@techfak.uni-bielefeld.de> Date: Tue, 30 Jan 2024 14:44:46 +0100 Subject: [PATCH] Added sample json file for testing (could change/break in future), added saving images created from states. --- overcooked_simulator/gui_2d_vis/drawing.py | 37 +- .../gui_2d_vis/overcooked_gui.py | 1 + .../gui_2d_vis/sample_state.json | 545 ++++++++++++++++++ 3 files changed, 581 insertions(+), 2 deletions(-) create mode 100644 overcooked_simulator/gui_2d_vis/sample_state.json diff --git a/overcooked_simulator/gui_2d_vis/drawing.py b/overcooked_simulator/gui_2d_vis/drawing.py index d5caba6d..ec72975b 100644 --- a/overcooked_simulator/gui_2d_vis/drawing.py +++ b/overcooked_simulator/gui_2d_vis/drawing.py @@ -1,4 +1,5 @@ import colorsys +import json import math from datetime import datetime, timedelta from pathlib import Path @@ -6,6 +7,7 @@ from pathlib import Path import numpy as np import numpy.typing as npt import pygame +import yaml from scipy.spatial import KDTree from overcooked_simulator import ROOT_DIR @@ -17,8 +19,8 @@ from overcooked_simulator.state_representation import ( ) USE_PLAYER_COOK_SPRITES = True -SHOW_INTERACTION_RANGE = True -SHOW_COUNTER_CENTERS = True +SHOW_INTERACTION_RANGE = False +SHOW_COUNTER_CENTERS = False def create_polygon(n, length): @@ -88,6 +90,8 @@ class Visualizer: grid_size, ) + print(state) + def draw_background(self, surface, width, height, grid_size): """Visualizes a game background.""" block_size = grid_size // 2 # Set the size of the grid block @@ -496,3 +500,32 @@ class Visualizer: orders_height // 2, ] screen.blit(order_screen, orders_rect) + + def save_state_image( + self, grid_size: int, state: dict, filename: str | Path + ) -> None: + width = int(np.ceil(state["kitchen"]["width"] * grid_size)) + height = int(np.ceil(state["kitchen"]["height"] * grid_size)) + + flags = pygame.HIDDEN + screen = pygame.display.set_mode((width, height), flags=flags) + + self.draw_gamescreen(screen, state, grid_size) + pygame.image.save(screen, filename) + + +def save_screenshot(state: dict, config: dict, filename: str | Path) -> None: + viz = Visualizer(config) + viz.create_player_colors(len(state["players"])) + pygame.init() + pygame.font.init() + viz.save_state_image(grid_size=40, state=state, filename=filename) + + +if __name__ == "__main__": + with open(ROOT_DIR / "gui_2d_vis" / "visualization.yaml", "r") as conf_file: + viz_config = yaml.safe_load(conf_file) + with open(ROOT_DIR / "gui_2d_vis" / "sample_state.json", "r") as jsonf: + sample_state = json.load(jsonf) + screenshot_filename = ROOT_DIR / "gui_2d_vis" / "screenshot.jpg" + save_screenshot(sample_state, viz_config, screenshot_filename) diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py index 7922bc2f..0a361a5e 100644 --- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py +++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py @@ -715,6 +715,7 @@ def main(url: str, port: int, manager_ids: list[str]): gui.start_pygame() + if __name__ == "__main__": parser = argparse.ArgumentParser( prog="Overcooked Simulator 2D PyGame Visualization", diff --git a/overcooked_simulator/gui_2d_vis/sample_state.json b/overcooked_simulator/gui_2d_vis/sample_state.json new file mode 100644 index 00000000..ae3fb243 --- /dev/null +++ b/overcooked_simulator/gui_2d_vis/sample_state.json @@ -0,0 +1,545 @@ +{ + "players": [ + { + "id": "0", + "pos": [ + 3.0, + 4.0 + ], + "facing_direction": [ + 0, + 1 + ], + "holding": null, + "current_nearest_counter_pos": null, + "current_nearest_counter_id": null + }, + { + "id": "1", + "pos": [ + 9.0, + 4.0 + ], + "facing_direction": [ + 0, + 1 + ], + "holding": null, + "current_nearest_counter_pos": null, + "current_nearest_counter_id": null + } + ], + "counters": [ + { + "id": "b032a0fcf1c54828a7bf3afc189547a6", + "category": "Counter", + "type": "Counter", + "pos": [ + 0.0, + 0.0 + ], + "occupied_by": null + }, + { + "id": "7b46f1d45f1e463494940d19f47648d2", + "category": "Counter", + "type": "Stove", + "pos": [ + 1.0, + 0.0 + ], + "occupied_by": { + "id": "240eb5d4e4dd4215a0ab9fbaa93f02f5", + "category": "Cooking Equipment", + "type": "Pan", + "progress_percentage": 0.0, + "content_list": [], + "content_ready": null + } + }, + { + "id": "9675528d610c4a90b8cdea2f52203151", + "category": "Counter", + "type": "Stove", + "pos": [ + 2.0, + 0.0 + ], + "occupied_by": { + "id": "54124ff47f9a4b2abb6e2df96a9db919", + "category": "Cooking Equipment", + "type": "Pot", + "progress_percentage": 0.0, + "content_list": [], + "content_ready": null + } + }, + { + "id": "08d334733913488789fa7d195405c931", + "category": "Counter", + "type": "Counter", + "pos": [ + 3.0, + 0.0 + ], + "occupied_by": null + }, + { + "id": "abace5244d92463693c5d57cd70ab9da", + "category": "Counter", + "type": "DeepFryer", + "pos": [ + 4.0, + 0.0 + ], + "occupied_by": { + "id": "987ea1da780c44cabc06883f5c721414", + "category": "Cooking Equipment", + "type": "Basket", + "progress_percentage": 0.0, + "content_list": [], + "content_ready": null + } + }, + { + "id": "6fd0f625ee2b43609d10b9163c6fbfcf", + "category": "Counter", + "type": "Oven", + "pos": [ + 5.0, + 0.0 + ], + "occupied_by": { + "id": "a8609b417c0a4c1f8a0d10ef042f2df6", + "category": "Cooking Equipment", + "type": "Peel", + "progress_percentage": 0.0, + "content_list": [], + "content_ready": null + } + }, + { + "id": "d5e5f842e2fb490f9ac7b837cca583f2", + "category": "Counter", + "type": "Counter", + "pos": [ + 6.0, + 0.0 + ], + "occupied_by": null + }, + { + "id": "3b054bbd2d5f49a39fa7d1bf10801575", + "category": "Counter", + "type": "TomatoDispenser", + "pos": [ + 7.0, + 0.0 + ], + "occupied_by": { + "id": "8d431317da8a439ab25a69f592d7e30d", + "category": "Item", + "type": "Tomato", + "progress_percentage": 0.0 + } + }, + { + "id": "0b88cdca1637439db60152762f9c535c", + "category": "Counter", + "type": "OnionDispenser", + "pos": [ + 8.0, + 0.0 + ], + "occupied_by": { + "id": "c2143424f8424e33aee383396a00f50b", + "category": "Item", + "type": "Onion", + "progress_percentage": 0.0 + } + }, + { + "id": "7608477a75cd4e4db83add343f721b10", + "category": "Counter", + "type": "LettuceDispenser", + "pos": [ + 9.0, + 0.0 + ], + "occupied_by": { + "id": "0301dd77d05044ab80d7bc45808546e9", + "category": "Item", + "type": "Lettuce", + "progress_percentage": 0.0 + } + }, + { + "id": "7510d5c4af424d3d988de020a80a3c63", + "category": "Counter", + "type": "BunDispenser", + "pos": [ + 10.0, + 0.0 + ], + "occupied_by": { + "id": "8c2e04c5828447088f8553f52c849132", + "category": "Item", + "type": "Bun", + "progress_percentage": 0.0 + } + }, + { + "id": "b4a03b6cdc8a4a088f46ceb68eaa0eff", + "category": "Counter", + "type": "Counter", + "pos": [ + 11.0, + 0.0 + ], + "occupied_by": null + }, + { + "id": "9396b59b16834e4ca594bcd7fe91f74f", + "category": "Counter", + "type": "Counter", + "pos": [ + 0.0, + 1.0 + ], + "occupied_by": null + }, + { + "id": "1bf2e112845442e09ce33f2807981c3a", + "category": "Counter", + "type": "MeatDispenser", + "pos": [ + 11.0, + 1.0 + ], + "occupied_by": { + "id": "d723a509ef6740538c42d0f491c2cdac", + "category": "Item", + "type": "Meat", + "progress_percentage": 0.0 + } + }, + { + "id": "fa6e820423ca41a5874f12e71419cf2a", + "category": "Counter", + "type": "Counter", + "pos": [ + 0.0, + 2.0 + ], + "occupied_by": null + }, + { + "id": "9525e23ec15445bb86962ead0b6f1076", + "category": "Counter", + "type": "PotatoDispenser", + "pos": [ + 11.0, + 2.0 + ], + "occupied_by": { + "id": "5f52f039c4f549418ee0be66ca98bee7", + "category": "Item", + "type": "Potato", + "progress_percentage": 0.0 + } + }, + { + "id": "638e272aeaa14bf8b6287a8a05a27ab8", + "category": "Counter", + "type": "ServingWindow", + "pos": [ + 0.0, + 3.0 + ], + "occupied_by": null + }, + { + "id": "5df4d8b89e01407facfb309c253d9b61", + "category": "Counter", + "type": "FishDispenser", + "pos": [ + 11.0, + 3.0 + ], + "occupied_by": { + "id": "f5f4ac928e4845b7a1b31d84861d1f49", + "category": "Item", + "type": "Fish", + "progress_percentage": 0.0 + } + }, + { + "id": "b1ab927ccbe140988bca5fe51694c299", + "category": "Counter", + "type": "Counter", + "pos": [ + 0.0, + 4.0 + ], + "occupied_by": null + }, + { + "id": "e3592afac20e4247b15641eae1818366", + "category": "Counter", + "type": "DoughDispenser", + "pos": [ + 11.0, + 4.0 + ], + "occupied_by": { + "id": "b7d7d308359849e6bbd2fe7baa6d87a8", + "category": "Item", + "type": "Dough", + "progress_percentage": 0.0 + } + }, + { + "id": "9d4a6e4726094f49873888bc74faf530", + "category": "Counter", + "type": "CuttingBoard", + "pos": [ + 0.0, + 5.0 + ], + "occupied_by": null, + "progressing": false + }, + { + "id": "c436371aeab84e4ab47454b89a3f4cb2", + "category": "Counter", + "type": "CheeseDispenser", + "pos": [ + 11.0, + 5.0 + ], + "occupied_by": { + "id": "e2fd82e3fab84dc59ab8b6c6c7963379", + "category": "Item", + "type": "Cheese", + "progress_percentage": 0.0 + } + }, + { + "id": "2ca7c251080c404787ef46e08e574290", + "category": "Counter", + "type": "CuttingBoard", + "pos": [ + 0.0, + 6.0 + ], + "occupied_by": null, + "progressing": false + }, + { + "id": "9c824c5009814894918c17ea951e34b5", + "category": "Counter", + "type": "SausageDispenser", + "pos": [ + 11.0, + 6.0 + ], + "occupied_by": { + "id": "d6d343968b9b4175a4e621f1febaaf1f", + "category": "Item", + "type": "Sausage", + "progress_percentage": 0.0 + } + }, + { + "id": "2c13a76d110749b39ff62866c658ae3e", + "category": "Counter", + "type": "Counter", + "pos": [ + 0.0, + 7.0 + ], + "occupied_by": null + }, + { + "id": "14b547181dd346daac28467b07277a3d", + "category": "Counter", + "type": "Counter", + "pos": [ + 11.0, + 7.0 + ], + "occupied_by": null + }, + { + "id": "188db4c4275f4ba7835989333b252b26", + "category": "Counter", + "type": "Counter", + "pos": [ + 0.0, + 8.0 + ], + "occupied_by": null + }, + { + "id": "f0a4790c290144cba2942705c3240a0f", + "category": "Counter", + "type": "PlateDispenser", + "pos": [ + 1.0, + 8.0 + ], + "occupied_by": [ + { + "id": "db41a69e880d4d64b535b265199d4148", + "category": "Cooking Equipment", + "type": "DirtyPlate", + "progress_percentage": 0.0, + "content_list": [], + "content_ready": null + }, + { + "id": "13fd4df8399d48c490091bb9ed3e1302", + "category": "Cooking Equipment", + "type": "DirtyPlate", + "progress_percentage": 0.0, + "content_list": [], + "content_ready": null + }, + { + "id": "5075019e20ed45c1aa7aa01ca5198c50", + "category": "Cooking Equipment", + "type": "Plate", + "progress_percentage": 0.0, + "content_list": [], + "content_ready": null + } + ] + }, + { + "id": "439b6269458c42f5b8be157da1bc0e22", + "category": "Counter", + "type": "Counter", + "pos": [ + 2.0, + 8.0 + ], + "occupied_by": null + }, + { + "id": "6a92b071a1db46bf80d82a413ea400d4", + "category": "Counter", + "type": "Sink", + "pos": [ + 3.0, + 8.0 + ], + "occupied_by": [], + "progressing": false + }, + { + "id": "b3852e97d88f42d4b724d4fb9b0cf8d8", + "category": "Counter", + "type": "SinkAddon", + "pos": [ + 4.0, + 8.0 + ], + "occupied_by": [] + }, + { + "id": "9f13c6729dd647f39575d1654d67a094", + "category": "Counter", + "type": "Counter", + "pos": [ + 5.0, + 8.0 + ], + "occupied_by": null + }, + { + "id": "0dc1e47f4a6448cb9e457c0d998f25f6", + "category": "Counter", + "type": "Trashcan", + "pos": [ + 6.0, + 8.0 + ], + "occupied_by": null + }, + { + "id": "87e13494ebaf44318f3ea05ef9d137a6", + "category": "Counter", + "type": "Counter", + "pos": [ + 7.0, + 8.0 + ], + "occupied_by": null + }, + { + "id": "6b83337a91414633a0793dfa4086f442", + "category": "Counter", + "type": "Counter", + "pos": [ + 8.0, + 8.0 + ], + "occupied_by": null + }, + { + "id": "355025b2ec144d008264009761caaee7", + "category": "Counter", + "type": "Sink", + "pos": [ + 9.0, + 8.0 + ], + "occupied_by": [], + "progressing": false + }, + { + "id": "3a12e9b62b8c4473b7b84f307cf3de4b", + "category": "Counter", + "type": "SinkAddon", + "pos": [ + 10.0, + 8.0 + ], + "occupied_by": [] + }, + { + "id": "b3ce6a85df3d40c0b376a61c3346fd09", + "category": "Counter", + "type": "Counter", + "pos": [ + 11.0, + 8.0 + ], + "occupied_by": null + } + ], + "kitchen": { + "width": 12.0, + "height": 9.0 + }, + "score": 0, + "orders": [ + { + "id": "823ab5e3211840778261c405ff7df91b", + "category": "Order", + "meal": "FriedFish", + "start_time": "2000-01-01T00:00:00", + "max_duration": 47.371306 + }, + { + "id": "2cb614440155426cae04b212f8f2729f", + "category": "Order", + "meal": "FriedFish", + "start_time": "2000-01-01T00:00:00", + "max_duration": 53.237394 + } + ], + "ended": false, + "env_time": "2000-01-01T00:00:00.675141", + "remaining_time": 299.324859 +} -- GitLab