Skip to content
Snippets Groups Projects
Commit edf18fd8 authored by Florian Schröder's avatar Florian Schröder
Browse files

Implement Fog of War feature in game environment

Added a Fog of War option that if activated, restricts the player's view in the game environment. This feature is an attribute in the game state, view_restriction, and incorporated into the get_json_state method to return player's viewing direction and angle if Fog of War is enabled. Additionally, added view restriction handling in the gui_2d_vis drawing method.
parent 274592dd
No related branches found
No related tags found
1 merge request!42Resolve "FOV of the players, fog-of-war like vision"
Pipeline #45103 passed
...@@ -235,7 +235,7 @@ class EnvironmentHandler: ...@@ -235,7 +235,7 @@ class EnvironmentHandler:
): ):
return self.envs[ return self.envs[
self.player_data[player_hash].env_id self.player_data[player_hash].env_id
].environment.get_json_state() ].environment.get_json_state(self.player_data[player_hash].player_id)
def pause_env(self, manager_id: str, env_id: str, reason: str): def pause_env(self, manager_id: str, env_id: str, reason: str):
"""Pause the specified environment. """Pause the specified environment.
......
...@@ -91,7 +91,11 @@ class Visualizer: ...@@ -91,7 +91,11 @@ class Visualizer:
grid_size, grid_size,
) )
print(state) if "view_restriction" in state:
direction = state["view_restriction"][0]
angel = state["view_restriction"][1]
# rotate direction vector in both direction with the angel
# draw 2 large rect which are rotated so that one edge is the viewing border
def draw_background(self, surface, width, height, grid_size): def draw_background(self, surface, width, height, grid_size):
"""Visualizes a game background.""" """Visualizes a game background."""
......
...@@ -31,6 +31,8 @@ from overcooked_simulator.utils import create_init_env_time, get_closest ...@@ -31,6 +31,8 @@ from overcooked_simulator.utils import create_init_env_time, get_closest
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
FOG_OF_WAR = True
class ActionType(Enum): class ActionType(Enum):
"""The 3 different types of valid actions. They can be extended via the `Action.action_data` attribute.""" """The 3 different types of valid actions. They can be extended via the `Action.action_data` attribute."""
...@@ -605,22 +607,30 @@ class Environment: ...@@ -605,22 +607,30 @@ class Environment:
"remaining_time": max(self.env_time_end - self.env_time, timedelta(0)), "remaining_time": max(self.env_time_end - self.env_time, timedelta(0)),
} }
def get_json_state(self, player_id: str = None): def get_json_state(self, player_id: str = None) -> str:
state = { if player_id in self.players:
"players": [p.to_dict() for p in self.players.values()], state = {
"counters": [c.to_dict() for c in self.counters], "players": [p.to_dict() for p in self.players.values()],
"kitchen": {"width": self.kitchen_width, "height": self.kitchen_height}, "counters": [c.to_dict() for c in self.counters],
"score": self.order_and_score.score, "kitchen": {"width": self.kitchen_width, "height": self.kitchen_height},
"orders": self.order_and_score.order_state(), "score": self.order_and_score.score,
"ended": self.game_ended, "orders": self.order_and_score.order_state(),
"env_time": self.env_time.isoformat(), "ended": self.game_ended,
"remaining_time": max( "env_time": self.env_time.isoformat(),
self.env_time_end - self.env_time, timedelta(0) "remaining_time": max(
).total_seconds(), self.env_time_end - self.env_time, timedelta(0)
} ).total_seconds(),
json_data = json.dumps(state) "view_restriction": [
assert StateRepresentation.model_validate_json(json_data=json_data) self.players[player_id].facing_direction.tolist(),
return json_data 40.0,
]
if FOG_OF_WAR
else None,
}
json_data = json.dumps(state)
assert StateRepresentation.model_validate_json(json_data=json_data)
return json_data
raise ValueError(f"No valid {player_id=}")
def reset_env_time(self): def reset_env_time(self):
"""Reset the env time to the initial time, defined by `create_init_env_time`.""" """Reset the env time to the initial time, defined by `create_init_env_time`."""
......
...@@ -64,6 +64,7 @@ class StateRepresentation(BaseModel): ...@@ -64,6 +64,7 @@ class StateRepresentation(BaseModel):
ended: bool ended: bool
env_time: datetime # isoformat str env_time: datetime # isoformat str
remaining_time: float remaining_time: float
view_restriction: None | list[list[float] | float]
def create_json_schema(): def create_json_schema():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment