#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <malloc.h>
//#include "lzssnone.inc"               // compression lzss for datas
#include "plusari.inc"                  // compression lzari for datas
#include "type.h"                       // definition de byte, word ...
#include "svga.h"                       // unite SVGA
#include "keyb.h"                       // handler keyboard
#include "Inits.c"                      // initialisations du Mag
#include "function.c"
#include "Rubs.c"


void MenuProg()
{ static int chxb,quitmp,i;
  chxb=4;
  do
  { if (!FadeOk) readGIF("MENU.GIF",Vtmpbuf,pal);       // charge le fond
    else { FadeZone2Black (pal,0,32);
           VClrbufUnder(Vtmpbuf,31);
         }
   if (GamMenu)  pal[16*3]=35;
   else pal[16*3]=TrueCol;
    readGIF("MENUPROG.GIF",Vbuffer,palt2);
    CopyBuf2BufUnder(Vbuffer,Vtmpbuf,16); 
    Selector(ProgPosXY[chxb*2],ProgPosXY[1+chxb*2],34,254);

    if (Vesa12Beuark) BufferCopyVesa12(Vtmpbuf);
    else VSwpscr(Vtmpbuf);            

   if (!FadeOk) FadeFBlack(pal);
   else { for (i=0; i<=2+31*3; i++) palt2[i]=0;  
          for (i=32*3; i<=768; i++) palt2[i]=pal[i];
          FadeP1ToP2(palt2,pal);
        }

    InitKey();
    do
     { TestZic(); TestGamMenu();
       quitmp=WasKey(KeyEsc);
            if (IsKey(KeyUp))   { if (chxb>1) chxb=Selection(ProgPosXY,chxb,chxb-1); }
       else if (IsKey(KeyDown)) { if (chxb<6) chxb=Selection(ProgPosXY,chxb,chxb+1); }
       else if (WasKey(KeyPad1)){ if (chxb!=1) chxb=Selection(ProgPosXY,chxb,1); }
       else if (WasKey(KeyPad2)){ if (chxb!=2) chxb=Selection(ProgPosXY,chxb,2); }
       else if (WasKey(KeyPad3)){ if (chxb!=3) chxb=Selection(ProgPosXY,chxb,3); }
       else if (WasKey(KeyPad4)){ if (chxb!=4) chxb=Selection(ProgPosXY,chxb,4); }
       else if (WasKey(KeyPad5)){ if (chxb!=5) chxb=Selection(ProgPosXY,chxb,5); }
       else if (WasKey(KeyPad6)){ if (chxb!=6) chxb=Selection(ProgPosXY,chxb,6); }
     } while (!WasKey(KeyEnter) && !WasKey(KeyPadEnter) && !quitmp);

   FadeOk=1;
    if (!quitmp)
          if (chxb==1) Progra("sparc.bin");
     else if (chxb==2) Progra("vesa2.bin");
     else if (chxb==3) Progra("asm.bin");
     else if (chxb==4) Progra("tunnel.bin");
     else if (chxb==5) Progra("lzw.bin");
     else if (chxb==6) Progra("pmode.bin");
   } while (!quitmp);
}

void MenuDelire()
{ static int chxb,quitmp,i;
  chxb=1;
  do
  { if (!FadeOk) readGIF("MENU.GIF",Vtmpbuf,pal);       // charge le fond
    else { FadeZone2Black (pal,0,32);
           VClrbufUnder(Vtmpbuf,31);
         }
   if (GamMenu)  pal[16*3]=35;
   else pal[16*3]=TrueCol;
    readGIF("MENUDELI.GIF",Vbuffer,palt2);
    CopyBuf2BufUnder(Vbuffer,Vtmpbuf,16); 
    Selector(DelirPosXY[chxb*2],DelirPosXY[1+chxb*2],34,254);

    if (Vesa12Beuark) BufferCopyVesa12(Vtmpbuf);
    else VSwpscr(Vtmpbuf);            

   if (!FadeOk) FadeFBlack(pal);
   else { for (i=0; i<=2+31*3; i++) palt2[i]=0;  
          for (i=32*3; i<=768; i++) palt2[i]=pal[i];
          FadeP1ToP2(palt2,pal);
        }

    InitKey();
    do
     { TestZic(); TestGamMenu();
       quitmp=WasKey(KeyEsc);
            if (IsKey(KeyUp))   { if (chxb>1) chxb=Selection(DelirPosXY,chxb,chxb-1); }
       else if (IsKey(KeyDown)) { if (chxb<6) chxb=Selection(DelirPosXY,chxb,chxb+1); }
       else if (WasKey(KeyPad1)){ if (chxb!=1) chxb=Selection(DelirPosXY,chxb,1); }
       else if (WasKey(KeyPad2)){ if (chxb!=2) chxb=Selection(DelirPosXY,chxb,2); }
       else if (WasKey(KeyPad3)){ if (chxb!=3) chxb=Selection(DelirPosXY,chxb,3); }
       else if (WasKey(KeyPad4)){ if (chxb!=4) chxb=Selection(DelirPosXY,chxb,4); }
       else if (WasKey(KeyPad5)){ if (chxb!=5) chxb=Selection(DelirPosXY,chxb,5); }
       else if (WasKey(KeyPad6)){ if (chxb!=6) chxb=Selection(DelirPosXY,chxb,6); }
     } while (!WasKey(KeyEnter) && !WasKey(KeyPadEnter) && !quitmp);

   FadeOk=1;
    if (!quitmp)
          if (chxb==1) Delire("delire1.bin");
     else if (chxb==2) Delire("delire2.bin");
     else if (chxb==3) Delire("gandalf.bin");
     else if (chxb==4) Delire("delire3.bin");
     else if (chxb==5) Delire("crepz.bin");
     else if (chxb==6) Delire("treguy.bin");
   } while (!quitmp);
}


