/**************************| Pyth.c |*********** 20 July 1989 *****/
/*                                                                */
/*  Pyth v1.1: (c) 7/89 by Andreas Krebs                          */
/*                         Mittelweg 9                            */
/*                         7110 Oehringen 2                       */
/*                                                                */
/*                                                                */
/*            This programm ist Public-Domain.                    */
/*                                                                */
/* Compile commands: (for Aztec-C v3.6a)                          */
/*                   cc Pyth                                      */
/*                   ln Pyth -lc -lm                              */
/*                                                                */
/******************************************************************/

#include <exec/types.h>
#include <intuition/intuition.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <libraries/mathffp.h>

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct MathBase *MathBase;
struct MathTransBase *MathTransBase;

struct Screen *PythScreen;
struct Window *PythWindow;
static struct RastPort *rp;

USHORT Kords[21][3][2];
USHORT Box[4][2];

struct NewScreen NewPythScreen =
{
  0,0,
  640,400,
  3,
  1,0,
  HIRES|LACE,
  CUSTOMSCREEN,
  NULL,
  NULL,
  NULL,
  NULL
};

struct NewWindow NewPythWindow =
{
  0,10,
  640,390,
  1,0,
  CLOSEWINDOW,
  WINDOWCLOSE|ACTIVATE|BORDERLESS,
  NULL,
  NULL,
  (UBYTE *)"Pyth v1.1 (c) 7/89 by Andreas Krebs",
  NULL,
  NULL,
  0,0,
  0,0,
  CUSTOMSCREEN
};

main()
{
  OpenALL();

  SetRGB4(&PythScreen->ViewPort,3L,15L,0L,0L);
  SetRGB4(&PythScreen->ViewPort,4L,0L,15L,15L);
  SetRGB4(&PythScreen->ViewPort,5L,0L,15L,0L);

  DrawPyth();

  Wait(1L << PythWindow->UserPort->mp_SigBit);

  CloseALL();
}

OpenALL()
{
  void *OpenLibrary();
  struct Screen *OpenScreen();
  struct Window *OpenWindow();

  if(!(IntuitionBase=(struct IntuitionBase *)
       OpenLibrary("intuition.library",0L)))
  {
    puts("Can't open intuition library !");
    exit(FALSE);
  }

  if(!(GfxBase=(struct GfxBase *)
       OpenLibrary("graphics.library",0L)))
  {
    puts("Can't open graphics library !");
    CloseALL();
    exit(FALSE);
  }

  if(!(MathBase=(struct MathBase *)
       OpenLibrary("mathffp.library",0L)))
  {
    puts("Can't open mathffp library !");
    CloseALL();
    exit(FALSE);
  }

  if(!(MathTransBase=(struct MathTransBase *)
       OpenLibrary("mathtrans.library",0L)))
  {
    puts("Can't open mathtrans library !");
    CloseALL();
    exit(FALSE);
  }

  if(GfxBase->DisplayFlags & (UWORD)PAL)
  {
    NewPythScreen.Height=512;
    NewPythWindow.Height=502;
  }

  if(!(PythScreen=(struct Screen *)
       OpenScreen(&NewPythScreen)))
  {
    puts("Can't open Screen !");
    CloseALL();
    exit(FALSE);
  }

  NewPythWindow.Screen=PythScreen;

  if(!(PythWindow=(struct Window *)
       OpenWindow(&NewPythWindow)))
  {
    puts("Can't open Window !");
    CloseALL();
    exit(FALSE);
  }

  rp=PythWindow->RPort;
}

CloseALL()
{
  if(PythWindow) CloseWindow(PythWindow);
  if(PythScreen) CloseScreen(PythScreen);
  if(MathBase) CloseLibrary(MathBase);
  if(MathTransBase) CloseLibrary(MathTransBase);
  if(GfxBase) CloseLibrary(GfxBase);
  if(IntuitionBase) CloseLibrary(IntuitionBase);
}

