Skip to content

context

GameContext #

A class recording the current game context.

Source code in cogip/tools/planner/context.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class GameContext(metaclass=Singleton):
    """
    A class recording the current game context.
    """

    def __init__(self):
        from .properties import Properties

        self.properties = Properties()
        self.game_duration: int = 90 if self.properties.robot_id == 1 else 100
        self.minimum_score: int = 0
        self.camp = Camp()
        self.avoidance_strategy = AvoidanceStrategy.AvoidanceCpp
        self.reset()

    @property
    def table(self) -> Table:
        """
        Selected table.
        """
        return tables[self.properties.table]

    def reset(self):
        """
        Reset the context.
        """
        self.playing = False
        self.score = self.minimum_score
        self.countdown = self.game_duration
        self.create_start_poses()
        self.create_artifacts()
        self.create_fixed_obstacles()
        self.create_actuators_states()

    @property
    def default_controller(self) -> ControllerEnum:
        match self.properties.strategy:
            case actions.Strategy.PidAngularSpeedTest:
                return ControllerEnum.ANGULAR_SPEED_TEST
            case actions.Strategy.PidLinearSpeedTest:
                return ControllerEnum.LINEAR_SPEED_TEST
            case _:
                return ControllerEnum.QUADPID

    def get_start_pose(self, n: StartPosition) -> Pose:
        """
        Define the possible start positions.
        Default positions for blue camp.
        """
        return self.start_poses.get(n, AdaptedPose())

    def create_start_poses(self):
        self.start_poses = {
            StartPosition.Bottom: AdaptedPose(
                x=-550 - self.properties.robot_length / 2,
                y=-100 - self.properties.robot_width / 2,
                O=0,
            ),
            StartPosition.Top: AdaptedPose(
                x=550 + self.properties.robot_length / 2,
                y=-900 - self.properties.robot_width / 2,
                O=180,
            ),
            StartPosition.Opposite: AdaptedPose(
                x=-350 + self.properties.robot_width / 2,
                y=1050 + self.properties.robot_length / 2,
                O=-90,
            ),
            StartPosition.PAMI2: AdaptedPose(
                x=550 + self.properties.robot_width * 0.5,
                y=-1350 - self.properties.robot_length / 2,
                O=90,
            ),
            StartPosition.PAMI3: AdaptedPose(
                x=550 + self.properties.robot_width * 1.5,
                y=-1350 - self.properties.robot_length / 2,
                O=90,
            ),
            StartPosition.PAMI4: AdaptedPose(
                x=550 + self.properties.robot_width * 2.5,
                y=-1350 - self.properties.robot_length / 2,
                O=90,
            ),
            StartPosition.PAMI5: AdaptedPose(
                x=550 + self.properties.robot_width * 3.5,
                y=-1350 - self.properties.robot_length / 2,
                O=90,
            ),
        }

        # Adapt poses for training table
        if self.properties.table == TableEnum.Training:
            self.start_poses[StartPosition.Top].x -= 1000
            self.start_poses[StartPosition.PAMI2].x -= 1000
            self.start_poses[StartPosition.PAMI3].x -= 1000
            self.start_poses[StartPosition.PAMI4].x -= 1000
            self.start_poses[StartPosition.PAMI5].x -= 1000

    def get_available_start_poses(self) -> list[StartPosition]:
        """
        Get start poses available depending on camp and table.
        """
        start_pose_indices = []
        for p in StartPosition:
            if self.properties.robot_id == 1 and p not in [
                StartPosition.Top,
                StartPosition.Bottom,
                StartPosition.Opposite,
            ]:
                continue
            pose = self.get_start_pose(p)
            if self.table.contains(pose):
                start_pose_indices.append(p)
        return start_pose_indices

    def create_artifacts(self):
        # Positions are related to the default camp blue.
        self.construction_areas: dict[ConstructionAreaID, ConstructionArea] = {}
        self.opponent_construction_areas: dict[ConstructionAreaID, ConstructionArea] = {}
        self.tribunes: dict[TribuneID, Tribune] = {}

        # Construction areas
        for id, area in construction_area_positions.items():
            match id:
                case ConstructionAreaID.LocalBottomSmall | ConstructionAreaID.OppositeBottomSmall:
                    ConstructionAreaClass = ConstructionAreaSmall
                case _:
                    ConstructionAreaClass = ConstructionAreaLarge
            adapted_pose = AdaptedPose(**area.model_dump())
            self.construction_areas[id] = ConstructionAreaClass(**adapted_pose.model_dump(), id=id, enabled=False)
            self.opponent_construction_areas[id] = ConstructionAreaClass(
                x=adapted_pose.x,
                y=-adapted_pose.y,
                O=-adapted_pose.O,
                id=id,
            )

        for id, tribune in tribune_positions.items():
            adapted_pose = AdaptedPose(**tribune.model_dump())
            self.tribunes[id] = Tribune(**adapted_pose.model_dump(), id=id)

        if self.properties.table == TableEnum.Training:
            self.opponent_construction_areas[ConstructionAreaID.OppositeCenterLarge].enabled = False

    def create_fixed_obstacles(self):
        # Positions are related to the default camp blue.
        self.fixed_obstacles: list[DynObstacleRect] = []

        # Opponent starting area, ramp and scene.
        pose = AdaptedPose(x=775, y=750)
        self.fixed_obstacles += [DynObstacleRect(x=pose.x, y=pose.y, angle=0, length_x=450, length_y=1500)]

        # Ramp and scene except for robot ID 5, the superstar.
        if self.properties.robot_id != 5:
            # Ramp
            pose = AdaptedPose(x=900, y=-650)
            self.fixed_obstacles += [DynObstacleRect(x=pose.x, y=pose.y, angle=0, length_x=200, length_y=400)]

            # Scene
            pose = AdaptedPose(x=825, y=-225)
            self.fixed_obstacles += [DynObstacleRect(x=pose.x, y=pose.y, angle=0, length_x=450, length_y=450)]

        # PAMIs starting area for robot ID 1, the main robot.
        if self.properties.robot_id == 1:
            pose = AdaptedPose(x=825, y=-1425)
            self.fixed_obstacles += [DynObstacleRect(x=pose.x, y=pose.y, angle=0, length_x=450, length_y=150)]

    def create_actuators_states(self):
        self.positional_actuator_states: dict[PositionalActuatorEnum, PositionalActuator] = {}
        self.bool_sensor_states: dict[BoolSensorEnum, BoolSensor] = {id: BoolSensor(id=id) for id in BoolSensorEnum}
        self.emulated_actuator_states: set[PositionalActuatorEnum] = {}

