#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wgt4.h>
#include <process.h>
#include <alloc.h>
#include "world.h"
#include "tileit.h"
#include "savetile.h"
#include "maktiles.h"
#include "placeit.h"
#include <io.h>
#include <fcntl.h>
#include <dir.h>
#include <dspi.h>


#ifdef __DPMI32__
	#define  CONFIG   "MIDI32.CFG"
	#define  DRV      "32BIT"
#elif defined(__DPMI16__)
	#define  CONFIG   "MIDI16.CFG"
	#define  DRV      "16BIT"
#else
	#define  CONFIG   "MIDI.CFG"
	#define  DRV      "DRVR"
#endif


int LDIR;
int RDIR;

block mapbackground;
block dummy;
block gridbackground;
block totalsbackground;
block populateback;
color populatepal[256];
color gridpal[256];
color totalspal[256];
color mapbackpal[256];
color scpalette[256];
block screen1;
block emsprite;
color bigtilepal[256];
block bigtile[114];
static int dungeons;
static int hamlets;
static int villages;
static int towns;
static int smallcity;
static int mediumcity;
static int largecity;
static int forest;
static int lightforest;
static int mountain;
static int desert;
static int plains;
static int coastal;
static int music;
static int dotmatrix;

FILE *fpie;
FILE *sav;
int MAP[81][51];



LPBYTE GetConfigString (void) {
	FILE *fp;
	DWORD dwLen;
	LPBYTE file;


	chdir("DRVR");
	fp = fopen ("MIDI.CFG", "rb");
	if (fp == NULL) {
		return NULL;
	}
	chdir("..");
	dwLen = filelength(fileno(fp));
	file = (LPBYTE)farmalloc(dwLen + 1);
	fread (file, 1, (WORD)dwLen, fp);
	fclose (fp);
	return file;

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


	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");
		return;
	}
	if ( midiPlaySMF (lpFile, &File) != MIDI_ERROR_NONE ) {
		midiCloseDriver();
		printf ("ERROR: Unable to play MIDI file\n");
		return;
	}

}



