//soldiers.js
//ian hatcher


//this line sets how often the program runs the "move()" function.  (500ms)
var ourInterval = setInterval("move()", 500);

//this variable holds the names of the html span tags determining Y coordinates
//(if this is confusing, look at the html source)
var Ynames=["a","b","c","d","e","f","g","h","i"];

//this map is how the program knows which word is where in the character grid
var map=["00000000011122222222222333333334444555666666666", "00000000011111111122223333333333444444455555555", "00000001112222233334444455555556667777788889999", "00000000011122222222222333333334444555666666666", "00000000011111111122223333333333444444455555555", "00000001112222233334444455555556667777788889999", "00000000011122222222222333333334444555666666666", "00000000011111111122223333333333444444455555555", "00000001112222233334444455555556667777788889999"];

//these are the lists of words for the two writing entities.  the &nbsp;s make
//the central "street" between the blocks.  (this is kind of lazy, i should
//probably change it)
var text10=["&nbsp;downtown", "on", "sidewalks&nbsp;&nbsp;&nbsp;&nbsp;", "avenues", "and", "in", "alleyways"];
var text11=["&nbsp;speaking", "fortunes", "and&nbsp;&nbsp;&nbsp;&nbsp;", "heralding", "small", "miracles"];
var text12=["onward", "as", "they", "had", "been&nbsp;&nbsp;&nbsp;&nbsp;", "onward", "as", "they", "had", "been&nbsp;&nbsp;"];

var text20=["&nbsp;careless", "of", "limousine&nbsp;&nbsp;&nbsp;&nbsp;", "appoint", "all", "to", "bootstrap"];
var text21=["&nbsp;redacted", "accounts", "for&nbsp;&nbsp;&nbsp;&nbsp;", "perishing", "after", "conflict"];
var text22=["forget", "it", "they", "all", "lost&nbsp;&nbsp;&nbsp;&nbsp;", "forget", "it", "they", "all", "lost&nbsp;&nbsp;"];

//these variables are for current locations
var locX=[0,0,0];
var locY=[0,0,0];
//these are for the current destinations
var destX=[0,0,0];
var destY=[0,0,0];
//these are for locations in the word map, as opposed to the numerical map underneath
var mapLocY=[0,0,0];
var mapLocX=[0,0,0];
//these variables hold the names of the span tags where the entities are
var spanName=["z0","z0","z0"];
//these are used to make the variable spanName
var vY=[0,0,0];
var vX=[0,0,0];
//these are the colors for the three entities
var col=["#bbb","#bbb","#000"];
//these indicate whether the entities are moving or not
var dead=[false,false,false];


function onLoadPage() {

	//generate starting positions
	for(i=0;i<3;i++) {
		locX[i] = Math.random()*100;
		locY[i] = Math.random()*100;
	}

	highlightLoc();		
	pickNewDest(0);		
	pickNewDest(1);		
	pickNewDest(2);	
}

function highlightLoc() {
	
	for(i=0;i<3;i++) {
	
		if(dead[i]!==true){
		
			//the next line recolors the current location before moving - unless
			//the current entity is #2, because that one's the invisible erasing
			//one
			if(i!==2){ document.getElementById(spanName[i]).style.color="#222";}
		
			//the next 5 lines convert the 100x100 grid locations into specific
			//word locations based on the map[] array 
			mapLocY[i] = Math.floor(locY[i]/(100/9));
			vY[i] = Ynames[mapLocY[i]];			
			mapLocX[i] = Math.floor(locX[i]/(100/46));
			vX[i] = map[mapLocY[i]].charAt(mapLocX[i]);
			spanName[i] = vY[i] + vX[i];

			document.getElementById(spanName[i]).style.color=col[i];

			//these sections change parts of the text depending on which entity
			//is currently moving 
			if(i==0){
			
				if(vY[i]=="a" || vY[i]=="d" || vY[i]=="g") {
					document.getElementById(spanName[i]).innerHTML=text10[vX[i]];
				}
				if(vY[i]=="b" || vY[i]=="e" || vY[i]=="h") {
					document.getElementById(spanName[i]).innerHTML=text11[vX[i]];
				}
				if(vY[i]=="c" || vY[i]=="f" || vY[i]=="i") {
					document.getElementById(spanName[i]).innerHTML=text12[vX[i]];
				}		
			}
			if(i==1){
			
				if(vY[i]=="a" || vY[i]=="d" || vY[i]=="g") {
					document.getElementById(spanName[i]).innerHTML=text20[vX[i]];
				}
				if(vY[i]=="b" || vY[i]=="e" || vY[i]=="h") {
					document.getElementById(spanName[i]).innerHTML=text21[vX[i]];
				}
				if(vY[i]=="c" || vY[i]=="f" || vY[i]=="i") {
					document.getElementById(spanName[i]).innerHTML=text22[vX[i]];
				}		
			}
			
		}	
	}
	
	//this section is what makes the entities sometimes "kill" each other when they
	//occupy the same word at the same time
	/*
	
	if(spanName[0]==spanName[1] && dead[1]!==true && dead[0]!==true){ 
		r=Math.random()*100;
		if(r>60){ dead[0]=true; }
	}
	if(spanName[0]==spanName[2] && dead[2]!==true && dead[0]!==true){
		r=Math.random()*100;
		if(r>60){ dead[2]=true; }
	}
	if(spanName[2]==spanName[1] && dead[1]!==true && dead[2]!==true){
		r=Math.random()*100;
		if(r>60){ dead[1]=true; }
	}
	*/
	
}


function pickNewDest(which) {
	destX[which] = Math.random()*100;
	destY[which] = Math.random()*100;	
}


function move() {

	for(i=0;i<3;i++) {
		
		//this checks to see whether the current location is close enough to
		//the destination point to count as reaching it.  if it is, it picks
		//a new destination.  if not, it moves the current location toward the
		//destination by 2 grid spaces on each axis.
		if(Math.abs(locX[i]-destX[i])<3 && Math.abs(locY[i]-destY[i])<3) {
			pickNewDest(i);
		}
		else {
			if(Math.abs(locX[i]-destX[i])>1){
				if(locX[i]>destX[i]){ locX[i]=locX[i]-2; }
				if(locX[i]<destX[i]){ locX[i]=locX[i]+2; }
			}
			if(Math.abs(locY[i]-destY[i])>1){
				if(locY[i]>destY[i]){ locY[i]=locY[i]-2; }
				if(locY[i]<destY[i]){ locY[i]=locY[i]+2; }
			}
		}	
	}

	highlightLoc();	

}