table: Table property #

Selected table.

get_available_start_poses() #

Get start poses available depending on camp and table.

Source code in cogip/tools/planner/context.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def get_available_start_poses(self) -> list[StartPosition]:
    """
    Get start poses available depending on camp and table.
    """
    start_pose_indices = []
    for p in StartPosition:
        if self.properties.robot_id == 1 and p not in [
            StartPosition.Top,
            StartPosition.Bottom,
            StartPosition.Opposite,
        ]:
            continue
        pose = self.get_start_pose(p)
        if self.table.contains(pose):
            start_pose_indices.append(p)
    return start_pose_indices

get_start_pose(n) #

Define the possible start positions. Default positions for blue camp.

Source code in cogip/tools/planner/context.py
72
73
74
75
76
77
def get_start_pose(self, n: StartPosition) -> Pose:
    """
    Define the possible start positions.
    Default positions for blue camp.
    """
    return self.start_poses.get(n, AdaptedPose())

reset() #

Reset the context.

Source code in cogip/tools/planner/context.py
50
51
52
53
54
55
56
57
58
59
60
def reset(self):
    """
    Reset the context.
    """
    self.playing = False
    self.score = self.minimum_score
    self.countdown = self.game_duration
    self.create_start_poses()
    self.create_artifacts()
    self.create_fixed_obstacles()
    self.create_actuators_states()