
var k, ok, rand, iwait_move=10, ou=-1, explo=600;

var alea, rot=1, inc_rot=1, atourn=0, iscore=0;
var timer_rot, timer_move;
var i,j;

var alea_my, rot_my=1, inc_rot_my=1, atourn_my=0, iscore_my=0;
var timer_rot_my, timer_move_my;
var i_my,j_my;

var icrash=0, idead=0, idead_my=0;

tank = new Array();
for (i=1; i < 9; i++){
tank[i] = new Image()}
tank[1].src = "images/T1.gif";
tank[2].src = "images/T2.gif";
tank[3].src = "images/T3.gif";
tank[4].src = "images/T4.gif";
tank[5].src = "images/T5.gif";
tank[6].src = "images/T6.gif";
tank[7].src = "images/T7.gif";
tank[8].src = "images/T8.gif";

tank_my = new Array();
for (i=1; i < 9; i++){
tank_my[i] = new Image()}
tank_my[1].src = "images/R1.gif";
tank_my[2].src = "images/R2.gif";
tank_my[3].src = "images/R3.gif";
tank_my[4].src = "images/R4.gif";
tank_my[5].src = "images/R5.gif";
tank_my[6].src = "images/R6.gif";
tank_my[7].src = "images/R7.gif";
tank_my[8].src = "images/R8.gif";

explod = new Array();
for (i=1; i < 15; i++){
explod[i] = new Image()}
explod[1].src = "images/explod1.gif";
explod[2].src = "images/explod2.gif";
explod[3].src = "images/explod3.gif";
explod[4].src = "images/explod4.gif";
explod[5].src = "images/explod5.gif";
explod[6].src = "images/explod6.gif";
explod[7].src = "images/explod7.gif";
explod[8].src = "images/explod8.gif";
explod[9].src = "images/explod9.gif";
explod[10].src = "images/explod10.gif";
explod[11].src = "images/explod11.gif";
explod[12].src = "images/explod12.gif";
explod[13].src = "images/explod13.gif";
explod[14].src = "images/explod14.gif";

chiffre = new Array();
for (i=1; i < 11; i++){
chiffre[i] = new Image()}
chiffre[1].src = "images/chiffre/pantext_ch1.gif";
chiffre[2].src = "images/chiffre/pantext_ch2.gif";
chiffre[3].src = "images/chiffre/pantext_ch3.gif";
chiffre[4].src = "images/chiffre/pantext_ch4.gif";
chiffre[5].src = "images/chiffre/pantext_ch5.gif";
chiffre[6].src = "images/chiffre/pantext_ch6.gif";
chiffre[7].src = "images/chiffre/pantext_ch7.gif";
chiffre[8].src = "images/chiffre/pantext_ch8.gif";
chiffre[9].src = "images/chiffre/pantext_ch9.gif";
chiffre[10].src = "images/chiffre/pantext_ch0.gif";

obstacle = new Array();
for (i=1; i <= 108; i++){
obstacle[i] = 0}
obstacle[1] = 2;
obstacle[2] = 2;
obstacle[3] = 2;
obstacle[10] = 2;
obstacle[11] = 2;
obstacle[12] = 2;

//////////////////////////////////////////////////////////////////////

function start(){
	setup();
	setup_my();
}
//////////////////////////////////////////////////////////////////////

function setup(){
	alea = Math.round( Math.random() * 95 ) + 12;
	var j2 = alea / 12 ;
	j = Math.floor( j2 );
	i = Math.floor( (j2 * 12) - (j * 12) );
	alea = (j*12) + i;
	document.images[alea].src = tank[1].src;
	score();
	rotate();
	move();
}

function rotate(){
	if ((idead ==0 || idead == -1 ) && idead_my ==0) {
	rot=rot+inc_rot;
	if (rot > 8) rot=1;
	if (rot < 1) rot=8;
	document.images[alea].src = tank[rot].src; }
}

function move(){
	ok=0; var speede=iwait_move;

	if (idead == 0) {

		if (rot ==1 || rot ==3 || rot ==5 || rot ==7) {
			if (Math.round( Math.random() * 4 ) == 0 ) {
				if (Math.round( Math.random() * 2 ) == 0 ) inc_rot = (Math.round( Math.random() ) * 2) -1;
				rotate();
				speede=5*iwait_move;
				ok=1;
			}
		}

		if (rot == 1 && j > 0 && obstacle[(((j-1)*12) + i)+1] == 0) {
			collision();
			if (icrash == 0) {j=j-1; ok=1;} }
		if (rot == 3 && i < 11 && obstacle[((j*12) + i + 1)+1] == 0)  {
			collision();
			if (icrash == 0) {i=i+1; ok=1} }
		if (rot == 5 && j < 8 && obstacle[(((j+1)*12) + i)+1] == 0)  {
			collision();
			if (icrash == 0) {j=j+1; ok=1} }
		if (rot == 7 && i > 0 && obstacle[((j*12) + i - 1)+1] == 0)  {
			collision();
			if (icrash == 0) {i=i-1; ok=1} }
	}

	if (ok == 0) {
		rotate();
		}
	if (idead == 0) {
		document.images[alea].src = "images/noir.gif";
		alea = (j*12) + i;
		document.images[alea].src = tank[rot].src;
	}

	timer_move = setTimeout('move()', speede);
}

