/**
* Model.java                                         
*
* --Copyright notice-- 
*
* Copyright (c) School of Geography, University of Leeds. 
* http://www.geog.leeds.ac.uk/
* This software is licensed under 'The Artistic License' which can be found at 
* the Open Source Initiative website at... 
* http://www.opensource.org/licenses/artistic-license.php
* Please note that the optional Clause 8 does not apply to this code.
*
* The Standard Version source code, and associated documentation can be found at... 
* [online] http://www.ccg.leeds.ac.uk/software/ 
*
* --End of Copyright notice-- 
*
*/

 
/**
* Main class for running a model.
* The class contains an array of agents plus a landscape object.
* The landscape keeps track of the density of agents in each location.
* The agents move randomly if they are in an area crowded with other agents.
* They move randomly if this is the case.
* Eventually they should end up evenly distributed.
* @author <A href="http://www.geog.leeds.ac.uk/people/a.evans/">Dr Andy Evans</A>
* @version 1.0
*/
public class Model {

	
	/**
	* Most stuff is done in main, to make using MPJ easier to integrate 
	* at a later stage. This version is not currently parallelised.
	*/
	public static void main (String args[]) {

		int width = 10;				// Landscape width
		int height = 10;			// Landscape height
		int densityLimit = 100;		// Density above which agents move.
		int numberOfAgents = 1000;
		int iterations = 100;		// Model iterations before stopping
		Agent [] agents = null;

		// Setup landcsape.
		
		Landscape landscape = new Landscape();
		landscape.setWidth(width);
		landscape.setHeight(height);

		// Setup agents.

		agents = new Agent[numberOfAgents];

		for (int i = 0; i < numberOfAgents; i++) {

			agents[i] = new Agent();
			agents[i].setDensityLimit(densityLimit);
			agents[i].setLandscape(landscape);		// Agents get a reference to the 
													// landscape to interrogate for densities.
													// They could also get a reference to the 
													// agent list if agents need to talk, 
													// but here they don't.

			// Randomly allocate agents a start location.
			
			int x = (new Double(Math.random()*(double)width)).intValue();
			int y = (new Double(Math.random()*(double)height)).intValue();

			agents[i].setX(x); 
			agents[i].setY(y); 

		} 

		// Give the landscape a reference to the agent list so it 
		// can find out where they all are and calculate densities.
		
		landscape.setAgents(agents);




		// Run

		for (int time = 0; time < iterations; time++) {

			landscape.calcDensities();

			for (int i = 0; i < numberOfAgents; i++) {
				agents[i].step();
			} 

		}




		// Report

		for (int x = 0 ; x < width; x++) {
			for (int y = 0; y < height; y++) {
				System.out.print(landscape.getDensity(x,y) + " ");
			} 
			System.out.println("");
		}


	}

}