Skip to content
Snippets Groups Projects
counters.py 6.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • Dominik Battefeld's avatar
    Dominik Battefeld committed
    import logging
    
    from typing import TYPE_CHECKING, Optional
    
    
    if TYPE_CHECKING:
        from overcooked_simulator.overcooked_environment import GameScore
    
    import numpy.typing as npt
    
    from overcooked_simulator.game_items import (
        CuttableItem,
    
    Dominik Battefeld's avatar
    Dominik Battefeld committed
    log = logging.getLogger(__name__)
    
    
    Fabian Heinrich's avatar
    Fabian Heinrich committed
        """Simple class for a counter at a specified position (center of counter). Can hold things on top."""
    
    
        def __init__(self, pos: npt.NDArray[float], occupied_by: Optional[Item] = None):
    
            self.pos: npt.NDArray[float] = pos
    
            self.occupied_by: Optional[Item] = occupied_by
    
            """Gets called upon a player performing the pickup action. If the counter can give something to
            the player, it does so. In the standard counter this is when an item is on the counter.
    
            Returns: The item which the counter is occupied by. None if nothing is there.
    
            """
    
            if on_hands:
                if self.occupied_by:
                    occupied_by = self.occupied_by
                    self.occupied_by = None
                    return occupied_by
                return None
            if self.occupied_by and isinstance(self.occupied_by, CookingEquipment):
                return self.occupied_by.release()
            occupied_by = self.occupied_by
    
        def can_drop_off(self, item: Item) -> bool:
    
            """Checks whether an item by the player can be dropped of. More relevant for example with
            ingredient dispensers, which should always be occupied and cannot take an item.
    
                item: The item for which to check, if it can be placed on the counter.
    
            Returns: True if the item can be placed on the counter, False if not.
    
            return self.occupied_by is None or self.occupied_by.can_combine(item)
    
        def drop_off(self, item: Item) -> Item | None:
    
            Returns: TODO Return information, whether the score is affected (Serving Window?)
    
            if self.occupied_by is None:
                self.occupied_by = item
    
            elif self.occupied_by.can_combine(item):
    
    
        def interact_start(self):
            """Starts an interaction by the player. Nothing happens for the standard counter."""
            pass
    
        def interact_stop(self):
            """Stops an interaction by the player. Nothing happens for the standard counter."""
            pass
    
    
            return (
                f"{self.__class__.__name__}(pos={self.pos},occupied_by={self.occupied_by})"
            )
    
    
    
    class CuttingBoard(Counter):
    
            self.progressing = False
            super().__init__(pos)
    
        def progress(self):
            """Called by environment step function for time progression"""
            if self.progressing:
                if isinstance(self.occupied_by, CuttableItem):
                    self.occupied_by.progress()
    
        def start_progress(self):
    
            """Starts the cutting process."""
    
            self.progressing = True
    
        def pause_progress(self):
    
            """Pauses the cutting process"""
    
            self.progressing = False
    
        def interact_start(self):
    
            """Handles player interaction, starting to hold key down."""
    
            self.start_progress()
    
        def interact_stop(self):
    
            """Handles player interaction, stopping to hold key down."""
    
            self.pause_progress()
    
    
        def __init__(self, pos, game_score: GameScore):
            self.game_score = game_score
    
        def drop_off(self, item) -> Item | None:
    
    Dominik Battefeld's avatar
    Dominik Battefeld committed
            log.debug(f"Drop off {item}")
    
    Florian Schröder's avatar
    Florian Schröder committed
            if (
                isinstance(item, CookingEquipment)
                and "Plate" in item.name
                and item.content is not None
            ):
    
                if isinstance(item.content, Meal) and item.content.progressed_steps:
    
    Florian Schröder's avatar
    Florian Schröder committed
                    return item.content.finished
    
    
                # TODO: Salad can be always be served. Check, if all needed parts are present.
                if item.content.name in ["Salad"]:
    
    Fabian Heinrich's avatar
    Fabian Heinrich committed
                    return True
            return False
    
        def can_drop_off(self, item: Item) -> bool:
    
    class Dispenser(Counter):
        def __init__(self, pos, dispensing):
            self.dispensing = dispensing
    
    Fabian Heinrich's avatar
    Fabian Heinrich committed
            super().__init__(
                pos,
    
                self.dispensing.create_item(),
    
    Fabian Heinrich's avatar
    Fabian Heinrich committed
            )
    
            return_this = self.occupied_by
            self.occupied_by = self.dispensing.create_item()
            return return_this
    
        def drop_off(self, item: Item) -> Item | None:
    
            if self.occupied_by.can_combine(item):
                return self.occupied_by.combine(item)
    
        def can_drop_off(self, item: Item) -> bool:
    
            return self.occupied_by.can_combine(item)
    
        def __repr__(self):
    
            return f"{self.dispensing.name}Dispenser"
    
        def drop_off(self, item: Item) -> Item | None:
    
            if isinstance(item, CookingEquipment):
                item.content = None
                return item
    
        def can_drop_off(self, item: Item) -> bool:
    
    
    class Stove(Counter):
    
        def can_drop_off(self, item: Item) -> bool:
            if self.occupied_by is None:
                return isinstance(item, CookingEquipment) and item.name in ["Pot", "Pan"]
            else:
                return self.occupied_by.can_combine(item)
    
    
        def progress(self):
            """Called by environment step function for time progression"""
    
            if (
                self.occupied_by
                and isinstance(self.occupied_by, CookingEquipment)
                and self.occupied_by.can_progress()
            ):