#include <stdio.h>
#include <dos.h>
#include <math.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <conio.h>

FILE *stream;
char s1[80];
float renderdist,effectsrenderdist;
int videomode,rendermethod,cockpitrendermethod,smalldetails;
int lightsourcing,flames,sparks;
int complexity,starfield,planets,longlaser,debrisremoval,debris;
int drawcockpit,attachedobjects;
int jcenterx,jcentery,jmaxx,jmaxy;
int control,jsens,msens,fx,music,sampling,volume,difficulty;
struct REGPACK reg; // Used for the registers; specifically the mouse.

void writeoptions(void);
void writedetails(void);

void menu(void);
void data(void);

void main(void)
{
  printf("Setup SIMPLE (C) Spideractive Ltd.\n");
  stream=fopen("3DDETAIL.CFG","r");
  fscanf(stream , "%s %d\n" , &s1,&videomode);
  fscanf(stream , "%s %d\n" , &s1,&rendermethod);
  fscanf(stream , "%s %d\n" , &s1,&cockpitrendermethod);
  fscanf(stream , "%s %d\n" , &s1,&smalldetails);
  fscanf(stream , "%s %f\n" , &s1,&renderdist);
  fscanf(stream , "%s %f\n" , &s1,&effectsrenderdist);
  fscanf(stream , "%s %d\n" , &s1,&lightsourcing);
  fscanf(stream , "%s %d\n" , &s1,&flames);
  fscanf(stream , "%s %d\n" , &s1,&sparks);
  fscanf(stream , "%s %d\n" , &s1,&complexity);
  fscanf(stream , "%s %d\n" , &s1,&starfield);
  fscanf(stream , "%s %d\n" , &s1,&planets);
  fscanf(stream , "%s %d\n" , &s1,&longlaser);
  fscanf(stream , "%s %d\n" , &s1,&debrisremoval);
  fscanf(stream , "%s %d\n" , &s1,&debris);
  fscanf(stream , "%s %d\n" , &s1,&drawcockpit);
  fscanf(stream , "%s %d\n" , &s1,&attachedobjects);
  fclose(stream);
  stream=fopen("3DOPTION.CFG","r");
  fscanf(stream,"%s %d\n",&s1,&control);
  fscanf(stream,"%s %d\n",&s1,&jsens);
  fscanf(stream,"%s %d\n",&s1,&msens);
  fscanf(stream,"%s %d\n",&s1,&fx);
  fscanf(stream,"%s %d\n",&s1,&music);
  fscanf(stream,"%s %d\n",&s1,&sampling);
  fscanf(stream,"%s %d\n",&s1,&volume);
  fscanf(stream,"%s %d\n",&s1,&difficulty);
  fclose(stream);
  menu();
  writeoptions();
  printf("Setup SIMPLE (C) Spideractive Ltd.\n");
}

