/
States.py
346 lines (266 loc) · 14.2 KB
/
States.py
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import math
import time
from Util import *
from rlbot.agents.base_agent import BaseAgent, SimpleControllerState
class wait:
def __init__(self):
self.expired = False
def available(self, agent):
if timeZ(agent.ball) > 1.5:
return True
return False
def execute(self, agent):
ball_future = future(agent.ball, timeZ(agent.ball))
if agent.me.boost < 35:
closest = 0
closest_distance = distance2D(boosts[0], ball_future)
for i in range(1,len(boosts)):
if distance2D(boosts[i], ball_future) < closest_distance and agent.boosts[i].is_active :
closest = i
closest_distance = distance2D(boosts[closest], ball_future)
target = boosts[closest]
speed = 2300
else:
target = ball_future
current = velocity2D(agent.me)
ratio = distance2D(agent.me,target)/(current + 0.01)
speed = cap(600 * ratio, 0, 2300)
if speed <= 100:
speed = 0
if ballReady(agent):
self.expired = True
return waitController(agent, target, speed)
class calcShot:
def __init__(self):
self.expired = False
def available(self,agent):
if ballReady(agent) and abs(agent.ball.location.data[1]) < 5050 and ballProject(agent) > 500 - (distance2D(agent.ball,agent.me)/2):
return True
return False
def execute(self,agent):
agent.controller = calcController
#getting the coordinates of the goalposts
leftPost = Vector3([-sign(agent.team)*700 , 5100*-sign(agent.team), 200])
rightPost = Vector3([sign(agent.team)*700, 5100*-sign(agent.team), 200])
#center = Vector3([0, 5150*-sign(agent.team), 200])
#time stuff that we don't worry about yet
time_guess = 0 # this is set to zero, so its not really doing anything. Just assuming where the ball is right now
bloc = future(agent.ball,time_guess)
#vectors from the goalposts to the ball & to Gosling
ball_left = angle2(bloc,leftPost)
ball_right = angle2(bloc,rightPost)
agent_left = angle2(agent.me,leftPost)
agent_right = angle2(agent.me,rightPost)
#determining if we are left/right/inside of cone
if agent_left > ball_left and agent_right > ball_right:
goal_target = rightPost
elif agent_left > ball_left and agent_right < ball_right:
goal_target = None
elif agent_left < ball_left and agent_right < ball_right:
goal_target = leftPost
else:
goal_target = None
if goal_target != None:
#if we are outside the cone, this is the same as Gosling's old code
goal_to_ball = (agent.ball.location - goal_target).normalize()
goal_to_agent = (agent.me.location - goal_target).normalize()
difference = goal_to_ball - goal_to_agent
error = cap(abs(difference.data[0])+ abs(difference.data[1]),1,10)
else:
#if we are inside the cone, our line to follow is a vector from the ball to us (although it's still named 'goal_to_ball')
goal_to_ball = (agent.me.location - agent.ball.location).normalize()
error = cap( distance2D(bloc,agent.me) /1000,0,1)
#this is measuring how fast the ball is traveling away from us if we were stationary
ball_dpp_skew = cap(abs(dpp(agent.ball.location, agent.ball.velocity, agent.me.location, [0,0,0]))/80, 1,1.5)
#same as Gosling's old distance calculation, but now we consider dpp_skew which helps us handle when the ball is moving
target_distance =cap( (40 + distance2D(agent.ball.location,agent.me)* (error**2))/1.8, 0,4000)
target_location = agent.ball.location + Vector3([(goal_to_ball.data[0]*target_distance) * ball_dpp_skew, goal_to_ball.data[1]*target_distance,0])
#this also adjusts the target location based on dpp
ball_something = dpp(target_location,agent.ball.velocity, agent.me,[0,0,0])**2
if ball_something > 100: #if we were stopped, and the ball is moving 100uu/s away from us
ball_something = cap(ball_something,0,80)
correction = agent.ball.velocity.normalize()
correction = Vector3([correction.data[0]*ball_something,correction.data[1]*ball_something,correction.data[2]*ball_something])
target_location += correction #we're adding some component of the ball's velocity to the target position so that we are able to hit a faster moving ball better
#it's important that this only happens when the ball is moving away from us.
#another target adjustment that applies if the ball is close to the wall
extra = 4120 - abs(target_location.data[0])
if extra < 0:
# we prevent our target from going outside the wall, and extend it so that Gosling gets closer to the wall before taking a shot, makes things more reliable
target_location.data[0] = cap(target_location.data[0],-4120,4120)
target_location.data[1] = target_location.data[1] + (-sign(agent.team)*cap(extra,-500,500))
#getting speed, this would be a good place to modify because it's not very good
target_local = toLocal(agent.ball.location,agent.me)
angle_to_target = cap(math.atan2(target_local.data[1], target_local.data[0]),-3,3)
#distance_to_target = distance2D(agent.me, target_location)
speed= 2000 - (100*(1+angle_to_target)**2)
#picking our rendered target color based on the speed we want to go
colorRed = cap(int( (speed/2300) * 255),0,255)
colorBlue =cap(255-colorRed,0,255)
#see the rendering tutorial on github about this, just drawing lines from the posts to the ball and one from the ball to the target
agent.renderer.begin_rendering()
agent.renderer.draw_line_3d(bloc.data, leftPost.data, agent.renderer.create_color(255,255,0,0))
agent.renderer.draw_line_3d(bloc.data, rightPost.data, agent.renderer.create_color(255,0,255,0))
agent.renderer.draw_line_3d(agent.ball.location.data,target_location.data, agent.renderer.create_color(255,colorRed,0,colorBlue))
agent.renderer.draw_rect_3d(target_location.data, 10,10, True, agent.renderer.create_color(255,colorRed,0,colorBlue))
agent.renderer.end_rendering()
if ballReady(agent) == False or abs(agent.ball.location.data[1]) > 5050:
self.expired = True
return agent.controller(agent,target_location,speed)
class quickShot:
def __init__(self):
self.expired = False
def available(self,agent):
if ballReady(agent) and ballProject(agent) > -500:
return True
return False
def execute(self,agent):
agent.controller = shotController
# left_post = Vector3([sign(agent.team)*GOAL_WIDTH/2,-sign(agent.team)*FIELD_LENGTH/2,100])
# right_post = Vector3([-sign(agent.team)*GOAL_WIDTH/2,-sign(agent.team)*FIELD_LENGTH/2,100])
# ball_left = angle2(agent.ball.location,left_post)
# ball_right = angle2(agent.ball.location,right_post)
# our_left = angle2(agent.me.location,left_post)
# our_right = angle2( agent.me.location,right_post)
offset = (agent.ball.location.data[0] / FIELD_WIDTH) * 3.14
x = agent.ball.location.data[0] + 100 * abs(math.cos(offset)) * sign(offset)
y = agent.ball.location.data[1] + 100 * abs(math.sin(offset)) * sign (agent.team)
target_location = toLocation([x,y, agent.ball.location.data[2]])
location = toLocal(target_location, agent.me)
angle_to_target = math.atan2(location.data[1], location.data[0])
distance_to_target = distance2D(agent.me, target_location)
speedCorrection = ((1 + abs(angle_to_target) **2 ) * 300)
speed = 2300 - speedCorrection + cap((distance_to_target/16)**2,0,speedCorrection)
if distance2D(agent.me.location, agent.ball.location) < 400 and abs(angle_to_target) > 2:
self.expired = True
elif calcShot().available(agent) == True:
self.expired = True
return agent.controller(agent, target_location, speed)
def calcController(agent, target_object, target_speed):
goal_local = toLocal([0, -sign(agent.team)*FIELD_LENGTH/2, 100], agent.me)
goal_angle = math.atan2(goal_local.data[1], goal_local.data[0])
loc = toLocal(target_object, agent.me)
controller_state = SimpleControllerState()
angle_to_targ = math.atan2(loc.data[1],loc.data[0])
current_speed = velocity2D(agent.me)
distance = distance2D(target_object, agent.me)
#steering
controller_state.steer = steer(angle_to_targ)
r = radius(current_speed)
slowdown = (Vector3([0,sign(target_object.data[0])*(r+40),0])-loc.flatten()).magnitude() / cap(r*1.5,1,1200)
target_speed = cap(current_speed*slowdown,0,current_speed)
# throttle
if agent.ball.location.data[0] == 0 and agent.ball.location.data[1] == 0:
controller_state.throttle, controller_state.boost = 1, True
else:
controller_state.throttle, controller_state.boost = throttle(target_speed,current_speed)
#dodging
time_diff = time.time() - agent.start
if (time_diff > 2.2 and distance <= 150) or (time_diff > 4 and distance >= 1000) and not kickoff(agent):
agent.start = time.time()
elif time_diff <= 0.1:
controller_state.jump = True
controller_state.pitch = -1
elif time_diff >= 0.1 and time_diff <= 0.15:
controller_state.jump = False
controller_state.pitch = -1
elif time_diff > 0.15 and time_diff < 1:
controller_state.jump = True
controller_state.yaw = math.sin(goal_angle)
controller_state.pitch = -abs(math.cos(goal_angle))
if not dodging(agent) and not agent.me.grounded:
target = agent.me.velocity.normalize()
targ_local = to_local(target.scale(500), agent.me)
return recoveryController(agent, targ_local)
return controller_state
def shotController(agent, target_object, target_speed):
goal_local = toLocal([0, -sign(agent.team)*FIELD_LENGTH/2, 100], agent.me)
goal_angle = math.atan2(goal_local.data[1], goal_local.data[0])
loc = toLocal(target_object, agent.me)
controller_state = SimpleControllerState()
angle_to_targ = math.atan2(loc.data[1],loc.data[0])
current_speed = velocity2D(agent.me)
distance = distance2D(target_object, agent.me)
#steering
controller_state.steer = steer(angle_to_targ)
# throttle
if agent.ball.location.data[0] == 0 and agent.ball.location.data[1] == 0:
controller_state.throttle, controller_state.boost = 1, True
else:
controller_state.throttle, controller_state.boost = throttle(target_speed,current_speed)
time_diff = time.time() - agent.start
#dodging
time_diff = time.time() - agent.start
if (time_diff > 2.2 and distance <= 270) or (time_diff > 4 and distance >= 1000):
agent.start = time.time()
elif time_diff <= 0.1:
controller_state.jump = True
controller_state.pitch = -1
elif time_diff >= 0.1 and time_diff <= 0.15:
controller_state.jump = False
controller_state.pitch = -1
elif time_diff > 0.15 and time_diff < 1:
controller_state.jump = True
controller_state.yaw = math.sin(goal_angle)
controller_state.pitch = -abs(math.cos(goal_angle))
if not dodging(agent) and not agent.me.grounded:
target = agent.me.velocity.normalize()
targ_local = to_local(target.scale(500), agent.me)
return recoveryController(agent, targ_local)
return controller_state
def waitController(agent, target, speed):
controller_state = SimpleControllerState()
loc = toLocal(target, agent.me)
angle_to_target = math.atan2(loc.data[1], loc.data[0])
controller_state.steer = steer(angle_to_target)
current_speed = velocity2D(agent.me)
if current_speed < speed:
controller_state.throttle = 1.0
elif current_speed - 50 > speed:
controller_state.throttle = -1.0
else:
controller_state.throttle = 0
time_diff = time.time() - agent.start
if time_diff > 2.2 and distance2D(target,agent.me) > (velocity2D(agent.me)*2.3) and abs(angle_to_target) < 1 and current_speed < speed:
agent.start = time.time()
elif time_diff <= 0.1:
controller_state.jump = True
controller_state.pitch = -1
elif time_diff >= 0.1 and time_diff <= 0.15:
controller_state.jump = False
controller_state.pitch = -1
elif time_diff > 0.15 and time_diff < 1:
controller_state.jump = True
controller_state.yaw = controller_state.steer
controller_state.pitch = -1
return controller_state
def recoveryController(agent, local): #accepts agent, the agent's controller, and a target (in local coordinates)
controller_state = SimpleControllerState()
turn = math.atan2(local.data[1],local.data[0])
up = toLocal(agent.me.location + Vector3([0,0,100]), agent.me)
target = [math.atan2(up.data[1],up.data[2]), math.atan2(local.data[2],local.data[0]), turn] #determining angles each axis must turn
controller_state.steer = steerPD(turn, 0)
controller_state.yaw = steerPD(target[2],-agent.me.rvelocity.data[2]/5)
controller_state.pitch = steerPD(target[1],agent.me.rvelocity.data[1]/5)
controller_state.roll = steerPD(target[0],agent.me.rvelocity.data[0]/5)
return controller_state
def speedDodgeController(agent, target_object, goal_angle):
controller_state = SimpleControllerState()
#dodging
time_diff = time.time() - agent.start
if time_diff > 2.2:
agent.start = time.time()
elif time_diff <= 0.1:
controller_state.jump = True
controller_state.pitch = -1
elif time_diff >= 0.1 and time_diff <= 0.15:
controller_state.jump = False
controller_state.pitch = -1
elif time_diff > 0.15 and time_diff < 1:
controller_state.jump = True
controller_state.yaw = math.sin(goal_angle)
controller_state.pitch = -abs(math.cos(goal_angle))
return controller_state
def steerPD(angle,rate):
final = ((35*(angle+rate))**3)/20
return cap(final,-1,1)