#!/usr/bin/python2.2

import sys

import World
import Route
import Command

try:
  host = sys.argv[1]
  port = int(sys.argv[2])

  command = Command.Command(host, port)

  stringmap  = command.read_world()
  playerdata = command.read_configuration()

  world = World.create_world(stringmap, playerdata) 

  num_moves = 0
  game_is_running = 1
  try:
    while game_is_running:
      response = command.get_response() # The result of the last action sent to the server.
      world.update(response)

      packages = command.get_packages()     # packages = [id, dest_x, dest_y, weight]
      world.update_packages(packages)

      move = world.robot.calculate_move()
      game_is_running = command.Move(move)
      num_moves += 1
  except (KeyboardInterrupt):
    command.quit()
  
  print 'Played for',num_moves,'moves.'
 
  command.send_log('mx1.mail.lycos.com','py_icfp@lycos.com','icfp-prog')  
  #                 ^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^   ^^^^^^^^^
  #                 Change these bits before distributing/publishing

except:
  print 'Could not connect to to host, check hostname and port number.'
  print 'usage: runme hostname port' 
