#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wgt4.h>
/*#include <alloc.h>*/
/*DS=SS and Auto Far Data in compile*/
block tempscreen;
block backscreen;
color cutpal[256];
block namingscreen;
color namingpalette[256];
color normpalette[256];
color backcolorpal[256];
char *dngnnameend;
char *legal_chars=" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_.";
int level;
int clevel;
int dt;
static int map[81][76];
FILE *fpmonst;


void readdata()
{
int dum,z,x,y,dta;
		fpmonst=fopen("temp\\dundata.tmp","r");
			fscanf(fpmonst,"%d",&clevel);   /*character level*/
			fscanf(fpmonst,"%d",&dum);        /*number of rooms*/
			fscanf(fpmonst,"%d",&dta);    /*dungeon type*/
		fclose(fpmonst);

	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);

}
/*******DISPLAY MINI MAP OF CURRENT LEVEL YESNO************/
int minimap(void)
{
int x,y,kep;

x=0;
y=0;
kep=0;

wcls(0);
tempscreen=wloadpcx256("cutmini.pcx",cutpal);
wsetpalette(0,255,cutpal);
wputblock(0,0,tempscreen,1);
wfreeblock(tempscreen);
	wsetcolor(255);
wtexttransparent(0);
wtextcolor(27);

	for(y=0;y<=75;y++){
	   for(x=0;x<=80;x++){
		 if(map[x][y]==1) wsetcolor(0);
		 if(map[x][y]>1) wsetcolor(50);
		 if(map[x][y]==14) wsetcolor(70);
		 if(map[x][y]==15) wsetcolor(70);
		 if(map[x][y]==16) wsetcolor(85);
		 if(map[x][y]==17) wsetcolor(85);
		 if(map[x][y]==18) wsetcolor(40);
		 if(map[x][y]==20) wsetcolor(0);
		 if(map[x][y]==19) wsetcolor(0);

		 wputpixel(120+x,40+y);
	   }
	}

	wsetcolor(255);
	wbar(70,191,100,199);
	wbar(230,191,260,199);
	wouttextxy(73,192,NULL,"YES");
	wouttextxy(238,192,NULL,"NO");
	wouttextxy(140,192,NULL,"KEEP?");
	wtextcolor(255);

	mon();

	 for(;;){
		if(but==1){
			noclick();
			if((my>190)&&(mx>70)&&(mx<100)){
				kep=1;
				tempscreen=wnewblock(88,0,242,189);
				wsavepcx256("dunmap\\minimap.pcx",tempscreen,cutpal);
				wfreeblock(tempscreen);
				break;
				}
			if((my>190)&&(mx>230)&&(mx<260)){
				 kep=0;
				 break;
				 }
		}

	 }
moff();
return kep;
}

/**********ENTER NAME AND SAVE DATA TO DATA SUBDIRECTORY************/
void namedungeon(void)
{
int qt,kep;
qt=0;
kep=0;
wcls(0);
	wsetpalette(0,255,namingpalette);
        wfade_out(1,255,0,namingpalette);
	wputblock(0,0,namingscreen,0);
	wfade_in(0,255,0,namingpalette);

wtexttransparent(0);
wtextcolor(27);
wouttextxy(30,50,NULL,"What is the name of this dungeon?");
wsetcolor(0);
wbar(40,60,260,70);
wtextcolor(255);
wtextbackground(0);
  wtexttransparent(TEXTFGBG);

wstring(40,62,dngnnameend,legal_chars,40);
wcls(0);
wsetpalette(0,255,backcolorpal);
wputblock(0,0,backscreen,0);

	mon();

	 while(qt==0){
		if(but==1){
			noclick();
			if((mx>76)&&(my>160)&&(mx<152)&&(my<179)){
				kep=1;
				qt=1;
				}
			if((mx>155)&&(my>160)&&(mx<231)&&(my<179)){
				 kep=0;
				 qt=1;
				 }
		}

	 }
moff();
/*SAVE DATA TO DISK*/
  if(kep==1){
	fpmonst=fopen("data\\dungeon.dta","a");
	fputs(dngnnameend,fpmonst);
	fputs("\n",fpmonst);
	fprintf(fpmonst,"%d\n",1); /*number of levels*/
	fprintf(fpmonst,"%d\n",clevel);
	fclose(fpmonst);
  }


wcls(0);
}



void main()
{
int REDO;
int keepthis,x,y;

keepthis=0;
REDO=0;
	vga256();
	wreadpalette(0,255,normpalette);
	minit();
        wcls(0);
          dngnnameend=(char *)malloc(40);    /*allocate for name of dungeon*/
          strcpy(dngnnameend," ");

        wsetpalette(0,255,normpalette);
        wtexttransparent(0);
	wtextcolor(255);
	wouttextxy(120,100,NULL,"Loading...");
	setlib("graphics\\cutgen.lib");

	namingscreen=wloadpcx256("naming.3",namingpalette);
	backscreen=wloadpcx256("dunrm1.pcx",backcolorpal);

	fpmonst=fopen("text\\dungeon.txt","w");
        fclose(fpmonst);
        wcls(0);

				 /*clear map for init*/
		    for(y=0;y<=75;y++){
			for(x=0;x<=80;x++){
				map[x][y]=1;
			}
		    }
	readdata();
	wcls(0);

	wtextcolor(255);

                /* wsetmode(newmode); for debugging*/
		keepthis=minimap();
		if(keepthis==0) REDO=1;

		wsetpalette(0,255,normpalette);
		wcls(0);
		if(REDO==0) namedungeon();

mdeinit();
wfreeblock(namingscreen);
wfreeblock(backscreen);
free(dngnnameend);
	if(REDO==0) execv("cutmapsv",NULL);
	if(REDO==1) execv("cut",NULL);
exit(0);
}

