-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
pyoblige.py
executable file
·139 lines (112 loc) · 4.17 KB
/
pyoblige.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
#!/usr/bin/env python3
#####################################################################
# This script demonstrates how to use the environment with PyOblige.
# https://github.com/mwydmuch/PyOblige
#####################################################################
import os
from argparse import ArgumentParser
from time import sleep
import oblige
import vizdoom as vzd
DEFAULT_CONFIG = "../../scenarios/oblige.cfg"
DEFAULT_SEED = 666
DEFAULT_OUTPUT_FILE = "test.wad"
if __name__ == "__main__":
parser = ArgumentParser("An example showing how to generate maps with PyOblige.")
parser.add_argument(
dest="config",
default=DEFAULT_CONFIG,
nargs="?",
help="Path to the configuration file of the scenario."
" Please see "
"../../scenarios/*cfg for more scenarios.",
)
parser.add_argument(
"-s",
"--seed",
default=DEFAULT_SEED,
type=int,
help="Number of iterations(actions) to run",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Use verbose mode during map generation.",
)
parser.add_argument(
"-o",
"--output_file",
default=DEFAULT_OUTPUT_FILE,
help="Where the wad file will be created.",
)
parser.add_argument(
"-x",
"--exit",
action="store_true",
help="Do not test the wad, just leave after generation.",
)
args = parser.parse_args()
game = vzd.DoomGame()
# Use your config
game.load_config(args.config)
game.set_doom_map("map01")
game.set_doom_skill(3)
# Create Doom Level Generator instance and set optional seed.
generator = oblige.DoomLevelGenerator()
generator.set_seed(args.seed)
# Set generator configs, specified keys will be overwritten.
generator.set_config({"size": "micro", "health": "more", "weapons": "sooner"})
# There are few predefined sets of settings already defined in Oblige package, like test_wad and childs_play_wad
generator.set_config(oblige.childs_play_wad)
# Tell generator to generate few maps (options for "length": "single", "few", "episode", "game").
generator.set_config({"length": "few"})
# Generate method will return number of maps inside wad file.
wad_path = args.output_file
print(f"Generating {wad_path} ...")
num_maps = generator.generate(wad_path, verbose=args.verbose)
print(f"Generated {num_maps} maps.")
if args.exit:
exit(0)
# Set Scenario to the new generated WAD
game.set_doom_scenario_path(args.output_file)
# Sets up game for spectator (you)
game.add_game_args("+freelook 1")
game.set_screen_resolution(vzd.ScreenResolution.RES_640X480)
game.set_window_visible(True)
game.set_mode(vzd.Mode.SPECTATOR)
game.init()
# Play as many episodes as maps in the new generated WAD file.
episodes = num_maps
# Play until the game (episode) is over.
for i in range(1, episodes + 1):
# Update map name
print(f"Map {i}/{episodes}")
map = f"map{i:02}"
game.set_doom_map(map)
game.new_episode()
time = 0
while not game.is_episode_finished():
state = game.get_state()
time = game.get_episode_time()
game.advance_action()
last_action = game.get_last_action()
reward = game.get_last_reward()
print(f"State #{state.number}")
print("Game variables: ", state.game_variables)
print("Action:", last_action)
print("Reward:", reward)
print("=====================")
print("Episode finished!")
print("Total reward:", game.get_total_reward())
print("Kills:", game.get_game_variable(vzd.GameVariable.KILLCOUNT))
print("Items:", game.get_game_variable(vzd.GameVariable.ITEMCOUNT))
print("Secrets:", game.get_game_variable(vzd.GameVariable.SECRETCOUNT))
print("Time:", time / 35, "s")
print("************************")
sleep(2.0)
game.close()
# Remove output of generator
os.remove(wad_path)
os.remove(wad_path.replace("wad", "old"))
os.remove(wad_path.replace("wad", "txt"))