;/*
sc RESOPT IGNORE=73 DATA=NEAR NMINC UCHAR CONSTLIB STREQ STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE Poly.c
Slink from LIB:catch.o Poly.o to //Clients/Poly LIB LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD NOICONS STRIPDEBUG
delete Poly.o
quit

 Poly 1.0  (Client for BServer)

 Copyright © 1995 by Stefano Reksten of 3AM - The Three Amigos!!!
 All rights reserved.

*/

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

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/layers.h>
#include <clib/alib_protos.h>
#include <string.h>
#include <time.h>
#include <math.h>

#include "/include/client.h"

char *ver = "$VER: Poly 1.0 "__AMIGADATE__;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Library *LayersBase;

struct DisplayIDInformation *dinfo;

struct Screen *scr;

#define MOREEDGES	6
#define MINEDGES	6
#define SCRDEPTH	4
#define NUMCOLORS	16

extern ULONG RangeSeed;
UWORD swidth, sheight, maxw, maxh;
UBYTE brightness;

/* Get a random color in r g b... */

UBYTE r, g, b;

UBYTE RandomColor( UBYTE rng )
{
if ( CheckAA() )
	return( (UBYTE)((RangeRand(rng<<4)+256-(rng<<4))*brightness/100) );
else
	return( (UBYTE)((RangeRand(rng)+16-rng)*brightness/100) );
}

void SetRGBTriplet( void )
{
r = RandomColor(16);
g = RandomColor(16);
b = RandomColor(16);
}

signed char costable[450] = {
  127,   127,   127,   127,   127,   127,   127,   127,   127,   127, 
  126,   126,   126,   125,   125,   124,   124,   123,   122,   122, 
  121,   120,   119,   119,   118,   117,   116,   115,   114,   113, 
  112,   111,   110,   109,   107,   106,   105,   104,   102,   101, 
   99,    98,    97,    95,    94,    92,    91,    89,    87,    86, 
   84,    82,    81,    79,    77,    75,    73,    72,    70,    68, 
   66,    64,    62,    60,    58,    56,    54,    52,    50,    48, 
   46,    44,    42,    40,    37,    35,    33,    31,    29,    27, 
   24,    22,    20,    18,    16,    13,    11,     9,     7,     4, 
    2,     0,    -2,    -4,    -7,    -9,   -11,   -13,   -16,   -18, 
  -20,   -22,   -24,   -27,   -29,   -31,   -33,   -35,   -37,   -40, 
  -42,   -44,   -46,   -48,   -50,   -52,   -54,   -56,   -58,   -60, 
  -62,   -64,   -66,   -68,   -70,   -72,   -73,   -75,   -77,   -79, 
  -81,   -82,   -84,   -86,   -87,   -89,   -91,   -92,   -94,   -95, 
  -97,   -98,   -99,  -101,  -102,  -104,  -105,  -106,  -107,  -109, 
 -110,  -111,  -112,  -113,  -114,  -115,  -116,  -117,  -118,  -119, 
 -119,  -120,  -121,  -122,  -122,  -123,  -124,  -124,  -125,  -125, 
 -126,  -126,  -126,  -127,  -127,  -127,  -128,  -128,  -128,  -128, 
 -128,  -128,  -128,  -128,  -128,  -128,  -128,  -127,  -127,  -127, 
 -126,  -126,  -126,  -125,  -125,  -124,  -124,  -123,  -122,  -122, 
 -121,  -120,  -119,  -119,  -118,  -117,  -116,  -115,  -114,  -113, 
 -112,  -111,  -110,  -109,  -107,  -106,  -105,  -104,  -102,  -101, 
  -99,   -98,   -97,   -95,   -94,   -92,   -91,   -89,   -87,   -86, 
  -84,   -82,   -81,   -79,   -77,   -75,   -73,   -72,   -70,   -68, 
  -66,   -64,   -62,   -60,   -58,   -56,   -54,   -52,   -50,   -48, 
  -46,   -44,   -42,   -40,   -37,   -35,   -33,   -31,   -29,   -27, 
  -24,   -22,   -20,   -18,   -16,   -13,   -11,    -9,    -7,    -4, 
   -2,    -0,     2,     4,     7,     9,    11,    13,    16,    18, 
   20,    22,    24,    27,    29,    31,    33,    35,    37,    40, 
   42,    44,    46,    48,    50,    52,    54,    56,    58,    60, 
   62,    64,    66,    68,    70,    72,    73,    75,    77,    79, 
   81,    82,    84,    86,    87,    89,    91,    92,    94,    95, 
   97,    98,    99,   101,   102,   104,   105,   106,   107,   109, 
  110,   111,   112,   113,   114,   115,   116,   117,   118,   119, 
  119,   120,   121,   122,   122,   123,   124,   124,   125,   125, 
  126,   126,   126,   127,   127,   127,   127,   127,   127,   127, 
  127,   127,   127,   127,   127,   127,   127,   127,   127,   127, 
  126,   126,   126,   125,   125,   124,   124,   123,   122,   122, 
  121,   120,   119,   119,   118,   117,   116,   115,   114,   113, 
  112,   111,   110,   109,   107,   106,   105,   104,   102,   101, 
   99,    98,    97,    95,    94,    92,    91,    89,    87,    86, 
   84,    82,    81,    79,    77,    75,    73,    72,    70,    68, 
   66,    64,    62,    60,    58,    56,    54,    52,    50,    48, 
   46,    44,    42,    40,    37,    35,    33,    31,    29,    27, 
   24,    22,    20,    18,    16,    13,    11,     9,     7,     4 };

