//This function does checks for the index'd character to see if there 
//was a need to change data in any of the map arrays.
void UpdateMapData()
{
	int x, y;
	int WX = _pc[_PCIndex]->getWorldX();
	int WY = _pc[_PCIndex]->getWorldY();
	int pQ = _pc[_PCIndex]->getQuad();

	Tile tempMap[maxLocalX][maxLocalY];

	ifstream input;

	//Checking to see if they went NW / NE / SW / SE to a new map
	if		(((pQ == 4) && (WX < _curWX && WY < _curWY)) || ((pQ == 3) && (WX > _curWX && WY < _curWY)) ||
			 ((pQ == 2) && (WX < _curWX && WY > _curWY)) || ((pQ == 1) && (WX > _curWX && WY > _curWY)) && _oldPCIndex == _PCIndex)
	{
		//Swap the maps within the quandrant maps
		for(y = 0; y < maxLocalY; y++)
			for(x = 0; x < maxLocalX; x++)
			{
				//temp copies D------------------------------------------
				tempMap[x][y].setGround		(LocalD[x][y].getGround());
				tempMap[x][y].setResource	(LocalD[x][y].getResource());
				//D copies A---------------------------------------------
				LocalD[x][y].setGround		(LocalA[x][y].getGround());
				LocalD[x][y].setResource	(LocalA[x][y].getResource());
				//A copies D (temp)--------------------------------------
				LocalA[x][y].setGround		(tempMap[x][y].getGround());
				LocalA[x][y].setResource	(tempMap[x][y].getResource());

				//temp copies C------------------------------------------
				tempMap[x][y].setGround		(LocalC[x][y].getGround());
				tempMap[x][y].setResource	(LocalC[x][y].getResource());
				//C copies B---------------------------------------------
				LocalC[x][y].setGround		(LocalB[x][y].getGround());
				LocalC[x][y].setResource	(LocalB[x][y].getResource());
				//B copies C (temp)--------------------------------------
				LocalB[x][y].setGround		(tempMap[x][y].getGround());
				LocalB[x][y].setResource	(tempMap[x][y].getResource());
			}
	}
	//Checking to see if they went N / S to a new map
	else if (((pQ == 3 || pQ == 4) && (WY < _curWY)) || ((pQ == 1 || pQ == 2) && (WY > _curWY)) && _oldPCIndex == _PCIndex)
	{ 
		for(y = 0; y < maxLocalY; y++)
			for(x = 0; x < maxLocalX; x++)
			{
				//temp copies D------------------------------------------
				tempMap[x][y].setGround		(LocalD[x][y].getGround());
				tempMap[x][y].setResource	(LocalD[x][y].getResource());
				//D copies B---------------------------------------------
				LocalD[x][y].setGround		(LocalB[x][y].getGround());
				LocalD[x][y].setResource	(LocalB[x][y].getResource());
				//B copies D---------------------------------------------
				LocalB[x][y].setGround		(tempMap[x][y].getGround());
				LocalB[x][y].setResource	(tempMap[x][y].getResource());

				//temp copies A------------------------------------------
				tempMap[x][y].setGround		(LocalA[x][y].getGround());
				tempMap[x][y].setResource	(LocalA[x][y].getResource());
				//A copies C---------------------------------------------
				LocalA[x][y].setGround		(LocalC[x][y].getGround());
				LocalA[x][y].setResource	(LocalC[x][y].getResource());
				//C copies A---------------------------------------------
				LocalC[x][y].setGround		(tempMap[x][y].getGround());
				LocalC[x][y].setResource	(tempMap[x][y].getResource());
			}
	}
	//Checking to see if they went W / E to a new map
	else if (((pQ == 2 || pQ == 4) && (WX < _curWX)) || ((pQ == 1 || pQ == 3) && (WX > _curWX)) && _oldPCIndex == _PCIndex )
	{ 
		for(y = 0; y < maxLocalY; y++)
			for(x = 0; x < maxLocalX; x++)
			{
				//tmp copies D for saving--------------------------------
				tempMap[x][y].setGround		(LocalD[x][y].getGround());
				tempMap[x][y].setResource	(LocalD[x][y].getResource());
				//D copies C---------------------------------------------
				LocalD[x][y].setGround		(LocalC[x][y].getGround());
				LocalD[x][y].setResource	(LocalC[x][y].getResource());
				//C copies D---------------------------------------------
				LocalC[x][y].setGround		(tempMap[x][y].getGround());
				LocalC[x][y].setResource	(tempMap[x][y].getResource());

				//tmp copies A for saving--------------------------------
				tempMap[x][y].setGround		(LocalA[x][y].getGround());
				tempMap[x][y].setResource	(LocalA[x][y].getResource());
				//A copies B---------------------------------------------
				LocalA[x][y].setGround		(LocalB[x][y].getGround());
				LocalA[x][y].setResource	(LocalB[x][y].getResource());
				//B copies A---------------------------------------------
				LocalB[x][y].setGround		(tempMap[x][y].getGround());
				LocalB[x][y].setResource	(tempMap[x][y].getResource());
			}
	}
	//Ensuring it is the same map they are on
	else if ((WX == _curWX && WY == _curWY) && _oldPCIndex == _PCIndex)
	{
		//This is where the maps actually get loaded
		if		((_curQuad == 1 && _pc[_PCIndex]->getQuad() == 2) || (_curQuad == 2 && _pc[_PCIndex]->getQuad() == 1) ||
				 (_curQuad == 3 && _pc[_PCIndex]->getQuad() == 4) || (_curQuad == 4 && _pc[_PCIndex]->getQuad() == 3))
		{ 
			//D and B do nothing - A and C get loaded
			LoadMap('A', WX, WY, pQ);
			LoadMap('C', WX, WY, pQ);
		}
		else if ((_curQuad == 1 && _pc[_PCIndex]->getQuad() == 3) || (_curQuad == 3 && _pc[_PCIndex]->getQuad() == 1) ||
				 (_curQuad == 2 && _pc[_PCIndex]->getQuad() == 4) || (_curQuad == 4 && _pc[_PCIndex]->getQuad() == 2))
		{ 
			//D and C do nothing - A and B get loaded
			LoadMap('A', WX, WY, pQ);
			LoadMap('B', WX, WY, pQ);
		}
		else if ((_curQuad == 1 && _pc[_PCIndex]->getQuad() == 4) || (_curQuad == 4 && _pc[_PCIndex]->getQuad() == 1) ||
				 (_curQuad == 2 && _pc[_PCIndex]->getQuad() == 3) || (_curQuad == 3 && _pc[_PCIndex]->getQuad() == 2))
		{ 
			//D does nothing - A B and C get loaded
			LoadMap('A', WX, WY, pQ);
			LoadMap('B', WX, WY, pQ);
			LoadMap('C', WX, WY, pQ);
		}
	}
	//Total reload if the Avatar Index does not match
	else if	(_oldPCIndex != _PCIndex)
	{
		GameStartMapLoader(_pc[_PCIndex]->getWorldX(), _pc[_PCIndex]->getWorldY(), _pc[_PCIndex]->getQuad());
	}
	//Default "You screwed up message"
	else
	{
		MessageBox(NULL, "Michael Screwed up the tests to determine quadrant", 
			"UpdateMapData()", MB_OK);
	}
}
//This function takes data from the four map arrays, and uses the index'd
//character to decide what to load into the player's view screen
void UpdateScreenData()
{
	int OffX, OffY;
	int littleX, littleY;

	for(int y = 0; y < maxScreenY; y++)
		for(int x = 0; x < maxScreenX; x++)
		{
			//Theses variables are made generic to allow for the game screen to be altered by 
			//simply changing the maxScreenX(Y) values in the global variables section
			OffX = (_pc[_PCIndex]->getX() - (maxScreenX/2)) + x;
			OffY = (_pc[_PCIndex]->getY() - (maxScreenY/2)) + y;
			
			//Data contained in LocalA array will always be in the 
			//NW, NE, SW, or SE corners compared to the index'd character

			//-------------------------------------------------------------------------------------------------
			//NW corner
			if		(OffX < 0 && OffY < 0)	
			{
				littleX = maxLocalX + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x);
				littleY = maxLocalY + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y);
				Screen[x][y].setGround	(LocalA[littleX][littleY].getGround());

				littleX = maxLocalX + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x);
				littleY = maxLocalY + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y);
				Screen[x][y].setResource(LocalA[littleX][littleY].getResource());
			}
			//NE Corner
			else if	(OffX > maxLocalX - 1 && OffY < 0)
			{
				littleX = (_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1));
				littleY = (maxLocalX) + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y);
				Screen[x][y].setGround	(LocalA[littleX][littleY].getGround());

				littleX = (_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1));
				littleY = (maxLocalX) + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y);
				Screen[x][y].setResource(LocalA[littleX][littleY].getResource());
			}
			//SW Corner
			else if	(OffX < 0 && OffY > maxLocalY - 1)
			{
				littleX = (maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x);
				littleY = (_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1));
				Screen[x][y].setGround	(LocalA[littleX][littleY].getGround());

				littleX = (maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x);
				littleY = (_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1));
				Screen[x][y].setResource(LocalA[littleX][littleY].getResource());
			}
			//SE Corner
			else if	(OffX > maxLocalX - 1 && OffY > maxLocalY - 1)
			{
				littleX = (_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1));
				littleY = (_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1));
				Screen[x][y].setGround	(LocalA[littleX][littleY].getGround());

				littleX = (_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1));
				littleY = (_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1));
				Screen[x][y].setResource(LocalA[littleX][littleY].getResource());
			}
			//-------------------------------------------------------------------------------------------------
			//LocalB will always be N, or S of the index'd character
			//South Center
			else if	((OffX >= 0 && OffX < maxLocalX) && OffY > maxLocalY - 1)
			{
				littleY = (_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1));
				Screen[x][y].setGround	(LocalB[OffX][littleY].getGround());
				Screen[x][y].setResource(LocalB[OffX][littleY].getResource());
			}
			//North Center
			else if	((OffX >= 0 && OffX < maxLocalX) && OffY < 0)
			{
				littleY = maxLocalX + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y);
				Screen[x][y].setGround	(LocalB[OffX][littleY].getGround());
				Screen[x][y].setResource(LocalB[OffX][littleY].getResource());
			}
			//-------------------------------------------------------------------------------------------------
			//LocalC will always be E or W of the index'd character
			//East Center
			else if	((OffY >= 0 && OffY < maxLocalY) && OffX > maxLocalX - 1)
			{
				littleX = (_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1));
				Screen[x][y].setGround	(LocalC[littleX][OffY].getGround());
				Screen[x][y].setResource(LocalC[littleX][OffY].getResource());
			}
			//West Center
			else if	((OffY >= 0 && OffY < maxLocalY) && OffX < 0)
			{
				littleX = (maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x);
				Screen[x][y].setGround	(LocalC[littleX][OffY].getGround());
				Screen[x][y].setResource(LocalC[littleX][OffY].getResource());
			}
			//-------------------------------------------------------------------------------------------------
			//LocalD is always the map the index'd character is standing on
			//The map for which you're on
			else if	((OffX >= 0 && OffX < maxLocalX) && (OffY >= 0 && OffY < maxLocalY))
			{
				Screen[x][y].setGround	(LocalD[OffX][OffY].getGround());
				Screen[x][y].setResource(LocalD[OffX][OffY].getResource());
			} 
			else
			{
				MessageBox(NULL, "Michael Screwed up the UpdateScreenData()", 
					"UpdateScreenData()", MB_OK);
			}

			//Clear Occupant
			Screen[x][y].setNULLOccu();
		} //end x - loop
}
			//Which Map, Avatar's WorldX, WorldY, Quadrant
