/**********************************/
/*  L Y N X                       */ 
/*  Vince Attack                  */ 
/*  Frederic Descharmes           */
/*  Octobre 2020                  */
/**********************************/
 
#include <stdlib.h>
#include <lynx.h>
#include <lynxlib.h>
#include "LoadFileTo.h"
#include "audio\audio.h"
#include "SCB.h"
#include "fonte.h"
#include "fontev.h"
#include "inc\fond.pal"
#include "inc\end.pal"

/* LYNX-specific #defines: */
#define JOY_DIRECTION		0xF0
#define JOY_RIGHT		      0x10
#define JOY_LEFT		      0x20
#define JOY_DOWN		      0x40
#define JOY_UP			      0x80

#define JOY_BUTTON		   0x03
#define BUTTON_OPTION1	   0x08
#define BUTTON_OPTION2	   0x04
#define BUTTON_INNER	      0x02
#define BUTTON_OUTER	      0x01

#define BUTTON_PAUSE	0x01

#define SCBPAL(a)   (*(uint *)((a)+19))

#define MAX_BLAGUES  5

unsigned char niveau[10][5];
unsigned char grille[10][5];
unsigned char SFX[10][5];
unsigned char BKPniveau[10][5];
unsigned char ligne[5];
unsigned char pile[3],BKPpile[3];
unsigned char jauge[7],BKPjauge[7];

char choix, fin;
unsigned char testJ, testB;
int chrono, BKPchrono;
char decalage, BKPdecalage;
char vitesseChrono,BKPvitesseChrono;
int scoreJeu,oldscore,BKPscore;
char x,y,BKPx,BKPy;
int i,j,k,r;
int KK, BKPKK, squalyl, godzil, godzilI, godzilJ;
char premierKK, bruni, chuteBruni, tempBruni[7],chutePenpen, folco,succes,zerosquare;
char effet, inner;
char tileset, BKPtileset;
char flagChute;

char couleur[7] = {1,11,5,7,13,15,3};
char txtFedora[250] = 
"Installation de Fedora 40.2812 en cours.^Desarmement du 65C02.....^Configuration du noyau......^Reeducation de Suzy et Mickey...........^Interconnexion des bus et des trams...^Rebootez la console pour redemarrer.&";
char txtBlague[600]; 

extern void LoadFileTo();

// assembler vertical retrace syncronisation routine
void Vsync() 
{
#asm
vretrace:
	lda $fd0a
	bne vretrace
#endasm
}

void AfficheSCB(ix, iy, iCTL0, idata, iSCB)
int ix, iy,iCTL0
char *idata
char *iSCB
{
   SCBX(iSCB) = ix;
   SCBY(iSCB) = iy;
   SCBCTL0(iSCB) = iCTL0;
   SCBDATA(iSCB) = idata;
   DrawSprite(iSCB);
}

void Aff5ch(c, x, y, col)
int c;
unsigned char x, y, col;
{
        AffCarV(c/10000+16,x,y,col);
        AffCarV((c%10000)/1000+16,x,y+4,col);
        AffCarV((c%1000)/100+16,x,y+8,col);
        AffCarV((c%100)/10+16,x,y+12,col);
        AffCarV((c%10)+16,x,y+16,col);      
}


void AffCar(c,xx,yy,col)
int xx,yy,c;
char col;
{
   SCBPAL(SCBtf)=col;
   AfficheSCB(xx,yy,192,f[c],SCBtf);
}

void AffCarV(c,xx,yy,col)
int xx,yy,c;
char col;
{
   SCBPAL(SCBt)=col;
   AfficheSCB(xx,yy,199,v[c],SCBt);
}

int AffPave(max)
int max;
{
   int a,xt,yt;

   xt=0;
   yt=0;
   i=0;

   while ((txtFedora[i] != '&') && (i < max))
   {
      if (txtFedora[i] == '^')
      {
            xt=0;
            yt+=8;
      }
      else if (txtFedora[i] > 31)
      {
         a = txtFedora[i] - ' ';
         AffCar(a,xt,yt,9);
         xt+=4;
      }
      i++;
   }
   if (txtFedora[i] == '&')
      return 10000;
   else
      return max+1;
}

