Run a host which expects one player to connect remotely.

def human():
  """Run a host which expects one player to connect remotely."""
  run_config = run_configs.get()

  map_inst = maps.get(FLAGS.map)

  if not FLAGS.rgb_screen_size or not FLAGS.rgb_minimap_size:
    logging.info("Use --rgb_screen_size and --rgb_minimap_size if you want rgb "
                 "observations.")

  ports = portspicker.pick_contiguous_unused_ports(4)  # 2 * num_players
  host_proc = run_config.start(extra_ports=ports, host=FLAGS.host,
                               timeout_seconds=FLAGS.timeout_seconds,
                               window_loc=(50, 50))
  client_proc = run_config.start(extra_ports=ports, host=FLAGS.host,
                                 connect=False, window_loc=(700, 50))

  create = sc_pb.RequestCreateGame(
      realtime=FLAGS.realtime, local_map=sc_pb.LocalMap(map_path=map_inst.path))
  create.player_setup.add(type=sc_pb.Participant)
  create.player_setup.add(type=sc_pb.Participant)

  controller = host_proc.controller
  controller.save_map(map_inst.path, map_inst.data(run_config))
  controller.create_game(create)

  print("-" * 80)
  print("Join host: agent_remote --map %s --host %s --host_port %s "
        "--lan_port %s" % (FLAGS.map, FLAGS.host, client_proc.port, ports[0]))
  print("-" * 80)
  sys.stdout.flush()

  join = sc_pb.RequestJoinGame()
  join.shared_port = 0  # unused
  join.server_ports.game_port = ports.pop(0)
  join.server_ports.base_port = ports.pop(0)
  join.client_ports.add(game_port=ports.pop(0), base_port=ports.pop(0))

  join.race = sc2_env.Race[FLAGS.user_race]
  join.player_name = FLAGS.user_name
  if FLAGS.render:
    join.options.raw = True
    join.options.score = True
    if FLAGS.feature_screen_size and FLAGS.feature_minimap_size:
      fl = join.options.feature_layer
      fl.width = 24
      FLAGS.feature_screen_size.assign_to(fl.resolution)
      FLAGS.feature_minimap_size.assign_to(fl.minimap_resolution)
    if FLAGS.rgb_screen_size and FLAGS.rgb_minimap_size:
      FLAGS.rgb_screen_size.assign_to(join.options.render.resolution)
      FLAGS.rgb_minimap_size.assign_to(join.options.render.minimap_resolution)
  controller.join_game(join)

  if FLAGS.render:
    renderer = renderer_human.RendererHuman(
        fps=FLAGS.fps, render_feature_grid=False)
    renderer.run(run_configs.get(), controller, max_episodes=1)
  else:  # Still step forward so the Mac/Windows renderer works.
    try:
      while True:
        frame_start_time = time.time()
        if not FLAGS.realtime:
          controller.step()
        obs = controller.observe()

        if obs.player_result:
          break
        time.sleep(max(0, frame_start_time - time.time() + 1 / FLAGS.fps))
    except KeyboardInterrupt:
      pass

  for p in [host_proc, client_proc]:
    p.close()

  portspicker.return_ports(ports)