#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wgt4.h>
#include <dos.h>
#include <dspi.h>
#include <cut.h>
#include <alloc.h>
/*extern unsigned _stklen = 24000U;*/





	#define  CONFIG   "MIDI.CFG"
	#define  DRV      "DRVR"


char *map[81][76];
color pal[256];
color mappalette[256];
block backscreen;

int TOTALCOUNT;
int MAXNUMTILES;
int oldmode;
FILE *fp;

LPBYTE ffile;


LPBYTE GetConfigString (void) {

	DWORD dwLen;



	chdir("DRVR");
	fp = fopen ("MIDI.CFG", "rb");
	if (fp == NULL) {
		printf("no sound configuration\nRun SETUP\n");
		exit(0);
	}
	chdir("..");
	dwLen = filelength(fileno(fp));
	ffile = (LPBYTE)farmalloc(dwLen + 1);
	fread (ffile, 1, (WORD)dwLen, fp);
	fclose (fp);
	return ffile;

}
void soundset()
{
	LPBYTE lpString;
	MIDICAPS caps;
	lpString = GetConfigString();
	if ( lpString == NULL ) {
		printf ("ERROR: Unable to load file %s\n", CONFIG);
		exit(0);
	}
	chdir("DRVR");
	if ( midiLoadDriver((LPSTR)lpString) != MIDI_ERROR_NONE ) {
		printf ("ERROR: Unable to load driver %s\n", lpString);
		chdir ("..");
		exit(0);
	}
	chdir ("..");
	midiSetDriverSpecific ( lpString + 13 );
	if ( midiDriverEntry() != MIDI_ERROR_NONE ) {
		printf ("ERROR: Cannot initialize driver\n");
		exit(0);
	}


	midiGetDeviceCaps (&caps);

}




void loadandplay(char *perrysong)
{

	SMF_0 File;
	HPBYTE lpFile;



	lpFile = midiLoadSMF (perrysong, &File);
	if (lpFile == NULL) {
		midiCloseDriver();
		printf ("ERROR: Unable to load midi file");
		exit(0);
	}
	if ( midiPlaySMF (lpFile, &File) != MIDI_ERROR_NONE ) {
		midiCloseDriver();
		printf ("ERROR: Unable to play MIDI file\n");
		exit(0);
	}

}



