forked from RLBot/RLBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_data_struct.py
263 lines (211 loc) · 10.6 KB
/
game_data_struct.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
import ctypes
import mmap
MAX_PLAYERS = 10
MAX_NAME_LENGTH = 32
MAX_BOOSTS = 50
SHARED_MEMORY_TAG = 'Local\\RLBotOutput'
class Vector3(ctypes.Structure):
_fields_ = [("X", ctypes.c_float),
("Y", ctypes.c_float),
("Z", ctypes.c_float)]
class Rotator(ctypes.Structure):
_fields_ = [("Pitch", ctypes.c_int),
("Yaw", ctypes.c_int),
("Roll", ctypes.c_int)]
class Touch(ctypes.Structure):
_fields_ = [("wPlayerName", ctypes.c_wchar * MAX_NAME_LENGTH),
("fTimeSeconds", ctypes.c_float),
("sHitLocation", Vector3),
("sHitNormal", Vector3)]
class ScoreInfo(ctypes.Structure):
_fields_ = [("Score", ctypes.c_int),
("Goals", ctypes.c_int),
("OwnGoals", ctypes.c_int),
("Assists", ctypes.c_int),
("Saves", ctypes.c_int),
("Shots", ctypes.c_int),
("Demolitions", ctypes.c_int)]
class PlayerInfo(ctypes.Structure):
_fields_ = [("Location", Vector3),
("Rotation", Rotator),
("Velocity", Vector3),
("AngularVelocity", Vector3),
("Score", ScoreInfo),
("bDemolished", ctypes.c_bool),
# True if your wheels are on the ground, the wall, or the ceiling. False if you're midair or turtling.
("bOnGround", ctypes.c_bool),
("bSuperSonic", ctypes.c_bool),
("bBot", ctypes.c_bool),
# True if the player has jumped. Falling off the ceiling / driving off the goal post does not count.
("bJumped", ctypes.c_bool),
# True if player has double jumped. False does not mean you have a jump remaining, because the
# aerial timer can run out, and that doesn't affect this flag.
("bDoubleJumped", ctypes.c_bool),
("wName", ctypes.c_wchar * MAX_NAME_LENGTH),
("Team", ctypes.c_ubyte),
("Boost", ctypes.c_int)]
class BallInfo(ctypes.Structure):
_fields_ = [("Location", Vector3),
("Rotation", Rotator),
("Velocity", Vector3),
("AngularVelocity", Vector3),
("Acceleration", Vector3),
("LatestTouch", Touch)]
class BoostInfo(ctypes.Structure):
_fields_ = [("Location", Vector3),
("bActive", ctypes.c_bool),
("Timer", ctypes.c_int)]
class GameInfo(ctypes.Structure):
_fields_ = [("TimeSeconds", ctypes.c_float),
("GameTimeRemaining", ctypes.c_float),
("bOverTime", ctypes.c_bool),
("bUnlimitedTime", ctypes.c_bool),
# True when cars are allowed to move, and during the pause menu. False during replays.
("bRoundActive", ctypes.c_bool),
# Only false during a kickoff, when the car is allowed to move, and the ball has not been hit,
# and the game clock has not started yet. If both players sit still, game clock will eventually
# start and this will become true.
("bBallHasBeenHit", ctypes.c_bool),
# Turns true after final replay, the moment the 'winner' screen appears. Remains true during next match
# countdown. Turns false again the moment the 'choose team' screen appears.
("bMatchEnded", ctypes.c_bool)]
# On the c++ side this struct has a long at the beginning for locking. This flag is removed from this struct so it isn't visible to users.
class GameTickPacket(ctypes.Structure):
_fields_ = [("gamecars", PlayerInfo * MAX_PLAYERS),
("numCars", ctypes.c_int),
("gameBoosts", BoostInfo * MAX_BOOSTS),
("numBoosts", ctypes.c_int),
("gameball", BallInfo),
("gameInfo", GameInfo)]
# Fully matching c++ struct
class GameTickPacketWithLock(ctypes.Structure):
_fields_ = [("lock", ctypes.c_long),
("iLastError", ctypes.c_int),
("gamecars", PlayerInfo * MAX_PLAYERS),
("numCars", ctypes.c_int),
("gameBoosts", BoostInfo * MAX_BOOSTS),
("numBoosts", ctypes.c_int),
("gameball", BallInfo),
("gameInfo", GameInfo)]
def print_vector_3(vector):
print("(X,Y,Z): " + str(round(vector.X, 2)) + "," + str(round(vector.Y, 2)) + "," + str(round(vector.Z, 2)))
def print_rotator(rotator):
print("(Pitch,Yaw,Roll): " + str(rotator.Pitch) + "," + str(rotator.Yaw) + "," + str(rotator.Roll))
def print_score_info(scoreInfo):
print("Score: " + str(scoreInfo.Score))
print("Goals: " + str(scoreInfo.Goals))
print("OwnGoals: " + str(scoreInfo.OwnGoals))
print("Assists: " + str(scoreInfo.Assists))
print("Saves: " + str(scoreInfo.Saves))
print("Shots: " + str(scoreInfo.Shots))
print("Demolitions: " + str(scoreInfo.Demolitions))
def print_player_info(index, playerInfo):
print("Car " + str(index))
print("Name: " + str(playerInfo.wName))
print("Team: " + str(playerInfo.Team))
print("Bot: " + str(playerInfo.bBot))
print("Location:")
print_vector_3(playerInfo.Location)
print("Rotation:")
print_rotator(playerInfo.Rotation)
print("Velocity:")
print_vector_3(playerInfo.Velocity)
print("Angular Velocity:")
print_vector_3(playerInfo.AngularVelocity)
print("SuperSonic: " + str(playerInfo.bSuperSonic))
print("Demolished: " + str(playerInfo.bDemolished))
print("Boost: " + str(playerInfo.Boost))
print("Score Info: ")
print_score_info(playerInfo.Score)
def print_ball_info(ballInfo):
print("Location:")
print_vector_3(ballInfo.Location)
print("Rotation:")
print_rotator(ballInfo.Rotation)
print("Velocity:")
print_vector_3(ballInfo.Velocity)
print("Angular Velocity:")
print_vector_3(ballInfo.AngularVelocity)
print("Acceleration:")
print_vector_3(ballInfo.Acceleration)
print("LatestHit:")
print_vector_3(ballInfo.LatestTouch.sHitLocation)
def print_boost_info(index, boostInfo):
print("Boost Pad " + str(index))
print("Location:")
print_vector_3(boostInfo.Location)
print("Active: " + str(boostInfo.bActive))
print("Timer: " + str(boostInfo.Timer))
def print_game_info(gameInfo):
print("Seconds: " + str(gameInfo.TimeSeconds))
print("Game Time Remaining: " + str(gameInfo.GameTimeRemaining))
print("Overtime: " + str(gameInfo.bOverTime))
def print_game_tick_packet_with_lock(gameTickPacket):
print("Lock: " + str(gameTickPacket.lock))
print("Last Error: " + str(gameTickPacket.iLastError))
print("NumCars: " + str(gameTickPacket.numCars))
print("NumBoosts: " + str(gameTickPacket.numBoosts))
print()
print_game_info(gameTickPacket.gameInfo)
print()
print("Ball Info:")
print_ball_info(gameTickPacket.gameball)
for i in range(gameTickPacket.numCars):
print()
print_player_info(i, gameTickPacket.gamecars[i])
for i in range(gameTickPacket.numBoosts):
print()
print_boost_info(i, gameTickPacket.gameBoosts[i])
def print_game_tick_packet(gameTickPacket):
print("NumCars: " + str(gameTickPacket.numCars))
print("NumBoosts: " + str(gameTickPacket.numBoosts))
print()
print_game_info(gameTickPacket.gameInfo)
print()
print("Ball Info:")
print_ball_info(gameTickPacket.gameball)
for i in range(gameTickPacket.numCars):
print()
print_player_info(i, gameTickPacket.gamecars[i])
for i in range(gameTickPacket.numBoosts):
print()
print_boost_info(i, gameTickPacket.gameBoosts[i])
# This negates all x and y values for balls and cars and rotates yaw 180 degrees.
def rotate_game_tick_packet_boost_omitted(game_tick_packet):
# Negate all x,y values for ball
game_tick_packet.gameball.Location.X = -1 * game_tick_packet.gameball.Location.X
game_tick_packet.gameball.Location.Y = -1 * game_tick_packet.gameball.Location.Y
game_tick_packet.gameball.Velocity.X = -1 * game_tick_packet.gameball.Velocity.X
game_tick_packet.gameball.Velocity.Y = -1 * game_tick_packet.gameball.Velocity.Y
# Angular velocity is stored on global axis so negating on x and y does make sense!
game_tick_packet.gameball.AngularVelocity.X = -1 * game_tick_packet.gameball.AngularVelocity.X
game_tick_packet.gameball.AngularVelocity.Y = -1 * game_tick_packet.gameball.AngularVelocity.Y
game_tick_packet.gameball.Acceleration.X = -1 * game_tick_packet.gameball.Acceleration.X
game_tick_packet.gameball.Acceleration.Y = -1 * game_tick_packet.gameball.Acceleration.Y
# ball touch data
game_tick_packet.gameball.LatestTouch.sHitLocation.X = -1 * game_tick_packet.gameball.LatestTouch.sHitLocation.X
game_tick_packet.gameball.LatestTouch.sHitLocation.Y = -1 * game_tick_packet.gameball.LatestTouch.sHitLocation.Y
game_tick_packet.gameball.LatestTouch.sHitNormal.X = -1 * game_tick_packet.gameball.LatestTouch.sHitNormal.X
game_tick_packet.gameball.LatestTouch.sHitNormal.Y = -1 * game_tick_packet.gameball.LatestTouch.sHitNormal.Y
# Rotate Yaw 180 degrees is all that is necessary.
ball_yaw = game_tick_packet.gameball.Rotation.Yaw
game_tick_packet.gameball.Rotation.Yaw = ball_yaw + 32768 if ball_yaw < 0 else ball_yaw - 32768
for i in range(game_tick_packet.numCars):
game_tick_packet.gamecars[i].Location.X = -1 * game_tick_packet.gamecars[i].Location.X
game_tick_packet.gamecars[i].Location.Y = -1 * game_tick_packet.gamecars[i].Location.Y
game_tick_packet.gamecars[i].Velocity.X = -1 * game_tick_packet.gamecars[i].Velocity.X
game_tick_packet.gamecars[i].Velocity.Y = -1 * game_tick_packet.gamecars[i].Velocity.Y
game_tick_packet.gamecars[i].AngularVelocity.X = -1 * game_tick_packet.gamecars[i].AngularVelocity.X
game_tick_packet.gamecars[i].AngularVelocity.Y = -1 * game_tick_packet.gamecars[i].AngularVelocity.Y
car_yaw = game_tick_packet.gamecars[i].Rotation.Yaw
game_tick_packet.gamecars[i].Rotation.Yaw = car_yaw + 32768 if car_yaw < 0 else car_yaw - 32768
# Running this file will read from shared memory and display contents
if __name__ == '__main__':
# Open anonymous shared memory for entire GameInputPacket
buff = mmap.mmap(-1, ctypes.sizeof(GameTickPacketWithLock), SHARED_MEMORY_TAG)
# Map buffer to ctypes structure
gameOutputPacket = GameTickPacketWithLock.from_buffer(buff)
# gameOutputPacket.numCars = 10 # Example write
# gameOutputPacket.numBoosts = 50 # Example write
# Print struct
print_game_tick_packet_with_lock(gameOutputPacket)