Skip to content

context

GameContext #

A class recording the current game context.

Source code in cogip/tools/planner/context.py
 23
 24
 25
 26
 27
 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
199
200
201
202
203
204
205
206
207
208
209
210
211
class GameContext:
    """
    A class recording the current game context.
    """

    def __init__(self, shared_properties: SharedProperties, initialize: bool = True):
        self.shared_properties = shared_properties
        if initialize:
            self.minimum_score: int = 0
            self.game_duration: int = 100
            self.score = self.minimum_score
            self.tribunes_in_robot = 0
            self.construction_areas: dict[ConstructionAreaID, ConstructionArea] = {}
            self.opponent_construction_areas: dict[ConstructionAreaID, ConstructionArea] = {}
            self.tribunes: dict[TribuneID, Tribune] = {}
            self.fixed_obstacles: dict[FixedObstacleID, FixedObstacle] = {}
            self.positional_actuator_states: dict[PositionalActuatorEnum, PositionalActuator] = {}
            self.bool_sensor_states: dict[BoolSensorEnum, BoolSensor] = {}
            self.emulated_actuator_states: set[PositionalActuatorEnum] = {}
            self.reset()

    def reset(self):
        """
        Reset the context.
        """
        self.score = self.minimum_score
        self.countdown = self.game_duration
        self.last_countdown = self.game_duration
        self.tribunes_in_robot = 0
        self.create_artifacts()
        self.create_fixed_obstacles()
        self.create_actuators_states()

    def deepcopy(self):
        """
        Return a deep copy of the GameContext instance.
        """
        new_ctx = GameContext(self.shared_properties, initialize=False)
        new_ctx.game_duration = self.game_duration
        new_ctx.minimum_score = self.minimum_score
        new_ctx.score = self.score
        new_ctx.countdown = self.countdown
        new_ctx.last_countdown = self.last_countdown
        new_ctx.tribunes_in_robot = self.tribunes_in_robot
        new_ctx.construction_areas = {k: v.model_copy() for k, v in self.construction_areas.items()}
        new_ctx.tribunes = {k: v.model_copy() for k, v in self.tribunes.items()}
        new_ctx.fixed_obstacles = {k: v.model_copy() for k, v in self.fixed_obstacles.items()}
        # Do not copy artifacts that are not used in actions but keep the code in comments to no forget
        # that this copy function is only a partial copy.
        # new_ctx.opponent_construction_areas = {
        #     k: v.model_copy(deep=True) for k, v in self.opponent_construction_areas.items()
        # }
        # new_ctx.positional_actuator_states = {
        #     k: v.model_copy(deep=True) for k, v in self.positional_actuator_states.items()
        # }
        return new_ctx

    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():
            adapted_pose = AdaptedPose(**area.model_dump())
            self.construction_areas[id] = ConstructionAreaSmall(**adapted_pose.model_dump(), id=id, enabled=False)
            self.opponent_construction_areas[id] = ConstructionAreaSmall(
                x=adapted_pose.x,
                y=-adapted_pose.y,
                O=-adapted_pose.O,
                id=id,
            )

        self.opponent_construction_areas[ConstructionAreaID.LocalBottomLarge3].enabled = False
        self.opponent_construction_areas[ConstructionAreaID.OppositeSideLarge3].enabled = False
        if self.shared_properties.table == TableEnum.Training:
            self.opponent_construction_areas[ConstructionAreaID.OppositeSideLarge1].enabled = False
            self.opponent_construction_areas[ConstructionAreaID.OppositeSideLarge2].enabled = False
            self.opponent_construction_areas[ConstructionAreaID.OppositeSideLarge3].enabled = False

        # Tribunes
        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.shared_properties.table == TableEnum.Training:
            self.tribunes[TribuneID.LocalTop] = self.tribunes[TribuneID.LocalTopTraining].model_copy(
                update={"id": TribuneID.LocalTop}
            )
            self.tribunes[TribuneID.LocalTop].x -= 73.0 / 2
            self.tribunes[TribuneID.LocalCenter].x -= 73.0 / 2
        del self.tribunes[TribuneID.LocalTopTraining]

    def create_fixed_obstacles(self):
        # Positions are related to the default camp blue.
        self.fixed_obstacles: dict[FixedObstacleID, FixedObstacle] = {}

        # Ramp
        self.fixed_obstacles[FixedObstacleID.Ramp] = FixedObstacle(
            **AdaptedPose(x=900, y=-650).model_dump(),
            length=400,
            width=200,
            id=FixedObstacleID.Ramp,
        )

        # Scene
        self.fixed_obstacles[FixedObstacleID.Scene] = FixedObstacle(
            **AdaptedPose(x=825, y=-225).model_dump(),
            length=450,
            width=450,
            id=FixedObstacleID.Scene,
        )

        # Pami 5 path
        self.fixed_obstacles[FixedObstacleID.Pami5Path] = FixedObstacle(
            **AdaptedPose(x=930, y=-1175).model_dump(),
            length=650,
            width=50,
            id=FixedObstacleID.Pami5Path,
        )

        # Opponent ramp
        self.fixed_obstacles[FixedObstacleID.OpponentRamp] = FixedObstacle(
            **AdaptedPose(x=900, y=650).model_dump(),
            length=400,
            width=200,
            id=FixedObstacleID.OpponentRamp,
        )

        # Opponent scene
        self.fixed_obstacles[FixedObstacleID.OpponentScene] = FixedObstacle(
            **AdaptedPose(x=825, y=225).model_dump(),
            length=450,
            width=450,
            id=FixedObstacleID.OpponentScene,
        )

        # Backstage
        self.fixed_obstacles[FixedObstacleID.Backstage] = FixedObstacle(
            **AdaptedPose(x=1000 - 450 / 2, y=-1500 + 150 + 450 / 2).model_dump(),
            length=450,
            width=450,
            enabled=False,
            id=FixedObstacleID.Backstage,
        )

        # PAMIs starting area for robot ID 1, the main robot.
        if self.shared_properties.robot_id == 1:
            self.fixed_obstacles[FixedObstacleID.PamiStartArea] = FixedObstacle(
                **AdaptedPose(x=825, y=-1425).model_dump(),
                length=150,
                width=450,
                id=FixedObstacleID.PamiStartArea,
            )

            self.fixed_obstacles[FixedObstacleID.PitArea] = FixedObstacle(
                **AdaptedPose(x=375, y=-350).model_dump(),
                length=700,
                width=350,
                enabled=False,
                id=FixedObstacleID.PitArea,
            )

            self.fixed_obstacles[FixedObstacleID.OpponentPitArea] = FixedObstacle(
                **AdaptedPose(x=375, y=350).model_dump(),
                length=700,
                width=350,
                enabled=False,
                id=FixedObstacleID.OpponentPitArea,
            )

        if (
            self.shared_properties.robot_id == 1
            and self.shared_properties.table == TableEnum.Training
            or self.shared_properties.robot_id == 5
        ):
            self.fixed_obstacles[FixedObstacleID.Ramp].enabled = False
            self.fixed_obstacles[FixedObstacleID.Scene].enabled = False
            self.fixed_obstacles[FixedObstacleID.Pami5Path].enabled = False

        if self.shared_properties.table == TableEnum.Training:
            for obstacle in self.fixed_obstacles.values():
                obstacle.x -= 1000

    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] = {}

