/* ******************************************************************** */
/*                                game.c                                */
/*                                                                      */
/* December 4, 1988: After a few months spent over C exercises and very */
/* simple programs based on various examples, this is my FIRST PROGRAM  */
/* and though it's apparently a cards game, actually it's just another  */
/* exercise that deals with screens, windows, menus and requesters...   */
/*                                                                      */
/* HISTORY              DESCRIPTION                                     */
/* -------              -----------                                     */
/* 29-Jan-89            clean-up for release                            */
/* 25-Jan-89            fixed minor bugs                                */
/* 21-Jan-89            amigamoves.c completed                          */
/* 12-Jan-89            game ready (in two players mode)                */
/* 09-Dec-89            drawn all cards                                 */
/* 04-Dec-89            started game programming                        */
/*                                                                      */
/* ******************************************************************** */



#include <intuition/intuition.h>
#include <graphics/gfxmacros.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <stdlib.h>

#include "mydefines.h"
#include "myscreen.h"
#include "mywindow.h"
#include "menustuff.h"
#include "event.c"
#include "menupick.c"
#include "shufflecards.c"
#include "displaycards.c"
#include "intascii.c"
#include "displaydata.c"
#include "gameloop.c"
#include "update.c"
#include "deletecard.c"
#include "writestring.c"
#include "initrequesters.c"
#include "amigamoves.c"

struct GfxBase          *GfxBase;
struct IntuitionBase    *IntuitionBase;

struct Screen           *OpenScreen();
struct Screen           *s;
struct ViewPort         *vp;
struct Window           *OpenWindow();
struct Window           *w;
struct RastPort         *rp;
struct Remember         *key;

UWORD mycolortable[] =
   {
   0x0000, 0x0eca, 0x0006, 0x0aaa, 0x0fb5, 0x0fd7, 0x0090, 0x0070,
   0x01a4, 0x0aa0, 0x02cf, 0x069c, 0x0f66, 0x0f88, 0x0fbb, 0x0c9f,
   0x0850, 0x0e52, 0x0a52, 0x0fca, 0x0f44, 0x0f88, 0x0fa2, 0x0fd4,
   0x0888, 0x0999, 0x0aa9, 0x0bba, 0x0eed, 0x0fff, 0x069d, 0x0a00
   };

UWORD mycardsposition[] = {0,          /* This one is not really used! */
      1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
      10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16};

int showtime = 30;                     /* How long to diplay the deck */
int peektime = 12;                     /* How long you can peek */
int shuffles = 40;                     /* How many times to shuffle */

int memory = 2;                        /* How many moves to remember */
int percentage = 10;                   /* % of peeking by computer */
int enemy = TRUE;                      /* My enemy (TRUE: Amiga) */

int turn1 = 0;
int turn2 = 0;
int score1 = 0;
int score2 = 0;
int peek1 = 0;
int peek2 = 0;

int selectbutton;
int currentmousex;
int currentmousey;

struct IntuiMessage *mess;

