#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <string.h>
#include <wgt4.h>
/*#include <alloc.h> */
/*during compile time us DS=SS and AUTO FAR DATA and no debug in OBJ*/
/*extern unsigned _stklen = 24000U;*/



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

color attackdiepalette[256];
color monsterselpalette[256];
color savingpalette[256];
block savingscreen;
block monstersel;
block attackdiescreen;
wgtfont font;

char flname[20]={"text\\dungeon.txt"};
int clevel,duntype;
int room;
int level;
int  moncount[11];
int attackdietype;
int totalmonsters;
char ml[8];

static FILE *rp;



void monexit()
{ int old;
 if((rp=fopen("temp\\mode.out","r"))!=NULL){
	fscanf(rp,"%d",&old);
	fclose(rp);
	}
	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);
}

void getsavdata()
{
static FILE *fp;
   fp=fopen("temp\\dundata.tmp","r");
       fscanf(fp,"%d",&clevel);
       fscanf(fp,"%d",&room);
       fscanf(fp,"%d",&level);
       fscanf(fp,"%d",&duntype);
   fclose(fp);
}


/*********SET MONSTER LEVEL BASED ON CHARACTER LEVEL************/
void mlevel(int l)
{
static FILE *wp;
	int r,s,freq,nm;
	int ar,ap,ad,num,qqt;
	/*char *f[20];*/
	char dum[81];
	char item[81];

       r=0;
       s=0;
       freq=0;
       qqt=0;


       ap=0;   /*attack points*/
       ad=0;   /*attack die*/
       ar=0;   /*armor class*/
       num=0;   /*number appearing*/
   for(;;){
       r=(random(l)+1);
       s=(random(moncount[r])+1);
       if(monsterdata[r][s].light==1) qqt=1;
    if(qqt==1) break;
    }
		ar=monsterdata[r][s].armor;
		ad=monsterdata[r][s].attackpoints;
		freq=monsterdata[r][s].frequency;

    wp=fopen(flname,"a");
    fputs("\n\n\n",wp);
    fclose(wp);
	num=(random(freq)+1);
	 wp=fopen(flname,"a");
	 fputs("Number of monsters in this room: ",wp);
	 sprintf(ml,"%d",num);
	 fputs(ml,wp);
	 fputs("\n",wp);
	 fclose(wp);

	wp=fopen(flname,"a");
	for(nm=1;nm<=num;nm++){
		ap=(random((attackdietype*ad))+1);

		 fputs("MONSTER:  ",wp);
		 fputs(monsterdata[r][s].name,wp);
		 fputs(" ",wp);
		 fputs("         AR: ",wp);
		sprintf(ml,"%d",ar);

		 fputs(ml,wp);
		 fputs(" AD: ",wp);
		 sprintf(ml,"%d",ad);

		 fputs(ml,wp);
		 fputs(" AP: ",wp);
		 sprintf(ml,"%d",ap);

		 fputs(ml,wp);
		 fputs("\n",wp);
	}
	fclose(wp);

}


/******CREATE RANDOM MONSTER********/
void monster(void)
{
  int l;
  l=0;
  l=(random(level)+1);

  mlevel(l);

}


/******CREATE ROAMING MONSTER LIST********/
void roaming(void)
{
 static FILE *wp;

  int r;
  int t,ar,hd,num;
  int s,qqt;
  r=0;
  t=0;
  s=0;
  ar=0;
  hd=0;
  qqt=0;

		wp=fopen(flname,"a");
		 fputs("ROAMING MONSTER TABLE:\n",wp);
		fclose(wp);

  for(t=1;t<=6;t++){
    num=0;
   for(;;){
    r=(random(level)+1);
    s=(random(moncount[r])+1);



		if(monsterdata[r][s].light==1) qqt=1;

    if(qqt==1) break;
    }
    qqt=0;
    wp=fopen(flname,"a");
    sprintf(ml,"%d",t);

    fputs(ml,wp);
    fputs(".   ",wp);
    fputs(monsterdata[r][s].name,wp);
    fputs("     AR:",wp);
    ar=monsterdata[r][s].armor;
    sprintf(ml,"%d",ar);

     fputs(ml,wp);
     fputs(" ",wp);
     fputs("  AD:",wp);
     hd=monsterdata[r][s].attackpoints;
     sprintf(ml,"%d",hd);

     fputs(ml,wp);
     num=monsterdata[r][s].frequency;
     if(num==1){
	fputs("  Number of monsters encountered:",wp);
	}
     if(num>1){
	fputs("  Number of monsters encountered:1-",wp);
	}
     sprintf(ml,"%d",num);

    fputs(ml,wp);
     fputs("\n",wp);
     fclose(wp);
  }
    wp=fopen(flname,"a");
    fputs("\n\n\n",wp);
    fclose(wp);

}


