text: # ------------------------------------------------------------------------------------------------
  # Copyright (c) 2018 Microsoft Corporation
  # 
  # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  # associated documentation files (the "Software"), to deal in the Software without restriction,
  # including without limitation the rights to use, copy, modify, merge, publish, distribute,
  # sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  # furnished to do so, subject to the following conditions:
  # 
  # The above copyright notice and this permission notice shall be included in all copies or
  # substantial portions of the Software.
  # 
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  # ------------------------------------------------------------------------------------------------
  
  import malmoenv
  import argparse
  from pathlib import Path
  import time
  from lxml import etree
  from threading import Thread
  import threading
  
  if __name__ == '__main__':
  
      parser = argparse.ArgumentParser(description='malmovnv test')
      parser.add_argument('--mission', type=str, default='missions/mobchase_single_agent.xml', help='the mission xml')
      parser.add_argument('--port', type=int, default=9000, help='the mission server port')
      parser.add_argument('--server', type=str, default='127.0.0.1', help='the mission server DNS or IP address')
      parser.add_argument('--server2', type=str, default=None, help="(Multi-agent) role N's server DNS or IP")
      parser.add_argument('--port2', type=int, default=9000, help="(Multi-agent) role N's mission port")
      parser.add_argument('--episodes', type=int, default=1, help='the number of resets to perform - default is 1')
      parser.add_argument('--episode', type=int, default=0, help='the start episode - default is 0')
      parser.add_argument('--resync', type=int, default=0, help='exit and re-sync on every N - default 0 meaning never')
      parser.add_argument('--experimentUniqueId', type=str, default='test1', help="the experiment's unique id.")
      args = parser.parse_args()
      if args.server2 is None:
          args.server2 = args.server
  
      xml = Path(args.mission).read_text()
  
      mission = etree.fromstring(xml)
      number_of_agents = len(mission.findall('{http://ProjectMalmo.microsoft.com}AgentSection'))
      print("number of agents: " + str(number_of_agents))
  
      def run(role):
          env = malmoenv.make()
          env.init(xml,
                   args.port, server=args.server,
                   server2=args.server2, port2=(args.port + role),
                   role=role,
                   exp_uid=args.experimentUniqueId,
                   episode=args.episode, resync=args.resync)
  
          def log(message):
              print('[' + str(role) + '] ' + message)
  
          for r in range(args.episodes):
              log("reset " + str(r))
              env.reset()
  
              done = False
              while not done:
                  action = env.action_space.sample()
                  log("action: " + str(action))
                  obs, reward, done, info = env.step(action)
                  log("reward: " + str(reward))
                  # log("done: " + str(done))
                  # log("info: " + str(info))
                  log(" obs: " + str(obs))
  
                  time.sleep(.05)
  
          env.close()
  
      threads = [Thread(target=run, args=(i,)) for i in range(number_of_agents)]
  
      [t.start() for t in threads]
      [t.join() for t in threads]
  