//---------------------------------------------------------------------------
// Hall of fame for Lethal Enforcers 1 & 2
//---------------------------------------------------------------------------

#include <sys\types.h>
#include <libetc.h>
#include <libgte.h>
#include <libgpu.h>
#include <rand.h>
#include <libcd.h>
#include <memory.h>
#include "..\include\compile.h"
#include "..\include\db.h"
#include "..\include\bkgnd.h"
#include "..\include\fonts.h"
#include "..\include\alocvram.h"

extern int debug;

typedef struct
{
   char    Name[4];
   int     Score;
   u_short Accuracy;
   u_short Class;
} HOF_ENTRY;

#ifndef __player
   #define __player
   typedef struct
   {
      int Score;
      int BulletType;
      int Bullets;
      int Lifepnt;
      int Counter;
      short GunX;
      short GunY;
      short ShotX;
      short ShotY;
      char Grade;
      short Shots;
      short Hits;
      short VictimsMiss;
      char PlayerNbr;
   } PLAYER;
#endif

typedef struct
{
   int Credits;
   PLAYER Plr1;
   PLAYER Plr2;
} Foreground;

void Options (Foreground *frgnd, DoubleBuffer *db, int vabnbr, u_long memoryp);
void Control_Read (int *trigger, Foreground *frgnd, int vabnbr, int *buttons);
void PlaySFX (int vabnbr, int sfxnbr, int voll, int volr);
void Draw_Gun_Cursor (Foreground *frgnd, DoubleBuffer *db, int drawit);

HOF_ENTRY  HALLFAME[10] = {{"GEF",99999,99,5},
                           {"ERC",  500,47,4},
                           {"MKY",  432,34,3},
                           {"MAN",  400,32,2},
                           {"FYD",  333,60,1},
                           {"BAR",  321,57,1},
                           {"RAT",  287,26,1},
                           {"ICU",  250,65,0},
                           {"KBS",  200,90,0},
                           {"WWW",  150,30,0}};
char *Classes[] = {"PATROLMAN","DETECTIVE","SERGEANT","LIEUTENANT",
                   "CAPTAIN","COMMANDER"};

char HallF[10][30] = {"1  GEF  99999  99  LIEUTENANT",
                      "2  RAT  99999  99  LIEUTENANT",
                      "3  BON  99999  99  LIEUTENANT",
                      "4  AIR  99999  99  LIEUTENANT",
                      "5  ALE  99999  99  LIEUTENANT",
                      "6  ONE  99999  99  LIEUTENANT",
                      "7  TWO  99999  99  LIEUTENANT",
                      "8  SIX  99999  99  LIEUTENANT",
                      "9  TEN  99999  99  LIEUTENANT",
                      "10 NET  99999  99  LIEUTENANT"};
char HallF2[8][17] = {"1ST  198765  GEF",
                      "2ND   12345  ERC",
                      "3RD    1321  RAT",
                      "4TH     117  POP",
                      "5TH      19  DOG",
                      "6TH       9  GOD",
                      "7TH       8  SOS",
                      "8TH       7  RUF"};

extern int commonvab;

//-------------------------------------
// Build the strings for the Hall of
// Fame
//-------------------------------------
void BuildHoF (void)
{
   int i, j, k;

   for (i = 0; i < 10; i++)
   {
      for (j = 3; j < 28; j++)          // Clear the string
         HallF[i][j] = ' ';

      HallF[i][3] = HALLFAME[i].Name[0];
      HallF[i][4] = HALLFAME[i].Name[1];
      HallF[i][5] = HALLFAME[i].Name[2];

      k = HALLFAME[i].Score;            // Convert the score to string
      HallF[i][8] = k / 10000 + 48;
      k -= k / 10000 * 10000;
      HallF[i][9] = k / 1000 + 48;
      k -= k / 1000 * 1000;
      HallF[i][10] = k / 100 + 48;
      k -= k / 100 * 100;
      HallF[i][11] = k / 10 + 48;
      k -= k / 10 * 10;
      HallF[i][12] = k + 48;
      k = 8;
      while ((k < 12) && (HallF[i][k] == '0'))
      {
         HallF[i][k] = ' ';
         k++;
      }

      HallF[i][15] = (HALLFAME[i].Accuracy / 10) + 48;  // Accuracy
      HallF[i][16] = (HALLFAME[i].Accuracy % 10) + 48;

      k = 0;
      while (Classes[HALLFAME[i].Class][k] != 0)        // Class
      {
         HallF[i][19 + k] = Classes[HALLFAME[i].Class][k];
         k++;
      }
      HallF[i][19 + k] = 0;
   }
}