int citycount(void)
{
char *st;
int x;
int cnt;


	if((fpie=fopen("data\\dungeon.dta","r"))!=NULL){
		for(;;){
			if(feof(fpie)) break;
			fgets(st,81,fpie);
			if(feof(fpie)) break;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			dungeons++;
			x++;
			}
	fclose(fpie);
	}
	if((fpie=fopen("data\\forest.dta","r"))!=NULL){
		for(;;){

			fgets(st,81,fpie);
			if(feof(fpie)) break;
			fgets(st,81,fpie);
			cnt=atoi(st);
			if(cnt==1) hamlets++;
			if(cnt==2) villages++;
			if(cnt==3) towns++;
			if(cnt==4) smallcity++;
			if(cnt==5) mediumcity++;
			if(cnt==6) largecity++;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			if(feof(fpie)) break;
			forest++;
			x++;
			}
	fclose(fpie);
	}
	if((fpie=fopen("data\\mountain.dta","r"))!=NULL){
		for(;;){
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			if(cnt==1) hamlets++;
			if(cnt==2) villages++;
			if(cnt==3) towns++;
			if(cnt==4) smallcity++;
			if(cnt==5) mediumcity++;
			if(cnt==6) largecity++;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			if(feof(fpie)) break;
			mountain++;
			x++;
			}
	fclose(fpie);
	}
	if((fpie=fopen("data\\desert.dta","r"))!=NULL){
		for(;;){
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			if(cnt==1) hamlets++;
			if(cnt==2) villages++;
			if(cnt==3) towns++;
			if(cnt==4) smallcity++;
			if(cnt==5) mediumcity++;
			if(cnt==6) largecity++;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			if(feof(fpie)) break;
			desert++;
			x++;
			}
	fclose(fpie);
	}
	if((fpie=fopen("data\\plains.dta","r"))!=NULL){
		for(;;){

			fgets(st,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			if(cnt==1) hamlets++;
			if(cnt==2) villages++;
			if(cnt==3) towns++;
			if(cnt==4) smallcity++;
			if(cnt==5) mediumcity++;
			if(cnt==6) largecity++;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			if(feof(fpie)) break;
			plains++;
			x++;
			}
	fclose(fpie);
	}

	if((fpie=fopen("data\\coastal.dta","r"))!=NULL){
		for(;;){

			fgets(st,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			if(cnt==1) hamlets++;
			if(cnt==2) villages++;
			if(cnt==3) towns++;
			if(cnt==4) smallcity++;
			if(cnt==5) mediumcity++;
			if(cnt==6) largecity++;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			if(feof(fpie)) break;
			coastal++;
			x++;
			}
	fclose(fpie);
	}


return(x);
}

int totals(void)
{
char ttls[10];
int stopp,s;
wcls(0);
dummy=wems_getdata(totalsbackground);
wputblock(0,0,dummy,0);
wsetpalette(0,255,totalspal);
stopp=0;
wtextcolor(1);
	if(dungeons>0){
		itoa(dungeons,ttls,10);
		wouttextxy(260,40,NULL,ttls);
		}
	if(hamlets>0){
		itoa(hamlets,ttls,10);
		wouttextxy(260,60,NULL,ttls);
		}
	if(villages>0){
		itoa(villages,ttls,10);
		wouttextxy(260,80,NULL,ttls);
		}
	if(towns>0){
		itoa(towns,ttls,10);
		wouttextxy(260,100,NULL,ttls);
		}
	if(smallcity>0){
		itoa(smallcity,ttls,10);
		wouttextxy(260,120,NULL,ttls);
		}
	if(mediumcity>0){
		itoa(mediumcity,ttls,10);
		wouttextxy(260,140,NULL,ttls);
		}
	if(largecity>0){
		itoa(largecity,ttls,10);
		wouttextxy(260,160,NULL,ttls);
		}
wsetcolor(255);


msetbounds(0,0,319,199);
mon();      /*turn mouse on*/
	while(stopp==0){
		if(but==1){
			noclick();
			if((mx>48)&&(mx<128)&&(my>53)&&(my<96)){
							s=1;
							stopp=1;
							}
			if((mx>48)&&(mx<128)&&(my>118)&&(my<161)){
							 s=0;
							 stopp=1;
							 }
		}
	}
moff();         /*turn mouse off*/
wcls(0);
dummy=wems_getdata(populateback);
wputblock(0,0,dummy,0);
wsetpalette(0,255,populatepal);
return(s);
}




void randomplace(void)
{
char *st;
char *name;
int pop; /*population*/
int lev; /*dungeon level*/
int cnt,xx,loop,loopcnt;
int brk;


   loopcnt=1;
   brk=0;
   if(dungeons>0){
     for(;;){
      if((fpie=fopen("data\\dungeon.dta","r"))!=NULL){
	for(loop=1;loop<=loopcnt;loop++){
			fgets(name,81,fpie);
			/*if(feof(fpie)) break;*/
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			lev=atoi(st);
			/*if(feof(fpie)) break; */
		}
		fclose(fpie);
	}
	placeovertile(8,107,name,lev);  /*ANYWHERE dungeon tile*/
	loopcnt++;
	if(loopcnt>dungeons) brk=1;
	if(brk==1) break;
      }
     } /*end of if dungeons>0*/
     loopcnt=1;
     brk=0;

     if(forest>0){
       for(;;){
	if((fpie=fopen("data\\forest.dta","r"))!=NULL){
	 for(loop=1;loop<=loopcnt;loop++){

			fgets(name,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			pop=cnt;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
		       /*if(feof(fpie)) break;*/
		}
		fclose(fpie);
	 }
	xx=(random(2)+1);
			if(xx==1){
				if(cnt==1) placeovertile(4,108,name,pop);
				if(cnt==2) placeovertile(4,109,name,pop);
				if(cnt==3) placeovertile(4,110,name,pop);
				if(cnt==4) placeovertile(4,111,name,pop);
				if(cnt==5) placeovertile(4,112,name,pop);
				if(cnt==6) placeovertile(4,113,name,pop);
				} /*forest*/
			if(xx==2){
				if(cnt==1) placeovertile(3,108,name,pop);
				if(cnt==2) placeovertile(3,109,name,pop);
				if(cnt==3) placeovertile(3,110,name,pop);
				if(cnt==4) placeovertile(3,111,name,pop);
				if(cnt==5) placeovertile(3,112,name,pop);
				if(cnt==6) placeovertile(3,113,name,pop);
				}/*lightforest*/


	loopcnt++;
	if(loopcnt>forest) brk=1;
	if(brk==1) break;
       }
     }
     loopcnt=1;
     brk=0;
     if(mountain>0){
      for(;;){
      if((fpie=fopen("data\\mountain.dta","r"))!=NULL){
       for(loop=1;loop<=loopcnt;loop++){

			fgets(name,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			pop=cnt;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			/*if(feof(fpie)) break;*/
		}
       }
       fclose(fpie);
	xx=(random(2)+1);
			if(xx==1){
				if(cnt==1) placeovertile(6,108,name,pop);
				if(cnt==2) placeovertile(6,109,name,pop);
				if(cnt==3) placeovertile(6,110,name,pop);
				if(cnt==4) placeovertile(6,111,name,pop);
				if(cnt==5) placeovertile(6,112,name,pop);
				if(cnt==6) placeovertile(6,113,name,pop);
				} /*mountains*/
			if(xx==2){
				if(cnt==1) placeovertile(5,108,name,pop);
				if(cnt==2) placeovertile(5,109,name,pop);
				if(cnt==3) placeovertile(5,110,name,pop);
				if(cnt==4) placeovertile(5,111,name,pop);
				if(cnt==5) placeovertile(5,112,name,pop);
				if(cnt==6) placeovertile(5,113,name,pop);
				}/*hills*/

      loopcnt++;
      if(loopcnt>mountain) brk=1;
      if(brk==1) break;
      }
   }  /*end of mountains*/

  brk=0;
  loopcnt=1;

  if(desert>0){
    for(;;){
	if((fpie=fopen("data\\desert.dta","r"))!=NULL){
	  for(loop=1;loop<=loopcnt;loop++){
			fgets(name,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			pop=cnt;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			}
	  fclose(fpie);
	}
	if(cnt==1) placeovertile(1,108,name,pop);
	if(cnt==2) placeovertile(1,109,name,pop);
	if(cnt==3) placeovertile(1,110,name,pop);
	if(cnt==4) placeovertile(1,111,name,pop);
	if(cnt==5) placeovertile(1,112,name,pop);
	if(cnt==6) placeovertile(1,113,name,pop);

	loopcnt++;
	if(loopcnt>desert) brk=1;
	if(brk==1) break;
    }
  } /*end desert*/

  loopcnt=1;
  brk=0;

  if(plains>0){
    for(;;){
	if((fpie=fopen("data\\plains.dta","r"))!=NULL){
	  for(loop=1;loop<=loopcnt;loop++){
			fgets(name,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			pop=cnt;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			/*if(feof(fpie)) break;*/
			}
	fclose(fpie);
	}
	if(cnt==1) placeovertile(2,108,name,pop);
	if(cnt==2) placeovertile(2,109,name,pop);
	if(cnt==3) placeovertile(2,110,name,pop);
	if(cnt==4) placeovertile(2,111,name,pop);
	if(cnt==5) placeovertile(2,112,name,pop);
	if(cnt==6) placeovertile(2,113,name,pop);

	loopcnt++;
	if(loopcnt>plains) brk=1;
	if(brk==1) break;
    }
  }

  loopcnt=1;
  brk=0;

  if(coastal>0){
	for(;;){
	  if((fpie=fopen("data\\coastal.dta","r"))!=NULL){
	    for(loop=1;loop<=loopcnt;loop++){
			fgets(name,81,fpie);
			fgets(st,81,fpie);
			cnt=atoi(st);
			pop=cnt;
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			fgets(st,81,fpie);
			/*if(feof(fpie)) break;*/
	    }
	    fclose(fpie);
	}
	if(cnt==1) placeovertile(0,108,name,pop);
	if(cnt==2) placeovertile(0,109,name,pop);
	if(cnt==3) placeovertile(0,110,name,pop);
	if(cnt==4) placeovertile(0,111,name,pop);
	if(cnt==5) placeovertile(0,112,name,pop);
	if(cnt==6) placeovertile(0,113,name,pop);

	loopcnt++;
	if(loopcnt>coastal) brk=1;
	if(brk==1) break;
    }
  }

return;
}





void main()
{

  int mnum,ix,iy,useEMS;
  int r,hexon; /*int for 0 in save*/
  int QT,sp,Quit_World;
  unsigned long mem_avail;
  mnum=1;
  r=0;
  QT=0;
  sp=0;
  Quit_World=0;

    vga256();
		if(wdetectcpu()<3){
		printf("Error- A 386 or better machine is required for this program.");
		printf("\n\n");
		exit(0);
		}
		if(!vgadetected()){
		printf("Error-VGA card required.\n");
		exit(0);
		}
	     /*	mem_avail=coreleft();
		if(mem_avail<580) {
		printf("Error- 580k free conventional memory needed.\n");
		printf("Try removing any non-essential files from your\n");
		printf("CONFIG.SYS or AUTOEXEC.BAT files, or create a boot\n");
		printf("disk with essential CONFIG.SYS and AUTOEXEC.BAT settings\n");
		exit(0);
		}    */
	useEMS=wems_present();
	if(!useEMS) {
		printf("\nNo EMS memory available.  Check your memory manager\n");
		exit(1);
		}


	remove("text\\mlegend.txt");   /*clear legend file for new*/
	remove("temp\\mlegend.tmp");
	music=0;
	dotmatrix=0;
	fpie=fopen("data\\world.cfg","r");
	fscanf(fpie,"%d",&music);   /*1=music 0=no music*/
	fscanf(fpie,"%d",&dotmatrix);   /*0=laser 1=dotmatrix*/
	fclose(fpie);
	if(minit()<0){        /*initialize mouse*/
		printf("No mouse or mouse driver was found.\n");
		printf("This program will not run without a mouse\n");
		exit(0);
		}



	wcls(0);
	wreadpalette(0,255,scpalette);
	 wtextcolor(255);
	wouttextxy(120,100,NULL,"LOADING...");
	if(music>0) soundset();
	wems_init();
	wems_open(64);
	setlib("graphics\\world.lib");
	/*setpassword("orcus");*/
	wems_loadsprites(bigtilepal,"bigtile.spr",bigtile,0,113);
	mapbackground=wloadpcx256("backmap.pcx",mapbackpal);
	mapbackground=wems_storeblock(mapbackground);
	totalsbackground=wloadpcx256("totals.pcx",totalspal);
	totalsbackground=wems_storeblock(totalsbackground);
	populateback=wloadpcx256("populate.pcx",populatepal);
	populateback=wems_storeblock(populateback);
	gridbackground=wloadpcx256("grid.pcx",gridpal);
	gridbackground=wems_storeblock(gridbackground);
	randomize();


wcls(0);

wsetpalette(0,255,scpalette);
while(QT==0){
 dummy=wems_getdata(mapbackground);
 wputblock(0,0,dummy,0);
 dummy=wnewblock(0,0,318,48); /*get overlay*/
 wtextcolor(0);
 wouttextxy(97,21,NULL,"Generating world");
 wouttextxy(95,19,NULL,"Generating world");
 wouttextxy(95,20,NULL,"Generating world");
 wouttextxy(97,20,NULL,"Generating world");
 wtextcolor(14);
 wouttextxy(96,20,NULL,"Generating world");


 for(mnum=1;mnum<=4;mnum++){
 if(music>0){
    if(mnum==1)  loadandplay("data\\m1");  /*play song if music on*/
    if(mnum==2)  {  midiStopOutput();
		loadandplay("data\\m2");  /*play song if music on*/
		 }
    if(mnum==3){  midiStopOutput();
		loadandplay("data\\m3");  /*play song if music on*/
		}
    if(mnum==4) {  midiStopOutput();
		loadandplay("data\\m4");  /*play song if music on*/

		}
    }
	initland();
	makemapl(mnum);
	makemapr(mnum);
	landremoval();
	makemapr(mnum);
	landremoval();



	if(mnum==1) {
		sav=fopen("temp\\map1.dat","wb");
		for(iy=1;iy<=50;iy++){
		  for(ix=1;ix<=80;ix++){
		    fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		    }
		}
		fclose(sav);
		ix=80;
		sav=fopen("temp\\map1r.dat","wb");
		for(iy=1;iy<=50;iy++){
		     fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		     }
		fclose(sav);
		iy=50;
		sav=fopen("temp\\map1b.dat","wb");
		for(ix=1;ix<=80;ix++){
		     fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		     }
		fclose(sav);
	    }
	if(mnum==2) {
		sav=fopen("temp\\map2.dat","wb");
		for(iy=1;iy<=50;iy++){
		  for(ix=1;ix<=80;ix++){
		    fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		    }
		}
		fclose(sav);
		sav=fopen("temp\\map2b.dat","wb");
		iy=50;
		for(ix=1;ix<=80;ix++){
		    fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		    }
		fclose(sav);
		ix=1;
		sav=fopen("temp\\map2l.dat","wb");
		for(iy=1;iy<=50;iy++){
		     fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		     }
		fclose(sav);
	    }
	if(mnum==3) {
		sav=fopen("temp\\map3.dat","wb");
		for(iy=1;iy<=50;iy++){
		  for(ix=1;ix<=80;ix++){
		    fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		    }
		}
		fclose(sav);
		sav=fopen("temp\\map3t.dat","wb");
		for(ix=1;ix<=80;ix++){
		    fwrite(&MAP[ix][1],sizeof(int),1,sav);
		    }
		fclose(sav);

		ix=80;
		sav=fopen("temp\\map3r.dat","wb");
		for(iy=1;iy<=50;iy++){
		    fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		    }
		fclose(sav);
	    }
	if(mnum==4) {
		sav=fopen("temp\\map4.dat","wb");
		for(iy=1;iy<=50;iy++){
		  for(ix=1;ix<=80;ix++){
		    fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		    }
		}
		fclose(sav);
		sav=fopen("temp\\map4t.dat","wb");
		for(ix=1;ix<=80;ix++){
		    fwrite(&MAP[ix][1],sizeof(int),1,sav);
		    }
		fclose(sav);
		ix=1;
		sav=fopen("temp\\map4l.dat","wb");
		for(iy=1;iy<=50;iy++){
		     fwrite(&MAP[ix][iy],sizeof(int),1,sav);
		     }
		fclose(sav);
		sav=fopen("temp\\cmap1.dat","wb");
		for(iy=1;iy<=50;iy++){
		 for(ix=1;ix<=80;ix++){
		    fwrite(&r,sizeof(int),1,sav);
		    }
		}
		fclose(sav);
		sav=fopen("temp\\cmap2.dat","wb");
		for(iy=1;iy<=50;iy++){
		 for(ix=1;ix<=80;ix++){
		    fwrite(&r,sizeof(int),1,sav);
		    }
		}
		fclose(sav);
		sav=fopen("temp\\cmap3.dat","wb");
		for(iy=1;iy<=50;iy++){
		 for(ix=1;ix<=80;ix++){
		    fwrite(&r,sizeof(int),1,sav);
		    }
		}
		fclose(sav);
		sav=fopen("temp\\cmap4.dat","wb");
		for(iy=1;iy<=50;iy++){
		 for(ix=1;ix<=80;ix++){
		    fwrite(&r,sizeof(int),1,sav);
		    }
		}
		fclose(sav);

	    }
  minimap(mnum);
  }
  if(music>0) midiStopOutput();
 wputblock(0,0,dummy,0);
 sound(440);
 delay(200);
 nosound();
 wtextcolor(0);
 wouttextxy(81,21,NULL,"KEEP   CREATE   QUIT");
 wouttextxy(79,19,NULL,"KEEP   CREATE   QUIT");
 wouttextxy(79,20,NULL,"KEEP   CREATE   QUIT");
 wouttextxy(81,20,NULL,"KEEP   CREATE   QUIT");
 wtextcolor(14);

 wouttextxy(80,20,NULL,"KEEP   CREATE   QUIT");

 mon();
	sp=0;
	QT=0;
	for(;;){
	   if(but==1){
		noclick();
		if((mx>81)&&(mx<113)&&(my>21)&&(my<29)){ QT=1;sp=1;}
		if((mx>137)&&(mx<186)&&(my>21)&&(my<29)) {QT=0;sp=1;}
		if((mx>210)&&(mx<242)&&(my>21)&&(my<29)) {QT=1;sp=1;
							Quit_World=1;}
	   }
	   if(sp==1) break;
	}

 moff();
 } /*END while QT==0*/

 if(Quit_World==0){
  if(citycount()!=0){

		      /*IF THINGS TO PLACE COUNT TOTAL CITIES AND DUNGEONS
			ASK IF YES OR NO FOR RANDOM PLACEMENT*/
	if(totals()!=0){
	randomplace();  /*place cities randomly*/
	}
   }

  wcls(0);
  wsetpalette(0,255,scpalette);
  maketiles();
  wcls(0);
  dummy=wems_getdata(gridbackground);
  wsetpalette(0,255,gridpal);

 wputblock(0,0,dummy,0);
 mon();

	sp=0;
	hexon=0;
	for(;;){
	   if(but==1){
		noclick();
		if(mx<160) {hexon=0;sp=1;}
		if(mx>160) {hexon=1;sp=1;}

	   }
	   if(sp==1) break;
	}
 moff();
  wsetpalette(0,255,scpalette);

  savetilemap(hexon);  /*1=hexon 0=hexoff*/
 } /*end IF QUITWORLD */

 /*Free up memory and quit program*/
wfreesprites(bigtile,0,114);
wcls(0);

  if(music>0) midiCloseDriver();
wems_close();
mdeinit();

if(Quit_World==0)  execv("fsrt",NULL);

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

