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


/*DS=SS and Auto Far Data in compile*/


color normpalette[256];
block monstersel;
block newmonsterscreen;
color newmonsterpalette[256];
color monsterselpalette[256];

char *legal_chars=" ()ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_.";
char *legal_num="0123456789";
char *legal_num2="0123456789-";
char *monsternameend;
char *monsteradend;
char *monsterappearend;
char *monsterarmorend;

struct monster {
	char name[82];
	int  armor;
	int  attackpoints;
	int  frequency;
	int  index;
	int  light;
	};
static  struct monster monsterdata[11][56];

int  moncount[11];


int level;
char ml[8];

static FILE *fp;


void monexit()
{ int old;
 if((fp=fopen("temp\\mode.out","r"))!=NULL){
	fscanf(fp,"%d",&old);
	fclose(fp);
	}
	wsetmode(old);
	printf("You are missing a file or files in the DATA directory.\n");
	printf("It is recommended that you install the program again\n");
	printf("to place the needed files in the appropriate directories.\n");
	printf("You may want to backup your TABLES files for the installation\n");
	printf("may replace any of your work.");
	exit(0);
}


/*******LOAD AND INITIALIZE MONSTER LIST***********/
void initmonsters(void)
{
static FILE *fpmonst;
char name[81];
char star[1]={"@"};
int z;
int x;
int  armor;
int  attackpoints;
int  frequency;
int numb,cnter;

moncount[0]=0;
moncount[1]=0;
moncount[2]=0;
moncount[3]=0;
moncount[4]=0;
moncount[5]=0;
moncount[6]=0;
moncount[7]=0;
moncount[8]=0;
moncount[9]=0;
moncount[10]=0;

  for(cnter=1;cnter<=10;cnter++){
       for(numb=1;numb<=55;numb++){
	 monsterdata[cnter][numb].light=0;
	 }
  }
      /********MONSTER LEVEL 1************/
	if((fpmonst=fopen("tables\\dungeon\\level1.tbl","r"))==NULL) monexit();

	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		moncount[1]++;
		if(moncount[1]==55) break;
		}
		fclose(fpmonst);

     /*********MONSER LEVEL 2************/
	if((fpmonst=fopen("tables\\dungeon\\level2.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[2]==55) break;
		moncount[2]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 3************/
	if((fpmonst=fopen("tables\\dungeon\\level3.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[3]==55) break;
		moncount[3]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 4************/
	if((fpmonst=fopen("tables\\dungeon\\level4.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[4]==55) break;
		moncount[4]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 5************/
	if((fpmonst=fopen("tables\\dungeon\\level5.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[5]==55) break;
		moncount[5]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 6************/
	if((fpmonst=fopen("tables\\dungeon\\level6.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[6]==55) break;
		moncount[6]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 7************/
	if((fpmonst=fopen("tables\\dungeon\\level7.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[7]==55) break;
		moncount[7]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 8************/
	if((fpmonst=fopen("tables\\dungeon\\level8.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[8]==55) break;
		moncount[8]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 9************/
	if((fpmonst=fopen("tables\\dungeon\\level9.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[9]==55) break;
		moncount[9]++;
		}
		fclose(fpmonst);


     /*********MONSER LEVEL 10************/
	if((fpmonst=fopen("tables\\dungeon\\level10.tbl","r"))==NULL) monexit();
	for(;;){
		fgets(name,80,fpmonst);
		if(name[0]==star[0]) break;
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		fgets(name,80,fpmonst);
		if(moncount[10]==55) break;
		moncount[10]++;
		}
		fclose(fpmonst);

     fpmonst=fopen("tables\\dungeon\\level1.tbl","r");
		numb=1;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);

	fpmonst=fopen("tables\\dungeon\\level2.tbl","r");
	numb=2;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);

	fpmonst=fopen("tables\\dungeon\\level3.tbl","r");
	numb=3;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);
	fpmonst=fopen("tables\\dungeon\\level4.tbl","r");
	numb=4;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);

	fpmonst=fopen("tables\\dungeon\\level5.tbl","r");
	numb=5;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);

	fpmonst=fopen("tables\\dungeon\\level6.tbl","r");
	numb=6;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);
	fpmonst=fopen("tables\\dungeon\\level7.tbl","r");
	numb=7;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);

	fpmonst=fopen("tables\\dungeon\\level8.tbl","r");
	numb=8;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);

	fpmonst=fopen("tables\\dungeon\\level9.tbl","r");
	numb=9;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);

	fpmonst=fopen("tables\\dungeon\\level10.tbl","r");
	numb=10;
     for(cnter=1;cnter<=moncount[numb];cnter++){
		   fgets( monsterdata[numb][cnter].name,80,fpmonst);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].armor=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].attackpoints=atoi(name);
		   fgets(name,80,fpmonst);
		   monsterdata[numb][cnter].frequency=atoi(name);
		   monsterdata[numb][cnter].index=cnter;
		   monsterdata[numb][cnter].light=1;
	}
	fclose(fpmonst);



}


void savedata(void)
{
int numb,cnter,t,i,j;
     /*count new total of monsters*/
     for(t=1;t<=10;t++){
	 i=0;
	 for(j=1;j<=55;j++){
	    if(monsterdata[t][j].light==1) i++;
	 }
	 moncount[t]=i;
     }


   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level1.tbl","w");
		numb=1;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);

   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level2.tbl","w");
		numb=2;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);

   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level3.tbl","w");
		numb=3;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);

   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level4.tbl","w");
		numb=4;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);

   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level5.tbl","w");
		numb=5;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);

   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level6.tbl","w");
		numb=6;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);

   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level7.tbl","w");
		numb=7;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);
   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level8.tbl","w");
		numb=8;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);
   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level9.tbl","w");
		numb=9;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);
   /*OPEN DATA FILES AND SAVE*/
   fp=fopen("tables\\dungeon\\level10.tbl","w");
		numb=10;  /*monster level number*/
     if(moncount[numb]==0){
		   fprintf(fp,"DEFAULT\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   fprintf(fp,"0\n");
		   }
    if(moncount[numb]>0){
     for(cnter=1;cnter<=moncount[numb];cnter++){
	  if(monsterdata[numb][cnter].light==1){
		   fprintf(fp,"%s",monsterdata[numb][cnter].name);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].armor);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].attackpoints);
		   fprintf(fp,"%d\n",monsterdata[numb][cnter].frequency);
		   }
	}
     } /*end if moncount>0*/
     fprintf(fp,"@\n");
    fclose(fp);
}


