/*                  START.C  main menu*/

#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <wgt4.h>
#include <io.h>
#include <wgtflic.h>
#include <dspi.h>
#include <alloc.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


FILE *fpie;
int oldmode;

block warning;
block menub;
block titlescreen;
color menupalette[256];
color warnpalette[256];
color titlepalette[256];

int city,dungeon,world,utility,quitt,check;
int music;

  struct time time1, time2;

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;
	}
}

void intro()
{
int          filefound;                 /* 1 if the animation file was found */
int          status;                    /* Status of FLI/FLC */
char ch;
int disslve[513]={
256,0,0,9,8,3,11,15,12,12,2,4,2,9,14,0,9,8,1,13,7,1,3,13,14
,5,7,3,14,7,4,11,11,7,11,2,6,13,0,10,5,6,15,0,12,15,4,5,0
,14,9,7,6,5,13,4,4,15,7,2,1,11,15,0,6,5,10,9,3,13,11,12,5
,0,14,2,9,6,3,10,1,11,9,7,13,3,8,11,13,0,2,14,2,9,11,6,8
,6,1,11,7,15,15,7,9,9,6,5,5,1,13,9,0,2,15,1,5,3,5,11,3
,1,10,15,10,4,0,15,0,8,15,1,7,14,6,3,3,9,12,13,4,4,12,4,6
,11,4,11,14,9,9,2,4,12,10,8,4,10,12,4,9,11,0,13,13,1,0,13,8
,1,1,1,11,10,6,7,2,0,4,14,1,14,14,10,10,7,7,9,2,14,3,13,10
,8,5,5,2,4,8,9,7,6,13,2,3,13,2,14,11,8,10,6,4,2,13,3,7
,12,13,12,7,15,1,4,7,11,8,7,0,5,11,12,6,1,4,1,14,8,8,3,10
,6,6,10,2,13,12,6,10,0,8,3,1,9,4,10,13,6,12,6,7,11,6,5,3
,8,13,4,5,3,2,7,14,2,12,4,14,10,8,14,4,12,3,12,9,11,2,8,3
,12,12,14,13,6,11,6,5,3,4,13,6,9,1,2,11,8,14,7,8,4,1,14,5
,9,10,2,8,9,5,7,1,15,2,3,13,15,8,1,2,15,6,4,15,10,3,12,0
,0,5,12,1,2,0,5,1,7,3,6,9,2,7,4,10,1,12,0,1,14,15,7,5
,8,7,10,9,1,6,3,0,8,2,8,9,1,9,10,15,10,14,10,7,12,8,14,12
,13,15,12,14,9,13,6,0,13,9,14,10,10,11,5,4,15,11,11,10,6,2,11,5
,11,1,8,0,10,0,14,8,11,12,8,12,7,10,7,12,7,15,12,15,2,10,0,10
,3,9,3,6,3,15,5,14,3,12,4,13,5,12,1,8,10,4,12,11,5,6,15,14
,14,0,2,2,0,3,8,6,0,11,0,15,15,13,13,1,15,3,13,5,14,7,0,7
,8,11,4,11,6,14,2,14,13,3,2,5,5,15,5,9,15,5,0,13,4,3,9,15
,5,8,15,9,12,4,1,15};
int stopp,d;

stopp=0;
  flicscreen = abuf;                    /* Set to visual screen */
					/* You must set this AFTER vga256(); */


      flic_mode = FLIC_DISK;  /*1=ems 0=disk*/

      if(music>0) loadandplay("data\\intro");  /*play song if music on*/
mon();
msetxy(319,199);
msetbounds(319,199,319,199);
    if (openflic ("graphics\\b.1") == FLIC_OK)    /* See if we opened file ok */

      do {
	gettime (&time1);             /* Find starting time of routine */
	status = nextframe ();                  /* Show frame of animation */
	if ((status != FLIC_OK))         /* && (status != FLIC_DONE))*/
	  break;                                /* Abort if error */
	/*delay (100);                   Delay proper amount */
       wretrace();
      /*	do{
	    gettime(&time2);
	    }while(wtimer(time1,time2)<5);
		 */
      } while (but==0);                      /* Continue until keypress */
	if(but==1)  {noclick();stopp=1;}

    /* NOTE: If you don't want the flic to loop, remove the check for
	     FLIC_DONE. This will abort playback when the animation is done */
	titlescreen=wnewblock(0,0,319,199);

    closeflic ();

	wputblock(0,0,titlescreen,0);
	wfreeblock(titlescreen);

	setlib("graphics\\credit.lib");

   do{
	if(but==1)  {noclick();stopp=1;}
    if(stopp==0){
	for (d=1;d<=4000;d++){
		delay(1);
		if(but==1)  {noclick();stopp=1;d=4001;}
	}
	if(but==1)  {noclick();stopp=1;}
	titlescreen=wloadpcx256("credit0.pcx",titlepalette);
	wsetpalette(0,255,titlepalette);
	wfade(titlescreen,disslve,1);
	wfreeblock(titlescreen);
	if(but==1)  {noclick();stopp=1;}
	}
    if(stopp==0){
	for (d=1;d<=6000;d++){
		delay(1);
		if(but==1)  {noclick();stopp=1;d=6001;}
	}

	if(but==1)  {noclick();stopp=1;}
	titlescreen=wloadpcx256("credit1.pcx",titlepalette);
	wsetpalette(0,255,titlepalette);
	wfade(titlescreen,disslve,1);
	wfreeblock(titlescreen);
	if(but==1)  {noclick();stopp=1;}
	}
    if(stopp==0){
	for (d=1;d<=6000;d++){
		delay(1);
		if(but==1)  {noclick();stopp=1;d=6001;}
	}
	if(but==1)  {noclick();stopp=1;}
	titlescreen=wloadpcx256("credit2.pcx",titlepalette);
	wsetpalette(0,255,titlepalette);
	wfade(titlescreen,disslve,3);
	wfreeblock(titlescreen);
	if(but==1)  {noclick();stopp=1;}
	}
    if(stopp==0){
	for (d=1;d<=6000;d++){
		delay(1);
		if(but==1)  {noclick();stopp=1;d=6001;}
	}
	if(but==1)  {noclick();stopp=1;}
	titlescreen=wloadpcx256("credit3.pcx",titlepalette);
	wsetpalette(0,255,titlepalette);
	wfade(titlescreen,disslve,3);
	wfreeblock(titlescreen);
	if(but==1) {noclick();stopp=1;}
	}
    } while(stopp==0);

moff();

    wfade_out(0,255,0,titlepalette);       /* Close current animation */
    if(music>0) midiStopOutput();
}