void AffBlague()
{
   int a,xt,yt;

   xt=153;
   yt=1;
   a = random()%MAX_BLAGUES;

   for (i=0;i<600;i++)
      txtBlague[i] = 0;
   LoadFileTo(72+a,txtBlague);
   i=0;

   while (txtBlague[i] != 0)
   {
      if (txtBlague[i] == 13)
      {
            yt=1;
            xt-=7;
      }
      else if (txtBlague[i] > 31)
      {
         a = txtBlague[i] - ' ';
         AffCarV(a,xt,yt,9);
         yt+=4;
      }
      i++;
   }
}
/*********************/
/* wait for a button */
/*********************/
void WaitButton()
{
   while (joystick);
   while (joystick != BUTTON_OUTER)
	   Audio_Play();
   while (joystick);
}


void loadLevel()
{

   SCBNEXT(SCBligne) = &SCB[0][0];
   for (i = 0; i < 5; i++)
   {
      bcopy(&spr0[0], &SCB[i][0], 11);  // src, dest, count
      SCBY(&SCB[i][0]) = 1+(i<<4);
      SCBNEXT(&SCB[i][0]) = &SCB[i+1][0];
   }
   SCBNEXT(&SCB[4][0]) = 0;

   x = 0;
   y = 9;
   for (i=0;i<10;i++)
      for (j=0;j<5;j++)
      {
         niveau[i][j] = 0;
         SFX[i][j] = 0;
         ligne[j] = (random())%6+1;
      }   
   for (k=0;k<7-choix*2;k++)
      nouvelleLigne();
}

void nouvelleLigne()
{
   for (j=0;j<5;j++)
      if (niveau[0][j] > 0)
         fin = 0;

   for (i=0;i<9;i++)
      for (j=0;j<5;j++)
      {
         niveau[i][j] = niveau[i+1][j];
      }   
   for (j=0;j<5;j++)
   {
      niveau[9][j] = ligne[j];
      ligne[j] = (random())%6+1;
   }   
      
   chrono = 160;
   decalage = 0;
}

void chuteGrille()
{
   char e;
   flagChute=0;
   for (i=8;i>=0;i--)
   {
      for (j=0;j<5;j++)
      {
         e = niveau[i][j]%100;
         if ((e > 0) && (e < 7))
         {
            if (niveau[i+1][j] == 0)
            {
               niveau[i+1][j] = 100+e;
               niveau[i][j] = 0;
               flagChute=1;
            }
            else
            {
               niveau[i][j] = e;
            }       
         }
         else if (e == 7)
         {
            if ((niveau[i+1][j] == 0) && (niveau[i+1][j+1] == 0))
            {
               niveau[i+1][j]   =  7;
               niveau[i+1][j+1] = niveau[i][j+1] + 10*chutePenpen;
               niveau[i][j]     = niveau[i-1][j] + 10*chutePenpen;
               niveau[i][j+1]   = niveau[i-1][j+1] + 10*chutePenpen;
               niveau[i-1][j]   =  0;
               niveau[i-1][j+1] =  0;
               flagChute=1;
            }
            else
            {
               chutePenpen = 1;
               if (niveau[i][j+1] > 30)
               {
                  niveau[i-1][j]   = 1+(random()%6);
                  niveau[i-1][j+1] = 1+(random()%6);
                  niveau[i][j]     = 1+(random()%6);
                  niveau[i][j+1]   = 1+(random()%6);                  
                  SFX[i-1][j]   = 10;
                  SFX[i-1][j+1] = 10;
                  SFX[i][j]     = 10;
                  SFX[i][j+1]   = 10;                  
                  flagChute=1;
               }
            }            
         }
         else if (e == 8)
         {
            for (k=0;k<5;k++)
            {
               if (niveau[i+1][k] != 0)
                  k = 10;
            }
            if (k < 10)
            {
               for (k=0;k<5;k++)
               {
                  niveau[i+1][k] = niveau[i][k];
                  if ((k > 1) && (chuteBruni == 1))
                     niveau[i+1][k] += 10;              
                  niveau[i][k] = 0;
                  flagChute=1;
               }
            }
            else
            {
               chuteBruni = 1;
               if (niveau[i][2] > 30)
               {
                  for (k=0;k<5;k++)
                  {
                     SFX[i][k] = 8;
                     niveau[i][k] = 1+(random()%6);
                     flagChute=1;
                  }
               }
            }            
         }
      }  
   } 
   for (j=0;j<5;j++)
   {
      e = niveau[9][j]%100;
      if ((e > 0) && (e < 7))
         niveau[9][j] = e;     
   }
}