/********CHANGE FROM SELECTED TO NOT SELECTED VISE VERSA*********/
void changeindex(int mnstlevel,int monnumber,int monend)
{

	if(monnumber<=monend){
		if(monsterdata[mnstlevel][monnumber].light==0){
			monsterdata[mnstlevel][monnumber].light=1;

		}
		else
		{
			monsterdata[mnstlevel][monnumber].light=0;

		}
	}
}




/********SHOW LIST OF CURRENT SELECTED MONSTERS TO SCREEN********/
void printmonsterlist(int monsterlevel,int start, int ending)
{

int t,len,place;
char monstername[81];
char monstername2[82];
char numbering[4];

place=40;
   if(start<=ending){

      for(t=start;t<=ending;t++){

		  if(t>0){
			len=0;
			sprintf(monstername,"%s",monsterdata[monsterlevel][t].name);
			for(;;){
				if(monstername[len]==10) break;
				len++;
			}
			monstername[len]=NULL;
			sprintf(monstername2,"%s",monstername);
			wtextcolor(253);
			if(monsterdata[monsterlevel][t].light==1) wtextcolor(255);

			sprintf(numbering,"%d",monsterdata[monsterlevel][t].index);
			wouttextxy(20,place,NULL,numbering);
			wouttextxy(50,place,NULL,monstername2);
			place=place+10;

		  }
	}
   }

}

