-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
228 lines (188 loc) · 10.4 KB
/
bot.py
File metadata and controls
228 lines (188 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from rlbot.agents.base_agent import BaseAgent, SimpleControllerState
from rlbot.messages.flat.QuickChatSelection import QuickChatSelection
from rlbot.utils.structures.game_data_struct import GameTickPacket
from util.ball_prediction_analysis import find_slice_at_time
from util.boost_pad_tracker import BoostPadTracker
from util.drive import steer_toward_target
from util.sequence import Sequence, ControlStep
from util.vec import Vec3
from util.orientation import Orientation
from gui import DebugGUI
class BoostHog(BaseAgent):
def __init__(self, name, team, index):
super().__init__(name, team, index)
self.chase_ball_game_time = 0
self.boosts_counted = False
self.active_sequence: Sequence = None
self.boost_pad_tracker = BoostPadTracker()
self.boost: bool = False
self.counter = 0 # Track time in ticks (or update intervals)
self.delay = 100 # Delay after 100 ticks (can adjust as needed)
self.has_kickoff_happened = False
self.boosts_collected = 0
self.debug_gui = DebugGUI(title=f"{self.name} Debug")
self.last_debug_time = -1.0
def initialize_agent(self):
# Set up information about the boost pads now that the game is active and the info is available
# Runs once before bot starts up
self.boost_pad_tracker.initialize_boosts(self.get_field_info())
def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
"""
This function will be called by the framework many times per second. This is where you can
see the motion of the ball, etc. and return controls to drive your car.
"""
# Keep our boost pad info updated with which pads are currently active
self.boost_pad_tracker.update_boost_status(packet)
# This is good to keep at the beginning of get_output. It will allow you to continue
# any sequences that you may have started during a previous call to get_output.
if self.active_sequence is not None and not self.active_sequence.done:
controls = self.active_sequence.tick(packet)
if controls is not None:
return controls
# Gather some information about our car and the ball
my_car = packet.game_cars[self.index]
info = self.get_field_info()
car_location = Vec3(my_car.physics.location)
nearest_boost_loc = self.get_nearest_boost(info, packet, car_location)
car_velocity = Vec3(my_car.physics.velocity)
ball_location = Vec3(packet.game_ball.physics.location)
target_location = nearest_boost_loc
boost_amount = my_car.boost
controls = SimpleControllerState()
game_time = packet.game_info.game_time_remaining * -1
controls = self.action_goto(my_car, target_location, packet, controls, boost_amount, car_velocity, nearest_boost_loc, ball_location, game_time)
if not self.has_kickoff_happened:
behavior_mode = "Doing kickoff"
elif self.has_kickoff_happened and (game_time - self.chase_ball_game_time) <= 5:
behavior_mode = "Chasing ball"
else:
behavior_mode = "Getting boost"
if packet.game_info.is_kickoff_pause:
self.has_kickoff_happened = False
# update boosts collected
self.update_boosts_collected(my_car, packet, game_time)
if target_location is None:
target_location = ball_location
corner_debug = "Time Elapsed: {}\n".format(game_time)
corner_debug += "Boosts collected: {}\n".format(self.boosts_collected)
corner_debug += "Target location: {}\n".format(target_location)
corner_debug += "Kickoff happened: {}\n".format(self.has_kickoff_happened)
corner_debug += "Ball location: {}\n".format(ball_location.flat())
corner_debug += "Behavior mode {}\n".format(behavior_mode)
# Draw some things to help understand what the bot is thinking
self.renderer.draw_line_3d(car_location, ball_location, self.renderer.white())
if nearest_boost_loc is not None:
self.renderer.draw_line_3d(car_location, nearest_boost_loc, self.renderer.yellow())
self.renderer.draw_rect_3d(nearest_boost_loc, 8, 8, True, self.renderer.white(), centered=True)
self.renderer.draw_string_3d(car_location, 1, 1, f'Speed: {car_velocity.length():.1f}', self.renderer.white())
if corner_debug:
corner_display_y = 900 - (corner_debug.count('\n') * 20)
self.renderer.draw_string_2d(10, corner_display_y, 1, 1, corner_debug, self.renderer.white())
if target_location is not None:
self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True)
if self.debug_gui.is_debug_enabled():
self.print_debug_info(packet, game_time)
return controls
def action_goto(self, my_car, target_location, packet, controls, boost_amount, car_velocity, nearest_boost_loc, ball_location, game_time):
field_center = Vec3(0, 0, 0)
if target_location is None:
controls.steer = steer_toward_target(my_car, field_center)
controls.throttle = 1.0
self.counter += 1
controls.boost = boost_amount > 20
return controls
# if boost_amount > 20:
# if car_location.dist(target_location) > 1500:
# We're far away from the ball, try to lead it a little
# ball_prediction = self.get_ball_prediction_struct()
# ball_in_future = find_slice_at_time(ball_prediction, packet.game_info.seconds_elapsed + 2)
# if ball_in_future is not None:
# target_location = Vec3(ball_in_future.physics.location)
# self.renderer.draw_line_3d(ball_location, target_location, self.renderer.cyan())
# else:
# target_location = ball_location
# else:
# target_location = nearest_boost_loc # Optional: could be skipped
if 750 < car_velocity.length() < 800:
return begin_front_flip(self, packet)
if not self.has_kickoff_happened:
target_location = ball_location
if ball_location.flat() != Vec3(0, 0,0):
self.has_kickoff_happened = True
elif self.has_kickoff_happened and (game_time - self.chase_ball_game_time) <= 5:
target_location = ball_location
if self.debug_gui.is_debug_enabled():
if game_time - self.last_debug_time >= 1.0:
print(f"[DEBUG] Ball chase timer: {game_time - self.chase_ball_game_time}")
print(f"[DEBUG] Game time: {game_time}")
self.last_debug_time = game_time
else:
target_location = nearest_boost_loc
# if self.boosts_collected >= 25:
# target_location = ball_location
controls.steer = steer_toward_target(my_car, target_location)
controls.throttle = 1.0
controls.boost = boost_amount > 20
return controls
def update_boosts_collected(self, my_car, packet, game_time):
if my_car.boost >= 100:
if not self.boosts_counted:
self.boosts_collected += 1
if self.debug_gui.is_debug_enabled():
if game_time - self.last_debug_time >= 1.0:
print(f"[DEBUG] Boost collected! Total: {self.boosts_collected}")
self.last_debug_time = game_time
self.boosts_counted = True
else:
self.boosts_counted = False
if self.boosts_collected >= 6:
self.chase_ball_game_time = game_time
if self.debug_gui.is_debug_enabled():
if game_time - self.last_debug_time >= 1.0:
print(f"[DEBUG] Ball chase timer STARTED at: {self.chase_ball_game_time}")
self.last_debug_time = game_time
self.boosts_collected = 0
def get_nearest_boost(self, info, packet, car_location):
nearest_boost_loc = None
# loop over all boosts
for i, boost in enumerate(info.boost_pads):
# only want large boosts that haven't been taken
if boost.is_full_boost and packet.game_boosts[i].is_active:
if not nearest_boost_loc:
nearest_boost_loc = boost.location
else:
# if this boost is closer, save that
if car_location.dist(Vec3(boost.location)) < car_location.dist(Vec3(nearest_boost_loc)):
nearest_boost_loc = boost.location
if nearest_boost_loc is None:
for i, boost in enumerate(info.boost_pads):
# only want large boosts that haven't been taken
if packet.game_boosts[i].is_active:
if not nearest_boost_loc:
nearest_boost_loc = boost.location
else:
# if this boost is closer, save that
if car_location.dist(Vec3(boost.location)) < car_location.dist(Vec3(nearest_boost_loc)):
nearest_boost_loc = boost.location
return nearest_boost_loc
def print_debug_info(self, packet: GameTickPacket, game_time):
car = packet.game_cars[self.index]
pos = Vec3(car.physics.location)
boost = car.boost
if game_time - self.last_debug_time >= 1.0:
print(f"[DEBUG] Car position: {pos}, Boost: {boost:.1f}")
self.last_debug_time = game_time
self.renderer.draw_string_3d(pos, 1, 1, f"Boost: {boost:.0f}", self.renderer.green())
def begin_front_flip(self, packet):
# Send some quickchat just for fun
self.send_quick_chat(team_only=False, quick_chat=QuickChatSelection.Information_IGotIt)
# Do a front flip. We will be committed to this for a few seconds and the bot will ignore other
# logic during that time because we are setting the active_sequence.
self.active_sequence = Sequence([
ControlStep(duration=0.05, controls=SimpleControllerState(jump=True)),
ControlStep(duration=0.05, controls=SimpleControllerState(jump=False)),
ControlStep(duration=0.2, controls=SimpleControllerState(jump=True, pitch=-1)),
ControlStep(duration=0.8, controls=SimpleControllerState()),
])
# Return the controls associated with the beginning of the sequence so we can start right away.
return self.active_sequence.tick(packet)