void addPile(v)
char v;
{
   if (choix != 3)      // pas d'effets en mode Easy
   {
      if (pile[0] == 0)
      {
         pile[0]=v;
         SCBDATA(SCBpile0) = rr[v-1];
      }
      else if (pile[1] == 0)
      {
         pile[1]=v;
         SCBDATA(SCBpile1) = rr[v-1];
      }
      else if (pile[2] == 0)
      {
         pile[2]=v;
         SCBDATA(SCBpile2) = rr[v-1];
      }
      else 
      {
         pile[0] = pile [1];
         pile[1] = pile [2];
         pile[2] = v;
         SCBDATA(SCBpile0) = rr[pile[0]-1];
         SCBDATA(SCBpile1) = rr[pile[1]-1];
         SCBDATA(SCBpile2) = rr[v-1];
      }
   }
}

void testGrille()
{
   bcopy(&niveau[0][0], &grille[0][0], 50);

   oldscore = scoreJeu;

   for (i=0;i<10;i++)
   {
      for (j=0;j<5;j++)
      {
         r = grille[i][j];
         if ((j<3) && (r > 0) && (r < 7))
         {
            if ((r == grille[i][j+1]) && (r == grille[i][j+2]))
            {
               scoreJeu+=5*r;
               jauge[r]++;
               niveau[i][j]   = 0;
               niveau[i][j+1] = 0;
               niveau[i][j+2] = 0;
               SFX[i][j]   =  8;
               SFX[i][j+1] =  8;
               SFX[i][j+2] =  8;
               if ((r==1) && (KK == 20000))
                  addPile(9);
               if ((r==6) && (folco == 2))
                  addPile(9);
               if (choix == 1)
               {
                  if (scoreJeu > 100)
                  {
                     if (scoreJeu%100 == 0)
                        addPile(7);
                     if ((scoreJeu < 1000) && (scoreJeu%111 == 0))
                        addPile(7);
                     else if ((scoreJeu < 10000) && (scoreJeu%1111 == 0))
                        addPile(7);
                  }
               }
            }
         }
         if ((i<8) && (r > 0) && (r < 7))
         {
            if ((r == grille[i+1][j]) && (r == grille[i+2][j]))
            {
               scoreJeu+=3*r;
               jauge[r]++;
               niveau[i][j]   = 0;
               niveau[i+1][j] = 0;
               niveau[i+2][j] = 0;
               SFX[i][j]   =  8;
               SFX[i+1][j] =  8;
               SFX[i+2][j] =  8;
               if ((r==1) && (KK == 20000))
                  addPile(9);
               if ((r==6) && (folco == 2))
                  addPile(9);
               if (choix == 1)
               {
                  if (scoreJeu > 100)
                  {
                     if (scoreJeu%100 == 0)
                        addPile(7);
                     if ((scoreJeu < 1000) && (scoreJeu%111 == 0))
                        addPile(7);
                     else if ((scoreJeu < 10000) && (scoreJeu%1111 == 0))
                        addPile(7);
                  }
               }
            }
         }
      } 
   }

   if (choix == 2)
   {
      if ((scoreJeu/100) > (oldscore/100))
         addPile(7);
   }

}


void loadTileSet(numero)
char numero;
{
   tileset = numero;
   if ((numero == 0) && (zerosquare == 1))
      LoadFileTo(6,fond);
   else
      LoadFileTo(3+numero,fond);
   for (i=0;i<9;i++)
   {
      LoadFileTo(12+18*(numero)+i,rr[i]);
      LoadFileTo(21+18*(numero)+i,ll[i]);
   }
   LoadFileTo(66+numero,ptPen);
   LoadFileTo(69+numero,barreB);
}

char gereJoystick()
{
   if (joystick & JOY_DIRECTION)
      return joystick;
   else
      return 0;
}

char gereBoutons()
{
   if (joystick & JOY_BUTTON)
      return joystick;
   else
      return 0;
}

