forked from Farama-Foundation/ViZDoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_episodes.py
executable file
·100 lines (68 loc) · 2.84 KB
/
record_episodes.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
#!/usr/bin/env python3
#####################################################################
# This script presents how to use Doom's native demo mechanism to
# replay episodes with perfect accuracy.
#####################################################################
import os
from random import choice
import vizdoom as vzd
game = vzd.DoomGame()
# Use other config file if you wish.
game.load_config(os.path.join(vzd.scenarios_path, "basic.cfg"))
game.set_episode_timeout(100)
# Record episodes while playing in 320x240 resolution without HUD
game.set_screen_resolution(vzd.ScreenResolution.RES_320X240)
game.set_render_hud(False)
# Episodes can be recorder in any available mode (PLAYER, ASYNC_PLAYER, SPECTATOR, ASYNC_SPECTATOR)
game.set_mode(vzd.Mode.PLAYER)
game.init()
actions = [[True, False, False], [False, True, False], [False, False, True]]
# Run and record this many episodes
episodes = 2
# Recording
print("\nRECORDING EPISODES")
print("************************\n")
for i in range(episodes):
# new_episode can record the episode using Doom's demo recording functionality to given file.
# Recorded episodes can be reconstructed with perfect accuracy using different rendering settings.
# This can not be used to record episodes in multiplayer mode.
game.new_episode("episode" + str(i) + "_rec.lmp")
while not game.is_episode_finished():
s = game.get_state()
r = game.make_action(choice(actions))
print("State #" + str(s.number))
print("Game variables:", s.game_variables[0])
print("Reward:", r)
print("=====================")
print("Episode", i, "finished.")
print("total reward:", game.get_total_reward())
print("************************\n")
game.close()
# New render settings for replay
game.set_screen_resolution(vzd.ScreenResolution.RES_800X600)
game.set_render_hud(True)
# Replay can be played in any mode.
game.set_mode(vzd.Mode.SPECTATOR)
game.init()
print("\nREPLAY OF EPISODE")
print("************************\n")
for i in range(episodes):
# Replays episodes stored in given file. Sending game command will interrupt playback.
game.replay_episode("episode" + str(i) + "_rec.lmp")
while not game.is_episode_finished():
s = game.get_state()
# Use advance_action instead of make_action.
game.advance_action()
r = game.get_last_reward()
# game.get_last_action is not supported and don't work for replay at the moment.
print("State #" + str(s.number))
print("Game variables:", s.game_variables[0])
print("Reward:", r)
print("=====================")
print("Episode", i, "finished.")
print("total reward:", game.get_total_reward())
print("************************")
game.close()
# Delete recordings (*.lmp files).
for i in range(episodes):
os.remove("episode" + str(i) + "_rec.lmp")