Skip to content

Commit 08ce45c

Browse files
committed
updates to playgame
1 parent 7e8d865 commit 08ce45c

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

ants/play_one_game.cmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
@echo off
2-
playgame.py --seed 42 --end_wait=0.25 --verbose --log_dir game_logs --turns 100 --map_file maps/symmetric_maps/symmetric_10.map %* "python dist/sample_bots/python/HunterBot.py" "python dist/sample_bots/python/LeftyBot.py" "python dist/sample_bots/python/HunterBot.py" "python dist/sample_bots/python/LeftyBot.py"
2+
python %~dp0playgame.py --seed 42 --end_wait=0.25 --verbose --log_dir game_logs --turns 100 --map_file %~dp0maps\symmetric_maps\symmetric_10.map %* "python %~dp0sample_bots\python\HunterBot.py" "python %~dp0sample_bots\python\LeftyBot.py" "python %~dp0sample_bots\python\HunterBot.py" "python %~dp0sample_bots\python\LeftyBot.py"
33

ants/playgame.py

+40-17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def main(argv):
8787
parser.add_option('--secure_jail', dest='secure_jail',
8888
action='store_true', default=False,
8989
help='Use the secure jail for each bot (*nix only)')
90+
parser.add_option('--fill', dest='fill',
91+
action='store_true', default=False,
92+
help='Fill up extra player starts with last bot specified')
93+
parser.add_option('-p', '--position', dest='position',
94+
default=0, type='int',
95+
help='Player position for first bot specified')
9096