char attack()
{
   loadTileSet(0);
   fin = 1;
   SCBX(SCBf) = 0;
   SCBY(SCBf) = 0;
   SCBDATA(SCBf) = fond;
   decalage = 0;
   KK = 0;
      // = 0        => valeur initiale
      // < 10000    => installation Fedora en cours
      // = 20000    => mode Fedora en cours
      // = 15000    => mode normal
   premierKK = 0;
   squalyl = 0;
   godzil = 0;
   godzilJ = 0;
   folco = 0;
   bruni = 0;
   chuteBruni = 0;
   chutePenpen = 0;
   vitesseChrono=2;
   zerosquare = 0;

   SCBDATA(SCBpile0) = vide;
   SCBDATA(SCBpile1) = vide;
   SCBDATA(SCBpile2) = vide;
   for (i=0;i<3;i++)
      pile[i]=0;


   for (i=0;i<7;i++)
   {
      jauge[i]=0;
      tempBruni[i]=0;
   }

   while (fin > 0)
   {
      if (KK == 10000)           // Reboot pour switch en Fedora mode
      {
         if ((joystick & BUTTON_OPTION1) && (switches & BUTTON_PAUSE))
         {
            while (switches & BUTTON_PAUSE);
            KK = 20000;
            loadTileSet(1);
          }
      }
      else if (effet == 5)       // Blague de Vince
      {
         effet = 0;
         DrawFBox(0,0,160,102,1);
         AffBlague();
         Vsync();
         SwapBuffers();
         WaitButton();
      }
      else
      {
         DrawSprite(SCBf);
         testJ=0;
         testJ=gereJoystick(); 
         testB=0;
         testB=gereBoutons();         

         if (decalage == 0)
         {
            if (pile[0] > 0)
            {
               succes = 0;
               switch (pile[0])
               {
                  case 1:           
                     if (choix == 1)      // Fedora Mode
                     {
                        if (premierKK == 0)
                        {
                           KK = 1;
                           premierKK = 1;
                        }
                        else
                           KK=10000;
                     }
                     else                 // fait apparaitre une ligne de blocs en haut
                     {
                        for (j=0;j<5;j++)
                        {
                           if (niveau[0][j] == 0)
                              niveau[0][j] = (random())%6+1;     
                        }
                     }
                     
                     succes = 1;
                     break;
                  case 2:                 // Squalyl : copie ligne du bas sur future ligne
                     squalyl = 80;
                     succes = 1;
                     break;
                  case 3:                 // Godzil mode
                     succes = 1;
                     godzil=240;
                     godzilJ=1+(random()%4);
                     break;
                  case 4:                 // Zeph mode : 2 blocs chutent sur 1 bloc identique
                     for (i=0;i<5;i++)
                     {
                        k = (random())%5;
                        if ((niveau[0][k] == 0) && (niveau[1][k] == 0))
                        {
                           for (j=2;j<10;j++)
                           {
                              if (niveau[j][k] > 0)
                              {
                                 if (niveau[j][k] < 7)
                                    j = 15;
                                 else
                                    j = 20;
                              }
                           }
                           if ((j > 10) && (j < 20))
                              i = 10;
                        }
                     }
                     if (i > 10)
                     {
                        for (j=2;j<10;j++)
                        {
                           if (niveau[j][k] > 0)
                           {
                              niveau[0][k]=niveau[j][k];
                              niveau[1][k]=niveau[j][k];
                              j = 10;
                           }
                        }
                     }
                     succes = 1;
                     break;
                  case 5:           
                     if (choix == 1)      // yn Vince mode : affiche une blague
                        effet = 5;
                     else                 // détruit tous les blocs d'un même type
                     {
                        k = (random())%6+1;
                        for (i=0;i<10;i++)
                        {
                           for (j=0;j<5;j++)
                           {
                              if ((niveau[i][j])%10 == k)
                              {
                                 niveau[i][j] = 0;
                                 SFX[i][j]   = 8;
                              }
                           }
                        }
                     }
                     succes = 1;
                     break;
                  case 6:                 // Folco mode : passage en mode affichage niveaux de gris
                     folco = 2;
                     loadTileSet(2);
                     succes = 1;
                     break;
                  case 7:                 // Penpen mode : affichage 'un bloc 2x2
                     k=random()%4;
                     chutePenpen = 0;
                     if ((niveau[0][k] == 0) && (niveau[0][k+1] == 0) && (niveau[1][k] == 0) && (niveau[1][k+1] == 0))
                     {
                        niveau[1][k]   =  7;
                        niveau[1][k+1] = 17;
                        niveau[0][k]   = 17;
                        niveau[0][k+1] = 17;
                        succes = 1;
                     }
                     break;
                  case 8:                 // Brunni mode : barre Brunni
                     for (j=0;j<5;j++)
                        if (niveau[0][j] > 0)
                           j=10;
                     
                     if (j<10)                           
                     {
                        if (vitesseChrono < 10)
                           vitesseChrono+=2;
                        niveau[0][0] = 8;
                        for (j=1;j<5;j++)
                           niveau[0][j] = 18;
                        succes = 1;
                     }
                     break;
                  case 9:                 // Normal mode : retour à la normale
                     folco = 0;
                     KK=15000;
                     loadTileSet(0);
                     succes = 1;
                     break;
               }
               if (succes == 1)
               {
                  flagChute = 1;
                  pile[0] = pile [1];
                  pile[1] = pile [2];
                  pile[2] = 0;
                  if (pile[0] > 0)
                     SCBDATA(SCBpile0) = rr[pile[0]-1];
                  else
                     SCBDATA(SCBpile0) = vide;
                  if (pile[1] > 0)
                     SCBDATA(SCBpile1) = rr[pile[1]-1];
                  else
                     SCBDATA(SCBpile1) = vide;
                  SCBDATA(SCBpile2) = vide;
               }
            }      
         }

         for (i=0;i<10;i++)
         {
            if (testJ == 0)
               testJ=gereJoystick();         
            if (testB == 0)
               testB=gereBoutons();         
            k = 144-(i<<4) + decalage;
            for (j=0;j<5;j++)
            {
               r = niveau[i][j]%100;
               SCBX(&SCB[j][0]) = k;
               if ((r > 0) && (r < 10))
               {
                  switch (r)
                  {
                     case 7 :
                        SCBDATA(&SCB[j][0]) = ptPen;
                        break;
                     case 8 :
                        SCBDATA(&SCB[j][0]) = barreB;
                        break;
                     default :                           
                        SCBDATA(&SCB[j][0]) = rr[r-1];
                  }
               }
               else
               {
                  SCBDATA(&SCB[j][0]) = vide;
               }
               
               r = SFX[i][j];
               SCBX(SCB1) = k;
               if (r > 0)
               {
                  SFX[i][j]--;
                  if (KK != 20000) 
                  {
                     SCBY(SCB1) = 1+(j<<4);
                     SCBDATA(SCB1) = explosion[r];
                     DrawSprite(SCB1);
                  }
               }
            }
            DrawSprite(SCBligne);            
         }

         if (testJ == 0)
            testJ=gereJoystick();         
         if (testB == 0)
            testB=gereBoutons();         
         k=decalage-16;
         for (j=0;j<5;j++)
         {
            r = ligne[j]-1;
            SCBX(&SCB[j][0]) = k;
            SCBDATA(&SCB[j][0]) = ll[r];
            if (squalyl == 72-(j<<4))
            {
               if (niveau[9][j] > 0)
                  ligne[j] = niveau[9][j];
               if (j == 4)
                  squalyl = 0;
            }
         }
         DrawSprite(SCBligne);            

         if (testJ == 0)
            gereJoystick();         
         if (testB == 0)
            testB=gereBoutons();         

         if (choix != 3)
         {
            DrawSprite(SCBpile0);
            for (i=1;i<7;i++)
            {
               DrawFBox(1,81+3*i,jauge[i]<<2,2,couleur[i]);
               if (tempBruni[i] == 1)
                  DrawFBox(25,81+3*i,2,2,couleur[i]);
            }

            if ((KK > 0) & (KK < 10000))
               KK=AffPave(KK);
            
            if ((squalyl > 0) && (squalyl < 100))
            {
                  SCBY(SCB1) = (80-squalyl);
                  SCBX(SCB1) =  0;
                  SCBDATA(SCB1) = s000004;
                  DrawSprite(SCB1);
                  squalyl-=4;
            }
            if (godzil > 0)
            {
                  SCBX(SCB1) =  godzil;
                  SCBY(SCB1) = godzilJ<<4;
                  SCBDATA(SCB1) = spgodzil;
                  godzil-=4;
                  if ((godzil<160) && ((godzil%16)==0))
                  {
                     godzilI = 10 - (godzil>>4);
                     k = niveau[godzilI][godzilJ];
                     if ((k<10) && (niveau[godzilI][godzilJ-1] < 10))
                     {
                        niveau[godzilI][godzilJ] = niveau[godzilI][godzilJ-1];
                        niveau[godzilI][godzilJ-1] = k;
                     }
                  }
                  DrawSprite(SCB1);
            }
         }

         SCBX(SCB1) = 144-(y<<4) + decalage;
         SCBY(SCB1) = 1+(x<<4);
         SCBDATA(SCB1) = curseur;
         DrawSprite(SCB1);


         if (testJ == 0)
            gereJoystick();         
         if (testB == 0)
            testB=gereBoutons();         

//         if (flagChute == 1)
            chuteGrille();

         if (testJ == 0)
            testJ=gereJoystick();         
         if (testB == 0)
            testB=gereBoutons();         

         if (decalage%8 == 1)
         {
            testGrille();
            flagChute = 1;
         }
         
         if (testJ == 0)
            testJ=gereJoystick();         
         if (testB == 0)
            testB=gereBoutons();         
                     
         chrono-=vitesseChrono;
         decalage = (160-chrono)/10;      
         if (chrono <= 0)
         {
            nouvelleLigne();
            if (y > 0)
               y--;
            if (fin == 0)
            {
               if (zerosquare == 1)
               {
                  fin = 1;    // sauve la partie 1 fois
                  SCBX(SCB1)=80;
                  SCBY(SCB1)=51;
                  SCBDATA(SCB1)=zsquare;
                  k=1;
                  for (i=256;i<4096;i+=k)
                  {
                     SCBHS(SCB1) = i;
                     SCBVS(SCB1) = i;
                     DrawSprite(SCB1);
                     Vsync();
                     SwapBuffers();
                     Audio_Play();
                     if (k<32)
                        k++;
                  }
                  SCBHS(SCB1) = 256;
                  SCBVS(SCB1) = 256;

                  flagChute = 1;
                  zerosquare = 2;
                  loadTileSet(BKPtileset);
                  decalage = BKPdecalage;
                  vitesseChrono = BKPvitesseChrono;
                  chrono = BKPchrono;
                  scoreJeu = BKPscore;
                  KK = BKPKK;
                  x=BKPx;
                  y=BKPy;
                  bcopy(&BKPjauge[0],&jauge[0],7);
                  bcopy(&BKPniveau[0][0], &niveau[0][0], 50);
                  bcopy(&BKPpile[0],&pile[0],3);
                  if (pile[0] > 0)
                     SCBDATA(SCBpile0) = rr[pile[0]-1];
                  else
                     SCBDATA(SCBpile0) = vide;
                  if (pile[1] > 0)
                     SCBDATA(SCBpile1) = rr[pile[1]-1];
                  else
                     SCBDATA(SCBpile1) = vide;
                  if (pile[2] > 0)
                     SCBDATA(SCBpile2) = rr[pile[2]-1];
                  else
                     SCBDATA(SCBpile2) = vide;
               }
               else
               {
                  SCBY(SCB1) = 68;
                  SCBX(SCB1) = 137;
                  SCBDATA(SCB1) = score;
                  for (inner=16;inner>=0;inner--)
                  {
                     DrawSprite(SCBf);
                     for (i=9;i>=0;i--)
                     {
                        k = (9-i)*inner-(16-inner);
                        for (j=0;j<5;j++)
                        {
                           r = niveau[i][j]%100;
                           SCBX(&SCB[j][0]) = k;
                           if ((r > 0) && (r < 10))
                           {
                              switch (r)
                              {
                                 case 7 :
                                    SCBDATA(&SCB[j][0]) = ptPen;
                                    break;
                                 case 8 :
                                    SCBDATA(&SCB[j][0]) = barreB;
                                    break;
                                 default :                           
                                    SCBDATA(&SCB[j][0]) = rr[r-1];
                              }
                           }
                           else
                           {
                              SCBDATA(&SCB[j][0]) = vide;
                           }
                        }
                        DrawSprite(SCBligne);            
                     }
                     DrawSprite(SCB1);
                     Aff5ch(scoreJeu,141,76,9);
                     Vsync();
                     SwapBuffers();
                     Audio_Play();
                  }
               }
            }
         }
         if (testJ == 0)
            gereJoystick();         
         if (testB == 0)
            testB=gereBoutons();         

         bruni = 0;
         for (i=1;i<7;i++)
         {
            bruni += tempBruni[i];
            if (jauge[i] >= 5)
            {
               addPile(i);
               tempBruni[i] = 1;
               jauge[i] = 0;
            }
         }
         if (bruni == 6)
         {
            addPile(8);
            chuteBruni = 0;
            for (j=1;j<7;j++)
               tempBruni[j] = 0;
         }

         AfficheSCB(137,68,199,score,SCB1);
         Aff5ch(scoreJeu,141,76,9);

         if (KK == 10000)
            AfficheSCB(100,8,199,fedora,SCB1);
   

         if (testJ == 0)
            gereJoystick();         
         if (testB == 0)
            testB=gereBoutons();         

         if ((testJ & JOY_LEFT) && (y<9))
            y++;
         if ((testJ & JOY_RIGHT) && (y>0))
            y--;
         if ((testJ & JOY_DOWN) && (x<3))
            x++;
         if ((testJ & JOY_UP) && (x>0))
            x--;

         if ((testB & BUTTON_OUTER))
         {
            chrono -= 10;
            decalage--;
            if (decalage<0)
               decalage=0;
         }      
         if ((testB & BUTTON_INNER) && (inner == 0))
         {
            r = niveau[y][x];
            if ((r<7) && (niveau[y][x+1]<7))
            {
               niveau[y][x] = niveau[y][x+1];
               niveau[y][x+1] = r;
               flagChute = 1;
            }
            inner = 1;
         }
         if (!(testB & BUTTON_INNER) && (inner == 1))
            inner = 0;

         if (joystick & BUTTON_OPTION1) 
         {
            if (zerosquare == 0)
            {
               zerosquare = 1;
               if (tileset == 0)
                  LoadFileTo(6,fond);
               BKPdecalage = decalage;
               BKPvitesseChrono = vitesseChrono;
               BKPchrono = chrono;
               BKPscore = scoreJeu;
               BKPKK = KK;
               BKPx=x;
               BKPy=y;
               BKPtileset = tileset;
               bcopy(&jauge[0],&BKPjauge[0],7);
               bcopy(&niveau[0][0], &BKPniveau[0][0], 50);
               bcopy(&pile[0],&BKPpile[0],3);
            }
         }

         if (switches & BUTTON_PAUSE)
         {
            while (switches & BUTTON_PAUSE);
            LoadFileTo(9,fond);
            DrawSprite(SCBf);
            Vsync();
            SwapBuffers();
            while (!joystick)
               Audio_Play;
            loadTileSet(tileset);
         }

         Vsync();
         SwapBuffers();
		   Audio_Play();
      }
   }

   WaitButton();
   LoadFileTo(11,fond);
   DrawSprite(SCBf);
   Aff5ch(scoreJeu,10,50,9);
   Vsync();
   SetRGB(pal_end);
   SwapBuffers();
   WaitButton();
}