DrawPyth()
{
  USHORT Seite[21],t;
  SHORT x=0,y=0,Nr=0,ax,ay;
  int sqr,sx,sy;
  FLOAT a,b,c,h,h2,e,f,wia1,wia2,wib,abx,aby,abx2,aby2;

  for(t=0;t<21;t++)
  {
    Seite[t]=0;
  }

  SetAPen(rp,5L);

  if(GfxBase->DisplayFlags & (UWORD)PAL)
  {
    Move(rp,320L,500L);
    Draw(rp,320L,400L);
    Draw(rp,220L,400L);
    Draw(rp,220L,500L);
    Draw(rp,320L,500L);

    Kords[0][0][0]=320;
    Kords[0][0][1]=400;
    Kords[0][1][0]=256;
    Kords[0][1][1]=352;
    Kords[0][2][0]=220;
    Kords[0][2][1]=400;
  }
  else
  {
    Move(rp,310L,388L);
    Draw(rp,310L,308L);
    Draw(rp,230L,308L);
    Draw(rp,230L,388L);
    Draw(rp,310L,388L);

    Kords[0][0][0]=310;
    Kords[0][0][1]=308;
    Kords[0][1][0]=259;
    Kords[0][1][1]=270;
    Kords[0][2][0]=230;
    Kords[0][2][1]=308;
  }

  DrawDreieck(0);

  do
  {
    Nr++;

    x=Kords[Nr-1][0][0]-Kords[Nr-1][1][0];
    y=Kords[Nr-1][0][1]-Kords[Nr-1][1][1];
    sqr=x*x+y*y;
    a=SPSqrt((FLOAT)(sqr));

    if(x==0.0 || x==-0.0)
      wia1=PI/2;
    else
      wia1=SPAtan((FLOAT)y/(FLOAT)x);

    if(x<=0.0 || x<=-0.0)
      wia1+=PI;

    x=Kords[Nr-1][1][0]-Kords[Nr-1][2][0];
    y=Kords[Nr-1][1][1]-Kords[Nr-1][2][1];
    sqr=x*x+y*y;
    b=SPSqrt((FLOAT)(sqr));

    if(x==0.0 || x==-0.0)
      wia2=PI/2.0;
    else
      wia2=-SPAtan((FLOAT)y/(FLOAT)x);

    if(x<=0.0 || x<=-0.0)
      wia2+=PI;

    x=Kords[Nr-1][2][0]-Kords[Nr-1][0][0];
    y=Kords[Nr-1][2][1]-Kords[Nr-1][0][1];
    sqr=x*x+y*y;
    c=SPSqrt((FLOAT)(sqr));

    h=a*0.6;   /*   a*sin(36.9°)   */
    e=a*0.8;   /*   a*sin(53.1°)   */
    f=c-e;

    if(Seite[Nr]==1)
    {
      abx=-b*SPSin(wia2);
      aby=b*SPCos(wia2);
    }
    else
    {
      abx=a*SPSin(wia1);
      aby=a*SPCos(wia1);
    }

    Box[0][0]=Kords[Nr-1][Seite[Nr]][0];
    Box[0][1]=Kords[Nr-1][Seite[Nr]][1];
    Box[1][0]=Kords[Nr-1][Seite[Nr]][0]+(SHORT)abx;
    Box[1][1]=Kords[Nr-1][Seite[Nr]][1]-(SHORT)aby;
    Box[2][0]=Kords[Nr-1][Seite[Nr]+1][0]+(SHORT)abx;
    Box[2][1]=Kords[Nr-1][Seite[Nr]+1][1]-(SHORT)aby;
    Box[3][0]=Kords[Nr-1][Seite[Nr]+1][0];
    Box[3][1]=Kords[Nr-1][Seite[Nr]+1][1];

    if(Seite[Nr]==1)
      SetAPen(rp,3L);
    else
      SetAPen(rp,5L);

    DrawBox();

    Kords[Nr][0][0]=Box[1][0];
    Kords[Nr][0][1]=Box[1][1];
    Kords[Nr][2][0]=Box[2][0];
    Kords[Nr][2][1]=Box[2][1];

    sx=(int)Box[Seite[Nr]+1][0];
    sy=(int)Box[Seite[Nr]+1][1];

    if(Seite[Nr]==1)
    {
      wib=wia2+0.927295;
      wib=PI-wib;

      aby2=f*SPSin(wib);
      abx2=f*SPCos(wib);
    }
    else
    {
      wib=wia1+0.6435011;

      aby2=e*SPSin(wib);
      abx2=e*SPCos(wib);
    }

    ax=(SHORT)abx2;
    ay=(SHORT)aby2;

    sx-=ax;
    sy-=ay;

    Kords[Nr][1][0]=(USHORT)sx;
    Kords[Nr][1][1]=(USHORT)sy;

    DrawDreieck(Nr);

    if((b<=3.0 && a<=4.0 && c<=5.0) || Nr>=20)
    {
      for(t=(Nr+1);t<21;t++)
      {
        Seite[t]=0;
      }

      while(Seite[Nr]==1)
      {
        Seite[Nr]=0;
        Nr--;
        if(Nr<1)
          break;
      }
      Seite[Nr]=1;
      Nr--;
    }

    if(GetMsg(PythWindow->UserPort))
    {
      CloseALL();
      exit(TRUE);
    }
  }while(Nr>-1);
}


DrawDreieck(Nr)
USHORT Nr;
{
  SetAPen(rp,4L);

  Move(rp,(LONG)Kords[Nr][2][0],(LONG)Kords[Nr][2][1]);

  PolyDraw(rp,3L,&Kords[Nr][0][0]);
}

DrawBox()
{
  Move(rp,(LONG)Box[3][0],(LONG)Box[3][1]);
  
  PolyDraw(rp,4L,&Box[0][0]);
}