void menu(void)
{
 char k[1];     // For the keyboard
 static int highlight=1; // Current selection on the menubar
 int spare,n,m;    // Mouse x and y. Spare variables and counters.
 unsigned char st[80];   // Used for passing strings through to the other funcs
 char s1[80];

 textcolor(7);
 textbackground(0);  // White on black
 clrscr();
 textcolor(14);
 textbackground(1); // Yellow on blue
 cprintf("ษออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออป");
 cprintf("วฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤ S e c e s s i o n ฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤฤถ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("บ                                                                              บ");
 cprintf("ศออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออออผ");
 data();
 do {
  k[0]=254; k[1]=254;
  k[0]=getch();
  if (k[0]==0) k[1]=getch();
  strlwr(k);

        switch(k[0])
        {
          case '1':
            control++;
            if(control>1) control=0;
          break;
          case '2':
            if (jsens>5) jsens--;
          break;
          case '3':
            if (msens>5) msens--;
          break;
          case '4':
            if(fx==1) fx=0; else fx=1;
          break;
          case '5':
            if (music==1) music=0; else music=1;
          break;
          case '6':
            sampling++; if(sampling>2) sampling=0;
          break;
          case '7':
            if (volume>1) volume--;
          break;
          case '8':
            difficulty--;
            if (difficulty<0) difficulty=3;
          break;
        }

        switch(k[0])
        {
          case 'q':
            control--;
            if(control<0) control=1;
          break;
          case 'w':
            if (jsens<30) jsens++;
          break;
          case 'e':
            if (msens<30) msens++;
          break;
          case 'r':
            if (fx==1) fx=0; else fx=1;
          break;
          case 't':
            if (music==1) music=0; else music=1;
          break;
          case 'y':
            sampling--; if(sampling<0) sampling=2;
          break;
          case 'u':
            if (volume<10) volume++;
          break;
          case 'i':
            difficulty++;
            if (difficulty>3) difficulty=0;
          break;
        }

  data();

  if (k[1]==80) { // Up
   highlight=highlight+1;         // Move the bar
   if (highlight>14) highlight=1; // Check to see if highlight is on screen
  }
  if (k[1]==72) { // Down
   highlight=highlight-1;
   if (highlight<1) highlight=14;
  }
  if (k[1]==75) { // Left
   highlight=highlight-5;
   if (highlight<1) highlight=highlight+14;
  }
  if (k[1]==77) { // Right
   highlight=highlight+5;
   if (highlight>14) highlight=highlight-14;
  }

  if ((k[1]==82)|| (highlight==3)&&(k[0]==13))  { // Insert a record
  }

   if (reg.r_bx>0) delay(100);         // A mouse button delay loop.

 } while (!(k[0]==27)); // Escape is not pressed
 // End of the main loop.
 textbackground(0);
 textcolor(7);
 gotoxy(1,24); // The bottom of the screen
}

void writeoptions(void)
{
  stream=fopen("3DOPTION.CFG","w");
  fprintf(stream,"CONTROL: %d\n",control);
  fprintf(stream,"JOYSTICKSENSITIVITY: %d\n",jsens);
  fprintf(stream,"MOUSESENSITIVITY: %d\n",msens);
  fprintf(stream,"SOUNDEFFECTS: %d\n",fx);
  fprintf(stream,"MUSICALSCORE: %d\n",music);
  fprintf(stream,"SAMPLINGSPEED: %d\n",sampling);
  fprintf(stream,"VOLUME: %d\n",volume);
  fprintf(stream,"DIFFICULTY: %d\n",difficulty);
  fclose(stream);
}

void writedetails(void)
{
  stream=fopen("3DDETAIL.CFG","w");
  fprintf(stream,"VIDEOMODE: %d\n",videomode);
  fprintf(stream,"RENDERMETHOD: %d\n",rendermethod);
  fprintf(stream,"COCKPITRENDERMETHOD: %d\n",cockpitrendermethod);
  fprintf(stream,"EFFECTSRENDERMETHOD: %d\n",smalldetails);
  fprintf(stream,"RENDERDISTANCE: %f\n",renderdist);
  fprintf(stream,"EFFECTSRENDERDISTANCE: %f\n",effectsrenderdist);
  fprintf(stream,"LIGHTSOURCING: %d\n",lightsourcing);
  fprintf(stream,"PYROTECHNICS: %d\n",flames);
  fprintf(stream,"EXPLOSIONEFFECTS: %d\n",sparks);
  fprintf(stream,"EFFECTSCOMPLEXITY: %d\n",complexity);
  fprintf(stream,"STARFIELDSIZE: %d\n",starfield);
  fprintf(stream,"PLANETS: %d\n",planets);
  fprintf(stream,"LONGPLASMABEAMS: %d\n",longlaser);
  fprintf(stream,"DEBRISREVOBAL: %d\n",debrisremoval);
  fprintf(stream,"DEBRISFIELD: %d\n",debris);
  fprintf(stream,"DRAWCOCKPIT: %d\n",drawcockpit);
  fprintf(stream,"ATTACHEDOBJECTS: %d\n",attachedobjects);
  fclose(stream);
}

void data(void)
{
    textcolor(15);
    switch(control)
    {
      case 0: sprintf(s1,"KEYBOARD "); break;
      case 1: sprintf(s1,"JOYSTICK "); break;
      case 2: sprintf(s1,"MOUSE    "); break;
    }
    gotoxy(15,4);
    cprintf("1,Q:     CONTROL METHOD             : %s",s1);
    sprintf(s1,"%d PERCENT   ",(25-(jsens-5))*4 );
    gotoxy(15,5);
    cprintf("2,W:     JOYSTICK SENSITIVITY       : %s",s1);
    sprintf(s1,"%d PERCENT   ",(25-(msens-5))*4 );
    gotoxy(15,6);
    cprintf("3,E:     MOUSE SENSITIVITY          : %s",s1);
    if(fx==1) sprintf(s1,"ON "); else sprintf(s1,"OFF");
    gotoxy(15,7);
    cprintf("4,R:     SOUND EFFECTS              : %s",s1);
    if(music==1) sprintf(s1,"ON "); else sprintf(s1,"OFF");
    gotoxy(15,8);
    cprintf("5,T:     MUSICAL SCORE              : %s",s1);
    switch(sampling)
    {
      case 0: sprintf(s1,"11000 HZ"); break;
      case 1: sprintf(s1,"22000 HZ"); break;
      case 2: sprintf(s1,"8000 HZ "); break;
    }
    gotoxy(15,9);
    cprintf("6,Y:     SAMPLING SPEED             : %s",s1);
    sprintf(s1,"%d0 PERCENT  ",11-volume);
    gotoxy(15,10);
    cprintf("7,U:     VOLUME                     : %s",s1);

    if(difficulty==0) sprintf(s1,"NORMAL           ");
    else if(difficulty==1) sprintf(s1,"BETTER WEAPONS  ");
    else if(difficulty==2) sprintf(s1,"INEPT ENEMIES  ");
    else sprintf(s1,"CHILDSPLAY         ");
    gotoxy(15,11);
    cprintf("8,I:     DIFFICULTY LEVEL           : %s",s1);
}