#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 backscreen;
block namingscreen;
color namingpalette[256];
color normpalette[256];
color backcolorpal[256];
char *citynameend;
char *legal_chars=" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_.";
static int citysize;  /*1=hamlet 2=village 3=town 4=sm city 5=med city 6=lg city*/
static int castle;
static int keap;
static int market;
static int walls;
static int terrain;
FILE *fpmonst;

/***********READ DATA***************/
void readdata(void)
{
  int x,y;

	fpmonst=fopen("temp\\city.tmp","r");

	fscanf(fpmonst,"%d",&citysize);
	fscanf(fpmonst,"%d",&terrain);
	fscanf(fpmonst,"%d",&castle);
	fscanf(fpmonst,"%d",&keap);
	fscanf(fpmonst,"%d",&market);
	fscanf(fpmonst,"%d",&walls);
	fclose(fpmonst);

}

/**********ENTER NAME AND SAVE DATA TO DATA SUBDIRECTORY************/
void namecity(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 city?");
wsetcolor(0);
wbar(40,60,260,70);
wtextcolor(255);
wtextbackground(0);
  wtexttransparent(TEXTFGBG);

wstring(40,62,citynameend,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){
	if(terrain==1) fpmonst=fopen("data\\forest.dta","a");
	if(terrain==2) fpmonst=fopen("data\\mountain.dta","a");
	if(terrain==3) fpmonst=fopen("data\\desert.dta","a");
	if(terrain==4) fpmonst=fopen("data\\plains.dta","a");
	if(terrain==5) fpmonst=fopen("data\\coastal.dta","a");
	if((terrain<=5)&&(terrain>0)){
		fputs(citynameend,fpmonst);
		fputs("\n",fpmonst);
		fprintf(fpmonst,"%d\n",citysize);
		fprintf(fpmonst,"%d\n",terrain);
		fprintf(fpmonst,"%d\n",castle);
		fprintf(fpmonst,"%d\n",keap);
		fclose(fpmonst);
	}
    }
       fpmonst=fopen("temp\\cn.tmp","w");
		fputs(citynameend,fpmonst);
		fputs("\n",fpmonst);
		fprintf(fpmonst,"%d\n",citysize);
		fprintf(fpmonst,"%d\n",terrain);
		fprintf(fpmonst,"%d\n",castle);
		fprintf(fpmonst,"%d\n",keap);
       fclose(fpmonst);

wcls(0);
}



void main()
{
int x,y;

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

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

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

	fpmonst=fopen("text\\city.txt","w");
	fclose(fpmonst);
	wcls(0);
	readdata();
	wcls(0);
	wtextcolor(255);
	wsetpalette(0,255,normpalette);
	wcls(0);
	namecity();

mdeinit();
wfreeblock(namingscreen);
wfreeblock(backscreen);

free(citynameend);
 execv("citytxt",NULL);

exit(0);
}