void MenuParty()
{ static int chxb,quitmp,i;
  chxb=4;
  do
  { if (!FadeOk) readGIF("MENU.GIF",Vtmpbuf,pal);       // charge le fond
    else { FadeZone2Black (pal,0,32);
           VClrbufUnder(Vtmpbuf,31);
         }
   if (GamMenu)  pal[16*3]=35;
   else pal[16*3]=TrueCol;
    readGIF("MENUPART.GIF",Vbuffer,palt2);
    CopyBuf2BufUnder(Vbuffer,Vtmpbuf,16); 
    Selector(PartPosXY[chxb*2],PartPosXY[1+chxb*2],34,254);

    if (Vesa12Beuark) BufferCopyVesa12(Vtmpbuf);
    else VSwpscr(Vtmpbuf);            

   if (!FadeOk) FadeFBlack(pal);
   else { for (i=0; i<=2+31*3; i++) palt2[i]=0;  
          for (i=32*3; i<=768; i++) palt2[i]=pal[i];
          FadeP1ToP2(palt2,pal);
        }

    InitKey();
    do
     { TestZic(); TestGamMenu();
       quitmp=WasKey(KeyEsc);
            if (IsKey(KeyUp))   { if (chxb>1) chxb=Selection(PartPosXY,chxb,chxb-1); }
       else if (IsKey(KeyDown)) { if (chxb<5) chxb=Selection(PartPosXY,chxb,chxb+1); }
       else if (WasKey(KeyPad1)){ if (chxb!=1) chxb=Selection(PartPosXY,chxb,1); }
       else if (WasKey(KeyPad2)){ if (chxb!=2) chxb=Selection(PartPosXY,chxb,2); }
       else if (WasKey(KeyPad3)){ if (chxb!=3) chxb=Selection(PartPosXY,chxb,3); }
       else if (WasKey(KeyPad4)){ if (chxb!=4) chxb=Selection(PartPosXY,chxb,4); }
       else if (WasKey(KeyPad5)){ if (chxb!=5) chxb=Selection(PartPosXY,chxb,5); }
     } while (!WasKey(KeyEnter) && !WasKey(KeyPadEnter) && !quitmp);

   FadeOk=1;
    if (!quitmp)
          if (chxb==1) Reports("b5rep.bin");  
     else if (chxb==2) Reports("euskal.bin");  
     else if (chxb==3) Reports("netzone.bin");  
     else if (chxb==4) Reports("p2b5.bin");  
     else if (chxb==5) Reports("wired97.bin");  
   } while (!quitmp);
}


