#define GUN1 (_PadRead (&p) & 0xFFFF)
#define GUN2 (_PadRead (&p) >> 16)
int         x1;
int         x2;
int         x3;
int         diff[3];

//-------------------------------------
// Reload a player's gun
//-------------------------------------
inline void ReloadPlrGun (PLAYER *plr, int vabnbr)
{
   int reload[7] = {23,36,6,8,12,6,12};  // Nbr of bullets we have at reload

   if (((plr->Lifepnt) || (DemoMode)) && (plr->Bullets != reload[plr->BulletType]) &&
       (plr->BulletType != 1) && (plr->Bullets != 22) &&
       (plr->BulletType != 3))
   {
      plr->Bullets = reload[plr->BulletType];
      if (vabnbr != -1)
         PlaySFX (vabnbr, 2, plr ? 0 : 127, plr ? 127 : 0);
   }
   else if (((DemoMode) || (plr->Lifepnt)) && (plr->Bullets == 0) &&
            ((plr->BulletType == 1) || (plr->BulletType == 3)))
   {
      plr->BulletType = 0;
      plr->Bullets = reload[plr->BulletType];
      if (vabnbr != -1)
         PlaySFX (vabnbr, 2, plr ? 0 : 127, plr ? 127 : 0);
   }
}

//-------------------------------------
// Common procedure for both guns and
// joysticks
//-------------------------------------
void Control_Read (int *trigger, Foreground *frgnd, int vabnbr, int *buttons)
{
   int         xpadd = GUN1;
   int         xpadd2 = GUN2;
   static int  trig1 = 0, trig2 = 0;
   static char oldid[2] = {0, 0};
   int         maxnbrbullet[7] = {1,50,1,1,3,1,1};
   int         minnbrbullet[7] = {0,0,0,0,3,0,0};
   int         displacement;
   PLAYER      *plr1 = &(frgnd->Plr1);
   PLAYER      *plr2 = &(frgnd->Plr2);
   int         i;

   // Need to read Gun coordinate ?
   if (trig1)
   {
      if (--trig1 == 0)
      {
         if (IntrusReload[0])
         {
            ReloadPlrGun (plr1, vabnbr);
            IntrusReload[0] = 0;
            plr1->GunX = -100;
            plr1->GunY = -100;
         }
         else
         {
            int gotsu = 0;
            for (i = 0; i < 1/****IntrusIndex[0]****/; i++)
               gotsu += IntrusGunX[0][i];
//            gotsu /= IntrusIndex[0];
            plr1->GunX = ((gotsu - 140 - XGunMiddle) / 5) + 160;
            if (plr1->GunX < 0)
               plr1->GunX = 0;
            else if (plr1->GunX > 319)
               plr1->GunX = 319;
            plr1->GunY = (IntrusGunY[0][0] - YGunMiddle) + 120;
            *trigger |= 1;
         }
      }
   }
   if (trig2)
   {
      if (--trig2 == 0)
      {
         if (IntrusReload[1])
         {
            ReloadPlrGun (plr2, vabnbr);
            IntrusReload[1] = 0;
            plr2->GunX = -100;
            plr2->GunY = -100;
         }
         else
         {
            int gotsu = 0;
            for (i = 0; i < 1/****IntrusIndex[1]****/; i++)
               gotsu += IntrusGunX[1][i];
//            gotsu /= IntrusIndex[1];
            plr2->GunX = ((gotsu - 140 - XGunMiddle) / 5) + 160;
            if (plr2->GunX < 0)
               plr2->GunX = 0;
            else if (plr2->GunX > 319)
               plr2->GunX = 319;
            plr2->GunY = (IntrusGunY[1][0] - YGunMiddle) + 120;
            *trigger |= 2;
         }
      }
   }

   if (gun[0]->id != oldid[0])          // Handle the case where a controller
   {                                    //  is changed (from joystick to gun
      oldid[0] = gun[0]->id;            //  or from gun to joystick)
      if (oldid[0] == PADID_CON)        //
      {                                 //
         plr1->GunX = 160;              //
         plr1->GunY = 120;              //
      }                                 //
   }                                    //
                                        //
   if (gun[1]->id != oldid[1])          //
   {                                    //
      oldid[1] = gun[1]->id;            //
      if (oldid[1] == PADID_CON)        //
      {                                 //
         plr2->GunX = 160;              //
         plr2->GunY = 120;              //
      }                                 //
   }                                    //

   //------------ Player 1 ------------
   if ((!GamePause) || ((GamePause) && (GamePausedBy == 0)))
   {

   if (gun[0]->id == PADID_GUN)             //******* Player 1: Gun *******
   {
      if (~old_padd & xpadd & PADRdown)     // Gun's select button
         *buttons |= 32;                    //
      if (xpadd & PADRdown)                 //
         *buttons |= 1024;                  //

      if ((~old_padd & xpadd & PADRleft) && (frgnd->Plr1.Lifepnt)) 
      {                                     // Trigger pressed.
         IntrusIndex[0] = 0;
         Flash[0] = 5;
         *buttons |= 64;
         trig1 = 2;                         // Wait 2 VSync before reading the
         IntrusReload[0] = 0;               // gun coordinate
      }

      if (~old_padd & xpadd & PADstart)      // For the start button...
      {
         if ((!plr1->Lifepnt) && (frgnd->Credits))
         {
            if ((LevelSelected == 0) && (LE1orLE2 == 0))
               PleaseWait[0] = 1;
            else
            {
               *buttons |= 1;
               frgnd->Credits--;
               if (vabnbr != -1)
                  PlaySFX (vabnbr, 17, 127,0);
               Update_Player (plr1);
               plr1->Lifepnt = 9;
            }
         }
         else
            *buttons |= 2;
      }
   }
   else                                     //***** Player 1: Joystick *****
   {
      if (xpadd & ButtonCode[ButtonSelection[0][2]]) // Fast cursor...
         displacement = 4;
      else
         displacement = 2;

      if (xpadd & PADLup)                    // For the gun 1's cursor
         if (plr1->GunY > 0)
            plr1->GunY -= displacement;

      if (~old_padd & xpadd & PADLup)
         *buttons |= 256;

      if (xpadd & PADLdown)
         if (plr1->GunY < 238)
            plr1->GunY += displacement;

      if (~old_padd & xpadd & PADLdown)
         *buttons |= 128;
      
      if (xpadd & PADLleft)
      {
         *buttons |= 8;
         if (plr1->GunX > 0)
            plr1->GunX -= displacement;
      }

      if (~old_padd & xpadd & PADLleft)
         *buttons |= 4096;

      if (xpadd & PADLright)
      {
         *buttons |= 16;
         if (plr1->GunX < 318)
            plr1->GunX += displacement;
      }

      if (~old_padd & xpadd & PADLright)
         *buttons |= 8192;

      if (~old_padd & xpadd & PADselect)     // Joystick's select button
         *buttons |= 512;                   //

      if (~old_padd & xpadd & PADRdown)      // Joystick's X button
         *buttons |= 2048;                  //

      if ((plr1->Counter > 0) && (~old_padd & xpadd & PADR1) && (!plr1->Lifepnt))
      {
         plr1->Counter -= plr1->Counter % 60;
         PlaySFX (commonvab, 5, 127,0);
      }

      if (bulletshot[0] > 1)                // Did we shot all the necessary
      {                                     // bullets
         if (bulletdelay[0]++ > 5)
         {
            Flash[0] = 3;
            bulletdelay[0] = 0;
            bulletshot[0]--;
            *trigger |= 1;
            nbrbullet[0]++;
            #ifdef RECORDDEMO
               if ((NbrMardien < MAXMARDIEN) && (plr1->Bullets > 0))
               {
                  ShotMardien[NbrMardien].X = plr1->GunX;
                  ShotMardien[NbrMardien].Y = plr1->GunY;
                  ShotMardien[NbrMardien].PlayerNbr = 0;
                  ShotMardien[NbrMardien].FrameNbr = FrameCounter;
                  NbrMardien++;
               }
            #endif
         }
      }
      else if ((~old_padd & xpadd & ButtonCode[ButtonSelection[0][0]]) &&
               (plr1->Lifepnt))
      {                                     // Fire a bullet
         Flash[0] = 3;
         *trigger |= 1;
         nbrbullet[0]++;
         #ifdef RECORDDEMO
            if ((NbrMardien < MAXMARDIEN) && (plr1->Bullets > 0))
            {
               ShotMardien[NbrMardien].X = plr1->GunX;
               ShotMardien[NbrMardien].Y = plr1->GunY;
               ShotMardien[NbrMardien].PlayerNbr = 0;
               ShotMardien[NbrMardien].FrameNbr = FrameCounter;
               NbrMardien++;
            }
         #endif
      }
      else if (!(xpadd & ButtonCode[ButtonSelection[0][0]]))
      {
         nbrbullet[0] = 0;
         bulletdelay[0] = 0;
      }
      else if (bulletdelay[0]++ > 5)
      {
         if ((++nbrbullet[0]) <= maxnbrbullet[plr1->BulletType])
         {
            *trigger |= 1;
            Flash[0] = 3;
            #ifdef RECORDDEMO
               if ((NbrMardien < MAXMARDIEN) && (plr1->Bullets > 0))
               {
                  ShotMardien[NbrMardien].X = plr1->GunX;
                  ShotMardien[NbrMardien].Y = plr1->GunY;
                  ShotMardien[NbrMardien].PlayerNbr = 0;
                  ShotMardien[NbrMardien].FrameNbr = FrameCounter;
                  NbrMardien++;
               }
            #endif
         }
         bulletdelay[0] = 0;
      }

      if ((~old_padd & xpadd & ButtonCode[ButtonSelection[0][1]]) &&
          (plr1->Lifepnt))                  // For the reload button...
         ReloadPlrGun (plr1, vabnbr);

      if (~old_padd & xpadd & PADstart)     // For the start button...
      {
         if ((!plr1->Lifepnt) && (frgnd->Credits))
         {
            if ((LevelSelected == 0) && (LE1orLE2 == 0))
               PleaseWait[0] = 1;
            else
            {
               *buttons |= 1;
               frgnd->Credits--;
               if (vabnbr != -1)
                  PlaySFX (vabnbr, 17, 127,0);
               Update_Player (plr1);
               plr1->Lifepnt = 9;
            }
         }
         else
            *buttons |= 2;
      }
   }

   }
   if ((!GamePause) || ((GamePause) && (GamePausedBy == 1)))
   {

   //------------ Player 2 ------------
   if (gun[1]->id == PADID_GUN)             //******* Player 2: Gun *******
   {
      if (~old_padd2 & xpadd2 & PADRdown)   // The gun's select button
         *buttons |= 32;                    //
      if (xpadd2 & PADRdown)                //
         *buttons |= 1024;                  //

      if ((~old_padd2 & xpadd2 & PADRleft) && (frgnd->Plr2.Lifepnt))
      {                                     // Trigger pressed.
         IntrusIndex[1] = 0;
         Flash[1] = 5;
         *buttons |= 64;
         trig2 = 2;                         // Wait 2 VSync before reading the
         IntrusReload[1] = 0;               // gun coordinate
      }

      if (~old_padd2 & xpadd2 & PADstart)   // For the start button...
      {
         if ((!plr2->Lifepnt) && (frgnd->Credits))
         {
            if ((LevelSelected == 0) && (LE1orLE2 == 0))
               PleaseWait[1] = 1;
            else
            {
               *buttons |= 1;
               frgnd->Credits--;
               Update_Player (plr2);
               plr2->Lifepnt = 9;
               if (vabnbr != -1)
                  PlaySFX (vabnbr, 18, 0,127);
            }
         }
         else
            *buttons |= 4;
      }
   }
   else                                 //***** Player 2: Joystick *****
   {
      if (xpadd2 & ButtonCode[ButtonSelection[1][2]])   // Fast cursor...
         displacement = 4;
      else
         displacement = 2;

      if (xpadd2 & PADLup)               // For the gun 2's cursor
         if (plr2->GunY > 0)
            plr2->GunY -= displacement;

      if (~old_padd2 & xpadd2 & PADLup)
         *buttons |= 256;

      if (xpadd2 & PADLdown)
         if (plr2->GunY < 238)
            plr2->GunY += displacement;

      if (~old_padd2 & xpadd2 & PADLdown)
         *buttons |= 128;
      
      if (xpadd2 & PADLleft)
      {
         *buttons |= 8;
         if (plr2->GunX > 0)
            plr2->GunX -= displacement;
      }

      if (~old_padd2 & xpadd2 & PADLleft)
         *buttons |= 4096;

      if (xpadd2 & PADLright)
      {
         *buttons |= 16;
         if (plr2->GunX < 318)
            plr2->GunX += displacement;
      }

      if (~old_padd2 & xpadd2 & PADLright)
         *buttons |= 8192;

      if (~old_padd2 & xpadd2 & PADselect)  // Joystick's select button
         *buttons |= 512;                  //

      if (~old_padd2 & xpadd2 & PADRdown)    // Joystick's X button
         *buttons |= 2048;                  //

      if ((plr2->Counter > 0) && (~old_padd2 & xpadd2 & PADR1) && (!plr2->Lifepnt))
      {
         plr2->Counter -= plr2->Counter % 60;
         PlaySFX (commonvab, 5, 127,0);
      }

      if (bulletshot[1] > 1)               // Did we shot all the necessary
      {                                    // bullets
         if (bulletdelay[1]++ > 5)
         {
            bulletdelay[1] = 0;
            bulletshot[1]--;
            *trigger |= 2;
            Flash[1] = 3;
            nbrbullet[1]++;
            #ifdef RECORDDEMO
               if ((NbrMardien < MAXMARDIEN) && (plr2->Bullets > 0))
               {
                  ShotMardien[NbrMardien].X = plr2->GunX;
                  ShotMardien[NbrMardien].Y = plr2->GunY;
                  ShotMardien[NbrMardien].PlayerNbr = 1;
                  ShotMardien[NbrMardien].FrameNbr = FrameCounter;
                  NbrMardien++;
               }
            #endif
         }
      }
      else if ((~old_padd2 & xpadd2 & ButtonCode[ButtonSelection[1][0]]) &&
               (plr2->Lifepnt))
      {                             // Fire a bullet
         Flash[1] = 3;
         *trigger |= 2;
         nbrbullet[1]++;
         #ifdef RECORDDEMO
            if ((NbrMardien < MAXMARDIEN) && (plr2->Bullets > 0))
            {
               ShotMardien[NbrMardien].X = plr2->GunX;
               ShotMardien[NbrMardien].Y = plr2->GunY;
               ShotMardien[NbrMardien].PlayerNbr = 1;
               ShotMardien[NbrMardien].FrameNbr = FrameCounter;
               NbrMardien++;
            }
         #endif
      }
      else if (!(xpadd2 & ButtonCode[ButtonSelection[1][0]]))
      {
         nbrbullet[1] = 0;
         bulletdelay[1] = 0;
      }
      else if (bulletdelay[1]++ > 5)
      {
         if ((++nbrbullet[1]) <= maxnbrbullet[plr2->BulletType])
         {
            *trigger |= 2;
            Flash[1] = 3;
            #ifdef RECORDDEMO
               if ((NbrMardien < MAXMARDIEN) && (plr2->Bullets > 0))
               {
                  ShotMardien[NbrMardien].X = plr2->GunX;
                  ShotMardien[NbrMardien].Y = plr2->GunY;
                  ShotMardien[NbrMardien].PlayerNbr = 1;
                  ShotMardien[NbrMardien].FrameNbr = FrameCounter;
                  NbrMardien++;
               }
            #endif
         }
         bulletdelay[1] = 0;
      }

      if ((~old_padd2 & xpadd2 & ButtonCode[ButtonSelection[1][1]]) &&
          (plr2->Lifepnt))                  // For the reload button...
         ReloadPlrGun (plr2, vabnbr);

      if (~old_padd2 & xpadd2 & PADstart)   // For the start button...
      {
         if ((!plr2->Lifepnt) && (frgnd->Credits))
         {
            if ((LevelSelected == 0) && (LE1orLE2 == 0))
               PleaseWait[1] = 1;
            else
            {
               *buttons |= 1;
               frgnd->Credits--;
               Update_Player (plr2);
               plr2->Lifepnt = 9;
               if (vabnbr != -1)
                  PlaySFX (vabnbr, 18, 0,127);
            }
         }
         else
            *buttons |= 4;
      }
   }

   }

   //--------- Demo mode shot ---------
   if (DemoMode)
   {
      if (ShotMardien[CurrentMardien].FrameNbr == FrameCounter)
      {
         Flash[0] = 3;
         CurrentMardien++;
      }
   }

   //------------ To quit -------------
   if (((~old_padd & xpadd) | (~old_padd2 & xpadd2)) & PADselect)
      *buttons |= 1;

   //----- For the debugging mode -----
   if (((~old_padd & xpadd) | (~old_padd2 & xpadd2)) & PADR2)
   {
      while ((GUN1 | GUN2) & PADR2);
      debug = debug ? 0 : 1;
   }

   //------- To see VRAM content ------
   if ((xpadd | xpadd2) & PADL2)
   {
      DISPENV content;
      int zonex = 320, zoney = 0;

//      screenshot (0);

      while ((xpadd | xpadd2) & PADL2)
      {
         xpadd = GUN1;
         xpadd2 = GUN2;

         SetDefDispEnv (&content, zonex,zoney, 320,240);
         PutDispEnv (&content);
         VSync (0);
         if ((xpadd | xpadd2) & PADRleft)
            if (zonex > -26)
               zonex -= 2;
         if ((xpadd | xpadd2) & PADRright)
            if (zonex < 728)
               zonex += 2;
         if ((xpadd | xpadd2) & PADRup)
            if (zoney > -25)
               zoney--;
         if ((xpadd | xpadd2) & PADRdown)
            if (zoney < 265)
               zoney++;
      }
   }

   //----------------------------------
   old_padd = xpadd;
   old_padd2 = xpadd2;
}