/*****************MAIN PROGRAM STARTS*********************/
void main()
{
int x,y;
int count,bigcount;
int dotx,doty;
int stopp,possible,maxn;
int direction,counter;
int music,dotmatrix;
float lineplace,line;


direction=0;
possible=0;
maxn=0;
count=1;
stopp=0;
bigcount=0;

MAXNUMTILES=60;

randomize();

vga256();



  wloadpalette("graphics\\default.pal",pal);
  wsetpalette(0,255,pal);
  wcls(0);

	fp=fopen("data\\world.cfg","r");
	fscanf(fp,"%d",&music);   /*1=music 0=no music*/
	fscanf(fp,"%d",&dotmatrix);   /*0=laser 1=dotmatrix*/
	fclose(fp);
	if(music>0) soundset();
 if(music>0) loadandplay("data\\m5");  /*play song if music on*/
	setlib("graphics\\cut.lib");
	backscreen=wloadpcx256("cutback.pcx",mappalette);
wsetpalette(0,255,mappalette);
wputblock(0,0,backscreen,0);
wfreeblock(backscreen);
  wtextcolor(15);
  wouttextxy(86,80,NULL,"Generating Dungeon");
  wouttextxy(52,115,NULL,"   0%");
  wouttextxy(210,115,NULL,"100%");
  wsetcolor(55);
  wrectangle(101,111,203,121);
  wsetcolor(70);


tileinit();
clearmap();
boarders(MAXNUMTILES);



	for(maxn=1;maxn<=MAXNUMTILES;maxn++){
	possible=random(37)+1;
	placetile(possible);
	}

TOTALCOUNT=countdot();

	while(count>0){

			dotx=random(79)+1;
			doty=random(74)+1;


			if(strcmp(map[dotx][doty],".")==0){
			  direction=random(4)+1;
			  stopp=0;
			  if(direction==1){        /*down*/
			    if(strcmp(map[dotx][doty+1],"X")==0) stopp=1;
			    if(strcmp(map[dotx][doty+1],"-")==0) stopp=1;
			    if(strcmp(map[dotx][doty+1],"a")==0) stopp=1;
			    if(strcmp(map[dotx][doty+1],"|")==0) stopp=1;
			    if(strcmp(map[dotx][doty+1],"c")==0) stopp=1;
			    y=doty;
			    counter=0;
			    while(stopp==0){
				map[dotx][y]=strdup("X");
				y++;
				counter++;
				if(counter>30) stopp=1;
				if(strcmp(map[dotx][y],"X")==0) stopp=1;
				if(strcmp(map[dotx][y+1],"-")==0) stopp=1;
				if(strcmp(map[dotx][y+1],"a")==0) stopp=1;
				if(strcmp(map[dotx][y+1],"|")==0) stopp=1;
				if(strcmp(map[dotx][y],"-")==0) stopp=1;
				if(strcmp(map[dotx][y],"a")==0) stopp=1;
				if(strcmp(map[dotx][y],"|")==0) stopp=1;
			    }
			    stopp=0;

			  } /*end direction up*/
			  stopp=0;
			  if(direction==2){        /*right*/
			    if(strcmp(map[dotx+1][doty],"X")==0) stopp=1;
			    if(strcmp(map[dotx+1][doty],"|")==0) stopp=1;
			    if(strcmp(map[dotx+1][doty],"u")==0) stopp=1;
			    if(strcmp(map[dotx+1][doty],"-")==0) stopp=1;
			    if(strcmp(map[dotx+1][doty],"c")==0) stopp=1;
			    x=dotx;
			    counter=0;
			    while(stopp==0){
				map[x][doty]=strdup("X");
				x++;
				counter++;
				if(counter>30) stopp=1;
				if(strcmp(map[x][doty],"X")==0) stopp=1;
				if(strcmp(map[x+1][doty],"|")==0) stopp=1;
				if(strcmp(map[x+1][doty],"u")==0) stopp=1;
				if(strcmp(map[x+1][doty],"-")==0) stopp=1;
				if(strcmp(map[x][doty],"|")==0) stopp=1;
				if(strcmp(map[x][doty],"u")==0) stopp=1;
				if(strcmp(map[x][doty],"-")==0) stopp=1;
			    }
			    stopp=0;

			  } /*end direction across*/
			  if(direction==3){        /*up*/
			    if(strcmp(map[dotx][doty-1],"X")==0) stopp=1;
			    if(strcmp(map[dotx][doty-1],"|")==0) stopp=1;
			    if(strcmp(map[dotx][doty-1],"a")==0) stopp=1;
			    if(strcmp(map[dotx][doty-1],"-")==0) stopp=1;
			    if(strcmp(map[dotx][doty-1],"c")==0) stopp=1;
			    y=doty;
			    counter=0;
			    while(stopp==0){
				map[dotx][y]=strdup("X");
				y--;
				counter++;
				if(counter>30) stopp=1;
				if(strcmp(map[dotx][y],"X")==0) stopp=1;
				if(strcmp(map[dotx][y-1],"|")==0) stopp=1;
				if(strcmp(map[dotx][y-1],"a")==0) stopp=1;
				if(strcmp(map[dotx][y-1],"-")==0) stopp=1;
				if(strcmp(map[dotx][y],"|")==0) stopp=1;
				if(strcmp(map[dotx][y],"a")==0) stopp=1;
				if(strcmp(map[dotx][y],"-")==0) stopp=1;
				}
				stopp=0;

			  } /*end direction up*/
			  stopp=0;
			  if(direction==4){        /*across*/
			    if(strcmp(map[dotx-1][doty],"X")==0) stopp=1;
			    if(strcmp(map[dotx-1][doty],"-")==0) stopp=1;
			    if(strcmp(map[dotx-1][doty],"u")==0) stopp=1;
			    if(strcmp(map[dotx-1][doty],"|")==0) stopp=1;
			    if(strcmp(map[dotx-1][doty],"c")==0) stopp=1;
			    x=dotx;
			    counter=0;
			    while(stopp==0){
				map[x][doty]=strdup("X");
				x--;
				counter++;
				if(counter>30) stopp=1;
				if(strcmp(map[x][doty],"X")==0) stopp=1;
				if(strcmp(map[x-1][doty],"-")==0) stopp=1;
				if(strcmp(map[x-1][doty],"u")==0) stopp=1;
				if(strcmp(map[x-1][doty],"|")==0) stopp=1;
				if(strcmp(map[x][doty],"-")==0) stopp=1;
				if(strcmp(map[x][doty],"u")==0) stopp=1;
				if(strcmp(map[x][doty],"|")==0) stopp=1;
			    }
			    stopp=0;

			  } /*end direction across*/
			bigcount=0;
			} /*if map==.*/
		count=countdot();
		bigcount++;
		lineplace=((float)count/(float)TOTALCOUNT);
		lineplace=(lineplace*100);
		line=(100-lineplace);
		if(line<=0) line=1;

		wbar(102,112,(102+(int)line),120);

		if(bigcount>500) count=0; /*if it takes too long to place*/
					  /*just end the mazemaking*/
	} /*end while*/

 if(music>0) midiStopOutput();
 if(music>0) midiCloseDriver();
 if(music>0) farfree(ffile);
	fp=fopen("temp\\map.out","wb");
	for(y=0;y<=75;y++){
		for(x=0;x<=80;x++){

	      if(strcmp(map[x][y],".")==0) fwrite(".",sizeof(char),1,fp);
	      if(strcmp(map[x][y],"N")==0) fwrite("N",sizeof(char),1,fp);
	      if(strcmp(map[x][y]," ")==0) fwrite(" ",sizeof(char),1,fp);
	      if(strcmp(map[x][y],"X")==0) fwrite("X",sizeof(char),1,fp);
	      if(strcmp(map[x][y],"-")==0) fwrite("-",sizeof(char),1,fp);
	      if(strcmp(map[x][y],"|")==0) fwrite("|",sizeof(char),1,fp);
	      if(strcmp(map[x][y],"u")==0) fwrite("u",sizeof(char),1,fp);
	      if(strcmp(map[x][y],"a")==0) fwrite("a",sizeof(char),1,fp);
	      if(strcmp(map[x][y],"c")==0) fwrite("c",sizeof(char),1,fp); /*column*/
	      if(strcmp(map[x][y],"0")==0) fwrite("0",sizeof(char),1,fp);
		}
	}
	fclose(fp);



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