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



block tiles[22];
block virtscreen;
color virtpalette[256];
block emsprite;
color tilepalette[256];
color normpalette[256];
wgtfont smallfont;

int map[81][76];

int max_room;
int clevel;
int level;
int dt;
int room;
char mapname[20]={"dunmap\\DMAP"};
char tempf[20];



void readdata()
{
FILE *fpmonst;
int z,x,y,d;


	fpmonst=fopen("temp\\mapcvt.out","rb");
	for(y=0;y<=75;y++){
		for(x=0;x<=80;x++){
		 fread(&z,sizeof(int),1,fpmonst);
		 map[x][y]=z;
		}
	}
	fclose(fpmonst);

		fpmonst=fopen("temp\\dundata.tmp","r");
			fscanf(fpmonst,"%d",&clevel);   /*character level*/
			fscanf(fpmonst,"%d",&d);        /*number of rooms*/
			fscanf(fpmonst,"%d",&level);    /*monster level*/
			fscanf(fpmonst,"%d",&dt);       /*dungeon type*/
		fclose(fpmonst);

}

void savedata()
{
FILE *fpmonst;
		fpmonst=fopen("temp\\dundata.tmp","w");
			fprintf(fpmonst,"%d\n",clevel);   /*character level*/
			fprintf(fpmonst,"%d\n",max_room);        /*number of rooms*/
			fprintf(fpmonst,"%d\n",level);    /*monster level*/
			fprintf(fpmonst,"%d\n",dt);       /*dungeon type*/
		fclose(fpmonst);

}

/*************NUMBER THE ROOMS************/
void numroom(int ROOMNUMBER, int xz, int yz)
{
 char p[4];
  wtextcolor(0);


		itoa(ROOMNUMBER,p,10);
		if(ROOMNUMBER<10) wouttextxy(xz,yz,smallfont,p);
		if((xz-4)<0) xz=xz+4;
		if(ROOMNUMBER>9) wouttextxy(xz-4,yz,smallfont,p);

}


void print_map()
{

int x,y,tile,xx,yy;

strcpy(tempf,mapname);
wcls(0);

wsetpalette(0,255,virtpalette);
wputblock(0,0,virtscreen,0);
wtextcolor(15);
wouttextxy(105,100,NULL,"Saving Maps...");
wtextcolor(0);
wsetscreen(virtscreen);
wcls(15);

wsetpalette(0,255,tilepalette);

	xx=0;
	yy=0;
	for(y=1;y<=24;y++){
	  for(x=1;x<=40;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"a.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);
xx=0;
yy=0;
wcls(15);

	for(y=1;y<=24;y++){
	  for(x=41;x<=80;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"b.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);
xx=0;
yy=0;
wcls(15);

	for(y=25;y<=48;y++){
	  for(x=1;x<=40;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"c.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);
xx=0;
yy=0;
wcls(15);

	for(y=25;y<=48;y++){
	  for(x=41;x<=80;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"d.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);
xx=0;
yy=0;
wcls(15);

	for(y=49;y<=72;y++){
	  for(x=1;x<=40;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"e.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);
xx=0;
yy=0;
wcls(15);

	for(y=49;y<=72;y++){
	  for(x=41;x<=80;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"f.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);

xx=0;
yy=0;
wcls(15);

	for(y=73;y<=75;y++){
	  for(x=1;x<=40;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"g.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);
xx=0;
yy=0;
wcls(15);

	for(y=73;y<=75;y++){
	  for(x=41;x<=80;x++){
			tile=map[x][y];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(xx,yy,emsprite,0);
			if(tile==19) {
					numroom(max_room,xx,yy);
					max_room++;
					}
			xx=xx+8;
	  }
	  xx=0;
	  yy=yy+8;
	}
		strcat(mapname,"h.pcx");
		wsavepcx256(mapname,virtscreen,tilepalette);
		strcpy(mapname,tempf);

wcls(0);
wnormscreen();
}



void main()
{



       vga256();
	wems_init();
	wems_open(16);
	wreadpalette(0,255,normpalette);


	setlib("graphics\\cutgen.lib");
	wsetpalette(0,255,normpalette);
	wtexttransparent(0);
	wtextcolor(255);
	wouttextxy(120,100,NULL,"Loading...");
	virtscreen=wloadpcx256("cutsav.pcx",virtpalette);
	smallfont=wloadfont ("tiny.fnt");
	wems_loadsprites(tilepalette,"cut.spr",tiles,0,20);



      max_room=1;
      readdata();
      wcls(0);
      print_map();
      savedata();

 wfreeblock(virtscreen);
 wfreefont(smallfont);
 wems_reset();
 wems_close();

 execv("monster",NULL);
}
