/*
	The four options of how the four world maps line up in comparison to
	the local map used to display data on the screen (aka the player's view)

	'A' is always a diagonal position from 'D'
	'B' is always top/bottom position from 'D'
	'C' is always left/right position from 'D'
	'D' is always the one the Avatar is currently occupying

	--------- ---------
	| A | B | | B | A | 
	--------- ---------
	| C | D | | D | C |  
	--------- ---------  
			(O)
	--------- ---------
	| C | D | | D | C |  
	--------- ---------
	| A | B | | B | A | 
	--------- ---------
*/

//Checks for the index'd character to see if there was a need to change data in any of the map arrays.

/*
	WX == World's X // Derived from _pc[_PCIndex]->getWorldX();
	WY == World's Y // Derived from _pc[_PCIndex]->getWorldY();
	pQ == Player's Quandrant (within a map section) // Derived from _pc[_PCIndex]->getQuad();
*/

//These are the size of the map data files' width and height...
int maxLocalX = 0;
int maxLocalX = 0;
void UpdateMapData(int WX, int WY, int pQ)
{
	//Localized loop variables
	int x = 0, y = 0;

	Tile tempMap[maxLocalX][maxLocalY];

	if(_oldPCIndex == _PCIndex)
	{
		//Checking to see if they went NW / NE / SW / SE to a new map
		if		((WX < _curWX && WY < _curWY) || (WX > _curWX && WY < _curWY) ||
				 (WX < _curWX && WY > _curWY) || (WX > _curWX && WY > _curWY))
		{
			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 old D-----------------------------------------
				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 old C-----------------------------------------
				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))
		{ 
			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 old 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 old 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))
		{ 
			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 C---------------------------------------------
				LocalD[x][y].setGround		(LocalC[x][y].getGround());
				LocalD[x][y].setResource	(LocalC[x][y].getResource());

				//C copies old D-----------------------------------------
				LocalC[x][y].setGround		(tempMap[x][y].getGround());
				LocalC[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 B---------------------------------------------
				LocalA[x][y].setGround		(LocalB[x][y].getGround());
				LocalA[x][y].setResource	(LocalB[x][y].getResource());

				//B copies old A-----------------------------------------
				LocalB[x][y].setGround		(tempMap[x][y].getGround());
				LocalB[x][y].setResource	(tempMap[x][y].getResource());
			}
		}
		//They're on the same map?
		else if (WX == _curWX && WY == _curWY)
		{
			//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
				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
				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
				LoadMap('A', WX, WY, pQ); LoadMap('B', WX, WY, pQ); LoadMap('C', WX, WY, pQ);
			}
		}
	}
	else GameStartMapLoader(_pc[_PCIndex]->getWorldX(), _pc[_PCIndex]->getWorldY(), _pc[_PCIndex]->getQuad());
}
//This function takes data from the four map arrays, and uses the index'd
//character to decide what to load into the map array for the screen
void UpdateScreenData()
{
	int x = 0, y = 0;
	int OffX = 0, OffY = 0;

	for(y = 0; y < maxScreenY; y++)
	{
		for(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 diagonal corners from 'D'
			//NW corner
			if		(OffX < 0 && OffY < 0)	
			{										
				Screen[x][y].setGround
					(LocalA[(maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX / 2) + x)]
						   [(maxLocalY) + (_pc[_PCIndex]->getY() - (maxScreenY / 2) + y)].getGround());
				Screen[x][y].setResource
					(LocalA[(maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX / 2) + x)]
						   [(maxLocalX) + (_pc[_PCIndex]->getY() - (maxScreenY / 2) + y)].getResource());
			}
			//SE Corner
			else if	(OffX > (maxLocalX - 1) && OffY > (maxLocalY - 1))
			{
				Screen[x][y].setGround
					(LocalA[(_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1))]
						   [(_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1))].getGround());
				Screen[x][y].setResource
					(LocalA[(_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1))]
						   [(_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1))].getResource());
			}
			//NE Corner
			else if	(OffX > (maxLocalX - 1) && OffY < 0)
			{
				Screen[x][y].setGround
					(LocalA[(_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1))]
						   [(maxLocalX) + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y)].getGround());
				Screen[x][y].setResource
					(LocalA[(_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1))]
						   [(maxLocalX) + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y)].getResource());
			}
			//SW Corner
			else if	(OffX < 0 && OffY > (maxLocalY - 1))
			{
				Screen[x][y].setGround
					(LocalA[(maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x)]
						   [(_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1))].getGround());
				Screen[x][y].setResource
					(LocalA[(maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x)]
						   [(_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1))].getResource());
			}
			//LocalB will always be N, or S of the index'd character------------------------------------------
			//North Center
			else if(OffY < 0)
			{
				Screen[x][y].setGround
					(LocalB[OffX]
						   [(maxLocalX) + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y)].getGround());
				Screen[x][y].setResource
					(LocalB[OffX]
						   [(maxLocalX) + (_pc[_PCIndex]->getY() - (maxScreenY/2) + y)].getResource());
			}
			//South Center
			else if(OffY > (maxLocalY - 1))
			{
				Screen[x][y].setGround
					(LocalB[OffX]
						   [(_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1))].getGround());
				Screen[x][y].setResource
					(LocalB[OffX]
						   [(_pc[_PCIndex]->getY() - (maxLocalY - 1)) + (y - (maxScreenY/2 + 1))].getResource());
			}
			//-------------------------------------------------------------------------------------------------
			//LocalC will always be E or W of the index'd character 
			//East Center
			else if(OffX > (maxLocalX - 1))
			{
				Screen[x][y].setGround
					(LocalC[(_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1))]
						   [OffY].getGround());
				Screen[x][y].setResource
					(LocalC[(_pc[_PCIndex]->getX() - (maxLocalX - 1)) + (x - (maxScreenX/2 + 1))]
						   [OffY].getResource());
			}
			//West Center
			else if(OffX < 0)
			{
				Screen[x][y].setGround
					(LocalC[(maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x)]
						   [OffY].getGround());
				Screen[x][y].setResource
					(LocalC[(maxLocalX) + (_pc[_PCIndex]->getX() - (maxScreenX/2) + x)]
						   [OffY].getResource());
			}
			//LocalD is always the map the index'd character is standing 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());
			}
			//Since we're already looping through the arrays...NULL out the occupant for future use
			Screen[x][y].setNULLOccu();
		} //end x - loop
	}//end y - loop
}
			/*Which Map*/
