-
Notifications
You must be signed in to change notification settings - Fork 0
/
isaac_env.py
306 lines (269 loc) · 10.1 KB
/
isaac_env.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
import sys
import signal
import socket
import gym
import json
import numpy as np
import cv2
import tqdm
from itertools import product
from time import sleep
from utils.virtual_controller import VirtualKeyboard
from utils.img import ImageCapture
from utils.utils import changeWindowName, kill_process, kill_steam, run_game, kill_game
from utils.wrappers import FrameStack, MaxAndSkipEnv, ScaledFloatFrame, WarpFrame
from speedhack.speedhack import run_speed_hack
from gym import spaces
gym.logger.set_level(40)
RECV_SIZE = 4096
class Isaac(gym.Env):
def __init__(
self,
speed_hack=True,
use_virtual_controller=True,
max_timesteps=None,
max_enemies_to_observe=1,
max_enemy_projectiles_to_observe=15,
max_tears_to_observe=5,
_semaphore=None,
_alter_window_name=True,
) -> None:
super().__init__()
if _semaphore:
_semaphore.acquire(block=True)
self.max_enemies = max_enemies_to_observe
self.max_projectiles = max_enemy_projectiles_to_observe
self.max_tears = max_tears_to_observe
self.max_steps = max_timesteps
self.use_virtual_controller = use_virtual_controller
# generate action dict
p = list(product(range(0, 5), repeat=2))
self.action_dict = {
k: self._tuple_to_vect(v) for k, v in zip(list(range(len(p))), p)
}
self.observation_shape = (
2
+ 2
+ self.max_tears * 4
+ self.max_enemies * 4
+ self.max_projectiles * 4,
)
self.observation_space = spaces.Box(
low=np.finfo(np.float32).min,
high=np.finfo(np.float32).max,
shape=self.observation_shape,
dtype=np.float32,
)
# init action space
self.action_space = spaces.Discrete(
len(p),
)
self.gamestate = {}
self.previous_gamestate = {}
exe_path = "C:/program files (x86)/steam/Steam.exe"
args = [
"C:/program files (x86)/steam/Steam.exe",
"-applaunch",
"250900",
"--set-stage=1",
"--set-stage-type=0",
]
kill_steam()
self.game_process = run_game(exe_path, args)
self.windown_name = (
f"{self.game_process.pid}"
if _alter_window_name
else f"Binding of Isaac: Repentance"
)
if speed_hack:
run_speed_hack("Binding of Isaac: Repentance", 500)
# Open connection with game
self.host, self.port = "127.0.0.1", 6942
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.setblocking(True)
self.connected = False
while not self.connected:
try:
self.s.connect((self.host, self.port))
self.connected = True
# print("Successful connection with game.", flush=True)
changeWindowName("Binding of Isaac: Repentance", self.windown_name)
except:
# print("Failed to connect to game, retrying...", flush=True)
sleep(1)
self.controller = VirtualKeyboard(self.windown_name, wait=0.5)
self.capturer = ImageCapture(self.windown_name)
self._current_step = 0
if _semaphore:
_semaphore.release()
@staticmethod
def _tuple_to_vect(tuple, sizes=[4, 4]):
vectors = [np.zeros(i) for i in sizes]
for idx, v in enumerate(tuple):
if v != 0:
vectors[idx][v - 1] = 1.0
return np.concatenate(vectors)
def compute_reward(self):
if self.previous_gamestate:
dmg_taken = (
self.gamestate["damagesTaken"] - self.previous_gamestate["damagesTaken"]
)
mob_dmg_taken = (
self.gamestate["mobDamagesTaken"]
- self.previous_gamestate["mobDamagesTaken"]
)
mob_dmg_taken = 1.0 if mob_dmg_taken != 0 else 0.0
mob_killed = (
self.gamestate["mobKilled"] - self.previous_gamestate["mobKilled"]
)
# ======
is_too_close = 0
for e in range(len(self.gamestate["enemy_positions"])):
dist_from_enemy = np.sqrt(
(
self.gamestate["player_position"][0]
- self.gamestate["enemy_positions"][e][0]
)
** 2
+ (
self.gamestate["player_position"][1]
- self.gamestate["enemy_positions"][e][1]
)
** 2
)
is_too_close += (
1.0 if dist_from_enemy > 270 or dist_from_enemy < 160 else -1.0
)
# ======
has_died = self.gamestate["isDead"]
cleared = not self.gamestate["isDead"] and self.gamestate["done"]
return (
is_too_close * -0.2
+ dmg_taken * -20.0
+ mob_killed * 20.0
+ has_died * -0.0
+ cleared * 20.0
+ mob_dmg_taken * 5.0
)
return 0.0
def observe(self, gamestate):
observation = np.zeros(self.observation_space.shape)
ptr = 0
# player infos
observation[ptr : ptr + 4] = (
gamestate["player_position"] + gamestate["player_velocity"]
)
observation[ptr : ptr + 4] /= [570, 410, 10, 10]
ptr = ptr + 4
# enemies infos
for i in range(self.max_enemies):
if i < len(gamestate["enemy_positions"]):
observation[ptr : ptr + 5] = (
gamestate["enemy_positions"][i]
+ gamestate["enemy_velocities"][i]
+ [gamestate["enemy_healths"][i]]
)
observation[ptr : ptr + 5] /= [570, 410, 10, 10, 300]
ptr += 4
# enemies projectiles infos
for i in range(self.max_projectiles):
if i < len(gamestate["enemy_projectiles_positions"]):
observation[ptr : ptr + 4] = (
gamestate["enemy_projectiles_positions"][i]
+ gamestate["enemy_projectiles_velocities"][i]
)
observation[ptr : ptr + 4] /= [570, 410, 10, 10]
ptr += 4
# tears infos
for i in range(self.max_tears):
if i < len(gamestate["tears_positions"]):
observation[ptr : ptr + 4] = (
gamestate["tears_positions"][i] + gamestate["tears_velocities"][i]
)
observation[ptr : ptr + 4] /= [570, 410, 10, 10]
ptr += 4
return observation
def reset(self, return_info=False, options=None, seed=None):
self._current_step = 0
self.gamestate = {}
self.previous_gamestate = {}
self.controller.update(self.controller.get_null_action())
request = {
"action": "reset",
"use_virtual_controller": self.use_virtual_controller,
}
self.s.sendall((json.dumps(request) + "\n").encode("utf-8"))
msg = self.s.recv(RECV_SIZE)
if msg == b"":
raise Exception("Connection Lost")
gamestate = json.loads(msg)
# observation = self.capturer.capture_frame() # => not reliable
observation = self.observe(gamestate)
if not return_info:
return observation
else:
return observation, {}
def step(self, action):
"""obs, reward, done, infos"""
act_to_send_low = np.where(self.action_dict[action][0:4] == 1.0)[0]
act_to_send_high = np.where(self.action_dict[action][4::] == 1.0)[0]
if not act_to_send_low:
act_to_send_low = -1
if not act_to_send_high:
act_to_send_low = -1
# Get subsidiary information like reward or done
## Send instruction to game
request = {
"action": "step",
}
if not self.use_virtual_controller:
request["player_move"] = int(act_to_send_high)
request["player_fire"] = int(act_to_send_low)
self.s.sendall((json.dumps(request) + "\n").encode("utf-8"))
if self.use_virtual_controller:
self.controller.update(self.action_dict[action])
## Receive gamestate from game (if state is terminal for example)
msg = self.s.recv(RECV_SIZE)
if msg == b"":
raise Exception("Connection Lost")
gamestate = json.loads(msg)
# Observe the environment
# observation = self.capturer.capture_frame() # => do not work reliably
observation = self.observe(gamestate)
if gamestate["done"]:
# Send input
if self.use_virtual_controller:
self.controller.update(self.controller.get_null_action())
self.previous_gamestate = self.gamestate
self.gamestate = gamestate
reward = self.compute_reward()
if self.max_steps and self._current_step >= self.max_steps:
return observation, reward, True, gamestate
self._current_step += 1
return observation, reward, gamestate["done"], gamestate
def close(self):
print("Closing environement")
# request = {"action": "close"}
# self.s.sendall((json.dumps(request) + "\n").encode("utf-8"))
self.s.close()
kill_process(self.windown_name)
def signal_handler(sig, frame):
kill_game()
sys.exit(0)
# register environment
gym.envs.register(id="isaac-v0", entry_point="isaac_env:Isaac")
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
# env = Isaac(speed_hack=True)
env = gym.make("isaac-v0", speed_hack=False)
obs = env.reset()
done = False
count = 0
# for i in tqdm.trange(0, 1000):
while True:
if done:
env.reset()
action = env.action_space.sample()
obs, reward, done, info = env.step(action)
# print(f"Step {count} => {action}")
count += 1