UWORD coord[MOREEDGES + MINEDGES][2];
UBYTE colors[NUMCOLORS][3];
UWORD max_radius;
UWORD min_radius;


void DrawAPolygon( UBYTE color )
{
UWORD x, y;
UBYTE n, m, edges;
UBYTE radius;
register struct RastPort *rp = &scr->RastPort;

radius = RangeRand( max_radius - min_radius ) + min_radius;

x = RangeRand( swidth - 2 * radius ) + radius;
y = RangeRand( sheight - 2 * radius ) + radius;

edges = RangeRand( MOREEDGES ) + MINEDGES;

for ( n = 0; n < edges; n++ )
	{
	coord[n][0] = x + radius * costable[ (360 * n) / edges ] / 128;
	coord[n][1] = y + radius * costable[ 90 + ((360 * n) / edges) ] / 128;
	}

SetRGBTriplet();
if ( CheckAA() )
	SetRGB32( &scr->ViewPort, color, r<<24, g<<24, b<<24 );
else
	SetRGB4( &scr->ViewPort, color, r, g, b );
colors[color][0] = r;
colors[color][1] = g;
colors[color][2] = b;

SetAPen( rp, color );
for ( n = 0; n < edges; n++ )
	for ( m = n; m < edges; m++ )
		{
		Move( rp, coord[n][0], coord[n][1] );
		Draw( rp, coord[m][0], coord[m][1] );
		}
}


#define POLY_ACTION_DRAWING	1
#define POLY_ACTION_CLEARING 2

void Blank( void )
{
UBYTE whichColor, poly_action, fade_delay;
BOOL faded, aa = CheckAA();
ULONG displayID;
struct Rectangle *rect;

rect = GETTXTOSCANRECT(dinfo);

swidth = RECTANGLEWIDTH(rect);
sheight = RECTANGLEHEIGHT(rect);
displayID = DISPLAYID(dinfo);
brightness = GETBRIGHTNESS(dinfo);

if ( !CheckAA() )
	{
	if ( displayID & SUPERHIRES )
		{
		displayID &= ~SUPERHIRES;
		displayID |= HIRES;
		}
	}

if ( (scr = OpenScreenTags( NULL,
		SA_Width, swidth,
		SA_Height, sheight,
		SA_Depth, SCRDEPTH,
		SA_Type, CUSTOMSCREEN,
		SA_Quiet, TRUE,
		SA_DisplayID, displayID,
		SA_Overscan, OSCAN_TEXT,
		TAG_END ) ) )
	{
	SpritesOff();
	SetRGB4( &scr->ViewPort, 0, 0, 0, 0 );

	max_radius = swidth > sheight ? (sheight >> 2) : (swidth >> 2);
	min_radius = max_radius >> 2;
	poly_action = POLY_ACTION_DRAWING;
	whichColor = 1;
	fade_delay = 0;

	while( STILL_BLANKING )
		{
		if ( poly_action == POLY_ACTION_DRAWING )
			{
			DrawAPolygon( whichColor );
			if ( ++whichColor >= NUMCOLORS )
				poly_action = POLY_ACTION_CLEARING;
			}
		else
			{
			faded = TRUE;
			WaitTOF();
			if ( ++fade_delay == 16 || aa )
				{
				fade_delay = 0;
				for ( whichColor = 1; whichColor < NUMCOLORS; whichColor++ )
					{
					if ( r = colors[whichColor][0] ) colors[whichColor][0]--;
					if ( g = colors[whichColor][1] ) colors[whichColor][1]--;
					if ( b = colors[whichColor][2] ) colors[whichColor][2]--;

					if ( r || g || b )
						{
						if ( aa )
							SetRGB32( &scr->ViewPort, whichColor, r<<24, g<<24, b<<24 );
						else
							SetRGB4( &scr->ViewPort, whichColor, r, g, b );
						faded = FALSE;
						}
					}

				if ( faded )
					{
					SetRast( &scr->RastPort, 0 );
					poly_action = POLY_ACTION_DRAWING;
					whichColor = 1;
					}
				}
			}
		}

	SpritesOn();
	CloseScreen( scr );
	}
else
	SendClientMsg( ACTION_FAILED );
}


void __main( char *line )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
		{
		if ( LayersBase = OpenLibrary( "layers.library", 37L ) )
			{
			if ( dinfo = OpenCommunication() )
				{
				RangeSeed = time( NULL );
				Blank();
				CloseCommunication( dinfo );
				}
			CloseLibrary( LayersBase );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