void LoadMap(char wM, int WX, int WY, int pQ)
{
	char		fileName[32]	= {0};
	char		error[32]		= {0};
	int			G = 0, R = 0;
	int			X = 0, Y = 0;//used for determining map file names...
	ifstream	input;

	//Determine map coordinates that need to be used

	//First switch() statement is for which map we're working with...
	//Note: 'A' is always a diagonal map away from 'D'
	//		'B' is always a top or bottom map away from 'D'
	//		'C' is always a left or right map away from 'D'

	//Inside switch() statements are for Quadrant player is in...
	//Note:	Quadrant '1' is top left.....
	//		Quadrant '2' is top right....
	//		Quadrant '3' is bottom left..
	//		Quadrant '4' is bottom right.
	switch(wM)
	{
	case 'A':
		switch(pQ)
		{
		case 1:		X = WX - 1; Y = WY - 1; break;
		case 2:		X = WX + 1; Y = WY - 1; break;
		case 3:		X = WX - 1; Y = WY + 1; break;
		default:	X = WX + 1; Y = WY + 1; break;
		}
		break;
	case 'B':
		switch(pQ)
		{
		case 1:
		case 2:		X = WX; Y = WY - 1;		break;
		default:	X = WX; Y = WY + 1;		break;
		}
		break;
	case 'C':
		switch(pQ)
		{
		case 1:
		case 3:		X = WX - 1; Y = WY;		break;
		default:	X = WX + 1; Y = WY;		break;
		}
		break;
	default:		X = WX; Y = WY;			break;
	}

	//Consolidate the name of the file and then open it...
	sprintf(fileName, "%d_%d.txt", X, Y);
	input.open(fileName);

	if(!input)
	{
		sprintf(error, "Failed to open: %s", fileName);
		MessageBox(NULL, error, "Map File Error", MB_OK);
	}
	else
	{
		for(int y = 0; y < maxLocalY; ++y)
			for(int x = 0; x < maxLocalX; ++x)
			{
				if(!input.eof())
				{
					input >> G >> R;

					switch(wM)
					{
					case 'A':
						LocalA[x][y].setGround(G);
						LocalA[x][y].setResource(R);
						break;
					case 'B':
						LocalB[x][y].setGround(G);
						LocalB[x][y].setResource(R);
						break;
					case 'C':
						LocalC[x][y].setGround(G);
						LocalC[x][y].setResource(R);
						break;
					default:
						LocalD[x][y].setGround(G);
						LocalD[x][y].setResource(R);
						break;
					}
				}
			}
	}
	//Failed or not...close the file...
	input.close();
}
//World-X, -Y are the coordinates of the Avatar...
//The pQ (Quadrant) is the Player's Quadrant within the D-Map.
void GameStartMapLoader(int WorldX, int WorldY, int pQ)
{
	char mapA[32] = {0};
	char mapB[32] = {0};
	char mapC[32] = {0};
	char mapD[32] = {0};

	//Localized iterators for loading map data...
	int x = 0, y = 0, z = 0;

	//G = Ground, R = Resource
	int G = 0, R = 0; 

	ifstream input;

	//Setting file names according to which quadrant the player is standing
	switch(pQ)
	{
	case 1:
		sprintf(mapA, "%d_%d.txt", WorldX - 1,	WorldY - 1);
		sprintf(mapB, "%d_%d.txt", WorldX,		WorldY - 1);
		sprintf(mapC, "%d_%d.txt", WorldX - 1, WorldY);
		break;
	case 2:
		sprintf(mapA, "%d_%d.txt", WorldX + 1,	WorldY - 1);
		sprintf(mapB, "%d_%d.txt", WorldX,		WorldY - 1);
		sprintf(mapC, "%d_%d.txt", WorldX + 1,	WorldY);
		break;
	case 3:
		sprintf(mapA, "%d_%d.txt", WorldX - 1,	WorldY + 1);
		sprintf(mapB, "%d_%d.txt", WorldX,		WorldY + 1);
		sprintf(mapC, "%d_%d.txt", WorldX - 1,	WorldY);
		break;
	default:
		sprintf(mapA, "%d_%d.txt", WorldX + 1,	WorldY + 1);
		sprintf(mapB, "%d_%d.txt", WorldX,		WorldY + 1);
		sprintf(mapC, "%d_%d.txt", WorldX + 1,	WorldY);
		break;
	}

	sprintf(mapD, "%d_%d.txt", WX, WY);

	input.open(mapA);
	for(y = 0; y < maxLocalY; ++y)
		for(x = 0; x < maxLocalX; ++x)
			if(!input.eof())
			{
				input >> G >> R; 
				LocalA[x][y].setGround(G);
				LocalA[x][y].setResource(R);
			}
	input.close();

	input.open(mapB);
	for(y = 0; y < maxLocalY; ++y)
		for(x = 0; x < maxLocalX; ++x)
			if(!input.eof())
			{
				input >> G >> R;
				LocalB[x][y].setGround(G);	
				LocalB[x][y].setResource(R);
			}
	input.close();

	input.open(mapC);
	for(y = 0; y < maxLocalY; ++y)
		for(x = 0; x < maxLocalX; ++x)
			if(!input.eof())
			{
				input >> G >> R;
				LocalC[x][y].setGround(G);	
				LocalC[x][y].setResource(R);
			}
	input.close();

	input.open(mapD);
	for(y = 0; y < maxLocalY; ++y)
		for(x = 0; x < maxLocalX; ++x)
			if(!input.eof())
			{
				input >> G >> R;
				LocalD[x][y].setGround(G);	
				LocalD[x][y].setResource(R);
			}
	input.close();
}