void Control_Read (int *trigger, Foreground *frgnd, int vabnbr, int *buttons);

//-------------------------------------
// What the fuck.
//-------------------------------------
static u_long _PadRead (PAD *p)
{
   u_short  data;
   u_char   id;
   short    i;

   for (i = 0; i < PADMAX; i++)
   {
      p->old[i] = p->data[i];

      if (p->buff[i][0] != 0)
         id = 0;
      else
         id = p->buff[i][1];

      p->Tbuf[i][0] = 0;

      switch (id)
      {
        case PADID_CON:
        case PADID_MOUSE:
        case PADID_GUN:  data = (u_short)((((p->buff[i][2] & 0xff) << 8)) |
                                 (p->buff[i][3]) & 0xff);
                          data = ~data & 0xffff;
                          p->mouse_x[i] = 0;
                          p->mouse_y[i] = 0;
                          break;
         default :        data = 0;
                          id = 0;
                          p->mouse_x[i] = 0;
                          p->mouse_y[i] = 0;
      }

      p->id[i] = id & 0xff;
      p->data[i] = data;

      data ^= p->old[i];
      data &= p->data[i];
      p->trig[i] = data;
   }

   return (p->data[0] & 0xffff) | (((p->data[1]) << 16) & 0xffff0000);
}

//-------------------------------------
// Initialize the fonts library
// TO BE REMOVED FOR THE REAL VERSION
//-------------------------------------
void InitPrint (void)
{
   int marde;

   FixAllocVram (960,256, 64,128, &marde);
   FntLoad (960, 256);
   SetDumpFnt (FntOpen (15, 25, 290, 190, 0, 1000));
}

//-------------------------------------
// Initialize a player
//-------------------------------------
void Init_Player (PLAYER *plr)
{
   plr->Score = 0;
   plr->BulletType = 0;
   plr->Bullets = 23;
   plr->Lifepnt = 0;
   plr->Counter = -120;
   plr->GunX = 160;
   plr->GunY = 120;
   plr->Grade = 0;
   plr->Shots = 0;
   plr->Hits = 0;
   plr->VictimsMiss = 0;
}

//-------------------------------------
// Initialize a player
//-------------------------------------
void Update_Player (PLAYER *plr)
{
   int reload[7] = {23,36,6,8,12,6,12};  // Nbr of bullets we have at reload

   if ((plr->Lifepnt == 0) && (plr->Counter == -120))
   {
      plr->Score = 0;
      plr->Lifepnt = 0;
      plr->Grade = 2;
      plr->Shots = 0;
      plr->Hits = 0;
      plr->VictimsMiss = 0;
      plr->BulletType = 0;
   }

   plr->Bullets = reload[plr->BulletType];
   plr->GunX = 160;
   plr->GunY = 120;
   plr->Counter = -120;
}

//-------------------------------------
// Initialize the gun library
//-------------------------------------
void Init_Gun_Library (void)
{
   StopGun ();
   ResetCallback ();
   InitPAD (&p.buff[0][0], PADBUFFLEN, &p.buff[1][0], PADBUFFLEN);
   InitGun (&p.buff[0][4], &p.buff[1][4], 1);
   SetGun (&p.buff[0][0], &p.buff[1][0]);
   StartPAD ();
   ChangeClearPAD (0);
   ResetGraph (0);
   SetGraphDebug (0);
   SetDispMask (1);
   GunCount = &p.buff[0][5];
   hv[0] = (HV_CNT *)&p.buff[0][6];
}

//-------------------------------------
// Play the shooting range intro, which
// is not a movie.
//-------------------------------------
PlayShootingRangeIntro (u_long memptr, DoubleBuffer *db, Foreground *frgnd)
{
   char *filename[2] = {"\\BACKGRND\\SHOOTING\\SHOOT1.BKD",
                        "\\BACKGRND\\SHOOTING\\SHOOT2.BKD"};
   Character *chars[2];
   Background bkd;
   int dummy;
   int counter = 0;
   int trigger = 0, buttons = 0;
   char meu1[] = {0,'S','h','o','o','t',' ','a','s',' ','m','a','n','y',' ',
                  't','a','r','g','e','t','s'};
   char meu2[] = {0,'i','n',' ','t','i','m','e',' ','a','s',' ','p','o','s',
                  's','i','b','l','e'};
   char replace;

   memptr = (u_long)((memptr + 3) & 0xFFFFFFFC);
   if ((frgnd->Plr1.Lifepnt > 0) && (frgnd->Plr2.Lifepnt > 0))
      bkd.Load (filename[1], &memptr);
   else
      bkd.Load (filename[0], &memptr);
   bkd.SetLocation (0,0);

   memptr = (u_long)((memptr + 3) & 0xFFFFFFFC);
   chars[0] = (Character *)memptr;
   memptr += sizeof (Character);

   chars[0]->Load ("\\SPRITES\\SHOOTING\\SHRNG.KRT", &memptr);

   chars[0]->Set_Location (322,120);
   chars[0]->StartAction (0, 0);
   chars[0]->SetDepth (12);
   chars[0]->SetCharacteristics (128);
   chars[1] = chars[0]->Clone_Character (1, &memptr);
   chars[1]->SetDepth (12);
   chars[1]->StartAction (1, 0);
   chars[1]->Set_Location (322 + 136, 120);
   chars[1]->SetCharacteristics (128);
   AddAttach (chars[1], chars[0], 136, 0);

   while (counter < 275)
   {
      FntPrint ("counter: %d\n", counter);

      Control_Read (&trigger, frgnd, -1, &buttons);
      if ((buttons & 1) || (trigger))
         counter = 900;
      buttons = 0;
      trigger = 0;

      bkd.Draw (db, 0);
      chars[0]->DrawNextFrame (0, 0, db, &dummy, &dummy, &dummy);
      chars[1]->DrawNextFrame (0, 0, db, &dummy, &dummy, &dummy);

      if (counter > 50)
      {
         if ((((counter - 50) % 4) == 0) && (((counter - 50) / 4) < 22))
         {
            replace = meu1[(counter - 50) / 4];
            meu1[(counter - 50) / 4] = meu1[(counter - 50) / 4 - 1];
            meu1[(counter - 50) / 4 - 1] = replace;
         }

         FontLeftOn (67, 170);
         SetFontColor (0);
         Print (meu1);
      }

      if (counter > 142)
      {
         if ((((counter - 142) % 4) == 0) && (((counter - 142) / 4) < 20))
         {
            replace = meu2[(counter - 142) / 4];
            meu2[(counter - 142) / 4] = meu2[(counter - 142) / 4 - 1];
            meu2[(counter - 142) / 4 - 1] = replace;
         }

         FontLeftOn (75, 190);
         SetFontColor (0);
         Print (meu2);
      }
      db->Swap (0);
      counter++;
   }

   //----------------------------------
   bkd.Clear ();
   chars[0]->Clear ();
   chars[1]->Clear ();
}
