-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcy_impulse_wars.pyx
179 lines (158 loc) · 5.56 KB
/
cy_impulse_wars.pyx
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
from libc.stdint cimport int32_t, uint8_t, uint16_t, uint64_t
from libc.stdlib cimport calloc, free
import pufferlib
from impulse_wars cimport (
MAX_DRONES,
CONTINUOUS_ACTION_SIZE,
scalarObsSize,
obsBytes,
alignedSize,
MAP_OBS_SIZE,
NUM_WALL_TYPES,
NUM_WEAPONS,
MAP_OBS_ROWS,
MAP_OBS_COLUMNS,
NUM_NEAR_WALL_OBS,
NEAR_WALL_POS_OBS_SIZE,
NEAR_WALL_OBS_SIZE,
NEAR_WALL_TYPES_OBS_OFFSET,
NEAR_WALL_POS_OBS_OFFSET,
NUM_FLOATING_WALL_OBS,
FLOATING_WALL_OBS_SIZE,
FLOATING_WALL_INFO_OBS_SIZE,
FLOATING_WALL_TYPES_OBS_OFFSET,
FLOATING_WALL_INFO_OBS_OFFSET,
NUM_WEAPON_PICKUP_OBS,
WEAPON_PICKUP_POS_OBS_SIZE,
WEAPON_PICKUP_OBS_SIZE,
WEAPON_PICKUP_TYPES_OBS_OFFSET,
WEAPON_PICKUP_POS_OBS_OFFSET,
NUM_PROJECTILE_OBS,
PROJECTILE_INFO_OBS_SIZE,
PROJECTILE_OBS_SIZE,
PROJECTILE_TYPES_OBS_OFFSET,
PROJECTILE_POS_OBS_OFFSET,
ENEMY_DRONE_OBS_OFFSET,
ENEMY_DRONE_OBS_SIZE,
DRONE_OBS_SIZE,
MISC_OBS_SIZE,
env,
initEnv,
rayClient,
createRayClient,
destroyRayClient,
resetEnv,
stepEnv,
destroyEnv,
LOG_BUFFER_SIZE,
logBuffer,
logEntry,
createLogBuffer,
destroyLogBuffer,
aggregateAndClearLogBuffer,
)
# doesn't seem like you can directly import C or Cython constants
# from Python so we have to create wrapper functions
def maxDrones() -> int:
return MAX_DRONES
def continuousActionsSize() -> int:
return CONTINUOUS_ACTION_SIZE
def obsConstants(numDrones: int) -> pufferlib.Namespace:
droneObsOffset = ENEMY_DRONE_OBS_OFFSET + ((numDrones - 1) * ENEMY_DRONE_OBS_SIZE)
return pufferlib.Namespace(
obsBytes=obsBytes(numDrones),
mapObsSize=MAP_OBS_SIZE,
scalarObsSize=scalarObsSize(numDrones),
scalarObsBytes=scalarObsSize(numDrones) * sizeof(float),
wallTypes=NUM_WALL_TYPES,
weaponTypes=NUM_WEAPONS + 1,
mapObsRows=MAP_OBS_ROWS,
mapObsColumns=MAP_OBS_COLUMNS,
scalarObsOffset=alignedSize(MAP_OBS_SIZE, sizeof(float)),
numNearWallObs=NUM_NEAR_WALL_OBS,
nearWallPosObsSize=NEAR_WALL_POS_OBS_SIZE,
nearWallObsSize=NEAR_WALL_OBS_SIZE,
nearWallTypesObsOffset=NEAR_WALL_TYPES_OBS_OFFSET,
nearWallPosObsOffset=NEAR_WALL_POS_OBS_OFFSET,
numFloatingWallObs=NUM_FLOATING_WALL_OBS,
floatingWallInfoObsSize=FLOATING_WALL_INFO_OBS_SIZE,
floatingWallObsSize=FLOATING_WALL_OBS_SIZE,
floatingWallTypesObsOffset=FLOATING_WALL_TYPES_OBS_OFFSET,
floatingWallInfoObsOffset=FLOATING_WALL_INFO_OBS_OFFSET,
numWeaponPickupObs=NUM_WEAPON_PICKUP_OBS,
weaponPickupPosObsSize=WEAPON_PICKUP_POS_OBS_SIZE,
weaponPickupObsSize=WEAPON_PICKUP_OBS_SIZE,
weaponPickupTypesObsOffset=WEAPON_PICKUP_TYPES_OBS_OFFSET,
weaponPickupPosObsOffset=WEAPON_PICKUP_POS_OBS_OFFSET,
numProjectileObs=NUM_PROJECTILE_OBS,
projectileInfoObsSize=PROJECTILE_INFO_OBS_SIZE,
projectileObsSize=PROJECTILE_OBS_SIZE,
projectileTypesObsOffset=PROJECTILE_TYPES_OBS_OFFSET,
projectilePosObsOffset=PROJECTILE_POS_OBS_OFFSET,
enemyDroneObsOffset=ENEMY_DRONE_OBS_OFFSET,
enemyDroneObsSize=ENEMY_DRONE_OBS_SIZE,
droneObsOffset=droneObsOffset,
droneObsSize=DRONE_OBS_SIZE,
miscObsSize=MISC_OBS_SIZE,
miscObsOffset=droneObsOffset + DRONE_OBS_SIZE,
)
cdef class CyImpulseWars:
cdef:
uint16_t numEnvs
uint8_t numDrones
bint render
env* envs
logBuffer *logs
rayClient* rayClient
def __init__(self, uint16_t numEnvs, uint8_t numDrones, uint8_t numAgents, uint8_t[:, :] observations, bint discretizeActions, float[:, :] contActions, int32_t[:, :] discActions, float[:] rewards, uint8_t[:] terminals, uint8_t[:] truncations, uint64_t seed, bint render, bint sittingDuck, bint isTraining, bint humanControl):
self.numEnvs = numEnvs
self.numDrones = numDrones
self.render = render
self.envs = <env*>calloc(numEnvs, sizeof(env))
self.logs = createLogBuffer(LOG_BUFFER_SIZE)
cdef int inc = numAgents
cdef int i
for i in range(self.numEnvs):
initEnv(
&self.envs[i],
numDrones,
numAgents,
&observations[i * inc, 0],
discretizeActions,
&contActions[i * inc, 0],
&discActions[i * inc, 0],
&rewards[i * inc],
&terminals[i * inc],
&truncations[i * inc],
self.logs,
seed + i,
sittingDuck,
isTraining,
)
self.envs[i].humanInput = humanControl
cdef _initRaylib(self):
self.rayClient = createRayClient()
cdef int i
for i in range(self.numEnvs):
self.envs[i].client = self.rayClient
def reset(self):
if self.render and self.rayClient == NULL:
self._initRaylib()
cdef int i
for i in range(self.numEnvs):
resetEnv(&self.envs[i])
def step(self):
cdef int i
for i in range(self.numEnvs):
stepEnv(&self.envs[i])
def log(self):
cdef logEntry log = aggregateAndClearLogBuffer(self.numDrones, self.logs)
return log
def close(self):
cdef int i
for i in range(self.numEnvs):
destroyEnv(&self.envs[i])
destroyLogBuffer(self.logs)
free(self.envs)
if self.rayClient != NULL:
destroyRayClient(self.rayClient)