// Game object
//***************************************************************************************************
function DisplayForagers(){
	game.foragers=0;
	for(ix=0;ix<game.numBadgers;ix++) if(badger.badgerForage[ix]==true)game.foragers++;
  	var newText = document.getElementById("foragers");
	newText.firstChild.nodeValue=game.foragers;
}

function Game(){
 
  	this.board="board";
  	this.deploy=1;
  	this.season="Spring";
  	this.year=0;
	this.report="";
	this.deathRoll="";
	this.birthRoll="";
	this.combatRoll="";
	this.clanSick=false;
	this.advisor=new Array(10);
	this.foragers=0;
	this.numBadgers=12;
	this.deathCount=0;
	this.cubsBorn=0;
	this.hungryBadgers=0;
	this.food=0;
	this.danger=2;
	this.score=0;
	this.instructions=false;
	this.roadDanger=false;
 	this.nmeClanNum=12;
	this.dispute=0;
	this.noking=true;
	this.noqueen=true;
	
  	this.DisplayDeploy = DisplayDeploy;
  	this.DisplaySeason = DisplaySeason;
  	this.DisplayYear = DisplayYear;
  	this.AdvanceSeason = AdvanceSeason;
  	this.DisplayReport = DisplayReport;
  	this.AdvanceGame =  AdvanceGame;
  	this.DisplayForagers = DisplayForagers;
  	this.DisplayFood = DisplayFood;
  	this.DisplayDanger = DisplayDanger;
  	this.DisplayScore = DisplayScore;
	this.GetReport = GetReport;
	}

function DisplayDeploy(){ //formerly showed number deployed now shows clan numbers.
  	var newText = document.getElementById("clancount");
	newText.firstChild.nodeValue=game.numBadgers;
}
function DisplaySeason(){
  	var newText = document.getElementById("season");
	newText.firstChild.nodeValue=game.season;
}
function DisplayYear(){
  	var newText = document.getElementById("year");
	newText.firstChild.nodeValue=game.year;
}
function DisplayScore(){
  	var newText = document.getElementById("score");
	newText.firstChild.nodeValue=game.score;
}

function GetReport(){
	return ('<DIV class="report">'+this.report+'</DIV>');
}

function DisplayReport(){
  	document.getElementById("cell6").innerHTML=this.GetReport();
}	



function getFood(){
	//calculate total food collection
 	game.food=0;
	for(n=0;n<100;n++){
	 	if(board.overlayfile[n]=="claim")game.food+=board.food[n];
	}	
}




function DisplayFood(){
  	getFood();
	var newText = document.getElementById("food");
	newText.firstChild.nodeValue=game.food;
}

function DisplayDanger(){
  	var newText = document.getElementById("risk");
	newText.firstChild.nodeValue=game.danger;
}



function AdvanceSeason(){
	switch (this.season){
	  case "Summer":
	  	this.season="Autumn";
	  	break;
	  case "Autumn":
	  	this.season="Winter";
		break;
	  case "Winter":
	  	this.season="Spring";
	  	this.year++;
	  	break;
	  case "Spring":
	  	this.season="Summer";
	  	break;
	}
}

function addReport(txt){
	game.report+=txt;
	
}

function appendComma(str){
	if(str!="")return (str+", ");
	return "";
}



