#include	<stdio.h>
#include	<strings.h>
#include	<math.h>

FILE	*fpr0,*fpr1,*fpr2,*fpr3,*fpw,*fpw2;

main()
{
	static char	filename[] = {"Mapdat-x4.DAT"};
	static char	filename0[] = {"../char/Map-Big-0.SCR"};
	static char	filename1[] = {"../char/Map-Big-1.SCR"};
	static char	filename2[] = {"../char/Map-Big-2.SCR"};
	static char	filename3[] = {"../char/Map-Big-3.SCR"};
	static char	cpcom1[] = {"mv "};
	static char	cpcom2[] = {" ../char/"};
	char	c[125];

	printf("\n\n\n<<<<< map0 data cnv START !! >>>>>\t\n\n\n");

	if(( fpr0 = fopen(filename0,"rb")) == NULL){
		fprintf(stderr,"read file open error !!\n");
		exit(1);
	}
	if(( fpr1 = fopen(filename1,"rb")) == NULL){
		fprintf(stderr,"read file open error !!\n");
		exit(1);
	}
	if(( fpr2 = fopen(filename2,"rb")) == NULL){
		fprintf(stderr,"read file open error !!\n");
		exit(1);
	}
	if(( fpr3 = fopen(filename3,"rb")) == NULL){
		fprintf(stderr,"read file open error !!\n");
		exit(1);
	}
	if(( fpw = fopen(filename,"wb")) == NULL){
		fprintf(stderr,"Write file open error !!\n");
		exit(1);
	}

	data_cnv();

	fclose(fpr0);
	fclose(fpr1);
	fclose(fpr2);
	fclose(fpr3);
	fclose(fpw);

	printf("\n\n<<<<< map0 data cnv END !! >>>>>\n\n\n");
/**************************/
	strcpy(c,cpcom1);
	strcat(c,filename);
	strcat(c, cpcom2);
	strcat(c,filename);
	printf("\n%s\n",c);
	system(c);
/**************************/
}

data_cnv()
{
	unsigned short	s0_0[32],s0_1[32],
			s1_0[32],s1_1[32];
	int	h,i,j;
	long	offset;

	offset=32*2;
	h=0;
	while(h<2){
		i=0;
		while(i<32){
			fread(s0_0,sizeof(short),32,fpr0);
			fseek(fpr0,0x800-64,1);
			fread(s0_1,sizeof(short),32,fpr0);

			fseek(fpr0,offset,0);

			fread(s1_0,sizeof(short),32,fpr1);
			fseek(fpr1,0x800-64,1);
			fread(s1_1,sizeof(short),32,fpr1);

			fseek(fpr1,offset,0);

			for(j=0;j<32;j++){
				s0_0[j]&=0xffef;
				s0_1[j]&=0xffef;

				s1_0[j]&=0xffef;
				s1_1[j]&=0xffef;
			}

			fwrite(s0_0,sizeof(short),32,fpw);
			fwrite(s0_1,sizeof(short),32,fpw);

			fwrite(s1_0,sizeof(short),32,fpw);
			fwrite(s1_1,sizeof(short),32,fpw);

			i++;
			offset=offset+(32*2);
		}
/*
		offset=offset+((32*2)*32);
*/
		offset=0x1000+32*2;
		fseek(fpr0,0x1000,0);
		fseek(fpr1,0x1000,0);
		h++;
	}

	offset=32*2;
	h=0;
	while(h<2){
		i=0;
		while(i<32){
			fread(s0_0,sizeof(short),32,fpr2);
			fseek(fpr2,0x800-64,1);
			fread(s0_1,sizeof(short),32,fpr2);

			fseek(fpr2,offset,0);

			fread(s1_0,sizeof(short),32,fpr3);
			fseek(fpr3,0x800-64,1);
			fread(s1_1,sizeof(short),32,fpr3);

			fseek(fpr3,offset,0);

			for(j=0;j<32;j++){
				s0_0[j]&=0xffef;
				s0_1[j]&=0xffef;

				s1_0[j]&=0xffef;
				s1_1[j]&=0xffef;
			}

			fwrite(s0_0,sizeof(short),32,fpw);
			fwrite(s0_1,sizeof(short),32,fpw);

			fwrite(s1_0,sizeof(short),32,fpw);
			fwrite(s1_1,sizeof(short),32,fpw);

			i++;
			offset=offset+(32*2);
		}
/*
		offset=offset+((32*2)*32);
*/
		offset=0x1000+32*2;
		fseek(fpr2,0x1000,0);
		fseek(fpr3,0x1000,0);
		h++;
	}
}

