// $Id: SimplePlayer.cpp,v 1.2 2000/01/04 21:45:34 jmvidal Exp $
//
//Robocop SimplePlayer
// written by Jose M. Vidal
// http://jmvidal.cse.sc.edu
// Distributed under the terms of the GNU GPL
//

#include "SimplePlayer.H"

/**
	A handy routine to convert a double into a string.
	*/
string myDtoa (double n)
{
  string         ret;
  strstream      tmp;
  tmp << n << ends;
  ret = tmp.str ();
  tmp.rdbuf()->freeze(0);
  return ret;
}


/** Initialize the player, connecting it to machine server at port */
SimplePlayer::SimplePlayer(const string & server, int port){
  if (sock.init(server,port)  == -1){
    cerr << "Could not open socket to " << server << ":" << port << endl;
    exit(1);
  }
};

/** Send the server the initial message and move the player into the
	playing field. */
void SimplePlayer::init(){
  string msg = "(init " + team + ")";
  sock.sendMessage(msg);
  string imsg;
  if (sock.receiveMessageBlock(imsg) == 1){
    //imsg is before_kickoff msg
    msg = "(move -20 0)";
    sock.sendMessage(msg);
  }
};


/** This is the main loop. We fetch a new message, parse it, and take
	action by sending a command to the server.
	Remember that the agent receives about 10 messages per second so your
	computation must take less than that, otherwise you will be running
	behind.
	*/
void SimplePlayer::run(const string & teamname){
  team = teamname;
  init();
  string msg;
  string mode = "before_kickoff";
  while (1) {
    if (sock.receiveMessageBlock(msg) == 1){
      List m;
      m.parse(msg);
      cout << "DATA is: " << endl;
      cout << m << endl;
      
      List first = m.getFirst();
      if (!first.isAtom()) {
		cerr << "ERROR: bad message " << m << endl;
		continue;
      }
      string f = first.getValue();
      m.eraseFirst();

      string time = m.getFirstAndErase().getValue();
      if (f == "hear") {
		string sender = m.getFirst().getValue();
		m.eraseFirst();
		if (sender == "referee") {
			mode = m.getFirst().getValue();
		}
		cout << "mode is now " << mode << endl;
      }
      else if (f == "see") {
		if ((mode == "kick_off_l") || (mode == "kick_off_r")) { //play ball
			for (List item = m.getFirstAndErase(); !item.isNull(); item = m.getFirstAndErase()){
			//	    cout << "item=" << item << "=" << endl;
			//item is ((goal r) 50 0 0 0)

			List objectList = item.getFirstAndErase(); //(goal r)
			string obj = objectList.getFirstAndErase().getValue(); //goal
			string distanceS;
			double distance;
			string directionS;
			double direction;
			if (obj == "ball") {
				distanceS = item.getFirstAndErase().getValue();
				distance = atof(distanceS.c_str());
				directionS = item.getFirstAndErase().getValue();
				direction = atof(directionS.c_str());
				if (fabs(direction) > 15 ) { //if more than 15 off, turn
					string outm = "(turn " + directionS + ")";
					sock.sendMessage(outm);
			}
			else { //otherwise move towards ball
				string outm = "(dash 100)";
				sock.sendMessage(outm);
			}
		}
	  }
	}
      }
      msg = "";
    };
  };
};