9197
# ants specific game options
9298
parser.add_option("--attack", dest="attack",
@@ -112,6 +118,8 @@ def main(argv):
112118
# the log directory will contain
113119
# the replay or stream file used by the visualizer, if requested
114120
# the bot input/output/error logs, if requested
121+
parser.add_option("-g", "--game", dest="game_id", default=0, type='int',
122+
help="game id to start at when numbering log files")
115123
parser.add_option("-l", "--log_dir", dest="log_dir", default=None,
116124
help="Directory to dump replay files to.")
117125
parser.add_option('-R', '--log_replay', dest='log_replay',
@@ -156,7 +164,7 @@ def main(argv):
156164
if opts.log_dir:
157165
prof_file = os.path.join(opts.log_dir, prof_file)
158166
# cProfile needs to be explitly told about out local and global context
159-
print("Running profile and outputting to %s" % (prof_file,), file=sys.stderr)
167+
print("Running profile and outputting to {0}".format(prof_file,), file=sys.stderr)
160168
cProfile.runctx("run_rounds(opts,args)", globals(), locals(), prof_file)
161169
else:
162170
# only use psyco if we are not profiling
@@ -203,6 +211,7 @@ def run_rounds(opts,args):
203211
random.seed(opts.seed)
204212
for round in range(opts.rounds):
205213
# initialize game
214+
game_id = round + opts.game_id
206215
with open(opts.map, 'r') as map_file:
207216
game_options['map'] = map_file.read()
208217
#opts['map'] = map_file.read()
@@ -223,25 +232,37 @@ def get_cmd_wd(cmd):
223232
return wd, ' '.join(new_cmd)
224233
bots = [get_cmd_wd(arg) for arg in args]
225234
bot_count = len(bots)
235+
# insure correct number of bots, or fill in remaining positions
226236
if game.num_players != len(bots):
227-
print("Incorrect number of bots for map. Need %s, got %s" %
228-
(game.num_players, len(bots)), file=sys.stderr)
229-
for arg in args:
230-
print("Bot Cmd: %s" % arg, file=sys.stderr)
231-
break
237+
if game.num_players > len(bots) and opts.fill:
238+
extra = game.num_players - len(bots)
239+
for _ in range(extra):
240+
bots.append(bots[-1])
241+
else:
242+
print("Incorrect number of bots for map. Need {0}, got {1}"
243+
.format(game.num_players, len(bots)), file=sys.stderr)
244+
for arg in args:
245+
print("Bot Cmd: {0}".format(arg), file=sys.stderr)
246+
break
247+
# move position of first bot specified
248+
if opts.position > 0 and opts.position <= len(bots):
249+
first_bot = bots[0]
250+
bots = bots[1:]
251+
bots.insert(opts.position, first_bot)
252+
232253
# initialize file descriptors
233254
if opts.log_dir and not os.path.exists(opts.log_dir):
234255
os.mkdir(opts.log_dir)
235-
if not opts.log_replay and not opts.log_stream:
256+
if not opts.log_replay and not opts.log_stream and (opts.log_dir or opts.log_stdout):
236257
opts.log_replay = True
237258
replay_path = None # used for visualizer launch
238259

239260
if opts.log_replay:
240261
if opts.log_dir:
241-
replay_path = os.path.join(opts.log_dir, '%s.replay' % round)
262+
replay_path = os.path.join(opts.log_dir, '{0}.replay'.format(game_id))
242263
engine_options['replay_log'] = open(replay_path, 'w')
243264
if opts.log_stdout:
244-
if engine_options['replay_log']:
265+
if 'replay_log' in engine_options and engine_options['replay_log']:
245266
engine_options['replay_log'] = Tee(sys.stdout, engine_options['replay_log'])
246267
else:
247268
engine_options['replay_log'] = sys.stdout
@@ -250,7 +271,7 @@ def get_cmd_wd(cmd):
250271

251272
if opts.log_stream:
252273
if opts.log_dir:
253-
engine_options['stream_log'] = open(os.path.join(opts.log_dir, '%s.stream' % round), 'w')
274+
engine_options['stream_log'] = open(os.path.join(opts.log_dir, '{0}.stream'.format(game_id)), 'w')
254275
if opts.log_stdout:
255276
if engine_options['stream_log']:
256277
engine_options['stream_log'] = Tee(sys.stdout, engine_options['stream_log'])
@@ -260,25 +281,25 @@ def get_cmd_wd(cmd):
260281
engine_options['stream_log'] = None
261282

262283
if opts.log_input and opts.log_dir:
263-
engine_options['input_logs'] = [open(os.path.join(opts.log_dir, '%s.bot%s.input' % (round, i)), 'w')
284+
engine_options['input_logs'] = [open(os.path.join(opts.log_dir, '{0}.bot{1}.input'.format(game_id, i)), 'w')
264285
for i in range(bot_count)]
265286
else:
266287
engine_options['input_logs'] = None
267288
if opts.log_output and opts.log_dir:
268-
engine_options['output_logs'] = [open(os.path.join(opts.log_dir, '%s.bot%s.output' % (round, i)), 'w')
289+
engine_options['output_logs'] = [open(os.path.join(opts.log_dir, '{0}.bot{1}.output'.format(game_id, i)), 'w')
269290
for i in range(bot_count)]
270291
else:
271292
engine_options['output_logs'] = None
272293
if opts.log_error and opts.log_dir:
273294
if opts.log_stderr:
274295
if opts.log_stdout:
275-
engine_options['error_logs'] = [Tee(Comment(sys.stderr), open(os.path.join(opts.log_dir, '%s.bot%s.error' % (round, i)), 'w'))
296+
engine_options['error_logs'] = [Tee(Comment(sys.stderr), open(os.path.join(opts.log_dir, '{0}.bot{1}.error'.format(game_id, i)), 'w'))
276297
for i in range(bot_count)]
277298
else:
278-
engine_options['error_logs'] = [Tee(sys.stderr, open(os.path.join(opts.log_dir, '%s.bot%s.error' % (round, i)), 'w'))
299+
engine_options['error_logs'] = [Tee(sys.stderr, open(os.path.join(opts.log_dir, '{0}.bot{1}.error'.format(game_id, i)), 'w'))
279300
for i in range(bot_count)]
280301
else:
281-
engine_options['error_logs'] = [open(os.path.join(opts.log_dir, '%s.bot%s.error' % (round, i)), 'w')
302+
engine_options['error_logs'] = [open(os.path.join(opts.log_dir, '{0}.bot{1}.error'.format(game_id, i)), 'w')
282303
for i in range(bot_count)]
283304
elif opts.log_stderr:
284305
if opts.log_stdout:
@@ -294,9 +315,11 @@ def get_cmd_wd(cmd):
294315
else:
295316
engine_options['verbose_log'] = sys.stdout
296317

297-
engine_options['gameid'] = round
318+
engine_options['game_id'] = game_id
298319
if opts.rounds > 1:
299-
print('# playgame round %s' % round)
320+
print('# playgame round {0}, game id {1}'.format(round, game_id))
321+
322+
# intercept replay log so we can add player names
300323
result = run_game(game, bots, engine_options)
301324
# close file descriptors
302325
if engine_options['stream_log']:

ants/visualizer/visualize_locally.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def generate(data, generated_path):
1818
common = os.path.commonprefix((path1, path2))
1919
path1 = path1[len(common):]
2020
path2 = path2[len(common):]
21-
mod_path = '/'.join(['..'] * (path2.count(os.sep)) + [os.path.split(path1)[0]])
21+
mod_path = '/'.join(['..'] * (path2.count(os.sep)) + [os.path.split(path1)[0].replace('\\', '/')])
2222
if len(mod_path) > 0 and mod_path[-1] != '/':
2323
mod_path += '/'
2424

0 commit comments

Comments
 (0)