Skip to content
Snippets Groups Projects
Commit 984851bc authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

Merge branch 'main' into 88-second-window

parents 2dbface9 a61fe9d0
No related branches found
No related tags found
1 merge request!55Resolve "Second window"
...@@ -623,7 +623,7 @@ class Trashcan(Counter): ...@@ -623,7 +623,7 @@ class Trashcan(Counter):
return None return None
def can_drop_off(self, item: Item) -> bool: def can_drop_off(self, item: Item) -> bool:
return True return item.name != "Extinguisher"
class CookingCounter(Counter): class CookingCounter(Counter):
......
...@@ -88,7 +88,8 @@ player_config: ...@@ -88,7 +88,8 @@ player_config:
player_speed_units_per_seconds: 6 player_speed_units_per_seconds: 6
interaction_range: 1.6 interaction_range: 1.6
restricted_view: False restricted_view: False
view_angle: 95 view_angle: 60
view_range: 3 # in grid units, can be "null"
effect_manager: effect_manager:
FireManager: FireManager:
......
...@@ -153,20 +153,23 @@ class Visualizer: ...@@ -153,20 +153,23 @@ class Visualizer:
direction = pygame.math.Vector2(state["view_restriction"]["direction"]) direction = pygame.math.Vector2(state["view_restriction"]["direction"])
pos = pygame.math.Vector2(state["view_restriction"]["position"]) pos = pygame.math.Vector2(state["view_restriction"]["position"])
angle = state["view_restriction"]["angle"] / 2 angle = state["view_restriction"]["angle"] / 2
range = state["view_restriction"]["range"]
pos = pos * grid_size + pygame.math.Vector2([grid_size / 2, grid_size / 2]) pos = pos * grid_size + pygame.math.Vector2([grid_size / 2, grid_size / 2])
rect_scale = max(width, height) rect_scale = max(width, height)
# rect_scale = 2 * grid_size
left_beam = pos + (direction.rotate(angle) * rect_scale * 2) left_beam = pos + (direction.rotate(angle) * rect_scale * 2)
right_beam = pos + (direction.rotate(-angle) * rect_scale * 2) right_beam = pos + (direction.rotate(-angle) * rect_scale * 2)
offset_front = direction * grid_size * 0.2
pygame.draw.polygon( pygame.draw.polygon(
screen, screen,
colors["black"], colors["black"],
( (
pos - (direction * grid_size * 0.6), pos - offset_front,
left_beam - (direction * grid_size * 0.6), left_beam - offset_front,
left_beam + (direction.rotate(90) * rect_scale), left_beam + (direction.rotate(90) * rect_scale),
pos pos
- (direction * rect_scale * 2) - (direction * rect_scale * 2)
...@@ -175,9 +178,28 @@ class Visualizer: ...@@ -175,9 +178,28 @@ class Visualizer:
- (direction * rect_scale * 2) - (direction * rect_scale * 2)
+ (direction.rotate(-90) * rect_scale), + (direction.rotate(-90) * rect_scale),
right_beam + (direction.rotate(-90) * rect_scale), right_beam + (direction.rotate(-90) * rect_scale),
right_beam - (direction * grid_size * 0.6), right_beam - offset_front,
), ),
) )
if range:
pygame.draw.polygon(
screen,
colors["black"],
(
pos
+ (direction.rotate(90) * rect_scale)
+ (direction * range * grid_size),
pos
+ (direction.rotate(-90) * rect_scale)
+ (direction * range * grid_size),
pos
+ (direction.rotate(-90) * rect_scale)
+ (direction * rect_scale),
pos
+ (direction.rotate(90) * rect_scale)
+ (direction * rect_scale),
),
)
def draw_background( def draw_background(
self, surface: pygame.Surface, width: int, height: int, grid_size: int self, surface: pygame.Surface, width: int, height: int, grid_size: int
......
...@@ -167,6 +167,9 @@ class Environment: ...@@ -167,6 +167,9 @@ class Environment:
self.player_view_angle = self.environment_config["player_config"][ self.player_view_angle = self.environment_config["player_config"][
"view_angle" "view_angle"
] ]
self.player_view_range = self.environment_config["player_config"][
"view_range"
]
self.extra_setup_functions() self.extra_setup_functions()
...@@ -236,8 +239,6 @@ class Environment: ...@@ -236,8 +239,6 @@ class Environment:
) = self.parse_layout_file() ) = self.parse_layout_file()
self.hook(LAYOUT_FILE_PARSED) self.hook(LAYOUT_FILE_PARSED)
self.counter_positions = np.array([c.pos for c in self.counters])
self.world_borders = np.array( self.world_borders = np.array(
[[-0.5, self.kitchen_width - 0.5], [-0.5, self.kitchen_height - 0.5]], [[-0.5, self.kitchen_width - 0.5], [-0.5, self.kitchen_height - 0.5]],
dtype=float, dtype=float,
...@@ -247,6 +248,9 @@ class Environment: ...@@ -247,6 +248,9 @@ class Environment:
"player_speed_units_per_seconds" "player_speed_units_per_seconds"
] ]
self.player_radius = self.environment_config["player_config"]["radius"] self.player_radius = self.environment_config["player_config"]["radius"]
self.player_interaction_range = self.environment_config["player_config"][
"interaction_range"
]
progress_counter_classes = list( progress_counter_classes = list(
filter( filter(
...@@ -266,6 +270,8 @@ class Environment: ...@@ -266,6 +270,8 @@ class Environment:
) )
"""Counters that needs to be called in the step function via the `progress` method.""" """Counters that needs to be called in the step function via the `progress` method."""
self.counter_positions = np.array([c.pos for c in self.counters])
self.order_and_score.create_init_orders(self.env_time) self.order_and_score.create_init_orders(self.env_time)
self.start_time = self.env_time self.start_time = self.env_time
"""The relative env time when it started.""" """The relative env time when it started."""
...@@ -287,6 +293,27 @@ class Environment: ...@@ -287,6 +293,27 @@ class Environment:
env_start_time_worldtime=datetime.now(), env_start_time_worldtime=datetime.now(),
) )
def overwrite_counters(self, counters):
self.counters = counters
self.counter_positions = np.array([c.pos for c in self.counters])
progress_counter_classes = list(
filter(
lambda cl: hasattr(cl, "progress"),
dict(
inspect.getmembers(
sys.modules["overcooked_simulator.counters"], inspect.isclass
)
).values(),
)
)
self.progressing_counters = list(
filter(
lambda c: c.__class__ in progress_counter_classes,
self.counters,
)
)
@property @property
def game_ended(self) -> bool: def game_ended(self) -> bool:
"""Whether the game is over or not based on the calculated `Environment.env_time_end`""" """Whether the game is over or not based on the calculated `Environment.env_time_end`"""
...@@ -663,8 +690,20 @@ class Environment: ...@@ -663,8 +690,20 @@ class Environment:
for idx, p in enumerate(self.players.values()): for idx, p in enumerate(self.players.values()):
if not (new_positions[idx] == player_positions[idx]).all(): if not (new_positions[idx] == player_positions[idx]).all():
p.turn(player_movement_vectors[idx]) p.pos = new_positions[idx]
p.move_abs(new_positions[idx]) p.perform_interact_stop()
p.turn(player_movement_vectors[idx])
facing_distances = np.linalg.norm(
p.facing_point - self.counter_positions, axis=1
)
closest_counter = self.counters[facing_distances.argmin()]
p.current_nearest_counter = (
closest_counter
if facing_distances.min() <= self.player_interaction_range
else None
)
def add_player(self, player_name: str, pos: npt.NDArray = None): def add_player(self, player_name: str, pos: npt.NDArray = None):
"""Add a player to the environment. """Add a player to the environment.
...@@ -780,6 +819,7 @@ class Environment: ...@@ -780,6 +819,7 @@ class Environment:
"position": self.players[player_id].pos.tolist(), "position": self.players[player_id].pos.tolist(),
"angle": self.player_view_angle, "angle": self.player_view_angle,
"counter_mask": None, "counter_mask": None,
"range": self.player_view_range,
} }
if self.player_view_restricted if self.player_view_restricted
else None, else None,
......
...@@ -37,6 +37,9 @@ class PlayerConfig: ...@@ -37,6 +37,9 @@ class PlayerConfig:
"""Whether or not the player can see the entire map at once or just a view frustrum.""" """Whether or not the player can see the entire map at once or just a view frustrum."""
view_angle: int | None = None view_angle: int | None = None
"""Angle of the players view if restricted.""" """Angle of the players view if restricted."""
view_range: float | None = None
"""Range of the players view if restricted. In grid units."""
class Player: class Player:
"""Class representing a player in the game environment. A player consists of a name, their position and what """Class representing a player in the game environment. A player consists of a name, their position and what
...@@ -88,19 +91,7 @@ class Player: ...@@ -88,19 +91,7 @@ class Player:
function of the environment""" function of the environment"""
self.current_movement = move_vector self.current_movement = move_vector
self.movement_until = move_until self.movement_until = move_until
self.perform_interact_stop()
def move(self, movement: npt.NDArray[float]):
"""Moves the player position by the given movement vector.
A unit direction vector multiplied by move_dist is added to the player position.
Args:
movement: 2D-Vector of length 1
"""
if self.interacting and np.any(movement):
self.perform_interact_stop()
self.pos += movement
if np.linalg.norm(movement) != 0:
self.turn(movement)
def move_abs(self, new_pos: npt.NDArray[float]): def move_abs(self, new_pos: npt.NDArray[float]):
"""Overwrites the player location by the new_pos 2d-vector. Absolute movement. """Overwrites the player location by the new_pos 2d-vector. Absolute movement.
...@@ -165,7 +156,7 @@ class Player: ...@@ -165,7 +156,7 @@ class Player:
self.holding.combine(returned_by_counter) self.holding.combine(returned_by_counter)
log.debug( log.debug(
f"Self: {self.holding}, {counter.__class__.__name__}: {counter.occupied_by}" f"Self: {self.holding.__class__.__name__}: {self.holding}, {counter.__class__.__name__}: {counter.occupied_by}"
) )
# if isinstance(self.holding, Plate): # if isinstance(self.holding, Plate):
# log.debug(self.holding.clean) # log.debug(self.holding.clean)
......
...@@ -71,6 +71,7 @@ class ViewRestriction(BaseModel): ...@@ -71,6 +71,7 @@ class ViewRestriction(BaseModel):
position: list[float] position: list[float]
angle: int # degrees angle: int # degrees
counter_mask: None | list[bool] counter_mask: None | list[bool]
range: float | None
class StateRepresentation(BaseModel): class StateRepresentation(BaseModel):
......
...@@ -124,7 +124,7 @@ def test_player_reach(env_config, layout_empty_config, item_info): ...@@ -124,7 +124,7 @@ def test_player_reach(env_config, layout_empty_config, item_info):
counter_pos = np.array([2, 2]) counter_pos = np.array([2, 2])
counter = Counter(pos=counter_pos, hook=Hooks(env)) counter = Counter(pos=counter_pos, hook=Hooks(env))
env.counters = [counter] env.overwrite_counters([counter])
env.add_player("1", np.array([2, 4])) env.add_player("1", np.array([2, 4]))
env.player_movement_speed = 1 env.player_movement_speed = 1
player = env.players["1"] player = env.players["1"]
...@@ -144,7 +144,7 @@ def test_pickup(env_config, layout_config, item_info): ...@@ -144,7 +144,7 @@ def test_pickup(env_config, layout_config, item_info):
counter_pos = np.array([2, 2]) counter_pos = np.array([2, 2])
counter = Counter(pos=counter_pos, hook=Hooks(env)) counter = Counter(pos=counter_pos, hook=Hooks(env))
counter.occupied_by = Item(name="Tomato", item_info=None) counter.occupied_by = Item(name="Tomato", item_info=None)
env.counters = [counter] env.overwrite_counters([counter])
env.add_player("1", np.array([2, 3])) env.add_player("1", np.array([2, 3]))
player = env.players["1"] player = env.players["1"]
......
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