//This method marks a cell in the maze as "in" and add the cells around to the frointer set
void Maze::expandMaze(int x, int y)
{
  grid[x][y].inMaze = CellState::in;
  addFrontier(x-1, y);
  addFrontier(x+1, y);
  addFrontier(x, y-1);
  addFrontier(x, y+1);
}
/**
 * @brief finds shortest path and gives node pointers to player to calculate velocity
 * @param map the map in question, not really needed.
 * @param thisCharacter Character that is to be moved.
 */
void NonPlayerCharacter::aStar(Tile*** const map, Character* thisCharacter)
{
	Tile * thisTile = NULL;
	Node * visitNode;
	Node * tempNode;
	bool stackFlag = false;
	int area;

	Node * stackNode = NULL;


	World * check =  World::getWorld();
	area = check->getArea();

	for (int yCount = 0; yCount < area; yCount++)
	{
		for (int xCount =0; xCount < area; xCount++)
		{
			thisTile = map[xCount] [yCount];
			//should not be needed if the character could know position as well.
			if (thisCharacter == thisTile->getHasCharacter()) {
				startNode = new Node(xCount, yCount, 0, 0, 0, NULL);
				startNode->unVisit();
			}

			if (thisTile->getIsGoal()) {
				goalNode = new Node(xCount, yCount, 0, xCount, yCount, NULL);
				goalNode->unVisit();
			}
		}
	}

	//until a complete stack has flagged
	while (!stackFlag && goalNode)
	{
		visitNode = startNode->findCheapestUnusedRecursively();

		if (visitNode)
		{
		visitNode->setVisit();


			//when the goal has been reached, meaning they can create a stack
			if( visitNode->getXPos() == goalNode->getXPos()
				&& visitNode->getYPos() == goalNode->getYPos())
			{
				stackNode = visitNode;

				thisCharacter->newStack(stackNode->getXPos(), stackNode->getYPos());
				if (startNode != stackNode)
				{
					while (startNode != stackNode)
					{
						stackNode = stackNode->getParent();
						thisCharacter->addStack(stackNode->getXPos(), stackNode->getYPos());
					}

					stackFlag = true;

					tempNode = stackNode->getParent();

				}

				else
				{
					//means no move is necessary
					tempNode = startNode;
				}

			}

				//when goal is not reached
			else
			{
				//adds new nodes to the frontier
				visitNode->setUpChild(addFrontier(visitNode->getXPos(), visitNode->getYPos(), 0, -1, visitNode, thisCharacter));
				visitNode->setRightChild(addFrontier(visitNode->getXPos(), visitNode->getYPos(), 1, 0, visitNode, thisCharacter));
				visitNode->setDownChild(addFrontier(visitNode->getXPos(), visitNode->getYPos(), 0, 1, visitNode, thisCharacter));
				visitNode->setLeftChild(addFrontier(visitNode->getXPos(), visitNode->getYPos(), -1, 0, visitNode, thisCharacter));
			}
		}
		else
		{
			stackFlag = true;
		}
	}


	//call tree destructor
	//pthread lol
	//	loldestroyeverything
	if (startNode)
	{
		delete startNode;
	}
	if (goalNode)
	{
		delete goalNode;
	}

}