#include <stdio.h>
#include <stdlib.h>
#include <dos.h>


void maketiles()
{
FILE *sv,*nwmap;
int nm,iy,ix,temp;
int tn;    /*type number*/
int hmap[81];
int vmap[51];
extern int MAP[81][51];
int top,bottom,left,right;
for(nm=1;nm<=4;nm++){

        if(nm==1) {
                sv=fopen("temp\\map1.dat","rb");       /*read map 1 data*/
                for(iy=1;iy<=50;iy++){
                  for(ix=1;ix<=80;ix++){
		    fread(&MAP[ix][iy],sizeof(int),1,sv);
                    }
                }
                fclose(sv);
                sv=fopen("temp\\map2l.dat","rb");
                for(iy=1;iy<=50;iy++){

                    fread(&temp,sizeof(int),1,sv);
                    vmap[iy]=temp;

                }
                fclose(sv);
                sv=fopen("temp\\map3t.dat","rb");

                  for(ix=1;ix<=80;ix++){
                    fread(&temp,sizeof(int),1,sv);
                    hmap[ix]=temp;
                    }

                fclose(sv);
        
        /*convert map 1 data to tile numbers*/
        nwmap=fopen("temp\\tmap1.dat","wb");
               
        for(iy=1;iy<=50;iy++){
          for(ix=1;ix<=80;ix++){
	     top=MAP[ix][iy-1];
	     bottom=MAP[ix][iy+1];
	     left=MAP[ix-1][iy];
	     right=MAP[ix+1][iy];
             
             /*if ix=80 right side of map change RIGHT*/
             if(ix==80) right=vmap[iy];
             
             /*if iy=50 bottom side of map change BOTTOM*/
             if(iy==50) bottom=hmap[ix];
             
	     tn=mktile(MAP[ix][iy],top,bottom,left,right);
             fwrite(&tn,sizeof(int),1,nwmap);          
          }
        }
        fclose(nwmap);
      } /* END FOR MAP 1*/
        
        if(nm==2) {
                sv=fopen("temp\\map2.dat","rb");       /*read map 2 data*/
                for(iy=1;iy<=50;iy++){
                  for(ix=1;ix<=80;ix++){
		    fread(&MAP[ix][iy],sizeof(int),1,sv);
                    }
                }
                fclose(sv);
                sv=fopen("temp\\map1r.dat","rb");
                for(iy=1;iy<=50;iy++){

                    fread(&temp,sizeof(int),1,sv);
                    vmap[iy]=temp;

                }
		fclose(sv);
                sv=fopen("temp\\map4t.dat","rb");

                  for(ix=1;ix<=80;ix++){
                    fread(&temp,sizeof(int),1,sv);
                    hmap[ix]=temp;
                    }

                fclose(sv);
        
        /*convert map 2 data to tile numbers*/
        nwmap=fopen("temp\\tmap2.dat","wb");
               
        for(iy=1;iy<=50;iy++){
          for(ix=1;ix<=80;ix++){
	     top=MAP[ix][iy-1];
	     bottom=MAP[ix][iy+1];
	     left=MAP[ix-1][iy];
	     right=MAP[ix+1][iy];
             
             /*if ix=1 left side of map change RIGHT*/
             if(ix==1) left=vmap[iy];
             
             /*if iy=50 bottom side of map change BOTTOM*/
             if(iy==50) bottom=hmap[ix];
             
	     tn=mktile(MAP[ix][iy],top,bottom,left,right);
             fwrite(&tn,sizeof(int),1,nwmap);          
          }
        }
        fclose(nwmap);
      } /* END FOR MAP 2*/

        if(nm==3) {
                sv=fopen("temp\\map3.dat","rb");       /*read map 3 data*/
                for(iy=1;iy<=50;iy++){
                  for(ix=1;ix<=80;ix++){
		    fread(&MAP[ix][iy],sizeof(int),1,sv);
                    }
                }
                fclose(sv);
                sv=fopen("temp\\map4l.dat","rb");
                for(iy=1;iy<=50;iy++){

                    fread(&temp,sizeof(int),1,sv);
                    vmap[iy]=temp;

                }
                fclose(sv);
                sv=fopen("temp\\map1b.dat","rb");

                  for(ix=1;ix<=80;ix++){
                    fread(&temp,sizeof(int),1,sv);
                    hmap[ix]=temp;
                    }

                fclose(sv);
        
        /*convert map 3 data to tile numbers*/
        nwmap=fopen("temp\\tmap3.dat","wb");
               
        for(iy=1;iy<=50;iy++){
          for(ix=1;ix<=80;ix++){
	     top=MAP[ix][iy-1];
	     bottom=MAP[ix][iy+1];
	     left=MAP[ix-1][iy];
	     right=MAP[ix+1][iy];
             
             /*if ix=80 right side of map change RIGHT*/
             if(ix==80) right=vmap[iy];
             
             /*if iy=1 bottom side of map change TOP*/
             if(iy==1) top=hmap[ix];
             
	     tn=mktile(MAP[ix][iy],top,bottom,left,right);
             fwrite(&tn,sizeof(int),1,nwmap);          
          }
        }
        fclose(nwmap);
      } /* END FOR MAP 3*/
        if(nm==4) {
                sv=fopen("temp\\map4.dat","rb");       /*read map 4 data*/
                for(iy=1;iy<=50;iy++){
                  for(ix=1;ix<=80;ix++){
		    fread(&MAP[ix][iy],sizeof(int),1,sv);
                    }
                }
                fclose(sv);
                sv=fopen("temp\\map3r.dat","rb");
                for(iy=1;iy<=50;iy++){

                    fread(&temp,sizeof(int),1,sv);
                    vmap[iy]=temp;

                }
                fclose(sv);
                sv=fopen("temp\\map2b.dat","rb");

                  for(ix=1;ix<=80;ix++){
                    fread(&temp,sizeof(int),1,sv);
                    hmap[ix]=temp;
                    }
               /* } */
                fclose(sv);
        
        /*convert map 4 data to tile numbers*/
        nwmap=fopen("temp\\tmap4.dat","wb");
               
        for(iy=1;iy<=50;iy++){
          for(ix=1;ix<=80;ix++){
	     top=MAP[ix][iy-1];
	     bottom=MAP[ix][iy+1];
	     left=MAP[ix-1][iy];
	     right=MAP[ix+1][iy];
             
             /*if ix=1 left side of map change RIGHT*/
	     if(ix==1) left=vmap[iy];
             
             /*if iy=1 bottom side of map change TOP*/
             if(iy==1) top=hmap[ix];
             
	     tn=mktile(MAP[ix][iy],top,bottom,left,right);
             fwrite(&tn,sizeof(int),1,nwmap);          
          }
        }
        fclose(nwmap);
      } /* END FOR MAP 4*/

  }/*END FOR 1-4 MAPS*/

return;
}
