-
Notifications
You must be signed in to change notification settings - Fork 2
/
minecraft_client.py
412 lines (356 loc) · 13.1 KB
/
minecraft_client.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import os
import json
import math
import pickle
import signal
import shutil
import base64
import argparse
from time import sleep, time
from tagilmo.utils.vereya_wrapper import MCConnector, RobustObserver
import tagilmo.utils.mission_builder as mb
from tagilmo.utils.mathutils import normAngle, degree2rad
import numpy as np
from airis_session import AirisSession, AgentAirisSession
API_URL = "http://127.0.0.1:8000/api"
session_running = True
def signal_handler(sig, frame):
global session_running
global mc
session_running = False
if 'mc' in globals():
del mc
def signal_handler_dummy(sig, frame):
pass
def get_cli_args():
parser = argparse.ArgumentParser(description='Python client for executing AIRIS')
parser.add_argument('--restore', action='store_true', help='Restore client session')
parser.add_argument('--agent', type=str, help='The agent address to connect to')
return parser.parse_args()
def read_session_id():
file_path = 'session_id.json'
try:
with open(file_path, 'r') as file:
data = json.load(file)
except FileNotFoundError:
print(f"Error: The file {file_path} was not found.")
except json.JSONDecodeError:
print(f"Error: The file {file_path} does not contain valid JSON.")
except Exception as e:
print(f"An error occurred: {str(e)}")
else:
return data['session_id']
def save_session_id(session_id):
file_path = 'session_id.json'
try:
with open(file_path, 'w') as file:
json.dump({"session_id": session_id}, file)
except Exception as e:
print(f"An error occurred: {str(e)}")
def lookDir(rob, pitch, yaw):
for t in range(3000):
sleep(0.02) # wait for action
aPos = rob.waitNotNoneObserve('getAgentPos')
dPitch = normAngle(degree2rad(pitch) - degree2rad(aPos[3]))
dYaw = normAngle(degree2rad(yaw) - degree2rad(aPos[4]))
if abs(dPitch) < 0.006 and abs(dYaw) < 0.006:
sleep(0.02) # sleep
break
rob.sendCommand("turn " + str(dYaw * 0.4))
rob.sendCommand("pitch " + str(dPitch * 0.4))
sleep(0.02)
rob.sendCommand("turn 0")
rob.sendCommand("pitch 0")
sleep(0.02)
def center(rob, pos, o_pitch, o_yaw):
dist = lookAt(rob, pos)
timeout = time()
if dist > 0.2:
rob.sendCommand('move .2')
while dist > 0.2:
if time() > timeout + 1:
break
dist = lookAt(rob, pos)
rob.sendCommand('move 0')
lookDir(rob, o_pitch, o_yaw)
def lookAt(rob, pos):
pos = [pos[0] + .5, pos[1], pos[2] + .5]
dist = 0
timeout = time()
for t in range(3000):
sleep(0.02)
aPos = rob.waitNotNoneObserve('getAgentPos')
dist = math.sqrt((aPos[0] - pos[0]) * (aPos[0] - pos[0]) + (aPos[2] - pos[2]) * (aPos[2] - pos[2]))
if dist < 0.5:
break
[pitch, yaw] = mc.dirToPos(aPos, pos)
pitch = normAngle(pitch - degree2rad(aPos[3]))
yaw = normAngle(yaw - degree2rad(aPos[4]))
if abs(pitch) < 0.02 and abs(yaw) < 0.02: break
rob.sendCommand("turn " + str(yaw * 0.4))
rob.sendCommand("pitch " + str(pitch * 0.4))
sleep(0.02)
if time() > timeout + 1:
break
rob.sendCommand("turn 0")
rob.sendCommand("pitch 0")
sleep(0.02)
return dist
def jump_forward(rob, stats):
a_stats = [mc.getFullStat(key) for key in fullStatKeys]
o_pitch = round(a_stats[3])
o_yaw = round(a_stats[4]) % 360
s_pos = [math.floor(stats[0]), math.floor(stats[1]), math.floor(stats[2])]
e_pos = s_pos
match o_yaw:
case 0:
e_pos = [s_pos[0], s_pos[1], s_pos[2] + 1]
case 45:
e_pos = [s_pos[0] - 1, s_pos[1], s_pos[2] + 1]
case 90:
e_pos = [s_pos[0] - 1, s_pos[1], s_pos[2]]
case 135:
e_pos = [s_pos[0] - 1, s_pos[1], s_pos[2] - 1]
case 180:
e_pos = [s_pos[0], s_pos[1], s_pos[2] - 1]
case 225:
e_pos = [s_pos[0] + 1, s_pos[1], s_pos[2] - 1]
case 270:
e_pos = [s_pos[0] + 1, s_pos[1], s_pos[2]]
case 315:
e_pos = [s_pos[0] + 1, s_pos[1], s_pos[2] + 1]
lookAt(rob, e_pos)
rob.sendCommand('move 1')
rob.sendCommand('jump 1')
timeout = time()
timedout = False
while [math.floor(stats[0]), math.floor(stats[1]), math.floor(stats[2])] != [e_pos[0], math.floor(stats[1]), e_pos[2]]:
lookAt(rob, e_pos)
if time() > timeout + 1:
timedout = True
break
stats = [mc.getFullStat(key) for key in fullStatKeys]
rob.sendCommand('move 0')
rob.sendCommand('jump 0')
sleep(.02)
lookDir(rob, o_pitch, o_yaw)
stats = [mc.getFullStat(key) for key in fullStatKeys]
old_stats = [mc.getFullStat(key) for key in fullStatKeys]
sleep(.5)
stats = [mc.getFullStat(key) for key in fullStatKeys]
while old_stats[1] != stats[1]:
old_stats = [mc.getFullStat(key) for key in fullStatKeys]
sleep(.05)
stats = [mc.getFullStat(key) for key in fullStatKeys]
if old_stats[1] == stats[1]:
old_stats = [mc.getFullStat(key) for key in fullStatKeys]
sleep(.05)
stats = [mc.getFullStat(key) for key in fullStatKeys]
def move_forward(rob, stats):
a_stats = [mc.getFullStat(key) for key in fullStatKeys]
o_pitch = round(a_stats[3])
o_yaw = round(a_stats[4]) % 360
s_pos = [math.floor(stats[0]), math.floor(stats[1]), math.floor(stats[2])]
e_pos = s_pos
match o_yaw:
case 0:
e_pos = [s_pos[0], s_pos[1], s_pos[2] + 1]
case 45:
e_pos = [s_pos[0] - 1, s_pos[1], s_pos[2] + 1]
case 90:
e_pos = [s_pos[0] - 1, s_pos[1], s_pos[2]]
case 135:
e_pos = [s_pos[0] - 1, s_pos[1], s_pos[2] - 1]
case 180:
e_pos = [s_pos[0], s_pos[1], s_pos[2] - 1]
case 225:
e_pos = [s_pos[0] + 1, s_pos[1], s_pos[2] - 1]
case 270:
e_pos = [s_pos[0] + 1, s_pos[1], s_pos[2]]
case 315:
e_pos = [s_pos[0] + 1, s_pos[1], s_pos[2] + 1]
lookAt(rob, e_pos)
rob.sendCommand('move 1')
timeout = time()
timedout = False
while [math.floor(stats[0]), math.floor(stats[1]), math.floor(stats[2])] != e_pos:
lookAt(rob, e_pos)
if time() > timeout + 1:
timedout = True
break
stats = [mc.getFullStat(key) for key in fullStatKeys]
if math.floor(stats[1]) != math.floor(e_pos[1]):
break
rob.sendCommand('move 0')
sleep(.05)
lookDir(rob, o_pitch, o_yaw)
stats = [mc.getFullStat(key) for key in fullStatKeys]
old_stats = [mc.getFullStat(key) for key in fullStatKeys]
sleep(.5)
stats = [mc.getFullStat(key) for key in fullStatKeys]
while old_stats[1] != stats[1]:
old_stats = [mc.getFullStat(key) for key in fullStatKeys]
sleep(.05)
stats = [mc.getFullStat(key) for key in fullStatKeys]
if old_stats[1] == stats[1]:
old_stats = [mc.getFullStat(key) for key in fullStatKeys]
sleep(.05)
stats = [mc.getFullStat(key) for key in fullStatKeys]
def main_loop(mc, rob, airis_session):
stats = [mc.getFullStat(key) for key in fullStatKeys]
stats = [math.floor(stats[0]), math.floor(stats[1]), math.floor(stats[2]), round(stats[3]), round(stats[4]) % 360] # round(stats[4]) % 360]
grid = mc.getNearGrid()
environment_state = {
'position':stats,
'nearby_grid':grid
}
action, state_output, edges_output = airis_session.pre_action(environment_state)
grid = np.array(grid)
grid_3d = grid.reshape((5, 5, 5))
grid_output = dict()
for yi, y in enumerate(grid_3d):
for zi, z in enumerate(grid_3d[yi]):
for xi, x in enumerate(grid_3d[yi][zi]):
if x != 'air':
grid_output[(stats[1] + yi - grid_origin_y, stats[2] + zi - grid_origin_z, stats[0] + xi - grid_origin_x)] = (stats[0] + xi - grid_origin_x, stats[1] + yi - grid_origin_y, stats[2] + zi - grid_origin_z, x)
state_output, edges_output = base64.b64decode(state_output), base64.b64decode(edges_output)
state_output, edges_output = pickle.loads(state_output), pickle.loads(edges_output)
np.save('output/state_output_temp.npy', state_output)
try:
os.replace('output/state_output_temp.npy', 'output/state_output.npy')
except PermissionError:
pass
np.save('output/edge_output_temp.npy', edges_output)
try:
os.replace('output/edge_output_temp.npy', 'output/edge_output.npy')
except PermissionError:
pass
np.save('output/grid_output_temp.npy', np.array(grid_output))
try:
os.replace('output/grid_output_temp.npy', 'output/grid_output.npy')
except PermissionError:
pass
match action:
case 'move 0':
lookDir(rob, 0, 0)
move_forward(rob, stats)
case 'move 45':
lookDir(rob, 0, 45)
move_forward(rob, stats)
case 'move 90':
lookDir(rob, 0, 90)
move_forward(rob, stats)
case 'move 135':
lookDir(rob, 0, 135)
move_forward(rob, stats)
case 'move 180':
lookDir(rob, 0, 180)
move_forward(rob, stats)
case 'move 225':
lookDir(rob, 0, 225)
move_forward(rob, stats)
case 'move 270':
lookDir(rob, 0, 270)
move_forward(rob, stats)
case 'move 315':
lookDir(rob, 0, 315)
move_forward(rob, stats)
case 'jump 0':
lookDir(rob, 0, 0)
jump_forward(rob, stats)
case 'jump 45':
lookDir(rob, 0, 45)
jump_forward(rob, stats)
case 'jump 90':
lookDir(rob, 0, 90)
jump_forward(rob, stats)
case 'jump 135':
lookDir(rob, 0, 135)
jump_forward(rob, stats)
case 'jump 180':
lookDir(rob, 0, 180)
jump_forward(rob, stats)
case 'jump 225':
lookDir(rob, 0, 225)
jump_forward(rob, stats)
case 'jump 270':
lookDir(rob, 0, 270)
jump_forward(rob, stats)
case 'jump 315':
lookDir(rob, 0, 315)
jump_forward(rob, stats)
sleep(1)
stats = [mc.getFullStat(key) for key in fullStatKeys]
stats = [math.floor(stats[0]), math.floor(stats[1]), math.floor(stats[2]), round(stats[3]), round(stats[4]) % 360] # round(stats[4]) % 360]
grid = mc.getNearGrid()
environment_state = {
'position':stats,
'nearby_grid':grid
}
# Post-Action API call goes here
try:
airis_session.post_action(environment_state,)
except:
pass
if __name__ == '__main__':
args = get_cli_args()
if args.restore:
session_id = read_session_id()
signal.signal(signal.SIGINT, signal_handler_dummy)
args = get_cli_args()
if args.restore:
session_id = read_session_id()
signal.signal(signal.SIGINT, signal_handler_dummy)
if not os.path.exists('output'):
# If it doesn't exist, create it
os.makedirs('output')
else:
# If it exists, empty its contents
for filename in os.listdir('output'):
file_path = os.path.join('output', filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(f'Failed to delete {file_path}. Reason: {e}')
miss = mb.MissionXML()
world = mb.defaultworld(
seed='5',
forceReset="false",
forceReuse="true")
miss.setWorld(world)
mc = MCConnector(miss)
mc.safeStart()
rob = RobustObserver(mc)
fullStatKeys = ['XPos', 'YPos', 'ZPos', 'Pitch', 'Yaw']
rob.sendCommand('chat /gamemode creative')
rob.sendCommand('chat /effect give @s minecraft:night_vision infinite 0 true')
sleep(5)
rob.sendCommand('chat /difficulty peaceful')
sleep(5)
rob.sendCommand('Starting!')
grid_origin_x = 2
grid_origin_y = 2
grid_origin_z = 2
stats = [mc.getFullStat(key) for key in fullStatKeys]
stats = [math.floor(stats[0]), math.floor(stats[1]), math.floor(stats[2]), round(stats[3]), 0] # round(stats[4]) % 360]
grid = mc.getNearGrid()
# Initialization API call goes here
goal = {'type': 'explore'}
actions = ['move 0', 'move 45', 'move 90', 'move 135', 'move 180', 'move 225', 'move 270', 'move 315', 'jump 0', 'jump 45', 'jump 90', 'jump 135', 'jump 180', 'jump 225', 'jump 270', 'jump 315']
if args.agent:
airis_session = AgentAirisSession(args.agent)
else:
airis_session = AirisSession(api_url=API_URL)
airis_session.initialize_session(goal, actions)
signal.signal(signal.SIGINT, signal_handler)
while session_running:
try:
main_loop(mc, rob, airis_session)
except NameError:
pass
session_id = airis_session.end_session()
if session_id:
save_session_id(session_id)