-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbot.py
75 lines (62 loc) · 2.32 KB
/
bot.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
import numpy as np
import torch
from rlbot.agents.base_agent import BaseAgent, SimpleControllerState
from rlbot.utils.structures.game_data_struct import GameTickPacket
from rlgym_compat import GameState
from agent import Agent
from your_obs import YourOBS
class RLGymPPOBot(BaseAgent):
def __init__(self, name, team, index):
super().__init__(name, team, index)
self.obs_builder = YourOBS()
self.agent = Agent()
self.tick_skip = your_tick_skip_here
self.game_state: GameState = None
self.controls = None
self.action = None
self.update_action = True
self.ticks = 0
self.prev_time = 0
print('====================================')
print('RLGym-PPO Bot Ready - Index:', index)
print('Make sure your FPS is at 120, 240, or 360!')
print('====================================')
def is_hot_reload_enabled(self):
return True
def initialize_agent(self):
# Initialize the rlgym GameState object now that the game is active and the info is available
self.game_state = GameState(self.get_field_info())
self.update_action = True
self.ticks = self.tick_skip # So we take an action the first tick
self.prev_time = 0
self.controls = SimpleControllerState()
self.action = np.zeros(8)
def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
cur_time = packet.game_info.seconds_elapsed
delta = cur_time - self.prev_time
self.prev_time = cur_time
ticks_elapsed = round(delta * 120)
self.ticks += ticks_elapsed
self.game_state.decode(packet, ticks_elapsed)
# We calculate the next action as soon as the prev action is sent
# This gives you tick_skip ticks to do your forward pass
if self.update_action:
self.update_action = False
player = self.game_state.players[self.index]
obs = self.obs_builder.build_obs(player, self.game_state, self.action)
self.action = self.agent.act(obs)
if self.ticks >= self.tick_skip - 1:
self.update_controls(self.action)
if self.ticks >= self.tick_skip:
self.ticks = 0
self.update_action = True
return self.controls
def update_controls(self, action):
self.controls.throttle = action[0]
self.controls.steer = action[1]
self.controls.pitch = action[2]
self.controls.yaw = action[3]
self.controls.roll = action[4]
self.controls.jump = action[5] > 0
self.controls.boost = action[6] > 0
self.controls.handbrake = action[7] > 0