void menus()
{
int stopp;
stopp=0;

  wcls(0);
	wsetpalette(0,255,menupalette);
	wputblock(0,0,menub,0);


mon();      /*turn mouse on*/
	while(stopp==0){
		if(but==1){
			noclick();
			if((mx>81)&&(mx<238)&&(my>52)&&(my<69)){
							stopp=1;
							city=1;/*CITY*/
							check=1;
							}
			if((mx>81)&&(mx<238)&&(my>73)&&(my<95)){
							 dungeon=1; /*DUNGEON*/
							 stopp=1;
							 check=1;
							 }
			if((mx>81)&&(mx<238)&&(my>102)&&(my<120)){
							 world=1; /*WORLD*/
							 stopp=1;
							 }
			if((mx>81)&&(mx<238)&&(my>123)&&(my<147)){
							 utility=1; /*UTILITY*/
							 stopp=1;
							 check=1;
							 }
			if((mx>135)&&(mx<186)&&(my>170)&&(my<194)){
							 quitt=1; /*QUIT*/
							 stopp=1;
							 check=1;
							 }
		}
	}
moff();         /*turn mouse off*/
}

void reset480(void)
{
union REGS r;
	r.x.ax= 0x03;
	int86(0x10,&r,&r);
	r.x.ax= 0x1202;
	r.x.bx= 0x0030;
	int86(0x10,&r,&r);
}