/*****GENERATE MAGIC ITEM LIST*******/
void table(int t1, int t2, int t3, int t4)
{
static FILE *fp;
	int r,s,count;
	char item[81];
	char aster[1]="@";
	r=0;
	s=0;
	count=0;

	if(t1>0) {
			r=0;
			fp=fopen("tables\\dungeon\\magic1.tbl","r");
			for(;;){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(item[0]==aster[0]) break;
				if(feof(fp)) break;
				r++;
			}

			fclose(fp);

			for(count=1;count<=t1;count++){
			  fp=fopen("tables\\dungeon\\magic1.tbl","r");
			  s=(random(r)+1);
			  for(r=1;r<=s;r++){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(feof(fp)) break;
				}
			   fclose(fp);
			   fp=fopen(flname,"a");
			   fputs("     ",fp);
			   fputs(item,fp);
			   fclose(fp);
			}
		 }
	if(t2>0) {
			r=0;
			fp=fopen("tables\\dungeon\\magic2.tbl","r");
			for(;;){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(item[0]==aster[0]) break;
				if(feof(fp)) break;
				r++;
			}

			fclose(fp);

			for(count=1;count<=t2;count++){
			  fp=fopen("tables\\dungeon\\magic2.tbl","r");
			  s=(random(r)+1);
			  for(r=1;r<=s;r++){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(feof(fp)) break;
				}
			   fclose(fp);

			   fp=fopen(flname,"a");
			   fputs("     ",fp);
			   fputs(item,fp);
			   fclose(fp);
			}
		 }

	if(t3>0) {
			r=0;
			fp=fopen("tables\\dungeon\\magic3.tbl","r");
			for(;;){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(item[0]==aster[0]) break;
				if(feof(fp)) break;
				r++;
			}

			fclose(fp);

			for(count=1;count<=t3;count++){
			  fp=fopen("tables\\dungeon\\magic3.tbl","r");
			  s=(random(r)+1);
			  for(r=1;r<=s;r++){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(feof(fp)) break;
				}
			   fclose(fp);
			   fp=fopen(flname,"a");
			   fputs("     ",fp);
			   fputs(item,fp);
			   fclose(fp);
			}
		 }
	if(t4>0) {
			r=0;
			fp=fopen("tables\\dungeon\\magic4.tbl","r");
			for(;;){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(item[0]==aster[0]) break;
				if(feof(fp)) break;
				r++;
			}

			fclose(fp);
			for(count=1;count<=t4;count++){
			  fp=fopen("tables\\dungeon\\magic4.tbl","r");
			  s=(random(r)+1);
			  for(r=1;r<=s;r++){
				if(feof(fp)) break;
				fgets(item,80,fp);
				if(feof(fp)) break;
				}
			   fclose(fp);
			   fp=fopen(flname,"a");
			   fputs("     ",fp);
			   fputs(item,fp);
			   fclose(fp);
			}
		 }

}


/*********SET TABLE FOR MAGIC ITEM GENERATION******/
void magic(void)
{
 int tb1,tb2,tb3,tb4;

 tb1=0;
 tb2=0;
 tb3=0;
 tb4=0;

 if(level==1) tb1=1;
 if(level==2) tb1=2;
 if(level==3) {tb1=2;tb2=1;}
 if(level==4) {tb1=2;tb2=2;}
 if(level==5) {tb1=3;tb2=2;tb3=1;}
 if(level==6) {tb1=3;tb2=3;tb3=1;}
 if(level==7) {tb1=3;tb2=2;tb3=1;tb4=1;}
 if(level==8) {tb1=3;tb2=2;tb3=2;tb4=1;}
 if(level==9) {tb1=3;tb2=3;tb3=2;tb4=2;}
 if(level==10){tb1=4;tb2=3;tb3=3;tb4=2;}

 table(tb1,tb2,tb3,tb4);

}


