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

 Line 1.2  (Client for BServer)

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

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

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <clib/alib_protos.h>
#include <stdlib.h>
#include <time.h>

#include "/include/client.h"

char *ver = "$VER: Line 1.2 "__AMIGADATE__;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

struct DisplayIDInformation *dinfo;

#define LINENUM 40
#define LASTLINE 39

struct { WORD FromX, FromY, ToX, ToY; } Line[LINENUM];
WORD IncX, IncY, IncTX, IncTY;

extern ULONG RangeSeed;

void DrawLine( void )
{
UWORD n, swidth, sheight;
struct Rectangle *rect;
register ULONG total_Line = 0;
UBYTE r, g, b, brightness;
struct Screen *scr;

rect = GETTXTOSCANRECT(dinfo);

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

if ( scr = OpenScreenTags( NULL,
	SA_DisplayID, DISPLAYID( dinfo ),
	SA_Width, swidth,
	SA_Height, sheight,
	SA_Left, (RECTANGLEWIDTH(rect) - swidth)/2,
	SA_Top, 0,
	SA_Depth, 1,
	SA_Overscan, OSCAN_TEXT,
	SA_Type, CUSTOMSCREEN,
	SA_Quiet, TRUE,
	TAG_END ) )
	{
	register struct ViewPort *vp = &(scr->ViewPort);
	register struct RastPort *rp = &(scr->RastPort);

	SpritesOff();

	r = 6*brightness/100; g = 7*brightness/100; b = 9*brightness/100;
	SetRGB4( vp, 0, 0, 0, 0 );
	SetRGB4( vp, 1, r, g, b );
	SetAPen( rp, 1 );
	SetDrMd( rp, COMPLEMENT );

	Line[0].FromX = RangeRand( swidth );
	Line[0].FromY = RangeRand( sheight );
	Line[0].ToX = RangeRand( swidth - Line[0].FromX + 4);
	Line[0].ToY = RangeRand( sheight - Line[0].FromY + 4);

	IncX = RangeRand( 40 ) - 19;
	IncY = RangeRand( 40 ) - 19;
	IncTX = RangeRand( 40 ) - 19;
	IncTY = RangeRand( 40 ) - 19;

	while( STILL_BLANKING )
		{
		WaitTOF();

		if ( total_Line++ > LASTLINE )
			{
			Move( rp, Line[LASTLINE].FromX, Line[LASTLINE].FromY );
			Draw( rp, Line[LASTLINE].ToX, Line[LASTLINE].ToY );
			}

		for ( n = LASTLINE; n; n-- )
			{
			Line[n].FromX = Line[n-1].FromX;
			Line[n].FromY = Line[n-1].FromY;
			Line[n].ToX = Line[n-1].ToX;
			Line[n].ToY = Line[n-1].ToY;
			}

		if ( Line[0].FromX + IncX <= 0 )
			{
			Line[0].FromX = 0;
			IncX = RangeRand( 20 ) + 1;
			}
		else
		if ( Line[0].FromX + IncX >= swidth )
			{
			Line[0].FromX = swidth - 1;
			IncX = RangeRand( 20 ) * (-1) - 1;
			}
		else
			Line[0].FromX += IncX;

		if ( Line[0].FromY + IncY < 0 )
			{
			Line[0].FromY = 0;
			IncY = RangeRand( 20 ) + 1;
			}
		else
		if ( Line[0].FromY + IncY >= sheight )
			{
			Line[0].FromY = sheight - 1;
			IncY = RangeRand( 20 ) * (-1) - 1;
			}
		else
			Line[0].FromY += IncY;

		if ( Line[0].ToX + IncTX <= 0 )
			{
			Line[0].ToX = 0;
			IncTX = RangeRand( 20 ) + 1;
			}
		else
		if ( Line[0].ToX + IncTX >= swidth )
			{
			Line[0].ToX = swidth - 1;
			IncTX = RangeRand( 20 ) * (-1) - 1;
			}
		else
			Line[0].ToX += IncTX;

		if ( Line[0].ToY + IncTY < 0 )
			{
			Line[0].ToY = 0;
			IncTY = RangeRand( 20 ) + 1;
			}
		else
		if ( Line[0].ToY + IncTY >= sheight )
			{
			Line[0].ToY = sheight - 1;
			IncTY = RangeRand( 20 ) * (-1) - 1;
			}
		else
			Line[0].ToY += IncTY;

		Move( rp, Line[0].FromX, Line[0].FromY );
		Draw( rp, Line[0].ToX, Line[0].ToY );

		r += RangeRand( 3 ) - 1; if ( r < 0 ) r = 0; if ( r > 15 ) r = 15;
		g += RangeRand( 3 ) - 1; if ( g < 0 ) g = 0; if ( g > 15 ) g = 15;
		b += RangeRand( 3 ) - 1; if ( b < 0 ) b = 0; if ( b > 15 ) b = 15;
		SetRGB4( vp, 1, r*brightness/100, g*brightness/100, b*brightness/100 );
		}

	CloseScreen( scr );
	SpritesOn();
	}
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 ( dinfo = OpenCommunication() )
			{
			RangeSeed = time( NULL );
			DrawLine();
			CloseCommunication( dinfo );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
