forked from Farama-Foundation/ViZDoom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffers.py
executable file
·116 lines (85 loc) · 3.74 KB
/
buffers.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
#!/usr/bin/env python
#####################################################################
# This script presents different buffers and formats.
# OpenCV is used here to display images, install it or remove any
# references to cv2
# Configuration is loaded from "../../examples/config/basic.cfg" file.
# <episodes> number of episodes are played.
# Random combination of buttons is chosen for every action.
#
# To see the scenario description go to "../../scenarios/README.md"
#####################################################################
from __future__ import print_function
from random import choice
from vizdoom import *
import cv2
game = DoomGame()
# Use other config file if you wish.
game.load_config("../../examples/config/deadly_corridor.cfg")
# game.set_console_enabled(True)
# game.set_window_visible(False)
# Just umcomment desired format for screen buffer (and map buffer).
# The last uncommented will be applied.
# Formats with C (CRCGCB and CBCGCR) were ommited cause they are not cv2 friendly.
# Default format is ScreenFormat.CRCGCB.
game.set_screen_format(ScreenFormat.RGB24)
# game.set_screen_format(ScreenFormat.ARGB32)
# game.set_screen_format(ScreenFormat.GRAY8)
# game.set_screen_format(ScreenFormat.BGR24)
# game.set_screen_format(ScreenFormat.RGBA32)
# game.set_screen_format(ScreenFormat.BGRA32)
# game.set_screen_format(ScreenFormat.ABGR32)
# Raw Doom buffer with palette's values. This one makes no sense in particular
# game.set_screen_format(ScreenFormat.DOOM_256_COLORS)
# Sets resolution for all buffers.
game.set_screen_resolution(ScreenResolution.RES_640X480)
# Enables depth buffer.
game.set_depth_buffer_enabled(True)
# Enables labeling of in game objects labeling.
game.set_labels_buffer_enabled(True)
# Enables buffer with top down map of he current episode/level .
game.set_automap_buffer_enabled(True)
game.set_automap_mode(AutomapMode.OBJECTS)
game.set_automap_rotate(False)
game.set_automap_render_textures(False)
game.set_render_hud(True)
game.set_render_minimal_hud(False)
game.set_mode(Mode.SPECTATOR)
game.init()
actions = [[True, False, False], [False, True, False], [False, False, True]]
episodes = 10
sleep_time = 0.028
for i in range(episodes):
print("Episode #" + str(i + 1))
# Not needed for the first episode but the loop is nicer.
game.new_episode()
while not game.is_episode_finished():
# Gets the state and possibly to something with it
state = game.get_state()
# Display all the buffers here!
# Just screen buffer, given in selected format. This buffer is always available.
screen = state.screen_buffer
cv2.imshow('ViZDoom Screen Buffer', screen)
# Depth buffer, always in 8-bit gray channel format.
# This is most fun. It looks best if you inverse colors.
depth = state.depth_buffer
if depth is not None:
cv2.imshow('ViZDoom Depth Buffer', depth)
# Labels buffer, always in 8-bit gray channel format.
# Shows only visible game objects (enemies, pickups, exploding barrels etc.), each with unique label.
# Labels data are available in state.labels, also see labels.py example.
labels = state.labels_buffer
if labels is not None:
cv2.imshow('ViZDoom Labels Buffer', labels)
# Map buffer, in the same format as screen buffer.
# Shows top down map of the current episode/level.
automap = state.automap_buffer
if automap is not None:
cv2.imshow('ViZDoom Map Buffer', automap)
cv2.waitKey(int(sleep_time * 1000))
game.make_action(choice(actions))
print("State #" + str(state.number))
print("=====================")
print("Episode finished!")
print("************************")
cv2.destroyAllWindows()