/* CrackBench V1.1 © 3/4/1989 Tim Pietzcker */
/* ...und wieder eins von diesen dummen Programmen, die nichts tun als
   unbescholtenen Programmierern einen Schreck einzujagen. Naja, wenn's euch
   Spaß macht, tut damit, was ihr wollt. Am besten, jemand schreibt noch 'ne
   Soundroutine dazu (Fensterklirren !)
*/

#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <exec/types.h>


struct IntuitionBase *IntuitionBase;
struct GfxBase       *GfxBase;
struct Library       *OpenLibrary();
struct Screen        *OpenScreen(),*MyScreen;
struct Window        *OpenWindow(),*MyWindow1,*MyWindow2;
struct IntuiMessage  *GetMsg(),*msg;
struct RastPort      *rp1,*rp2;

struct NewWindow TheWindow1 =
{
   0,0,
   640,10,
   0,1,
   CLOSEWINDOW,
   ACTIVATE|WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH,
   NULL,NULL,
   (UBYTE *)"CrackBench © 4/1989 Tim Pietzcker",
   NULL,NULL,
   0,0,0,0,
   WBENCHSCREEN
};

struct NewWindow TheWindow2 =
{
   0,0,
   640,256,
   0,0,
   MOUSEBUTTONS,
   BORDERLESS|BACKDROP,
   NULL,NULL,
   NULL,
   NULL,NULL,
   0,0,0,0,
   CUSTOMSCREEN
};

struct NewScreen TheScreen =
{
   0,0,
   640,256,
   2,0,1,
   HIRES,
   CUSTOMSCREEN|SCREENBEHIND,
   NULL,
   NULL,
   NULL,
   NULL
};

#define NUMLINES 12

int anz_linien[NUMLINES];

long alte_koords[NUMLINES][2];

long richtung[NUMLINES][2] =
{
   { -10,0  },
   { -7 ,3  },
   { -10,5  },
   {   0,5  },
   {  10,5  },
   {   7,3  },
   {  10,0  },
   {  10,-5 },
   {   7,-3 },
   {   0,-5 },
   { -10,-5 },
   {  -7,-3 }
};

open_all()
{
   if ((IntuitionBase = (struct IntuitionBase *)
      OpenLibrary("intuition.library",0l)) == NULL) get_out();
   
   if ((GfxBase = (struct GfxBase *)
      OpenLibrary("graphics.library",0l)) == NULL) get_out();

   if ((MyScreen = OpenScreen(&TheScreen)) == NULL) get_out();

   TheWindow2.Screen = MyScreen;
   
   if ((MyWindow2 = OpenWindow(&TheWindow2)) == NULL) get_out();
   rp2 = MyWindow2->RPort;
   
   if ((MyWindow1 = OpenWindow(&TheWindow1)) == NULL) get_out();
   rp1 = &MyWindow1->WScreen->RastPort;
   /* RastPort der Workbench (nicht des Windows !!) */
}

get_out()
{
   if (MyWindow1)     CloseWindow(MyWindow1);
   if (MyWindow2)     CloseWindow(MyWindow2);
   if (MyScreen)      CloseScreen(MyScreen);
   if (GfxBase)       CloseLibrary(GfxBase);
   if (IntuitionBase) CloseLibrary(IntuitionBase);
   exit();
}

copy_workbench()
{
   ULONG sec,mic;

   SetDrMd(rp2,JAM1);
   SetAPen(rp2,2l);
   ClipBlit(rp1,0l,0l,rp2,0l,0l,640l,256l,(long)0xC0);
   ShowTitle(MyScreen,FALSE);
   ScreenToFront(MyScreen);
   CurrentTime(&sec,&mic);
   srand((USHORT)sec);
}

wait_events()
{
   do
   {
      Wait(1l<<MyWindow1->UserPort->mp_SigBit | 
           1l<<MyWindow2->UserPort->mp_SigBit);

      if (msg=GetMsg(MyWindow1->UserPort))
      {
         /* CLOSEWINDOW */
         ReplyMsg(msg);
         get_out();
      }
      if (msg=GetMsg(MyWindow2->UserPort))
      {
         switch (msg->Code)
         {
            case SELECTDOWN:
               crack((long)msg->MouseX,(long)msg->MouseY);
               break;
	    default:
	       break;
         }
         ReplyMsg(msg);
      }
   } while (TRUE);
}

crack(x,y)
long x,y;
{
   int i,j;
   for (i=0;i<NUMLINES;i++)
   {
      alte_koords[i][0] = x;
      alte_koords[i][1] = y;
      anz_linien[i] = (int)(rand()%6 + 5);
   }
   
   for (j=0;j<=10;j++)
      for (i=0;i<NUMLINES;i++)
      {
	 if (anz_linien[i] >= j)
	 {
            Move(rp2,alte_koords[i][0],alte_koords[i][1]);
	    alte_koords[i][0] += richtung[i][0] +(long)(rand()%20 - 10);
	    alte_koords[i][1] += richtung[i][1]+ (long)(rand()%10 - 5);
            Draw(rp2,alte_koords[i][0],alte_koords[i][1]);
	 }
      }
}

main()
{
   open_all();
   copy_workbench();
   wait_events();
}