/*********RANDOMLY SELECT TREASURE CONTAINER**********/
void container(void)
{

static FILE *wp;
static FILE *fp;
int o,r,s,count;
char ch[1]={"*"};
char st[81];

o=0;
r=0;
s=0;
count=0;

	 wp=fopen(flname,"a");
	 fputs("These items are contained in ",wp);
	 fclose(wp);

	fp=fopen("tables\\dungeon\\containr.tbl","r");
		for(;;){
		if(feof(fp)) break;
		fgets(st,80,fp);
		if(st[0]=='@') break;
		if(feof(fp)) break;
		if(st[0]==ch[0]) r++;
		}
	fclose(fp);
	r=r-1;
	s=(random(r)+1);
	fp=fopen("tables\\dungeon\\containr.tbl","r");
	count=0;
			for(;;){
				if(feof(fp)) break;
				fgets(st,80,fp);
				if(st[0]=='@') break;
				if(feof(fp)) break;
				if(st[0]==ch[0]) count++;
				if(count==s) break;
				}

			wp=fopen(flname,"a");
				for(;;){
					fgets(st,80,fp);
					if(st[0]=='@') break;
					if(feof(fp)) break;
					if(st[0]==ch[0]) break;
					fputs(st,wp);
					}
			fclose(wp);
			fclose(fp);

	o=(random(100)+1);   /*trap*/
	if(o<17) {
		 wp=fopen(flname,"a");
		 fputs("The treasure has a trap (if applicable)-\n",wp);
		 fputs("             ",wp);
		 fclose(wp);
		r=0;
		fp=fopen("tables\\dungeon\\contrap.tbl","r");
		for(;;){
			if(feof(fp)) break;
			fgets(st,80,fp);
			if(st[0]=='@') break;
			if(feof(fp)) break;
			if(st[0]==ch[0]) r++;
		}
		fclose(fp);
		r=r-1;
		s=(random(r)+1);
		fp=fopen("tables\\dungeon\\contrap.tbl","r");
		count=0;
			for(;;){
				if(feof(fp)) break;
				fgets(st,80,fp);
				if(st[0]=='@') break;
				if(feof(fp)) break;
				if(st[0]==ch[0]) count++;
				if(count==s) break;
				}
			wp=fopen(flname,"a");
			for(;;){
				if(feof(fp)) break;
				fgets(st,80,fp);
				if(st[0]=='@') break;
				if(feof(fp)) break;
				if(st[0]==ch[0]) break;
				fputs(st,wp);
				}

			fclose(wp);
			fclose(fp);
	}
	wp=fopen(flname,"a");
	fputs("\n",wp);
	fclose(wp);

}



/***CREATE TRAPS WITH MONSTERS****/
void monster_tr(void)
{
static  FILE *wp;
  int l,pc;
  int o,p,count;
  l=0;
  pc=0;
  o=0;
  p=0;
  count=0;
  l=(random(level));
  mlevel(l);
   wp=fopen(flname,"a");
   fputs("There is also some treasure in this room.\n",wp);
   fputs("There is the following:\n",wp);
   fclose(wp);
  p=(random(level)+1);
  for(count=0;count<p;count++){

    o=(random(12)+1);

    if(o==1) {
	    pc=(random(1000*level)+1);
	    sprintf(ml,"%d",pc);

	    wp=fopen(flname,"a");
	      fputs(ml,wp);
	      fputs(" copper pieces\n",wp);
	     fclose(wp);
	     }

    if(o==2) {
		pc=(random(1000*level)+1);
		sprintf(ml,"%d",pc);

		wp=fopen(flname,"a");
		 fputs(ml,wp);
		 fputs(" silver pieces\n",wp);
		fclose(wp);
		}

    if(o==3) {
		pc=(random(750*level)+1);
		sprintf(ml,"%d",pc);

		wp=fopen(flname,"a");
		 fputs(ml,wp);
		 fputs(" steel pieces\n",wp);
		fclose(wp);
		}

    if(o==4) {
		pc=(random(250*level)+1);

		sprintf(ml,"%d",pc);
		wp=fopen(flname,"a");
		 fputs(ml,wp);
		 fputs(" gold pieces\n",wp);
		fclose(wp);
		}

    if(o==5) {
		pc=(random(100*level)+1);
		sprintf(ml,"%d",pc);

		wp=fopen(flname,"a");
		 fputs(ml,wp);
		 fputs(" platinum pieces\n",wp);
		fclose(wp);
		}

    if(o==6) {
	      wp=fopen(flname,"a");
	      pc=(random(level)+1);
	      sprintf(ml,"%d",pc);

		 fputs(ml,wp);
		 fputs(" gems worth ",wp);
		 pc=(random(125*level)+1);
		sprintf(ml,"%d",pc);
		 fputs(ml,wp);
		 fputs(" gold pieces\n",wp);
		fclose(wp);
		}

    if(o==7) {
		wp=fopen(flname,"a");
		pc=(random(4)+level);
		sprintf(ml,"%d",pc);

		 fputs(ml,wp);
		 fputs(" pieces of jewelry worth ",wp);
		 pc=(random(100*level)+1);
		 sprintf(ml,"%d",pc);

		 fputs(ml,wp);
		 fputs(" gold pieces\n",wp);
		fclose(wp);
		}

  if(o>=8) magic();
  container();
  }

}


