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


block dummy2;
block virtscreen;
color tilepalette[256];
color normpalette[256];
color mappalette2[256];
block tiles[113];
block emsprite;
static char mapname[20]={"dunmap\\DMAP"};
int  MAP[66][23];
static int max_room;
static int clevel;
static int level;
static int dt;
static int room;
static char tempf[20];


FILE *fpmonst;


void readdata()
{
int x,y,z;

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

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

void savedata()
{
		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 TILE, int xz, int yz)
{
  char p[4];
  wtextcolor(0);

  if(max_room>9) xz=xz-2;
  if(max_room>99) xz=xz-5;
  if(TILE==5){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==6){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==7){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==8){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==9){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==10){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==11){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==12){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==13){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==14){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==15){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==16){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==17){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
  if(TILE==36){
		sprintf(p,"%d",max_room);
		wouttextxy((xz+10),(yz+12),NULL,p);
		max_room++;
		}
}

/*********SAVE MAPS TO PCX FILES*****************/
void print_map(void)
{
int mpx1,mpy1,xc,yc;
int tile;

mpx1=0;
mpy1=0;
xc=0;
yc=0;
tile=0;

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


wsetpalette(0,255,tilepalette);

wputblock(0,0,dummy2,0);

mpx1=1;
mpy1=1;

		for(yc=1;yc<=6;yc++){
			for(xc=1;xc<=10;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}

		strcat(mapname,"a.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);



mpx1=1;
mpy1=1;



wputblock(0,0,dummy2,0);


		strcpy(mapname,tempf);
		for(yc=1;yc<=6;yc++){
			for(xc=11;xc<=18;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}

		strcat(mapname,"b.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);





mpx1=1;
mpy1=1;



wputblock(0,0,dummy2,0);


		strcpy(mapname,tempf);
		for(yc=7;yc<=12;yc++){
			for(xc=1;xc<=10;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}


		strcat(mapname,"c.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);



mpx1=1;
mpy1=1;



wputblock(0,0,dummy2,0);


		strcpy(mapname,tempf);

		for(yc=7;yc<=12;yc++){
			for(xc=11;xc<=18;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}


		strcat(mapname,"d.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);


mpx1=1;
mpy1=1;


wputblock(0,0,dummy2,0);


		strcpy(mapname,tempf);

		for(yc=13;yc<=18;yc++){
			for(xc=1;xc<=10;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}


		strcat(mapname,"e.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);


mpx1=1;
mpy1=1;



wputblock(0,0,dummy2,0);


		strcpy(mapname,tempf);
		for(yc=13;yc<=18;yc++){
			for(xc=11;xc<=18;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}


		strcat(mapname,"f.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);


mpx1=1;
mpy1=1;

wputblock(0,0,dummy2,0);


		strcpy(mapname,tempf);

		for(yc=19;yc<=24;yc++){
			for(xc=1;xc<=10;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}

		strcat(mapname,"g.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);


mpx1=1;
mpy1=1;



wputblock(0,0,dummy2,0);


		strcpy(mapname,tempf);

		for(yc=19;yc<=24;yc++){
			for(xc=11;xc<=18;xc++){
			tile=MAP[yc][xc];
			emsprite=wems_getsprite(tile,tiles);
			wputblock(mpx1,mpy1,emsprite,1);
			numroom(tile,mpx1,mpy1);
			mpx1=mpx1+32;
			}
		mpx1=1;
		mpy1=mpy1+32;
		}

		strcat(mapname,"h.pcx");

		wsavepcx256(mapname,virtscreen,tilepalette);


strcpy(mapname,tempf);
wnormscreen();

wcls(0);
}



void main()
{
int useEMS;


       vga256();
	useEMS=wems_present();
	if(!useEMS) {
		printf("\nNo EMS memory available.  Check your memory manager\n");
		exit(0);
		}
	wcls(0);

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


	setlib("graphics\\natgen.lib");
	wsetpalette(0,255,normpalette);
	wtexttransparent(0);
	wtextcolor(255);
	wouttextxy(120,100,NULL,"Loading...");
	dummy2=wloadpcx256("back.3",mappalette2);
	wems_loadsprites(tilepalette,"tcave.3",tiles,0,112);
	wremap(mappalette2,dummy2,tilepalette);


	wcls(0);
	virtscreen=wloadpcx256("natsav.pcx",mappalette2);
      max_room=1;
      readdata();
      wcls(0);
      print_map();
      savedata();

 wfreeblock(dummy2);
 wfreeblock(virtscreen);

  wems_close();

 execv("monster",NULL);
 exit(0);
}