/**********ENTER NEW MONSTER************/
void newmonsterentry(int newlevel,int newtotal)
{
 int empty;
 int x,y;
 y=0;


	  strcpy(monsternameend," ");
	  strcpy(monsteradend," ");
	  strcpy(monsterappearend," ");
	  strcpy(monsterarmorend," ");




 for(x=1;x<=newtotal;x++){
	if(monsterdata[newlevel][x].light==0){y=x;x=newtotal+1;} /*Break?*/
	}
 if(y==0){
		if(newtotal<56){ y=newtotal+1;
		moncount[newlevel]=moncount[newlevel]+1;
		}
 }
wcls(0);
	wsetpalette(0,255,newmonsterpalette);
	wfade_out(1,255,0,newmonsterpalette);
	wputblock(0,0,newmonsterscreen,0);
	wfade_in(0,255,0,newmonsterpalette);

if(y==0){
	wtextcolor(255);
	wouttextxy(30,80,NULL,"Monster Database is Full");
	wouttextxy(90,170,NULL,"**Press Any Key**");
	getch();
	}

if(y>0){
  wtexttransparent(0);
  wtextcolor(27);
  wouttextxy(30,50,NULL,"Monster Name or Type:");

  wtextcolor(255);
  wtextbackground(0);
  wtexttransparent(TEXTFGBG);

  wstring(40,62,monsternameend,legal_chars,40);
  strcat(monsternameend,"\n");
  wtexttransparent(0);
  wtextcolor(27);
  wouttextxy(30,75,NULL,"Monster Attack Die:");

  wtextcolor(255);
  wtextbackground(0);
  wtexttransparent(TEXTFGBG);
  wstring(40,87,monsteradend,legal_num,2);

  wtexttransparent(0);
  wtextcolor(27);
  wouttextxy(30,100,NULL,"Monster Armor Points:");

  wtextcolor(255);
  wtextbackground(0);
  wtexttransparent(TEXTFGBG);
  wstring(40,112,monsterarmorend,legal_num2,3);

  wtexttransparent(0);
  wtextcolor(27);
  wouttextxy(30,125,NULL,"Maximum number Appearing:");

  wtextcolor(255);
  wtextbackground(0);
  wtexttransparent(TEXTFGBG);
  wstring(40,137,monsterappearend,legal_num,4);


  /*log in new data*/
   sprintf(monsterdata[newlevel][y].name,"%s",monsternameend);
   monsterdata[newlevel][y].armor=atoi(monsterarmorend);
   monsterdata[newlevel][y].attackpoints=atoi(monsteradend);
   monsterdata[newlevel][y].frequency=atoi(monsterappearend);
   monsterdata[newlevel][y].index=y;
   monsterdata[newlevel][y].light=1;
 } /*end if y>0 */


wcls(0);
}


