#include "main.h"
#include "bmp2map.h"



//--------------------------------------------------
//
//--------------------------------------------------
void Mountain()
{
	int x, y, i;
	int chk=1;
	//rrggbb

	mntn *neu;
	neu=a_mntn;
	

	
	for(x=MinX; x<MaxX; x++)
		for(y=MinY; y<MaxY; y++)
		{
		neu=a_mntn;
			while( neu->next!=NULL )
			{
			if(BMPID[x][y] == neu->color)
				MapOcc[x][y]=20;
			neu=neu->next;
			}
		}

	for(x=MinX; x<MaxX; x++)
		for(y=MinY; y<MaxY; y++)
		{
		neu=a_mntn;
		chk=1;
			while( neu->next!=NULL )
			{
				if(BMPID[x][y] == neu->color)
				{
					MapID[x][y]=RandomID(neu->ID);
					MapAlt[x][y]=RandomNum(neu->zlev[0],neu->zlev[1]);
					for(i=0;i<14;i+=2)		
						if(MapOcc[x][y-i]!=0 && MapOcc[x+i][y-i]!=0 && MapOcc[x+i][y]!=0 && MapOcc[x+i][y+i]!=0 && MapOcc[x][y+i]!=0 && MapOcc[x-i][y+i]!=0 && MapOcc[x-i][y]!=0 && MapOcc[x-i][y-i]!=0)
						{
							MapAlt[x][y]=RandomNum(neu->zlev[i],neu->zlev[i+1]);
							if( MapAlt[x][y]>127)
								MapAlt[x][y]=RandomNum(120,125);
							if( neu->color_spez!=0 && (MapAlt[x][y]>115 || i>8) )
								MapOcc[x][y]=chk;
						}
						else break;
				}
				chk++;
				neu=neu->next;
			}
		}

	for(x=MinX; x<MaxX; x++)
		for(y=MinY; y<MaxY; y++)
		{
			if(MapOcc[x][y]==20)
				MapOcc[x][y]=0;
			if(MapOcc[x][y]>0)
			{
				neu=a_mntn;
				while( MapOcc[x][y]!=1 )
				{
					neu=neu->next;
					MapOcc[x][y]--;
				}
				BMPID[x][y] = neu->color_spez;
				MapID[x][y] = RandomID(neu->ID_spez);
				MapOcc[x][y]=0;
			}
		}
}



//--------------------------------------------------
//
//--------------------------------------------------
void SmoothTiles(int x, int y)
{
smooth*neu;

	neu=a_smooth;

	while( neu->next!=NULL )
	{
		SmoothTilesCheck(x, y, neu);
		neu=neu->next;
	}
}