function score(){
	var diz = Math.floor(iscore/10);
	var unit = iscore - (diz * 10);
	if (diz == 0) diz=10;
	if (unit == 0) unit=10;
	document.images[1].src = chiffre[unit].src;
	document.images[0].src = chiffre[diz].src;
	complete();
}

/////////////////////////////////////////////////////////////////////

function setup_my(){
	alea_my = Math.round( Math.random() * 95 ) + 12;
	var j2_my = alea_my / 12 ;
	j_my = Math.floor( j2_my );
	i_my = Math.floor( (j2_my * 12) - (j_my * 12) );
	alea_my = (j_my*12) + i_my;
	document.images[alea_my].src = tank_my[1].src;
	score_my();
	rotate_my();
	move_my();
}

function rotate_my(){
	if ((idead_my ==0 || idead_my == -1 ) && idead == 0 ) {
	rot_my=rot_my+inc_rot_my;
	if (rot_my > 8) rot_my=1;
	if (rot_my < 1) rot_my=8;
	document.images[alea_my].src = tank_my[rot_my].src; }
}

function move_my(){
	ok=0; pousse=0; var speede=iwait_move;

	if (idead_my == 0) {

		if (rot_my ==1 || rot_my ==3 || rot_my ==5 || rot_my ==7) {
			if (Math.round( Math.random() * 4 ) == 0 ) {
				if (Math.round( Math.random() * 2 ) == 0 ) inc_rot_my = (Math.round( Math.random() ) * 2) -1;
				rotate_my();
				speede=iwait_move*5;
				ok=1;
			}
		}

		var quelcase = (((j_my-1)*12) + i_my)+1;
		if (rot_my == 1 && j_my > 0 && obstacle[quelcase] != 2) {
			if (obstacle[quelcase] == 1) {
				if (j_my - 2 >= 0 && obstacle[quelcase - 12] == 0 && quelcase-12-1 != alea && quelcase-1 != alea) {
					pousse=1;
					obstacle[quelcase - 12]=1;
					document.images[quelcase-12-1].src = "images/obstacle.gif";
					obstacle[quelcase]=0;
					document.images[quelcase-1].src = "images/noir.gif";
					j_my=j_my-1; ok=1;
					}
			} else {
				collision_my();
				if (icrash == 0) {j_my=j_my-1; ok=1} 
			}
		}

		var quelcase = ((j_my*12) + i_my + 1)+1;
		if (rot_my == 3 && i_my < 11 && obstacle[quelcase] != 2)  {
			if (obstacle[quelcase] == 1 ) {
				if (i_my + 2 <= 11 && obstacle[quelcase + 1] == 0 && quelcase+1-1 != alea && quelcase-1 != alea) {
					pousse=1;
					obstacle[quelcase + 1]=1;
					document.images[quelcase+1-1].src = "images/obstacle.gif";
					obstacle[quelcase]=0;
					document.images[quelcase-1].src = "images/noir.gif";
					i_my=i_my+1; ok=1;
					}
			} else {
				collision_my();
				if (icrash == 0) {i_my=i_my+1; ok=1}
			}
		}		

		var quelcase = (((j_my+1)*12) + i_my)+1;
		if (rot_my == 5 && j_my < 8 && obstacle[quelcase] != 2)  {
			if (obstacle[quelcase] == 1) {
				if (j_my+2 <= 8 && obstacle[quelcase + 12] == 0 && quelcase+12-1 != alea && quelcase-1 != alea) {
					pousse=1;
					obstacle[quelcase + 12]=1;
					document.images[quelcase+12-1].src = "images/obstacle.gif";
					obstacle[quelcase]=0;
					document.images[quelcase-1].src = "images/noir.gif";
					j_my=j_my+1; ok=1;
					}
			} else {
				collision_my();
				if (icrash == 0) {j_my=j_my+1; ok=1}
			}
		}	

		var quelcase = ((j_my*12) + i_my - 1)+1
		if (rot_my == 7 && i_my > 0 && obstacle[quelcase] != 2)  {
			if (obstacle[quelcase] == 1 ) {
				if (i_my-2 >= 0 && obstacle[quelcase - 1] == 0 && quelcase-1-1 != alea && quelcase-1 != alea) {
					pousse=1;
					obstacle[quelcase - 1]=1;
					document.images[quelcase-1-1].src = "images/obstacle.gif";
					obstacle[quelcase]=0;
					document.images[quelcase-1].src = "images/noir.gif";
					i_my=i_my-1; ok=1;
					}
			} else {
				collision_my();
				if (icrash == 0) {i_my=i_my-1; ok=1}
			}
		}		

	}

	if (ok == 0) {
		rotate_my();
		}

	if (idead_my == 0 ) {
		document.images[alea_my].src = "images/noir.gif";
		alea_my = (j_my*12) + i_my;
		document.images[alea_my].src = tank_my[rot_my].src;
	}
	if (pousse == 0 ) 	{ timer_move_my = setTimeout('move_my()', speede);}
		else		{ timer_move_my = setTimeout('move_my()', iwait_move*20);}
}