void unregistered(void)
{
  clrscr();
  textcolor(9);
  gotoxy(1,1);
  cprintf("The ");
  textcolor(15);
  gotoxy(5,1);
  cprintf("Fantasy World Builder");
  textcolor(9);
  gotoxy(27,1);
  cprintf("took 1 year to create.  The goal was to create a");
  gotoxy(1,2);
  cprintf("computer aid that would quickly create gaming aids for gamemasters");
  gotoxy(1,3);
  cprintf("to use.  (many of us do not have the time to create from scratch.)");
  textcolor(10);
  gotoxy(1,5);
  cprintf("This program is released as ");
  textcolor(15);
  gotoxy(29,5);
  cprintf("SHAREWARE");
  textcolor(10);
  gotoxy(38,5);
  cprintf(".  You have ");
  textcolor(15);
  gotoxy(51,5);
  cprintf("30 days");
  textcolor(10);
  gotoxy(59,5);
  cprintf("to use and evaluate");
  gotoxy(1,6);
  cprintf("this product.  After the 30 days, if you find this program to be of");
  gotoxy(1,7);
  cprintf("use to you, please register your copy.");
  textcolor(12);
  gotoxy(1,9);
  cprintf("By registering you will receive the latest version of the software,");
  gotoxy(1,10);
  cprintf("printed instruction manual,and the latest version of the ");
  textcolor(15);
  gotoxy(58,10);
  cprintf("Fantasy War Simulator");
  textcolor(12);
  gotoxy(79,10);
  cprintf(",");
  gotoxy(1,11);
  cprintf("a useful tool to simulate large scale battles in a fantasy setting.");
  textcolor(13);
  gotoxy(1,12);
  cprintf("All software is shipped on 3 1/2 inch high-desity disks.");
  gotoxy(1,13);
  cprintf("         REGISTRATION COST:    $20.00 US dollars");
  gotoxy(1,14);
  cprintf("                              +$03.50 Shipping/Packaging");
  gotoxy(1,15);
  cprintf("                     TOTAL:    $23.50 US check,bank money order,");
  gotoxy(1,16);
  cprintf("                                      International bank money order");
  gotoxy(1,17);
  cprintf("Make payment to:   Perry Horner");
  gotoxy(1,18);
  cprintf("                   PO BOX 2282");
  gotoxy(1,19);
  cprintf("                   Peoria, AZ  85380-2282");
  gotoxy(1,20);
  cprintf("                   USA");
  gotoxy(1,22);
  textcolor(3);
  cprintf("Thank you for your interest in this program.  P_HORNER@ASU.EDU");
  delay(20000);
  textcolor(12);
  gotoxy(1,23);
  cprintf("                          Press any key");
  getch();
  reset480();
}

void main()
{
int useEMS,intstart;
  int ok,dotmatrix,mem_avail;
  int stopp,newmode;
  city=0;
  dungeon=0;
  world=0;
  utility=0;
  stopp=0;
  ok=0;
  check=0;

     if((fpie=fopen("data\\world.cfg","r"))!=NULL){;
	fscanf(fpie,"%d",&music);   /*1=music 0=no music*/
	fscanf(fpie,"%d",&dotmatrix);   /*0=laser 1=dotmatrix*/
	fclose(fpie);
       }
       else{ printf("No configuration found.  Run SETUP.EXE\n");
	     exit(0);
	    }
		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);
		}
	useEMS=wems_present();
	if(!useEMS) {
		printf("\nNo EMS memory available.  Check your memory manager\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);
		} */

	      /* Clear the screen */
	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);
		}

  if((fpie=fopen("temp\\mode.out","r"))==NULL){
		newmode=wgetmode();
		vga256();
		fclose(fpie);
		fpie=fopen("temp\\mode.out","w");
		fprintf(fpie,"%d\n",newmode);
		fclose(fpie);
      }
      else{ vga256();
	   }
  wcls(0);

	if(music>0) soundset();  /*set music*/

	fpie=fopen("data\\start.cfg","r");
	fscanf(fpie,"%d",&intstart);
	fclose(fpie);
	if(intstart==0){
			  intro();
			  fpie=fopen("data\\start.cfg","w");
			  fprintf(fpie,"%d",1);
			  fclose(fpie);
	} /*load introduction*/

	setlib("graphics\\start.lib");
	setpassword(" ");
	/*setpassword("orcus");*/
	warning=wloadpcx256("warning.pcx",warnpalette);
	menub=wloadpcx256("menu.pcx",menupalette);



msetbounds(0,0,319,199);
msetxy(100,100);

while(ok==0){
  stopp=0;
  menus();
  if(world==1){
	wcls(0);
	wsetpalette(0,255,warnpalette);
	wputblock(0,0,warning,0);
	mon();      /*turn mouse on*/
	while(stopp==0){
		if(but==1){
			noclick();
			if((mx>97)&&(mx<120)&&(my>165)&&(my<175)){
							stopp=1;
							ok=1;
							world=1;
							}
			if((mx>161)&&(mx<206)&&(my>165)&&(my<175)){
							 world=0;
							 stopp=1;
							 }
		}
	}
   moff();         /*turn mouse off*/
   } /*end if*/
   if(check==1) ok=1;
 }/*end while*/



    if(music>0) midiCloseDriver();

    wfreeblock(menub);
    wfreeblock(warning);

    mdeinit();

   wcls(0);

if(city==1)    execv("city",NULL);
if(dungeon==1) execv("dungeon",NULL);
if(world==1)  execv("world",NULL);
if(utility==1) execv("utility",NULL);


fpie=fopen("data\\start.cfg","w");
fprintf(fpie,"%d",0);
fclose(fpie);
    fpie=fopen("temp\\mode.out","r");
       fscanf(fpie,"%d",&oldmode);
       fclose(fpie);
       remove("temp\\mode.out");

    wsetmode(oldmode);
    reset480();
    /*unregistered();*/
exit(0);

}