deepcopy() #

Return a deep copy of the GameContext instance.

Source code in cogip/tools/planner/context.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def deepcopy(self):
    """
    Return a deep copy of the GameContext instance.
    """
    new_ctx = GameContext(self.shared_properties, initialize=False)
    new_ctx.game_duration = self.game_duration
    new_ctx.minimum_score = self.minimum_score
    new_ctx.score = self.score
    new_ctx.countdown = self.countdown
    new_ctx.last_countdown = self.last_countdown
    new_ctx.tribunes_in_robot = self.tribunes_in_robot
    new_ctx.construction_areas = {k: v.model_copy() for k, v in self.construction_areas.items()}
    new_ctx.tribunes = {k: v.model_copy() for k, v in self.tribunes.items()}
    new_ctx.fixed_obstacles = {k: v.model_copy() for k, v in self.fixed_obstacles.items()}
    # Do not copy artifacts that are not used in actions but keep the code in comments to no forget
    # that this copy function is only a partial copy.
    # new_ctx.opponent_construction_areas = {
    #     k: v.model_copy(deep=True) for k, v in self.opponent_construction_areas.items()
    # }
    # new_ctx.positional_actuator_states = {
    #     k: v.model_copy(deep=True) for k, v in self.positional_actuator_states.items()
    # }
    return new_ctx

reset() #

Reset the context.

Source code in cogip/tools/planner/context.py
44
45
46
47
48
49
50
51
52
53
54
def reset(self):
    """
    Reset the context.
    """
    self.score = self.minimum_score
    self.countdown = self.game_duration
    self.last_countdown = self.game_duration
    self.tribunes_in_robot = 0
    self.create_artifacts()
    self.create_fixed_obstacles()
    self.create_actuators_states()