<!--
///temporary thing
var Destination0;


//destination0.top=20;

// declare variables
// this array holds the people graphics
var people=[];

// width and height of people icons by rank
// there is no rank 0, they go 1 to 10, so rank 0 will be a dummy entry
 var person_width = new Array (0, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32);
 var person_height = new Array (0, 13, 15, 17, 19, 22, 23, 25, 27, 29, 31);

// root name for person icons
var peopleIconRoot = "flatlander";

// timer for game ticks, measured in milliseconds
var gameInterval;
var gameTick = 100;

var settingsDialogX = "204px";
var settingsDialogY = "108px";

// is the game running or paused?
var isPaused = 1;

// is a modal dialog open?
var modalDialogOpen = 0;

// was the game paused or running when the modal dialog was opened?
var previousPauseStatus = 0;

// define the map boundaries to match the size & position of the mmap div
var leftEdge = 0;
var rightEdge = 768;
var topEdge = 0;
var bottomEdge = 580;

// initial values for Settings dialog
var defaultSetSeed=false;
var setSeed;

var defaultStartSeed = 123456;
var startSeed;
var randomSeed;
var minSeed = 1;
var seed_string = "Seed must be a positive whole number"

var defaultTribeSize = 20; 
var tribeSize;
var min_num_flatlanders = 1;
var max_num_flatlanders = 50;
var num_flatlanders_string = "Number of flatlanders must be a whole number between 1 and 50";

// each person will be given a permanent unique ID
// this will be incremented for each new person
var nextID=0;
var defaultStrategy = "solo";
var defaultLeader = 1;

// distance within which a person can perceive objects
var sightDistance = 80;

// nearest a person wants to get to their leader
var leaderClosestDistance = 30;

// max angle by which a person can turn (radians per second)
var maxTurnAngle = 0.01;

function InitGame()
{
	// temporary image to show destination
	Destination0 = document.getElementById("Destination");
	
	// choose a new random seed, or use the fixed one from the Settings dialog
	if (setSeed==true)
	{
		randomSeed = startSeed;
	}
	else
	{
		randomSeed = Math.round(Math.random() * 100000);
	}
		
	// create the people
	if (people!=document.getElementById('TheTribe').getElementsByTagName('img'))
	{
		// if the game hasn't been run before
		// create a new tribe containing this many people
		CreateTribe(tribeSize);
	}
	else
	{
		// if you're starting again without refreshing
		// one person already exists because you can't delete the last person
		CreateTribe(tribeSize-1);
	}
		
	// start the game timer
	if (isPaused==1)
	{
		PauseRun();
	}
}

function InitUI()
{
	// set the game values to match the defaults
	setSeed=defaultSetSeed;
	startSeed=defaultStartSeed;
	tribeSize=defaultTribeSize;
	
	// set the default values of the Settings dialog
	document.getElementById("number_of_flatlanders").value=tribeSize;
	document.getElementById("seed").value=startSeed;
	document.getElementById("set_seed").checked=setSeed;
	SettingsDialogEnableSeed();
	
	InitGame();
}

function CreateTribe(num)
{
	var x;
	var y;
	// reset the unique IDs
	nextID=0;
	
	// create a tribe with the specified number of people
	var i;
	for (i = 1; i <= (num); i++)
	{
		// create a new person
		var oPerson = NewPerson();

	}
	
	// iterate through all the people, including the mystery first one!
	var i;
	for (i = 0; i <= (people.length-1); i++)
	{
		// generate random properties for a person
		// choose a random starting position
		x = RandomNumber(1) * (rightEdge - leftEdge);
		y = RandomNumber(1) * (bottomEdge - topEdge);
		
		// choose a random starting speed (also used as max speed)
		var new_speed = RandomNumber(10)+10;
		// choose a random direction
		var new_angle = RandomAngle();
		// now convert this to x, y components of velocity
		var unitVector = GetUnitVector(new_angle);
				
		// choose a random colour with 3 equally likely values
		var randomThreeValues = RandomInteger(3);
		var colour;
		switch (randomThreeValues)
		{
			case 1: colour = "red";
				break;
			case 2: colour = "yellow";
				break;
			case 3: colour = "blue";
				break;
		}
		
		// choose a random rank between 1 and 10
		var rank=RandomInteger(10); 
		
		AssignID(people[i]);
		InitPerson(people[i], x, y, unitVector._x, unitVector._y, new_speed, 0, colour, rank, defaultStrategy);
	}
}

function NewPerson()
{
	var oPerson;
	
	if (people!=document.getElementById('TheTribe').getElementsByTagName('img'))
	{
		// if this is the first person created
		// Connect the array of people to the 'template' image
		people = document.getElementById('TheTribe').getElementsByTagName('img');
	}
	
	else
	{
		// create a new person as a copy of the first one
		var oParent = document.getElementById('TheTribe');
		oParent.appendChild(people[0].cloneNode(true));
	}
	
	// add them to the array of people
	oPerson=people[people.length-1];
	oPerson._index = people.length-1;
	return oPerson;
}	

function AssignID(oPerson)
{
	// call this function to give a new person a unique ID
	// give them a unique ID
	oPerson._ID = nextID;
	nextID += 1;
}