function score_my(){
	var diz = Math.floor(iscore_my/10);
	var unit = iscore_my - (diz * 10);
	if (diz == 0) diz=10;
	if (unit == 0) unit=10;
	document.images[11].src = chiffre[unit].src;
	document.images[10].src = chiffre[diz].src;
	complete();
}

/////////////////////////////////////////////////////////////////////

function collision(){
	icrash=0;
	if (rot == 1) {
		if (i_my == i && j-1 == j_my  ) icrash=1;
		}
	if (rot == 3) {
		if ( i+1 == i_my && j == j_my ) icrash=1;
		}
	if (rot == 5) {
		if (i_my == i && j+1 == j_my ) icrash=1;
		}
	if (rot == 7) {
		if ( i-1 == i_my && j == j_my ) icrash=1;
		}
	if (icrash == 1 && ou < 0) {
		idead_my=1;
		iscore++;
		score();

		setTimeout("document.images[alea_my].src = explod[1].src", 100);
		setTimeout("document.images[alea_my].src = explod[2].src", 200);
		setTimeout("document.images[alea_my].src = explod[3].src", 300);
		setTimeout("document.images[alea_my].src = explod[4].src", 400);
		setTimeout("document.images[alea_my].src = explod[5].src", 500);
		setTimeout("document.images[alea_my].src = explod[6].src", 600);
		setTimeout("document.images[alea_my].src = explod[7].src", 700);
		setTimeout("document.images[alea_my].src = explod[8].src", 800);
		setTimeout("document.images[alea_my].src = explod[9].src", 900);
		setTimeout("document.images[alea_my].src = explod[10].src", 1000);
		setTimeout("document.images[alea_my].src = explod[11].src", 1100);
		setTimeout("document.images[alea_my].src = explod[12].src", 1200);
		setTimeout("document.images[alea_my].src = explod[13].src", 1300);
		setTimeout("document.images[alea_my].src = explod[14].src", 1400);

		setTimeout("idead_my=-1", 1500);
		setTimeout("idead_my=0", 3000);
		ou = alea_my;
		setTimeout("aff_obstacle()", 3100);
		obstacle[alea_my+1] = 1;
	}
}

function collision_my(){
	icrash=0;
	if (rot_my == 1) {
		if (i_my == i && j == j_my-1 ) icrash=1;
		}
	if (rot_my == 3) {
		if ( i == i_my+1 && j == j_my ) icrash=1;
		}
	if (rot_my == 5) {
		if (i_my == i && j == j_my+1 ) icrash=1;
		}
	if (rot_my == 7) {
		if ( i == i_my-1 && j == j_my ) icrash=1;
		}
	if (icrash == 1 && ou < 0) {
		idead=1;
		iscore_my++;
		score_my();

		setTimeout("document.images[alea].src = explod[1].src", 100);
		setTimeout("document.images[alea].src = explod[2].src", 200);
		setTimeout("document.images[alea].src = explod[3].src", 300);
		setTimeout("document.images[alea].src = explod[4].src", 400);
		setTimeout("document.images[alea].src = explod[5].src", 500);
		setTimeout("document.images[alea].src = explod[6].src", 600);
		setTimeout("document.images[alea].src = explod[7].src", 700);
		setTimeout("document.images[alea].src = explod[8].src", 800);
		setTimeout("document.images[alea].src = explod[9].src", 900);
		setTimeout("document.images[alea].src = explod[10].src", 1000);
		setTimeout("document.images[alea].src = explod[11].src", 1100);
		setTimeout("document.images[alea].src = explod[12].src", 1200);
		setTimeout("document.images[alea].src = explod[13].src", 1300);
		setTimeout("document.images[alea].src = explod[14].src", 1400);

		setTimeout("idead=-1", 1500);
		setTimeout("idead=0", 3000);
		ou = alea;
		setTimeout("aff_obstacle()", 3100);
		obstacle[alea+1] = 1;
	}
}

function tir(){
}

function aff_obstacle(){
	if (ou > 0 && ou != alea && ou != alea_my) {
		document.images[ou].src = "images/obstacle.gif";
		ou = -1;
		}
		else { 
			setTimeout('aff_obstacle()', 25);
		}
}

function plus(){
	iwait_move= iwait_move - 20;
	if (iwait_move < 1) iwait_move=1;
	}

function moins(){
	iwait_move= iwait_move + 20;
	}

function complete(){
	var complet=Math.round(((iscore+iscore_my)/108) * 100);
	var diz = Math.floor(complet/10);
	var unit = complet - (diz * 10);
	if (diz == 0) diz=10;
	if (unit == 0) unit=10;
	document.scor2.src = chiffre[unit].src;
	document.scor1.src = chiffre[diz].src;
	}