/***********GENERATE RANDOM TRAP*************/
void trap(void)
{
static FILE *wp;
static FILE *fp;
int r,s,count;
char ch[1]={"*"};
char st[81];

r=0;
s=0;
count=0;

	 wp=fopen(flname,"a");
	 fputs("There is a trap in this room.\n",wp);
	 fclose(wp);

	  fp=fopen("tables\\dungeon\\trap.tbl","r");
		for(;;){

		if(feof(fp)) break;
		fgets(st,80,fp);
		if(st[0]=='@') break;
		if(feof(fp)) break;
		if(st[0]==ch[0]) r++;
		}
		fclose(fp);
		r=r-1;

		s=(random(r)+1);

		fp=fopen("tables\\dungeon\\trap.tbl","r");
		count=0;

			for(;;){
				if(feof(fp)) break;
				fgets(st,80,fp);
				if(st[0]=='@') break;
				if(feof(fp)) break;
				if(st[0]==ch[0]) count++;
				if(count==s) break;
			}
			wp=fopen(flname,"a");
			for(;;){
				if(feof(fp)) break;
				fgets(st,80,fp);
				if(st[0]=='@') break;
				if(feof(fp)) break;
				if(st[0]==ch[0]) break;
				 fputs(st,wp);
		       }
		fclose(wp);
		fclose(fp);

}


/************GENERATE RANDOM ROOM DESCRIPTION**********/
void desc(void)
{
static FILE *fp;
static FILE *wp;
int r,s,count;
char ch[1]={"*"};
char st[81];

	r=0;
	s=0;
	count=0;

	if(duntype==1) fp=fopen("tables\\dungeon\\cutdesc.tbl","r");
	if(duntype==2) fp=fopen("tables\\dungeon\\natdesc.tbl","r");
	for(;;){
	if(feof(fp)) break;
	fgets(st,80,fp);
	if(st[0]=='@') break;
	if(st[0]==ch[0]) r++;
	if(feof(fp)) break;
	}
	fclose(fp);
	r=r-1;

	s=(random(r)+1);

	if(duntype==1) fp=fopen("tables\\dungeon\\cutdesc.tbl","r");
	if(duntype==2) fp=fopen("tables\\dungeon\\natdesc.tbl","r");
	for(count=1;count<=s;count++){
		for(;;){
			if(feof(fp)) break;
			fgets(st,80,fp);
			if(st[0]=='@') break;
			if(st[0]==ch[0]) break;
			if(feof(fp)) break;
		}
	}
		wp=fopen(flname,"a");
		for(;;){
			if(feof(fp)) break;
			fgets(st,80,fp);
			if(st[0]=='@') break;
			if(feof(fp)) break;
			if(st[0]==ch[0]) break;
			 fputs(st,wp);
		}
		fclose(wp);
		fclose(fp);
}


