Skip to content

Commit 5f59d0d

Browse files
committed
Adding a sequence system to help with dodges.
1 parent c5603c7 commit 5f59d0d

2 files changed

Lines changed: 66 additions & 21 deletions

File tree

src/bot.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,34 @@
55

66
from util.orientation import Orientation
77
from util.vec import Vec3
8+
from util.sequence import Sequence, ControlStep
89

910

1011
class MyBot(BaseAgent):
1112

1213
def initialize_agent(self):
1314
# This runs once before the bot starts up
1415
self.controller_state = SimpleControllerState()
16+
self.active_sequence: Sequence = None
1517

1618
def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
17-
ball_location = Vec3(packet.game_ball.physics.location)
1819

20+
if self.active_sequence and not self.active_sequence.done:
21+
return self.active_sequence.tick(packet)
22+
23+
ball_location = Vec3(packet.game_ball.physics.location)
1924
my_car = packet.game_cars[self.index]
2025
car_location = Vec3(my_car.physics.location)
26+
car_velocity = Vec3(my_car.physics.velocity)
27+
28+
if 550 < car_velocity.length() < 600:
29+
self.active_sequence = Sequence([
30+
ControlStep(0.05, SimpleControllerState(jump=True)),
31+
ControlStep(0.05, SimpleControllerState(jump=False)),
32+
ControlStep(0.2, SimpleControllerState(jump=True, pitch=-1)),
33+
ControlStep(0.8, SimpleControllerState()),
34+
])
35+
return self.active_sequence.tick(packet)
2136

2237
car_to_ball = ball_location - car_location
2338

@@ -27,18 +42,8 @@ def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
2742

2843
steer_correction_radians = find_correction(car_direction, car_to_ball)
2944

30-
if steer_correction_radians > 0:
31-
# Positive radians in the unit circle is a turn to the left.
32-
turn = -1.0 # Negative value for a turn to the left.
33-
action_display = "turn left"
34-
else:
35-
turn = 1.0
36-
action_display = "turn right"
37-
3845
self.controller_state.throttle = 1.0
39-
self.controller_state.steer = turn
40-
41-
draw_debug(self.renderer, my_car, packet.game_ball, action_display)
46+
self.controller_state.steer = -1 if steer_correction_radians > 0 else 1.0
4247

4348
return self.controller_state
4449

@@ -60,12 +65,3 @@ def find_correction(current: Vec3, ideal: Vec3) -> float:
6065
diff -= 2 * math.pi
6166

6267
return diff
63-
64-
65-
def draw_debug(renderer, car, ball, action_display):
66-
renderer.begin_rendering()
67-
# draw a line from the car to the ball
68-
renderer.draw_line_3d(car.physics.location, ball.physics.location, renderer.white())
69-
# print the action that the bot is taking
70-
renderer.draw_string_3d(car.physics.location, 2, 2, action_display, renderer.white())
71-
renderer.end_rendering()

src/util/sequence.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from dataclasses import dataclass
2+
from typing import List
3+
4+
from rlbot.agents.base_agent import SimpleControllerState
5+
from rlbot.utils.structures.game_data_struct import GameTickPacket
6+
7+
8+
@dataclass
9+
class StepResult:
10+
controls: SimpleControllerState
11+
done: bool
12+
13+
14+
class Step:
15+
def tick(self, packet: GameTickPacket) -> StepResult:
16+
raise NotImplementedError
17+
18+
19+
class ControlStep(Step):
20+
def __init__(self, duration: float, controls: SimpleControllerState):
21+
self.duration = duration
22+
self.controls = controls
23+
self.start_time: float = None
24+
25+
def tick(self, packet: GameTickPacket) -> StepResult:
26+
if self.start_time is None:
27+
self.start_time = packet.game_info.seconds_elapsed
28+
elapsed_time = packet.game_info.seconds_elapsed - self.start_time
29+
return StepResult(controls=self.controls, done=elapsed_time > self.duration)
30+
31+
32+
class Sequence:
33+
def __init__(self, steps: List[Step]):
34+
self.steps = steps
35+
self.index = 0
36+
self.done = False
37+
38+
def tick(self, packet: GameTickPacket):
39+
while True:
40+
if self.index >= len(self.steps):
41+
self.done = True
42+
return SimpleControllerState()
43+
step = self.steps[self.index]
44+
result = step.tick(packet)
45+
if result.done:
46+
self.index += 1
47+
if self.index >= len(self.steps):
48+
self.done = True
49+
return result.controls

0 commit comments

Comments
 (0)