Skip to content

Commit 7d969f3

Browse files
committed
Adding a spike watcher utility so bots know who has spiked the ball.
1 parent 5f59d0d commit 7d969f3

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

src/bot.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ looks_config = ./appearance.cfg
66
python_file = ./bot.py
77

88
# Name of the bot in-game
9-
name = PythonExampleBot
9+
name = SpikeRushExampleBot
1010

1111
# The maximum number of ticks per second that your bot wishes to receive.
1212
maximum_tick_rate_preference = 120

src/bot.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
from rlbot.agents.base_agent import BaseAgent, SimpleControllerState
44
from rlbot.utils.structures.game_data_struct import GameTickPacket
5+
from rlbot.utils.structures.quick_chats import QuickChats
56

67
from util.orientation import Orientation
8+
from util.spikes import SpikeWatcher
79
from util.vec import Vec3
810
from util.sequence import Sequence, ControlStep
911

@@ -14,17 +16,22 @@ def initialize_agent(self):
1416
# This runs once before the bot starts up
1517
self.controller_state = SimpleControllerState()
1618
self.active_sequence: Sequence = None
19+
self.spike_watcher = SpikeWatcher()
1720

1821
def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
1922

2023
if self.active_sequence and not self.active_sequence.done:
2124
return self.active_sequence.tick(packet)
2225

26+
self.spike_watcher.read_packet(packet)
27+
2328
ball_location = Vec3(packet.game_ball.physics.location)
2429
my_car = packet.game_cars[self.index]
2530
car_location = Vec3(my_car.physics.location)
2631
car_velocity = Vec3(my_car.physics.velocity)
2732

33+
# Example of using a sequence
34+
# This will do a front flip if the car's velocity is between 550 and 600
2835
if 550 < car_velocity.length() < 600:
2936
self.active_sequence = Sequence([
3037
ControlStep(0.05, SimpleControllerState(jump=True)),
@@ -34,13 +41,23 @@ def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
3441
])
3542
return self.active_sequence.tick(packet)
3643

37-
car_to_ball = ball_location - car_location
44+
# Example of using the spike watcher.
45+
# This will make the bot say I got it! when it spikes the ball,
46+
# then release it 3 seconds later.
47+
if self.spike_watcher.carrying_car == my_car:
48+
if self.spike_watcher.carry_duration == 0:
49+
self.send_quick_chat(QuickChats.CHAT_EVERYONE, QuickChats.Information_IGotIt)
50+
elif self.spike_watcher.carry_duration > 3:
51+
return SimpleControllerState(use_item=True)
3852

53+
# The rest of this code just ball chases.
3954
# Find the direction of our car using the Orientation class
4055
car_orientation = Orientation(my_car.physics.rotation)
4156
car_direction = car_orientation.forward
4257

43-
steer_correction_radians = find_correction(car_direction, car_to_ball)
58+
target = ball_location
59+
car_to_target = target - car_location
60+
steer_correction_radians = find_correction(car_direction, car_to_target)
4461

4562
self.controller_state.throttle = 1.0
4663
self.controller_state.steer = -1 if steer_correction_radians > 0 else 1.0

src/util/spikes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from rlbot.utils.structures.game_data_struct import PlayerInfo, GameTickPacket
2+
3+
from util.vec import Vec3
4+
5+
6+
class SpikeWatcher:
7+
def __init__(self):
8+
self.carrying_car: PlayerInfo = None
9+
self.spike_moment = 0
10+
self.carry_duration = 0
11+
12+
def read_packet(self, packet: GameTickPacket):
13+
ball_location = Vec3(packet.game_ball.physics.location)
14+
closest_candidate: PlayerInfo = None
15+
closest_distance = 999999
16+
for i in range(packet.num_cars):
17+
car = packet.game_cars[i]
18+
car_location = Vec3(car.physics.location)
19+
distance = car_location.dist(ball_location)
20+
if distance < 190:
21+
if distance < closest_distance:
22+
closest_candidate = car
23+
closest_distance = distance
24+
if closest_candidate != self.carrying_car and closest_candidate is not None:
25+
self.spike_moment = packet.game_info.seconds_elapsed
26+
27+
self.carrying_car = closest_candidate
28+
if self.carrying_car is not None:
29+
self.carry_duration = packet.game_info.seconds_elapsed - self.spike_moment
30+
print(closest_distance)

0 commit comments

Comments
 (0)