int main(int argc, char* argv[]) {
	if (argc < 4) {
		printf("Usage: %s <host> <port> <player name>\n", argv[0]);
		return 1;
	}
	
	int port = atoi(argv[2]);

	// declare socket variables
	int sock, n;
	struct sockaddr_in serv_addr;
	struct hostent *server;

	char buffer[512];

	// initialize socket variables
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0)
		error("Error opening socket");
	server = gethostbyname(argv[1]);
	if (server == NULL)
		error("Error: no such host");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
	serv_addr.sin_port = htons(port);
	
	if (connect(sock, &serv_addr, sizeof(serv_addr)) < 0)
		error("Error connecting");

	// zero the buffer
	bzero(buffer, 512);
	char line[512];
	bzero(buffer, 512);
	while (strcmp("IDENTIFY?", line) != 0)
		readline(sock, buffer, line);
	writeline(sock, argv[3]);
	
	while (1) {
		readline(sock, buffer, line);
		
		if (strcmp(line, "DIRECTION?") == 0) {
			send_direction(sock);
		} else if (strcmp(line, "MAGNITUDE?") == 0) {
			send_magnitude(sock);
		} else if (strcmp(line, "TOURNAMENT OVER") == 0) {
			break;
		}
	}

	close(sock);
	return 0;
}
Ejemplo n.º 2
0
void envoi_chemin_ricochet (Robot* robot)
{
    int receive;
    while(chemin_ricochet.size() != 0)
    {
        switch(chemin_ricochet.back())
        {
            case HAUT:
                send_direction (robot->id,HAUT,0);
                receive = recevoir(robot->id);

                send_direction (robot->id,0,0);

                receive = recevoir(robot->id);

                break;
            case BAS:
                send_direction (robot->id,BAS,0);
                receive = recevoir(robot->id);

                send_direction (robot->id,0,0);

                receive = recevoir(robot->id);
                break;
            case DROITE:
                send_direction (robot->id,DROITE,0);
                receive = recevoir(robot->id);

                send_direction (robot->id,0,0);

                receive = recevoir(robot->id);
                break;
            case GAUCHE:
                send_direction (robot->id,GAUCHE,0);
                receive = recevoir(robot->id);

                send_direction (robot->id,0,0);

                receive = recevoir(robot->id);
                break;
        }
        chemin_ricochet.pop_back();
    }
}
Ejemplo n.º 3
0
void navigate_path(){
	int bool;
	struct timespec wait;
	wait.tv_sec = 0;
	wait.tv_nsec = SLEEP_DURATION;
	reset_timer();
	set_direction(&current);
	set_distance(&current);
	send_direction(&current.current_destination.angle);
	send_distance(&current.current_destination.distance);
	while(running == 1){//Infinite loop until it reaches point or collision avoidance occurs.
		printf("%d\n", current.num);
		update_position(&current);
		send_position(&current.current_point);
		if(check(current.current_point, current.current_destination) == 1){
			if(check(current.current_point, current.ending_point) == 1){
				current.path[current.num] = current.current_destination;
				current.num++;
				count++;
				current.path = realloc(current.path, sizeof(position) * (current.num+1));
			    	if(current.path == NULL){
			    		printf("!!!!!\n");
			    		send_stop();
			    	}
				send_stop();
				bool = 0;
				free(current.path);
				free(route.path);
				break;
			}
			else{
			   current.path[current.num] = current.current_destination;
			   current.num++;
			   count++;
			   printf("%d\n", sizeof(position) * (current.num+1));
			   current.path = realloc(current.path, sizeof(position) * (current.num+1));
			    	if(current.path == NULL){
			    		printf("!!!!!\n");
			    		send_stop();
			    	}
			   current.current_destination = route.path[count];
			   bool = 1;
			   break;
			}
		}