//-------------------------------------
// Build the strings for the Hall of
// Fame of Lethal Enforcers II
//-------------------------------------
void BuildHoF2 (void)
{
   int i, j, k;

   for (i = 0; i < 8; i++)
   {
      for (j = 5; j < 16; j++)          // Clear the string
         HallF2[i][j] = ' ';

      HallF2[i][13] = HALLFAME[i].Name[0];
      HallF2[i][14] = HALLFAME[i].Name[1];
      HallF2[i][15] = HALLFAME[i].Name[2];

      k = HALLFAME[i].Score;            // Convert the score to string
      HallF2[i][5] = k / 100000 + 48;
      k -= k / 100000 * 100000;
      HallF2[i][6] = k / 10000 + 48;
      k -= k / 10000 * 10000;
      HallF2[i][7] = k / 1000 + 48;
      k -= k / 1000 * 1000;
      HallF2[i][8] = k / 100 + 48;
      k -= k / 100 * 100;
      HallF2[i][9] = k / 10 + 48;
      k -= k / 10 * 10;
      HallF2[i][10] = k + 48;
      k = 5;
      while ((k < 10) && (HallF2[i][k] == '0'))
      {
         HallF2[i][k] = ' ';
         k++;
      }
   }
}

//-------------------------------------
// Hall of fame for Lethal Enforcers II
//-------------------------------------
int Hall_Of_Fame2 (u_long memptr, DoubleBuffer *db, Foreground *frgnd,
                   int vabnbr)
{
   u_long     padd = 0, padd2 = 0;
   Background bkd;
   int        i;
   int        Counter = 240;
   int        trig, buttons = 0;

   if (memptr & 3)
      memptr = (u_long)((memptr + 4) & 0xFFFFFFFC);
   bkd.Load ("\\BACKGRND\\MISCELLA\\HOFLE2.BKD", &memptr);
   bkd.SetLocation (0,0);

   BuildHoF2 ();

   while (Counter--)
   {
      Control_Read (&trig, frgnd, vabnbr, &buttons);
      if (buttons & 1)                  // Start button ?
         Counter = 0;
      buttons = 0;

      for (i = 0; i < 8; i++)       // Print the lines
      {
         FontLeftOn (84, 78 + i * 17);
         Print (HallF2[i]);
      }

      bkd.Draw (db, 0);

      db->Swap (0);
   }

   db->ClearDrawArea (0,0,0);   // Turn the screen to black
   db->Swap (0);                //
   db->Swap (0);                //

   //----------------------------------
   bkd.Clear ();
}

