From d2e7af03c02d8b50fec3056d2852ce122221198e Mon Sep 17 00:00:00 2001
From: fheinrich <fheinrich@techfak.uni-bielefeld.de>
Date: Tue, 16 Jan 2024 11:46:33 +0100
Subject: [PATCH] Added game time limit, property game_ended of
 overcooked_environment returns true when time limit is reached. GUI switches
 to game end screen when it is finished.

---
 .../game_content/environment_config.yaml         |  5 ++++-
 .../gui_2d_vis/overcooked_gui.py                 |  5 ++++-
 .../gui_2d_vis/visualization.yaml                |  4 ++--
 overcooked_simulator/overcooked_environment.py   | 16 +++++++++++++++-
 overcooked_simulator/simulation_runner.py        |  4 +++-
 5 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/overcooked_simulator/game_content/environment_config.yaml b/overcooked_simulator/game_content/environment_config.yaml
index 158a2229..48d23d2e 100644
--- a/overcooked_simulator/game_content/environment_config.yaml
+++ b/overcooked_simulator/game_content/environment_config.yaml
@@ -1,4 +1,7 @@
 plates:
   clean_plates: 3
   dirty_plates: 2
-  plate_delay: [ 5, 10 ]
\ No newline at end of file
+  plate_delay: [ 5, 10 ]
+
+game:
+  time_limit_seconds: 10.0
diff --git a/overcooked_simulator/gui_2d_vis/overcooked_gui.py b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
index 999eccfc..33990634 100644
--- a/overcooked_simulator/gui_2d_vis/overcooked_gui.py
+++ b/overcooked_simulator/gui_2d_vis/overcooked_gui.py
@@ -163,7 +163,7 @@ class PyGameGUI:
             game_height + (2 * self.screen_margin),
         )
 
-        return window_width, window_height, game_width, game_height, grid_size
+        return int(window_width), int(window_height), game_width, game_height, grid_size
 
     def create_player_colors(self) -> list[Color]:
         number_player = len(self.simulator.env.players)
@@ -735,6 +735,9 @@ class PyGameGUI:
                         self.handle_keys()
 
                         state = self.simulator.get_state()
+                        if state["ended"]:
+                            self.finished_button_press()
+                            self.manage_button_visibility()
                         self.draw(state)
 
                         game_screen_rect = self.game_screen.get_rect()
diff --git a/overcooked_simulator/gui_2d_vis/visualization.yaml b/overcooked_simulator/gui_2d_vis/visualization.yaml
index b4bfdc00..a1381661 100644
--- a/overcooked_simulator/gui_2d_vis/visualization.yaml
+++ b/overcooked_simulator/gui_2d_vis/visualization.yaml
@@ -1,8 +1,8 @@
 # colors: https://www.webucator.com/article/python-color-constants-module/
 
 GameWindow:
-  WhatIsFixed: window_height  # entweder grid oder window_width
-  size: 500
+  WhatIsFixed: grid  # grid or window_width or window_height
+  size: 40
   screen_margin: 100
   start_width: 600
   start_height: 600
diff --git a/overcooked_simulator/overcooked_environment.py b/overcooked_simulator/overcooked_environment.py
index 8f5adf96..64ad2252 100644
--- a/overcooked_simulator/overcooked_environment.py
+++ b/overcooked_simulator/overcooked_environment.py
@@ -134,6 +134,14 @@ class Environment:
         self.score: int = 0
 
         self.env_time = create_init_env_time()
+        self.env_time_end = self.env_time + timedelta(
+            seconds=environment_config["game"]["time_limit_seconds"]
+        )
+        log.debug(f"End time: {self.env_time_end}")
+
+    @property
+    def game_ended(self) -> bool:
+        return self.env_time >= self.env_time_end
 
     def load_item_info(self) -> dict[str, ItemInfo]:
         with open(self.item_info_path, "r") as file:
@@ -437,6 +445,7 @@ class Environment:
         and time limits.
         """
         self.env_time += passed_time
+
         with self.lock:
             for counter in self.counters:
                 if isinstance(counter, (CuttingBoard, Stove, Sink, PlateDispenser)):
@@ -448,7 +457,12 @@ class Environment:
         Returns: Dict of lists of the current relevant game objects.
 
         """
-        return {"players": self.players, "counters": self.counters, "score": self.score}
+        return {
+            "players": self.players,
+            "counters": self.counters,
+            "score": self.score,
+            "ended": self.game_ended,
+        }
 
     def get_state_json(self):
         """Get the current state of the game environment as a json-like nested dictionary.
diff --git a/overcooked_simulator/simulation_runner.py b/overcooked_simulator/simulation_runner.py
index 8ad41e28..f0a6e9c8 100644
--- a/overcooked_simulator/simulation_runner.py
+++ b/overcooked_simulator/simulation_runner.py
@@ -98,9 +98,11 @@ class Simulator(Thread):
 
         overslept_in_ns = 0
         self.env.reset_env_time()
+        last_step_start = time.time_ns()
         while not self.finished:
             step_start = time.time_ns()
-            self.step(timedelta(seconds=overslept_in_ns / 1_000_000_000))
+            self.step(timedelta(seconds=(step_start - last_step_start) / 1_000_000_000))
+            last_step_start = step_start
             step_duration = time.time_ns() - step_start
 
             time_to_sleep_ns = self.preferred_sleep_time_ns - (
-- 
GitLab