void MenuTrueLife()
{ static int chxb,quitmp,i;
  chxb=1;
  do
  { if (!FadeOk) readGIF("MENU.GIF",Vtmpbuf,pal);       // charge le fond
    else { FadeZone2Black (pal,0,32);
           VClrbufUnder(Vtmpbuf,31);
         }
   if (GamMenu)  pal[16*3]=35;
   else pal[16*3]=TrueCol;
    readGIF("MENULIFE.GIF",Vbuffer,palt2);
    CopyBuf2BufUnder(Vbuffer,Vtmpbuf,16); 
    Selector(TruLifPosXY[chxb*2],TruLifPosXY[1+chxb*2],34,254);

    if (Vesa12Beuark) BufferCopyVesa12(Vtmpbuf);
    else VSwpscr(Vtmpbuf);            

   if (!FadeOk) FadeFBlack(pal);
   else { for (i=0; i<=2+31*3; i++) palt2[i]=0;  
          for (i=32*3; i<=768; i++) palt2[i]=pal[i];
          FadeP1ToP2(palt2,pal);
        }

    InitKey();
    do
     { TestZic(); TestGamMenu();
       quitmp=WasKey(KeyEsc);
            if (IsKey(KeyUp))   { if (chxb>1) chxb=Selection(TruLifPosXY,chxb,chxb-1); }
       else if (IsKey(KeyDown)) { if (chxb<5) chxb=Selection(TruLifPosXY,chxb,chxb+1); }
       else if (WasKey(KeyPad1)){ if (chxb!=1) chxb=Selection(TruLifPosXY,chxb,1); }
       else if (WasKey(KeyPad2)){ if (chxb!=2) chxb=Selection(TruLifPosXY,chxb,2); }
       else if (WasKey(KeyPad3)){ if (chxb!=3) chxb=Selection(TruLifPosXY,chxb,3); }
       else if (WasKey(KeyPad4)){ if (chxb!=4) chxb=Selection(TruLifPosXY,chxb,4); }
       else if (WasKey(KeyPad5)){ if (chxb!=5) chxb=Selection(TruLifPosXY,chxb,5); }
     } while (!WasKey(KeyEnter) && !WasKey(KeyPadEnter) && !quitmp);

   FadeOk=1;
    if (!quitmp)
          if (chxb==1) TrueLife("scream.bin");  
     else if (chxb==2) TrueLife("bbs.bin");  
     else if (chxb==3) TrueLife("fantasy.bin");  
     else if (chxb==4) TrueLife("linux.bin");  
     else if (chxb==5) TrueLife("gil.bin");  
   } while (!quitmp);
}

void MenuC2G()
{ static int chxb,quitmp,i;
  chxb=4;
  do
  { if (!FadeOk) readGIF("MENU.GIF",Vtmpbuf,pal);       // charge le fond
    else { FadeZone2Black (pal,0,32);
           VClrbufUnder(Vtmpbuf,31);
         }
   if (GamMenu)  pal[16*3]=35;
   else pal[16*3]=TrueCol;
    readGIF("MENUC2G.GIF",Vbuffer,palt2);
    CopyBuf2BufUnder(Vbuffer,Vtmpbuf,16); 
    Selector(C2GPosXY[chxb*2],C2GPosXY[1+chxb*2],34,254);

    if (Vesa12Beuark) BufferCopyVesa12(Vtmpbuf);
    else VSwpscr(Vtmpbuf);            

   if (!FadeOk) FadeFBlack(pal);
   else { for (i=0; i<=2+31*3; i++) palt2[i]=0;  
          for (i=32*3; i<=768; i++) palt2[i]=pal[i];
          FadeP1ToP2(palt2,pal);
        }

    InitKey();
    do
     { TestZic(); TestGamMenu();
       quitmp=WasKey(KeyEsc);
            if (IsKey(KeyUp))   { if (chxb>1) chxb=Selection(C2GPosXY,chxb,chxb-1); }
       else if (IsKey(KeyDown)) { if (chxb<5) chxb=Selection(C2GPosXY,chxb,chxb+1); }
       else if (WasKey(KeyPad1)){ if (chxb!=1) chxb=Selection(C2GPosXY,chxb,1); }
       else if (WasKey(KeyPad2)){ if (chxb!=2) chxb=Selection(C2GPosXY,chxb,2); }
       else if (WasKey(KeyPad3)){ if (chxb!=3) chxb=Selection(C2GPosXY,chxb,3); }
       else if (WasKey(KeyPad4)){ if (chxb!=4) chxb=Selection(C2GPosXY,chxb,4); }
       else if (WasKey(KeyPad5)){ if (chxb!=5) chxb=Selection(C2GPosXY,chxb,5); }
     } while (!WasKey(KeyEnter) && !WasKey(KeyPadEnter) && !quitmp);

   FadeOk=1;
    if (!quitmp)
          if (chxb==1) CoupGueule("cdg03.bin");
     else if (chxb==2) CoupGueule("magic.bin");
     else if (chxb==3) CoupGueule("cdg01.bin");
     else if (chxb==4) CoupGueule("kvfoutez.bin");
     else if (chxb==5) CoupGueule("cdg02.bin");
   } while (!quitmp);

}