//-------------------------------------
// Show the hall of fame, and wait for
// one user to press start
//-------------------------------------
int Hall_Of_Fame (u_long memptr, DoubleBuffer *db, Foreground *frgnd,
                   int vabnbr)
{
   u_long     padd = 0, padd2 = 0;
   int        Black = 1;
   Background bkd;
   int        OfsWritings;
   int        i, j, k;
   int        Counter;
   int        coorx[5];
   int        trig, buttons = 0;

   if (memptr & 3)
      memptr = (u_long)((memptr + 4) & 0xFFFFFFFC);
   bkd.Load ("\\BACKGRND\\MISCELLA\\HALLFAME.BKD", &memptr);
   bkd.SetLocation (0,0);

   BuildHoF ();

   OfsWritings = 350;
   Counter = 50;
   while ((Black) && (Counter)) // First, "BEST COP" & "BLA BLA BLA" appear...
   {
      if (VSync (-1) & 32)
      {
         SetFontColor (0);
         FontCenterOn (160,195);
         Print ("Press start to begin\n");
         FontCenterOn (160,213);
         Print ("Press select for options");
      }

      Control_Read (&trig, frgnd, vabnbr, &buttons);
      if (buttons & 1)                  // Start button ?
         Black = 0;
      if (buttons & 544)                // Select button ?
      {
         db->ClearDrawArea (0,0,0);     // Turn the screen to black
         db->Swap (0);                  //
         db->Swap (0);                  //
         bkd.Clear ();
         Options (frgnd, db, vabnbr, memptr);
         return (0);
      }

      bkd.Draw (db, 0);

      SetFontColor (0);
      FontCenterOn (160 - OfsWritings, 30);
      Print ("BEST COPS");
      FontCenterOn (160 + OfsWritings, 50);
      Print ("RANK NAME SCORE ACCURACY CLASS");

      if (OfsWritings)
         OfsWritings -= 10;
      else
         Counter--;

      db->Swap (0);
   }

   coorx[0] = 36 - 16 * 22;
   coorx[1] = 36 + 16 * 22;
   coorx[2] = 36 - 16 * 22;
   coorx[3] = 36 + 16 * 22;
   coorx[4] = 36 - 16 * 22;
   Counter = 140;
   while ((Black) && (Counter))         // Make the first 5 lines to appear...
   {
      if (VSync (-1) & 32)
      {
         SetFontColor (0);
         FontCenterOn (160,195);
         Print ("Press start to begin\n");
         FontCenterOn (160,213);
         Print ("Press select for options");
      }

      Control_Read (&trig, frgnd, vabnbr, &buttons);
      if (buttons & 1)                  // Start button ?
         Black = 0;
      if (buttons & 544)                // Select button ?
      {
         db->ClearDrawArea (0,0,0);     // Turn the screen to black
         db->Swap (0);                  //
         db->Swap (0);                  //
         bkd.Clear ();
         Options (frgnd, db, vabnbr, memptr);
         return (0);
      }

      bkd.Draw (db, 0);

      SetFontColor (0);
      FontCenterOn (160, 30);
      Print ("BEST COPS");
      FontCenterOn (160, 50);
      Print ("RANK NAME SCORE ACCURACY CLASS");
      SetFontColor (1);

      if (coorx[0] != 36)           // Displacement of the lines
         coorx[0] += 16;
      else if (coorx[1] != 36)
         coorx[1] -= 16;
      else if (coorx[2] != 36)
         coorx[2] += 16;
      else if (coorx[3] != 36)
         coorx[3] -= 16;
      else if (coorx[4] != 36)
         coorx[4] += 16;
      else
         Counter--;

      for (i = 0; i < 5; i++)       // Print the lines
      {
         FontLeftOn (coorx[i], 80 + i * 27);
         Print (HallF[i]);
      }

      db->Swap (0);
   }

   Counter = 1;
   OfsWritings = 0;
   while ((Black) && (Counter))         // Make the first 5 lines disappear...
   {
      if (VSync (-1) & 32)
      {
         SetFontColor (0);
         FontCenterOn (160,195);
         Print ("Press start to begin\n");
         FontCenterOn (160,213);
         Print ("Press select for options");
      }

      Control_Read (&trig, frgnd, vabnbr, &buttons);
      if (buttons & 1)                  // Start button ?
         Black = 0;
      if (buttons & 544)                // Select button ?
      {
         db->ClearDrawArea (0,0,0);     // Turn the screen to black
         db->Swap (0);                  //
         db->Swap (0);                  //
         bkd.Clear ();
         Options (frgnd, db, vabnbr, memptr);
         return (0);
      }

      bkd.Draw (db, 0);

      SetFontColor (0);
      FontCenterOn (160, 30);
      Print ("BEST COPS");
      FontCenterOn (160, 50);
      Print ("RANK NAME SCORE ACCURACY CLASS");

      SetFontColor (1);
      for (i = 0; i < 5; i++)
      {
         if (i & 1)
            j = 160 + OfsWritings;
         else
            j = 160 - OfsWritings;

         FontLeftOn (j - 124, 80 + i * 27);
         Print (HallF[i]);
      }

      if (OfsWritings != 350)
         OfsWritings += 10;
      else
         Counter--;

      db->Swap (0);
   }

   coorx[0] = 36 - 16 * 22;
   coorx[1] = 36 + 16 * 22;
   coorx[2] = 36 - 16 * 22;
   coorx[3] = 36 + 16 * 22;
   coorx[4] = 36 - 16 * 22;
   Counter = 140;
   while ((Black) && (Counter))         // Make the last 5 lines to appear...
   {
      if (VSync (-1) & 32)
      {
         SetFontColor (0);
         FontCenterOn (160,195);
         Print ("Press start to begin\n");
         FontCenterOn (160,213);
         Print ("Press select for options");
      }

      Control_Read (&trig, frgnd, vabnbr, &buttons);
      if (buttons & 1)                  // Start button ?
         Black = 0;
      if (buttons & 544)                // Select button ?
      {
         db->ClearDrawArea (0,0,0);     // Turn the screen to black
         db->Swap (0);                  //
         db->Swap (0);                  //
         bkd.Clear ();
         Options (frgnd, db, vabnbr, memptr);
         return (0);
      }

      bkd.Draw (db, 0);

      SetFontColor (0);
      FontCenterOn (160, 30);
      Print ("BEST COPS");
      FontCenterOn (160, 50);
      Print ("RANK NAME SCORE ACCURACY CLASS");
      SetFontColor (1);

      if (coorx[0] != 36)           // Displacement of the lines
         coorx[0] += 16;
      else if (coorx[1] != 36)
         coorx[1] -= 16;
      else if (coorx[2] != 36)
         coorx[2] += 16;
      else if (coorx[3] != 36)
         coorx[3] -= 16;
      else if (coorx[4] != 36)
         coorx[4] += 16;
      else
         Counter--;

      for (i = 0; i < 5; i++)       // Print the lines
      {
         FontLeftOn (coorx[i], 80 + i * 27);
         Print (HallF[i + 5]);
      }

      db->Swap (0);
   }

   if (padd & PADstart)
      frgnd->Plr1.Lifepnt = 9;
   if (padd2 & PADstart)
      frgnd->Plr2.Lifepnt = 9;

   db->ClearDrawArea (0,0,0);   // Turn the screen to black
   db->Swap (0);                //
   db->Swap (0);                //

   //----------------------------------
   bkd.Clear ();
}