/*********GENERATE RANDOM MISC CONTENTS*******/
void contents(void)
{
static FILE *fp;
static FILE *wp;
	int r,s,count,ss;
	char item[81];

	r=0;
	s=0;
	count=0;
	ss=0;

	 wp=fopen(flname,"a");
	 fputs("This room contains the following:\n",wp);
	 fclose(wp);
	fp=fopen("tables\\dungeon\\contents.tbl","r");
	ss=(random(10)+1);
	for(;;){
		if(feof(fp)) break;
		fgets(item,80,fp);
		if(item[0]=='@') break;
		if(item[0]!='@') r++;
		if(feof(fp)) break;
	}
	fclose(fp);
	for(count=1;count<=ss;count++){
		fp=fopen("tables\\dungeon\\contents.tbl","r");
		s=(random(r)+1);
		for(r=1;r<=s;r++){
			if(feof(fp)) break;
			fgets(item,80,fp);
			if(item[0]=='@') break;
			if(feof(fp)) break;
		}
		 wp=fopen(flname,"a");
		 fputs("        ",wp);
		 fputs(item,wp);
		 fclose(wp);
		fclose(fp);
	}
}


/********CREATE TEXT FOR INTERIOR***********/
void interior(void)
{

  int rn;
  int rl;
  char st[81];

  rn=0;
  rl=0;



	 rp=fopen(flname,"a");
	     fputs("========================================================\n\n\n",rp);

	     fputs("\n\n\n",rp);
	     fputs("        For characters of level ",rp);
	     sprintf(ml,"%d",clevel);

	     fputs(ml,rp);
	     fputs(" or higher\n\n\n",rp);
	     fputs("The gamemaster should look over this module and find a suitable\n",rp);
	     fputs("starting position as an entrance.  Also make any modifications\n",rp);
	     fputs("to the dungeon map such as connecting unconnected rooms and\n",rp);
	     fputs("passageways.  Possibly having teleports to the different parts\n",rp);
	     fputs("or levels.  You can use a paint program to modify the map files.\n",rp);
	     fputs("By drawing in key items on the map (secret doors, traps, etc)\n",rp);
	     fputs("the gamemaster can quickly see where things are at.\n\n\n",rp);
	     fclose(rp);

  roaming();    /*CHECK FOR ROAMING MONSTERS*/

  for(rn=1;rn<=room;rn++){
	   rl=(random(28)+1);
	   rp=fopen(flname,"a");
	   fputs("\n\nROOM ",rp);
	   sprintf(ml,"%d",rn);

	   fputs(ml,rp);
	   fputs("\n",rp);
	   fputs("-----------------------------------------------------\n",rp);
	   fclose(rp);
			if(rl<5){
				rp=fopen(flname,"a");
				fputs("****Empty Room****\n\n",rp);
				fclose(rp);
			}
			if(rl>=5) desc();
			if((rl>=5)&&(rl<=13)) monster();
			if((rl>=14)&&(rl<=21)) monster_tr();
			if((rl>=22)&&(rl<25)) trap();
			if(rl>=25) contents();
	 }

}



/*********SELECT ALL MONSTERS TO USE IN DUNGEON**************/
void selectall(int mnstlevel,int type)
{
  int countm;

   for(countm=1;countm<=moncount[mnstlevel];countm++){
	if(type==1){
		if(monsterdata[mnstlevel][countm].light==0){
			totalmonsters++;
			monsterdata[mnstlevel][countm].light=1;
		}
	}
	if(type==0){
		if(monsterdata[mnstlevel][countm].light==1){
			totalmonsters--;
			monsterdata[mnstlevel][countm].light=0;
		}
	}
   }
}



/********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;
			totalmonsters++;
		}
		else
		{
			monsterdata[mnstlevel][monnumber].light=0;
			totalmonsters--;
		}
	}
}



/********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;

		  }
	}
   }

}



/***************SELECT WHAT MONSTERS TO USE IN DUNGEON**************/
void monsterselect(void)
{
	int stopp;
	int t,len,typep,typet;
	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;
	typep=0;
	typet=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,font,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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,ml);
			bypass=1;
			mon();
		}
		if((my>140)&&(my<156)&&(mx>274)&&(mx<305)&&(bypass==0)){
		     if(monsterlevel<level){
			typep=0;
			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);

			wouttextxy(286,165,font,ml);
			bypass=1;
			mon();
			}
		}
		if((my>=180)&&(my<=192)&&(mx>274)&&(mx<302)&&(bypass==0)){
		     if(monsterlevel>1){
			monsterlevel--;
			typep=0;
			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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,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);

			wouttextxy(286,165,font,ml);
			bypass=1;
			mon();
			}
		}
	if((mx>142)&&(mx<178)&&(my>131)&&(my<142)){
			if(typep==0){
				selectall(monsterlevel,1);
				typet=1;
			}
			if(typep==1)
			{
				selectall(monsterlevel,0);
				typet=0;
			}
			typep=typet;
			moff();
			wputblock(0,0,monstersel,0);
			printmonsterlist(monsterlevel,mstartindex,mendindex);
			wtextcolor(255);
			sprintf(ml,"%d",monsterlevel);

			wouttextxy(286,165,font,ml);
			mon();
	}
	if((mx>11)&&(mx<62)&&(my>162)&&(my<182)&&(totalmonsters>0))  stopp=1;
     }
   if(stopp==1) break;
   }

 moff();
}





