-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
record_multiplayer.py
executable file
·100 lines (66 loc) · 2.82 KB
/
record_multiplayer.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/python3
#####################################################################
# This script presents how to use Doom's native demo mechanism to
# record multiplayer game and replay it with perfect accuracy.
#####################################################################
# WARNING:
# Due to the bug in built-in bots recording game with bots will result in the desynchronization of the recording.
from multiprocessing import Process
from random import choice
import vizdoom as vzd
from vizdoom import DoomGame, os
def player1():
game = vzd.DoomGame()
game.load_config(os.path.join(vzd.scenarios_path, "multi_duel.cfg"))
game.add_game_args("-host 2 -deathmatch +timelimit 1 +sv_spawnfarthest 1 ")
game.add_game_args("+name Player1 +colorset 0")
# Unfortunately multiplayer game cannot be recorded using new_episode() method, use this command instead.
game.add_game_args("-record multi_rec.lmp")
game.init()
actions = [[True, False, False], [False, True, False], [False, False, True]]
while not game.is_episode_finished():
if game.is_player_dead():
game.respawn_player()
game.make_action(choice(actions))
print("Game finished!")
print("Player1 frags:", game.get_game_variable(vzd.GameVariable.FRAGCOUNT))
game.close()
def player2():
game = DoomGame()
game.load_config("../../scenarios/multi_duel.cfg")
game.set_window_visible(False)
game.add_game_args("-join 127.0.0.1")
game.add_game_args("+name Player2 +colorset 3")
game.init()
actions = [[True, False, False], [False, True, False], [False, False, True]]
while not game.is_episode_finished():
if game.is_player_dead():
game.respawn_player()
game.make_action(choice(actions))
print("Player2 frags:", game.get_game_variable(vzd.GameVariable.FRAGCOUNT))
game.close()
def replay_as_player2():
game = DoomGame()
game.load_config("../config/multi_duel.cfg")
# At this moment ViZDoom will crash if there is no starting point - this is workaround for multiplayer map.
game.add_game_args("-host 1 -deathmatch")
game.init()
# Replays episode recorded by player 1 from perspective of player2.
game.replay_episode("multi_rec.lmp", 2)
while not game.is_episode_finished():
game.advance_action()
print("Game finished!")
print("Player1 frags:", game.get_game_variable(vzd.GameVariable.PLAYER1_FRAGCOUNT))
print("Player2 frags:", game.get_game_variable(vzd.GameVariable.PLAYER2_FRAGCOUNT))
game.close()
# Delete multi_rec.lmp
os.remove("multi_rec.lmp")
if __name__ == "__main__":
print("\nRECORDING")
print("************************\n")
p1 = Process(target=player1)
p1.start()
player2()
print("\nREPLAY")
print("************************\n")
replay_as_player2()