//-------------------------------------
// Enter a name in the hall of fame
//-------------------------------------
int EnterNameHOF (Foreground *frgnd, int leversion, u_long memptr, DoubleBuffer *db)
{
   TIM_IMAGE  timimg;
   POLY_F4    *plain;
   SPRT       *spr;
   DR_MODE    *drmode;
   Background bkd;
   PLAYER     *plr[2] = {&frgnd->Plr1, &frgnd->Plr2};
   register int i;
   int        j, k, m;
   int        coorx, coory;
   int        actualcase[2] = {0,0};
   char       newname[2][3] = {{32,32,32}, {32,32,32}};
   int        counter = 60000;
   int        dummy;
   int        trigger = 0;
   int        hofpos[2] = {11,11};
   int        accuracy[2];
   int        plrnbr;
   int        basex[2] = {46, 210};
   int        basx[2] = {23, 187};
   int        GradeAccu[6] = {0,17,34,51,68,85};
   int        plrcase[2];
   short      pictvrammemhandle;
   int        tpx, tpy;
   int        tiscclut;

   leversion = 1;

   if (frgnd->Plr1.Shots)
      accuracy[0] = frgnd->Plr1.Hits * 100 / frgnd->Plr1.Shots;
   else
      accuracy[0] = 0;

   if (frgnd->Plr2.Shots)
      accuracy[1] = frgnd->Plr2.Hits * 100 / frgnd->Plr2.Shots;
   else
      accuracy[1] = 0;

   for (i = 0; i < 10; i++)
   {
      if ((plr[0]->Score > HALLFAME[i].Score) && !(plr[0]->Lifepnt))
      {
         hofpos[0] = i;
         plr[0]->Lifepnt = 1;
      }
      else if ((plr[0]->Score == HALLFAME[i].Score) &&
               (accuracy[0] > HALLFAME[i].Accuracy) && !(plr[0]->Lifepnt))
      {
         plr[0]->Lifepnt = 1;
         hofpos[0] = i;
      }

      if ((plr[1]->Score > HALLFAME[i].Score) && !(plr[1]->Lifepnt))
      {
         hofpos[1] = i;
         plr[1]->Lifepnt = 1;
      }
      else if ((plr[1]->Score == HALLFAME[i].Score) &&
               (accuracy[1] > HALLFAME[i].Accuracy) && !(plr[1]->Lifepnt))
      {
         hofpos[1] = i;
         plr[1]->Lifepnt = 1;
      }
   }

   int mardi = hofpos[1];
   if (hofpos[0] == hofpos[1])
   {
      if (plr[1]->Score > plr[0]->Score)
      {
         if ((++hofpos[0]) == 10)
            plr[0]->Lifepnt = 0;

         for (i = 8; i >= hofpos[1]; i--)
            HALLFAME[i + 1] = HALLFAME[i];
         if (hofpos[0] < 9)
         {
            for (i = 8; i >= hofpos[0]; i--)
               HALLFAME[i + 1] = HALLFAME[i];
         }
      }
      else
      {
         if ((++hofpos[1]) == 10)
            plr[1]->Lifepnt = 0;

         for (i = 8; i >= hofpos[0]; i--)
            HALLFAME[i + 1] = HALLFAME[i];
         if (hofpos[1] < 9)
         {
            for (i = 8; i >= hofpos[1]; i--)
               HALLFAME[i + 1] = HALLFAME[i];
         }
      }
   }
   else if (hofpos[0] < hofpos[1])
   {
      if ((++hofpos[1]) == 10)
         plr[1]->Lifepnt = 0;

      for (i = 8; i >= hofpos[0]; i--)
         HALLFAME[i + 1] = HALLFAME[i];
      if (hofpos[1] < 9)
      {
         for (i = 8; i >= hofpos[1]; i--)
            HALLFAME[i + 1] = HALLFAME[i];
      }
   }
   else
   {
      if ((++hofpos[0]) == 10)
         plr[0]->Lifepnt = 0;

      for (i = 8; i >= hofpos[1]; i--)
         HALLFAME[i + 1] = HALLFAME[i];
      if (hofpos[0] < 9)
      {
         for (i = 8; i >= hofpos[0]; i--)
            HALLFAME[i + 1] = HALLFAME[i];
      }
   }

   if (hofpos[0] < 10)
   {
      i = hofpos[0];
      HALLFAME[i].Score = plr[0]->Score;
      if (plr[0]->Shots == 0)
         HALLFAME[i].Accuracy = 0;
      else
         HALLFAME[i].Accuracy = plr[0]->Hits  * 100 / plr[0]->Shots;
      for (j = 0; j < 6; j++)
      {
         if (HALLFAME[i].Accuracy < GradeAccu[j])
            HALLFAME[i].Class = j;
      }
   }

   if (hofpos[1] < 10)
   {
      i = hofpos[1];
      HALLFAME[i].Score = plr[1]->Score;
      if (plr[1]->Shots == 0)
         HALLFAME[i].Accuracy = 0;
      else
         HALLFAME[i].Accuracy = plr[1]->Hits  * 100 / plr[1]->Shots;
      for (j = 0; j < 6; j++)
      {
         if (HALLFAME[i].Accuracy < GradeAccu[j])
            HALLFAME[i].Class = j;
      }
   }

   if ((hofpos[0] > 10) && (hofpos[1] > 10))
      return (0);

   //----------------------------------
   if (memptr & 3)
      memptr = (u_long)((memptr + 4) & 0xFFFFFFFC);
   if (leversion == 0)
   {
      if (bkd.Load ("\\BACKGRND\\MISCELLA\\HOFNAME1.BKD", &memptr) == 0)
         return (-1);
   }
   else
   {
      if (bkd.Load ("\\BACKGRND\\MISCELLA\\HOFNAME2.BKD", &memptr) == 0)
         return (-1);
   }
   bkd.SetLocation (0,0);

   //----------------------------------
   if (memptr & 3)
      memptr = (u_long)((memptr + 4) & 0xFFFFFFFC);
   if (leversion == 0)
   {
      if (ReadFont ("\\SPRITES\\HOFLETR1.TIM", memptr) == 0)
         return (-1);
   }
   else
   {
      if (ReadFont ("\\SPRITES\\HOFLETR2.TIM", memptr) == 0)
         return (-1);
   }

   if (OpenTIM ((u_long *)memptr))
      return (-1);

   if (!ReadTIM (&timimg))
      return (-1);

   point po = AllocVram (timimg.prect->w, timimg.prect->h,
                         &pictvrammemhandle, 1);

   tpx = po.x;
   tpy = po.y;

   if (timimg.prect)
   {
      RECT shit2 = {po.x, po.y, timimg.prect->w, timimg.prect->h};
      LoadImage (&shit2, timimg.paddr);
      DrawSync (0);
   }

   if ((tiscclut = AllocClut ()) == -1)
      return (-1);
   else
      tiscclut += 480;

   RECT shit1 = {0,tiscclut, 256,1};
   if (timimg.crect)
      LoadImage (&shit1, (u_long *)timimg.caddr);

   //----------------------------------
   while (counter)
   {
      //---------------
      Control_Read (&trigger, frgnd, commonvab, &dummy);

      if (debug)
      {
         FntPrint ("Plr1 X:%d  Y:%d\n", plr[0]->GunX, plr[0]->GunY);
         FntPrint ("Plr2 X:%d  Y:%d\n", plr[1]->GunX, plr[1]->GunY);
      }

      if (hofpos[0] > 9)
         plr[0]->Lifepnt = 0;
      if (hofpos[1] > 9)
         plr[1]->Lifepnt = 0;

      if (trigger != 0)
      {
         if (trigger & 1)
         {
            trigger &= ~1;
            plrnbr = 0;
         }
         else
         {
            trigger &= ~2;
            plrnbr = 1;
         }

         // A case has been selected.  Which one?
         i = plrcase[plrnbr];
         if (i != -1)
         {
            PlaySFX (commonvab, 32, (!plrnbr) ? 127 : 0, plrnbr ? 127 : 0);

            if (i < 26)             // A letter of the alphabet
            {
               newname[plrnbr][actualcase[plrnbr]] = i + 65;
               if (actualcase[plrnbr] < 2)
                  actualcase[plrnbr]++;
            }
            else if (i == 26)       // 'End' has been selected
            {
               plr[plrnbr]->Lifepnt = 0;
               if ((plr[0]->Lifepnt == 0) && (plr[1]->Lifepnt == 0))
                  counter = 0;
            }
            else                    // Otherwise it's BackSpace (<-)
            {
               if (actualcase[plrnbr])
                  actualcase[plrnbr]--;
            }

         }
         else
            PlaySFX (commonvab, 3, (!plrnbr) ? 127 : 0, plrnbr ? 127 : 0);
      }

      //---------------
      bkd.Draw (db, 0);

      for (i = 0; i < 2; i++)
      {
         if (hofpos[i] < 10)
         {
            for (j = 0; j < 3; j++)
            {
               if (j != actualcase[i])
               {
                  plain = db->Get_Plain ();
                  setRGB0 (plain, 0,0,0);
                  setXYWH (plain, 33 + i * 154 + j * 34, 19, 32, 32);
                  setSemiTrans (plain, 1);
                  db->Add_Plain (plain, 9);
               }

               if (newname[i][j] != 32)
               {
                  spr = db->Get_Spr ();
                  setXY0 (spr, 33 + i * 154 + j * 34, 19);
                  setWH (spr, 32,32);
                  setUV0 (spr, ((newname[i][j] - 65) % 7) * 37,
                               ((newname[i][j] - 65) / 7) * 37);
                  spr->clut = GetClut (0, tiscclut);
                  db->Add_Spr (spr, 10);
               }

               if ((j == actualcase[i]) && (VSync (-1) & 32))
               {
                  plain = db->Get_Plain ();
                  setRGB0 (plain, 0,0,0);
                  setXYWH (plain, 33 + i * 154 + j * 34, 19, 32, 32);
                  setSemiTrans (plain, 0);
                  db->Add_Plain (plain, 9);
               }
            }
         }
      }

      for (i = 0; i < 4; i++)
      {
         for (j = 0; j < 7; j++)
         {
            spr = db->Get_Spr ();
            setXY0 (spr, j * 37 + 33, i * 37 + 73);
            setWH (spr, 32,32);
            setUV0 (spr, j * 37, i * 37);
            spr->clut = GetClut (0, tiscclut);
            db->Add_Spr (spr, 10);
         }
      }

      RECT tww = {tpx,tpy,256,256};
      drmode = db->Get_DrMode ();
      SetDrawMode (drmode, 0, 0, GetTPage (1, 0, tpx,tpy), &tww);
      db->Add_DrMode (drmode, 10);

      // Draw a red square for player 1
      if (plr[0]->Lifepnt)
      {
         coorx = plr[0]->GunX;
         coory = plr[0]->GunY;
         if ((coorx >= 31) && (coorx < 287) && (coory >= 71) && (coory < 216))
         {
            plrcase[0] = ((coory - 71) / 37) * 7 + ((coorx - 31) / 37);
            coorx = ((coorx - 31) / 37 * 37) + 33;
            coory = ((coory - 71) / 37 * 37) + 73;

            plain = db->Get_Plain ();
            setRGB0 (plain, 255,0,0);
            setXYWH (plain, coorx, coory, 32, 32);
            setSemiTrans (plain, 1);
            db->Add_Plain (plain, 9);
         }
      }
      else
         plrcase[0] = -1;

      // Draw a blue square for player 2
      if (plr[1]->Lifepnt)
      {
         coorx = plr[1]->GunX;
         coory = plr[1]->GunY;

         if ((coorx >= 31) && (coorx < 287) && (coory >= 71) && (coory < 216))
         {
            plrcase[1] = ((coory - 71) / 37) * 7 + ((coorx - 31) / 37);
            coorx = ((coorx - 31) / 37 * 37) + 33;
            coory = ((coory - 71) / 37 * 37) + 73;

            plain = db->Get_Plain ();
            setRGB0 (plain, 0,0,255);
            setXYWH (plain, coorx, coory, 32, 32);
            setSemiTrans (plain, 1);
            db->Add_Plain (plain, 9);
         }
      }
      else
         plrcase[1] = -1;
      
      Draw_Gun_Cursor (frgnd, db, 0);
      db->Swap (debug);
   }

   //----------------------------------
   for (i = 0; i < 2; i++)
   {
      if ((hofpos[i] >= 0) && (hofpos[i] < 10))
      {
         HALLFAME[hofpos[i]].Name[0] = newname[i][0];
         HALLFAME[hofpos[i]].Name[1] = newname[i][1];
         HALLFAME[hofpos[i]].Name[2] = newname[i][2];
      }
   }

   //----------------------------------
   bkd.Clear ();

   FreeClut (tiscclut);
   FreeVram (pictvrammemhandle);

   db->ClearDrawArea (0,0,0);   // Turn the screen to black
   db->Swap (0);                //
   db->Swap (0);                //
}
