forked from Farama-Foundation/ViZDoom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_instances.py
executable file
·83 lines (53 loc) · 2 KB
/
multiple_instances.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
#!/usr/bin/env python
from __future__ import print_function
from random import choice
from vizdoom import *
# For multiplayer game use process (ZDoom's multiplayer sync mechanism prevents threads to work as expected).
from multiprocessing import Process
# For singleplayer games threads can also be used.
# from threading import Thread
# Run this many episodes
episodes = 10
def player1():
game = DoomGame()
# game.load_config('../config/basic.cfg')
# or
game.load_config('../config/multi_duel.cfg')
game.add_game_args("-host 2 -deathmatch +timelimit 1.0 +sv_spawnfarthest 1")
game.add_game_args("+name Player1 +colorset 0")
game.init()
actions = [[True, False, False], [False, True, False], [False, False, True]]
for i in range(episodes):
print("Episode #" + str(i + 1))
while not game.is_episode_finished():
if game.is_player_dead():
game.respawn_player()
game.make_action(choice(actions))
print("Episode finished!")
print("Player1 frags:", game.get_game_variable(GameVariable.FRAGCOUNT))
# Starts a new episode. All players have to call new_episode() in multiplayer mode.
game.new_episode()
game.close()
def player2():
game = DoomGame()
# game.load_config('../config/basic.cfg')
# or
game.load_config('../config/multi_duel.cfg')
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]]
for i in range(episodes):
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(GameVariable.FRAGCOUNT))
game.new_episode()
game.close()
# p1 = Thread(target = player1)
# p1.start()
if __name__ == '__main__':
p1 = Process(target=player1)
p1.start()
player2()