/*************************************
 * Swarm Main Class for simulation
 * CSC 204 - Spring 2008
 * Homework 2
 * Mark Goadrich
 *
 * To call this class, type   
 * % java Swarm <numAnts> <timeSteps> <wiggleRoom> <minDist>
 *************************************/
public class Swarm {
    public static void main(String[] args) {

	// Read the data from the user
	int numAnts = Integer.parseInt(args[0]);
	int timeSteps = Integer.parseInt(args[1]);
	double wiggleRoom = Double.parseDouble(args[2]);
	double minDist = Double.parseDouble(args[3]);

	// Initialize all the ants
	Ant[] ants = new Ant[numAnts];
	for (int i = 0; i < numAnts; i++) {
	    ants[i] = new Ant(Math.random(), Math.random());
	}

	// Give each ant a friend to follow
	for (int i = 0; i < numAnts; i++) {
	    int other = (int)(Math.random() * (numAnts - 1));
	    if (other >= i) {
		other++;
	    }
	    ants[i].addFriend(ants[other]);
	}

	// Set the scale of the world to 0,0 to 1,1
	StdDraw.setXscale(0, 1);
	StdDraw.setYscale(0, 1);

	// Run the simulation for timeSteps
	for (int t = 0; t < timeSteps; t++) {

	    // Clear the screen
	    StdDraw.clear();

	    // Draw each ant
	    for (int i = 0; i < numAnts; i++) {
		ants[i].draw();
	    }

	    // Move and wiggle each ant
	    for (int i = 0; i < numAnts; i++) {
		ants[i].move();
		ants[i].wiggle(wiggleRoom);
	    }

	    // Repel from the closest ant if they're too close
	    for (int i = 0; i < numAnts; i++) {
		Ant other = null;
		double distance = 1000;
		for (int j = 0; j < numAnts; j++) {
		    if (i != j) {
			double mydist = ants[i].getPos().distance(ants[j].getPos());
			if (mydist < distance) {
			    other = ants[j];
			    distance = mydist;
			}
		    }
		}
		if (distance < minDist) {
		    ants[i].repel(other);
		}
	    }
	}
    }
}