//--------------------------------------------------
//
//--------------------------------------------------
void SmoothTilesCheck(int x, int y, smooth *neu)
{
	int special = 0;
	int z;
	int A;
	int B;

	farbe *farbe1 = neu->color_a;
	farbe *farbe2;
	
	while( farbe1 != NULL )
	{
		A = farbe1->color;

		farbe2 = neu->color_b;
		while( farbe2 != NULL )
		{
			B = farbe2->color;
		
			if(B==0)
			{
				if(MapOcc[x][y]==0 && BMPID[x][y]==A)
				{
					// x = nicht def.

					//KANTEN
 					//xB
					//Ax
					if(BMPID[x+1][y-1]!=A) 
					{
						if(neu->side[7][1]==0)
							MapID[x][y]=neu->side[7][0];
						else MapID[x][y]=neu->side[7][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y-1]+MapAlt[x-1][y+1];
					}
					//Bx
					//xA
					if(BMPID[x-1][y-1]!=A) 
					{
						if(neu->side[8][1]==0)
							MapID[x][y]=neu->side[8][0];
						else MapID[x][y]=neu->side[8][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x-1][y-1]+MapAlt[x+1][y+1];
					}
					//GA
					//BG
					if(BMPID[x-1][y+1]!=A)
					{
						if(neu->side[5][1]==0)
							MapID[x][y]=neu->side[5][0];
						else MapID[x][y]=neu->side[5][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x-1][y+1]+MapAlt[x+1][y-1];
					}
					//Ax
					//xB
					if(BMPID[x+1][y+1]!=A)
					{
						if(neu->side[4][1]==0)
							MapID[x][y]=neu->side[4][0];
						else MapID[x][y]=neu->side[4][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y+1]+MapAlt[x-1][y-1];
					}
		
					//SEITEN
					// B 
					//xAx
					if(BMPID[x][y-1]!=A)
					{
						if(neu->side[2][1]==0)
							MapID[x][y]=neu->side[2][0];
						else MapID[x][y]=neu->side[2][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x][y-1]+MapAlt[x][y+1];
					}
					//xAx
					// B 
					if(BMPID[x][y+1]!=A)
					{
						if(neu->side[0][1]==0)
							MapID[x][y]=neu->side[0][0];
						else MapID[x][y]=neu->side[0][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x][y+1]+MapAlt[x][y-1];
					}
					//x
					//AB 
					//x
					if(BMPID[x+1][y]!=A) 
					{
						if(neu->side[3][1]==0)
							MapID[x][y]=neu->side[3][0];
						else MapID[x][y]=neu->side[3][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y]+MapAlt[x-1][y];
					}
					// x
					//BA 
					// x
					if(BMPID[x-1][y]!=A)
					{
						if(neu->side[1][1]==0)
							MapID[x][y]=neu->side[1][0];
						else MapID[x][y]=neu->side[1][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x-1][y]+MapAlt[x+1][y];
					}

					//ECKEN
					//B
					//AB
					if(BMPID[x+1][y]!=A && BMPID[x][y-1]!=A)
					{
						if(neu->side[11][1]==0)
							MapID[x][y]=neu->side[11][0];
						else MapID[x][y]=neu->side[11][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y-1]+MapAlt[x-1][y+1];
					}
					// B
					//BA
					if(BMPID[x-1][y]!=A && BMPID[x][y-1]!=A)
					{
						if(neu->side[10][1]==0)
							MapID[x][y]=neu->side[10][0];
						else MapID[x][y]=neu->side[10][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x-1][y-1]+MapAlt[x+1][y+1];
					}
					//BA
					// B
					if(BMPID[x-1][y]!=A && BMPID[x][y+1]!=A)
					{
						if(neu->side[9][1]==0)
							MapID[x][y]=neu->side[9][0];
						else MapID[x][y]=neu->side[9][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x-1][y+1]+MapAlt[x+1][y-1];
					}
					//AB
					//B
					if(BMPID[x+1][y]!=A && BMPID[x][y+1]!=A) 
					{
						if(neu->side[8][1]==0)
							MapID[x][y]=neu->side[8][0];
						else MapID[x][y]=neu->side[8][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y+1]+MapAlt[x-1][y-1];
					}
				}
			}
			else
			{
				if(MapOcc[x][y]==0 && BMPID[x][y]==A)
				{
					// B = B
					// x = A

					//KANTEN
 					//xB
					//Ax
					if(BMPID[x+1][y-1]==B) 
					{
						if(neu->side[7][1]==0)
							MapID[x][y]=neu->side[7][0];
						else MapID[x][y]=neu->side[7][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y-1]+MapAlt[x-1][y+1];
					}
					//Bx
					//xA
					if(BMPID[x-1][y-1]==B) 
					{
						if(neu->side[6][1]==0)
							MapID[x][y]=neu->side[6][0];
						else MapID[x][y]=neu->side[6][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x-1][y-1]+MapAlt[x+1][y+1];
					}
					//xA
					//Bx
					if(BMPID[x-1][y+1]==B)
					{
						if(neu->side[5][1]==0)
							MapID[x][y]=neu->side[5][0];
						else MapID[x][y]=neu->side[5][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x-1][y+1]+MapAlt[x+1][y-1];
					}
					//Ax
					//xB
					if(BMPID[x+1][y+1]==B)
					{
						if(neu->side[4][1]==0)
							MapID[x][y]=neu->side[4][0];
						else MapID[x][y]=neu->side[4][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y+1]+MapAlt[x-1][y-1];
					}
		
					//SEITEN
					// B
					//xAx
					if(BMPID[x][y-1]==B)
					{
						if(neu->side[2][1]==0)
							MapID[x][y]=neu->side[2][0];
						else MapID[x][y]=neu->side[2][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x][y-1]+MapAlt[x][y+1];
					}
					//xAx
					// B 
					if(BMPID[x][y+1]==B)
					{
						if(neu->side[0][1]==0)
							MapID[x][y]=neu->side[0][0];
						else MapID[x][y]=neu->side[0][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x][y+1]+MapAlt[x][y-1];
					}
					//x
					//AB 
					//x
					if(BMPID[x+1][y]==B) 
					{
						if(neu->side[3][1]==0)
							MapID[x][y]=neu->side[3][0];
						else MapID[x][y]=neu->side[3][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y]+MapAlt[x-1][y];
					}
					// x
					//BA 
					// x
					if(BMPID[x-1][y]==B)
					{
						if(neu->side[1][1]==0)
							MapID[x][y]=neu->side[1][0];
						else MapID[x][y]=neu->side[1][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x-1][y]+MapAlt[x+1][y];
					}

					//ECKEN
					//B
					//AB
					if(BMPID[x+1][y]==B && BMPID[x][y-1]==B)
					{
						if(neu->side[11][1]==0)
							MapID[x][y]=neu->side[11][0];
						else MapID[x][y]=neu->side[11][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y-1]+MapAlt[x-1][y+1];
					}
					// B
					//BA
					if(BMPID[x-1][y]==B && BMPID[x][y-1]==B)
					{
						if(neu->side[10][1]==0)
							MapID[x][y]=neu->side[10][0];
						else MapID[x][y]=neu->side[10][RandomNum(0,1)];
						special = 1;
						z=MapAlt[x-1][y-1]+MapAlt[x+1][y+1];
					}
					//BA
					// B
					if(BMPID[x-1][y]==B && BMPID[x][y+1]==B)
					{
						if(neu->side[9][1]==0)
							MapID[x][y]=neu->side[9][0];
						else MapID[x][y]=neu->side[9][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x-1][y+1]+MapAlt[x+1][y-1];
					}
					//AB
					//B
					if(BMPID[x+1][y]==B && BMPID[x][y+1]==B) 
					{
						if(neu->side[8][1]==0)
							MapID[x][y]=neu->side[8][0];
						else MapID[x][y]=neu->side[8][RandomNum(0,1)];
						special = 2;
						z=MapAlt[x+1][y+1]+MapAlt[x-1][y-1];
					}
				}
			}

			farbe2 = farbe2->next;
		}
		farbe1 = farbe1->next;
	}

	if(special>0)
		MapOcc[x][y] = 1;
	
	if(BMPZ[x][y]==0)							// Höhenvorbesetzung berücksichtigen
	{
		if(special>=1 && neu->special>=1)
			MapAlt[x][y] = z / 2;
		if(special==2 && neu->special==2)
			MapAlt[x][y] = RandomNum(20, 30);
	}

}


