-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
cig_multiplayer_bots.py
executable file
·103 lines (75 loc) · 3.06 KB
/
cig_multiplayer_bots.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
#!/usr/bin/env python3
#####################################################################
# This script presents how to play a deathmatch game with built-in bots.
#####################################################################
import os
from random import choice
import vizdoom as vzd
game = vzd.DoomGame()
# Use CIG example config or your own.
game.load_config(os.path.join(vzd.scenarios_path, "cig.cfg"))
game.set_doom_map("map01") # Limited deathmatch.
# game.set_doom_map("map02") # Full deathmatch.
# Start multiplayer game only with your AI
# (with options that will be used in the competition, details in cig_mutliplayer_host.py example).
game.add_game_args(
"-host 1 -deathmatch +timelimit 10.0 "
"+sv_forcerespawn 1 +sv_noautoaim 1 +sv_respawnprotect 1 +sv_spawnfarthest 1 +sv_nocrouch 1 "
"+viz_respawn_delay 10 +viz_nocheat 1"
)
# Bots are loaded from file, that by default is bots.cfg located in the same dir as ViZDoom exe
# Other location of bots configuration can be specified by passing this argument
game.add_game_args("+viz_bots_path ../../scenarios/perfect_bots.cfg")
# Name your agent and select color
# colors: 0 - green, 1 - gray, 2 - brown, 3 - red, 4 - light gray, 5 - light brown, 6 - light red, 7 - light blue
game.add_game_args("+name AI +colorset 0")
game.set_mode(vzd.Mode.PLAYER)
game.set_console_enabled(True)
# game.set_window_visible(False)
game.init()
# Three example sample actions
actions = [
[1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0],
]
last_frags = 0
# Play with this many bots
bots = 7
# Run this many episodes
episodes = 10
for i in range(episodes):
print(f"Episode #{i + 1}")
# Add specific number of bots
# (file examples/bots.cfg must be placed in the same directory as the Doom executable file,
# edit this file to adjust bots).
game.send_game_command("removebots")
for i in range(bots):
game.send_game_command("addbot")
# Play until the game (episode) is over.
while not game.is_episode_finished():
# Get the state.
state = game.get_state()
# Analyze the state.
# Make your action.
game.make_action(choice(actions))
frags = game.get_game_variable(vzd.GameVariable.FRAGCOUNT)
if frags != last_frags:
last_frags = frags
print(f"Player has {frags} frags.")
# Check if player is dead
if game.is_player_dead():
print("Player died.")
# Use this to respawn immediately after death, new state will be available.
game.respawn_player()
print("Episode finished.")
print("************************")
print("Results:")
server_state = game.get_server_state()
for i in range(len(server_state.players_in_game)):
if server_state.players_in_game[i]:
print(f"{server_state.players_names[i]}: {server_state.players_frags[i]}")
print("************************")
# Starts a new episode. All players have to call new_episode() in multiplayer mode.
game.new_episode()
game.close()