/***************SELECT THE ATTACK DIE**********************/
void attackdieselect(void)
{
	int stopp;

	stopp=0;

wcls(0);
	wsetpalette(0,255,attackdiepalette);

	wfade_out(1,255,0,attackdiepalette);
	wputblock(0,0,attackdiescreen,0);
	wfade_in(0,255,0,attackdiepalette);


wtexttransparent(0);
wtextcolor(255);

sprintf(ml,"%d",attackdietype);

wouttextxy(140,128,font,ml);

mon();

	for(;;){
		if(but==1){
			noclick();
			if((mx>22)&&(mx<66)&&(my>66)&&(my<93)){
				attackdietype=4;
				moff();
				wputblock(0,0,attackdiescreen,0);
				sprintf(ml,"%d",attackdietype);

				wouttextxy(140,128,font,ml);
				mon();
				}
			if((mx>79)&&(mx<117)&&(my>55)&&(my<92)){
				attackdietype=6;
				moff();
				wputblock(0,0,attackdiescreen,0);
				sprintf(ml,"%d",attackdietype);
				wouttextxy(140,128,font,ml);
				mon();
				}
			if((mx>126)&&(mx<151)&&(my>63)&&(my<88)){
				attackdietype=8;
				moff();
				wputblock(0,0,attackdiescreen,0);
				sprintf(ml,"%d",attackdietype);
				wouttextxy(140,128,font,ml);
				mon();
				}
			if((mx>167)&&(mx<193)&&(my>63)&&(my<85)){
				attackdietype=10;
				moff();
				wputblock(0,0,attackdiescreen,0);
				sprintf(ml,"%d",attackdietype);
				wouttextxy(140,128,font,ml);
				mon();
				}
			if((mx>211)&&(mx<237)&&(my>63)&&(my<87)){
				attackdietype=12;
				moff();
				wputblock(0,0,attackdiescreen,0);
				sprintf(ml,"%d",attackdietype);
				wouttextxy(140,128,font,ml);
				mon();
				}
			if((mx>253)&&(mx<278)&&(my>60)&&(my<84)){
				attackdietype=20;
				moff();
				wputblock(0,0,attackdiescreen,0);
				sprintf(ml,"%d",attackdietype);
				wouttextxy(140,128,font,ml);
				mon();
				}

			if((mx>114)&&(mx<190)&&(my>176)&&(my<192))
				 {
				 stopp=1;
				 }
		}
		if(stopp==1) break;
	}
moff();
wcls(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;


      /********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=0;
	}
	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=0;
	}
	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=0;
	}
	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=0;
	}
	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=0;
	}
	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=0;
	}
	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=0;
	}
	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=0;
	}
	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=0;
	}
	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=0;
	}
	fclose(fpmonst);



}

void main()
{
/*void main(int argc,char **argv, char **envp){ */
	int fre;



	getsavdata();
	initmonsters();


	vga256();
	minit();
	wcls(0);
	wtextcolor(15);
	wouttextxy(120,100,NULL,"Loading...");

	setlib("graphics\\monster.lib");
	/*setpassword("orcus");*/
	/************/
	font=wloadfont("font.3");
	attackdiescreen=wloadpcx256("atdie.3",attackdiepalette);
	monstersel=wloadpcx256("monster.3",monsterselpalette);
	savingscreen=wloadpcx256("saving.3",savingpalette);



	attackdietype=4;

	attackdieselect();


	monsterselect();

		wcls(0);

		wsetpalette(0,255,savingpalette);
		wputblock(0,0,savingscreen,0);

	interior();


wfreefont(font);
	wfreeblock(savingscreen);
	wfreeblock(attackdiescreen);
	wfreeblock(monstersel);
mdeinit();

	execv("start.exe",NULL);
}