void LoadMap(char wM, int WX, int WY, unsigned short pQ)
{
	int tempG = 0, tempR = 0, tmpX = 0, tmpY = 0;
	char error[64], fileName[16];
	ifstream input;

	switch(wM)
	{
	case 'A':
		if		(pQ == 1)		{ tmpX = WX - 1; tmpY = WY - 1; }
		else if	(pQ == 2)		{ tmpX = WX + 1; tmpY = WY - 1; }
		else if	(pQ == 3)		{ tmpX = WX - 1; tmpY = WY + 1; }
		else					{ tmpX = WX + 1; tmpY = WY + 1; }
		break;
	case 'B':
		if(pQ == 1 || pQ == 2)	{ tmpX = WX + 0; tmpY = WY - 1; }
		else					{ tmpX = WX + 0; tmpY = WY + 1; }
		break;
	case 'C':
		if(pQ == 1 || pQ == 3)	{ tmpX = WX - 1; tmpY = WY + 0; }
		else					{ tmpX = WX + 1; tmpY = WY + 0; }
		break;
	default: tmpX = WX + 0; tmpY = WY + 0; break;
	};

	sprintf(fileName, "%d %d.txt", tmpX, tmpY);
	input.open(fileName);

	if(!input)
	{
		sprintf(error, "Failed to open: %s", fileName);
		MessageBox(NULL, error, TEXT("Map File Error"), MB_OK);
		return;
	}
	else
	{
		for(int y = 0; y < maxLocalY; y++)
			for(int x = 0; x < maxLocalX; x++)
			{
				input >> tempG >> tempR;
				switch(wM)
				{
				case 'A':	LocalA[x][y].setGround(tempG);	LocalA[x][y].setResource(tempR);	break;
				case 'B':	LocalB[x][y].setGround(tempG);	LocalB[x][y].setResource(tempR);	break;
				case 'C':	LocalC[x][y].setGround(tempG);	LocalC[x][y].setResource(tempR);	break;
				default:	LocalD[x][y].setGround(tempG);	LocalD[x][y].setResource(tempR);	break;
				}
			}
	}
	input.close();
}
						//Avatar's WorldX, WorldY and Quadrant