/***************SELECT WHAT MONSTERS TO USE IN DUNGEON**************/
void monsterselect(void)
{
	int stopp;
	int t,len;
	int mstartindex;
	int mendindex;
	int monsterlevel;
	int bypass;

wcls(0);

	wsetpalette(0,255,monsterselpalette);
	wputblock(0,0,monstersel,0);

wtexttransparent(0);
wtextcolor(253);

	monsterlevel=1;
	bypass=0;

	mstartindex=1;
	mendindex=7;
	if((moncount[monsterlevel]-7)<0) mendindex=moncount[monsterlevel];

	printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);

			wouttextxy(286,165,NULL,ml);
 mon();
  for(;;){
  bypass=0;
	if(but==2) noclick();
	if(but==1){
		noclick();
		if((my>40)&&(my<=50)&&(bypass==0)){
			moff();
			wputblock(0,0,monstersel,0);
			changeindex(monsterlevel,mstartindex,mendindex);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
		}
		if((my>50)&&(my<=60)&&(mstartindex+1<=mendindex)&&(bypass==0)){
			moff();
			wputblock(0,0,monstersel,0);
			changeindex(monsterlevel,mstartindex+1,mendindex);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
		}
		if((my>60)&&(my<=70)&&(mstartindex+2<=mendindex)&&(bypass==0)){
			moff();
			wputblock(0,0,monstersel,0);
			changeindex(monsterlevel,mstartindex+2,mendindex);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
		}
		if((my>70)&&(my<=80)&&(mstartindex+3<=mendindex)&&(bypass==0)){
			moff();
			wputblock(0,0,monstersel,0);
			changeindex(monsterlevel,mstartindex+3,mendindex);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
		}
		if((my>80)&&(my<=90)&&(mstartindex+4<=mendindex)&&(bypass==0)){
			moff();
			wputblock(0,0,monstersel,0);
			changeindex(monsterlevel,mstartindex+4,mendindex);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
		}
		if((my>90)&&(my<=100)&&(mstartindex+5<=mendindex)&&(bypass==0)){
			moff();
			wputblock(0,0,monstersel,0);
			changeindex(monsterlevel,mstartindex+5,mendindex);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
		}
		if((my>100)&&(my<=110)&&(mstartindex+6<=mendindex)&&(bypass==0)){
			moff();
			wputblock(0,0,monstersel,0);
			changeindex(monsterlevel,mstartindex+6,mendindex);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
		}
		if((my>140)&&(my<156)&&(mx>274)&&(mx<305)&&(bypass==0)){
		     if(monsterlevel<level){
			monsterlevel++;
			if(monsterlevel>level) monsterlevel=level;
			mstartindex=1;
			mendindex=7;
			if((moncount[monsterlevel]-7)<0) mendindex=moncount[monsterlevel];
			moff();
			wputblock(0,0,monstersel,0);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
			}
		}
		if((my>=180)&&(my<=192)&&(mx>274)&&(mx<302)&&(bypass==0)){
		     if(monsterlevel>1){
			monsterlevel--;

			if(monsterlevel<=0) monsterlevel=1;
			mstartindex=1;
			mendindex=7;
			if((moncount[monsterlevel]-7)<0) mendindex=moncount[monsterlevel];
			moff();
			wputblock(0,0,monstersel,0);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
			}
		}
		if((my>176)&&(my<188)&&(mx>154)&&(mx<165)&&(bypass==0)){
		     if(mendindex<(moncount[monsterlevel])){
			mstartindex++;
			mendindex++;
			moff();
			wputblock(0,0,monstersel,0);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
			}
		}
		if((my>152)&&(my<163)&&(mx>154)&&(mx<165)&&(bypass==0)){
		     if(mstartindex>1){
			mstartindex--;
			mendindex--;
			moff();
			wputblock(0,0,monstersel,0);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
			}
		}
	if((mx>10)&&(mx<63)&&(my>136)&&(my<153)){
	    moff();
	    newmonsterentry(monsterlevel,moncount[monsterlevel]);
					/*add new monster*/
			wsetpalette(0,255,monsterselpalette);
			wputblock(0,0,monstersel,0);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);
			wtexttransparent(0);
			if(monsterlevel<10) wouttextxy(286,165,NULL,ml);
			if(monsterlevel>9)  wouttextxy(280,165,NULL,ml);
			bypass=1;
			mon();
	}
	if((mx>11)&&(mx<62)&&(my>162)&&(my<182))  stopp=1;
     }
   if(stopp==1) break;
   }

 moff();
}


void main()
{

	level=10;
	vga256();
	  monsternameend=(char *)malloc(40);    /*allocate for name*/
	  strcpy(monsternameend," ");
	  monsteradend=(char *)malloc(2);    /*allocate for attack die*/
	  strcpy(monsteradend," ");
	  monsterappearend=(char *)malloc(4);    /*allocate for frequency*/
	  strcpy(monsterappearend," ");
	  monsterarmorend=(char *)malloc(3);    /*allocate for armor*/
	  strcpy(monsterarmorend," ");

	initmonsters();

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

	monstersel=wloadpcx256("select.pcx",monsterselpalette);
	newmonsterscreen=wloadpcx256("new.pcx",newmonsterpalette);


	minit();
	monsterselect();
	wcls(0);
	savedata();

	mdeinit();
	wfreeblock(newmonsterscreen);
	wfreeblock(monstersel);
	free(monsternameend);
	free(monsteradend);
	free(monsterappearend);
	free(monsterarmorend);
     execv("utility",NULL);
}

