
#include "main.h"
#include "bmp2map.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>


//--------------------------------------------------
//	Sichern der orginalen Muls als .org
//--------------------------------------------------
void archivDATA(char name[40])
{
int slen;
char newname[40];

	slen=strlen(name);
	strcpy(newname,name);						// Kopieren des String
	newname[slen-1]='o';						// Endung zu .org ändern
	newname[slen-2]='r';
	newname[slen-3]='g';
	remove(newname);								// alte map0.org ggf. entfernen
	rename(name,newname);						// geänderten Namen übergeben
}


void archivMUL()
{
int slen;
	slen=strlen(mapmul);
	strcpy(orgmap,mapmul);
	slen=strlen(statics);
	strcpy(orgstat,statics);
	slen=strlen(staidx);
	strcpy(orgstaid,staidx);
	archivDATA(mapmul);
	archivDATA(statics);
	archivDATA(staidx);	
}


//--------------------------------------------------
//	Verarbeiten der Höhendifferenz 
//--------------------------------------------------
int processStatics()
{
int blockx, blocky, i, tmp=0, changes=0, xcor=0, ycor=0, items=0, buffer;
char z;


FILE *statics0,*staticsN,*staidx;


	staticsN = fopen(statics, "wb");

	if(((statics0 = fopen(orgstat, "rb"))!=NULL)&&((staidx = fopen(orgstaid, "rb"))!=NULL))
	{
		printf("---------------------------------------\n");
	}
	else 
	{
		printf("\nError: Can't open\n\t\t%s\n\tor\t%s",orgstat,orgstaid);
		getchar();
		getchar();
		exit(-1);
	}
	printf("SYS|Process %s...",statics);

	for(blockx=0; blockx<768; blockx++)
		for(blocky=0; blocky<512; blocky++)
		{
			tmp=0; items=0;
			fread( &buffer, 4, 1, staidx );		// start
			fread( &tmp, 4, 1, staidx );		// length
			fread( &buffer, 4, 1, staidx );

			if(tmp==0xFFFFFFFF)
				continue;
			
			items=tmp/7;
			for(i=0;i<items;i++)
			{
				fread ( &buffer, 2, 1, statics0 );		// ID
				fwrite( &buffer, 2, 1, staticsN );
				fread ( &xcor, 1, 1, statics0 );		// X
				fwrite( &xcor, 1, 1, staticsN );
				fread ( &ycor, 1, 1, statics0 );		// Y
				fwrite( &ycor, 1, 1, staticsN );
					
				fread( &z, 1, 1, statics0 );			// Z
				if(diff[xcor+8*blockx][ycor+8*blocky]!=0)
				{
					z+=diff[xcor+8*blockx][ycor+8*blocky];
					changes++;
				}
				fwrite( &z, 1, 1, staticsN );

				fread ( &buffer, 2, 1, statics0 );		// color
				fwrite( &buffer, 2, 1, staticsN );
			}
		}
	fclose(staidx);
	fclose(statics0);
	fclose(staticsN);
	printf("Staticchanges: %g",changes);
	return 0;
}



//--------------------------------------------------
//	Verarbeiten des neuen Höhenlevels
//--------------------------------------------------
void processMUL()
{
int blockx, blocky, x, y;
int empty=0, id=0, changes=0;							// 0-besetzen, da sonst Probleme
signed char z;
bool check;												// -128 - +127

FILE *map1,*map2;


	if((map1 = fopen(orgmap,"rb"))!=NULL)
	{
		printf("---------------------------------------\n");
		printf("SYS|Load muls...\n");
	}
	else 
	{
		printf("\nError: Can't open %s\n\t\t",orgmap);
		getchar();
		getchar();
		exit(-1);
	}
	printf("SYS|Process %s...",mapmul);

	map2 = fopen(mapmul,"wb");

	for(blockx=0; blockx<768; blockx++)
		for(blocky=0; blocky<512; blocky++)
		{
			fread( &empty, 4, 1, map1 );				// BlockHeader ohne Funktion
			fwrite( &empty, 4, 1, map2 );				// BlockHeader ohne Funktion

			for(y=(8*blocky); y<(8*(blocky+1)); y++)
				for(x=(8*blockx); x<(8*(blockx+1)); x++)
				{	
					if(x >= XBORDER || y >= YBORDER)	// Abfrage über Grösse von mapz.bmp
						check=false;
					else check=true;
						
					fread( &id, 2, 1, map1 );
					fwrite( &id, 2, 1, map2 );

					fread( &z, 1, 1, map1 );
					if(check && ((diff[x][y]=(mapz[x][y]-z))!=0))		// Eintrag der Diff-Werte in den Array
						{
							changes++;
							z+=diff[x][y];				
						}
					else diff[x][y]=0;
					fwrite( &z, 1, 1, map2 );
				}
		}
	printf("Mapzchanges: %g",changes);

	fclose(map1);
	fclose(map2);
}



//--------------------------------------------------
//
//--------------------------------------------------
int main()
{ 
	SetConsoleTitle("Hoehenmodulator - Request: Varan.dar@gmx.de");

	printf("SYS|Load config data...\n");
	getConfig();
	printf("SYS|Cache mapz.bmp...\n");
	CacheBMP();
	archivMUL();
	printf("Files saved as\n\t%s\n\t%s",orgstat,orgmap);
	processMUL();
	printf("---------------------------------------\n");
	processStatics();
	delay(5);

return 0;
}