void menu()                      // Le MENU !
{ static int chx,i;
chx=1;
do
  { if (!FadeOk)
     { readGIF("MENU.GIF",Vtmpbuf,pal);       // charge le fond
       TrueCol=pal[16*3];
     }
    else { FadeZone2Black (pal,0,32);
           VClrbufUnder(Vtmpbuf,31);
         }
   if (GamMenu)  pal[16*3]=35;
   else pal[16*3]=TrueCol;
   readGIF("MENUA.GIF",Vbuffer,palt2);    // charge le menu
   CopyBuf2BufUnder(Vbuffer,Vtmpbuf,16);  // copie juste le menu
   Selector(PrincPosXY[chx*2],PrincPosXY[1+chx*2],34,254);

   if (Vesa12Beuark) BufferCopyVesa12(Vtmpbuf);
   else VSwpscr(Vtmpbuf);            

   if (!FadeOk) FadeFBlack(pal);
   else { for (i=0; i<=2+31*3; i++) palt2[i]=0;  
          for (i=32*3; i<=768; i++) palt2[i]=pal[i];
          FadeP1ToP2(palt2,pal);
        }

   InitKey();
   do
     { TestZic(); TestGamMenu();
           if (IsKey(KeyUp))   { if (chx>1) chx=Selection(PrincPosXY,chx,chx-1); }
      else if (IsKey(KeyDown)) { if (chx<7) chx=Selection(PrincPosXY,chx,chx+1); }
      else if (WasKey(KeyPad1)){ if (chx!=1) chx=Selection(PrincPosXY,chx,1); }
      else if (WasKey(KeyPad2)){ if (chx!=2) chx=Selection(PrincPosXY,chx,2); }
      else if (WasKey(KeyPad3)){ if (chx!=3) chx=Selection(PrincPosXY,chx,3); }
      else if (WasKey(KeyPad4)){ if (chx!=4) chx=Selection(PrincPosXY,chx,4); }
      else if (WasKey(KeyPad5)){ if (chx!=5) chx=Selection(PrincPosXY,chx,5); }
      else if (WasKey(KeyPad6)){ if (chx!=6) chx=Selection(PrincPosXY,chx,6); }
      else if (WasKey(KeyPad7)){ if (chx!=7) chx=Selection(PrincPosXY,chx,7); }
      else if (WasKey(KeyEsc)) { if (chx!=7) chx=Selection(PrincPosXY,chx,7); }
    } while (!WasKey(KeyEnter) && !WasKey(KeyPadEnter));

FadeOk=1;
  if (chx!=7)
   switch(chx)
    { case 1: Edito();          break;
      case 2: MenuDelire();     break;
      case 3: MenuTrueLife();   break;
      case 4: MenuProg();       break;
      case 5: MenuParty();      break;
      case 6: MenuC2G();        break;
    }



 } while (chx!=7);
}




int main(int argc, char *argv[])
{  static int chx2, chx3;
   int i,SSchk;

   argc = argc; argv = argv;         // Sinon, Watcom peut faire Warning ...
   EDarchive(argv[0]);       
//   EDarchive("defcon.dat");       

SSchk=1; Vesa12Beuark=0; WaitAtInit=0; ForceV2=0; hiddenEngaged=0;
DebugMode=0; Clip=0; Ascenseur=0; NotTestWin=0; NoSound=0;

 for(i=1; i<argc; i++)
  {      if (Compare (argv[i],"/Hidden")) hiddenEngaged=1;
    else if (Compare (argv[i],"/NoSound")) NoSound=1;
    else if (Compare (argv[i],"/A")) Ascenseur=1;
    else if (Compare (argv[i],"/B")) ForceV2=1;
    else if (Compare (argv[i],"/S")) SSchk=0; 
    else if (Compare (argv[i],"/O")) Vesa12Beuark=1;
    else if (Compare (argv[i],"/T")) NotTestWin=1;
    else if (Compare (argv[i],"/W")) WaitAtInit=1;
    else if (Compare (argv[i],"/D")) { DebugMode=1; Vesa12Beuark=1; WaitAtInit=1; Ascenseur=1;}
    else if (Compare (argv[i],"/H") || Compare (argv[i],"/?"))

 { printf("\n!DefCoN Mag: Issue One ! Final Version (V1.0)\n\n");
        printf("\t/A       Ascenseur Engaged!\n"
               "\t/B       Bypass \"Vesa 2.0 not Found!\"\n"
               "\t/H /?    This help screen\n"
               "\t/O       For Old Vesa Compatibility (No LFB.. beuark!)\n"
               "\t/S       Sound SetUp\n"
               "\t/T       Don't Test If Win present (So you want to reset?)\n"
               "\t/W       Wait At Start Up\n"
               "\t/NoSound Pas de son!\n");
//                /D       Debug Mode
//                /Hidden  Lance automatiquement la hidden part!
   return 1;
 }
  }

 if (SSchk) { if (!InitAll( 1)) return 1; }
 else       { if (!InitAll(22)) return 1; }


 if (Vesa12Beuark) OldVesaEngaged();

 TheBeginning();

 FadeOk=0;

 menu();


 TheEnd();
 if (hidden) HiddenPart();
 DeInitAll();

   printf("\n!DefCoN Mag: Issue One ! Final Version (V1.0)\n\n");
   printf("Pour tous problŠmes, contactez ArraKiS (arrakis@hol.fr)\n"
          "Pour vous renseigner sur les BugFix: http://www.mygale.org/~defcon\n");

  return 0;
}
