|
1 | 1 | import math |
2 | 2 |
|
3 | | -from RLBotFramework.agents.base_agent import BaseAgent |
| 3 | +from RLBotFramework.agents.base_flatbuffer_agent import BaseFlatbufferAgent, SimpleControllerState |
| 4 | +from RLBotMessages.flat import GameTickPacket |
4 | 5 |
|
5 | 6 | URotationToRadians = math.pi / float(32768) |
6 | 7 |
|
7 | 8 |
|
8 | | -class PythonExample(BaseAgent): |
| 9 | +class PythonExample(BaseFlatbufferAgent): |
9 | 10 |
|
10 | | - def get_output_vector(self, game_tick_packet): |
| 11 | + def get_output(self, packet: GameTickPacket) -> SimpleControllerState: |
| 12 | + controller_state = SimpleControllerState() |
11 | 13 |
|
12 | | - ball_location = Vector2(game_tick_packet.gameball.Location.X, game_tick_packet.gameball.Location.Y) |
| 14 | + if packet.Ball() is None: # This happens during replays |
| 15 | + return controller_state |
13 | 16 |
|
14 | | - my_car = game_tick_packet.gamecars[self.index] |
15 | | - car_location = Vector2(my_car.Location.X, my_car.Location.Y) |
| 17 | + ball_location = Vector2(packet.Ball().Physics().Location().X(), packet.Ball().Physics().Location().Y()) |
| 18 | + |
| 19 | + my_car = packet.Players(self.index) |
| 20 | + car_location = Vector2(my_car.Physics().Location().X(), my_car.Physics().Location().Y()) |
16 | 21 | car_direction = get_car_facing_vector(my_car) |
17 | 22 | car_to_ball = ball_location - car_location |
18 | 23 |
|
19 | 24 | steer_correction_radians = car_direction.correction_to(car_to_ball) |
20 | 25 |
|
21 | 26 | if steer_correction_radians > 0: |
22 | 27 | # Positive radians in the unit circle is a turn to the left. |
23 | | - turn = -1.0 # Negative value for a turn to the left. |
| 28 | + turn = -1.0 # Negative value for a turn to the left. |
24 | 29 | else: |
25 | 30 | turn = 1.0 |
26 | 31 |
|
27 | | - return [ |
28 | | - 1.0, # throttle |
29 | | - turn, # steer |
30 | | - 0.0, # pitch |
31 | | - 0.0, # yaw |
32 | | - 0.0, # roll |
33 | | - 0, # jump |
34 | | - 0, # boost |
35 | | - 0 # handbrake |
36 | | - ] |
| 32 | + controller_state.throttle = 1.0 |
| 33 | + controller_state.steer = turn |
| 34 | + |
| 35 | + return controller_state |
37 | 36 |
|
38 | 37 |
|
39 | 38 | class Vector2: |
@@ -65,10 +64,10 @@ def correction_to(self, ideal): |
65 | 64 |
|
66 | 65 |
|
67 | 66 | def get_car_facing_vector(car): |
68 | | - pitch = float(car.Rotation.Pitch) |
69 | | - yaw = float(car.Rotation.Yaw) |
| 67 | + pitch = car.Physics().Rotation().Pitch() |
| 68 | + yaw = car.Physics().Rotation().Yaw() |
70 | 69 |
|
71 | | - facing_x = math.cos(pitch * URotationToRadians) * math.cos(yaw * URotationToRadians) |
72 | | - facing_y = math.cos(pitch * URotationToRadians) * math.sin(yaw * URotationToRadians) |
| 70 | + facing_x = math.cos(pitch) * math.cos(yaw) |
| 71 | + facing_y = math.cos(pitch) * math.sin(yaw) |
73 | 72 |
|
74 | 73 | return Vector2(facing_x, facing_y) |
0 commit comments