forked from RLBot/RLBotPythonExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprevious.py
More file actions
76 lines (55 loc) · 2.43 KB
/
previous.py
File metadata and controls
76 lines (55 loc) · 2.43 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
from rlbot.agents.base_agent import BaseAgent, SimpleControllerState
from rlbot.utils.structures.game_data_struct import GameTickPacket
import math
import time
def distance(x1, y1, x2, y2):
return math.sqrt((x2 - x1)**2+(y2-y1)**2)
class TutorialBot(BaseAgent):
def __init__(self, name, team, index):
super().__init__(name, team, index)
self.controller = SimpleControllerState
# Constants
self.DODGE_TIME = 0.2
self.DISTANCE_TO_DODGE = 500
# Game values
self.bot_pos = None
self.bot_yaw = None
# Dodging
self.should_dodge = False
self.on_second_jump = False
self.next_dodge_time = 0
def aim(self, target_x, target_y):
angle_between_bot_and_target = math.atan2(target_y - self.bot_pos.y,
target_x - self.bot_pos.x)
angle_front_to_target = angle_between_bot_and_target - self.bot_yaw
# Correct the values
if angle_front_to_target < -math.pi:
angle_front_to_target += 2 * math.pi
if angle_front_to_target > math.pi:
angle_front_to_target -= 2 * math.pi
if angle_front_to_target < math.radians(-10):
# If the target is more than 10 degrees right from the centre, steer left
self.controller.steer = -1
elif angle_front_to_target > math.radians(10):
# If the target is more than 10 degrees left from the centre, steer right
self.controller.steer = 1
else:
# If the target is less than 10 degrees from the centre, steer straight
self.controller.steer = 0
def dodge(self):
if self.should_dodge and time.time() > self.next_dodge_time:
self.controller.jump = True
self.controller.pitch = -1
if self.on_second_jump:
self.on_second_jump = False
self.should_dodge = False
else:
self.on_second_jump = True
self.next_dodge_time = time.time() + self.DODGE_TIME
def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
# Update game data variables
self.bot_yaw = packet.game_cars[self.index].physics.rotation.yaw
self.bot_pos = packet.game_cars[self.index].physics.location
ball_pos = packet.game_ball.physics.location
self.aim(ball_pos.x, ball_pos.y)
self.controller.jump = 0