// Stupid2 startegy for 5 players
public class GoGoBoysFC extends Team {
	int team, w, h;
	double[] dir;
	double[] speed;
	double[] shotdir;

	double[][] formation; // array containing the formation - [player][x,y,width,height]
	double[][] pos;
	double[] action_radius;
	int goalx, goaly;  // Opponents goal
	int homex, homey;
	int shootw; // Randomness margin for shooting
	
	// Intializes with what team this is, and the height and width of the field
	GoGoBoysFC(int team, int w, int h) {
		this.team = team; // It's good to know, what team you are!
		this.w = w; // Width of field
		this.h = h; // Height of field

		
		// Arrays used for saving direction, speed and shot-direction for the 5 players
		dir = new double[5];
		speed = new double[5];
		shotdir = new double[5];

		shootw = 60;
		
		formation = new double[5][4];
		pos = new double[5][2];
		action_radius = new double[5];
		// Position of opponents goal...
		goalx = 0;
		goaly = h/2;
		homex = w;
		homey = h/2;
		if(team==0) {
			goalx = w;
			homex = 0;
		}
	
		// The formation		
		// Keeper
		formation[0][0] = 40;
		formation[0][1] = h/2;
		formation[0][2] = 0;
		formation[0][3] = 0;
		action_radius[0] = w/6;
		
		// Striker 1
		formation[4][0] = 5*w/6;
		formation[4][1] = h/2;
		formation[4][2] = 5;
		formation[4][3] = h/2;
		action_radius[4] = w/4;
		
		// Off. midfielder
		formation[3][0] = 3*w/5;
		formation[3][1] = h/2;
		formation[3][2] = w/10;
		formation[3][3] = 3*h/4;
		action_radius[3] = w/4;
		
		// Defender 1
		formation[1][0] = w/4;
		formation[1][1] = 2*h/3;
		formation[1][2] = 0;
		formation[1][3] = 0;
		action_radius[1] = w/4;
		
		// Defender 2
		formation[2][0] = w/4;
		formation[2][1] = h/3;
		formation[2][2] = 0;
		formation[2][3] = 0;
		action_radius[2] = w/4;

		if(team==1)
			for(int i=0; i<5; i++) formation[i][0] = w - formation[i][0];
	}

	// Distance function...
	double dist(double x, double y, double a, double b) {
		return Math.sqrt((x-a)*(x-a)+(y-b)*(y-b));
	}

	// Pick a random direction...
	double randomDir() {
		return Math.random()*2*Math.PI;
	}

	// Direction to opponents goal -- with some randomness added
	double goalDir(double x, double y) {
		if(y < h/2) return getDir(x,y,goalx,goaly - 0.5*shootw);
		else return getDir(x,y,goalx,goaly + 0.5*shootw);
	}

	// Get the direction one has to go to get from (x,y) to (a,b)
	double getDir(double x, double y, double a, double b) {
		return Math.atan2(b-y,a-x);			
	}


	// What to do, what to do...
	double[][] action(double[][][] playerPos, double[] ballPos, double time, int[] score) {
		double[][] a = new double[5][4];
		
		double[] dist_to_ball = new double[5];


	
		// If there are some player longer upfield, and - pass to him
		for(int i=0; i<5; i++) {
			// If nothing better to do -- shoot for the goal
			shotdir[i] = goalDir(ballPos[0],ballPos[1]);
			speed[i] = 0;
			if( (playerPos[team][i][0] < 3*w/4 && team==0) || (playerPos[team][i][0] > w/4 && team==1) ) {
				for(int j=0; j<5; j++) {
					if( (team==0 && playerPos[team][j][0] > playerPos[team][i][0]) || (team==1 && playerPos[team][j][0] < playerPos[team][i][0]) ) 
						shotdir[i] = getDir(playerPos[team][i][0],playerPos[team][i][1],playerPos[team][j][0],playerPos[team][j][1]);
				}
			}
		}

		// Player 0 just kicks the ball away...
		shotdir[0] =  randomDir()/3 - Math.PI/3;
		if(team==1) shotdir[0] = randomDir()/3 - Math.PI/3 + Math.PI;
		dir[0] = getDir(playerPos[team][0][0],playerPos[team][0][1], pos[0][0], pos[0][1]);

		// Everybody runs for their position		
		for(int i=0; i<5; i++) {
			dir[i] = getDir(playerPos[team][i][0],playerPos[team][i][1], pos[i][0], pos[i][1]);
		}

		// How far is the ball from the players zone - if it is close enough, run for it!
		for(int i=0; i<5; i++) {
			dist_to_ball[i] = dist(formation[i][0],formation[i][1],ballPos[0],ballPos[1]);
			if(dist_to_ball[i] < action_radius[i]) {
				speed[i] = 3;
				dir[i] = getDir(playerPos[team][i][0],playerPos[team][i][1], ballPos[0], ballPos[1]);
			}
		}

				
		// Randomize the position within the given bounds
		if(time % 50 == 0) {
			for(int i=1; i<5; i++) {
				pos[i][0] = formation[i][0] + (Math.random()-0.5)*formation[i][2];
				pos[i][1] = formation[i][1] + (Math.random()-0.5)*formation[i][3];
			}
		}
		
		// The keeper has to stand between the ball and the goal
		pos[0][0] = homex + formation[0][0]*Math.cos(getDir(homex, homey, ballPos[0], ballPos[1]));
		pos[0][1] = homey + formation[0][0]*Math.sin(getDir(homex, homey, ballPos[0], ballPos[1]));

		// If the player is not at his home point, run for it
		for(int i=0; i<5; i++) {
			if(dist(playerPos[team][i][0],playerPos[team][i][1],pos[i][0],pos[i][1]) > 3)
				speed[i] = 3;
		}
		
		// Save the orders for returning
		for(int i=0; i<5; i++) {
			a[i][0] = dir[i];
			a[i][1] = speed[i];
			a[i][2] = shotdir[i];
			a[i][3] = 15;
		}

		return a;
	}
}