function AdvanceGame(){
 
 	if(game.instructions==true || game.board=="table")return;
 	game.report="";
	game.deathRoll="";
	game.birthRoll="";
	game.combatRoll="";
	game.deathCount=0;
	game.clanSick=false;
	game.dispute=0;
	for(n=0;n<10;n++)game.advisor[n]=false;
 	addReport("<br /><span class='repH'>Report for "+game.season+" year "+game.year+"</span><hr />");
	game.hungryBadgers=0;
	game.cubsBorn=0;
	assessClaim();
	doEnemy();
	var clan=game.numBadgers;
	for(b=0;b<clan;b++) badger.DoBadger(b);
	badger.CleanupBadgers();
	if(game.hungryBadgers>0) addReport(game.hungryBadgers+" badgers went hungry.<br />");
	else addReport("All the badgers had enough food.<br />");
	addReport("Badgers born:"+game.cubsBorn+"<br />");
	
	addReport("Badgers died:"+game.deathCount+"<br />");
	addReport("The clan now has "+game.numBadgers+" members<br /><hr />");
	game.DisplayDeploy();
	
	if(game.birthRoll!=""){
		addReport(game.birthRoll);
		addReport('<hr />')
	}
	if(game.deathRoll!=""){
		addReport(game.deathRoll);
		addReport('<hr />')
	}
	if(game.combatRoll!=""){
		addReport(game.combatRoll);
		addReport('<hr />')
	}
	if(game.danger>=30)game.advisor[8]=true;
	if(game.deploy==0 && game.foragers>0)game.advisor[9]=true;
	if(game.season=="Winter" && game.cubsBorn==0) game.advisor[1]=true; 
	if(game.numBadgers>20) game.advisor[10]=true; 
	updateTable();

	game.AdvanceSeason();
	replenishTerritories();
	board.GetValidMoves();
 	refreshBoard();
	advisorReports();
	assessClaim();
	game.DisplaySeason();
	game.DisplayYear();
	game.DisplayReport();
	game.DisplayForagers();
	game.DisplayScore();
	if(game.numBadgers<=0)showGameOver();
}


function depleteTerritory(){

	var t=0;
	var fed=false;
	while(t<100){	
		if(board.overlayfile[t]=="claim"){  //forage area
		   if(board.food[t]>0) { //food found
				board.food[t]--;
				fed=true;
				t=100;				 
				}	
		}
		t++;
	}
	return (fed);
}


function depleteSett(){
	if(board.food[board.sett]>0){
	 	board.food[board.sett]--;
		return true;
	}
	else {
		game.advisor[6]=true;
		return false;
	}
}

function getOverCrowding(){
	//num 0-50
	m=game.numBadgers/12;
	return(rand(10)<m)
}



function advisorReports(){
	if (game.advisor[0]) addReport("<br /><b><i>The badgers need more food! Check that all healthy badgers are foraging and the badgers are foraging enough areas.</i></b><br /><br />");
	if (game.advisor[1]) addReport("<b><i>The clan needs more members. Keep the clan fed and healthy and try making contact with other clans.</i></b><br /><br />");
	if (game.advisor[2]) addReport("<b><i>Deaths on the roads occur when the clan is foraging in those areas. Older badgers may have more road sense.</i></b><br /><br />");
	if (game.advisor[3]) addReport("<b><i>As many as 50% of badger cubs may die in their first year.</i></b><br /><br />");
	if (game.advisor[4]) addReport("<b><i>Many factors may contribute to sickness in the clan. Main causes here are encounters with other clans and malnutrition.</i></b><br /><br />");
	if (game.advisor[5]) addReport("<b><i>The younger clan members are most at risk when foraging.</i></b><br /><br />");
	if (game.advisor[6]) addReport("<b><i>The badgers home territory can only support a few non-foragers.</i></b><br /><br />");
	if (game.advisor[7]) addReport("<b><i>Cubs suckle for about 12 weeks after they are born.</i></b><br /><br />");
	if (game.advisor[8]) addReport("<b><i>The amount of risk the clan are exposed to affects the number that may die. A risk value of 50% gives a badger an even chance of dying during a season. A 33% risk converts to a 1 in 3 chance of dying.  </i></b><br /><br />");
	if (game.advisor[9]) addReport("<b><i>Badgers were selected to go foraging but no areas were set to be foraged.</i></b><br /><br />");
	if (game.advisor[10]) addReport("<b><i>Overcrowding in the sett is leading to fighting in the clan.</i></b><br /><br />");
}
