#include "Human.h"
using namespace std;




//Default Constructor
Human::Human() {}

//CREATION!!!!!!!
Human::Human(City *city, int width, int height)
{
	this->city = city;
	this->width = width;
	this->height = height;
	//Random generator seeded with the time
	srand(time(NULL));
}

 //DESTRUCTION!!!!!
Human::~Human() {}


void Human::move()
{
	vector<int> moveDirections; //vector fo rgetting the possible moves

	if (y - 1 >= 0 && city->getOrganism(x, y - 1) == NULL) {
		
		moveDirections.push_back(NORTH);
	
	}
	
	if (y + 1 < height && city->getOrganism(x, y + 1) == NULL) {
		
		moveDirections.push_back(SOUTH);
	
	}

	if (x - 1 >= 0 && city->getOrganism(x - 1, y) == NULL) { 
		
		moveDirections.push_back(WEST);
	
	}

	if (x + 1 < width && city->getOrganism(x + 1, y) == NULL) { 
		
		moveDirections.push_back(EAST); 
	
	}

	if (!moveDirections.empty()){

		//pickas a random direction to move in
		int move = moveDirections.at(rand() % moveDirections.size());

		city->setOrganism(NULL, x, y); //clears the last posision

		//puts a human in the next space
		if (move == NORTH) {
			
			this->setPosition(x, y - 1);
		
		}

		else if (move == SOUTH) { 
			
			this->setPosition(x, y + 1); 
		
		}

		else if (move == EAST) {
			
			this->setPosition(x + 1, y); 
		
		}

		else if (move == WEST) {
			
			this->setPosition(x - 1, y); 
		
		}

		city->setOrganism(this, x, y); //add human to the city
	}

	 //the human has now moved
	this->setHasMoved(true);

}


void Human::spawn() {

	//if there are max humans spawned
	if (this->getSpawnCount() >= 3){

		//vector for storing the possible ways a human can move
		vector<int> moveDirections;
		srand(time(NULL));

		
		if (y - 1 >= 0 && city->getOrganism(x, y - 1) == NULL) { 

			moveDirections.push_back(NORTH); 

		}

		if (y + 1 < height && city->getOrganism(x, y + 1) == NULL) {

			moveDirections.push_back(SOUTH); 
		
		}

		if (x + 1 < width && city->getOrganism(x + 1, y) == NULL) { 
			
			moveDirections.push_back(EAST); 
		
		}

		if (x - 1 >= 0 && city->getOrganism(x - 1, y) == NULL) {
			
			moveDirections.push_back(WEST); 
		
		}

		//Picks a arondom move
		if (!moveDirections.empty()) {

			//picks a random number to plug into the vector
			int spawnGridSpot = moveDirections.at(rand() % moveDirections.size()); 

			//new Human!!
			Human *myHuman = new Human(city, GRID_WIDTH, GRID_HEIGHT);

			//human's new position is set
			if (spawnGridSpot == NORTH) { 
				
				myHuman->setPosition(x, y - 1); 
			
			}

			else if (spawnGridSpot == SOUTH) { myHuman->setPosition(x, y + 1); }

			else if (spawnGridSpot == EAST) { 
				
				myHuman->setPosition(x + 1, y); 
			
			}

			else if (spawnGridSpot == WEST) {
				
				myHuman->setPosition(x - 1, y); 
			
			}

			//puts human in the grid
			city->setOrganism(myHuman, myHuman->x, myHuman->y);

			//Human has now moved
			myHuman->setHasMoved(true);
			myHuman->setSpawnCount(0); 
			myHuman->setHasSpawned(true);

		}

		 
		//this->setSpawnCount(0);
	}
	else
		//increases counter
		this->setSpawnCount(this->getSpawnCount() + 1); 

}