|
| 1 | +'''Rocket League data pre-processing.''' |
| 2 | + |
| 3 | +from .utils import Car, Ball, BoostPad, arr_from_list, arr_from_rot, arr_from_vec, orient_matrix |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | + |
| 8 | +def setup(self, packet, field_info): |
| 9 | + """Sets up the variables and classes for the agent. |
| 10 | +
|
| 11 | + Arguments: |
| 12 | + self {BaseAgent} -- The agent. |
| 13 | + packet {GameTickPacket} -- Information about the game. |
| 14 | + field_info {FieldInfoPacket} -- Information about the game field. |
| 15 | + """ |
| 16 | + |
| 17 | + # Game info |
| 18 | + self.game_time = packet.game_info.seconds_elapsed |
| 19 | + self.dt = 1.0 / 120.0 |
| 20 | + self.last_time = 0.0 |
| 21 | + self.r_active = packet.game_info.is_round_active |
| 22 | + self.ko_pause = packet.game_info.is_kickoff_pause |
| 23 | + self.m_ended = packet.game_info.is_match_ended |
| 24 | + self.gravity = packet.game_info.world_gravity_z |
| 25 | + |
| 26 | + # Creates Car objects for each car. |
| 27 | + self.teammates = [] |
| 28 | + self.opponents = [] |
| 29 | + for index in range(packet.num_cars): |
| 30 | + car = packet.game_cars[index] |
| 31 | + if index == self.index: |
| 32 | + self.player = Car(self.index, self.team, self.name) |
| 33 | + elif car.team == self.team: |
| 34 | + self.teammates.append(Car(index, car.team, car.name)) |
| 35 | + else: |
| 36 | + self.opponents.append(Car(index, car.team, car.name)) |
| 37 | + |
| 38 | + # Creates a Ball object. |
| 39 | + self.ball = Ball() |
| 40 | + |
| 41 | + # Creates Boostpad objects. |
| 42 | + self.l_pads = [] |
| 43 | + self.s_pads = [] |
| 44 | + for i in range(field_info.num_boosts): |
| 45 | + pad = field_info.boost_pads[i] |
| 46 | + pad_type = self.l_pads if pad.is_full_boost else self.s_pads |
| 47 | + pad_obj = BoostPad(i, arr_from_vec(pad.location)) |
| 48 | + pad_type.append(pad_obj) |
| 49 | + |
| 50 | + |
| 51 | +def process(self, packet): |
| 52 | + """Processes the gametick packet. |
| 53 | + Arguments: |
| 54 | + self {BaseAgent} -- The agent. |
| 55 | + packet {GameTickPacket} -- The game packet being processed. |
| 56 | + """ |
| 57 | + |
| 58 | + # Processing game info. |
| 59 | + self.game_time = packet.game_info.seconds_elapsed |
| 60 | + self.dt = self.game_time - self.last_time |
| 61 | + self.last_time = self.game_time |
| 62 | + self.r_active = packet.game_info.is_round_active |
| 63 | + self.ko_pause = packet.game_info.is_kickoff_pause |
| 64 | + self.m_ended = packet.game_info.is_match_ended |
| 65 | + self.gravity = packet.game_info.world_gravity_z |
| 66 | + |
| 67 | + # Processing player data. |
| 68 | + # From packet: |
| 69 | + self.player.pos = arr_from_vec(packet.game_cars[self.player.index].physics.location) |
| 70 | + self.player.rot = arr_from_rot(packet.game_cars[self.player.index].physics.rotation) |
| 71 | + self.player.vel = arr_from_vec(packet.game_cars[self.player.index].physics.velocity) |
| 72 | + self.player.ang_vel = arr_from_vec(packet.game_cars[self.player.index].physics.angular_velocity) |
| 73 | + self.player.dead = packet.game_cars[self.player.index].is_demolished |
| 74 | + self.player.wheel_c = packet.game_cars[self.player.index].has_wheel_contact |
| 75 | + self.player.sonic = packet.game_cars[self.player.index].is_super_sonic |
| 76 | + self.player.jumped = packet.game_cars[self.player.index].jumped |
| 77 | + self.player.d_jumped = packet.game_cars[self.player.index].double_jumped |
| 78 | + self.player.boost = packet.game_cars[self.player.index].boost |
| 79 | + # Calculated: |
| 80 | + self.player.orient_m = orient_matrix(self.player.rot, self.player.orient_m) |
| 81 | + |
| 82 | + # Processing teammates. |
| 83 | + for teammate in self.teammates: |
| 84 | + # From packet: |
| 85 | + teammate.pos = arr_from_vec(packet.game_cars[teammate.index].physics.location) |
| 86 | + teammate.rot = arr_from_rot(packet.game_cars[teammate.index].physics.rotation) |
| 87 | + teammate.vel = arr_from_vec(packet.game_cars[teammate.index].physics.velocity) |
| 88 | + teammate.ang_vel = arr_from_vec(packet.game_cars[teammate.index].physics.angular_velocity) |
| 89 | + teammate.dead = packet.game_cars[teammate.index].is_demolished |
| 90 | + teammate.wheel_c = packet.game_cars[teammate.index].has_wheel_contact |
| 91 | + teammate.sonic = packet.game_cars[teammate.index].is_super_sonic |
| 92 | + teammate.jumped = packet.game_cars[teammate.index].jumped |
| 93 | + teammate.d_jumped = packet.game_cars[teammate.index].double_jumped |
| 94 | + teammate.boost = packet.game_cars[teammate.index].boost |
| 95 | + |
| 96 | + # Processing opponents. |
| 97 | + for opponent in self.opponents: |
| 98 | + # From packet: |
| 99 | + opponent.pos = arr_from_vec(packet.game_cars[opponent.index].physics.location) |
| 100 | + opponent.rot = arr_from_rot(packet.game_cars[opponent.index].physics.rotation) |
| 101 | + opponent.vel = arr_from_vec(packet.game_cars[opponent.index].physics.velocity) |
| 102 | + opponent.ang_vel = arr_from_vec(packet.game_cars[opponent.index].physics.angular_velocity) |
| 103 | + opponent.dead = packet.game_cars[opponent.index].is_demolished |
| 104 | + opponent.wheel_c = packet.game_cars[opponent.index].has_wheel_contact |
| 105 | + opponent.sonic = packet.game_cars[opponent.index].is_super_sonic |
| 106 | + opponent.jumped = packet.game_cars[opponent.index].jumped |
| 107 | + opponent.d_jumped = packet.game_cars[opponent.index].double_jumped |
| 108 | + opponent.boost = packet.game_cars[opponent.index].boost |
| 109 | + |
| 110 | + # Processing Ball data. |
| 111 | + self.ball.pos = arr_from_vec(packet.game_ball.physics.location) |
| 112 | + self.ball.rot = arr_from_rot(packet.game_ball.physics.rotation) |
| 113 | + self.ball.vel = arr_from_vec(packet.game_ball.physics.velocity) |
| 114 | + self.ball.ang_vel = arr_from_vec(packet.game_ball.physics.angular_velocity) |
| 115 | + self.ball.last_touch = packet.game_ball.latest_touch |
| 116 | + |
| 117 | + # Processing ball prediction. |
| 118 | + ball_prediction_struct = self.get_ball_prediction_struct() |
| 119 | + dtype = [ |
| 120 | + ('physics', [ |
| 121 | + ('pos', '<f4', 3), |
| 122 | + ('rot', [ |
| 123 | + ('pitch', '<f4'), |
| 124 | + ('yaw', '<f4'), |
| 125 | + ('roll', '<f4') |
| 126 | + ]), |
| 127 | + ('vel', '<f4', 3), |
| 128 | + ('ang_vel', '<f4', 3) |
| 129 | + ]), |
| 130 | + ('time', '<f4') |
| 131 | + ] |
| 132 | + self.ball_prediction = np.ctypeslib.as_array( |
| 133 | + ball_prediction_struct.slices).view(dtype)[:ball_prediction_struct.num_slices] |
| 134 | + |
| 135 | + # Processing Boostpads. |
| 136 | + self.active_pads = [] |
| 137 | + for pad in self.l_pads + self.s_pads: |
| 138 | + pad.active = packet.game_boosts[pad.index].is_active |
| 139 | + pad.timer = packet.game_boosts[pad.index].timer |
| 140 | + if pad.active == True: |
| 141 | + self.active_pads.append(pad) |
0 commit comments