def __init__(self, config): self.step_count = 8 server_address = self.address(config.worker_index) self.game = zero_ad.ZeroAD(server_address) self.prev_state = None self.state = None self.cum_reward = 0
def __init__(self, action_builder, state_builder, reward_builder=WinLoseReward(), step_count=8): self.actions = action_builder self.states = state_builder self.reward = reward_builder self.action_space = self.actions.space self.observation_space = self.states.space self.step_count = step_count self.game = zero_ad.ZeroAD(self.address) self.prev_state = None self.state = None self.cum_reward = 0
from os import path from .env import register_envs register_envs() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument( 'states', help='states.jsonl file to generate demonstration from') parser.add_argument( '--url', default='http://127.0.0.1:6000', help='0 AD game server URL (running with --rlinterface flag)') parser.add_argument('--out', default=None) args = parser.parse_args() outdir = args.out if args.out is None: outdir = 'viz-' + path.dirname(args.states) os.makedirs(outdir, exist_ok=True) game = zero_ad.ZeroAD(args.url) builder = Minimap() with open(args.states, 'r') as states_file: jsons = (json.loads(line) for line in states_file if line.strip()) states = (zero_ad.GameState(json_data, game) for json_data in jsons) images = (builder.to_image(state) for state in states) for (i, image) in enumerate(images): image.save(path.join(outdir, f'state-{i}.png'), format='PNG')
This code example is a single file with comments inline (inspired by learnxinyminutes :)). """ import zero_ad import math from functools import partial from string import Template from os import path import json import numpy as np import time # First, we will establish a connection to our running game engine. # This is assuming you have already started 0 A.D. with the --rl-interface # flag (using the default port) game = zero_ad.ZeroAD('http://127.0.0.1:6000') # Before we get into the main logic, we will load some necessary files namely # the scenario configurations and a template we will use for modifying the cavalry # attack cooldown/prepare time scriptdir = path.dirname(path.realpath(__file__)) scenario_config_path = path.join(scriptdir, 'scenarios', 'CavalryVsSpearmen.json') with open(scenario_config_path, 'r') as f: cav_vs_spearmen_scenario = f.read() scenario_config_path = path.join(scriptdir, 'scenarios', 'CavalryVsSlingers.json') with open(scenario_config_path, 'r') as f: cav_vs_slingers_scenario = f.read()
import zero_ad from os import path script_dir = path.dirname(path.realpath(__file__)) scenario_config_path = path.join(script_dir, 'arcadia.json') with open(scenario_config_path, 'r') as f: arcadia_config = f.read() game = zero_ad.ZeroAD('http://localhost:6000') state = game.reset(arcadia_config) for _ in range(500): game.step() print('step!')