def main(args): """Init the battle field and start the main loop.""" usage = """usage: %prog [OPTIONS] Start a Language Wars battle field""" parser = optparse.OptionParser(usage) parser.add_option('-r', '--robot', dest='robot', action="append", default=[], help='Add a robot to the battlefield (name:pygame | name:commands:botpath)') parser.add_option('-o', '--output', dest='output', action="append", default=[], help='Active output module (pygame | dump:filename)') parser.add_option('-f', '--framerate', dest='frame_rate', type="int", default=None, help='Force framerate (for non-interactive)') parser.add_option('-p', '--play-battle', dest='play_battle', default=None, help='Dump file to play') parser.add_option('-c', '--field-config-file', dest='config_file', default=None, help='Path to YAML config file') options, args0 = parser.parse_args(args) config_file = options.config_file or "config/field.yml" config = yaml.load(open(config_file).read()) battlefield.add_yaml_constructors() battlefield.add_yaml_representers() if options.play_battle: dump = open(options.play_battle) field = battlefield.read_yaml_block(dump) else: field, input_callbacks = init_robots(config_file, config, options.robot) output_callbacks = get_output_callbacks(field, options.output) if options.play_battle: battlefield.play(dump, output_callbacks) return if not field.robots: parser.print_help() return 2 elif len(field.robots) < 2: lib.error("Need at least 2 robots to fight") return 1 delta = (1.0 / options.frame_rate if options.frame_rate else None) try: start_time = time.time() lib.debug("Start battle (%d robots: %s)" % (len(field.robots), ", ".join(field.robots))) winner = battlefield.run(field, input_callbacks, output_callbacks, delta) battle_time = time.time() - start_time print "winner: %s (%s)" % (winner.name, battle_time) except battlefield.AbortBattle: lib.error("Battle aborted") return 1
def process_command(field, new_robot, command): """Return new field for robot for a string line command.""" def _run_command(spline, new_robot, new_bullets): command, args = spline[0], spline[1:] new_bullet = None if command == "set-speed": new_robot.speed = float(args[0]) consumed = 2 elif command == "set-rotation-speed": new_robot.rotation = float(args[0]) consumed = 2 elif command == "set-turret-rotation-speed": new_robot.turret_rotation = float(args[0]) consumed = 2 elif command == "fire": new_bullet = battlefield.fire_bullet(new_robot, field) new_bullets.append(new_bullet) consumed = 1 elif command == "rotate-turret-to-angle-and-fire": if new_robot.time_to_fire: if new_robot.turret_final_angle is None: new_robot.turret_final_angle = float(args[0]) elif new_robot.fire_angle is None: new_robot.fire_angle = float(args[0]) consumed = 2 elif command == "rotate-turret-to-angle": if new_robot.turret_final_angle is None: new_robot.turret_final_angle = float(args[0]) consumed = 2 else: raise ValueError, "unknown command: %s" % command return consumed, new_robot, new_bullets new_bullets = [] while command: try: consumed, new_robot, new_bullets = _run_command(command, new_robot, new_bullets) del command[:consumed] except ValueError, exc: lib.debug("command error: %s" % exc) break