void GameStartMapLoader(int WX, int WY, int pQ)
{
	const int FileNameSize = 32;
	char fileNameA[FileNameSize] = {0};
	char fileNameB[FileNameSize] = {0};
	char fileNameC[FileNameSize] = {0};
	char fileNameD[FileNameSize] = {0};
	int x;
	int y;
	int tempG, tempR;

	ifstream input;

	/*
	Quadrants are the four sections of map 'D'.
	And laid out in this format...
	---------
	| 1 | 2 |
	---------
	| 3 | 4 |
	---------
	...which determines which map arrays get loaded
	according to the following switch statement.
	*/

	//Setting file names according where player is standing------------
	switch(pQ)
	{
	case 1:
	//	---------
	//	| A | B |
	//	---------
	//	| C | D |
	//	---------
		sprintf(fileNameA, "%d %d.txt", WX - 1, WY - 1);
		sprintf(fileNameB, "%d %d.txt", WX,     WY - 1);
		sprintf(fileNameC, "%d %d.txt", WX - 1, WY);
		break;
	case 2:
	//	---------
	//	| B | A |
	//	---------
	//	| D | C |
	//	---------
		sprintf(fileNameA, "%d %d.txt", WX + 1, WY - 1);
		sprintf(fileNameB, "%d %d.txt", WX,     WY - 1);
		sprintf(fileNameC, "%d %d.txt", WX + 1, WY);
		break;
	case 3:
	//	---------
	//	| C | D |
	//	---------
	//	| A | B |
	//	---------
		sprintf(fileNameA, "%d %d.txt", WX - 1, WY + 1);
		sprintf(fileNameB, "%d %d.txt", WX,     WY + 1);
		sprintf(fileNameC, "%d %d.txt", WX - 1, WY);
		break;
	default:
	//	---------
	//	| D | C |
	//	---------
	//	| B | A |
	//	---------
		sprintf(fileNameA, "%d %d.txt", WX + 1, WY + 1);
		sprintf(fileNameB, "%d %d.txt", WX,     WY + 1);
		sprintf(fileNameC, "%d %d.txt", WX + 1, WY);
		break;
	};

	sprintf(fileNameD, "%d %d.txt", WX, WY);
	//End setting map file names----------------------------------------

	//Looping through each map array to load it.
	input.open(fileNameA);
	for (y = 0; y < maxLocalY; y++)
		for(x = 0; x < maxLocalX; x++)
		{
			input >> tempG >> tempR;
			LocalA[x][y].setGround(tempG);
			LocalA[x][y].setResource(tempR);
		}
	input.close();

	input.open(fileNameB);
	for (y = 0; y < maxLocalY; y++)		
		for(x = 0; x < maxLocalX; x++)
		{
			input >> tempG >> tempR;
			LocalB[x][y].setGround(tempG);
			LocalB[x][y].setResource(tempR);
		}
	input.close();

	input.open(fileNameC);
	for (y = 0; y < maxLocalY; y++)
		for(x = 0; x < maxLocalX; x++)
		{
			input >> tempG >> tempR;
			LocalC[x][y].setGround(tempG);
			LocalC[x][y].setResource(tempR);
		}
	input.close();

	input.open(fileNameD);
	for (y = 0; y < maxLocalY; y++)
		for(x = 0; x < maxLocalX; x++)
		{
			input >> tempG >> tempR;
			LocalD[x][y].setGround(tempG);
			LocalD[x][y].setResource(tempR);
		}
	input.close();
}