void exit_system() {sys_exit(NULL);}

void dat_read()
{
	var mix;
	for(mix = 0; mix < hData.object_block_count; mix++) 
	{
		you = map_objects[mix];
		ent_remove(you);
	}
	
    
	var mapdata_count = 1;
	var block_size = 1;
	
	datfile = fopen(filepfad,"r+b");
	
	if(datfile == NULL){error("Fehler DAT Read");}
	fread(hData, sizeof(DAT_STRUC_HEADER), 1, datfile);
	
	hmData = (DAT_STRUC_HIGHMAP*)sys_malloc(hData.hmap_block_count * sizeof(DAT_STRUC_HIGHMAP)); 
	waData = (DAT_STRUC_WATERMAP*)sys_malloc(hData.wmap_block_count * sizeof(DAT_STRUC_WATERMAP)); 
	//objData = (DAT_STRUC_MAPOBJECTS*)sys_malloc(hData.object_block_count * sizeof(DAT_STRUC_MAPOBJECTS)); 
	
	fread(hmData, sizeof(DAT_STRUC_HIGHMAP),  hData.hmap_block_count, datfile);
	fread(waData, sizeof(DAT_STRUC_WATERMAP), hData.wmap_block_count, datfile);
	
	int reslut = fread(objData, sizeof(DAT_STRUC_MAPOBJECTS), hData.object_block_count, datfile);
	if (reslut != hData.object_block_count) {error("Reading error"); sys_exit (NULL);printf("Block Count = %i reslut = %i",hData.object_block_count,reslut);}
	fread(effData_1, sizeof(DAT_STRUC_EFFECT_1), 2, datfile);
	fclose(datfile);
	bDest = bmap_createblack(2048,2048,24);
	
	//map id Enblenden
	
	smadata = fopen(_chr(file_sma),"r+b");
	if(smadata != NULL)
	{
		fread(hsmadata, sizeof(SMA_STRUC_HEADER), 1, smadata);
		fread(sma_data2, sizeof(SMA_STRUC_DATEN),  65536, smadata);
		fread(smamb, sizeof(SMA_MONSTER_BLOCK_COUNT),  1, smadata);
		fread(monster_data, sizeof(SMA_MONSTER_DATEN),  smamb.monster_block_count, smadata);
		fclose(smadata);
	}

	mapdata = fopen(_chr(file_map),"r+b");
	if(mapdata != NULL)
	{
		fread(hMap, sizeof(MAP_TOTAL_HEADER), 1, mapdata);
		while(hMap.DataNumber >= mapdata_count)
		{
			fread(hMapData, sizeof(MAP_DATA_HEADER), 1, mapdata);
			fread(mDataBlock, sizeof(MAP_DATA_BLOCK), hMapData.FileSize, mapdata);
			temp_map_name = str_create(_chr(hMapData.filename));
			str_cat(temp_map_name, ".jpg");
			tempfile = fopen(_chr(temp_map_name),"w+b");
			fwrite(mDataBlock, sizeof(MAP_DATA_BLOCK), hMapData.FileSize,tempfile);
			fclose(tempfile);
			maptexturparts[mapdata_count] = temp_map_name;
			mapdata_count+=1;
		}
		
		BMAP* bSource1 = bmap_create(maptexturparts[1]); 
		BMAP* bSource2 = bmap_create(maptexturparts[2]); 
		BMAP* bSource3 = bmap_create(maptexturparts[3]); 
		BMAP* bSource4 = bmap_create(maptexturparts[4]); 
		BMAP* bSource5 = bmap_create(maptexturparts[5]); 
		BMAP* bSource6 = bmap_create(maptexturparts[6]); 
		BMAP* bSource7 = bmap_create(maptexturparts[7]); 
		BMAP* bSource8 = bmap_create(maptexturparts[8]); 
		BMAP* bSource9 = bmap_create(maptexturparts[9]); 
		BMAP* bSource10 = bmap_create(maptexturparts[10]); 
		BMAP* bSource11 = bmap_create(maptexturparts[11]); 
		BMAP* bSource12 = bmap_create(maptexturparts[12]); 
		BMAP* bSource13 = bmap_create(maptexturparts[13]); 
		BMAP* bSource14 = bmap_create(maptexturparts[14]); 
		BMAP* bSource15 = bmap_create(maptexturparts[15]); 
		BMAP* bSource16 = bmap_create(maptexturparts[16]); 
		
		file_delete(maptexturparts[1]);
		file_delete(maptexturparts[2]);
		file_delete(maptexturparts[3]);
		file_delete(maptexturparts[4]);		
		file_delete(maptexturparts[5]);
		file_delete(maptexturparts[6]);
		file_delete(maptexturparts[7]);
		file_delete(maptexturparts[8]);
		file_delete(maptexturparts[9]);
		file_delete(maptexturparts[10]);
		file_delete(maptexturparts[11]);
		file_delete(maptexturparts[12]);
		file_delete(maptexturparts[13]);
		file_delete(maptexturparts[14]);
		file_delete(maptexturparts[15]);
		file_delete(maptexturparts[16]);
		
		bmap_blit(bDest,bSource1, vector(0,0,0), NULL);
		bmap_blit(bDest,bSource2, vector(512,0,0), NULL);
		bmap_blit(bDest,bSource3, vector(1024,0,0), NULL);
		bmap_blit(bDest,bSource4, vector(1536,0,0), NULL);
		
		bmap_blit(bDest,bSource5, vector(0,512,0), NULL);
		bmap_blit(bDest,bSource6, vector(512,512,0), NULL);
		bmap_blit(bDest,bSource7, vector(1024,512,0), NULL);
		bmap_blit(bDest,bSource8, vector(1536,512,0), NULL);
		
		bmap_blit(bDest,bSource9, vector(0,1024,0), NULL);
		bmap_blit(bDest,bSource10, vector(512,1024,0), NULL);
		bmap_blit(bDest,bSource11, vector(1024,1024,0), NULL);
		bmap_blit(bDest,bSource12, vector(1536,1024,0), NULL);
		
		bmap_blit(bDest,bSource13, vector(0,1536,0), NULL);
		bmap_blit(bDest,bSource14, vector(512,1536,0), NULL);
		bmap_blit(bDest,bSource15, vector(1024,1536,0), NULL);
		bmap_blit(bDest,bSource16, vector(1536,1536,0), NULL);
		Bmap_fileName = str_create(hData.FileName);
		str_cat(Bmap_fileName,".tga");
		
		
		bmap_save(bDest,Bmap_fileName );
	}
	terrain_create();
	create_obj();
}