function InitPerson(oPerson, x, y, velocity_x, velocity_y, maxSpeed, speed, colour, rank, strategy)
{
	// initialize their properties
	oPerson._vx = velocity_x;
	oPerson._vy = velocity_y;
	oPerson._maxSpeed = maxSpeed;
	oPerson._speed = speed;

	// construct the icon name and set it as the image
	oPerson._colour=colour;
	oPerson._rank=rank;
	var rank_constructor=oPerson._rank;
	// icon names are "01", "02" etc. to allow them to sort correctly in file manager
	if (rank_constructor < 10) {rank_constructor = "0" + rank_constructor;}
	oPerson.src="icons/flatlanders/" + peopleIconRoot + "_" + oPerson._colour + "_" + rank_constructor + ".gif";
	
	// x, y & z co-ordinates should be stored as integers
	oPerson._x = x;
	oPerson._y = y;
	oPerson.style.left=Math.round(oPerson._x);
	oPerson.style.top=Math.round(oPerson._y);
	
	// collision area/visible icon size comes from a fixed array
	oPerson._width = person_width[oPerson._rank];
	oPerson._height = person_height[oPerson._rank];
	
	oPerson.style.width = person_width[oPerson._rank];
	oPerson.style.height = person_height[oPerson._rank];
	
	// set their starting strategy
	oPerson._strategy = strategy;
	// initialise the strategy counter to 0
	oPerson._counter = 0;
	
	// and the counter for choosing a strategy, but out of step with everybody else
	oPerson._chooseStrategyCounter = RandomInteger(10);
	
	// set each person to be their own starting leader, which should bump them into solo strategy
	oPerson._leaderID = oPerson._ID;
}

function RunStrategy(oPerson)
{
	switch (oPerson._strategy)
	{
		case "evaluate": StrategyEvaluate(oPerson);
			break;
		case "drag": StrategyDrag(oPerson);
			break;
		case "solo": StrategySolo(oPerson);
			break;
		case "lead": WriteDebug1("lead");
			break;
		case "follow": StrategyFollow(oPerson);
			break;
	}
	return true;
}

function CheckStrategyCounter(oPerson)
{
	//	check for a new leader every second
	if (oPerson._chooseStrategyCounter <= 0)
	{
		ChooseStrategy(oPerson);
	}
	else
	{
		oPerson._chooseStrategyCounter -= 1;
	}
	return true;
}

function ChooseStrategy(oPerson)
{
	var oldLeader = oPerson._leaderID;
	oPerson._leaderID = GetLeader(oPerson);
	if (oPerson._leaderID == oPerson._ID)
	{
	 	if (oldLeader != oPerson._leaderID)
	 	{
		 // WriteDebug1("changing leader to self: " + oPerson._ID);
		 }
	 	
		 oPerson._strategy = "solo";
	}
	else
	{
		oPerson._strategy = "follow";
	}

	// reset the strategy counter
	oPerson._chooseStrategyCounter = 1000 / gameTick;
	return true;
}

function StrategyEvaluate(oPerson)
{
	ChooseStrategy(oPerson);
	return true;
}

function StrategyDrag(oPerson)
{
	// don't do anything while being dragged!
	oPerson._speed = 0;
	return true;
}

var tempcounter = 0;
var tempsum = 0;
var tempaverage = 0;

function StrategySolo(oPerson)
{
	// check whether it's time to evaluate strategy
	CheckStrategyCounter(oPerson);
	
	// solo strategy is a random walk of short hops with occasional long hops
	var num_seconds;
	var distance;
	var direction;
	
	if (oPerson._counter <= 0)
	{
    // randomly choose a new direction	
		direction = RandomAngle();
		tempcounter += 1;
		tempsum += direction;
		tempaverage = tempsum / tempcounter;
		WriteDebug1(direction + ", ave angle: " + tempaverage);
	 	
		// 1 time in 10, do a long hop between 10 & 15 seconds
		if (RandomInteger(10)==10)
		{
			num_seconds = (RandomInteger(10) + 5);
		}
		else
		{
			// short hop betweeen 2 & 4 seconds
			num_seconds = (RandomInteger(2) + 2);
		}
		// counter will run until the person should have reached the destination
		// plus 30 seconds to allow for obstacle avoidance
		oPerson._counter = (num_seconds + 30)* (1000 / gameTick);
		// set the person's destination as the place they will reach if they travel
	 	// at max speed for this number of seconds
		oPerson._destination = GetDestination(oPerson, direction, (num_seconds * oPerson._maxSpeed));
		
		// if the destination is off the map, force it onto the map
		ForceIntoMap(oPerson._destination);
	}
	
	var AmIThereYet = MoveTo(oPerson, oPerson._destination);
//	WriteDebug1(DistanceBetween(oPerson, oPerson._destination));
	if (AmIThereYet == true)
	{
    // if the person has reached their destination, pick a new one
    oPerson._counter = 0;
  }
  else
  {
  	oPerson._counter -= 1;  
  }
  MoveObject(oPerson);
	return true;
}

function StrategyFollow(oPerson)
{
	// check whether it's time to evaluate strategy
	CheckStrategyCounter(oPerson);
	
	// find the person's leader
	var oLeader = FindPersonByID(oPerson._leaderID);
	Follow(oPerson, oLeader);
	
	MoveObject(oPerson);
	return true;
}

function GameUpdate()
{
	// execute each person's strategy
	var oPerson;

	var i;
	for (i = 0; i <= (people.length-1); i++)
	{
		oPerson=people[i];
		RunStrategy(oPerson);
	}
}



window.onload = InitUI;

//-->