_main()
   {
   extern USHORT mycards[];            /* image data */
   extern struct Image mycardsimage[]; /* Image structures */

   extern USHORT naahdataoff[];
   extern struct Image naahimageoff;
   extern USHORT naahdataon[];
   extern struct Image naahimageon;
   extern struct Gadget naahgadget;

   extern USHORT yeahdataoff[];
   extern struct Image yeahimageoff;
   extern USHORT yeahdataon[];
   extern struct Image yeahimageon;
   extern struct Gadget yeahgadget;

   extern USHORT clickdataoff[];
   extern struct Image clickimageoff;
   extern USHORT clickdataon[];
   extern struct Image clickimageon;
   extern struct Gadget clickgadget;

   UWORD *ptrold;                      /* Pointer to CHIP MEMORY */
   UWORD *ptrnew;                      /* Working pointer to CHIP MEMORY */

   int i;
   int retvalue = DUMMY_VALUE;


   key = NULL;

   if(!(GfxBase = (struct GfxBase *)
   OpenLibrary("graphics.library", 0L)))
      {
      Write(Output(), "\nERROR: Unable to open graphics.library!\n\n", 42);
      cleanup();
      Exit(10);
      }

   if(!(IntuitionBase = (struct IntuitionBase *)
   OpenLibrary("intuition.library", 0L)))
      {
      Write(Output(), "\nERROR: Unable to open intuition.library!\n\n", 43);
      cleanup();
      Exit(20);
      }

   if(!(s = OpenScreen(&myscreen)))
      {
      Write(Output(), "\nERROR: Unable to open screen <myscreen>!\n\n", 43);
      cleanup();
      Exit(30);
      }
   mywindow.Screen = s;
   vp = &(s -> ViewPort);
   LoadRGB4(vp, &mycolortable[0], 32);

   if(!(w = OpenWindow(&mywindow)))
      {
      Write(Output(), "\nERROR: Unable to open window <mywindow>!\n\n", 43);
      cleanup();
      Exit(40);
      }
   rp = w -> RPort;



/* Allocate enough (CHIP) memory for image data */

   if(!(ptrold = (UWORD *)AllocRemember(&key, ARRAYSIZE * 2, MEMF_CHIP)))
      {
      Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
      cleanup();
      Exit(50);
      }
   ptrnew = ptrold;

/* Now I want to move the image data into CHIP MEMORY */

   for (i = 0; i < ARRAYSIZE; i++)
      *ptrnew++ = mycards[i];
   ptrnew = ptrold;

/* Initialize all the Image structures */

   for (i = 0; i < CARDNUM; i++)
      mycardsimage[i].ImageData = ptrnew + CARDSIZE * i;



/* Allocate enough (CHIP) memory for image data (gadgets) */

   if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
      {
      Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
      cleanup();
      Exit(60);
      }
   ptrnew = ptrold;

/* Now I want to move the image data into CHIP MEMORY */

   for (i = 0; i < PEEKIMAGESIZE; i++)
      *ptrnew++ = naahdataoff[i];
   ptrnew = ptrold;

/* Initialize the Image structures */

      naahimageoff.ImageData = ptrnew;

/* Allocate enough (CHIP) memory for image data (gadgets) */

   if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
      {
      Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
      cleanup();
      Exit(70);
      }
   ptrnew = ptrold;

/* Now I want to move the image data into CHIP MEMORY */

   for (i = 0; i < PEEKIMAGESIZE; i++)
      *ptrnew++ = naahdataon[i];
   ptrnew = ptrold;

/* Initialize the Image structures */

      naahimageon.ImageData = ptrnew;

/* Allocate enough (CHIP) memory for image data (gadgets) */

   if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
      {
      Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
      cleanup();
      Exit(80);
      }
   ptrnew = ptrold;

/* Now I want to move the image data into CHIP MEMORY */

   for (i = 0; i < PEEKIMAGESIZE; i++)
      *ptrnew++ = yeahdataoff[i];
   ptrnew = ptrold;

/* Initialize the Image structures */

      yeahimageoff.ImageData = ptrnew;

/* Allocate enough (CHIP) memory for image data (gadgets) */

   if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
      {
      Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
      cleanup();
      Exit(90);
      }
   ptrnew = ptrold;

/* Now I want to move the image data into CHIP MEMORY */

   for (i = 0; i < PEEKIMAGESIZE; i++)
      *ptrnew++ = yeahdataon[i];
   ptrnew = ptrold;

/* Initialize the Image structures */

      yeahimageon.ImageData = ptrnew;

/* Allocate enough (CHIP) memory for image data (gadgets) */

   if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
      {
      Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
      cleanup();
      Exit(100);
      }
   ptrnew = ptrold;

/* Now I want to move the image data into CHIP MEMORY */

   for (i = 0; i < PEEKIMAGESIZE; i++)
      *ptrnew++ = clickdataoff[i];
   ptrnew = ptrold;

/* Initialize the Image structures */

      clickimageoff.ImageData = ptrnew;

/* Allocate enough (CHIP) memory for image data (gadgets) */

   if(!(ptrold = (UWORD *)AllocRemember(&key, PEEKIMAGESIZE * 2, MEMF_CHIP)))
      {
      Write(Output(), "\nERROR: Unable to move data...\n\n", 32);
      cleanup();
      Exit(110);
      }
   ptrnew = ptrold;

/* Now I want to move the image data into CHIP MEMORY */

   for (i = 0; i < PEEKIMAGESIZE; i++)
      *ptrnew++ = clickdataon[i];
   ptrnew = ptrold;

/* Initialize the Image structures */

      clickimageon.ImageData = ptrnew;



/* And finally we drop into the main loop!!! */

   initrequesters();
   SetRast(rp, 6);
   SetMenuStrip(w, &mygamemenu[0]);

   while(1)
      {
      retvalue = waitformess();
      if (retvalue == PICKED_START)
         gameloop();
      }
   return(0);
   }



cleanup()
   {
   ClearMenuStrip(w);
   if (key)
      FreeRemember(&key, TRUE);
   if (w)
      CloseWindow(w);
   if (s)
      CloseScreen(s);
   if (IntuitionBase)
      CloseLibrary(IntuitionBase);
   if (GfxBase)
      CloseLibrary(GfxBase);
   return(0);
   }



waitformess()
   {
   int value;

   mess = (struct IntuiMessage *)GetMsg(w -> UserPort);
   if (mess == NULL)
      Wait(1 << w -> UserPort -> mp_SigBit);
   else
      value = handleevent(mess);
   return(value);
   }