void credits()
{
   fin=0;
   LoadFileTo(10,fond);
   while (fin == 0)
   {
      DrawSprite(SCBf);

      Vsync();
      SwapBuffers();

      while(!joystick)
         r=random();               

      if (joystick & BUTTON_INNER)
         fin=1;
      if (joystick & BUTTON_OUTER)
         fin=1;
      while(joystick);
   }
}

void Menu()
{
   fin=0;
   LoadFileTo(2,fond);
   SCBX(SCBf) = 0;
   SCBY(SCBf) = 0;
   SCBDATA(SCBf) = fond;
   SetRGB(pal);
   while (fin == 0)
   {
      DrawSprite(SCBf);

      AfficheSCB(21+22*choix,1,199,point,SCB1);

      Vsync();
      SwapBuffers();

      while(!joystick)
         r=random();               
      if (joystick & JOY_RIGHT)
         choix = (choix+1)%4;
      if (joystick & JOY_LEFT)
         choix = (choix+3)%4;

      if (joystick & BUTTON_INNER)
         fin=1;
      if (joystick & BUTTON_OUTER)
         fin=1;
      while(joystick);
   }
}

/**************************************************************************
**                                                                       **
**                                                                       **
**************************************************************************/
main() 
{
   InitIRQ();
   Audio_Init();
   CLI;

   SetBuffers(SCREEN, RENDER,0);

   choix = 1;
   for (;;)
   {
      Menu();
      if (choix == 0)
         credits();
      else
      {
         scoreJeu = 0;
         loadLevel();
         attack();
      }
   }
}
