Index: source/tools/rlclient/python/tests/conftest.py =================================================================== --- /dev/null +++ source/tools/rlclient/python/tests/conftest.py @@ -0,0 +1,32 @@ +import pytest +import subprocess +from os import path +import os +import zero_ad + +PYRO_BINARY = os.environ.get('PYROGENESIS_BIN', 'pyrogenesis') +scriptdir = path.dirname(path.realpath(__file__)) +with open(path.join(scriptdir, '..', 'samples', 'arcadia.json'), 'r') as f: + config = f.read() + +@pytest.fixture(scope='session') +def game(): + proc = subprocess.Popen( + [ + PYRO_BINARY, + '--rl-interface=127.0.0.1:4783', + '--autostart-nonvisual' + ], + stdout=subprocess.PIPE + ) + line = proc.stdout.readline() + while 'RL interface listening' not in line.decode(): + line = proc.stdout.readline() + game = zero_ad.ZeroAD('http://localhost:4783') + yield game + proc.terminate() + +@pytest.fixture(scope='function') +def arcadia(game): + game.reset(config) + yield game Index: source/tools/rlclient/python/tests/test_actions.py =================================================================== --- source/tools/rlclient/python/tests/test_actions.py +++ source/tools/rlclient/python/tests/test_actions.py @@ -1,12 +1,5 @@ import zero_ad -import json import math -from os import path - -game = zero_ad.ZeroAD('http://localhost:6000') -scriptdir = path.dirname(path.realpath(__file__)) -with open(path.join(scriptdir, '..', 'samples', 'arcadia.json'), 'r') as f: - config = f.read() def dist (p1, p2): return math.sqrt(sum((math.pow(x2 - x1, 2) for (x1, x2) in zip(p1, p2)))) @@ -26,8 +19,19 @@ return units[index] -def test_construct(): - state = game.reset(config) +def step_until(game, fn, message, max_steps=500): + state = game.step() + step_count = 1 + while not fn(state): + state = game.step() + step_count += 1 + if step_count > max_steps: + raise Exception(message) + + return state + +def test_construct(arcadia): + state = arcadia.current_state female_citizens = state.units(owner=1, type='female_citizen') house_tpl = 'structures/spart/house' house_count = len(state.units(owner=1, type=house_tpl)) @@ -35,61 +39,61 @@ z = 640 build_house = zero_ad.actions.construct(female_citizens, house_tpl, x, z, autocontinue=True) # Check that they start building the house - state = game.step([build_house]) - while len(state.units(owner=1, type=house_tpl)) == house_count: - state = game.step() + state = arcadia.step([build_house]) + step_until(arcadia, lambda s: len(s.units(owner=1, type=house_tpl)) > house_count, + 'House not created') -def test_gather(): - state = game.reset(config) +def test_gather(arcadia): + state = arcadia.current_state female_citizen = state.units(owner=1, type='female_citizen')[0] trees = state.units(owner=0, type='tree') nearby_tree = closest(state.units(owner=0, type='tree'), female_citizen.position()) collect_wood = zero_ad.actions.gather([female_citizen], nearby_tree) - state = game.step([collect_wood]) - while len(state.unit(female_citizen.id()).data['resourceCarrying']) == 0: - state = game.step() + state = arcadia.step([collect_wood]) + step_until(arcadia, lambda s: len(s.unit(female_citizen.id()).data['resourceCarrying']) == 1, + 'Citizen did not pick up resource') -def test_train(): - state = game.reset(config) +def test_train(arcadia): + state = arcadia.current_state civic_centers = state.units(owner=1, type="civil_centre") spearman_type = 'units/spart/infantry_spearman_b' spearman_count = len(state.units(owner=1, type=spearman_type)) train_spearmen = zero_ad.actions.train(civic_centers, spearman_type) - state = game.step([train_spearmen]) - while len(state.units(owner=1, type=spearman_type)) == spearman_count: - state = game.step() + state = arcadia.step([train_spearmen]) + step_until(arcadia, lambda s: len(s.units(owner=1, type=spearman_type)) > spearman_count, + 'No spearmen created') -def test_walk(): - state = game.reset(config) +def test_walk(arcadia): + state = arcadia.current_state female_citizens = state.units(owner=1, type='female_citizen') x = 680 z = 640 initial_distance = dist(center(female_citizens), [x, z]) walk = zero_ad.actions.walk(female_citizens, x, z) - state = game.step([walk]) + state = arcadia.step([walk]) distance = initial_distance while distance >= initial_distance: - state = game.step() + state = arcadia.step() female_citizens = state.units(owner=1, type='female_citizen') distance = dist(center(female_citizens), [x, z]) -def test_attack(): - state = game.reset(config) +def test_attack(arcadia): + state = arcadia.current_state units = state.units(owner=1, type='cavalry') target = state.units(owner=2, type='female_citizen')[0] initial_health = target.health() - state = game.step([zero_ad.actions.reveal_map()]) + state = arcadia.step([zero_ad.actions.reveal_map()]) attack = zero_ad.actions.attack(units, target) - state = game.step([attack]) - while state.unit(target.id()).health() >= initial_health: - state = game.step() + state = arcadia.step([attack]) + step_until(arcadia, lambda s: s.unit(target.id()).health() < initial_health, + 'Target did not lose health') -def test_chat(): - state = game.reset(config) +def test_chat(arcadia): + state = arcadia.current_state chat = zero_ad.actions.chat('hello world!!') - state = game.step([chat]) + state = arcadia.step([chat])